home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1357 < prev    next >
Internet Message Format  |  1990-12-28  |  4KB

  1. From: paul@manray.asd.sgi.com (Paul Haeberli)
  2. Newsgroups: alt.sources
  3. Subject: [comp.sys.sgi] Convert MacPaint files to IRIS image file format.
  4. Message-ID: <12138@stag.math.lsa.umich.edu>
  5. Date: 21 May 90 04:40:11 GMT
  6.  
  7. Archive-name: macpainttoiris/15-May-90
  8. Original-posting-by: paul@manray.asd.sgi.com (Paul Haeberli)
  9. Original-subject: Convert MacPaint files to IRIS image file format.
  10. Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)
  11.  
  12. [This is an experimental alt.sources re-posting from the newsgroup(s)
  13. comp.sys.sgi. Comments on this service to emv@math.lsa.umich.edu 
  14. (Edward Vielmetti).]
  15.  
  16.  
  17. /*
  18.  *    frommac -
  19.  *        Convert macpaint image files to IRIS image files.
  20.  *
  21.  *                Paul Haeberli - 1989
  22.  */
  23. #include "image.h"
  24.  
  25. #define MAXXSIZE    576
  26. #define MAXYSIZE    720
  27.  
  28. short sbuf[4096];
  29. unsigned char cbuf[72+256];
  30.  
  31. main(argc,argv) 
  32. int argc;
  33. char *argv[];
  34.     if(argc<3) {
  35.     fprintf(stderr,"usage: frommac image.mac image.bw\n");
  36.     exit(1);
  37.     }
  38.     if(readmac(argv[1],argv[2],512)) 
  39.     exit(0);
  40.     else if(readmac(argv[1],argv[2],512+128))
  41.     exit(0);
  42.     else {
  43.     fprintf(stderr,"frommac: bad macpaint file %s\n",argv[1]);
  44.     exit(1);
  45.     }
  46. }
  47.  
  48. readmac(iname,oname,offset)
  49. char *iname, *oname;
  50. int offset;
  51. {
  52.     FILE *inf;
  53.     IMAGE *oimage;
  54.     int i, y;
  55.  
  56.     inf = fopen(iname,"r");
  57.     if(!inf) {
  58.     fprintf(stderr,"frommac: can't open input file %s\n",iname);
  59.     exit(1);
  60.     }
  61.     oimage = iopen(oname,"w",RLE(1),2,MAXXSIZE,MAXYSIZE,1);
  62.     for (i=0; i<offset; i++)
  63.     getc(inf);
  64.     for(y=0; y<MAXYSIZE; y++) {
  65.     if(!readline(inf)) {
  66.         iclose(oimage);
  67.         fclose(inf);
  68.         return 0;
  69.         break;
  70.     }
  71.     bitstorow(cbuf,sbuf,MAXXSIZE);
  72.     putrow(oimage,sbuf,MAXYSIZE-1-y,0);
  73.     }
  74.     iclose(oimage);
  75.     fclose(inf);
  76.     fprintf(stderr,"head size was %d\n",offset);
  77.     return 1;
  78. }
  79.  
  80. readline(inf)
  81. FILE *inf;
  82. {
  83.     int pos, cnt, val;
  84.  
  85.     pos = 0;
  86.     while(pos < 72) {
  87.     cnt = getc(inf);
  88.     if((cnt&0x80)==0) {
  89.         cnt++;            
  90.         while(cnt--)
  91.         cbuf[pos++] = getc(inf);
  92.     } else {    
  93.         cnt = 257-cnt;
  94.         val = getc(inf);     
  95.         while (cnt--)
  96.         cbuf[pos++] = val;
  97.     }
  98.     }
  99.     if(pos==72) 
  100.     return 1;
  101.     else
  102.     return 0;
  103. }
  104.  
  105. /*
  106.  *    row -
  107.  *        support for operations on image rows.
  108.  *
  109.  */
  110. zerorow(sptr,n)
  111. short *sptr;
  112. int n;
  113. {
  114.     bzero(sptr,n*sizeof(short));
  115. }
  116.  
  117. setrow(sptr,val,n)
  118. short *sptr;
  119. int val, n;
  120. {
  121.     if(val==0)
  122.     zerorow(sptr,n);
  123.     else {
  124.     while(n>=8) {
  125.         sptr[0] = val;
  126.         sptr[1] = val;
  127.         sptr[2] = val;
  128.         sptr[3] = val;
  129.         sptr[4] = val;
  130.         sptr[5] = val;
  131.         sptr[6] = val;
  132.         sptr[7] = val;
  133.         sptr += 8;
  134.         n -= 8;
  135.     }
  136.     while(n--) 
  137.         *sptr++ = val;
  138.     }
  139. }
  140.  
  141. bitstorow(bits,sbuf,n)
  142. unsigned char *bits;
  143. short *sbuf;
  144. int n;
  145. {
  146.     int i, val, nbytes;
  147.  
  148.     nbytes = ((n-1)/8)+1;
  149.     for(i = 0; i<nbytes; i++ ) {
  150.     val = *bits++;
  151.     if(val&0x80)
  152.         sbuf[0] = 0;
  153.     else
  154.         sbuf[0] = 255;
  155.     if(val&0x40)
  156.         sbuf[1] = 0;
  157.     else
  158.         sbuf[1] = 255;
  159.     if(val&0x20)
  160.         sbuf[2] = 0;
  161.     else
  162.         sbuf[2] = 255;
  163.     if(val&0x10)
  164.         sbuf[3] = 0;
  165.     else
  166.         sbuf[3] = 255;
  167.     if(val&0x08)
  168.         sbuf[4] = 0;
  169.     else
  170.         sbuf[4] = 255;
  171.     if(val&0x04)
  172.         sbuf[5] = 0;
  173.     else
  174.         sbuf[5] = 255;
  175.     if(val&0x02)
  176.         sbuf[6] = 0;
  177.     else
  178.         sbuf[6] = 255;
  179.     if(val&0x01)
  180.         sbuf[7] = 0;
  181.     else
  182.         sbuf[7] = 255;
  183.     sbuf += 8;
  184.     }
  185. }
  186.  
  187. rowtobits(sbuf,bits,n)
  188. short *sbuf;
  189. unsigned char *bits;
  190. int n;
  191. {
  192.     int i, val, nbytes, thresh;
  193.  
  194.     nbytes = ((n-1)/8)+1;
  195.     thresh = 128;
  196.     for(i = 0; i<nbytes; i++) {
  197.     val = 0;
  198.     if(sbuf[0]<thresh)
  199.         val |= 0x80;
  200.     if(sbuf[1]<thresh)
  201.         val |= 0x40;
  202.     if(sbuf[2]<thresh)
  203.         val |= 0x20;
  204.     if(sbuf[3]<thresh)
  205.         val |= 0x10;
  206.     if(sbuf[4]<thresh)
  207.         val |= 0x08;
  208.     if(sbuf[5]<thresh)
  209.         val |= 0x04;
  210.     if(sbuf[6]<thresh)
  211.         val |= 0x02;
  212.     if(sbuf[7]<thresh)
  213.         val |= 0x01;
  214.     sbuf += 8;
  215.     *bits++ = val;
  216.     }
  217. }
  218.