home *** CD-ROM | disk | FTP | other *** search
- /* libppm1.c - ppm utility library part 1
- **
- ** 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"
- #include "pgm.h"
- #include "libpgm.h"
- #include "pbm.h"
- #include "libpbm.h"
-
-
- ppm_readppminitrest( file, colsP, rowsP)
- FILE* file;
- int *colsP, *rowsP;
- {
- int maxval;
-
- /* Read size. */
- *colsP = pbm_getint( file );
- *rowsP = pbm_getint( file );
-
- /* Read maxval. */
- maxval = pbm_getint( file );
- if ( maxval > PPM_MAXMAXVAL )
- return prgmerr(DEBUGANY, "maxval too large %d > %d", maxval, PPM_MAXMAXVAL);
- return maxval;
- }
-
- static gray* grayrow;
- static bit* bitrow;
-
-
- #ifdef __STDC__
- ppm_readppmrow( FILE* file, pixel* pixelrow, int cols, pixval maxval, int format )
- #else /*__STDC__*/
- ppm_readppmrow( file, pixelrow, cols, maxval, format )
- FILE* file;
- pixel* pixelrow;
- int cols, format;
- pixval maxval;
- #endif /*__STDC__*/
- {
- register int col;
- register pixel* pP;
- register pixval r, g, b;
- register gray* gP;
- register bit* bP;
-
- switch ( format )
- {
- case PPM_FORMAT:
- for ( col = 0, pP = pixelrow; col < cols; ++col, ++pP )
- {
- r = pbm_getint( file );
- #ifdef _DEBUG_
- if ( r > maxval )
- message("r value out of bounds (%u > %u)", r, maxval);
- #endif /*DEBUG*/
- g = pbm_getint( file );
- #ifdef _DEBUG_
- if ( g > maxval )
- message("g value out of bounds (%u > %u)", g, maxval);
- #endif /*DEBUG*/
- b = pbm_getint( file );
- #ifdef _DEBUG_
- if ( b > maxval )
- message("b value out of bounds (%u > %u)", b, maxval);
- #endif /*DEBUG*/
- PPM_ASSIGN( *pP, r, g, b );
- }
- break;
-
- case RPPM_FORMAT:
- for ( col = 0, pP = pixelrow; col < cols; ++col, ++pP ) {
- r = pbm_getrawbyte( file );
- #ifdef _DEBUG_
- if ( r > maxval )
- message("r value out of bounds (%u > %u)", r, maxval);
- #endif /*DEBUG*/
- g = pbm_getrawbyte( file );
- #ifdef _DEBUG_
- if ( g > maxval )
- message("g value out of bounds (%u > %u)", g, maxval);
- #endif /*DEBUG*/
- b = pbm_getrawbyte( file );
- #ifdef _DEBUG_
- if ( b > maxval )
- message("b value out of bounds (%u > %u)", b, maxval);
- #endif /*DEBUG*/
- PPM_ASSIGN( *pP, r, g, b );
- }
- break;
-
- case PGM_FORMAT:
- case RPGM_FORMAT:
- pgm_readpgmrow( file, grayrow, cols, maxval, format );
- for ( col = 0, gP = grayrow, pP = pixelrow; col < cols; ++col, ++gP, ++pP )
- {
- r = *gP;
- PPM_ASSIGN( *pP, r, r, r );
- }
- break;
-
- case PBM_FORMAT:
- case RPBM_FORMAT:
- pbm_readpbmrow( file, bitrow, cols, format );
- for ( col = 0, bP = bitrow, pP = pixelrow; col < cols; ++col, ++bP, ++pP )
- {
- r = ( *bP == PBM_WHITE ) ? maxval : 0;
- PPM_ASSIGN( *pP, r, r, r );
- }
- break;
-
- default:return prgmerr(DEBUGANY, "PPM read ???");
- }
- return cols;
- }
-