| 1 |
/* sgitopnm.c - read an SGI image and and produce a portable anymap |
| 2 |
** |
| 3 |
** Copyright (C) 1994 by Ingo Wilken ([email protected]) |
| 4 |
** |
| 5 |
** Based on the SGI image description v0.9 by Paul Haeberli ([email protected]) |
| 6 |
** Available via ftp from sgi.com:graphics/SGIIMAGESPEC |
| 7 |
** |
| 8 |
** The definitive document describing the SGI image file format, |
| 9 |
** SGI Image File Format Version 1.00 is available from |
| 10 |
** ftp://ftp.sgi.com/graphics/grafica/sgiimage.html |
| 11 |
** |
| 12 |
** |
| 13 |
** Permission to use, copy, modify, and distribute this software and its |
| 14 |
** documentation for any purpose and without fee is hereby granted, provided |
| 15 |
** that the above copyright notice appear in all copies and that both that |
| 16 |
** copyright notice and this permission notice appear in supporting |
| 17 |
** documentation. This software is provided "as is" without express or |
| 18 |
** implied warranty. |
| 19 |
** |
| 20 |
*/ |
| 21 |
|
| 22 |
|
| 23 |
#include <unistd.h> |
| 24 |
#include <limits.h> |
| 25 |
#include "pm_c_util.h" |
| 26 |
#include "mallocvar.h" |
| 27 |
#include "shhopt.h" |
| 28 |
#include "pnm.h" |
| 29 |
#include "sgi.h" |
| 30 |
|
| 31 |
#define MAX_ZSIZE 256 |
| 32 |
|
| 33 |
|
| 34 |
struct CmdlineInfo { |
| 35 |
/* All the information the user supplied in the command line, |
| 36 |
in a form easy for the program to use. |
| 37 |
*/ |
| 38 |
const char * inputFileName; /* '-' if stdin */ |
| 39 |
unsigned int verbose; |
| 40 |
unsigned int channelSpec; |
| 41 |
unsigned int channel; |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
static void |
| 47 |
parseCommandLine(int argc, const char ** argv, |
| 48 |
struct CmdlineInfo * const cmdlineP) { |
| 49 |
/*---------------------------------------------------------------------------- |
| 50 |
parse program command line described in Unix standard form by argc |
| 51 |
and argv. Return the information in the options as *cmdlineP. |
| 52 |
|
| 53 |
If command line is internally inconsistent (invalid options, etc.), |
| 54 |
issue error message to stderr and abort program. |
| 55 |
|
| 56 |
Note that the strings we return are stored in the storage that |
| 57 |
was passed to us as the argv array. We also trash *argv. |
| 58 |
-----------------------------------------------------------------------------*/ |
| 59 |
optEntry * option_def; /* Used by OPTENT3 */ |
| 60 |
optStruct3 opt; |
| 61 |
|
| 62 |
unsigned int option_def_index; |
| 63 |
|
| 64 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 65 |
|
| 66 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 67 |
OPTENT3(0, "channel", OPT_UINT, |
| 68 |
&cmdlineP->channel, |
| 69 |
&cmdlineP->channelSpec, 0); |
| 70 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, |
| 71 |
&cmdlineP->verbose, 0); |
| 72 |
OPTENT3(0, "noverbose", OPT_FLAG, NULL, |
| 73 |
NULL, 0); /* backward compatibility */ |
| 74 |
|
| 75 |
opt.opt_table = option_def; |
| 76 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 77 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 78 |
|
| 79 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 80 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 81 |
|
| 82 |
free(option_def); |
| 83 |
|
| 84 |
if (!cmdlineP->channelSpec) |
| 85 |
cmdlineP->channel = MAX_ZSIZE + 1; |
| 86 |
/* Invalid value; to suppress Valgrind error */ |
| 87 |
|
| 88 |
if (argc-1 < 1) |
| 89 |
cmdlineP->inputFileName = "-"; |
| 90 |
else if (argc-1 == 1) |
| 91 |
cmdlineP->inputFileName = argv[1]; |
| 92 |
else |
| 93 |
pm_error("Program takes at most one argument: input file name"); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
/* basic I/O functions, taken from ilbmtoppm.c */ |
| 99 |
|
| 100 |
static void |
| 101 |
readerr(FILE * const f) { |
| 102 |
|
| 103 |
if (ferror(f)) |
| 104 |
pm_error("read error"); |
| 105 |
else |
| 106 |
pm_error("premature EOF"); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
static short |
| 112 |
getBigShort(FILE * const ifP) { |
| 113 |
|
| 114 |
short s; |
| 115 |
|
| 116 |
if (pm_readbigshort(ifP, &s) == -1) |
| 117 |
readerr(ifP); |
| 118 |
|
| 119 |
return s; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
static long |
| 125 |
getBigLong(FILE * const ifP) { |
| 126 |
|
| 127 |
long l; |
| 128 |
|
| 129 |
if (pm_readbiglong(ifP, &l) == -1) |
| 130 |
readerr(ifP); |
| 131 |
|
| 132 |
return l; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static unsigned char |
| 138 |
getByte(FILE * const ifP) { |
| 139 |
|
| 140 |
int i; |
| 141 |
|
| 142 |
i = getc(ifP); |
| 143 |
if (i == EOF) |
| 144 |
readerr(ifP); |
| 145 |
|
| 146 |
return (unsigned char) i; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static void |
| 152 |
readBytes(FILE * const ifP, |
| 153 |
int const n, |
| 154 |
char * const buf) { |
| 155 |
|
| 156 |
int r; |
| 157 |
|
| 158 |
r = fread((void *)buf, 1, n, ifP); |
| 159 |
|
| 160 |
if (r != n) |
| 161 |
readerr(ifP); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
static short |
| 167 |
getByteAsShort(FILE * const ifP) { |
| 168 |
|
| 169 |
return (short)getByte(ifP); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static const char * |
| 175 |
compressionName(unsigned char const storageCode) { |
| 176 |
|
| 177 |
switch (storageCode) { |
| 178 |
case STORAGE_VERBATIM: |
| 179 |
return "none"; |
| 180 |
case STORAGE_RLE: |
| 181 |
return "RLE"; |
| 182 |
default: |
| 183 |
return "unknown"; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
/* entry in RLE offset table */ |
| 190 |
typedef struct { |
| 191 |
long start; /* offset in file */ |
| 192 |
long length; /* length of compressed scanline */ |
| 193 |
} TabEntry; |
| 194 |
|
| 195 |
typedef short ScanElem; |
| 196 |
typedef ScanElem * ScanLine; |
| 197 |
|
| 198 |
#define WORSTCOMPR(x) (2*(x) + 2) |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
static Header * |
| 203 |
readHeader(FILE * const ifP, |
| 204 |
bool const outChannelSpec, |
| 205 |
bool const verbose) { |
| 206 |
|
| 207 |
Header * headP; |
| 208 |
|
| 209 |
MALLOCVAR_NOFAIL(headP); |
| 210 |
|
| 211 |
headP->magic = getBigShort(ifP); |
| 212 |
headP->storage = getByte(ifP); |
| 213 |
headP->bpc = getByte(ifP); |
| 214 |
headP->dimension = getBigShort(ifP); |
| 215 |
headP->xsize = getBigShort(ifP); |
| 216 |
headP->ysize = getBigShort(ifP); |
| 217 |
headP->zsize = getBigShort(ifP); |
| 218 |
if (headP->zsize > MAX_ZSIZE) |
| 219 |
pm_error("Too many channels in input image: %u", |
| 220 |
(unsigned int) headP->zsize ); |
| 221 |
headP->pixmin = getBigLong(ifP); |
| 222 |
headP->pixmax = getBigLong(ifP); |
| 223 |
if (headP->pixmin >= headP->pixmax) |
| 224 |
pm_error("Invalid sgi image header: pixmin larger than pixmax"); |
| 225 |
readBytes(ifP, 4, headP->dummy1); |
| 226 |
readBytes(ifP, 80, headP->name); |
| 227 |
headP->colormap = getBigLong(ifP); |
| 228 |
readBytes(ifP, 404, headP->dummy2); |
| 229 |
|
| 230 |
if (headP->magic != SGI_MAGIC) |
| 231 |
pm_error("bad magic number - not an SGI image"); |
| 232 |
if (headP->storage != STORAGE_VERBATIM && headP->storage != STORAGE_RLE) |
| 233 |
pm_error("unknown compression type"); |
| 234 |
if (headP->bpc < 1 || headP->bpc > 2) |
| 235 |
pm_error("illegal precision value %d (only 1-2 allowed)", headP->bpc); |
| 236 |
if (headP->colormap != CMAP_NORMAL) |
| 237 |
pm_error("non-normal pixel data of a form we don't recognize"); |
| 238 |
|
| 239 |
/* adjust ysize/zsize to dimension, just to be sure */ |
| 240 |
switch (headP->dimension) { |
| 241 |
case 1: |
| 242 |
headP->ysize = 1; |
| 243 |
break; |
| 244 |
case 2: |
| 245 |
headP->zsize = 1; |
| 246 |
break; |
| 247 |
case 3: |
| 248 |
switch (headP->zsize) { |
| 249 |
case 1: |
| 250 |
headP->dimension = 2; |
| 251 |
break; |
| 252 |
case 2: |
| 253 |
if (!outChannelSpec) |
| 254 |
pm_message("2-channel image, using only first channel. " |
| 255 |
"Extract alpha channel with -channel=1"); |
| 256 |
break; |
| 257 |
case 3: |
| 258 |
break; |
| 259 |
default: |
| 260 |
if (!outChannelSpec) |
| 261 |
pm_message("%u-channel image, using only first 3 channels " |
| 262 |
"Extract %s with -channel=%c", |
| 263 |
headP->zsize, |
| 264 |
headP->zsize==4 ? |
| 265 |
"alpha channel" : "additional channels", |
| 266 |
headP->zsize==4 ? '3' : 'N'); |
| 267 |
break; |
| 268 |
} |
| 269 |
break; |
| 270 |
default: |
| 271 |
pm_error("illegal dimension value %u (only 1-3 allowed)", |
| 272 |
headP->dimension); |
| 273 |
} |
| 274 |
|
| 275 |
if (verbose) { |
| 276 |
pm_message("raster size %ux%u, %u channels", |
| 277 |
headP->xsize, headP->ysize, headP->zsize); |
| 278 |
pm_message("compression: 0x%02x = %s", |
| 279 |
headP->storage, compressionName(headP->storage)); |
| 280 |
headP->name[79] = '\0'; /* just to be safe */ |
| 281 |
pm_message("Image name: '%s'", headP->name); |
| 282 |
} |
| 283 |
|
| 284 |
return headP; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
static TabEntry * |
| 290 |
readTable(FILE * const ifP, |
| 291 |
int const tablen) { |
| 292 |
|
| 293 |
TabEntry * table; |
| 294 |
unsigned int i; |
| 295 |
|
| 296 |
MALLOCARRAY_NOFAIL(table, tablen); |
| 297 |
|
| 298 |
for (i = 0; i < tablen; ++i) |
| 299 |
table[i].start = getBigLong(ifP); |
| 300 |
for (i = 0; i < tablen; ++i) |
| 301 |
table[i].length = getBigLong(ifP); |
| 302 |
|
| 303 |
return table; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
static void |
| 309 |
rleDecompress(ScanElem * const srcStart, |
| 310 |
int const srcleftStart, |
| 311 |
ScanElem * const destStart, |
| 312 |
int const destleftStart) { |
| 313 |
|
| 314 |
ScanElem * src; |
| 315 |
int srcleft; |
| 316 |
ScanElem * dest; |
| 317 |
int destleft; |
| 318 |
|
| 319 |
for (src = srcStart, |
| 320 |
srcleft = srcleftStart, |
| 321 |
dest = destStart, |
| 322 |
destleft = destleftStart; srcleft; ) { |
| 323 |
|
| 324 |
unsigned char const el = (unsigned char)(*src++ & 0xff); |
| 325 |
unsigned int const count = (unsigned int)(el & 0x7f); |
| 326 |
|
| 327 |
--srcleft; |
| 328 |
|
| 329 |
if (count == 0) |
| 330 |
return; |
| 331 |
if (destleft < count) |
| 332 |
pm_error("RLE error: too much input data " |
| 333 |
"(space left %d, need %d)", destleft, count); |
| 334 |
destleft -= count; |
| 335 |
if (el & 0x80) { |
| 336 |
unsigned int i; |
| 337 |
if (srcleft < count) |
| 338 |
pm_error("RLE error: not enough data for literal run " |
| 339 |
"(data left %d, need %d)", srcleft, count); |
| 340 |
srcleft -= count; |
| 341 |
for (i = 0; i < count; ++i) |
| 342 |
*dest++ = *src++; |
| 343 |
} else { |
| 344 |
unsigned int i; |
| 345 |
if (srcleft == 0) |
| 346 |
pm_error("RLE error: not enough data for replicate run"); |
| 347 |
for (i = 0; i < count; ++i) |
| 348 |
*dest++ = *src; |
| 349 |
++src; |
| 350 |
--srcleft; |
| 351 |
} |
| 352 |
} |
| 353 |
pm_error("RLE error: no terminating 0-byte"); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
static ScanLine * |
| 359 |
readChannels(FILE * const ifP, |
| 360 |
Header * const head, |
| 361 |
TabEntry * const table, |
| 362 |
bool const outChannelSpec, |
| 363 |
unsigned int const outChannel) { |
| 364 |
|
| 365 |
ScanLine * image; |
| 366 |
ScanElem * temp; |
| 367 |
unsigned int channel; |
| 368 |
unsigned int maxchannel; |
| 369 |
|
| 370 |
if (outChannelSpec) { |
| 371 |
maxchannel = outChannel + 1; |
| 372 |
MALLOCARRAY_NOFAIL(image, head->ysize); |
| 373 |
} else if (head->zsize <= 2) { |
| 374 |
maxchannel = 1; |
| 375 |
MALLOCARRAY_NOFAIL(image, head->ysize); |
| 376 |
} else { |
| 377 |
maxchannel = 3; |
| 378 |
MALLOCARRAY_NOFAIL(image, head->ysize * maxchannel); |
| 379 |
} |
| 380 |
if (table) |
| 381 |
MALLOCARRAY_NOFAIL(temp, WORSTCOMPR(head->xsize)); |
| 382 |
|
| 383 |
for (channel = 0; channel < maxchannel; ++channel) { |
| 384 |
unsigned int row; |
| 385 |
for (row = 0; row < head->ysize; ++row) { |
| 386 |
int const sgiIndex = channel * head->ysize + row; |
| 387 |
|
| 388 |
unsigned long int iindex; |
| 389 |
|
| 390 |
iindex = outChannelSpec ? row : sgiIndex; |
| 391 |
if (!outChannelSpec || outChannel == channel) |
| 392 |
MALLOCARRAY_NOFAIL(image[iindex], head->xsize); |
| 393 |
|
| 394 |
if (table) { |
| 395 |
if (!outChannelSpec || channel >= outChannel) { |
| 396 |
pm_filepos const offset = (pm_filepos) |
| 397 |
table[sgiIndex].start; |
| 398 |
long const length = head->bpc == 2 ? |
| 399 |
table[sgiIndex].length / 2 : |
| 400 |
table[sgiIndex].length; |
| 401 |
|
| 402 |
unsigned int i; |
| 403 |
|
| 404 |
/* Note: (offset < currentPosition) can happen */ |
| 405 |
|
| 406 |
pm_seek2(ifP, &offset, sizeof(offset)); |
| 407 |
|
| 408 |
for (i = 0; i < length; ++i) |
| 409 |
if (head->bpc == 1) |
| 410 |
temp[i] = getByteAsShort(ifP); |
| 411 |
else |
| 412 |
temp[i] = getBigShort(ifP); |
| 413 |
rleDecompress(temp, length, image[iindex], head->xsize); |
| 414 |
} |
| 415 |
} else { |
| 416 |
unsigned int i; |
| 417 |
for (i = 0; i < head->xsize; ++i) { |
| 418 |
ScanElem p; |
| 419 |
if (head->bpc == 1) |
| 420 |
p = getByteAsShort(ifP); |
| 421 |
else |
| 422 |
p = getBigShort(ifP); |
| 423 |
|
| 424 |
if (!outChannelSpec || outChannel == channel) |
| 425 |
image[iindex][i] = p; |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
} |
| 430 |
if (table) |
| 431 |
free(temp); |
| 432 |
return image; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
static void |
| 438 |
imageToPnm(Header * const head, |
| 439 |
ScanLine * const image, |
| 440 |
xelval const maxval, |
| 441 |
bool const outChannelSpec, |
| 442 |
unsigned int const outChannel) { |
| 443 |
|
| 444 |
int const sub = head->pixmin; |
| 445 |
xel * const pnmrow = pnm_allocrow(head->xsize); |
| 446 |
|
| 447 |
int row; |
| 448 |
int format; |
| 449 |
|
| 450 |
if (head->zsize <= 2 || outChannelSpec) { |
| 451 |
pm_message("writing PGM image"); |
| 452 |
format = PGM_TYPE; |
| 453 |
} else { |
| 454 |
pm_message("writing PPM image"); |
| 455 |
format = PPM_TYPE; |
| 456 |
} |
| 457 |
|
| 458 |
pnm_writepnminit(stdout, head->xsize, head->ysize, maxval, format, 0); |
| 459 |
for (row = head->ysize-1; row >= 0; --row) { |
| 460 |
unsigned int col; |
| 461 |
for (col = 0; col < head->xsize; ++col) { |
| 462 |
if (format == PGM_TYPE) |
| 463 |
PNM_ASSIGN1(pnmrow[col], image[row][col] - sub); |
| 464 |
else { |
| 465 |
pixval r, g, b; |
| 466 |
r = image[row][col] - sub; |
| 467 |
g = image[head->ysize + row][col] - sub; |
| 468 |
b = image[2* head->ysize + row][col] - sub; |
| 469 |
PPM_ASSIGN(pnmrow[col], r, g, b); |
| 470 |
} |
| 471 |
} |
| 472 |
pnm_writepnmrow(stdout, pnmrow, head->xsize, maxval, format, 0); |
| 473 |
} |
| 474 |
pnm_freerow(pnmrow); |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
int |
| 480 |
main(int argc, const char * argv[]) { |
| 481 |
|
| 482 |
struct CmdlineInfo cmdline; |
| 483 |
FILE * ifP; |
| 484 |
TabEntry * table; |
| 485 |
ScanLine * image; |
| 486 |
Header * headP; |
| 487 |
xelval maxval; |
| 488 |
|
| 489 |
pm_proginit(&argc, argv); |
| 490 |
|
| 491 |
parseCommandLine(argc, argv, &cmdline); |
| 492 |
|
| 493 |
ifP = pm_openr_seekable(cmdline.inputFileName); |
| 494 |
|
| 495 |
headP = readHeader(ifP, cmdline.channelSpec, cmdline.verbose); |
| 496 |
|
| 497 |
maxval = headP->pixmax - headP->pixmin; |
| 498 |
if (maxval > PNM_OVERALLMAXVAL) |
| 499 |
pm_error("Maximum sample value in input image (%d) is too large. " |
| 500 |
"This program's limit is %d.", |
| 501 |
maxval, PNM_OVERALLMAXVAL); |
| 502 |
|
| 503 |
if (cmdline.channelSpec && cmdline.channel >= headP->zsize) |
| 504 |
pm_error("channel out of range - only %d channels in image", |
| 505 |
headP->zsize); |
| 506 |
|
| 507 |
if (headP->storage != STORAGE_VERBATIM) |
| 508 |
table = readTable(ifP, headP->ysize * headP->zsize); |
| 509 |
else |
| 510 |
table = NULL; |
| 511 |
|
| 512 |
image = readChannels(ifP, headP, table, |
| 513 |
cmdline.channelSpec, cmdline.channel); |
| 514 |
|
| 515 |
imageToPnm(headP, image, maxval, cmdline.channelSpec, cmdline.channel); |
| 516 |
|
| 517 |
pm_close(ifP); |
| 518 |
|
| 519 |
return 0; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|