home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume5 / xldimage / part01 / faces.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-13  |  3.8 KB  |  178 lines

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