home *** CD-ROM | disk | FTP | other *** search
/ Geek 6 / Geek-006.iso / linux / video / xmovie-1.5.3.tar.gz / xmovie-1.5.3.tar / xmovie-1.5.3 / guicast / pngtoh.c < prev    next >
C/C++ Source or Header  |  2000-11-29  |  3KB  |  126 lines

  1. #include <ctype.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/stat.h>
  5.  
  6. // Convert input files to .h files consisting of hex arrays
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     if(argc < 2) return 1;
  11.  
  12.     for(argc--; argc > 0; argc--)
  13.     {
  14.         FILE *in;
  15.         FILE *out;
  16.         char variable[1024], header_fn[1024], output_fn[1024], *suffix, *prefix;
  17.         int i;
  18.         int bytes_per_row = 16;
  19.         char row[1024], byte[1024], character;
  20.         struct stat st;
  21.         long total_bytes;
  22.  
  23.         in = fopen(argv[argc], "rb");
  24.         if(!in) continue;
  25.  
  26.         stat(argv[argc], &st);
  27.         total_bytes = (long)st.st_size;
  28.  
  29. // Replace . with _png and append .h to filename
  30.         strcpy(output_fn, argv[argc]);
  31.         suffix = strrchr(output_fn, '.');
  32.         if(suffix) *suffix = '_';
  33.         strcat(output_fn, ".h");
  34.  
  35.         out = fopen(output_fn, "w");
  36.         if(!out)
  37.         {
  38.             fclose(in);
  39.             continue;
  40.         }
  41.  
  42.  
  43. // Strip leading directories for variable and header
  44.         prefix = strrchr(output_fn, '/');
  45.         if(!prefix) 
  46.             prefix = output_fn;
  47.         else
  48.             prefix++;
  49.  
  50.         strcpy(header_fn, prefix);
  51.         for(i = 0; i < strlen(header_fn); i++)
  52.         {
  53. // Replace leading digits
  54.             if(i == 0 && isdigit(header_fn[i]))
  55.             {
  56.                 int k;
  57.                 for(k = strlen(header_fn); k >= 0; k--)
  58.                 {
  59.                     header_fn[k + 1] = header_fn[k];
  60.                 }
  61.                 header_fn[0] = '_';
  62.             }
  63.  
  64. // Replace . with _ for header
  65.             if(header_fn[i] == '.') 
  66.                 header_fn[i] = '_';
  67.             else
  68.                 header_fn[i] = toupper(header_fn[i]);
  69.         }
  70.  
  71. // Strip .h for variable
  72.         strcpy(variable, prefix);
  73.         suffix = strrchr(variable, '.');
  74.         if(suffix) *suffix = 0;
  75.  
  76. // Replace leading digits
  77.         if(isdigit(variable[0]))
  78.         {
  79.             int k;
  80.             for(k = strlen(variable); k >= 0; k--)
  81.             {
  82.                 variable[k + 1] = variable[k];
  83.             }
  84.             variable[0] = '_';
  85.         }
  86.  
  87. // Print the header
  88.         fprintf(out, "#ifndef %s\n"
  89.                      "#define %s\n"
  90.                      "\n"
  91.                      "static unsigned char %s[] = \n{\n",
  92.                      header_fn,
  93.                      header_fn,
  94.                      variable);
  95.  
  96. // Print the size of the file
  97.         fprintf(out, "\t0x%02x, 0x%02x, 0x%02x, 0x%02x, \n",
  98.             (unsigned long)(total_bytes & 0xff000000) >> 24,
  99.             (unsigned long)(total_bytes & 0xff0000) >> 16,
  100.             (unsigned long)(total_bytes & 0xff00) >> 8,
  101.             (unsigned long)(total_bytes & 0xff));
  102.  
  103.         while(total_bytes > 0)
  104.         {
  105.             sprintf(row, "\t");
  106.             for(i = 0; i < bytes_per_row && total_bytes > 0; i++)
  107.             {
  108.                 if(i == 0)
  109.                     sprintf(byte, "0x%02x", fgetc(in));
  110.                 else
  111.                     sprintf(byte, ", 0x%02x", fgetc(in));
  112.                 strcat(row, byte);
  113.                 total_bytes--;
  114.             }
  115.             if(total_bytes > 0)
  116.                 sprintf(byte, ", \n");
  117.             else
  118.                 sprintf(byte, "\n");
  119.  
  120.             fprintf(out, "%s%s", row, byte);
  121.         }
  122.  
  123.         fprintf(out, "};\n\n#endif\n");
  124.     }
  125. }
  126.