| 1 |
/*============================================================================= |
| 2 |
pamtopdbimg |
| 3 |
=============================================================================== |
| 4 |
|
| 5 |
Convert Palm Pilot PDB Image format (for viewing by |
| 6 |
Pilot Image Viewer) to Netpbm image. |
| 7 |
|
| 8 |
Bryan Henderson derived this from Eric Howe's program named |
| 9 |
'imgvtopnm', in September 2010. |
| 10 |
=============================================================================*/ |
| 11 |
/* |
| 12 |
* Copyright (C) 1997 Eric A. Howe |
| 13 |
* |
| 14 |
* This program is free software; you can redistribute it and/or modify |
| 15 |
* it under the terms of the GNU General Public License as published by |
| 16 |
* the Free Software Foundation; either version 2 of the License, or |
| 17 |
* (at your option) any later version. |
| 18 |
* |
| 19 |
* This program is distributed in the hope that it will be useful, |
| 20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
* GNU General Public License for more details. |
| 23 |
* |
| 24 |
* You should have received a copy of the GNU General Public License |
| 25 |
* along with this program; if not, write to the Free Software |
| 26 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 27 |
* |
| 28 |
* Authors: Eric A. Howe ([email protected]) |
| 29 |
* Bryan Henderson |
| 30 |
*/ |
| 31 |
#include <stdlib.h> |
| 32 |
#include <assert.h> |
| 33 |
|
| 34 |
#include "pm_c_util.h" |
| 35 |
#include "mallocvar.h" |
| 36 |
#include "nstring.h" |
| 37 |
#include "shhopt.h" |
| 38 |
#include "pam.h" |
| 39 |
|
| 40 |
#include "ipdb.h" |
| 41 |
|
| 42 |
|
| 43 |
struct cmdlineInfo { |
| 44 |
/* All the information the user supplied in the command line, |
| 45 |
in a form easy for the program to use. |
| 46 |
*/ |
| 47 |
const char * inputFileName; /* '-' if stdin */ |
| 48 |
const char * notefile; /* NULL if not specified */ |
| 49 |
unsigned int verbose; |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static void |
| 55 |
parseCommandLine(int argc, const char ** argv, |
| 56 |
struct cmdlineInfo * const cmdlineP) { |
| 57 |
/*---------------------------------------------------------------------------- |
| 58 |
parse program command line described in Unix standard form by argc |
| 59 |
and argv. Return the information in the options as *cmdlineP. |
| 60 |
|
| 61 |
If command line is internally inconsistent (invalid options, etc.), |
| 62 |
issue error message to stderr and abort program. |
| 63 |
|
| 64 |
Note that the strings we return are stored in the storage that |
| 65 |
was passed to us as the argv array. We also trash *argv. |
| 66 |
-----------------------------------------------------------------------------*/ |
| 67 |
optEntry * option_def; /* Used by OPTENT3 */ |
| 68 |
optStruct3 opt; |
| 69 |
|
| 70 |
unsigned int option_def_index; |
| 71 |
|
| 72 |
unsigned int notefileSpec; |
| 73 |
|
| 74 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 75 |
|
| 76 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 77 |
OPTENT3(0, "notefile", OPT_STRING, &cmdlineP->notefile, |
| 78 |
¬efileSpec, 0); |
| 79 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, |
| 80 |
&cmdlineP->verbose, 0); |
| 81 |
|
| 82 |
opt.opt_table = option_def; |
| 83 |
opt.short_allowed = false; /* We have no short (old-fashioned) options */ |
| 84 |
opt.allowNegNum = false; /* We have no 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 (!notefileSpec) |
| 90 |
cmdlineP->notefile = NULL; |
| 91 |
|
| 92 |
if (argc-1 < 1) |
| 93 |
cmdlineP->inputFileName = "-"; |
| 94 |
else if (argc-1 == 1) |
| 95 |
cmdlineP->inputFileName = argv[1]; |
| 96 |
else |
| 97 |
pm_error("Program takes at most one argument: input file name"); |
| 98 |
|
| 99 |
free(option_def); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
#define getg16pixel(b,o) (((b) >> (4 - 4*(o))) & 0x0f) |
| 105 |
#define getgpixel(b,o) (((b) >> (6 - 2*(o))) & 0x03) |
| 106 |
#define getmpixel(b,o) (((b) >> (7 - (o))) & 0x01) |
| 107 |
|
| 108 |
|
| 109 |
static void |
| 110 |
abortShort() { |
| 111 |
pm_error("Invalid image. Compression algorithm runs out of " |
| 112 |
"compressed data before generating the expected " |
| 113 |
"amount of image data"); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
static void |
| 119 |
abortOverrun() { |
| 120 |
pm_error("Invalid image. Compression algorithm finds the end of " |
| 121 |
"the image in the middle of a run"); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
static void |
| 127 |
decompress(const uint8_t * const compressed, |
| 128 |
size_t const compressedSize, |
| 129 |
size_t const imageSize, |
| 130 |
uint8_t ** const uncompressedP) { |
| 131 |
/*---------------------------------------------------------------------------- |
| 132 |
Decompress the data 'compressed', which is 'compressedSize' bytes long. |
| 133 |
Return the decompressed data in newly malloced storage as |
| 134 |
*decompressedP. Decompression should yield exactly 'imageSize' bytes. |
| 135 |
-----------------------------------------------------------------------------*/ |
| 136 |
/* |
| 137 |
* The compression scheme used is a simple RLE; the control codes, |
| 138 |
* CODE, are one byte and have the following meanings: |
| 139 |
* |
| 140 |
* CODE > 0x80 Insert (CODE + 1 - 0x80) copies of the next byte. |
| 141 |
* CODE <= 0x80 Insert the next (CODE + 1) literal bytes. |
| 142 |
* |
| 143 |
* Compressed pieces can (and do) cross row boundaries. |
| 144 |
*/ |
| 145 |
uint8_t * uncompressed; |
| 146 |
|
| 147 |
MALLOCARRAY(uncompressed, imageSize); |
| 148 |
|
| 149 |
if (uncompressed) { |
| 150 |
const uint8_t * inP; |
| 151 |
uint8_t * outP; |
| 152 |
size_t bytesLeft; |
| 153 |
|
| 154 |
for (bytesLeft = imageSize, |
| 155 |
inP = &compressed[0], outP = &uncompressed[0]; |
| 156 |
bytesLeft > 0; |
| 157 |
) { |
| 158 |
|
| 159 |
int got, put; |
| 160 |
|
| 161 |
if (inP > compressed + compressedSize) |
| 162 |
abortShort(); |
| 163 |
|
| 164 |
if (*inP > 0x80) { |
| 165 |
put = *inP++ + 1 - 0x80; |
| 166 |
if (outP + put > uncompressed + imageSize) |
| 167 |
abortOverrun(); |
| 168 |
memset(outP, *inP, put); |
| 169 |
got = 1; |
| 170 |
} else { |
| 171 |
put = *inP++ + 1; |
| 172 |
if (inP + put > compressed + compressedSize) |
| 173 |
abortShort(); |
| 174 |
if (outP + put > uncompressed + imageSize) |
| 175 |
abortOverrun(); |
| 176 |
memcpy(outP, inP, put); |
| 177 |
got = put; |
| 178 |
} |
| 179 |
inP += got; |
| 180 |
outP += put; |
| 181 |
assert(bytesLeft >= put); |
| 182 |
bytesLeft -= put; |
| 183 |
} |
| 184 |
} |
| 185 |
*uncompressedP = uncompressed; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
#define UNKNOWN_OFFSET (uint32_t)-1 |
| 191 |
|
| 192 |
static void |
| 193 |
readCompressed(IMAGE * const imgP, |
| 194 |
uint32_t const end_offset, |
| 195 |
FILE * const fP, |
| 196 |
size_t * const dataSizeP, |
| 197 |
uint8_t ** const dataP, |
| 198 |
int * const retvalP) { |
| 199 |
/*---------------------------------------------------------------------------- |
| 200 |
Read the compressed data from file *fP (actually, if the image isn't |
| 201 |
compressed, then it's just the regular data). |
| 202 |
|
| 203 |
Return the data in newly malloced storage as *dataP, which is |
| 204 |
*dataSizeP bytes long. |
| 205 |
-----------------------------------------------------------------------------*/ |
| 206 |
int retval; |
| 207 |
uint8_t * buffer; |
| 208 |
size_t dataSize; |
| 209 |
|
| 210 |
dataSize = 0; /* initial value */ |
| 211 |
|
| 212 |
if (end_offset == UNKNOWN_OFFSET) { |
| 213 |
/* |
| 214 |
* Read sufficient amount for worst-case compressed image, |
| 215 |
* or until EOF. Some of them have an extra zero byte |
| 216 |
* dangling off the end. I originally thought this was |
| 217 |
* an empty note record (even though there was no record |
| 218 |
* header for it); however, the release notes for Image |
| 219 |
* Compression Manager 1.1 on http://www.pilotgear.com |
| 220 |
* note this extra byte as a bug in Image Compression |
| 221 |
* Manager 1.0 which 1.1 fixes. We'll just blindly read |
| 222 |
* this extra byte and ignore it by paying attention to |
| 223 |
* the image dimensions. |
| 224 |
*/ |
| 225 |
size_t const maxCompressedSizeWithBloat = ipdb_imgSize(imgP) * 2; |
| 226 |
/* |
| 227 |
* Provide a buffer large enough for the worst case. |
| 228 |
* See note in lib/util/runlength.c . |
| 229 |
* We do not use pm_rlenc_allocoutbuf() because there is no |
| 230 |
* guarantee that the encoder that produced the image was |
| 231 |
* efficient. |
| 232 |
*/ |
| 233 |
MALLOCARRAY(buffer, maxCompressedSizeWithBloat); |
| 234 |
|
| 235 |
if (buffer == NULL) |
| 236 |
retval = ENOMEM; |
| 237 |
else { |
| 238 |
dataSize = fread(buffer, 1, maxCompressedSizeWithBloat, fP); |
| 239 |
if (dataSize <= 0) |
| 240 |
retval = EIO; |
| 241 |
else |
| 242 |
retval = 0; |
| 243 |
|
| 244 |
if (retval != 0) |
| 245 |
free(buffer); |
| 246 |
} |
| 247 |
} else { |
| 248 |
/* |
| 249 |
* Read to the indicated offset. |
| 250 |
*/ |
| 251 |
dataSize = end_offset - ftell(fP) + 1; |
| 252 |
|
| 253 |
MALLOCARRAY(buffer, dataSize); |
| 254 |
|
| 255 |
if (buffer == NULL) |
| 256 |
retval = ENOMEM; |
| 257 |
else { |
| 258 |
ssize_t rc; |
| 259 |
rc = fread(buffer, 1, dataSize, fP); |
| 260 |
if (rc != dataSize) |
| 261 |
retval = EIO; |
| 262 |
else |
| 263 |
retval = 0; |
| 264 |
|
| 265 |
if (retval != 0) |
| 266 |
free(buffer); |
| 267 |
} |
| 268 |
} |
| 269 |
*dataSizeP = dataSize; |
| 270 |
*dataP = buffer; |
| 271 |
*retvalP = retval; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
static void |
| 277 |
imageReadHeader(FILE * const fileP, |
| 278 |
IMAGE * const imgP, |
| 279 |
bool const dump) { |
| 280 |
|
| 281 |
fread(&imgP->name, 1, 32, fileP); |
| 282 |
pm_readcharu(fileP, &imgP->version); |
| 283 |
pm_readcharu(fileP, &imgP->type); |
| 284 |
fread(&imgP->reserved1, 1, 4, fileP); |
| 285 |
fread(&imgP->note, 1, 4, fileP); |
| 286 |
pm_readbigshortu(fileP, &imgP->x_last); |
| 287 |
pm_readbigshortu(fileP, &imgP->y_last); |
| 288 |
fread(&imgP->reserved2, 1, 4, fileP); |
| 289 |
pm_readbigshortu(fileP, &imgP->x_anchor); |
| 290 |
pm_readbigshortu(fileP, &imgP->y_anchor); |
| 291 |
pm_readbigshortu(fileP, &imgP->width); |
| 292 |
pm_readbigshortu(fileP, &imgP->height); |
| 293 |
|
| 294 |
if (dump) { |
| 295 |
pm_message("PDB IMAGE header:"); |
| 296 |
pm_message(" Name: '%.*s'", (int)sizeof(imgP->name), imgP->name); |
| 297 |
pm_message(" Version: %02x", imgP->version); |
| 298 |
pm_message(" Type: %s", ipdb_typeName(imgP->type)); |
| 299 |
pm_message(" Note: %02x %02x %02x %02x", |
| 300 |
imgP->note[0], imgP->note[1], imgP->note[2], imgP->note[3]); |
| 301 |
pm_message(" X_last: %u", imgP->x_last); |
| 302 |
pm_message(" Y_last: %u", imgP->y_last); |
| 303 |
pm_message(" X_anchor: %u", imgP->x_anchor); |
| 304 |
pm_message(" Y_anchor: %u", imgP->y_anchor); |
| 305 |
pm_message(" Width: %u", imgP->width); |
| 306 |
pm_message(" Height: %u", imgP->height); |
| 307 |
pm_message("Pixels per byte: %u", ipdb_imgPpb(imgP)); |
| 308 |
pm_message("Image size: %lu bytes", |
| 309 |
(unsigned long)ipdb_imgSize(imgP)); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
static int |
| 316 |
imageReadData(FILE * const fileP, |
| 317 |
IMAGE * const imgP, |
| 318 |
uint32_t const end_offset) { |
| 319 |
|
| 320 |
size_t const imageSize = ipdb_imgSize(imgP); |
| 321 |
|
| 322 |
int retval; |
| 323 |
size_t dataSize; |
| 324 |
uint8_t * buffer; |
| 325 |
|
| 326 |
readCompressed(imgP, end_offset, fileP, &dataSize, &buffer, &retval); |
| 327 |
|
| 328 |
if (retval == 0) { |
| 329 |
/* |
| 330 |
* Compressed data can cross row boundaries so we decompress |
| 331 |
* the data here to avoid messiness in the row access functions. |
| 332 |
*/ |
| 333 |
if (dataSize < imageSize || imgP->version == 1) { |
| 334 |
if (imgP->version == 0) |
| 335 |
pm_message("Image header says raster data is uncompressed. " |
| 336 |
"Encountered only %u instead of the " |
| 337 |
"required %u bytes. Assuming compressed mode.", |
| 338 |
(unsigned)dataSize, (unsigned)imageSize); |
| 339 |
decompress(buffer, dataSize, imageSize, &imgP->data); |
| 340 |
if (imgP->data == NULL) |
| 341 |
retval = ENOMEM; |
| 342 |
else |
| 343 |
imgP->compressed = true; |
| 344 |
free(buffer); |
| 345 |
} else { |
| 346 |
if (dataSize > imageSize) |
| 347 |
pm_message("Image header says raster data is uncompressed. " |
| 348 |
"Encountered %u instead of the required %u bytes. " |
| 349 |
"Assuming uncompressed mode.", |
| 350 |
(unsigned)dataSize, (unsigned)imageSize); |
| 351 |
imgP->compressed = false; |
| 352 |
imgP->data = buffer; |
| 353 |
/* Storage at 'buffer' now belongs to *imgP */ |
| 354 |
} |
| 355 |
} |
| 356 |
return retval; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
static int |
| 362 |
imageRead(IMAGE * const imgP, |
| 363 |
uint32_t const end_offset, |
| 364 |
FILE * const fileP, |
| 365 |
bool const verbose) { |
| 366 |
|
| 367 |
if (imgP) { |
| 368 |
imgP->r->offset = (uint32_t)ftell(fileP); |
| 369 |
|
| 370 |
imageReadHeader(fileP, imgP, verbose); |
| 371 |
|
| 372 |
imageReadData(fileP, imgP, end_offset); |
| 373 |
} |
| 374 |
return 0; |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
static int |
| 380 |
textRead(TEXT * const textP, |
| 381 |
FILE * const fileP) { |
| 382 |
|
| 383 |
int retval; |
| 384 |
char * s; |
| 385 |
char buf[128]; |
| 386 |
int used, alloced, len; |
| 387 |
|
| 388 |
if (textP == NULL) |
| 389 |
return 0; |
| 390 |
|
| 391 |
textP->r->offset = (uint32_t)ftell(fileP); |
| 392 |
|
| 393 |
/* |
| 394 |
* What a pain in the ass! Why the hell isn't there a length |
| 395 |
* attached to the text record? I suppose the designer wasn't |
| 396 |
* concerned about non-seekable (i.e. pipes) input streams. |
| 397 |
* Perhaps I'm being a little harsh, the lack of a length probably |
| 398 |
* isn't much of an issue on the Pilot. |
| 399 |
*/ |
| 400 |
used = 0; |
| 401 |
alloced = 0; |
| 402 |
s = NULL; |
| 403 |
retval = 0; /* initial value */ |
| 404 |
while ((len = fread(buf, 1, sizeof(buf), fileP)) != 0 && retval == 0) { |
| 405 |
if (buf[len - 1] == '\0') |
| 406 |
--len; |
| 407 |
if (used + len > alloced) { |
| 408 |
alloced += 2 * sizeof(buf); |
| 409 |
REALLOCARRAY(s, alloced); |
| 410 |
|
| 411 |
if (s == NULL) |
| 412 |
retval = ENOMEM; |
| 413 |
} |
| 414 |
if (retval == 0) { |
| 415 |
memcpy(s + used, buf, len); |
| 416 |
used += len; |
| 417 |
} |
| 418 |
} |
| 419 |
if (retval == 0) { |
| 420 |
textP->data = calloc(1, used + 1); |
| 421 |
if (textP->data == NULL) |
| 422 |
retval = ENOMEM; |
| 423 |
else |
| 424 |
memcpy(textP->data, s, used); |
| 425 |
} |
| 426 |
if (s) |
| 427 |
free(s); |
| 428 |
|
| 429 |
return retval; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
static int |
| 435 |
pdbheadRead(PDBHEAD * const pdbHeadP, |
| 436 |
FILE * const fileP) { |
| 437 |
|
| 438 |
int retval; |
| 439 |
|
| 440 |
fread(pdbHeadP->name, 1, 32, fileP); |
| 441 |
pm_readbigshortu(fileP, &pdbHeadP->flags); |
| 442 |
pm_readbigshortu(fileP, &pdbHeadP->version); |
| 443 |
pm_readbiglongu2(fileP, &pdbHeadP->ctime); |
| 444 |
pm_readbiglongu2(fileP, &pdbHeadP->mtime); |
| 445 |
pm_readbiglongu2(fileP, &pdbHeadP->btime); |
| 446 |
pm_readbiglongu2(fileP, &pdbHeadP->mod_num); |
| 447 |
pm_readbiglongu2(fileP, &pdbHeadP->app_info); |
| 448 |
pm_readbiglongu2(fileP, &pdbHeadP->sort_info); |
| 449 |
fread(pdbHeadP->type, 1, 4, fileP); |
| 450 |
fread(pdbHeadP->id, 1, 4, fileP); |
| 451 |
pm_readbiglongu2(fileP, &pdbHeadP->uniq_seed); |
| 452 |
pm_readbiglongu2(fileP, &pdbHeadP->next_rec); |
| 453 |
pm_readbigshortu(fileP, &pdbHeadP->num_recs); |
| 454 |
|
| 455 |
if (!memeq(pdbHeadP->type, IPDB_vIMG, 4) |
| 456 |
|| !memeq(pdbHeadP->id, IPDB_View, 4)) |
| 457 |
retval = E_NOTIMAGE; |
| 458 |
else |
| 459 |
retval = 0; |
| 460 |
|
| 461 |
return retval; |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
static int |
| 467 |
rechdrRead(RECHDR * const rechdrP, |
| 468 |
FILE * const fileP) { |
| 469 |
|
| 470 |
int retval; |
| 471 |
off_t len; |
| 472 |
|
| 473 |
pm_readbiglongu2(fileP, &rechdrP->offset); |
| 474 |
|
| 475 |
len = (off_t)rechdrP->offset - ftell(fileP); |
| 476 |
switch(len) { |
| 477 |
case 4: |
| 478 |
case 12: |
| 479 |
/* |
| 480 |
* Version zero (eight bytes of record header) or version |
| 481 |
* two with a note (two chunks of eight record header bytes). |
| 482 |
*/ |
| 483 |
fread(&rechdrP->unknown[0], 1, 3, fileP); |
| 484 |
fread(&rechdrP->rec_type, 1, 1, fileP); |
| 485 |
rechdrP->n_extra = 0; |
| 486 |
rechdrP->extra = NULL; |
| 487 |
retval = 0; |
| 488 |
break; |
| 489 |
case 6: |
| 490 |
/* |
| 491 |
* Version one (ten bytes of record header). |
| 492 |
*/ |
| 493 |
fread(&rechdrP->unknown[0], 1, 3, fileP); |
| 494 |
fread(&rechdrP->rec_type, 1, 1, fileP); |
| 495 |
rechdrP->n_extra = 2; |
| 496 |
MALLOCARRAY(rechdrP->extra, rechdrP->n_extra); |
| 497 |
if (rechdrP->extra == NULL) |
| 498 |
retval = ENOMEM; |
| 499 |
else { |
| 500 |
fread(rechdrP->extra, 1, rechdrP->n_extra, fileP); |
| 501 |
retval = 0; |
| 502 |
} |
| 503 |
break; |
| 504 |
default: |
| 505 |
/* |
| 506 |
* hmmm.... I'll assume this is the record header |
| 507 |
* for a text record. |
| 508 |
*/ |
| 509 |
fread(&rechdrP->unknown[0], 1, 3, fileP); |
| 510 |
fread(&rechdrP->rec_type, 1, 1, fileP); |
| 511 |
rechdrP->n_extra = 0; |
| 512 |
rechdrP->extra = NULL; |
| 513 |
retval = 0; |
| 514 |
break; |
| 515 |
} |
| 516 |
if (retval == 0) { |
| 517 |
if ((rechdrP->rec_type != IMG_REC && rechdrP->rec_type != TEXT_REC) |
| 518 |
|| !memeq(rechdrP->unknown, IPDB_MYST, 3)) |
| 519 |
retval = E_NOTRECHDR; |
| 520 |
} |
| 521 |
return retval; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
static int |
| 527 |
ipdbRead(IPDB * const pdbP, |
| 528 |
FILE * const fileP, |
| 529 |
bool const verbose) { |
| 530 |
|
| 531 |
int retval; |
| 532 |
|
| 533 |
int status; |
| 534 |
|
| 535 |
status = pdbheadRead(pdbP->p, fileP); |
| 536 |
|
| 537 |
if (status != 0) |
| 538 |
retval = status; |
| 539 |
else { |
| 540 |
pdbP->i = ipdb_imageCreate(pdbP->p->name, IMG_GRAY, 0, 0); |
| 541 |
if (pdbP->i == NULL) |
| 542 |
retval = ENOMEM; |
| 543 |
else { |
| 544 |
int status; |
| 545 |
status = rechdrRead(pdbP->i->r, fileP); |
| 546 |
if (status != 0) |
| 547 |
retval = status; |
| 548 |
else { |
| 549 |
if (pdbP->p->num_recs > 1) { |
| 550 |
pdbP->t = ipdb_textAlloc(); |
| 551 |
if (pdbP->t == NULL) |
| 552 |
retval = ENOMEM; |
| 553 |
else { |
| 554 |
int status; |
| 555 |
status = rechdrRead(pdbP->t->r, fileP); |
| 556 |
if (status != 0) |
| 557 |
retval = status; |
| 558 |
else |
| 559 |
retval = 0; |
| 560 |
} |
| 561 |
} else |
| 562 |
retval = 0; |
| 563 |
|
| 564 |
if (retval == 0) { |
| 565 |
uint32_t const offset = |
| 566 |
pdbP->t == NULL ? |
| 567 |
UNKNOWN_OFFSET : pdbP->t->r->offset - 1; |
| 568 |
|
| 569 |
int status; |
| 570 |
|
| 571 |
status = imageRead(pdbP->i, offset, fileP, verbose); |
| 572 |
if (status != 0) |
| 573 |
retval = status; |
| 574 |
else { |
| 575 |
if (pdbP->t != NULL) { |
| 576 |
int status; |
| 577 |
|
| 578 |
status = textRead(pdbP->t, fileP); |
| 579 |
if (status != 0) |
| 580 |
retval = status; |
| 581 |
} |
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
return retval; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
static void |
| 593 |
g16unpack(const uint8_t * const p, |
| 594 |
uint8_t * const g, |
| 595 |
int const w) { |
| 596 |
|
| 597 |
static const uint8_t pal[] = |
| 598 |
{0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, |
| 599 |
0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00}; |
| 600 |
const uint8_t * seg; |
| 601 |
unsigned int i; |
| 602 |
|
| 603 |
for (i = 0, seg = p; i < w; i += 2, ++seg) { |
| 604 |
g[i + 0] = pal[getg16pixel(*seg, 0)]; |
| 605 |
g[i + 1] = pal[getg16pixel(*seg, 1)]; |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
static void |
| 612 |
gunpack(const uint8_t * const p, |
| 613 |
uint8_t * const g, |
| 614 |
int const w) { |
| 615 |
|
| 616 |
static const uint8_t pal[] = {0xff, 0xaa, 0x55, 0x00}; |
| 617 |
const uint8_t * seg; |
| 618 |
unsigned int i; |
| 619 |
|
| 620 |
for (i = 0, seg = p; i < w; i += 4, ++seg) { |
| 621 |
g[i + 0] = pal[getgpixel(*seg, 0)]; |
| 622 |
g[i + 1] = pal[getgpixel(*seg, 1)]; |
| 623 |
g[i + 2] = pal[getgpixel(*seg, 2)]; |
| 624 |
g[i + 3] = pal[getgpixel(*seg, 3)]; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
static void |
| 631 |
munpack(const uint8_t * const p, |
| 632 |
uint8_t * const b, |
| 633 |
int const w) { |
| 634 |
|
| 635 |
static const uint8_t pal[] = {PAM_BW_WHITE, PAM_BLACK}; |
| 636 |
const uint8_t * seg; |
| 637 |
unsigned int i; |
| 638 |
|
| 639 |
for (i = 0, seg = p; i < w; i += 8, ++seg) { |
| 640 |
b[i + 0] = pal[getmpixel(*seg, 0)]; |
| 641 |
b[i + 1] = pal[getmpixel(*seg, 1)]; |
| 642 |
b[i + 2] = pal[getmpixel(*seg, 2)]; |
| 643 |
b[i + 3] = pal[getmpixel(*seg, 3)]; |
| 644 |
b[i + 4] = pal[getmpixel(*seg, 4)]; |
| 645 |
b[i + 5] = pal[getmpixel(*seg, 5)]; |
| 646 |
b[i + 6] = pal[getmpixel(*seg, 6)]; |
| 647 |
b[i + 7] = pal[getmpixel(*seg, 7)]; |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
static void |
| 654 |
g16row(IPDB * const pdbP, |
| 655 |
unsigned int const row, |
| 656 |
uint8_t * const buffer) { |
| 657 |
|
| 658 |
g16unpack(ipdb_imgRow(pdbP->i, row), buffer, ipdb_width(pdbP)); |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
static void |
| 664 |
grow(IPDB * const pdbP, |
| 665 |
unsigned int const row, |
| 666 |
uint8_t * const buffer) { |
| 667 |
|
| 668 |
gunpack(ipdb_imgRow(pdbP->i, row), buffer, ipdb_width(pdbP)); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
static void |
| 674 |
mrow(IPDB * const pdbP, |
| 675 |
unsigned int const row, |
| 676 |
uint8_t * const buffer) { |
| 677 |
|
| 678 |
munpack(ipdb_imgRow(pdbP->i, row), buffer, ipdb_width(pdbP)); |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
static void |
| 684 |
writeImgPam(IPDB * const pdbP, |
| 685 |
FILE * const ofP) { |
| 686 |
|
| 687 |
struct pam pam; |
| 688 |
tuple * tupleRow; |
| 689 |
unsigned int row; |
| 690 |
uint8_t * imgRow; |
| 691 |
|
| 692 |
MALLOCARRAY(imgRow, ipdb_width(pdbP)); |
| 693 |
|
| 694 |
pam.size = sizeof(pam); |
| 695 |
pam.len = PAM_STRUCT_SIZE(tuple_type); |
| 696 |
pam.file = ofP; |
| 697 |
pam.plainformat = 0; |
| 698 |
pam.width = ipdb_width(pdbP); |
| 699 |
pam.height = ipdb_height(pdbP); |
| 700 |
pam.depth = 1; |
| 701 |
pam.maxval = ipdb_type(pdbP) == IMG_MONO ? 1 : 255; |
| 702 |
pam.bytes_per_sample = pnm_bytespersample(pam.maxval); |
| 703 |
pam.format = PAM_FORMAT; |
| 704 |
strcpy(pam.tuple_type, |
| 705 |
ipdb_type(pdbP) == IMG_MONO ? |
| 706 |
PAM_PBM_TUPLETYPE : PAM_PGM_TUPLETYPE); |
| 707 |
|
| 708 |
pnm_writepaminit(&pam); |
| 709 |
|
| 710 |
tupleRow = pnm_allocpamrow(&pam); |
| 711 |
|
| 712 |
for (row = 0; row < pam.height; ++row) { |
| 713 |
unsigned int col; |
| 714 |
|
| 715 |
|
| 716 |
if (ipdb_type(pdbP) == IMG_MONO) |
| 717 |
mrow(pdbP, row, imgRow); |
| 718 |
else if (ipdb_type(pdbP) == IMG_GRAY) |
| 719 |
grow(pdbP, row, imgRow); |
| 720 |
else |
| 721 |
g16row(pdbP, row, imgRow); |
| 722 |
|
| 723 |
for (col = 0; col < pam.width; ++col) |
| 724 |
tupleRow[col][0] = imgRow[col]; |
| 725 |
|
| 726 |
pnm_writepamrow(&pam, tupleRow); |
| 727 |
} |
| 728 |
pnm_freepamrow(tupleRow); |
| 729 |
|
| 730 |
free(imgRow); |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
static void |
| 736 |
writeText(IPDB * const pdbP, |
| 737 |
const char * const name) { |
| 738 |
|
| 739 |
const char * const note = ipdb_text(pdbP); |
| 740 |
|
| 741 |
FILE * fP; |
| 742 |
|
| 743 |
if (name == NULL || note == NULL) { |
| 744 |
} else { |
| 745 |
fP = pm_openw(name); |
| 746 |
if (fP == NULL) |
| 747 |
pm_error("Could not open note file '%s' for output", name); |
| 748 |
|
| 749 |
fprintf(fP, "%s\n", note); |
| 750 |
|
| 751 |
pm_close(fP); |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
int |
| 758 |
main(int argc, const char ** argv) { |
| 759 |
|
| 760 |
struct cmdlineInfo cmdline; |
| 761 |
FILE * ifP; |
| 762 |
IPDB * pdbP; |
| 763 |
int status; |
| 764 |
|
| 765 |
pm_proginit(&argc, argv); |
| 766 |
|
| 767 |
parseCommandLine(argc, argv, &cmdline); |
| 768 |
|
| 769 |
ifP = pm_openr(cmdline.inputFileName); |
| 770 |
|
| 771 |
pdbP = ipdb_alloc(); |
| 772 |
if (pdbP == NULL) |
| 773 |
pm_error("Could not allocate IPDB structure."); |
| 774 |
|
| 775 |
status = ipdbRead(pdbP, ifP, cmdline.verbose); |
| 776 |
if (status != 0) |
| 777 |
pm_error("Image header read error: %s.", ipdb_err(status)); |
| 778 |
|
| 779 |
writeImgPam(pdbP, stdout); |
| 780 |
|
| 781 |
writeText(pdbP, cmdline.notefile); |
| 782 |
|
| 783 |
ipdb_free(pdbP); |
| 784 |
|
| 785 |
pm_close(ifP); |
| 786 |
|
| 787 |
return EXIT_SUCCESS; |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
|