home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga Shareware Floppies / ma01.dms / ma01.adf / wasp / src / ppm.c < prev    next >
C/C++ Source or Header  |  1991-12-29  |  3KB  |  222 lines

  1. /* wasp - Copyright 1991 by Steven Reiz
  2.  * see COPYING and wasp.c for further info,
  3.  * ppm.c, 24/7/91, 22/10/91 - 25/10/91,
  4.  * 8/12/91, 27/12/91
  5.  */
  6.  
  7. static char *sourcefile=__FILE__;
  8.  
  9. #include "wasp.h"
  10.  
  11. int
  12. read_ppm(void)
  13. {
  14.     int y;
  15.     long depth;
  16.     char typ[3];
  17.  
  18.     cread(typ, 3);
  19.     if (typ[0]!='P' || typ[1]<'0' || typ[1]>'9' || typ[2]!='\n') {
  20.         cseek_in(0L, 0);
  21.         return 0;
  22.     }
  23.     typ[2]='\0';
  24.     xsz=getn(' ');
  25.     ysz=getn('\n');
  26.     depth=getn('\n')+1;
  27.     printe("PPM/%s input; %ld x %ld, depth: %ld\n", typ, xsz, ysz, depth);
  28.     if (!outfilename)
  29.         exit(0);
  30.     if (depth!=256) {
  31.         printe("depths other than 256 are not supported\n");
  32.         exit(1);
  33.     }
  34.     rgb=Malloc(ysz*sizeof(u_short *));
  35.     for (y=0; y<ysz; ++y)
  36.             rgb[y]=Calloc(xsz*sizeof(u_short));
  37.     switch (typ[1]) {
  38.     case '5':
  39.         read_grey8();
  40.     break;
  41.     case '6':
  42.         read_rgb24();
  43.     break;
  44.     default:
  45.         printe("types other than P5 or P6 are not supported\n");
  46.         exit(1);
  47.     break;
  48.     }
  49.     return 1;
  50. }
  51.  
  52.  
  53. void
  54. write_ppm(void)
  55. {
  56.     printe("PPM/P6 output; %ld x %ld, depth: 256\n", xsz, ysz);
  57.     cwrite("P6\n", 3);
  58.     putn((int)xsz);
  59.     cwrite(" ", 1);
  60.     putn((int)ysz);
  61.     cwrite("\n255\n", 5);
  62.     write_rgb24();
  63.     erase_counter("PPM file written, %ld bytes", cseek_out(0L, 1));
  64. }
  65.  
  66.  
  67. int
  68. getn(char terminator)
  69. {
  70.     int n;
  71.     char c;
  72.  
  73.     n=0;
  74.     while (1) {
  75.         cread(&c, 1);
  76.         if (c<'0' || c>'9')
  77.             break;
  78.         n=10*n+c-'0';
  79.     }
  80.     assert(c==terminator);
  81.     return n;
  82. }
  83.  
  84.  
  85. void
  86. putn(int n)
  87. {
  88.     int i;
  89.     char line[20];
  90.  
  91.     i=19;
  92.     do {
  93.         line[i--]=n%10+'0';
  94.         n/=10;
  95.     } while (n>0);
  96.     cwrite(line+i+1, 19-i);
  97. }
  98.  
  99.  
  100. int
  101. read_hl(void)
  102. {
  103.     int y;
  104.     char typ[3];
  105.     short n;
  106.     long lon;
  107.  
  108.     cread(typ, 3);
  109.     if (typ[0]!='H' || typ[1]!='L' || typ[2]!='2') {
  110.         cseek_in(0L, 0);
  111.         return 0;
  112.     }
  113.     cread(&n, 2);
  114.     xsz=n;
  115.     cread(&n, 2);
  116.     ysz=n;
  117.     cread(&lon, 4);
  118.     printe("HL2 input; %ld x %ld\n", xsz, ysz);
  119.     if (!outfilename)
  120.         exit(0);
  121.     rgb=Malloc(ysz*sizeof(u_short *));
  122.     for (y=0; y<ysz; ++y)
  123.             rgb[y]=Calloc(xsz*sizeof(u_short));
  124.     read_rgb24();
  125.     return 1;
  126. }
  127.  
  128.  
  129. void
  130. read_rgb24(void)
  131. {
  132.     char *buf;
  133.     int width, color;
  134.     int y, x;
  135.     char *bufp;
  136.     u_short *p;
  137.  
  138.     cread_type=CREAD_NONFATAL;
  139.     width=xsz*3;
  140.     buf=Malloc(width);
  141.     init_counter(0, (int)ysz, 10, "reading 24-bits RGB");
  142.     for (y=0; y<ysz; ++y) {
  143.         counter();
  144.         cread(buf, width);
  145.         if (cread_result!=width)
  146.             break;
  147.         x=xsz-1;
  148.         bufp=buf;
  149.         p=rgb[y];
  150.         do {
  151.             color=(*bufp++ & 0xf0)<<4;
  152.             color|= *bufp++ & 0xf0;
  153.             color|=(*bufp++ >>4) & 0x0f;
  154.             *p++ =color;
  155.         } while (--x>=0);
  156.     }
  157.     erase_counter(NULL);
  158.     free(buf);
  159.     cread_type=CREAD_STRICT;
  160. }
  161.  
  162.  
  163. void
  164. read_grey8(void)
  165. {
  166.     char *buf;
  167.     int color;
  168.     int y, x;
  169.     char *bufp;
  170.     u_short *p;
  171.  
  172.     cread_type=CREAD_NONFATAL;
  173.     buf=Malloc((int)xsz);
  174.     init_counter(0, (int)ysz, 10, "reading 8-bits grey");
  175.     for (y=0; y<ysz; ++y) {
  176.         counter();
  177.         cread(buf, (int)xsz);
  178.         if (cread_result!=xsz)
  179.             break;
  180.         x=xsz-1;
  181.         bufp=buf;
  182.         p=rgb[y];
  183.         do {
  184.             color= *bufp++ & 0xf0;
  185.             color|=(color<<4)|(color>>4);
  186.             *p++ =color;
  187.         } while (--x>=0);
  188.     }
  189.     erase_counter(NULL);
  190.     free(buf);
  191.     cread_type=CREAD_STRICT;
  192. }
  193.  
  194.  
  195. void
  196. write_rgb24(void)
  197. {
  198.     char *buf;
  199.     int width, color;
  200.     int y, x;
  201.     char *bufp;
  202.     u_short *p;
  203.     
  204.     width=xsz*3;
  205.     buf=Malloc(width);
  206.     init_counter(0, (int)ysz, 10, "writing 24-bits RGB");
  207.     for (y=0; y<ysz; ++y) {
  208.         counter();
  209.         x=xsz-1;
  210.         bufp=buf;
  211.         p=rgb[y];
  212.         do {
  213.             color= *p++;
  214.             *bufp++ =(color>>4)&0xf0;
  215.             *bufp++ =color&0xf0;
  216.             *bufp++ =color<<4;
  217.         } while (--x>=0);
  218.         cwrite(buf, width);
  219.     }
  220.     free(buf);
  221. }
  222.