| 1 |
/* bioradtopgm.c - convert a Biorad confocal image into a portable graymap |
| 2 |
** |
| 3 |
** Copyright (C) 1993 by Oliver Trepte, [email protected] |
| 4 |
** |
| 5 |
** Derived from the pbmplus package, |
| 6 |
** Copyright (C) 1989 by Jef Poskanzer. |
| 7 |
** |
| 8 |
** Permission to use, copy, modify, and distribute this software and its |
| 9 |
** documentation for any purpose and without fee is hereby granted, provided |
| 10 |
** that the above copyright notice appear in all copies and that both that |
| 11 |
** copyright notice and this permission notice appear in supporting |
| 12 |
** documentation. This software is provided "as is" without express or |
| 13 |
** implied warranty. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include <ctype.h> |
| 17 |
|
| 18 |
#include "pgm.h" |
| 19 |
#include "nstring.h" |
| 20 |
|
| 21 |
#define BYTE unsigned char |
| 22 |
#define BIORAD_HEADER_LENGTH 76 |
| 23 |
#define BYTE_TO_WORD(lsb,msb) (((BYTE) lsb) + (((BYTE) msb) << 8)) |
| 24 |
|
| 25 |
int |
| 26 |
main(int argc, char * argv[]) { |
| 27 |
FILE* ifp; |
| 28 |
gray* grayrow; |
| 29 |
gray* gP; |
| 30 |
int argn, row, i; |
| 31 |
int col, val, val2; |
| 32 |
int rows=0, cols=0, image_num= -1, image_count, byte_word, check_word; |
| 33 |
int maxval; |
| 34 |
BYTE buf[BIORAD_HEADER_LENGTH]; |
| 35 |
const char* const usage = "[-image#] [Bioradfile]"; |
| 36 |
|
| 37 |
|
| 38 |
pgm_init( &argc, argv ); |
| 39 |
|
| 40 |
argn = 1; |
| 41 |
|
| 42 |
while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' ) |
| 43 |
{ |
| 44 |
if ( ISDIGIT( argv[argn][1] )) |
| 45 |
{ |
| 46 |
image_num = atoi( (argv[argn]+1) ); |
| 47 |
|
| 48 |
/* TODO atoi() ignores errors. Should be: |
| 49 |
|
| 50 |
const char * error; |
| 51 |
|
| 52 |
pm_string_to_int( argv[argn]+1, &image_num, error); |
| 53 |
|
| 54 |
if (error) |
| 55 |
pm_error("Invalid image number. %s", error); |
| 56 |
else if (image_num < 0) |
| 57 |
pm_error("Internal error: negative image number"); |
| 58 |
*/ |
| 59 |
} |
| 60 |
else |
| 61 |
pm_usage( usage ); |
| 62 |
++argn; |
| 63 |
} |
| 64 |
|
| 65 |
if ( argn < argc ) |
| 66 |
{ |
| 67 |
ifp = pm_openr( argv[argn] ); |
| 68 |
++argn; |
| 69 |
} |
| 70 |
else |
| 71 |
ifp = stdin; |
| 72 |
|
| 73 |
if ( argn != argc ) |
| 74 |
pm_usage( usage ); |
| 75 |
|
| 76 |
for ( i = 0; i < BIORAD_HEADER_LENGTH; ++i ) |
| 77 |
{ |
| 78 |
val = getc( ifp ); |
| 79 |
if ( val == EOF ) |
| 80 |
pm_error( "EOF / read error" ); |
| 81 |
buf[ i ] = val; |
| 82 |
} |
| 83 |
|
| 84 |
cols = BYTE_TO_WORD(buf[0], buf[1]); |
| 85 |
rows = BYTE_TO_WORD(buf[2], buf[3]); |
| 86 |
image_count = BYTE_TO_WORD(buf[4], buf[5]); |
| 87 |
byte_word = BYTE_TO_WORD(buf[14], buf[15]); |
| 88 |
check_word = BYTE_TO_WORD(buf[54], buf[55]); |
| 89 |
|
| 90 |
if ( check_word != 12345 ) |
| 91 |
pm_error( "Not a Biorad file" ); |
| 92 |
|
| 93 |
if ( cols <= 0 ) |
| 94 |
pm_error( "Strange image size, cols = %d", cols); |
| 95 |
|
| 96 |
if ( rows <= 0 ) |
| 97 |
pm_error( "Strange image size, rows = %d", rows); |
| 98 |
|
| 99 |
if ( image_count <= 0 ) |
| 100 |
pm_error( "Number of images in file is %d", image_count); |
| 101 |
|
| 102 |
if ( byte_word ) |
| 103 |
maxval = 255; |
| 104 |
else |
| 105 |
{ |
| 106 |
maxval = 65535; /* Perhaps this should be something else */ |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
pm_message( "Image size: %d cols, %d rows", cols, rows); |
| 111 |
pm_message( "%s", |
| 112 |
(byte_word) ? "Byte image (8 bits)" : "Word image (16 bits)"); |
| 113 |
|
| 114 |
if ( image_num < 0 ) |
| 115 |
pm_message( "Input contains %d image%c", |
| 116 |
image_count, (image_count > 1) ? 's' : '\0'); |
| 117 |
else |
| 118 |
{ |
| 119 |
if ( image_num >= image_count ) |
| 120 |
pm_error( "Cannot extract image %d, input contains only %d image%s", |
| 121 |
image_num, image_count, (image_count > 1) ? "s" : "" ); |
| 122 |
for ( i = (byte_word) ? image_num : image_num*2 ; i > 0 ; --i ) { |
| 123 |
for ( row = 0; row < rows; ++row) |
| 124 |
for ( col = 0; col < cols; ++col ) |
| 125 |
{ |
| 126 |
val = getc( ifp ); |
| 127 |
if ( val == EOF ) { |
| 128 |
pm_error( "EOF / read error" ); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
pgm_writepgminit( stdout, cols, rows, (gray) maxval, 0 ); |
| 134 |
grayrow = pgm_allocrow( cols ); |
| 135 |
|
| 136 |
for ( row = 0; row < rows; ++row) |
| 137 |
{ |
| 138 |
for ( col = 0, gP = grayrow; col < cols; ++col ) |
| 139 |
{ |
| 140 |
val = getc( ifp ); |
| 141 |
if ( val == EOF ) |
| 142 |
pm_error( "EOF / read error" ); |
| 143 |
if (byte_word) |
| 144 |
*gP++ = val; |
| 145 |
else |
| 146 |
{ |
| 147 |
val2 = getc( ifp ); |
| 148 |
if ( val2 == EOF ) |
| 149 |
pm_error( "EOF / read error" ); |
| 150 |
*gP++ = BYTE_TO_WORD(val, val2); |
| 151 |
} |
| 152 |
} |
| 153 |
pgm_writepgmrow( stdout, grayrow, cols, (gray) maxval, 0 ); |
| 154 |
} |
| 155 |
|
| 156 |
pm_close( ifp ); |
| 157 |
pm_close( stdout ); |
| 158 |
|
| 159 |
} |
| 160 |
exit( 0 ); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|