home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / HSI2ARR.ZIP / HSI2ARR.BAK < prev    next >
Text File  |  1994-08-17  |  7KB  |  200 lines

  1. /* HSI2ARR.C -- Coded by Darion.  August 16th, 1994
  2.  * Converts IMAGE ALCHEMY'S HSI RAW format + palette to C arrays
  3.  * And defines.  Coded by Darion (thimj@u.washington.edu) 8-16-94
  4.  * Any Questions?  email me..                                     */
  5.  
  6. #include <stdio.h>      /* the standards */
  7. #include <dos.h>
  8. #include <stdlib.h>
  9.  
  10.  
  11. void help(int);        /* prototyping is wonderful :) */
  12. int getWord(FILE *stream);
  13.  
  14. /* this header was taken right out of the back of the manual */
  15.  
  16. typedef struct {
  17.     char magicnum[6];           /* magic numbers for verifying file */
  18.     int version;                /* current version of format.. Currently 4 */
  19.     int width;                  /* width of picture */
  20.     int height;                 /* height of picture */
  21.     int palsize;                /* number of colors */
  22.     int hordpi;                 /* horizontal dots per inch */
  23.     int verdpi;                 /* vertical dots per inch */
  24.     int gamma;                  /* gamma factor */
  25.     char reserved[12];          /* reserved for future use */
  26.     } HSIRAW;
  27.  
  28. HSIRAW rawhdr;                  /* creates one (1) structure of type HSIRAW */
  29. unsigned char *paldata;         /* unsigned char pointer to be filled in later */
  30. unsigned char *picdata;         /* buffer for holding picture data */
  31.  
  32. void main(int argc, char *argv[])
  33. {
  34.  
  35.     FILE *fp, *fp1;             /* File pointers for infile and outfile respectively */
  36.     int x, temp, temp1=0,z;
  37.     int value,blah;            /* generic variables */
  38.  
  39.     if(argc < 2) help(1);        /* if they think they have a interface, yell at em */
  40.  
  41.     if ((fp = fopen(argv[1], "rb"))== NULL) /* open .RAW file for read+binary */
  42.         {
  43.         fprintf(stderr, "Cannot open input file.\n"); /* catch any errors */
  44.         help(2);
  45.         }
  46.     fread(rawhdr.magicnum, 6 , 1, fp);  /* read in the first 6 bytes, the 'magicnumbers' */
  47.     rawhdr.version = getWord(fp);        /* read in the rest of the header */
  48.     rawhdr.width = getWord(fp);        /* must use special function to get int's */
  49.     rawhdr.height = getWord(fp);        /* function pulled from back of Image Alchemy manual */
  50.     rawhdr.palsize = getWord(fp);        /* it reads any int on any OS the same */
  51.     rawhdr.hordpi = getWord(fp);
  52.     rawhdr.verdpi = getWord(fp);
  53.     rawhdr.gamma = getWord(fp);
  54.     fread(rawhdr.reserved, 12, 1, fp);
  55.  
  56. /* this code below validates file, checking the magicnumbers */
  57.  
  58.     if(rawhdr.magicnum[0] != 0x6d || rawhdr.magicnum[1] != 0x68 ||
  59.        rawhdr.magicnum[2] != 0x77 || rawhdr.magicnum[3] != 0x61 ||
  60.        rawhdr.magicnum[4] != 0x6e || rawhdr.magicnum[5] != 0x68)
  61.         {
  62.         fprintf(stderr, "Invalid HSI-RAW file.\n");
  63.         help(3);
  64.         }
  65. /* allocate space for the palette...  the size of the space allocated must be
  66.  * equal to THREE times the amount of colors in the picture.  This is because
  67.  * three BYTE sized values make up ONE palette entry */
  68.  
  69.     paldata = (char *)malloc(rawhdr.palsize*3);
  70.  
  71. /* read in the palette, it is the LAST thing after the header.  So
  72.  * you have a 32 byte header, then the palette, then image data */
  73.  
  74.     fread(paldata, rawhdr.palsize * 3, 1, fp);
  75.     for(x = 0;x < rawhdr.palsize * 3;x++)
  76.         paldata[x] = paldata[x] >> 2;
  77.  
  78. /* allocate space for the picture buffer.  I just used the width of the
  79.  * picture for the size */
  80.  
  81.     picdata = (char *)malloc(rawhdr.width);
  82.  
  83. /* debug info.. if you want to 'mess' with the code a little bit, I hope this
  84.  * can help        */
  85.  
  86. #ifdef DEBUG
  87.     printf("Palsize: %d\n", rawhdr.palsize);
  88.     printf("Allocated: %d\n", rawhdr.palsize * 3);
  89.     printf("Version: %d\n", rawhdr.version);
  90.     printf("Width: %d\n", rawhdr.width);
  91.     printf("Height: %d\n", rawhdr.height);
  92.     printf("Hordpi: %d\n", rawhdr.hordpi);
  93.     printf("Verdpi: %d\n", rawhdr.verdpi);
  94.     printf("Gamma: %d\n", rawhdr.gamma);
  95. #endif
  96.  
  97. #ifdef DEBUG
  98.     for(temp=0;temp < rawhdr.palsize * 3;temp +=3)
  99.         printf("Pal Data: %d %d %d\n", paldata[temp], paldata[temp+1], paldata[temp+2]);
  100. #endif
  101.  
  102. /* check for and open output file */
  103.     if ((fp1 = fopen(argv[2], "wb"))== NULL)
  104.         {
  105.         fprintf(stderr, "Cannot open output file.\n");
  106.         help(4);
  107.         }
  108.  
  109. /* begin to output to file, you can customize your own output files here */
  110. /* fprintf is easy, prints formatted text into a stream, in this case a file */
  111.  
  112.     fprintf(fp1,"#define PICDATA_COLORS %d\n", rawhdr.palsize);
  113.     fprintf(fp1,"#define PICDATA_HORDPI %d\n", rawhdr.hordpi);
  114.     fprintf(fp1,"#define PICDATA_VERDPI %d\n", rawhdr.verdpi);
  115.     fprintf(fp1,"#define PICDATA_GAMMA %d\n", rawhdr.gamma);
  116.     fprintf(fp1,"#define PICDATA_WIDTH %d\n", rawhdr.width);
  117.     fprintf(fp1,"#define PICDATA_HEIGHT %d\n", rawhdr.height);
  118.     fprintf(fp1,"unsigned char PALDATA[%d] = {\n",rawhdr.palsize * 3);
  119. /* set up the loop in which we will print out our palette data */
  120. /* I just loop from 0 to 765 right here, leaving 3 so I can terminate the */
  121. /* array properly */
  122.  
  123.     temp = 0;
  124.     for(temp=0;temp < ((rawhdr.palsize * 3)-3);temp += 15)
  125.         {
  126.         fprintf(fp1, "%d,%d,%d, ", paldata[temp], paldata[temp+1], paldata[temp+2]);
  127.         fprintf(fp1, "%d,%d,%d, ", paldata[temp+3], paldata[temp+4], paldata[temp+5]);
  128.         fprintf(fp1, "%d,%d,%d, ", paldata[temp+6], paldata[temp+7], paldata[temp+8]);
  129.         fprintf(fp1, "%d,%d,%d, ", paldata[temp+9], paldata[temp+10], paldata[temp+11]);
  130.         fprintf(fp1, "%d,%d,%d,\n", paldata[temp+12], paldata[temp+13], paldata[temp+14]);
  131.         }
  132. /* terminate the array properly */
  133.  
  134.     fprintf(fp1, "%d,%d,%d };\n\n", paldata[temp], paldata[temp+1], paldata[temp+2]);
  135. /* start with the picture data */
  136.  
  137.     fprintf(fp1,"char PICDATA[%d][%d] = {\n",rawhdr.height, rawhdr.width);
  138.     printf("\n\nConverting... ");
  139. /* same process.  I use a buffer equivelant to the WIDTH of the picture, so
  140.  * I loop HEIGHT times.  I read in ONE line of the picture each time through
  141.  * the loop.  I then loop again, disecting that line into the output file. I
  142.  * only go height-1 so I can once again terminate my array properly */
  143.  
  144.     if(rawhdr.width < 20) value = rawhdr.width;
  145.     else value = 30;
  146.     for(temp = 0;temp < rawhdr.height-1; temp++)
  147.         {
  148.         fread(picdata, rawhdr.width, 1, fp);
  149.         temp1=0;
  150.         while(temp1 < rawhdr.width)
  151.             {
  152.             for(z = 0;z < value;z++)
  153.                 {
  154.                 if(temp1 == rawhdr.width) break;
  155.                 fprintf(fp1, "%d,", picdata[temp1++]);
  156.                 }
  157.             fprintf(fp1, "\n");
  158.             }
  159.         }
  160. /* here is where I terminate the array, I just read in the last line and
  161.  * disect it until I have one left, then I print the last one with the closing
  162.  * bracket */
  163.  
  164.     fread(picdata, rawhdr.width, 1, fp);
  165.     temp1 = 0;
  166.     while(temp1 < (rawhdr.width-1))
  167.             {
  168.             for(z = 0;z < value;z++)
  169.                 {
  170.                 if(temp1 == (rawhdr.width-1)) break;
  171.                 fprintf(fp1, "%d,", picdata[temp1++]);
  172.                 }
  173.             fprintf(fp1, "\n");
  174.             }
  175.     fprintf(fp1, "%d };",picdata[temp1]);
  176. }
  177.  
  178. void help(int errorlevel)
  179. {
  180.     printf("\n");
  181.     printf("HSI2ARR -- Converts HSI Raw format to C Array\n");
  182.     printf("     By Darion (thimj@u.washington.edu)\n");
  183.     printf("─────────────────────────────────────────────\n");
  184.     printf("Usage: hsi2arr <input file> <output file>\n");
  185.     printf("Example: hsi2arr eddings.raw eddings.h\n");
  186.  
  187.     exit(errorlevel);
  188. }
  189.  
  190. /* here is that routine I mentioned above, pulls an integer out of a stream
  191.  * regardless of the HIGH or LOW position of the bytes, I guess some OS's
  192.  * differ in the way that they arrange the bytes of their integers */
  193.  
  194. int getWord(FILE *stream)
  195. {
  196.     register int temp;
  197.     temp=getc(stream) << 8;
  198.     return(getc(stream) | temp);
  199. }
  200.