/* libppm2.c - ppm utility library part 2 ** ** Copyright (C) 1989 by Jef Poskanzer. ** ** Permission to use, copy, modify, and distribute this software and its ** documentation for any purpose and without fee is hereby granted, provided ** that the above copyright notice appear in all copies and that both that ** copyright notice and this permission notice appear in supporting ** documentation. This software is provided "as is" without express or ** implied warranty. */ #include "ppm.h" #include "libppm.h" void ppm_writeppminit( FILE* file, int cols, int rows, pixval maxval, int forceplain ) { #ifdef PGM_BIGGRAYS if ( maxval <= 65535 && ! forceplain ) #else /* PGM_BIGGRAYS */ if ( maxval <= 255 && ! forceplain ) #endif /* PGM_BIGGRAYS */ fprintf( file, "%c%c\n%d %d\n%d\n", PPM_MAGIC1, RPPM_MAGIC2, cols, rows, maxval ); else fprintf( file, "%c%c\n%d %d\n%d\n", PPM_MAGIC1, PPM_MAGIC2, cols, rows, maxval ); } static void putus( unsigned short n, FILE* file ) { if ( n >= 10 ) putus( n / 10, file ); (void) putc( n % 10 + '0', file ); } static void ppm_writeppmrowraw( FILE* file, pixel* pixelrow, int cols, pixval maxval ) { int col; pixel* pP; pixval val; gray* grayrow; gray* gP; grayrow = pgm_allocrow( 3 * cols ); for ( col = 0, pP = pixelrow, gP = grayrow; col < cols; ++col, ++pP ) { val = PPM_GETR( *pP ); #ifdef DEBUG if ( val > maxval ) pm_error( "r value out of bounds (%u > %u)", val, maxval ); #endif /*DEBUG*/ *gP++ = val; val = PPM_GETG( *pP ); #ifdef DEBUG if ( val > maxval ) pm_error( "g value out of bounds (%u > %u)", val, maxval ); #endif /*DEBUG*/ *gP++ = val; val = PPM_GETB( *pP ); #ifdef DEBUG if ( val > maxval ) pm_error( "b value out of bounds (%u > %u)", val, maxval ); #endif /*DEBUG*/ *gP++ = val; } #ifdef PGM_BIGGRAYS if ( maxval > 255 ) { for ( col = 0, pP = pixelrow, gP = grayrow; col < cols; ++col, ++pP ) if ( pm_writelittleshort( file, *gP ) < 0 ) pm_error( "write error" ); } else if ( fwrite( grayrow, 1, 3 * cols, file ) != 3 * cols ) pm_error( "write error" ); #else /* PGM_BIGGRAYS */ if ( fwrite( grayrow, 1, 3 * cols, file ) != 3 * cols ) pm_error( "write error" ); #endif /* PGM_BIGGRAYS */ pgm_freerow( grayrow ); } static void ppm_writeppmrowplain( FILE* file, pixel* pixelrow, int cols, pixval maxval ) { int col, charcount; pixel* pP; pixval val; charcount = 0; for ( col = 0, pP = pixelrow; col < cols; ++col, ++pP ) { if ( charcount >= 65 ) { (void) putc( '\n', file ); charcount = 0; } else if ( charcount > 0 ) { (void) putc( ' ', file ); (void) putc( ' ', file ); charcount += 2; } val = PPM_GETR( *pP ); #ifdef DEBUG if ( val > maxval ) pm_error( "r value out of bounds (%u > %u)", val, maxval ); #endif /*DEBUG*/ putus( val, file ); (void) putc( ' ', file ); val = PPM_GETG( *pP ); #ifdef DEBUG if ( val > maxval ) pm_error( "g value out of bounds (%u > %u)", val, maxval ); #endif /*DEBUG*/ putus( val, file ); (void) putc( ' ', file ); val = PPM_GETB( *pP ); #ifdef DEBUG if ( val > maxval ) pm_error( "b value out of bounds (%u > %u)", val, maxval ); #endif /*DEBUG*/ putus( val, file ); charcount += 11; } if ( charcount > 0 ) (void) putc( '\n', file ); } void ppm_writeppmrow( FILE* file, pixel* pixelrow, int cols, pixval maxval, int forceplain ) { #ifdef PGM_BIGGRAYS if ( maxval <= 65535 && ! forceplain ) #else /* PGM_BIGGRAYS */ if ( maxval <= 255 && ! forceplain ) #endif /* PGM_BIGGRAYS */ ppm_writeppmrowraw( file, pixelrow, cols, maxval ); else ppm_writeppmrowplain( file, pixelrow, cols, maxval ); } void ppm_writeppm( FILE* file, pixel** pixels, int cols, int rows, pixval maxval, int forceplain ) { int row; ppm_writeppminit( file, cols, rows, maxval, forceplain ); for ( row = 0; row < rows; ++row ) ppm_writeppmrow( file, pixels[row], cols, maxval, forceplain ); }