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