home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 309.lha / PBM_PLUS / pgm / libpgm2.c < prev    next >
C/C++ Source or Header  |  1980-12-04  |  3KB  |  152 lines

  1. /* libpgm2.c - pgm 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 "pgm.h"
  15. #include "libpgm.h"
  16.  
  17. void
  18. pgm_writepgminit( file, cols, rows, maxval )
  19. FILE *file;
  20. int cols, rows;
  21. gray maxval;
  22.     {
  23. #ifdef PBMPLUS_RAWBITS
  24.     if ( maxval <= 255 )
  25.     fprintf(
  26.         file, "%c%c\n%d %d\n%d\n", PGM_MAGIC1, RPGM_MAGIC2,
  27.         cols, rows, maxval );
  28.     else
  29.     fprintf(
  30.         file, "%c%c\n%d %d\n%d\n", PGM_MAGIC1, PGM_MAGIC2,
  31.         cols, rows, maxval );
  32. #else PBMPLUS_RAWBITS
  33.     fprintf(
  34.     file, "%c%c\n%d %d\n%d\n", PGM_MAGIC1, PGM_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. pgm_writepgmrowraw( file, grayrow, cols, maxval )
  53. FILE *file;
  54. gray *grayrow;
  55. int cols;
  56. gray maxval;
  57.     {
  58.     register int col;
  59.     register gray *gP;
  60.  
  61.     for ( col = 0, gP = grayrow; col < cols; col++, gP++ )
  62.     {
  63. #ifdef DEBUG
  64.     if ( *gP > maxval )
  65.         pm_error( "value out of bounds (%u > %u)", *gP, maxval, 0,0,0 );
  66. #endif DEBUG
  67.     if ( putc( *gP, file ) == EOF )
  68.         {
  69.         perror( "pgm_writepgmrow" );
  70.         exit( 1 );
  71.         }
  72.     }
  73.     }
  74. #endif PBMPLUS_RAWBITS
  75.  
  76. static void
  77. pgm_writepgmrowplain( file, grayrow, cols, maxval )
  78. FILE *file;
  79. gray *grayrow;
  80. int cols;
  81. gray maxval;
  82.     {
  83.     register int col, charcount;
  84.     register gray *gP;
  85.  
  86.     charcount = 0;
  87.     for ( col = 0, gP = grayrow; col < cols; col++, gP++ )
  88.     {
  89.     if ( charcount >= 70 )
  90.         {
  91.         if ( putc( '\n', file ) == EOF )
  92.         {
  93.         perror( "pgm_writepgmrow" );
  94.         exit( 1 );
  95.         }
  96.         charcount = 0;
  97.         }
  98. #ifdef DEBUG
  99.     if ( *gP > maxval )
  100.         pm_error( "value out of bounds (%u > %u)", *gP, maxval, 0,0,0 );
  101. #endif DEBUG
  102.     if ( putus( (unsigned long) *gP, file ) == EOF )
  103.         {
  104.         perror( "pgm_writepgmrow" );
  105.         exit( 1 );
  106.         }
  107.     if ( putc( ' ', file ) == EOF )
  108.         {
  109.         perror( "pgm_writepgmrow" );
  110.         exit( 1 );
  111.         }
  112.     charcount += 4;
  113.     }
  114.     if ( putc( '\n', file ) == EOF )
  115.     {
  116.     perror( "pgm_writepgmrow" );
  117.     exit( 1 );
  118.     }
  119.     }
  120.  
  121. void
  122. pgm_writepgmrow( file, grayrow, cols, maxval )
  123. FILE *file;
  124. gray *grayrow;
  125. int cols;
  126. gray maxval;
  127.     {
  128. #ifdef PBMPLUS_RAWBITS
  129.     if ( maxval <= 255 )
  130.     pgm_writepgmrowraw( file, grayrow, cols, maxval );
  131.     else
  132.     pgm_writepgmrowplain( file, grayrow, cols, maxval );
  133. #else PBMPLUS_RAWBITS
  134.     pgm_writepgmrowplain( file, grayrow, cols, maxval );
  135. #endif PBMPLUS_RAWBITS
  136.     }
  137.  
  138. void
  139. pgm_writepgm( file, grays, cols, rows, maxval )
  140. FILE *file;
  141. gray **grays;
  142. int cols, rows;
  143. gray maxval;
  144.     {
  145.     int row;
  146.  
  147.     pgm_writepgminit( file, cols, rows, maxval );
  148.  
  149.     for ( row = 0; row < rows; row++ )
  150.     pgm_writepgmrow( file, grays[row], cols, maxval );
  151.     }
  152.