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

  1. /* pbmtozinc.c - read a portable bitmap and produce an bitmap file
  2. **               in the format used by the Zinc Interface Library (v1.0)
  3. **               November 1990.
  4. **
  5. ** Author: James Darrell McCauley
  6. **         Department of Agricultural Engineering
  7. **         Texas A&M University
  8. **         College Station, Texas 77843-2117 USA
  9. **
  10. ** Copyright (C) 1988 by James Darrell McCauley (jdm5548@diamond.tamu.edu)
  11. **                    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. int
  25. main( argc, argv )
  26.     int argc;
  27.     char* argv[];
  28.     {
  29.     FILE* ifp;
  30.     bit* bitrow;
  31.     register bit* bP;
  32.     int rows, cols, format, padright, row;
  33.     register int col;
  34.     char name[100];
  35.     char* cp;
  36.     int itemsperline;
  37.     register int bitsperitem;
  38.     register int item;
  39.     int firstitem;
  40.     char* hexchar = "084c2a6e195d3b7f";
  41.  
  42.  
  43.     pbm_init( &argc, argv );
  44.  
  45.     if ( argc > 2 )
  46.     pm_usage( "[pbmfile]" );
  47.  
  48.     if ( argc == 2 )
  49.     {
  50.     ifp = pm_openr( argv[1] );
  51.     strcpy( name, argv[1] );
  52.     if ( strcmp( name, "-" ) == 0 )
  53.         strcpy( name, "noname" );
  54.  
  55.     if ( ( cp = index( name, '.' ) ) != 0 )
  56.         *cp = '\0';
  57.     }
  58.     else
  59.     {
  60.     ifp = stdin;
  61.     strcpy( name, "noname" );
  62.     }
  63.  
  64.     pbm_readpbminit( ifp, &cols, &rows, &format );
  65.     bitrow = pbm_allocrow( cols );
  66.  
  67.     /* Compute padding to round cols up to the nearest multiple of 16. */
  68.     padright = ( ( cols + 15 ) / 16 ) * 16 - cols;
  69.  
  70.     printf( "USHORT %s[] = {\n",name);
  71.     printf( "  %d\n", cols );
  72.     printf( "  %d\n", rows );
  73.  
  74.     itemsperline = 0;
  75.     bitsperitem = 0;
  76.     item = 0;
  77.     firstitem = 1;
  78.  
  79. #define PUTITEM \
  80.     { \
  81.     if ( firstitem ) \
  82.     firstitem = 0; \
  83.     else \
  84.     putchar( ',' ); \
  85.     if ( itemsperline == 11 ) \
  86.     { \
  87.     putchar( '\n' ); \
  88.     itemsperline = 0; \
  89.     } \
  90.     if ( itemsperline == 0 ) \
  91.     putchar( ' ' ); \
  92.     ++itemsperline; \
  93.     putchar('0'); \
  94.     putchar('x'); \
  95.     putchar(hexchar[item & 15]); \
  96.     putchar(hexchar[(item >> 4) & 15]); \
  97.     putchar(hexchar[(item >> 8) & 15]); \
  98.     putchar(hexchar[item >> 12]); \
  99.     bitsperitem = 0; \
  100.     item = 0; \
  101.     }
  102.  
  103. #define PUTBIT(b) \
  104.     { \
  105.     if ( bitsperitem == 16 ) \
  106.     PUTITEM; \
  107.     if ( (b) == PBM_BLACK ) \
  108.     item += 1 << bitsperitem; \
  109.     ++bitsperitem; \
  110.     }
  111.  
  112.     for ( row = 0; row < rows; ++row )
  113.     {
  114.     pbm_readpbmrow( ifp, bitrow, cols, format );
  115.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  116.         PUTBIT( *bP );
  117.     for ( col = 0; col < padright; ++col )
  118.         PUTBIT( 0 );
  119.         }
  120.  
  121.     pm_close( ifp );
  122.     
  123.     if ( bitsperitem > 0 )
  124.     PUTITEM;
  125.     printf( "};\n" );
  126.  
  127.     exit( 0 );
  128.     }
  129.