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

  1. From: paul@manray.asd.sgi.com (Paul Haeberli)
  2. Newsgroups: alt.sources
  3. Subject: [comp.sys.sgi] Convert IRIS image files to MacPaint format . . .
  4. Message-ID: <12137@stag.math.lsa.umich.edu>
  5. Date: 21 May 90 04:39:55 GMT
  6.  
  7. Archive-name: iristomacpaint/15-May-90
  8. Original-posting-by: paul@manray.asd.sgi.com (Paul Haeberli)
  9. Original-subject: Convert IRIS image files to MacPaint 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.  *    tomac -
  19.  *        Convert an IRIS image file to macpaint format.
  20.  *
  21.  *    to compile:
  22.  *        cc tomac.c -o tomac -limage -I/usr/include/gl
  23.  *
  24.  *                Paul Haeberli - 1989
  25.  *
  26.  */
  27. #include "image.h"
  28.  
  29. #define HEADERSIZE    512
  30.  
  31. #define MAXXSIZE    576
  32. #define MAXYSIZE    720
  33.  
  34. short sbuf[4096];
  35. unsigned char ibits[MAXXSIZE/8+20];
  36. unsigned char pbits[MAXXSIZE/8+20];
  37. char header[HEADERSIZE];
  38.  
  39. main(argc,argv) 
  40. int argc;
  41. char *argv[];
  42.     if(argc<3) {
  43.     fprintf(stderr,"usage: tomac image.bw image.mac\n");
  44.     exit(1);
  45.     }
  46.     writemac(argv[1],argv[2]);
  47. }
  48.  
  49. writemac(iname,oname)
  50. char *iname, *oname;
  51. {
  52.     FILE *outf;
  53.     IMAGE *iimage;
  54.     int i, y, n;
  55.     int xmargin, ymargin;
  56.     int xsize, ysize;
  57.  
  58.     iimage = iopen(iname,"r");
  59.     if(!iimage) {
  60.     fprintf(stderr,"tomac: can't open input file %s\n",iname);
  61.     exit(1);
  62.     }
  63.     outf = fopen(oname,"w");
  64.     if(!outf) {
  65.     fprintf(stderr,"tomac: can't open output file %s\n",iname);
  66.     exit(1);
  67.     }
  68.     xsize = iimage->xsize;
  69.     ysize = iimage->ysize;
  70.     xmargin = (MAXXSIZE-xsize)/2.0;
  71.     if(xmargin<0)
  72.     xmargin = 0;
  73.     ymargin = (MAXYSIZE-ysize)/2.0;
  74.     if(ymargin<0)
  75.     ymargin = 0;
  76.     for (i=0; i<HEADERSIZE; i++)
  77.     fputc(0,outf);
  78.     setrow(sbuf,sbuf,255,MAXXSIZE);
  79.     for(y=0; y<MAXYSIZE; y++) {
  80.      if(y>ymargin && y<(ymargin+ysize)) 
  81.         getrow(iimage,sbuf+xmargin,ysize-1-(y-ymargin),0);
  82.      else
  83.         setrow(sbuf,255,MAXXSIZE);
  84.     rowtobits(sbuf,ibits,MAXXSIZE);
  85.     n = packbits(ibits,pbits,MAXXSIZE);
  86.       fwrite(pbits,n,1,outf);
  87.     }
  88.     iclose(iimage);
  89.     fclose(outf);
  90.     return 1;
  91. }
  92.  
  93. packbits(ibits,pbits,nbits)
  94. unsigned char *ibits, *pbits;
  95. int nbits;
  96. {
  97.     int bytes;
  98.     unsigned char *sptr;
  99.     unsigned char *ibitsend;
  100.     unsigned char *optr = pbits;
  101.     int nbytes, todo, cc, count;
  102.  
  103.     nbytes = ((nbits-1)/8)+1;
  104.     ibitsend = ibits+nbytes;
  105.     while(ibits<ibitsend) {
  106.     sptr = ibits;
  107.     ibits += 2;
  108.     while((ibits<ibitsend)&&((ibits[-2]!=ibits[-1])||(ibits[-1]!=ibits[0])))
  109.         ibits++;
  110.      if(ibits != ibitsend) {
  111.         ibits -= 2;
  112.     }
  113.     count = ibits-sptr;
  114.     while(count) {
  115.         todo = count>127 ? 127:count;
  116.         count -= todo;
  117.         *optr++ = todo-1;
  118.         while(todo--)
  119.         *optr++ = *sptr++;
  120.     }
  121.     if(ibits == ibitsend)
  122.         break;
  123.     sptr = ibits;
  124.     cc = *ibits++;
  125.     while( (ibits<ibitsend) && (*ibits == cc) )
  126.         ibits++;
  127.     count = ibits-sptr;
  128.     while(count) {
  129.         todo = count>128 ? 128:count;
  130.         count -= todo;
  131.         *optr++ = 257-todo;
  132.         *optr++ = cc;
  133.     }
  134.     }
  135.     return optr - pbits;
  136. }
  137.  
  138. /*
  139.  *    row -
  140.  *        support for operations on image rows.
  141.  *
  142.  */
  143. zerorow(sptr,n)
  144. short *sptr;
  145. int n;
  146. {
  147.     bzero(sptr,n*sizeof(short));
  148. }
  149.  
  150. setrow(sptr,val,n)
  151. short *sptr;
  152. int val, n;
  153. {
  154.     if(val==0)
  155.     zerorow(sptr,n);
  156.     else {
  157.     while(n>=8) {
  158.         sptr[0] = val;
  159.         sptr[1] = val;
  160.         sptr[2] = val;
  161.         sptr[3] = val;
  162.         sptr[4] = val;
  163.         sptr[5] = val;
  164.         sptr[6] = val;
  165.         sptr[7] = val;
  166.         sptr += 8;
  167.         n -= 8;
  168.     }
  169.     while(n--) 
  170.         *sptr++ = val;
  171.     }
  172. }
  173.  
  174. bitstorow(bits,sbuf,n)
  175. unsigned char *bits;
  176. short *sbuf;
  177. int n;
  178. {
  179.     int i, val, nbytes;
  180.  
  181.     nbytes = ((n-1)/8)+1;
  182.     for(i = 0; i<nbytes; i++ ) {
  183.     val = *bits++;
  184.     if(val&0x80)
  185.         sbuf[0] = 0;
  186.     else
  187.         sbuf[0] = 255;
  188.     if(val&0x40)
  189.         sbuf[1] = 0;
  190.     else
  191.         sbuf[1] = 255;
  192.     if(val&0x20)
  193.         sbuf[2] = 0;
  194.     else
  195.         sbuf[2] = 255;
  196.     if(val&0x10)
  197.         sbuf[3] = 0;
  198.     else
  199.         sbuf[3] = 255;
  200.     if(val&0x08)
  201.         sbuf[4] = 0;
  202.     else
  203.         sbuf[4] = 255;
  204.     if(val&0x04)
  205.         sbuf[5] = 0;
  206.     else
  207.         sbuf[5] = 255;
  208.     if(val&0x02)
  209.         sbuf[6] = 0;
  210.     else
  211.         sbuf[6] = 255;
  212.     if(val&0x01)
  213.         sbuf[7] = 0;
  214.     else
  215.         sbuf[7] = 255;
  216.     sbuf += 8;
  217.     }
  218. }
  219.  
  220. rowtobits(sbuf,bits,n)
  221. short *sbuf;
  222. unsigned char *bits;
  223. int n;
  224. {
  225.     int i, val, nbytes, thresh;
  226.  
  227.     nbytes = ((n-1)/8)+1;
  228.     thresh = 128;
  229.     for(i = 0; i<nbytes; i++) {
  230.     val = 0;
  231.     if(sbuf[0]<thresh)
  232.         val |= 0x80;
  233.     if(sbuf[1]<thresh)
  234.         val |= 0x40;
  235.     if(sbuf[2]<thresh)
  236.         val |= 0x20;
  237.     if(sbuf[3]<thresh)
  238.         val |= 0x10;
  239.     if(sbuf[4]<thresh)
  240.         val |= 0x08;
  241.     if(sbuf[5]<thresh)
  242.         val |= 0x04;
  243.     if(sbuf[6]<thresh)
  244.         val |= 0x02;
  245.     if(sbuf[7]<thresh)
  246.         val |= 0x01;
  247.     sbuf += 8;
  248.     *bits++ = val;
  249.     }
  250. }
  251.