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

  1. /* sunraster.c:
  2.  *
  3.  * sun rasterfile image type
  4.  *
  5.  * jim frost 09.27.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. #include "sunraster.h"
  14.  
  15. static void babble(name, header)
  16.      char           *name;
  17.      struct rheader *header;
  18. {
  19.   printf("%s is a", name);
  20.   switch (memToVal(header->type, 4)) {
  21.   case ROLD:
  22.     printf("n old-style");
  23.     break;
  24.   case RSTANDARD:
  25.     printf(" standard");
  26.     break;
  27.   case RRLENCODED:
  28.     printf(" run-length encoded");
  29.     break;
  30.   default:
  31.     printf(" unknown-type");
  32.   }
  33.   printf(" %dx%d ", memToVal(header->width, 4), memToVal(header->height, 4));
  34.   if (memToVal(header->depth, 4) > 1)
  35.     printf("%d plane color", memToVal(header->depth, 4));
  36.   else
  37.     printf("monochrome");
  38.   printf(" Sun rasterfile\n");
  39. }
  40.  
  41. int sunRasterIdent(fullname, name)
  42.      char *fullname, *name;
  43. { ZFILE          *zf;
  44.   struct rheader  header;
  45.   int             r;
  46.  
  47.   if (! (zf= zopen(fullname))) {
  48.     perror("sunRasterIdent");
  49.     return(0);
  50.   }
  51.   switch (zread(zf, &header, sizeof(struct rheader))) {
  52.   case -1:
  53.     perror("sunRasterIdent");
  54.     r= 0;
  55.     break;
  56.  
  57.   case sizeof(struct rheader):
  58.     if (memToVal(header.magic, 4) != RMAGICNUMBER) {
  59.       r= 0;
  60.       break;
  61.     }
  62.     babble(name, &header);
  63.     r= 1;
  64.     break;
  65.  
  66.   default:
  67.     r= 0;
  68.     break;
  69.   }
  70.   zclose(zf);
  71.   return(r);
  72. }
  73.  
  74. /* read either rl-encoded or normal image data
  75.  */
  76.  
  77. static void sunread(zf, buf, len, enc)
  78.      ZFILE        *zf;
  79.      byte         *buf;
  80.      unsigned int  len;
  81.      unsigned int  enc;  /* true if encoded file */
  82. { static byte repchar, remaining= 0;
  83.  
  84.   /* rl-encoded read
  85.    */
  86.  
  87.   if (enc) {
  88.     while (len--)
  89.       if (remaining) {
  90.     remaining--;
  91.     *(buf++)= repchar;
  92.       }
  93.       else {
  94.     if (zread(zf, &repchar, 1) != 1) {
  95.       printf("sunRasterLoad: Bad read on image data\n");
  96.       exit(1);
  97.     }
  98.     if (repchar == RESC) {
  99.       if (zread(zf, &remaining, 1) != 1) {
  100.         printf("sunRasterLoad: Bad read on image data\n");
  101.         exit(1);
  102.       }
  103.       if (remaining == 0)
  104.         *(buf++)= RESC;
  105.       else {
  106.         if (zread(zf, &repchar, 1) != 1) {
  107.           printf("sunRasterLoad: Bad read on image data\n");
  108.           exit(1);
  109.         }
  110.         *(buf++)= repchar;
  111.       }
  112.     }
  113.     else
  114.       *(buf++)= repchar;
  115.       }
  116.   }
  117.  
  118.   /* normal read
  119.    */
  120.  
  121.   else {
  122.     if (zread(zf, buf, len) < len) {
  123.       printf("sunRasterLoad: Bad read on image data\n");
  124.       exit(1);
  125.     }
  126.   }
  127. }
  128.  
  129. Image *sunRasterLoad(fullname, name, verbose)
  130.      char         *fullname, *name;
  131.      unsigned int  verbose;
  132. { ZFILE          *zf;
  133.   struct rheader  header;
  134.   unsigned int    mapsize;
  135.   byte           *map;
  136.   byte           *mapred, *mapgreen, *mapblue;
  137.   unsigned int    depth;
  138.   unsigned int    linelen;   /* length of raster line in bytes */
  139.   unsigned int    fill;      /* # of fill bytes per raster line */
  140.   unsigned int    enc;
  141.   byte            fillchar;
  142.   Image          *image;
  143.   byte           *lineptr;
  144.   unsigned int    y;
  145.  
  146.   if (! (zf= zopen(fullname))) {
  147.     perror("sunRasterLoad");
  148.     return(NULL);
  149.   }
  150.   switch (zread(zf, &header, sizeof(struct rheader))) {
  151.   case -1:
  152.     perror("sunRasterLoad");
  153.     zclose(zf);
  154.     exit(1);
  155.  
  156.   case sizeof(struct rheader):
  157.     if (memToVal(header.magic, 4) != RMAGICNUMBER) {
  158.       zclose(zf);
  159.       return(NULL);
  160.     }
  161.     if (verbose)
  162.       babble(name, &header);
  163.     break;
  164.  
  165.   default:
  166.     zclose(zf);
  167.     return(NULL);
  168.   }
  169.  
  170.   /* get an image to put the data in
  171.    */
  172.  
  173.   depth= memToVal(header.depth, 4);
  174.   if (depth == 1)
  175.     image= newBitImage(memToVal(header.width, 4),
  176.                memToVal(header.height, 4));
  177.   else
  178.     image= newRGBImage(memToVal(header.width, 4),
  179.                memToVal(header.height, 4),
  180.                memToVal(header.depth, 4));
  181.  
  182.   /* set up the colormap
  183.    */
  184.  
  185.   if (depth == 1)
  186.     linelen= (image->width / 8) + (image->width % 8 ? 1 : 0);
  187.   else
  188.     linelen= image->width * image->pixlen;
  189.   fill= (linelen % 2 ? 1 : 0);
  190.   if (mapsize= memToVal(header.maplen, 4)) {
  191.     map= lmalloc(mapsize);
  192.     if (zread(zf, map, mapsize) < mapsize) {
  193.       printf("sunRasterLoad: Bad read on colormap\n");
  194.       exit(1);
  195.     }
  196.     mapsize /= 3;
  197.     mapred= map;
  198.     mapgreen= mapred + mapsize;
  199.     mapblue= mapgreen + mapsize;
  200.     for (y= 0; y < mapsize; y++) {
  201.       *(image->rgb.red + y)= (*(mapred++) << 8);
  202.       *(image->rgb.green + y)= (*(mapgreen++) << 8);
  203.       *(image->rgb.blue + y)= (*(mapblue++) << 8);
  204.     }
  205.     lfree(map);
  206.     image->rgb.used= mapsize;
  207.   }
  208.  
  209.   enc= (memToVal(header.type, 4) == RRLENCODED);
  210.   lineptr= image->data;
  211.   for (y= 0; y < image->height; y++) {
  212.     sunread(zf, lineptr, linelen, enc);
  213.     lineptr += linelen;
  214.     if (fill)
  215.       sunread(zf, &fillchar, fill, enc);
  216.   }
  217.   zclose(zf);
  218.   image->title= dupString(name);
  219.   return(image);
  220. }
  221.