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 >
Wrap
C/C++ Source or Header
|
1980-12-04
|
4KB
|
176 lines
/* hipstopgm.c - read a HIPS file and produce a portable graymap
**
** Copyright (C) 1989 by Jef Poskanzer.
**
** Permission to use, copy, modify, and distribute this software and its
** documentation for any purpose and without fee is hereby granted, provided
** that the above copyright notice appear in all copies and that both that
** copyright notice and this permission notice appear in supporting
** documentation. This software is provided "as is" without express or
** implied warranty.
*/
#include <stdio.h>
#ifdef SYSV
#include <string.h>
#else SYSV
#include <strings.h>
#endif SYSV
#include "pgm.h"
struct HIPS_Header {
char *orig_name; /* An indication of the originator of this sequence. */
char *seq_name; /* The sequence name. */
int num_frame; /* The number of frames in this sequence. */
char *orig_date; /* The date the sequence was originated. */
int rows; /* The number of rows in each image, the height. */
int cols; /* The number of columns in each image, the width. */
int bits_per_pixel; /* The number of significant bits per pixel. */
int bit_packing; /* Nonzero if the bits were packed such as to
eliminate any unused bits resulting from a
bits_per_pixel value which was not an even
multiple of eight. */
int pixel_format; /* An indication of the format of each pixel.
char *seq_history; /* A description of the sequence of transformations
leading up to the current image. */
char *seq_desc; /* A free form description of the contents of the
sequence. */
};
#define HIPS_PFBYTE 0
#define HIPS_PFSHORT 1
#define HIPS_PFINT 2
#define HIPS_PFFLOAT 3
#define HIPS_PFCOMPLEX 4
main( argc, argv )
int argc;
char *argv[];
{
FILE *ifd;
register gray *grayrow, *gP;
int argn, row;
register int col;
gray maxval;
int rows, cols;
struct HIPS_Header h;
pm_progname = argv[0];
argn = 1;
if ( argn < argc )
{
ifd = pm_openr( argv[argn] );
argn++;
}
else
ifd = stdin;
if ( argn != argc )
pm_usage( "[hipsfile]" );
read_hips_header( ifd, &h );
cols = h.cols;
rows = h.rows * h.num_frame;
switch ( h.pixel_format )
{
case HIPS_PFBYTE:
if ( h.bits_per_pixel != 8 )
pm_error(
"can't handle unusual bits_per_pixel %d", h.bits_per_pixel,
0,0,0,0 );
if ( h.bit_packing != 0 )
pm_error( "can't handle bit_packing", 0,0,0,0,0 );
maxval = 255;
break;
default:
pm_error( "unknown pixel format %d", h.pixel_format, 0,0,0,0 );
}
pgm_writepgminit( stdout, cols, rows, maxval );
grayrow = pgm_allocrow( cols );
for ( row = 0; row < rows; row++)
{
for ( col = 0, gP = grayrow; col < cols; col++, gP++ )
{
int ich;
switch ( h.pixel_format )
{
case HIPS_PFBYTE:
ich = getc( ifd );
if ( ich == EOF )
pm_error( "premature EOF", 0,0,0,0,0 );
*gP = (gray) ich;
break;
default:
pm_error( "can't happen", 0,0,0,0,0 );
}
}
pgm_writepgmrow( stdout, grayrow, cols, maxval );
}
pm_close( ifd );
exit( 0 );
}
read_hips_header( fd, hP )
FILE *fd;
struct HIPS_Header *hP;
{
char buf[5000];
/* Read and toss orig_name. */
read_line( fd, buf, 5000 );
/* Read and toss seq_name. */
read_line( fd, buf, 5000 );
/* Read num_frame. */
read_line( fd, buf, 5000 );
hP->num_frame = atoi( buf );
/* Read and toss orig_date. */
read_line( fd, buf, 5000 );
/* Read rows. */
read_line( fd, buf, 5000 );
hP->rows = atoi( buf );
/* Read cols. */
read_line( fd, buf, 5000 );
hP->cols = atoi( buf );
/* Read bits_per_pixel. */
read_line( fd, buf, 5000 );
hP->bits_per_pixel = atoi( buf );
/* Read bit_packing. */
read_line( fd, buf, 5000 );
hP->bit_packing = atoi( buf );
/* Read pixel_format. */
read_line( fd, buf, 5000 );
hP->pixel_format = atoi( buf );
/* Now read and toss lines until we get one with just a period. */
do
{
read_line( fd, buf, 5000 );
}
while ( strcmp( buf, ".\n" ) != 0 );
}
read_line( fd, buf, size )
FILE *fd;
char *buf;
int size;
{
if ( fgets( buf, size, fd ) == NULL )
pm_error( "error reading header", 0,0,0,0,0 );
}