home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga Shareware Floppies / ma01.dms / ma01.adf / wasp / src / readras.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-28  |  3.0 KB  |  175 lines

  1. /* wasp - Copyright 1991 by Steven Reiz
  2.  * see COPYING and wasp.c for further info,
  3.  * readras.c, 24/7/91, 8/12/91, 27/12/91
  4.  */
  5.  
  6. static char *sourcefile=__FILE__;
  7.  
  8. #include "wasp.h"
  9.  
  10. static struct {
  11.     long magic;
  12.     long width;
  13.     long height;
  14.     long depth;
  15.     long length;
  16.     long type;
  17.     long maptype;
  18.     long maplength; /* nr of bytes, always directly behind the header */
  19. } header;
  20. #define MAGIC        0x59a66a95
  21. #define RASOLD        0
  22. #define RASSTANDARD    1
  23. #define RASBYTEENCODED    2
  24. #define RASEXPERIMENTAL    0xffff
  25. #define RMTNONE        0
  26. #define RMTRAW        1
  27. #define RMTRGB        2
  28. #define BUFSZ        32768
  29. static char *buf;
  30. static long xsz1;
  31.  
  32.  
  33. read_ras(void)
  34. {
  35.     short y;
  36.  
  37.     cread(&header, (int)sizeof(header));
  38.     if (header.magic!=MAGIC) {
  39.         cseek_in(0L, 0);
  40.         return 0;
  41.     }
  42.     xsz=header.width;
  43.     xsz1=(xsz+15)& -16L;
  44.     ysz=header.height;
  45.     printe("RAS input; %ld x %ld, depth: %ld", xsz, ysz, header.depth);
  46.     if (header.type==RASBYTEENCODED)
  47.         printe(", compressed");
  48.     if (header.maplength==0)
  49.         header.maptype=RMTNONE;
  50.     if (header.maptype!=RMTNONE)
  51.         printe(", with colormap");
  52.     pute('\n');
  53.     if (!outfilename)
  54.         exit(0);
  55.     if (header.depth!=1)
  56.         error0(E0_FATAL, E1_RAS, E2_FORMAT, E3_ONLYDEPTH1);
  57.     rgb=Malloc(ysz*sizeof(u_short *));
  58.     for (y=0; y<ysz; ++y)
  59.         rgb[y]=Calloc(xsz1*sizeof(u_short));
  60.     buf=Malloc(BUFSZ);
  61.     init_counter(0, (int)ysz, 10, "reading RAS");
  62.     if (header.type==RASBYTEENCODED)
  63.         read_compressed();
  64.     else
  65.         read_normal();
  66.     erase_counter(NULL);
  67.     return 1;
  68. }
  69.  
  70.  
  71. void
  72. read_normal(void)
  73. {
  74.     short *bufp;
  75.     int bufn;
  76.     int x, y;
  77.     u_short *p;
  78.     int sh, mask;
  79.  
  80.     cread_type=CREAD_VARLEN;
  81.     cread(buf, BUFSZ);
  82.     bufn=cread_result/2;
  83.     bufp=(short *)buf;
  84.     for (y=0; y<ysz; ++y) {
  85.         counter();
  86.         x=xsz1/16-1;
  87.         p=rgb[y];
  88.         do {
  89.             if (--bufn<0) {
  90.                 bufp=(short *)buf;
  91.                 cread(buf, BUFSZ);
  92.                 bufn=cread_result/2;
  93.                 if (bufn<=0)
  94.                     goto end_read_normal;
  95.             }
  96.             sh= *bufp++;
  97.             mask=0x8000;
  98.             do {
  99.                 *p++ =(sh&mask ? 0 : 0xfff);
  100.                 mask>>=1;
  101.             } while (mask);
  102.         } while (--x>=0);
  103.     }
  104. end_read_normal:
  105.     cread_type=CREAD_STRICT;
  106. }
  107.  
  108.  
  109. void
  110. read_compressed(void)
  111. {
  112.     u_char *bufp;
  113.     int bufn;
  114.     int x, y;
  115.     u_short *p;
  116.     int sh, mask, count;
  117.  
  118.     cread_type=CREAD_VARLEN;
  119.     cread(buf, BUFSZ);
  120.     bufn=cread_result;
  121.     bufp=(u_char *)buf;
  122.     y=0;
  123.     counter();
  124.     x=xsz-1;
  125.     p=rgb[y];
  126.     do {
  127.         if (--bufn<0) {
  128.             bufp=(u_char *)buf;
  129.             cread(buf, BUFSZ);
  130.             bufn=cread_result;
  131.             if (bufn<=0)
  132.                 goto end_read_compressed;
  133.         }
  134.         sh= *bufp++;
  135.         if (sh==0x80) {
  136.             if (--bufn<0) {
  137.                 bufp=(u_char *)buf;
  138.                 cread(buf, BUFSZ);
  139.                 bufn=cread_result;
  140.                 if (bufn<=0)
  141.                     goto end_read_compressed;
  142.             }
  143.             if (count= *bufp++) {
  144.                 if (--bufn<0) {
  145.                     bufp=(u_char *)buf;
  146.                     cread(buf, BUFSZ);
  147.                     bufn=cread_result;
  148.                     if (bufn<=0)
  149.                         goto end_read_compressed;
  150.                 }
  151.                 sh= *bufp++;
  152.             }
  153.         } else
  154.             count=0;
  155.         do {
  156.             mask=0x80;
  157.             do {
  158.                 *p++ =(sh&mask ? 0 : 0xfff);
  159.                 if (--x<0) {
  160.                     ++y;
  161.                     if (y>=ysz)
  162.                         goto end_read_compressed;
  163.                     counter();
  164.                     x=xsz-1;
  165.                     p=rgb[y];
  166.                     mask=0; count=0;
  167.                 }
  168.                 mask>>=1;
  169.             } while (mask);
  170.         } while (--count>=0);
  171.     } while (1);
  172. end_read_compressed:
  173.     cread_type=CREAD_STRICT;
  174. }
  175.