| 1 |
/* rawtopgm.c - convert raw grayscale bytes into 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 <stdbool.h> |
| 14 |
#include <math.h> |
| 15 |
|
| 16 |
#include "pm_c_util.h" |
| 17 |
#include "mallocvar.h" |
| 18 |
#include "shhopt.h" |
| 19 |
#include "pgm.h" |
| 20 |
|
| 21 |
struct CmdlineInfo { |
| 22 |
/* All the information the user supplied in the command line, |
| 23 |
in a form easy for the program to use. |
| 24 |
*/ |
| 25 |
const char * inputFileName; |
| 26 |
unsigned int headerskip; |
| 27 |
float rowskip; |
| 28 |
int bottomfirst; /* the -bottomfirst/-bt option */ |
| 29 |
int autosize; /* User wants us to figure out the size */ |
| 30 |
unsigned int width; |
| 31 |
unsigned int height; |
| 32 |
int bpp; |
| 33 |
/* bytes per pixel in input format. 1 or 2 */ |
| 34 |
int littleendian; |
| 35 |
/* logical: samples in input are least significant byte first */ |
| 36 |
int maxval; /* -maxval option, or -1 if none */ |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
static void |
| 41 |
parseCommandLine(int argc, const char ** argv, |
| 42 |
struct CmdlineInfo * const cmdlineP) { |
| 43 |
/*---------------------------------------------------------------------------- |
| 44 |
Note that the file spec array we return is stored in the storage that |
| 45 |
was passed to us as the argv array. |
| 46 |
-----------------------------------------------------------------------------*/ |
| 47 |
optEntry * option_def; |
| 48 |
optStruct3 opt; |
| 49 |
|
| 50 |
unsigned int option_def_index; |
| 51 |
|
| 52 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 53 |
|
| 54 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 55 |
OPTENT3(0, "bottomfirst", OPT_FLAG, &cmdlineP->bottomfirst, |
| 56 |
NULL, 0); |
| 57 |
OPTENT3(0, "bt", OPT_FLAG, &cmdlineP->bottomfirst, |
| 58 |
NULL, 0); |
| 59 |
OPTENT3(0, "topbottom", OPT_FLAG, &cmdlineP->bottomfirst, |
| 60 |
NULL, 0); |
| 61 |
OPTENT3(0, "tb", OPT_FLAG, &cmdlineP->bottomfirst, |
| 62 |
NULL, 0); |
| 63 |
OPTENT3(0, "headerskip", OPT_UINT, &cmdlineP->headerskip, |
| 64 |
NULL, 0); |
| 65 |
OPTENT3(0, "rowskip", OPT_FLOAT, &cmdlineP->rowskip, |
| 66 |
NULL, 0); |
| 67 |
OPTENT3(0, "bpp", OPT_INT, &cmdlineP->bpp, |
| 68 |
NULL, 0); |
| 69 |
OPTENT3(0, "littleendian", OPT_FLAG, &cmdlineP->littleendian, |
| 70 |
NULL, 0); |
| 71 |
OPTENT3(0, "maxval", OPT_UINT, &cmdlineP->maxval, |
| 72 |
NULL, 0); |
| 73 |
|
| 74 |
/* Set the defaults */ |
| 75 |
cmdlineP->bottomfirst = false; |
| 76 |
cmdlineP->headerskip = 0; |
| 77 |
cmdlineP->rowskip = 0.0; |
| 78 |
cmdlineP->bpp = 1; |
| 79 |
cmdlineP->littleendian = 0; |
| 80 |
cmdlineP->maxval = -1; |
| 81 |
|
| 82 |
opt.opt_table = option_def; |
| 83 |
opt.short_allowed = false; /* We have no short (old-fashioned) options */ |
| 84 |
opt.allowNegNum = false; /* We may have parms that are negative numbers */ |
| 85 |
|
| 86 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 87 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 88 |
|
| 89 |
if (argc-1 == 0) { |
| 90 |
cmdlineP->inputFileName = "-"; |
| 91 |
cmdlineP->autosize = true; |
| 92 |
} else if (argc-1 == 1) { |
| 93 |
cmdlineP->inputFileName = argv[1]; |
| 94 |
cmdlineP->autosize = true; |
| 95 |
} else if (argc-1 == 2) { |
| 96 |
cmdlineP->inputFileName = "-"; |
| 97 |
cmdlineP->autosize = false; |
| 98 |
cmdlineP->width = pm_parse_width(argv[1]); |
| 99 |
cmdlineP->height = pm_parse_height(argv[2]); |
| 100 |
} else if (argc-1 == 3) { |
| 101 |
cmdlineP->inputFileName = argv[3]; |
| 102 |
cmdlineP->autosize = false; |
| 103 |
cmdlineP->width = pm_parse_width(argv[1]); |
| 104 |
cmdlineP->height = pm_parse_height(argv[2]); |
| 105 |
} else |
| 106 |
pm_error("Program takes zero, one, two, or three arguments. You " |
| 107 |
"specified %d", argc-1); |
| 108 |
|
| 109 |
if (cmdlineP->bpp != 1 && cmdlineP->bpp != 2) |
| 110 |
pm_error("Bytes per pixel (-bpp) must be 1 or 2. You specified %d.", |
| 111 |
cmdlineP->bpp); |
| 112 |
|
| 113 |
if (cmdlineP->maxval == 0) |
| 114 |
pm_error("Maxval (-maxval) may not be zero."); |
| 115 |
|
| 116 |
if (cmdlineP->maxval > 255 && cmdlineP->bpp == 1) |
| 117 |
pm_error("You have specified one byte per pixel, but a maxval " |
| 118 |
"too large to fit in one byte: %d", cmdlineP->maxval); |
| 119 |
if (cmdlineP->maxval > 65535) |
| 120 |
pm_error("Maxval must be less than 65536. You specified %d.", |
| 121 |
cmdlineP->maxval); |
| 122 |
|
| 123 |
if (cmdlineP->rowskip && cmdlineP->autosize) |
| 124 |
pm_error("If you specify -rowskip, you must also give the image " |
| 125 |
"dimensions."); |
| 126 |
if (cmdlineP->rowskip && cmdlineP->bottomfirst) |
| 127 |
pm_error("You cannot specify both -rowskip and -bottomfirst. This is " |
| 128 |
"a limitation of this program."); |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
static void |
| 135 |
computeImageSize(struct CmdlineInfo const cmdline, |
| 136 |
long const nRead, |
| 137 |
unsigned int * const rowsP, |
| 138 |
unsigned int * const colsP) { |
| 139 |
|
| 140 |
if (cmdline.autosize) { |
| 141 |
int sqrtTrunc = |
| 142 |
(int) sqrt((double) (nRead - cmdline.headerskip)); |
| 143 |
if (sqrtTrunc * sqrtTrunc + cmdline.headerskip != nRead) |
| 144 |
pm_error( "You must specify the dimensions of the image unless " |
| 145 |
"it is a quadratic image. This one is not quadratic: " |
| 146 |
"The number of " |
| 147 |
"pixels in the input is %ld, which is not a perfect " |
| 148 |
"square.", nRead - cmdline.headerskip); |
| 149 |
*rowsP = *colsP = sqrtTrunc; |
| 150 |
pm_message( "Image size: %u cols, %u rows", *colsP, *rowsP); |
| 151 |
} else { |
| 152 |
*rowsP = cmdline.height; |
| 153 |
*colsP = cmdline.width; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
static void |
| 160 |
skipHeader(FILE * const ifP, |
| 161 |
unsigned int const headerskip) { |
| 162 |
|
| 163 |
int i; |
| 164 |
|
| 165 |
for (i = 0; i < headerskip; ++i) { |
| 166 |
/* Read a byte out of the file */ |
| 167 |
int val; |
| 168 |
val = getc(ifP); |
| 169 |
if (val == EOF) |
| 170 |
pm_error("EOF / read error reading Byte %u in the header", i ); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
static gray |
| 177 |
readFromFile(FILE * const ifP, |
| 178 |
unsigned int const bpp, |
| 179 |
unsigned int const row, |
| 180 |
unsigned int const col, |
| 181 |
bool const littleEndian) { |
| 182 |
/*---------------------------------------------------------------------------- |
| 183 |
Return the next sample value from the input file *ifP, assuming the |
| 184 |
input stream is 'bpp' bytes per pixel (1 or 2). In the case of two |
| 185 |
bytes, if 'littleEndian', assume least significant byte is first. |
| 186 |
Otherwise, assume MSB first. |
| 187 |
|
| 188 |
In error messages, say this is Column 'col', Row 'row'. Exit program if |
| 189 |
error. |
| 190 |
-----------------------------------------------------------------------------*/ |
| 191 |
gray retval; |
| 192 |
|
| 193 |
if (bpp == 1) { |
| 194 |
int val; |
| 195 |
val = getc(ifP); |
| 196 |
if (val == EOF) |
| 197 |
pm_error( "EOF / read error at Row %u Column %u", |
| 198 |
row, col); |
| 199 |
retval = (gray) val; |
| 200 |
} else { |
| 201 |
short val; |
| 202 |
int rc; |
| 203 |
rc = littleEndian ? |
| 204 |
pm_readlittleshort(ifP, &val) : pm_readbigshort(ifP, &val); |
| 205 |
if (rc != 0) |
| 206 |
pm_error( "EOF / read error at Row %u Column %u", |
| 207 |
row, col); |
| 208 |
retval = (gray) val; |
| 209 |
} |
| 210 |
return retval; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
int |
| 216 |
main(int argc, const char ** argv) { |
| 217 |
|
| 218 |
struct CmdlineInfo cmdline; |
| 219 |
FILE * ifP; |
| 220 |
gray * grayrow; |
| 221 |
unsigned int rows, cols; |
| 222 |
gray maxval; |
| 223 |
char * buf; |
| 224 |
/* pixels1 and pixels2 are the array of pixels in the input buffer |
| 225 |
(assuming we are using an input buffer). pixels1 is the array |
| 226 |
as if the pixels are one byte each. pixels2 is the array as if |
| 227 |
they are two bytes each. |
| 228 |
*/ |
| 229 |
unsigned char * pixels1; |
| 230 |
unsigned short * pixels2; |
| 231 |
long nRead; |
| 232 |
unsigned int row; |
| 233 |
float toskip; |
| 234 |
|
| 235 |
pm_proginit(&argc, argv); |
| 236 |
|
| 237 |
parseCommandLine(argc, argv, &cmdline); |
| 238 |
|
| 239 |
ifP = pm_openr(cmdline.inputFileName); |
| 240 |
|
| 241 |
if (cmdline.autosize || cmdline.bottomfirst) { |
| 242 |
buf = pm_read_unknown_size(ifP, &nRead); |
| 243 |
pixels1 = (unsigned char *) buf; |
| 244 |
pixels2 = (unsigned short *) buf; |
| 245 |
} else |
| 246 |
buf = NULL; |
| 247 |
|
| 248 |
computeImageSize(cmdline, nRead, &rows, &cols); |
| 249 |
|
| 250 |
if (!buf) |
| 251 |
skipHeader(ifP, cmdline.headerskip); |
| 252 |
|
| 253 |
toskip = 0.00001; |
| 254 |
|
| 255 |
if (cmdline.maxval == -1) |
| 256 |
maxval = (cmdline.bpp == 1 ? (gray) 255 : (gray) 65535); |
| 257 |
else |
| 258 |
maxval = cmdline.maxval; |
| 259 |
|
| 260 |
pgm_writepgminit(stdout, cols, rows, maxval, 0); |
| 261 |
|
| 262 |
grayrow = pgm_allocrow(cols); |
| 263 |
|
| 264 |
for (row = 0; row < rows; ++row) { |
| 265 |
unsigned int col; |
| 266 |
unsigned int rowpos; /* index of this row in pixel array */ |
| 267 |
|
| 268 |
if (cmdline.bottomfirst) |
| 269 |
rowpos = (rows-row-1) * cols; |
| 270 |
else |
| 271 |
rowpos = row * cols; |
| 272 |
|
| 273 |
for (col = 0; col < cols; ++col) { |
| 274 |
if (buf) { |
| 275 |
if (cmdline.bpp == 1) |
| 276 |
grayrow[col] = pixels1[rowpos+col]; |
| 277 |
else |
| 278 |
grayrow[col] = pixels2[rowpos+col]; |
| 279 |
} else { |
| 280 |
grayrow[col] = readFromFile(ifP, cmdline.bpp, |
| 281 |
row, col, |
| 282 |
cmdline.littleendian > 0); |
| 283 |
} |
| 284 |
} |
| 285 |
for (toskip += cmdline.rowskip; toskip >= 1.0; toskip -= 1.0) { |
| 286 |
/* Note that if we're using a buffer, cmdline.rowskip is zero */ |
| 287 |
int val; |
| 288 |
val = getc(ifP); |
| 289 |
if (val == EOF) |
| 290 |
pm_error("EOF / read error skipping bytes at the end " |
| 291 |
"of Row %u.", row); |
| 292 |
} |
| 293 |
pgm_writepgmrow(stdout, grayrow, cols, maxval, 0); |
| 294 |
} |
| 295 |
|
| 296 |
if (buf) |
| 297 |
free(buf); |
| 298 |
pm_close(ifP); |
| 299 |
pm_close(stdout); |
| 300 |
|
| 301 |
return 0; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|