home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 309.lha / PBM_PLUS / pbm / libpbm3.c < prev    next >
C/C++ Source or Header  |  1980-12-04  |  2KB  |  108 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 <stdio.h>
  14. #include "pbm.h"
  15. #include "libpbm.h"
  16.  
  17. void
  18. pbm_writepbminit( file, cols, rows )
  19. FILE *file;
  20. int cols, rows;
  21.     {
  22. #ifdef PBMPLUS_RAWBITS
  23.     fprintf( file, "%c%c\n%d %d\n", PBM_MAGIC1, RPBM_MAGIC2, cols, rows );
  24. #else PBMPLUS_RAWBITS
  25.     fprintf( file, "%c%c\n%d %d\n", PBM_MAGIC1, PBM_MAGIC2, cols, rows );
  26. #endif PBMPLUS_RAWBITS
  27.     }
  28.  
  29. void
  30. pbm_writepbmrow( file, bitrow, cols )
  31. FILE *file;
  32. bit *bitrow;
  33. int cols;
  34.     {
  35. #ifdef PBMPLUS_RAWBITS
  36.     register int col, bitshift;
  37.     register unsigned char item;
  38.     register bit *bP;
  39.  
  40.     bitshift = 7;
  41.     item = 0;
  42.     for ( col = 0, bP = bitrow; col < cols; col++, bP++ )
  43.     {
  44.     if ( *bP )
  45.         item += 1 << bitshift;
  46.     bitshift--;
  47.     if ( bitshift == -1 )
  48.         {
  49.         if ( putc( item, file ) == EOF )
  50.         {
  51.         perror( "pbm_writepbmrow" );
  52.         exit( 1 );
  53.         }
  54.         bitshift = 7;
  55.         item = 0;
  56.         }
  57.     }
  58.     if ( bitshift != 7 )
  59.     if ( putc( item, file ) == EOF )
  60.         {
  61.         perror( "pbm_writepbmrow" );
  62.         exit( 1 );
  63.         }
  64. #else PBMPLUS_RAWBITS
  65.     register int col, charcount;
  66.     register bit *bP;
  67.  
  68.     charcount = 0;
  69.     for ( col = 0, bP = bitrow; col < cols; col++, bP++ )
  70.     {
  71.     if ( charcount >= 70 )
  72.         {
  73.         if ( putc( '\n', file ) == EOF )
  74.         {
  75.         perror( "pbm_writepbmrow" );
  76.         exit( 1 );
  77.         }
  78.         charcount = 0;
  79.         }
  80.     if ( putc( *bP ? '1' : '0', file ) == EOF )
  81.         {
  82.         perror( "pbm_writepbmrow" );
  83.         exit( 1 );
  84.         }
  85.     charcount++;
  86.     }
  87.     if ( putc( '\n', file ) == EOF )
  88.     {
  89.     perror( "pbm_writepbmrow" );
  90.     exit( 1 );
  91.     }
  92. #endif PBMPLUS_RAWBITS
  93.     }
  94.  
  95. void
  96. pbm_writepbm( file, bits, cols, rows )
  97. FILE *file;
  98. bit **bits;
  99. int cols, rows;
  100.     {
  101.     int row;
  102.  
  103.     pbm_writepbminit( file, cols, rows );
  104.  
  105.     for ( row = 0; row < rows; row++ )
  106.     pbm_writepbmrow( file, bits[row], cols );
  107.     }
  108.