home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / sun / lib2 / libppm1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-01  |  3.0 KB  |  127 lines

  1. /* libppm1.c - ppm utility library part 1
  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. #include "pgm.h"
  16. #include "libpgm.h"
  17. #include "pbm.h"
  18. #include "libpbm.h"
  19.  
  20.  
  21. ppm_readppminitrest( file, colsP, rowsP)
  22. FILE* file;
  23. int    *colsP, *rowsP;
  24. {
  25. int    maxval;
  26.  
  27.     /* Read size. */
  28.     *colsP = pbm_getint( file );
  29.     *rowsP = pbm_getint( file );
  30.  
  31.     /* Read maxval. */
  32.     maxval = pbm_getint( file );
  33.     if ( maxval > PPM_MAXMAXVAL )
  34.     return    prgmerr(DEBUGANY, "maxval too large %d > %d", maxval, PPM_MAXMAXVAL);
  35. return    maxval;
  36. }
  37.  
  38. static gray* grayrow;
  39. static bit* bitrow;
  40.  
  41.  
  42. #ifdef __STDC__
  43. ppm_readppmrow( FILE* file, pixel* pixelrow, int cols, pixval maxval, int format )
  44. #else /*__STDC__*/
  45. ppm_readppmrow( file, pixelrow, cols, maxval, format )
  46. FILE* file;
  47. pixel* pixelrow;
  48. int cols, format;
  49. pixval maxval;
  50. #endif /*__STDC__*/
  51. {
  52. register int col;
  53. register pixel* pP;
  54. register pixval r, g, b;
  55. register gray* gP;
  56. register bit* bP;
  57.  
  58.     switch ( format )
  59.     {
  60.     case PPM_FORMAT:
  61.     for ( col = 0, pP = pixelrow; col < cols; ++col, ++pP )
  62.         {
  63.         r = pbm_getint( file );
  64. #ifdef _DEBUG_
  65.         if ( r > maxval )
  66.         message("r value out of bounds (%u > %u)", r, maxval);
  67. #endif /*DEBUG*/
  68.         g = pbm_getint( file );
  69. #ifdef _DEBUG_
  70.         if ( g > maxval )
  71.         message("g value out of bounds (%u > %u)", g, maxval);
  72. #endif /*DEBUG*/
  73.         b = pbm_getint( file );
  74. #ifdef _DEBUG_
  75.         if ( b > maxval )
  76.         message("b value out of bounds (%u > %u)", b, maxval);
  77. #endif /*DEBUG*/
  78.         PPM_ASSIGN( *pP, r, g, b );
  79.         }
  80.     break;
  81.  
  82.     case RPPM_FORMAT:
  83.     for ( col = 0, pP = pixelrow; col < cols; ++col, ++pP )    {
  84.         r = pbm_getrawbyte( file );
  85. #ifdef _DEBUG_
  86.         if ( r > maxval )
  87.         message("r value out of bounds (%u > %u)", r, maxval);
  88. #endif /*DEBUG*/
  89.         g = pbm_getrawbyte( file );
  90. #ifdef _DEBUG_
  91.         if ( g > maxval )
  92.         message("g value out of bounds (%u > %u)", g, maxval);
  93. #endif /*DEBUG*/
  94.         b = pbm_getrawbyte( file );
  95. #ifdef _DEBUG_
  96.         if ( b > maxval )
  97.         message("b value out of bounds (%u > %u)", b, maxval);
  98. #endif /*DEBUG*/
  99.         PPM_ASSIGN( *pP, r, g, b );
  100.     }
  101.     break;
  102.  
  103.     case PGM_FORMAT:
  104.     case RPGM_FORMAT:
  105.     pgm_readpgmrow( file, grayrow, cols, maxval, format );
  106.     for ( col = 0, gP = grayrow, pP = pixelrow; col < cols; ++col, ++gP, ++pP )
  107.         {
  108.         r = *gP;
  109.         PPM_ASSIGN( *pP, r, r, r );
  110.         }
  111.     break;
  112.  
  113.     case PBM_FORMAT:
  114.     case RPBM_FORMAT:
  115.     pbm_readpbmrow( file, bitrow, cols, format );
  116.     for ( col = 0, bP = bitrow, pP = pixelrow; col < cols; ++col, ++bP, ++pP )
  117.         {
  118.         r = ( *bP == PBM_WHITE ) ? maxval : 0;
  119.         PPM_ASSIGN( *pP, r, r, r );
  120.         }
  121.     break;
  122.  
  123.     default:return    prgmerr(DEBUGANY, "PPM read ???");
  124.     }
  125. return    cols;
  126. }
  127.