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

  1. /* libppm2.c - ppm utility library part 2
  2. **
  3. ** Copyright (C) 1989 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 "ppm.h"
  14. #include "libppm.h"
  15.  
  16. static void putus ARGS((unsigned short n, FILE* file));
  17. static void ppm_writeppmrowplain ARGS((FILE* file, pixel* pixelrow, int cols, pixval maxval));
  18. #ifdef PBMPLUS_RAWBITS
  19. static void ppm_writeppmrowraw ARGS((FILE* file, pixel* pixelrow, int cols, pixval maxval));
  20. #endif /* PBMPLUS_RAWBITS */
  21. #if __STDC__
  22. void
  23. ppm_writeppminit( FILE* file, int cols, int rows, pixval maxval, int forceplain )
  24. #else /*__STDC__*/
  25. void
  26. ppm_writeppminit( file, cols, rows, maxval, forceplain )
  27.     FILE* file;
  28.     int cols, rows;
  29.     pixval maxval;
  30.     int forceplain;
  31. #endif /*__STDC__*/
  32.     {
  33. #ifdef PBMPLUS_RAWBITS
  34.     if ( maxval <= 255 && ! forceplain ) {
  35.     fprintf(
  36.         file, "%c%c\n%d %d\n%d\n", PPM_MAGIC1, RPPM_MAGIC2,
  37.         cols, rows, maxval );
  38. #ifdef VMS
  39.         set_outfile_binary();
  40. #endif
  41.         }
  42.     else
  43.     fprintf(
  44.         file, "%c%c\n%d %d\n%d\n", PPM_MAGIC1, PPM_MAGIC2,
  45.         cols, rows, maxval );
  46. #else /*PBMPLUS_RAWBITS*/
  47.     fprintf(
  48.     file, "%c%c\n%d %d\n%d\n", PPM_MAGIC1, PPM_MAGIC2,
  49.     cols, rows, maxval );
  50. #endif /*PBMPLUS_RAWBITS*/
  51.     }
  52.  
  53. static void
  54. putus( n, file )
  55.     unsigned short n;
  56.     FILE* file;
  57.     {
  58.     if ( n >= 10 )
  59.     putus( n / 10, file );
  60.     (void) putc( n % 10 + '0', file );
  61.     }
  62.  
  63. #ifdef PBMPLUS_RAWBITS
  64. static void
  65. ppm_writeppmrowraw( file, pixelrow, cols, maxval )
  66.     FILE* file;
  67.     pixel* pixelrow;
  68.     int cols;
  69.     pixval maxval;
  70.     {
  71.     register int col;
  72.     register pixel* pP;
  73.     register pixval val;
  74.  
  75.     for ( col = 0, pP = pixelrow; col < cols; ++col, ++pP )
  76.     {
  77.     val = PPM_GETR( *pP );
  78. #ifdef DEBUG
  79.     if ( val > maxval )
  80.         pm_error( "r value out of bounds (%u > %u)", val, maxval );
  81. #endif /*DEBUG*/
  82.     (void) putc( val, file );
  83.     val = PPM_GETG( *pP );
  84. #ifdef DEBUG
  85.     if ( val > maxval )
  86.         pm_error( "g value out of bounds (%u > %u)", val, maxval );
  87. #endif /*DEBUG*/
  88.     (void) putc( val, file );
  89.     val = PPM_GETB( *pP );
  90. #ifdef DEBUG
  91.     if ( val > maxval )
  92.         pm_error( "b value out of bounds (%u > %u)", val, maxval );
  93. #endif /*DEBUG*/
  94.     (void) putc( val, file );
  95.         }
  96.     }
  97. #endif /*PBMPLUS_RAWBITS*/
  98.  
  99. static void
  100. ppm_writeppmrowplain( file, pixelrow, cols, maxval )
  101.     FILE* file;
  102.     pixel* pixelrow;
  103.     int cols;
  104.     pixval maxval;
  105.     {
  106.     register int col, charcount;
  107.     register pixel* pP;
  108.     register pixval val;
  109.  
  110.     charcount = 0;
  111.     for ( col = 0, pP = pixelrow; col < cols; ++col, ++pP )
  112.     {
  113.     if ( charcount >= 65 )
  114.         {
  115.         (void) putc( '\n', file );
  116.         charcount = 0;
  117.         }
  118.     else if ( charcount > 0 )
  119.         {
  120.         (void) putc( ' ', file );
  121.         (void) putc( ' ', file );
  122.         charcount += 2;
  123.         }
  124.     val = PPM_GETR( *pP );
  125. #ifdef DEBUG
  126.     if ( val > maxval )
  127.         pm_error( "r value out of bounds (%u > %u)", val, maxval );
  128. #endif /*DEBUG*/
  129.     putus( val, file );
  130.     (void) putc( ' ', file );
  131.     val = PPM_GETG( *pP );
  132. #ifdef DEBUG
  133.     if ( val > maxval )
  134.         pm_error( "g value out of bounds (%u > %u)", val, maxval );
  135. #endif /*DEBUG*/
  136.     putus( val, file );
  137.     (void) putc( ' ', file );
  138.     val = PPM_GETB( *pP );
  139. #ifdef DEBUG
  140.     if ( val > maxval )
  141.         pm_error( "b value out of bounds (%u > %u)", val, maxval );
  142. #endif /*DEBUG*/
  143.     putus( val, file );
  144.     charcount += 11;
  145.     }
  146.     if ( charcount > 0 )
  147.     (void) putc( '\n', file );
  148.     }
  149.  
  150. #if __STDC__
  151. void
  152. ppm_writeppmrow( FILE* file, pixel* pixelrow, int cols, pixval maxval, int forceplain )
  153. #else /*__STDC__*/
  154. void
  155. ppm_writeppmrow( file, pixelrow, cols, maxval, forceplain )
  156.     FILE* file;
  157.     pixel* pixelrow;
  158.     int cols;
  159.     pixval maxval;
  160.     int forceplain;
  161. #endif /*__STDC__*/
  162.     {
  163. #ifdef PBMPLUS_RAWBITS
  164.     if ( maxval <= 255 && ! forceplain )
  165.     ppm_writeppmrowraw( file, pixelrow, cols, maxval );
  166.     else
  167.     ppm_writeppmrowplain( file, pixelrow, cols, maxval );
  168. #else /*PBMPLUS_RAWBITS*/
  169.     ppm_writeppmrowplain( file, pixelrow, cols, maxval );
  170. #endif /*PBMPLUS_RAWBITS*/
  171.     }
  172.  
  173. #if __STDC__
  174. void
  175. ppm_writeppm( FILE* file, pixel** pixels, int cols, int rows, pixval maxval, int forceplain )
  176. #else /*__STDC__*/
  177. void
  178. ppm_writeppm( file, pixels, cols, rows, maxval, forceplain )
  179.     FILE* file;
  180.     pixel** pixels;
  181.     int cols, rows;
  182.     pixval maxval;
  183.     int forceplain;
  184. #endif /*__STDC__*/
  185.     {
  186.     int row;
  187.  
  188.     ppm_writeppminit( file, cols, rows, maxval, forceplain );
  189.  
  190.     for ( row = 0; row < rows; ++row )
  191.     ppm_writeppmrow( file, pixels[row], cols, maxval, forceplain );
  192.     }
  193.