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

  1. /* pbmtox10bm.c - read a portable bitmap and produce an X10 bitmap file
  2. **
  3. ** Copyright (C) 1988 by Jef Poskanzer.
  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 "pbm.h"
  14.  
  15. int
  16. main( argc, argv )
  17.     int argc;
  18.     char* argv[];
  19.     {
  20.     FILE* ifp;
  21.     bit* bitrow;
  22.     register bit* bP;
  23.     int rows, cols, format, padright, row;
  24.     register int col;
  25.     char name[100];
  26.     char* cp;
  27.     int itemsperline;
  28.     register int bitsperitem;
  29.     register int item;
  30.     int firstitem;
  31.     char* hexchar = "0123456789abcdef";
  32.  
  33.  
  34.     pbm_init( &argc, argv );
  35.  
  36.     if ( argc > 2 )
  37.     pm_usage( "[pbmfile]" );
  38.  
  39.     if ( argc == 2 )
  40.     {
  41.     ifp = pm_openr( argv[1] );
  42.     strcpy( name, argv[1] );
  43.     if ( strcmp( name, "-" ) == 0 )
  44.         strcpy( name, "noname" );
  45.  
  46.     if ( ( cp = index( name, '.' ) ) != 0 )
  47.         *cp = '\0';
  48.     }
  49.     else
  50.     {
  51.     ifp = stdin;
  52.     strcpy( name, "noname" );
  53.     }
  54.  
  55.     pbm_readpbminit( ifp, &cols, &rows, &format );
  56.     bitrow = pbm_allocrow( cols );
  57.  
  58.     /* Compute padding to round cols up to the nearest multiple of 16. */
  59.     padright = ( ( cols + 15 ) / 16 ) * 16 - cols;
  60.  
  61.     printf( "#define %s_width %d\n", name, cols );
  62.     printf( "#define %s_height %d\n", name, rows );
  63.     printf( "static short %s_bits[] = {\n", name );
  64.  
  65.     itemsperline = 0;
  66.     bitsperitem = 0;
  67.     item = 0;
  68.     firstitem = 1;
  69.  
  70. #define PUTITEM \
  71.     { \
  72.     if ( firstitem ) \
  73.     firstitem = 0; \
  74.     else \
  75.     putchar( ',' ); \
  76.     if ( itemsperline == 11 ) \
  77.     { \
  78.     putchar( '\n' ); \
  79.     itemsperline = 0; \
  80.     } \
  81.     if ( itemsperline == 0 ) \
  82.     putchar( ' ' ); \
  83.     ++itemsperline; \
  84.     putchar('0'); \
  85.     putchar('x'); \
  86.     putchar(hexchar[item >> 12]); \
  87.     putchar(hexchar[(item >> 8) & 15]); \
  88.     putchar(hexchar[(item >> 4) & 15]); \
  89.     putchar(hexchar[item & 15]); \
  90.     bitsperitem = 0; \
  91.     item = 0; \
  92.     }
  93.  
  94. #define PUTBIT(b) \
  95.     { \
  96.     if ( bitsperitem == 16 ) \
  97.     PUTITEM; \
  98.     if ( (b) == PBM_BLACK ) \
  99.     item += 1 << bitsperitem; \
  100.     ++bitsperitem; \
  101.     }
  102.  
  103.     for ( row = 0; row < rows; ++row )
  104.     {
  105.     pbm_readpbmrow( ifp, bitrow, cols, format );
  106.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  107.         PUTBIT(*bP);
  108.     for ( col = 0; col < padright; ++col )
  109.         PUTBIT(0);
  110.         }
  111.  
  112.     pm_close( ifp );
  113.     
  114.     if ( bitsperitem > 0 )
  115.     PUTITEM;
  116.     printf( "};\n" );
  117.  
  118.     exit( 0 );
  119.     }
  120.