home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsm / netpbmsca / pbm / c / pbmtogem < prev    next >
Encoding:
Text File  |  1993-10-04  |  3.7 KB  |  164 lines

  1. /* pbmtogem.c - read a portable bitmap and produce a GEM .img file
  2. **
  3. ** Author: David Beckemeyer (bdt!david)
  4. **
  5. ** Much of the code for this program was taken from other
  6. ** pbmto* programs.  I just modified the code to produce
  7. ** a .img header and generate .img "Bit Strings".
  8. **
  9. ** Thanks to Diomidis D. Spinellis for the .img header format.
  10. **
  11. ** Copyright (C) 1988 by David Beckemeyer (bdt!david) and Jef Poskanzer.
  12. **
  13. ** Permission to use, copy, modify, and distribute this software and its
  14. ** documentation for any purpose and without fee is hereby granted, provided
  15. ** that the above copyright notice appear in all copies and that both that
  16. ** copyright notice and this permission notice appear in supporting
  17. ** documentation.  This software is provided "as is" without express or
  18. ** implied warranty.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include "pbm.h"
  23.  
  24. /*
  25.  * File header structure
  26.  */
  27. struct header {
  28.     short           version;/* Image file version */
  29.     unsigned short  hlen;    /* Header length in bytes */
  30.     unsigned short  planes;    /* Number of planes */
  31.     unsigned short  patlen;    /* Pattern definition length (bytes) */
  32.     unsigned short  pxlen;    /* Pixel height (microns) */
  33.     unsigned short  pxht;    /* Pixel height (microns) */
  34.     unsigned short  linewid;/* Scan line width (pixels) */
  35.     unsigned short  nitems;    /* Number of scan line items */
  36. };
  37.  
  38. #define MAXCOL 60
  39. static unsigned short outrow[MAXCOL];
  40.  
  41. static void putinit ARGS(( struct header* hdr ));
  42. static void putbit ARGS(( bit b ));
  43. static void putrest ARGS(( void ));
  44. static void putitem ARGS(( void ));
  45. static void putrow ARGS(( void ));
  46.  
  47. int
  48. main( argc, argv )
  49.     int argc;
  50.     char* argv[];
  51.     {
  52.     FILE* ifp;
  53.     bit* bitrow;
  54.     register bit* bP;
  55.     int rows, cols, format, row, col, pad;
  56.     struct header hd;
  57.  
  58.  
  59.     pbm_init( &argc, argv );
  60.  
  61.     if ( argc > 2 )
  62.     pm_usage( "[pbmfile]" );
  63.  
  64.     if ( argc == 2 )
  65.     ifp = pm_openr( argv[1] );
  66.     else
  67.     ifp = stdin;
  68.  
  69.     pbm_readpbminit( ifp, &cols, &rows, &format );
  70.     
  71.     if (cols > MAXCOL * 16)
  72.     cols = MAXCOL * 16;
  73.  
  74.     bitrow = pbm_allocrow( cols );
  75.     
  76.     hd.version = 1;    /* Image file version */
  77.     hd.hlen = 16;    /* Header length in bytes */
  78.     hd.planes = 1;    /* Number of planes */
  79.     hd.patlen = 2;    /* Pattern definition length (bytes) */
  80.     hd.pxlen = 372;    /* Pixel height (microns) */
  81.     hd.pxht = 372;    /* Pixel height (microns) */
  82.     hd.linewid = ((cols + 15) / 16) * 16;    /* Scan line width (pixels) */
  83.     hd.nitems = rows;    /* Number of scan line items */
  84.  
  85.     pad = hd.linewid - cols;
  86.  
  87.     putinit( &hd );
  88.     for ( row = 0; row < rows; ++row )
  89.     {
  90.     pbm_readpbmrow( ifp, bitrow, cols, format );
  91.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  92.         putbit( *bP );
  93.         for ( col = 0; col < pad; ++col )
  94.         putbit( 0 );
  95.         }
  96.  
  97.     pm_close( ifp );
  98.  
  99.     putrest( );
  100.  
  101.     exit( 0 );
  102.     }
  103.  
  104. static short item, outcol, outmax;
  105. static short bitsperitem, bitshift;
  106.  
  107. static void
  108. putinit( hdr )
  109.     struct header* hdr;
  110.     {
  111.     fwrite( hdr, 32, 1, stdout );
  112.     item = 0;
  113.     bitsperitem = 0;
  114.     bitshift = 15;
  115.     outcol = 0;
  116.     outmax = hdr->linewid / 16;
  117.     }
  118.  
  119. #if __STDC__
  120. static void
  121. putbit( bit b )
  122. #else /*__STDC__*/
  123. static void
  124. putbit( b )
  125.     bit b;
  126. #endif /*__STDC__*/
  127.     {
  128.     if ( bitsperitem == 16 )
  129.     putitem( );
  130.     ++bitsperitem;
  131.     if ( b == PBM_BLACK )
  132.     item += 1 << bitshift;
  133.     --bitshift;
  134.     }
  135.  
  136. static void
  137. putrest( )
  138.     {
  139.     if ( bitsperitem > 0 )
  140.     putitem( );
  141.     if ( outcol > 0 )
  142.     putrow( );
  143.     }
  144.  
  145. static void
  146. putitem( )
  147.     {
  148.     outrow[outcol++] = item;
  149.     if (outcol >= outmax)
  150.     putrow( );
  151.     item = 0;
  152.     bitsperitem = 0;
  153.     bitshift = 15;
  154.     }
  155.  
  156. static void
  157. putrow( )
  158.     {
  159.     (void) putc(0x80, stdout);        /* a Bit string */
  160.     (void) putc(outcol*2, stdout);    /* count */
  161.     fwrite( outrow, outcol*2, 1, stdout );
  162.     outcol = 0;
  163.     }        
  164.