home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xloadimg.zip / xloadimage.4.1 / faces.c < prev    next >
C/C++ Source or Header  |  1993-10-21  |  4KB  |  181 lines

  1. /* faces.c:
  2.  *
  3.  * faces format image loader
  4.  *
  5.  * jim frost 07.06.89
  6.  *
  7.  * Copyright 1989 Jim Frost.
  8.  * See included file "copyright.h" for complete copyright information.
  9.  */
  10.  
  11. #include "copyright.h"
  12. #include "image.h"
  13.  
  14. /* SUPPRESS 560 */
  15.  
  16. static short        HexTable[256];  /* conversion value */
  17. static unsigned int Initialized= 0; /* easier to fill in at run time */
  18.  
  19. #define HEXIGNORE -1
  20. #define HEXBAD    -2
  21.  
  22. /* build a hex digit value table with the bits inverted
  23.  */
  24.  
  25. static void initHexTable()
  26. { int a;
  27.  
  28.   for (a= 0; a < 256; a++)
  29.     HexTable[a]= HEXBAD;
  30.  
  31.   HexTable['0']= 0x0;
  32.   HexTable['1']= 0x1;
  33.   HexTable['2']= 0x2;
  34.   HexTable['3']= 0x3;
  35.   HexTable['4']= 0x4;
  36.   HexTable['5']= 0x5;
  37.   HexTable['6']= 0x6;
  38.   HexTable['7']= 0x7;
  39.   HexTable['8']= 0x8;
  40.   HexTable['9']= 0x9;
  41.   HexTable['A']= 0xa; HexTable['a']= HexTable['A'];
  42.   HexTable['B']= 0xb; HexTable['b']= HexTable['B'];
  43.   HexTable['C']= 0xc; HexTable['c']= HexTable['C'];
  44.   HexTable['D']= 0xd; HexTable['d']= HexTable['D'];
  45.   HexTable['E']= 0xe; HexTable['e']= HexTable['E'];
  46.   HexTable['F']= 0xf; HexTable['f']= HexTable['F'];
  47.   HexTable['\r']= HEXIGNORE;
  48.   HexTable['\n']= HEXIGNORE;
  49.   HexTable['\t']= HEXIGNORE;
  50.   HexTable[' ']= HEXIGNORE;
  51.  
  52.   Initialized = 1;
  53. }
  54.  
  55. /* read a hex value and return its value
  56.  */
  57.  
  58. static int nextInt(zf, len)
  59.      ZFILE        *zf;
  60.      unsigned int  len;
  61. { int c;
  62.   int value= 0;
  63.   int count;
  64.  
  65.   len <<= 1;
  66.   for (count= 0; count < len;) {
  67.     c= zgetc(zf);
  68.     if (c == EOF)
  69.       return(-1);
  70.     else {
  71.       c= HexTable[c & 0xff];
  72.       switch(c) {
  73.       case HEXIGNORE:
  74.     break;
  75.       case HEXBAD:
  76.     return(-1);
  77.       default:
  78.     value= (value << 4) + c;
  79.     count++;
  80.       }
  81.     }
  82.   }
  83.   return(value);
  84. }
  85.  
  86. Image *facesLoad(fullname, name, verbose)
  87.      char *fullname, *name;
  88. { ZFILE        *zf;
  89.   Image        *image;
  90.   char          fname[BUFSIZ];
  91.   char          lname[BUFSIZ];
  92.   char          buf[BUFSIZ];
  93.   unsigned int  w, h, d, iw, ih, id;
  94.   unsigned int  x, y;
  95.   int           value;
  96.   unsigned int  linelen;
  97.   byte         *lineptr, *dataptr;
  98.  
  99.   if (!Initialized)
  100.     initHexTable();
  101.  
  102.   if (! (zf= zopen(fullname)))
  103.     return(NULL);
  104.  
  105.   w= h= d= 0;
  106.   fname[0]= lname[0]= '\0';
  107.   while (zgets((byte *)buf, BUFSIZ - 1, zf)) {
  108.     if (! strcmp(buf, "\n"))
  109.       break;
  110.     if (!strncmp(buf, "FirstName:", 10))
  111.       strcpy(fname, buf + 11);
  112.     else if (!strncmp(buf, "LastName:", 9))
  113.       strcpy(lname, buf + 10);
  114.     else if (!strncmp(buf, "Image:", 6)) {
  115.       if (sscanf(buf + 7, "%d%d%d", &iw, &ih, &id) != 3) {
  116.     printf("%s: Bad Faces Project image\n", fullname);
  117.     exit(1);
  118.       }
  119.     }
  120.     else if (!strncmp(buf, "PicData:", 8)) {
  121.       if (sscanf(buf + 9, "%d%d%d", &w, &h, &d) != 3) {
  122.     printf("%s: Bad Faces Project image\n", fullname);
  123.     exit(1);
  124.       }
  125.     }
  126.   }
  127.   if (!w || !h || !d) {
  128.     zclose(zf);
  129.     return(NULL);
  130.   }
  131.   znocache(zf);
  132.  
  133.   if (verbose)
  134.     printf("%s is a  %dx%d %d-bit grayscale Faces Project image\n",
  135.        name, w, h, d);
  136.  
  137.   image= newRGBImage(w, h, d);
  138.   fname[strlen(fname) - 1]= ' ';
  139.   strcat(fname, lname);
  140.   fname[strlen(fname) - 1]= '\0';
  141.   image->title= dupString(fname);
  142.  
  143.   /* image is greyscale; build RGB map accordingly
  144.    */
  145.  
  146.   for (x= 0; x < image->rgb.size; x++)
  147.     *(image->rgb.red + x)= *(image->rgb.green + x)= *(image->rgb.blue + x)=
  148.       (65536 / image->rgb.size) * x;
  149.   image->rgb.used= image->rgb.size;
  150.  
  151.   /* read in image data
  152.    */
  153.  
  154.   linelen= w * image->pixlen;
  155.   lineptr= image->data + (h * linelen);
  156.   for (y= 0; y < h; y++) {
  157.     lineptr -= linelen;
  158.     dataptr= lineptr;
  159.     for (x= 0; x < w; x++) {
  160.       if ((value= nextInt(zf, image->pixlen)) < 0) {
  161.     printf("%s: Bad Faces Project image data\n", fullname);
  162.     exit(1);
  163.       }
  164.       *(dataptr++)= value;
  165.     }
  166.   }
  167.   zclose(zf);
  168.   return(image);
  169. }
  170.  
  171. int facesIdent(fullname, name)
  172.      char *fullname, *name;
  173. { Image *image;
  174.  
  175.   if (image= facesLoad(fullname, name, 1)) {
  176.     freeImage(image);
  177.     return(1);
  178.   }
  179.   return(0);
  180. }
  181.