home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / NETWORK / netpbm_src.lzh / NETPBM / PGM / hipstopgm.c < prev    next >
C/C++ Source or Header  |  1996-11-18  |  4KB  |  181 lines

  1. /* hipstopgm.c - read a HIPS file and produce a portable graymap
  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 "pgm.h"
  14.  
  15. struct HIPS_Header {
  16.     char* orig_name;    /* An indication of the originator of this sequence. */
  17.     char* seq_name;    /* The sequence name. */
  18.     int num_frame;    /* The number of frames in this sequence. */
  19.     char* orig_date;    /* The date the sequence was originated. */
  20.     int rows;        /* The number of rows in each image, the height. */
  21.     int cols;        /* The number of columns in each image, the width. */
  22.     int bits_per_pixel;    /* The number of significant bits per pixel. */
  23.     int bit_packing;    /* Nonzero if the bits were packed such as to
  24.                eliminate any unused bits resulting from a
  25.                bits_per_pixel value which was not an even
  26.                multiple of eight. */
  27.     int pixel_format;    /* An indication of the format of each pixel. */
  28.     char* seq_history;    /* A description of the sequence of transformations
  29.                leading up to the current image. */
  30.     char* seq_desc;    /* A free form description of the contents of the
  31.                sequence. */
  32.     };
  33. #define HIPS_PFBYTE 0
  34. #define HIPS_PFSHORT 1
  35. #define HIPS_PFINT 2
  36. #define HIPS_PFFLOAT 3
  37. #define HIPS_PFCOMPLEX 4
  38.  
  39. static void read_hips_header ARGS(( FILE* fd, struct HIPS_Header* hP ));
  40. static void read_line ARGS(( FILE* fd, char* buf, int size ));
  41.  
  42. int
  43. main( argc, argv )
  44. int argc;
  45. char* argv[];
  46.     {
  47.     FILE* ifp;
  48.     gray* grayrow;
  49.     register gray* gP;
  50.     int argn, row;
  51.     register int col;
  52.     int maxval;
  53.     int rows, cols;
  54.     struct HIPS_Header h;
  55.  
  56.  
  57.     pgm_init( &argc, argv );
  58.  
  59.     argn = 1;
  60.  
  61.     if ( argn < argc )
  62.     {
  63.     ifp = pm_openr( argv[argn] );
  64.     argn++;
  65.     }
  66.     else
  67.     ifp = stdin;
  68.  
  69.     if ( argn != argc )
  70.     pm_usage( "[hipsfile]" );
  71.  
  72.     read_hips_header( ifp, &h );
  73.  
  74.     cols = h.cols;
  75.     rows = h.rows * h.num_frame;
  76.  
  77.     switch ( h.pixel_format )
  78.     {
  79.     case HIPS_PFBYTE:
  80.     if ( h.bits_per_pixel != 8 )
  81.         pm_error(
  82.         "can't handle unusual bits_per_pixel %d", h.bits_per_pixel );
  83.     if ( h.bit_packing != 0 )
  84.         pm_error( "can't handle bit_packing" );
  85.     maxval = 255;
  86.     break;
  87.  
  88.     default:
  89.     pm_error( "unknown pixel format %d", h.pixel_format );
  90.     }
  91.     if ( maxval > PGM_MAXMAXVAL )
  92.     pm_error(
  93.       "bits_per_pixel is too large - try reconfiguring with PGM_BIGGRAYS" );
  94.  
  95.     pgm_writepgminit( stdout, cols, rows, (gray) maxval, 0 );
  96.     grayrow = pgm_allocrow( cols );
  97.     for ( row = 0; row < rows; row++)
  98.     {
  99.     for ( col = 0, gP = grayrow; col < cols; col++, gP++ )
  100.         {
  101.         int ich;
  102.  
  103.         switch ( h.pixel_format )
  104.         {
  105.         case HIPS_PFBYTE:
  106.         ich = getc( ifp );
  107.         if ( ich == EOF )
  108.             pm_error( "EOF / read error" );
  109.         *gP = (gray) ich;
  110.         break;
  111.  
  112.         default:
  113.         pm_error( "can't happen" );
  114.         }
  115.         }
  116.     pgm_writepgmrow( stdout, grayrow, cols, (gray) maxval, 0 );
  117.     }
  118.     pm_close( ifp );
  119.     pm_close( stdout );
  120.  
  121.     exit( 0 );
  122.     }
  123.  
  124. static void
  125. read_hips_header( fd, hP )
  126. FILE* fd;
  127. struct HIPS_Header* hP;
  128.     {
  129.     char buf[5000];
  130.  
  131.     /* Read and toss orig_name. */
  132.     read_line( fd, buf, 5000 );
  133.  
  134.     /* Read and toss seq_name. */
  135.     read_line( fd, buf, 5000 );
  136.  
  137.     /* Read num_frame. */
  138.     read_line( fd, buf, 5000 );
  139.     hP->num_frame = atoi( buf );
  140.  
  141.     /* Read and toss orig_date. */
  142.     read_line( fd, buf, 5000 );
  143.  
  144.     /* Read rows. */
  145.     read_line( fd, buf, 5000 );
  146.     hP->rows = atoi( buf );
  147.  
  148.     /* Read cols. */
  149.     read_line( fd, buf, 5000 );
  150.     hP->cols = atoi( buf );
  151.  
  152.     /* Read bits_per_pixel. */
  153.     read_line( fd, buf, 5000 );
  154.     hP->bits_per_pixel = atoi( buf );
  155.  
  156.     /* Read bit_packing. */
  157.     read_line( fd, buf, 5000 );
  158.     hP->bit_packing = atoi( buf );
  159.  
  160.     /* Read pixel_format. */
  161.     read_line( fd, buf, 5000 );
  162.     hP->pixel_format = atoi( buf );
  163.  
  164.     /* Now read and toss lines until we get one with just a period. */
  165.     do
  166.     {
  167.     read_line( fd, buf, 5000 );
  168.     }
  169.     while ( strcmp( buf, ".\n" ) != 0 );
  170.     }
  171.  
  172. static void
  173. read_line( fd, buf, size )
  174. FILE* fd;
  175. char* buf;
  176. int size;
  177.     {
  178.     if ( fgets( buf, size, fd ) == NULL )
  179.     pm_error( "error reading header" );
  180.     }
  181.