home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / ppm / gouldtoppm.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  3KB  |  123 lines

  1. /* gouldtoppm.c - read GOULD imaging system files and write a portable pixmap
  2. **
  3. ** Copyright (C) 1990  Stephen P. Lesniewski
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include "ppm.h"
  15.  
  16. #define MAXVAL 255
  17.  
  18. static void getgouldheader ARGS(( FILE *infile, unsigned long *cols, unsigned long *nlines, unsigned long *bytesperline, unsigned long *bitsperpixel, unsigned long *NB ));
  19.  
  20. int
  21. main( argc, argv )
  22. int argc;
  23. char *argv[];
  24. {
  25.     FILE *ifp;
  26.     pixel *pixrow;
  27.  
  28.     unsigned long cols, nlines, bytesperline, bitsperpixel, NB, x, y;
  29.     unsigned char pbuffer[4];
  30.  
  31.     int color_type;
  32.  
  33.  
  34.     ppm_init( &argc, argv );
  35.  
  36.     if ( argc > 2 )
  37.         pm_usage( "[gouldfile]" );
  38.  
  39.     if ( argc == 2 )
  40.         ifp = pm_openr( argv[1] );
  41.     else
  42.         ifp = stdin;
  43.  
  44.     getgouldheader( ifp, &cols, &nlines, &bytesperline, &bitsperpixel, &NB);
  45.  
  46.     ppm_writeppminit(stdout, cols, nlines, MAXVAL, 0);
  47.  
  48.     color_type = bitsperpixel/8;
  49.     if (color_type == 0) color_type = NB;
  50.  
  51.     pixrow = ppm_allocrow(cols);
  52.  
  53.     for (y = 0; y < nlines; ++y)
  54.     {
  55.         for (x = 0; x < cols; ++x)
  56.         {
  57.             switch (color_type)
  58.             {
  59.             case 0:
  60.                 pm_error("incorrect color type" );
  61.  
  62.             case 1:
  63.                 fread(pbuffer, 1, 1, ifp);
  64.                 PPM_ASSIGN( pixrow[x], pbuffer[0], pbuffer[0], pbuffer[0]);
  65.                 break;
  66.  
  67.             case 2:
  68.                 fread(pbuffer, 2, 1, ifp);
  69.                 PPM_ASSIGN( pixrow[x], pbuffer[0], pbuffer[1], pbuffer[1]);
  70.                 break;
  71.  
  72.             case 3:
  73.                 fread(pbuffer, 3, 1, ifp);
  74.                 PPM_ASSIGN( pixrow[x], pbuffer[0], pbuffer[1], pbuffer[2]);
  75.                 break;
  76.  
  77.             default :
  78.                 fread(pbuffer, 3, 1, ifp);
  79.                 PPM_ASSIGN( pixrow[x], pbuffer[0], pbuffer[1], pbuffer[2]);
  80.                 break;
  81.  
  82.             } /* switch */
  83.         }
  84.         ppm_writeppmrow(stdout, pixrow, cols, MAXVAL, 0);
  85.     }
  86.  
  87.     pm_close(ifp);
  88.     pm_close(stdout);
  89.  
  90.     exit(0);
  91. }
  92.  
  93. static void
  94. getgouldheader( infile, cols, nlines, bytesperline, bitsperpixel, NB)
  95. FILE *infile;
  96. unsigned long *cols, *nlines, *bytesperline, *bitsperpixel, *NB;
  97. {
  98.     unsigned long nlines_new, bytesperline_new, numheaderrec;
  99.     unsigned char headerblk[512];
  100.     int i;
  101.  
  102.     if (fread(headerblk, 512, 1, infile) == 0)
  103.         pm_error("cannot read gould header" );
  104.  
  105.     *nlines       = (headerblk[3]<<8) | headerblk[2];
  106.     *bytesperline = (headerblk[5]<<8) | headerblk[4];
  107.     *bitsperpixel = (headerblk[7]<<8) | headerblk[6];
  108.     numheaderrec  = (headerblk[9]<<8) | headerblk[8];
  109.     nlines_new    = (headerblk[15]<<24)| (headerblk[14]<<16) | (headerblk[13]<<8) | (headerblk[12]);
  110.     bytesperline_new = (headerblk[19]<<24)| (headerblk[18]<<16) | (headerblk[17]<<8) | (headerblk[16]);
  111.     *NB           = (headerblk[23]<<24)| (headerblk[22]<<16) | (headerblk[21]<<8) | (headerblk[20]);
  112.  
  113.     if (numheaderrec > 1)
  114.         for (i = 1 ; i <numheaderrec; ++i)
  115.             if (fread(headerblk, 512, 1, infile) == 0)
  116.                 pm_error("cannot read gould header(2nd)" );
  117.  
  118.     if (*nlines==0) *nlines=nlines_new;
  119.     if (*bytesperline==0) *bytesperline=bytesperline_new;
  120.  
  121.     *cols = (*bytesperline)*8 / (*bitsperpixel);
  122. }
  123.