home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume2 / pbm / Part4 / libpbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-07  |  2.7 KB  |  162 lines

  1. /* libpbm.c - pbm utility library
  2. **
  3. ** Copyright (C) 1988 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 "pbm.h"
  15.  
  16. int
  17. pbm_getint( file )
  18. FILE *file;
  19.     {
  20.     char ch;
  21.     int i;
  22.  
  23.     do
  24.     {
  25.     ch = pbm_getc( file );
  26.     }
  27.     while ( ch == ' ' || ch == '\t' || ch == '\n' );
  28.  
  29.     if ( ch < '0' || ch > '9' )
  30.     {
  31.     fprintf( stderr, "Junk in file where an integer should be!\n" );
  32.     exit( 1 );
  33.     }
  34.  
  35.     i = 0;
  36.     do
  37.     {
  38.     i = i * 10 + ch - '0';
  39.     ch = pbm_getc( file );
  40.         }
  41.     while ( ch >= '0' && ch <= '9' );
  42.  
  43.     return ( i );
  44.     }
  45.  
  46.  
  47. bit
  48. pbm_getbit( file )
  49. FILE *file;
  50.     {
  51.     char ch;
  52.  
  53.     do
  54.     {
  55.     ch = pbm_getc( file );
  56.     }
  57.     while ( ch == ' ' || ch == '\t' || ch == '\n' );
  58.  
  59.     if ( ch != '0' && ch != '1' )
  60.     {
  61.     fprintf( stderr, "Junk in file where bits should be!\n" );
  62.     exit( 1 );
  63.     }
  64.  
  65.     return ( ( ch == '1' ) ? 1 : 0 );
  66.     }
  67.  
  68. char
  69. pbm_getc( file )
  70. FILE *file;
  71.     {
  72.     int ich;
  73.     char ch;
  74.  
  75.     ich = getc( file );
  76.     if ( ich == NULL )
  77.     {
  78.     fprintf( stderr, "Premature EOF.\n" );
  79.     exit( 1 );
  80.     }
  81.     ch = (char) ich;
  82.     
  83.     if ( ch == '#' )
  84.     {
  85.     do
  86.         {
  87.         ich = getc( file );
  88.         if ( ich == NULL )
  89.         {
  90.         fprintf( stderr, "Premature EOF.\n" );
  91.         exit( 1 );
  92.         }
  93.         ch = (char) ich;
  94.         }
  95.     while ( ch != '\n' );
  96.     }
  97.  
  98.     return ( ch );
  99.     }
  100.  
  101. bit **
  102. pbm_allocarray( cols, rows )
  103. int cols, rows;
  104.     {
  105.     bit **bits;
  106.     int i;
  107.  
  108.     bits = (bit **) malloc( rows * sizeof( bit *) );
  109.     for ( i = 0; i < rows; i++ )
  110.     {
  111.     bits[i] = (bit *) malloc( cols * sizeof( bit ) );
  112.     }
  113.  
  114.     return ( bits );
  115.     }
  116.  
  117.  
  118. bit **pbm_readpbm( file, colsP, rowsP )
  119. FILE *file;
  120. int *colsP, *rowsP;
  121.     {
  122.     bit **bits;
  123.     int row, col;
  124.  
  125.     *colsP = pbm_getint( file );
  126.     *rowsP = pbm_getint( file );
  127.  
  128.     bits = pbm_allocarray( *colsP, *rowsP );
  129.  
  130.     for ( row = 0; row < *rowsP; row++ )
  131.         for ( col = 0; col < *colsP; col++ )
  132.         bits[row][col] = pbm_getbit( file );
  133.  
  134.     return ( bits );
  135.     }
  136.  
  137. pbm_writepbm( file, bits, cols, rows )
  138. FILE *file;
  139. bit **bits;
  140. int cols, rows;
  141.     {
  142.     int row, col, linecount;
  143.  
  144.     fprintf( file, "%d %d\n", cols, rows );
  145.  
  146.     for ( row = 0; row < rows; row++ )
  147.     {
  148.     linecount = 0;
  149.         for ( col = 0; col < cols; col++ )
  150.         {
  151.         if ( linecount >= 70 )
  152.         {
  153.         putc( '\n', file );
  154.         linecount = 0;
  155.         }
  156.         putc( bits[row][col] ? '1' : '0', file );
  157.         linecount++;
  158.         }
  159.     putc( '\n', file );
  160.         }
  161.     }
  162.