home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsm / netpbmsca / pbm / c / pbmtopi3 < prev    next >
Encoding:
Text File  |  1993-10-04  |  2.4 KB  |  126 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 short item;
  75. static short bitsperitem, bitshift;
  76.  
  77. static void
  78. putinit( )
  79.     {
  80.     struct degasHDR {
  81.     short res;
  82.     short pal[16];
  83.     } hdr;
  84.  
  85.     hdr.res = 2;
  86.     hdr.pal[0] = 0x0777;
  87.     hdr.pal[1] = 0x0700;
  88.     fwrite( &hdr, sizeof(hdr), 1, stdout );
  89.     item = 0;
  90.     bitsperitem = 0;
  91.     bitshift = 15;
  92.     }
  93.  
  94. #if __STDC__
  95. static void
  96. putbit( bit b )
  97. #else /*__STDC__*/
  98. static void
  99. putbit( b )
  100.     bit b;
  101. #endif /*__STDC__*/
  102.     {
  103.     if ( bitsperitem == 16 )
  104.     putitem( );
  105.     ++bitsperitem;
  106.     if ( b == PBM_BLACK )
  107.     item += 1 << bitshift;
  108.     --bitshift;
  109.     }
  110.  
  111. static void
  112. putrest( )
  113.     {
  114.     if ( bitsperitem > 0 )
  115.     putitem( );
  116.     }
  117.  
  118. static void
  119. putitem( )
  120.     {
  121.     fwrite( &item, sizeof(item), 1, stdout );
  122.     item = 0;
  123.     bitsperitem = 0;
  124.     bitshift = 15;
  125.     }
  126.