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