Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

pngtoh.c

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify it
00003  * under the terms of the GNU Lesser General Public License as published
00004  * by the Free Software Foundation; either version 2 of the License, or
00005  * (at your option) any later version.
00006  * 
00007  * This program is distributed in the hope that it will be useful, but
00008  * WITHOUT ANY WARRANTY; without even the implied warranty of
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00010  * Lesser General Public License for more details.
00011  * 
00012  * You should have received a copy of the GNU Lesser General Public
00013  * License along with this program; if not, write to the Free Software
00014  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 
00015  * USA
00016  */
00017  
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 #include <ctype.h>
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <sys/stat.h>
00030 
00031 // Convert input files to .h files consisting of hex arrays
00032 
00033 int main(int argc, char *argv[])
00034 {
00035         if(argc < 2) return 1;
00036 
00037         for(argc--; argc > 0; argc--)
00038         {
00039                 FILE *in;
00040                 FILE *out;
00041                 char variable[1024], header_fn[1024], output_fn[1024], *suffix, *prefix;
00042                 int i;
00043                 int bytes_per_row = 16;
00044                 char row[1024], byte[1024], character;
00045                 struct stat st;
00046                 long total_bytes;
00047 
00048                 in = fopen(argv[argc], "rb");
00049                 if(!in) continue;
00050 
00051                 stat(argv[argc], &st);
00052                 total_bytes = (long)st.st_size;
00053 
00054 // Replace . with _png and append .h to filename
00055                 strcpy(output_fn, argv[argc]);
00056                 suffix = strrchr(output_fn, '.');
00057                 if(suffix) *suffix = '_';
00058                 strcat(output_fn, ".h");
00059 
00060                 out = fopen(output_fn, "w");
00061                 if(!out)
00062                 {
00063                         fclose(in);
00064                         continue;
00065                 }
00066 
00067 
00068 // Strip leading directories for variable and header
00069                 prefix = strrchr(output_fn, '/');
00070                 if(!prefix) 
00071                         prefix = output_fn;
00072                 else
00073                         prefix++;
00074 
00075                 strcpy(header_fn, prefix);
00076                 for(i = 0; i < strlen(header_fn); i++)
00077                 {
00078 // Replace leading digits
00079                         if(i == 0 && isdigit(header_fn[i]))
00080                         {
00081                                 int k;
00082                                 for(k = strlen(header_fn); k >= 0; k--)
00083                                 {
00084                                         header_fn[k + 1] = header_fn[k];
00085                                 }
00086                                 header_fn[0] = '_';
00087                         }
00088 
00089 // Replace . with _ for header
00090                         if(header_fn[i] == '.') 
00091                                 header_fn[i] = '_';
00092                         else
00093                                 header_fn[i] = toupper(header_fn[i]);
00094                 }
00095 
00096 // Strip .h for variable
00097                 strcpy(variable, prefix);
00098                 suffix = strrchr(variable, '.');
00099                 if(suffix) *suffix = 0;
00100 
00101 // Replace leading digits
00102                 if(isdigit(variable[0]))
00103                 {
00104                         int k;
00105                         for(k = strlen(variable); k >= 0; k--)
00106                         {
00107                                 variable[k + 1] = variable[k];
00108                         }
00109                         variable[0] = '_';
00110                 }
00111 
00112 // Print the header
00113                 fprintf(out, "#ifndef %s\n"
00114                                          "#define %s\n"
00115                                          "\n"
00116                                          "static unsigned char %s[] = \n{\n",
00117                                          header_fn,
00118                                          header_fn,
00119                                          variable);
00120 
00121 // Print the size of the file
00122                 fprintf(out, "\t0x%02x, 0x%02x, 0x%02x, 0x%02x, \n",
00123                         (unsigned long)(total_bytes & 0xff000000) >> 24,
00124                         (unsigned long)(total_bytes & 0xff0000) >> 16,
00125                         (unsigned long)(total_bytes & 0xff00) >> 8,
00126                         (unsigned long)(total_bytes & 0xff));
00127 
00128                 while(total_bytes > 0)
00129                 {
00130                         sprintf(row, "\t");
00131                         for(i = 0; i < bytes_per_row && total_bytes > 0; i++)
00132                         {
00133                                 if(i == 0)
00134                                         sprintf(byte, "0x%02x", fgetc(in));
00135                                 else
00136                                         sprintf(byte, ", 0x%02x", fgetc(in));
00137                                 strcat(row, byte);
00138                                 total_bytes--;
00139                         }
00140                         if(total_bytes > 0)
00141                                 sprintf(byte, ", \n");
00142                         else
00143                                 sprintf(byte, "\n");
00144 
00145                         fprintf(out, "%s%s", row, byte);
00146                 }
00147 
00148                 fprintf(out, "};\n\n#endif\n");
00149                 fclose(out);
00150         }
00151 }

Generated on Sun Jan 8 13:26:35 2006 for Guicast-svn by  doxygen 1.4.4