home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pbm / pbmtopi3.c < prev    next >
C/C++ Source or Header  |  1994-01-31  |  3KB  |  124 lines

  1. /* pbmtopi3.c - read a portable bitmap and produce a Atari Degas .pi3 file
  2. **
  3. ** Module created from other pbmplus tools by David Beckemeyer.
  4. **
  5. ** Copyright (C) 1988 by David Beckemeyer and Jef Poskanzer.
  6. **
  7. ** Permission to use, copy, modify, and distribute this software and its
  8. ** documentation for any purpose and without fee is hereby granted, provided
  9. ** that the above copyright notice appear in all copies and that both that
  10. ** copyright notice and this permission notice appear in supporting
  11. ** documentation.  This software is provided "as is" without express or
  12. ** implied warranty.
  13. */
  14.  
  15. #include <stdio.h>
  16. #include "pbm.h"
  17.  
  18. static void putinit ARGS(( void ));
  19. static void putbit ARGS(( bit b ));
  20. static void putrest ARGS(( void ));
  21. static void putitem ARGS(( void ));
  22.  
  23. int
  24. main( argc, argv )
  25.     int argc;
  26.     char* argv[];
  27.     {
  28.     FILE* ifp;
  29.     bit* bitrow;
  30.     register bit* bP;
  31.     int rows, cols, format, padright, row, col;
  32.  
  33.  
  34.     pbm_init( &argc, argv );
  35.  
  36.     if ( argc > 2 )
  37.     pm_usage( "[pbmfile]" );
  38.  
  39.     if ( argc == 2 )
  40.     ifp = pm_openr( argv[1] );
  41.     else
  42.     ifp = stdin;
  43.  
  44.     pbm_readpbminit( ifp, &cols, &rows, &format );
  45.     if (cols > 640)
  46.     cols = 640;
  47.     if (rows > 400)
  48.     rows = 400;
  49.     bitrow = pbm_allocrow( cols );
  50.     
  51.     /* Compute padding to round cols up to 640 */
  52.     padright = 640 - cols;
  53.  
  54.     putinit( );
  55.     for ( row = 0; row < rows; ++row )
  56.     {
  57.     pbm_readpbmrow( ifp, bitrow, cols, format );
  58.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  59.         putbit( *bP );
  60.     for ( col = 0; col < padright; ++col )
  61.         putbit( 0 );
  62.         }
  63.     while (row++ < 400)
  64.     for ( col = 0; col < 640; ++col)
  65.         putbit( 0 );
  66.  
  67.     pm_close( ifp );
  68.  
  69.     putrest( );
  70.  
  71.     exit( 0 );
  72.     }
  73.  
  74. static char item;
  75. static short bitsperitem, bitshift;
  76.  
  77. static void
  78. putinit( )
  79.     {
  80.     int i;
  81.     if (pm_writebigshort (stdout, (short) 2) == -1
  82.     || pm_writebigshort (stdout, (short) 0x777) == -1)
  83.       pm_error ("write error");
  84.     for (i = 1; i < 16; i++)
  85.       if (pm_writebigshort (stdout, (short) 0) == -1)
  86.     pm_error ("write error");
  87.     item = 0;
  88.     bitsperitem = 0;
  89.     bitshift = 7;
  90.     }
  91.  
  92. #if __STDC__
  93. static void
  94. putbit( bit b )
  95. #else /*__STDC__*/
  96. static void
  97. putbit( b )
  98.     bit b;
  99. #endif /*__STDC__*/
  100.     {
  101.     if (bitsperitem == 8)
  102.     putitem( );
  103.     ++bitsperitem;
  104.     if ( b == PBM_BLACK )
  105.     item += 1 << bitshift;
  106.     --bitshift;
  107.     }
  108.  
  109. static void
  110. putrest( )
  111.     {
  112.     if ( bitsperitem > 0 )
  113.     putitem( );
  114.     }
  115.  
  116. static void
  117. putitem( )
  118.     {
  119.     putc (item, stdout);
  120.     item = 0;
  121.     bitsperitem = 0;
  122.     bitshift = 7;
  123.     }
  124.