home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume2 / pbm / Part3 / pbmtoxbm.c < prev    next >
C/C++ Source or Header  |  1991-08-07  |  3KB  |  135 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 <stdio.h>
  14. #ifdef    OS_SYSV
  15. #include <string.h>
  16. #else    OS_SYSV
  17. #include <strings.h>
  18. #endif    OS_SYSV
  19. #include "pbm.h"
  20.  
  21. main( argc, argv )
  22. int argc;
  23. char *argv[];
  24.     {
  25.     FILE *ifd;
  26.     bit **bits;
  27.     int rows, cols, rucols, padright, row, col;
  28.     char name[100], *cp;
  29.  
  30.     if ( argc > 2 )
  31.     {
  32.     fprintf( stderr, "usage:  %s [pbmfile]\n", argv[0] );
  33.     exit( 1 );
  34.     }
  35.  
  36.     if ( argc == 2 )
  37.     {
  38.         ifd = fopen( argv[1], "r" );
  39.         if ( ifd == NULL )
  40.         {
  41.         fprintf( stderr, "%s: can't open.\n", argv[1] );
  42.         exit( 1 );
  43.         }
  44.     strcpy( name, argv[1] );
  45.  
  46. #ifdef    OS_SYSV
  47.     if ( ( cp = strchr( name, '.' ) ) != 0 )
  48. #else    OS_SYSV
  49.     if ( ( cp = index( name, '.' ) ) != 0 )
  50. #endif    OS_SYSV
  51.         *cp = '\0';
  52.     }
  53.     else
  54.     {
  55.     ifd = stdin;
  56.     strcpy( name, "noname" );
  57.     }
  58.  
  59.     bits = pbm_readpbm( ifd, &cols, &rows );
  60.  
  61.     if ( ifd != stdin )
  62.     fclose( ifd );
  63.     
  64.     /* Round cols up to the nearest multiple of 8. */
  65.     rucols = ( cols + 7 ) / 8;
  66.     rucols = rucols * 8;
  67.     padright = rucols - cols;
  68.  
  69.     printf( "#define %s_width %d\n", name, cols );
  70.     printf( "#define %s_height %d\n", name, rows );
  71.     printf( "static char %s_bits[] = {\n", name );
  72.  
  73.     putinit( );
  74.     for ( row = 0; row < rows; row++ )
  75.     {
  76.         for ( col = 0; col < cols; col++ )
  77.         putbit( bits[row][col] );
  78.     for ( col = 0; col < padright; col++ )
  79.         putbit( 0 );
  80.         }
  81.     putrest( );
  82.  
  83.     exit( 0 );
  84.     }
  85.  
  86.  
  87. int item, bitsperitem, bitshift, itemsperline, firstitem;
  88.  
  89. putinit( )
  90.     {
  91.     itemsperline = 0;
  92.     bitsperitem = 0;
  93.     item = 0;
  94.     bitshift = 0;
  95.     firstitem = 1;
  96.     }
  97.  
  98. putbit( b )
  99. bit b;
  100.     {
  101.     if ( bitsperitem == 8 )
  102.     putitem( );
  103.     bitsperitem++;
  104.     if ( b )
  105.     item += 1 << bitshift;
  106.     bitshift++;
  107.     }
  108.  
  109. putrest( )
  110.     {
  111.     if ( bitsperitem > 0 )
  112.     putitem( );
  113.     printf( "};\n" );
  114.     }
  115.  
  116. putitem( )
  117.     {
  118.     if ( firstitem )
  119.     firstitem = 0;
  120.     else
  121.     printf( "," );
  122.     if ( itemsperline == 15 )
  123.     {
  124.     putchar( '\n' );
  125.     itemsperline = 0;
  126.     }
  127.     if ( itemsperline == 0 )
  128.     printf( " " );
  129.     itemsperline++;
  130.     printf( "0x%02x", item );
  131.     bitsperitem = 0;
  132.     item = 0;
  133.     bitshift = 0;
  134.     }
  135.