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

  1. /* pbmtoxbm.c - read a portable bitmap and produce an X11 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 8. */
  59.     padright = ( ( cols + 7 ) / 8 ) * 8 - cols;
  60.  
  61.     printf( "#define %s_width %d\n", name, cols );
  62.     printf( "#define %s_height %d\n", name, rows );
  63.     printf( "static char %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 == 15 ) \
  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 >> 4]); \
  87.     putchar(hexchar[item & 15]); \
  88.     bitsperitem = 0; \
  89.     item = 0; \
  90.     }
  91.  
  92. #define PUTBIT(b) \
  93.     { \
  94.     if ( bitsperitem == 8 ) \
  95.     PUTITEM; \
  96.     if ( (b) == PBM_BLACK ) \
  97.     item += 1 << bitsperitem; \
  98.     ++bitsperitem; \
  99.     }
  100.  
  101.     for ( row = 0; row < rows; ++row )
  102.     {
  103.     pbm_readpbmrow( ifp, bitrow, cols, format );
  104.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  105.         PUTBIT(*bP);
  106.     for ( col = 0; col < padright; ++col )
  107.         PUTBIT(0);
  108.         }
  109.  
  110.     if ( ifp != stdin )
  111.     fclose( ifp );
  112.  
  113.     if ( bitsperitem > 0 )
  114.     PUTITEM;
  115.     printf( "};\n" );
  116.  
  117.     exit( 0 );
  118.     }
  119.