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