diff options
author | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2017-02-18 07:46:26 +0300 |
---|---|---|
committer | Alexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com> | 2017-02-18 07:46:26 +0300 |
commit | ea1d6b238296d5860041c4c366c7cd8864483fee (patch) | |
tree | 13bbf9efcb39567ff31198f89b8a6ba9d5bb68c6 | |
parent | e72a6f16fba4110a8fd191364b85f6ceb02aff3d (diff) | |
parent | c4d9e3a9248d199fe63b078bdc5d2557089a9599 (diff) |
Merge branch 'master' of https://github.com/ClusterM/retroarch-clover
-rw-r--r-- | fbgrab-1.3/COPYING | 341 | ||||
-rw-r--r-- | fbgrab-1.3/INSTALL | 20 | ||||
-rw-r--r-- | fbgrab-1.3/Makefile | 19 | ||||
-rw-r--r-- | fbgrab-1.3/fbgrab.c | 598 | ||||
-rwxr-xr-x | makemod.sh | 11 |
5 files changed, 989 insertions, 0 deletions
diff --git a/fbgrab-1.3/COPYING b/fbgrab-1.3/COPYING new file mode 100644 index 0000000..a52b16e --- /dev/null +++ b/fbgrab-1.3/COPYING @@ -0,0 +1,341 @@ + + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) 19yy <name of author> + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + <signature of Ty Coon>, 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/fbgrab-1.3/INSTALL b/fbgrab-1.3/INSTALL new file mode 100644 index 0000000..ca2b841 --- /dev/null +++ b/fbgrab-1.3/INSTALL @@ -0,0 +1,20 @@ + +requirements to build: +you need these on your system: +libpng +libz + +to build: +run +make + +to install +run +make install + +Troubleshooting +If you don't realize how to install splint, remove that row from the Makefile. +You propably have the libs already installed but if not try to get the from your +distribution and secondly from: +zlib : http://www.gzip.org/zlib/ +libpng : http://www.libpng.org/ diff --git a/fbgrab-1.3/Makefile b/fbgrab-1.3/Makefile new file mode 100644 index 0000000..4b90ac3 --- /dev/null +++ b/fbgrab-1.3/Makefile @@ -0,0 +1,19 @@ +### +### I think it's not worth to make such a small project +### modular. So this is a simple gnu Makefile... +### + +NES_ROOT = /home/cluster/nesmini +LIBPNG = ../../libpng.arm +ZLIB = ../../zlib + +CFLAGS-NES += -L$(NES_ROOT)/lib -L$(NES_ROOT)/usr/lib -I$(LIBPNG) -I$(ZLIB) -Wno-unused-function -Wl,--dynamic-linker=/lib/ld-linux-armhf.so.3,-sysroot=$(NES_ROOT),-rpath,-nostartfiles +CC-NES = arm-linux-gnueabihf-gcc + +all: fbgrab + +fbgrab: fbgrab.c + $(CC-NES) -g -Wall $(CFLAGS-NES) $(LDFLAGS-NES) $< -lpng -lz -o fbgrab + +clean: + -$(RM) fbgrab *~ \#*\# diff --git a/fbgrab-1.3/fbgrab.c b/fbgrab-1.3/fbgrab.c new file mode 100644 index 0000000..5e4dd89 --- /dev/null +++ b/fbgrab-1.3/fbgrab.c @@ -0,0 +1,598 @@ +/* + * fbgrab - takes screenshots using the framebuffer. + * + * (C) Gunnar Monell <gmo@linux.nu> 2002 + * + * This program is free Software, see the COPYING file + * and is based on Stephan Beyer's <fbshot@s-beyer.de> FBShot + * (C) 2000. + * + * For features and differences, read the manual page. + * + * This program has been checked with "splint +posixlib" without + * warnings. Splint is available from http://www.splint.org/ . + * Patches and enhancements of fbgrab have to fulfill this too. + */ + +/* + * clover mod by Cluster + * it automatically crops image and ignores transparent pixels + * http//clusterrr.com clusterrr@clusterrr.com + * (C) 2017 + */ + + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/ioctl.h> +#include <string.h> +#include <getopt.h> +#include <sys/vt.h> /* to handle vt changing */ +#include <png.h> /* PNG lib */ +#include <zlib.h> +#include <linux/fb.h> /* to handle framebuffer ioctls */ + +#define VERSION "1.2" +#define DEFAULT_FB "/dev/fb0" +#define MAX_LEN 512 +#define UNDEFINED -1 + +static int srcBlue = 0; +static int srcGreen = 1; +static int srcRed = 2; +static int srcAlpha = 3; + +static const int Blue = 0; +static const int Green = 1; +static const int Red = 2; +static const int Alpha = 3; + +/*@noreturn@*/ static void fatal_error(char *message) +{ + fprintf(stderr, "%s\n", message); + exit(EXIT_FAILURE); +} + +static void usage(char *binary) +{ + fprintf(stderr, "Usage: %s\t[-hi] [-{C|c} vt] [-d dev] [-s n] [-z n]\n" + "\t\t[-f fromfile -w n -h n -b n] filename.png\n", binary); +} + +static void help(char *binary) +{ + fprintf(stderr, "fbgrab - takes screenshots using the framebuffer, v%s\n", VERSION); + fprintf(stderr, "clover mod\n"); + + usage(binary); + + fprintf(stderr, "\nPossible options:\n"); +/* please keep this list alphabetical */ + fprintf(stderr, "\t-b n \tforce use of n bits/pixel, required when reading from file\n"); + fprintf(stderr, "\t-C n \tgrab from console n, for slower framebuffers\n"); + fprintf(stderr, "\t-c n \tgrab from console n\n"); + fprintf(stderr, "\t-d dev\tuse framebuffer device dev instead of default\n"); + fprintf(stderr, "\t-f file\t read from file instead of framebuffer\n"); + fprintf(stderr, "\t-h n \tset height to n pixels, required when reading from file\n" + "\t\tcan be used to force height when reading from framebuffer\n"); + fprintf(stderr, "\t-i \tturns on interlacing in PNG\n"); + fprintf(stderr, "\t-s n \tsleep n seconds before making screenshot\n"); + fprintf(stderr, "\t-v \tverbose, print debug information.\n"); + fprintf(stderr, "\t-w n \tset width to n pixels, required when reading from file\n" + "\t\tcan be used to force width when reading from framebuffer\n"); + fprintf(stderr, "\t-z n \tPNG compression level: 0 (fast) .. 9 (best)\n"); + fprintf(stderr, "\t-? \tprint this usage information\n"); +} + + +static void chvt(int num) +{ + int fd; + + if(-1 == (fd = open("/dev/console", O_RDWR))) + fatal_error("Cannot open /dev/console"); + + if (ioctl(fd, VT_ACTIVATE, num) != 0) + fatal_error("ioctl VT_ACTIVATE"); + + if (ioctl(fd, VT_WAITACTIVE, num) != 0) + fatal_error("ioctl VT_WAITACTIVE"); + + (void) close(fd); +} + +static unsigned short int change_to_vt(unsigned short int vt_num) +{ + int fd; + unsigned short int old_vt; + struct vt_stat vt_info; + + memset(&vt_info, 0, sizeof(struct vt_stat)); + + if(-1 == (fd=open("/dev/console", O_RDONLY))) + fatal_error("Couldn't open /dev/console"); + + if (ioctl(fd, VT_GETSTATE, &vt_info) != 0) + fatal_error("ioctl VT_GETSTATE"); + + (void) close (fd); + + old_vt = vt_info.v_active; + + chvt((int) vt_num); /* go there for information */ + + return old_vt; +} + +static void get_framebufferdata(char *device, struct fb_var_screeninfo *fb_varinfo_p, int verbose) +{ + int fd; + struct fb_fix_screeninfo fb_fixedinfo; + + /* now open framebuffer device */ + if(-1 == (fd=open(device, O_RDONLY))) + { + fprintf(stderr, "Error: Couldn't open %s.\n", device); + exit(EXIT_FAILURE); + } + + if (ioctl(fd, FBIOGET_VSCREENINFO, fb_varinfo_p) != 0) + fatal_error("ioctl FBIOGET_VSCREENINFO"); + + if (ioctl(fd, FBIOGET_FSCREENINFO, &fb_fixedinfo) != 0) + fatal_error("ioctl FBIOGET_FSCREENINFO"); + + if (verbose) + { + fprintf(stderr, "frame buffer fixed info:\n"); + fprintf(stderr, "id: \"%s\"\n", fb_fixedinfo.id); + switch (fb_fixedinfo.type) + { + case FB_TYPE_PACKED_PIXELS: + fprintf(stderr, "type: packed pixels\n"); + break; + case FB_TYPE_PLANES: + fprintf(stderr, "type: non interleaved planes\n"); + break; + case FB_TYPE_INTERLEAVED_PLANES: + fprintf(stderr, "type: interleaved planes\n"); + break; + case FB_TYPE_TEXT: + fprintf(stderr, "type: text/attributes\n"); + break; + case FB_TYPE_VGA_PLANES: + fprintf(stderr, "type: EGA/VGA planes\n"); + break; + default: + fprintf(stderr, "type: undefined!\n"); + break; + } + fprintf(stderr, "line length: %i bytes (%i pixels)\n", fb_fixedinfo.line_length, fb_fixedinfo.line_length/(fb_varinfo_p->bits_per_pixel/8)); + + fprintf(stderr, "\nframe buffer variable info:\n"); + fprintf(stderr, "resolution: %ix%i\n", fb_varinfo_p->xres, fb_varinfo_p->yres); + fprintf(stderr, "virtual resolution: %ix%i\n", fb_varinfo_p->xres_virtual, fb_varinfo_p->yres_virtual); + fprintf(stderr, "offset: %ix%i\n", fb_varinfo_p->xoffset, fb_varinfo_p->yoffset); + fprintf(stderr, "bits_per_pixel: %i\n", fb_varinfo_p->bits_per_pixel); + fprintf(stderr, "grayscale: %s\n", fb_varinfo_p->grayscale ? "true" : "false"); + fprintf(stderr, "red: offset: %i, length: %i, msb_right: %i\n", fb_varinfo_p->red.offset, fb_varinfo_p->red.length, fb_varinfo_p->red.msb_right); + fprintf(stderr, "blue: offset: %i, length: %i, msb_right: %i\n", fb_varinfo_p->blue.offset, fb_varinfo_p->green.length, fb_varinfo_p->green.msb_right); + fprintf(stderr, "green: offset: %i, length: %i, msb_right: %i\n", fb_varinfo_p->green.offset, fb_varinfo_p->blue.length, fb_varinfo_p->blue.msb_right); + fprintf(stderr, "alpha: offset: %i, length: %i, msb_right: %i\n", fb_varinfo_p->transp.offset, fb_varinfo_p->transp.length, fb_varinfo_p->transp.msb_right); + fprintf(stderr, "pixel format: %s\n", fb_varinfo_p->nonstd == 0 ? "standard" : "non-standard"); + } + srcBlue = fb_varinfo_p->blue.offset >> 3; + srcGreen = fb_varinfo_p->green.offset >> 3; + srcRed = fb_varinfo_p->red.offset >> 3; + + if (fb_varinfo_p->transp.length > 0) { + srcAlpha = fb_varinfo_p->transp.offset >> 3; + } else { + srcAlpha = -1; // not used + } + + (void) close(fd); +} + +static void read_framebuffer(char *device, size_t bytes, unsigned char *buf_p, int skip_bytes) +{ + int fd; + + if(-1 == (fd=open(device, O_RDONLY))) + { + fprintf(stderr, "Error: Couldn't open %s.\n", device); + exit(EXIT_FAILURE); + } + + if (skip_bytes) { + lseek(fd, skip_bytes, SEEK_SET); + } + + if (buf_p == NULL || read(fd, buf_p, bytes) != (ssize_t) bytes) + fatal_error("Error: Not enough memory or data\n"); +} + +static void convert1555to32(int width, int height, + unsigned char *inbuffer, + unsigned char *outbuffer) +{ + unsigned int i; + + for (i=0; i < (unsigned int) height*width*2; i+=2) + { + /* BLUE = 0 */ + outbuffer[(i<<1)+Blue] = (inbuffer[i+1] & 0x7C) << 1; + /* GREEN = 1 */ + outbuffer[(i<<1)+Green] = (((inbuffer[i+1] & 0x3) << 3) | + ((inbuffer[i] & 0xE0) >> 5)) << 3; + /* RED = 2 */ + outbuffer[(i<<1)+Red] = (inbuffer[i] & 0x1f) << 3; + /* ALPHA = 3 */ + outbuffer[(i<<1)+Alpha] = '\0'; + } +} + +static void convert565to32(int width, int height, + unsigned char *inbuffer, + unsigned char *outbuffer) +{ + unsigned int i; + + for (i=0; i < (unsigned int) height*width*2; i+=2) + { + /* BLUE = 0 */ + outbuffer[(i<<1)+Blue] = (inbuffer[i] & 0x1f) << 3; + /* GREEN = 1 */ + outbuffer[(i<<1)+Green] = (((inbuffer[i+1] & 0x7) << 3) | + (inbuffer[i] & 0xE0) >> 5) << 2; + /* RED = 2 */ + outbuffer[(i<<1)+Red] = (inbuffer[i+1] & 0xF8); + /* ALPHA = 3 */ + outbuffer[(i<<1)+Alpha] = '\0'; + } +} + +static void convert888to32(int width, int height, + unsigned char *inbuffer, + unsigned char *outbuffer) +{ + unsigned int i; + + for (i=0; i < (unsigned int) height*width; i++) + { + /* BLUE = 0 */ + outbuffer[(i<<2)+Blue] = inbuffer[i*3+srcBlue]; + /* GREEN = 1 */ + outbuffer[(i<<2)+Green] = inbuffer[i*3+srcGreen]; + /* RED = 2 */ + outbuffer[(i<<2)+Red] = inbuffer[i*3+srcRed]; + /* ALPHA */ + outbuffer[(i<<2)+Alpha] = '\0'; + } +} + +static void convert8888to32(int width, int height, + unsigned char *inbuffer, + unsigned char *outbuffer) +{ + unsigned int i; + + for (i=0; i < (unsigned int) height*width; i++) + { + /* BLUE = 0 */ + outbuffer[(i<<2)+Blue] = inbuffer[i*4+srcBlue]; + /* GREEN = 1 */ + outbuffer[(i<<2)+Green] = inbuffer[i*4+srcGreen]; + /* RED = 2 */ + outbuffer[(i<<2)+Red] = inbuffer[i*4+srcRed]; + /* ALPHA */ + outbuffer[(i<<2)+Alpha] = /*srcAlpha >= 0 ? inbuffer[i*4+srcAlpha] :*/ 0; + } +} + +static void convert8888to32_crop(int width, int height, int* outWidth, int* outHeight, + unsigned char *inbuffer, + unsigned char *outbuffer) +{ + unsigned int i; + int minX = -1; + int maxX = -1; + int minY = -1; + int maxY = -1; + int pixels = 0; + + for (i=0; i < (unsigned int) height*width; i++) + { + int x = i%width; + int y = i/width; + unsigned char a = inbuffer[i*4+srcAlpha]; + if (minX < 0 && minY < 0 && a) + { + minX = x; + minY = y; + } + if (x > 0 && minX >= 0 && maxX < 0 && !a) + maxX = x-1; + if (x >= minX && x <= maxX && y > 0 && minY >= 0 && maxY < 0 && !a) + maxY = y-1; + if ((minX >= 0 && x >= minX) && (maxX < 0 || x <= maxX) && (minY >= 0 && y >= minY) && (maxY < 0 || y <= maxY)) + { + /* BLUE = 0 */ + outbuffer[(pixels<<2)+Blue] = inbuffer[i*4+srcBlue]; + /* GREEN = 1 */ + outbuffer[(pixels<<2)+Green] = inbuffer[i*4+srcGreen]; + /* RED = 2 */ + outbuffer[(pixels<<2)+Red] = inbuffer[i*4+srcRed]; + /* ALPHA */ + outbuffer[(pixels<<2)+Alpha] = /*srcAlpha >= 0 ? inbuffer[i*4+srcAlpha] :*/ 0; + pixels++; + } + } + + if (minX < 0) minX = 0; + if (minY < 0) minY = 0; + if (maxX < 0) maxX = width-1; + if (maxY < 0) maxY = height-1; + *outWidth = maxX - minX + 1; + *outHeight = maxY - minY + 1; + fprintf(stderr, "minx=%d maxx=%d miny=%d maxy=%d\n", minX, maxX, minY, maxY); + fprintf(stderr, "width=%d height=%d\n", *outWidth, *outHeight); +} + +static void write_PNG(unsigned char *outbuffer, char *filename, + int width, int height, int interlace, int compression) +{ + int i; + int bit_depth=0, color_type; + png_bytep row_pointers[height]; + png_structp png_ptr; + png_infop info_ptr; + FILE *outfile; + + if (strcmp(filename, "-") == 0) + outfile = stdout; + else { + outfile = fopen(filename, "wb"); + if (!outfile) + { + fprintf(stderr, "Error: Couldn't fopen %s.\n", filename); + exit(EXIT_FAILURE); + } + } + + for (i=0; i<height; i++) + row_pointers[i] = outbuffer + i * 4 * width; + + + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, + (png_voidp) NULL, (png_error_ptr) NULL, (png_error_ptr) NULL); + + if (!png_ptr) + fatal_error("Error: Couldn't create PNG write struct."); + + info_ptr = png_create_info_struct(png_ptr); + if (!info_ptr) + { + png_destroy_write_struct(&png_ptr, (png_infopp) NULL); + fatal_error("Error: Couldn't create PNG info struct."); + } + + png_init_io(png_ptr, outfile); + + png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); + + bit_depth = 8; + color_type = PNG_COLOR_TYPE_RGB_ALPHA; + png_set_invert_alpha(png_ptr); + png_set_bgr(png_ptr); + + png_set_IHDR(png_ptr, info_ptr, width, height, + bit_depth, color_type, interlace, + PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); + + png_write_info(png_ptr, info_ptr); + + fprintf(stderr, "Now writing PNG file (compression %d)\n", compression); + + png_write_image(png_ptr, row_pointers); + + png_write_end(png_ptr, info_ptr); + /* puh, done, now freeing memory... */ + png_destroy_write_struct(&png_ptr, &info_ptr); + + if (outfile != NULL) + (void) fclose(outfile); + /*@i2@*/ } /* tell splint to ignore false warning for not + released memory of png_ptr and info_ptr */ + +static void convert_and_write(unsigned char *inbuffer, char *filename, + int width, int height, int bits, int interlace, int compression) +{ + size_t bufsize = (size_t) width * height * 4; + + unsigned char *outbuffer = malloc(bufsize); + + if (outbuffer == NULL) + fatal_error("Not enough memory"); + + memset(outbuffer, 0, bufsize); + + fprintf(stderr, "Converting image from %i\n", bits); + + int outWidth = 0; + int outHeight = 0; + + switch(bits) + { + case 15: + convert1555to32(width, height, inbuffer, outbuffer); + write_PNG(outbuffer, filename, width, height, interlace, compression); + break; + case 16: + convert565to32(width, height, inbuffer, outbuffer); + write_PNG(outbuffer, filename, width, height, interlace, compression); + break; + case 24: + convert888to32(width, height, inbuffer, outbuffer); + write_PNG(outbuffer, filename, width, height, interlace, compression); + break; + case 32: +// convert8888to32(width, height, inbuffer, outbuffer); + convert8888to32_crop(width, height, &outWidth, &outHeight, inbuffer, outbuffer); + write_PNG(outbuffer, filename, outWidth, outHeight, interlace, compression); + break; + default: + fprintf(stderr, "%d bits per pixel are not supported! ", bits); + exit(EXIT_FAILURE); + } + + (void) free(outbuffer); +} + + +/******** + * MAIN * + ********/ + +int main(int argc, char **argv) +{ + unsigned char *buf_p; + char *device = NULL; + char *outfile = argv[argc-1]; + int optc; + int vt_num=UNDEFINED, bitdepth=UNDEFINED, height=UNDEFINED, width=UNDEFINED; + int old_vt=UNDEFINED; + size_t buf_size; + char infile[MAX_LEN]; + struct fb_var_screeninfo fb_varinfo; + int waitbfg=0; /* wait before grabbing (for -C )... */ + int interlace = PNG_INTERLACE_NONE; + int verbose = 0; + int png_compression = Z_DEFAULT_COMPRESSION; + int skip_bytes = 0; + + memset(infile, 0, MAX_LEN); + memset(&fb_varinfo, 0, sizeof(struct fb_var_screeninfo)); + + + for(;;) + { + optc=getopt(argc, argv, "f:z:w:b:h:iC:c:d:s:?v"); + if (optc==-1) + break; + switch (optc) + { +/* please keep this list alphabetical */ + case 'b': + bitdepth = atoi(optarg); + break; + case 'C': + waitbfg = 1; + /*@fallthrough@*/ + case 'c': + vt_num = atoi(optarg); + break; + case 'd': + device = optarg; + break; + case 'f': + strncpy(infile, optarg, MAX_LEN); + break; + case 'h': + height = atoi(optarg); + break; + case '?': + help(argv[0]); + return 1; + case 'i': + interlace = PNG_INTERLACE_ADAM7; + break; + case 'v': + verbose = 1; + break; + case 's': + (void) sleep((unsigned int) atoi(optarg)); + break; + case 'w': + width = atoi(optarg); + break; + case 'z': + png_compression = atoi(optarg); + break; + default: + usage(argv[0]); + } + } + + if ((optind==argc) || (1!=argc-optind)) + { + usage(argv[0]); + return 1; + } + + if (UNDEFINED != vt_num) + { + old_vt = (int) change_to_vt((unsigned short int) vt_num); + if (waitbfg != 0) (void) sleep(3); + } + + if (strlen(infile) > 0) + { + if (UNDEFINED == bitdepth || UNDEFINED == width || UNDEFINED == height) + { + fprintf(stderr, "Width, height and bitdepth are mandatory when reading from file\n"); + exit(EXIT_FAILURE); + } + } + else + { + if (NULL == device) + { + device = getenv("FRAMEBUFFER"); + if (NULL == device) + { + device = DEFAULT_FB; + } + } + + get_framebufferdata(device, &fb_varinfo, verbose); + + if (UNDEFINED == bitdepth) + bitdepth = (int) fb_varinfo.bits_per_pixel; + + if (UNDEFINED == width) + width = (int) fb_varinfo.xres; + + if (UNDEFINED == height) + height = (int) fb_varinfo.yres; + + skip_bytes = (fb_varinfo.yoffset * fb_varinfo.xres) * (fb_varinfo.bits_per_pixel >> 3); + + fprintf(stderr, "Resolution: %ix%i depth %i\n", width, height, bitdepth); + + strncpy(infile, device, MAX_LEN - 1); + } + + buf_size = width * height * (((unsigned int) bitdepth + 7) >> 3); + + buf_p = malloc(buf_size); + + if(buf_p == NULL) + fatal_error("Not enough memory"); + + memset(buf_p, 0, buf_size); + + read_framebuffer(infile, buf_size, buf_p, skip_bytes); + + if (UNDEFINED != old_vt) + (void) change_to_vt((unsigned short int) old_vt); + + convert_and_write(buf_p, outfile, width, height, bitdepth, interlace, png_compression); + + (void) free(buf_p); + + return 0; +} + diff --git a/makemod.sh b/makemod.sh new file mode 100755 index 0000000..22724ba --- /dev/null +++ b/makemod.sh @@ -0,0 +1,11 @@ +#!/bin/sh +moddir=retroarch.hmod +modname=retroarch.hmod +outdir=output +[ -z "$1" ] || outdir=$1 +cd "$moddir" +rm -f "$modname" +tar -czvf "$modname" * +mkdir -p "../$outdir" +rm -f "../$outdir/$modname" +mv "$modname" "../$outdir/$modname" |