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

  1. /* ybmtopbm.c - read a file from Bennet Yee's 'xbm' program and write a pbm.
  2. **
  3. ** Written by Jamie Zawinski based on code (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. #include "pbm.h"
  15.  
  16. static void getinit ARGS(( FILE* file, short* colsP, short* rowsP, short* depthP, short* padrightP ));
  17. static bit getbit ARGS(( FILE* file ));
  18.  
  19. #define YBM_MAGIC  ( ( '!' << 8 ) | '!' )
  20.  
  21. int
  22. main( argc, argv )
  23.     int argc;
  24.     char* argv[];
  25.     {
  26.     FILE* ifp;
  27.     bit* bitrow;
  28.     register bit* bP;
  29.     short rows, cols, padright, row, col;
  30.     short depth;
  31.  
  32.     pbm_init( &argc, argv );
  33.  
  34.     if ( argc > 2 )
  35.     pm_usage( "[ybmfile]" );
  36.  
  37.     if ( argc == 2 )
  38.     ifp = pm_openr( argv[1] );
  39.     else
  40.     ifp = stdin;
  41.  
  42.     getinit( ifp, &cols, &rows, &depth, &padright );
  43.     if ( depth != 1 )
  44.     pm_error(
  45.         "YBM file has depth of %d, must be 1",
  46.         (int) depth );
  47.  
  48.     pbm_writepbminit( stdout, cols, rows, 0 );
  49.     bitrow = pbm_allocrow( cols );
  50.  
  51.     for ( row = 0; row < rows; ++row )
  52.     {
  53.     /* Get data. */
  54.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  55.         *bP = getbit( ifp );
  56.     /* Discard line padding */
  57.         for ( col = 0; col < padright; ++col )
  58.         (void) getbit( ifp );
  59.     pbm_writepbmrow( stdout, bitrow, cols, 0 );
  60.     }
  61.  
  62.     pm_close( ifp );
  63.     pm_close( stdout );
  64.  
  65.     exit( 0 );
  66.     }
  67.  
  68. static int item;
  69. static int bitsperitem, bitshift;
  70.  
  71. static void
  72. getinit( file, colsP, rowsP, depthP, padrightP )
  73.     FILE* file;
  74.     short* colsP;
  75.     short* rowsP;
  76.     short* depthP;
  77.     short* padrightP;
  78.     {
  79.     short magic;
  80.  
  81.     if ( pm_readbigshort( file, &magic ) == -1 )
  82.     pm_error( "EOF / read error" );
  83.     if ( magic != YBM_MAGIC )
  84.     pm_error( "bad magic number in YBM file" );
  85.     if ( pm_readbigshort( file, colsP ) == -1 )
  86.     pm_error( "EOF / read error" );
  87.       if ( pm_readbigshort( file, rowsP ) == -1 )
  88.     pm_error( "EOF / read error" );
  89.  
  90.     *depthP = 1;
  91.     *padrightP = ( ( *colsP + 15 ) / 16 ) * 16 - *colsP;
  92.     bitsperitem = 0;
  93.     }
  94.  
  95. static bit
  96. getbit( file )
  97.     FILE* file;
  98.     {
  99.     bit b;
  100.  
  101.     if ( bitsperitem == 0 )
  102.     {
  103.     item = getc(file) | getc(file)<<8;
  104.     if ( item == EOF )
  105.         pm_error( "EOF / read error" );
  106.     bitsperitem = 16;
  107.     bitshift = 0;
  108.     }
  109.     b = ( ( item >> bitshift) & 1 ) ? PBM_BLACK : PBM_WHITE;
  110.     --bitsperitem;
  111.     ++bitshift;
  112.     return b;
  113.     }
  114.