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

  1. /* libpbm3.c - pbm utility library part 3
  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. #include "libpbm.h"
  15.  
  16. static void pbm_writepbmrowplain ARGS((FILE* file, bit* bitrow, int cols));
  17. #ifdef PBMPLUS_RAWBITS
  18. static void pbm_writepbmrowraw ARGS((FILE* file, bit* bitrow, int cols));
  19. #endif /* PBMPLUS_RAWBITS */
  20. void
  21. pbm_writepbminit( file, cols, rows, forceplain )
  22.     FILE* file;
  23.     int cols, rows;
  24.     int forceplain;
  25.     {
  26. #ifdef PBMPLUS_RAWBITS
  27.     if ( ! forceplain ) {
  28.     fprintf( file, "%c%c\n%d %d\n", PBM_MAGIC1, RPBM_MAGIC2, cols, rows );
  29. #ifdef VMS
  30.         set_outfile_binary();
  31. #endif
  32.         }
  33.     else
  34.     fprintf( file, "%c%c\n%d %d\n", PBM_MAGIC1, PBM_MAGIC2, cols, rows );
  35. #else /*PBMPLUS_RAWBITS*/
  36.     fprintf( file, "%c%c\n%d %d\n", PBM_MAGIC1, PBM_MAGIC2, cols, rows );
  37. #endif /*PBMPLUS_RAWBITS*/
  38.     }
  39.  
  40. #ifdef PBMPLUS_RAWBITS
  41. static void
  42. pbm_writepbmrowraw( file, bitrow, cols )
  43.     FILE* file;
  44.     bit* bitrow;
  45.     int cols;
  46.     {
  47.     register int col, bitshift;
  48.     register unsigned char item;
  49.     register bit* bP;
  50.  
  51.     bitshift = 7;
  52.     item = 0;
  53.     for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  54.     {
  55.     if ( *bP )
  56.         item += 1 << bitshift;
  57.     --bitshift;
  58.     if ( bitshift == -1 )
  59.         {
  60.         (void) putc( item, file );
  61.         bitshift = 7;
  62.         item = 0;
  63.         }
  64.     }
  65.     if ( bitshift != 7 )
  66.     (void) putc( item, file );
  67.     }
  68. #endif /*PBMPLUS_RAWBITS*/
  69.  
  70. static void
  71. pbm_writepbmrowplain( file, bitrow, cols )
  72.     FILE* file;
  73.     bit* bitrow;
  74.     int cols;
  75.     {
  76.     register int col, charcount;
  77.     register bit* bP;
  78.  
  79.     charcount = 0;
  80.     for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  81.     {
  82.     if ( charcount >= 70 )
  83.         {
  84.         (void) putc( '\n', file );
  85.         charcount = 0;
  86.         }
  87.     (void) putc( *bP ? '1' : '0', file );
  88.     ++charcount;
  89.     }
  90.     (void) putc( '\n', file );
  91.     }
  92.  
  93. void
  94. pbm_writepbmrow( file, bitrow, cols, forceplain )
  95.     FILE* file;
  96.     bit* bitrow;
  97.     int cols;
  98.     int forceplain;
  99.     {
  100. #ifdef PBMPLUS_RAWBITS
  101.     if ( ! forceplain )
  102.     pbm_writepbmrowraw( file, bitrow, cols );
  103.     else
  104.     pbm_writepbmrowplain( file, bitrow, cols );
  105. #else /*PBMPLUS_RAWBITS*/
  106.     pbm_writepbmrowplain( file, bitrow, cols );
  107. #endif /*PBMPLUS_RAWBITS*/
  108.     }
  109.  
  110. void
  111. pbm_writepbm( file, bits, cols, rows, forceplain )
  112.     FILE* file;
  113.     bit** bits;
  114.     int cols, rows;
  115.     int forceplain;
  116.     {
  117.     int row;
  118.  
  119.     pbm_writepbminit( file, cols, rows, forceplain );
  120.  
  121.     for ( row = 0; row < rows; ++row )
  122.     pbm_writepbmrow( file, bits[row], cols, forceplain );
  123.     }
  124.