| 1 |
/** rlatopam.c - read Alias/Wavefront RLA or RPF file |
| 2 |
** |
| 3 |
** Copyright (C) 2005 Matte World Digital |
| 4 |
** |
| 5 |
** Author: Simon Walton |
| 6 |
** |
| 7 |
** Permission to use, copy, modify, and distribute this software and its |
| 8 |
** documentation for any purpose and without fee is hereby granted, provided |
| 9 |
** that the above copyright notice appear in all copies and that both that |
| 10 |
** copyright notice and this permission notice appear in supporting |
| 11 |
** documentation. This software is provided "as is" without express or |
| 12 |
** implied warranty. |
| 13 |
**/ |
| 14 |
|
| 15 |
|
| 16 |
#include <string.h> |
| 17 |
#include <errno.h> |
| 18 |
|
| 19 |
#include "pm_c_util.h" |
| 20 |
#include "shhopt.h" |
| 21 |
#include "mallocvar.h" |
| 22 |
#include "pam.h" |
| 23 |
#include "rla.h" |
| 24 |
|
| 25 |
static int * offsets; |
| 26 |
static bool is_float; |
| 27 |
static bool has_matte; |
| 28 |
|
| 29 |
static unsigned int depth; |
| 30 |
static unsigned int width; |
| 31 |
static unsigned int height; |
| 32 |
static unsigned int chanBits; |
| 33 |
static short storageType; |
| 34 |
static struct pam outpam; |
| 35 |
static unsigned int numChan; |
| 36 |
|
| 37 |
|
| 38 |
static void |
| 39 |
parseCommandLine(int const argc, |
| 40 |
char ** const argv, |
| 41 |
const char ** const inputFileNameP) { |
| 42 |
|
| 43 |
if (argc-1 < 1) |
| 44 |
*inputFileNameP = "-"; |
| 45 |
else { |
| 46 |
*inputFileNameP = argv[1]; |
| 47 |
|
| 48 |
if (argc-1 > 1) |
| 49 |
pm_error("There is at most one argument - input file name. " |
| 50 |
"You specified %u", argc-1); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
static bool littleEndian; |
| 57 |
|
| 58 |
|
| 59 |
static void |
| 60 |
determineEndianness(void) { |
| 61 |
|
| 62 |
union { |
| 63 |
unsigned char bytes[2]; |
| 64 |
unsigned short number; |
| 65 |
} u; |
| 66 |
|
| 67 |
u.number = 1; |
| 68 |
|
| 69 |
littleEndian = (u.bytes[0] == 1); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static unsigned short |
| 75 |
byteswap(unsigned short const input) { |
| 76 |
|
| 77 |
return (input << 8) | (input >> 8); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
static void |
| 83 |
read_header(FILE * const ifP, |
| 84 |
rlahdr * const hdrP) { |
| 85 |
|
| 86 |
rlahdr hdr; |
| 87 |
size_t bytesRead; |
| 88 |
|
| 89 |
fseek (ifP, 0, SEEK_SET); |
| 90 |
|
| 91 |
/* Here we have a hack. The bytes in the file are almost in the |
| 92 |
same format as the compiler stores 'hdr' in memory. The only |
| 93 |
difference is that the compiler may store the integer values |
| 94 |
in the obscene little-endian format. So we just read the whole |
| 95 |
header into 'hdr' as if it were the right format, and then |
| 96 |
correct the integer values by swapping their bytes. |
| 97 |
|
| 98 |
The _right_ way to do this is to read the file one field at a time, |
| 99 |
using pm_readbigshort() where appropriate. |
| 100 |
*/ |
| 101 |
|
| 102 |
bytesRead = fread(&hdr, sizeof hdr, 1, ifP); |
| 103 |
if (bytesRead != 1) |
| 104 |
pm_error("Unexpected EOF on input file."); |
| 105 |
|
| 106 |
if (littleEndian) { |
| 107 |
hdr.window.left = byteswap(hdr.window.left); |
| 108 |
hdr.window.right = byteswap(hdr.window.right); |
| 109 |
hdr.window.bottom = byteswap(hdr.window.bottom); |
| 110 |
hdr.window.top = byteswap(hdr.window.top); |
| 111 |
hdr.active_window.left = byteswap(hdr.active_window.left); |
| 112 |
hdr.active_window.right = byteswap(hdr.active_window.right); |
| 113 |
hdr.active_window.bottom = byteswap(hdr.active_window.bottom); |
| 114 |
hdr.active_window.top = byteswap(hdr.active_window.top); |
| 115 |
hdr.storage_type = byteswap(hdr.storage_type); |
| 116 |
hdr.num_chan = byteswap(hdr.num_chan); |
| 117 |
hdr.num_matte = byteswap(hdr.num_matte); |
| 118 |
hdr.revision = byteswap(hdr.revision); |
| 119 |
hdr.chan_bits = byteswap(hdr.chan_bits); |
| 120 |
hdr.matte_type = byteswap(hdr.matte_type); |
| 121 |
hdr.matte_bits = byteswap(hdr.matte_bits); |
| 122 |
} |
| 123 |
|
| 124 |
if (hdr.revision != 0xfffe && hdr.revision != 0xfffd) |
| 125 |
pm_error("Invalid file header. \"revision\" field should contain " |
| 126 |
"0xfffe or 0xfffd, but contains 0x%04x", hdr.revision); |
| 127 |
|
| 128 |
*hdrP = hdr; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
static void |
| 134 |
decodeFP(unsigned char * const in, |
| 135 |
unsigned char * const out, |
| 136 |
int const width, |
| 137 |
int const stride) { |
| 138 |
|
| 139 |
unsigned int x; |
| 140 |
unsigned char * inputCursor; |
| 141 |
unsigned char * outputCursor; |
| 142 |
|
| 143 |
inputCursor = &in[0]; |
| 144 |
|
| 145 |
for (x = 0; x < width; ++x) { |
| 146 |
union {char bytes [4]; float fv;} fi; |
| 147 |
unsigned short val; |
| 148 |
|
| 149 |
if (littleEndian) { |
| 150 |
fi.bytes [3] = *inputCursor++; |
| 151 |
fi.bytes [2] = *inputCursor++; |
| 152 |
fi.bytes [1] = *inputCursor++; |
| 153 |
fi.bytes [0] = *inputCursor++; |
| 154 |
} else { |
| 155 |
fi.bytes [0] = *inputCursor++; |
| 156 |
fi.bytes [1] = *inputCursor++; |
| 157 |
fi.bytes [2] = *inputCursor++; |
| 158 |
fi.bytes [3] = *inputCursor++; |
| 159 |
} |
| 160 |
|
| 161 |
val = fi.fv > 1 ? 65535 : (fi.fv < 0 ? 0 : |
| 162 |
(unsigned short) (65535 * fi.fv + .5)); |
| 163 |
outputCursor[0] = val >> 8; outputCursor[1] = val & 0xff; |
| 164 |
outputCursor += stride; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
static unsigned char * |
| 171 |
decode(unsigned char * const input, |
| 172 |
unsigned char * const output, |
| 173 |
int const xFile, |
| 174 |
int const xImage, |
| 175 |
int const stride) { |
| 176 |
|
| 177 |
int x; |
| 178 |
unsigned int bytes; |
| 179 |
unsigned int useX; |
| 180 |
unsigned char * inputCursor; |
| 181 |
unsigned char * outputCursor; |
| 182 |
|
| 183 |
inputCursor = &input[0]; |
| 184 |
outputCursor = &output[0]; |
| 185 |
x = xFile; |
| 186 |
bytes = 0; |
| 187 |
useX = 0; |
| 188 |
|
| 189 |
while (x > 0) { |
| 190 |
int count; |
| 191 |
|
| 192 |
count = *(signed char *)inputCursor++; |
| 193 |
++bytes; |
| 194 |
|
| 195 |
if (count >= 0) { |
| 196 |
/* Repeat pixel value (count + 1) times. */ |
| 197 |
while (count-- >= 0) { |
| 198 |
if (useX < xImage) { |
| 199 |
*outputCursor = *inputCursor; |
| 200 |
outputCursor += stride; |
| 201 |
} |
| 202 |
--x; |
| 203 |
++useX; |
| 204 |
} |
| 205 |
++inputCursor; |
| 206 |
++bytes; |
| 207 |
} else { |
| 208 |
/* Copy (-count) unencoded values. */ |
| 209 |
for (count = -count; count > 0; --count) { |
| 210 |
if (useX < xImage) { |
| 211 |
*outputCursor = *inputCursor; |
| 212 |
outputCursor += stride; |
| 213 |
} |
| 214 |
++inputCursor; |
| 215 |
++bytes; |
| 216 |
--x; |
| 217 |
++useX; |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
return inputCursor; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
static void |
| 227 |
decode_row(FILE * const ifP, |
| 228 |
int const row, |
| 229 |
unsigned char * const rb) { |
| 230 |
|
| 231 |
static unsigned char * read_buffer = NULL; |
| 232 |
unsigned int chan; |
| 233 |
int rc; |
| 234 |
|
| 235 |
if (!read_buffer) { |
| 236 |
MALLOCARRAY(read_buffer, width * 4); |
| 237 |
if (read_buffer == 0) |
| 238 |
pm_error("Unable to get memory for read_buffer"); |
| 239 |
} |
| 240 |
|
| 241 |
rc = fseek (ifP, offsets [height - row - 1], SEEK_SET); |
| 242 |
if (rc != 0) |
| 243 |
pm_error("fseek() failed with errno %d (%s)", |
| 244 |
errno, strerror(errno)); |
| 245 |
|
| 246 |
for (chan = 0; chan < outpam.depth; ++chan) { |
| 247 |
unsigned short length; |
| 248 |
size_t bytesRead; |
| 249 |
|
| 250 |
pm_readbigshortu(ifP, &length); |
| 251 |
if (length > width * 4) |
| 252 |
pm_error("Line too long - row %u, channel %u", row, chan); |
| 253 |
|
| 254 |
bytesRead = fread(read_buffer, 1, length, ifP); |
| 255 |
if (bytesRead != length) |
| 256 |
pm_error("EOF encountered unexpectedly"); |
| 257 |
|
| 258 |
if (is_float) |
| 259 |
decodeFP(read_buffer, rb + chan * 2, width, outpam.depth * 2); |
| 260 |
else if (depth > 8) { |
| 261 |
/* Hi byte */ |
| 262 |
unsigned char * const newpos = |
| 263 |
decode(read_buffer, rb + chan * 2, width, width, |
| 264 |
outpam.depth * 2); |
| 265 |
/* Lo byte */ |
| 266 |
decode(newpos, rb + chan * 2 + 1, width, width, |
| 267 |
outpam.depth * 2); |
| 268 |
} else |
| 269 |
decode(read_buffer, rb + chan, width, width, outpam.depth); |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
static void |
| 276 |
getHeaderInfo(FILE * const ifP, |
| 277 |
unsigned int * const widthP, |
| 278 |
unsigned int * const heightP, |
| 279 |
unsigned int * const depthP, |
| 280 |
bool * const hasMatteP, |
| 281 |
unsigned int * const chanBitsP, |
| 282 |
short * const storageType) { |
| 283 |
|
| 284 |
rlahdr hdr; |
| 285 |
int width, height; |
| 286 |
|
| 287 |
read_header(ifP, &hdr); |
| 288 |
|
| 289 |
height = hdr.active_window.top - hdr.active_window.bottom + 1; |
| 290 |
width = hdr.active_window.right - hdr.active_window.left + 1; |
| 291 |
if (width <=0) |
| 292 |
pm_error("Invalid input image. It says its width isn't positive"); |
| 293 |
if (height <=0) |
| 294 |
pm_error("Invalid input image. It says its height isn't positive"); |
| 295 |
|
| 296 |
*widthP = width; |
| 297 |
*heightP = height; |
| 298 |
|
| 299 |
if (hdr.num_chan != 1 && hdr.num_chan != 3) |
| 300 |
pm_error ("Input image has bad number of channels: %d. " |
| 301 |
"Should be 1 or 3.", |
| 302 |
hdr.num_chan); |
| 303 |
|
| 304 |
*depthP = hdr.chan_bits <= 8 ? 8 : 16; |
| 305 |
|
| 306 |
*hasMatteP = (hdr.num_matte > 0); |
| 307 |
*chanBitsP = hdr.chan_bits; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
static void |
| 313 |
readOffsetArray(FILE * const ifP, |
| 314 |
int ** const offsetsP, |
| 315 |
unsigned int const height) { |
| 316 |
|
| 317 |
int * offsets; |
| 318 |
unsigned int row; |
| 319 |
|
| 320 |
MALLOCARRAY(offsets, height); |
| 321 |
if (offsets == NULL) |
| 322 |
pm_error("Unable to allocate memory for the offsets array"); |
| 323 |
|
| 324 |
for (row = 0; row < height; ++row) { |
| 325 |
long l; |
| 326 |
pm_readbiglong(ifP, &l); |
| 327 |
offsets[row] = l; |
| 328 |
} |
| 329 |
*offsetsP = offsets; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
static void |
| 335 |
destroyOffsetArray(int * const offsets) { |
| 336 |
|
| 337 |
free(offsets); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
static void |
| 343 |
readAndWriteRaster(FILE * const ifP, |
| 344 |
const struct pam * const outpamP) { |
| 345 |
|
| 346 |
unsigned char * rowBuffer; |
| 347 |
tuple * tuplerow; |
| 348 |
unsigned int row; |
| 349 |
|
| 350 |
/* Hold one row of all image planes */ |
| 351 |
rowBuffer = calloc(1, width * outpamP->depth * 4); |
| 352 |
if (rowBuffer == NULL) |
| 353 |
pm_error("Unable to allocate memory for row buffer."); |
| 354 |
|
| 355 |
tuplerow = pnm_allocpamrow(outpamP); |
| 356 |
|
| 357 |
for (row = 0; row < height; ++row) { |
| 358 |
unsigned int col; |
| 359 |
unsigned char * rbP; |
| 360 |
|
| 361 |
decode_row(ifP, row, rowBuffer); |
| 362 |
for (col = 0, rbP = rowBuffer; col < width; ++col) { |
| 363 |
unsigned int chan; |
| 364 |
for (chan = 0; chan < outpamP->depth; ++chan) { |
| 365 |
if (depth > 8) { |
| 366 |
tuplerow[col][chan] = 256 * rbP[0] + rbP[1]; |
| 367 |
rbP += 2; |
| 368 |
} else { |
| 369 |
tuplerow[col][chan] = *rbP; |
| 370 |
rbP += 1; |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
pnm_writepamrow(outpamP, tuplerow); |
| 375 |
} |
| 376 |
pnm_freepamrow(tuplerow); |
| 377 |
free(rowBuffer); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
int |
| 383 |
main(int argc, |
| 384 |
char * argv[]) { |
| 385 |
|
| 386 |
const char * inputFileName; |
| 387 |
FILE * ifP; |
| 388 |
|
| 389 |
pnm_init(&argc, argv); |
| 390 |
|
| 391 |
determineEndianness(); |
| 392 |
|
| 393 |
parseCommandLine(argc, argv, &inputFileName); |
| 394 |
|
| 395 |
ifP = pm_openr_seekable(inputFileName); |
| 396 |
|
| 397 |
getHeaderInfo(ifP, &width, &height, &depth, &has_matte, |
| 398 |
&chanBits, &storageType); |
| 399 |
|
| 400 |
outpam.size = outpam.len = sizeof (struct pam); |
| 401 |
outpam.file = stdout; |
| 402 |
outpam.format = PAM_FORMAT; |
| 403 |
outpam.height = height; |
| 404 |
outpam.width = width; |
| 405 |
outpam.depth = numChan + (has_matte ? 1 : 0); |
| 406 |
outpam.maxval = (1 << (chanBits > 16 ? |
| 407 |
(9 + (chanBits - 1) % 8) |
| 408 |
/* Take top 2 of 3 or 4 bytes */ |
| 409 |
: chanBits)) - 1; |
| 410 |
|
| 411 |
/* Most apps seem to assume 32 bit integer is really floating point */ |
| 412 |
if (chanBits == 32 || storageType == 4) { |
| 413 |
is_float = TRUE; |
| 414 |
outpam.maxval = 65535; |
| 415 |
depth = 16; |
| 416 |
} |
| 417 |
|
| 418 |
outpam.bytes_per_sample = depth / 8; |
| 419 |
strcpy(outpam.tuple_type, (numChan == 3 ? "RGB" : "GRAYSCALE")); |
| 420 |
if (has_matte) |
| 421 |
strcat(outpam.tuple_type, "A"); |
| 422 |
|
| 423 |
readOffsetArray(ifP, &offsets, height); |
| 424 |
|
| 425 |
pnm_writepaminit(&outpam); |
| 426 |
|
| 427 |
readAndWriteRaster(ifP, &outpam); |
| 428 |
|
| 429 |
destroyOffsetArray(offsets); |
| 430 |
|
| 431 |
pm_close(ifP); |
| 432 |
|
| 433 |
return 0; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|