home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 564a.lha / wasp_v1.21 / Src.LZH / Src / readras.c < prev    next >
C/C++ Source or Header  |  1991-07-24  |  3KB  |  174 lines

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