| 1 |
/*============================================================================= |
| 2 |
pamtopdbimg |
| 3 |
=============================================================================== |
| 4 |
|
| 5 |
Convert Netpbm image to Palm Pilot PDB Image format (for viewing by |
| 6 |
Pilot Image Viewer). |
| 7 |
|
| 8 |
Bryan Henderson derived this from Eric Howe's programs named |
| 9 |
'pgmtoimgv' and 'pbmtoimgv' 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, September 2010. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <stdlib.h> |
| 33 |
#include <assert.h> |
| 34 |
#include <string.h> |
| 35 |
#include <time.h> |
| 36 |
#include <sys/stat.h> |
| 37 |
|
| 38 |
#include "pm_c_util.h" |
| 39 |
#include "mallocvar.h" |
| 40 |
#include "nstring.h" |
| 41 |
#include "shhopt.h" |
| 42 |
#include "runlength.h" |
| 43 |
#include "pam.h" |
| 44 |
|
| 45 |
#include "ipdb.h" |
| 46 |
|
| 47 |
enum CompMode {COMPRESSED, MAYBE, UNCOMPRESSED}; |
| 48 |
|
| 49 |
struct CmdlineInfo { |
| 50 |
/* All the information the user supplied in the command line, |
| 51 |
in a form easy for the program to use. |
| 52 |
*/ |
| 53 |
const char * inputFileName; /* '-' if stdin */ |
| 54 |
const char * title; |
| 55 |
const char * notefile; /* NULL if not specified */ |
| 56 |
enum CompMode compMode; |
| 57 |
unsigned int depth4; |
| 58 |
unsigned int fixedtime; |
| 59 |
}; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
static void |
| 64 |
parseCommandLine(int argc, const char ** argv, |
| 65 |
struct CmdlineInfo * const cmdlineP) { |
| 66 |
/*---------------------------------------------------------------------------- |
| 67 |
parse program command line described in Unix standard form by argc |
| 68 |
and argv. Return the information in the options as *cmdlineP. |
| 69 |
|
| 70 |
If command line is internally inconsistent (invalid options, etc.), |
| 71 |
issue error message to stderr and abort program. |
| 72 |
|
| 73 |
Note that the strings we return are stored in the storage that |
| 74 |
was passed to us as the argv array. We also trash *argv. |
| 75 |
-----------------------------------------------------------------------------*/ |
| 76 |
optEntry * option_def; /* Used by OPTENT3 */ |
| 77 |
optStruct3 opt; |
| 78 |
|
| 79 |
unsigned int option_def_index; |
| 80 |
|
| 81 |
unsigned int titleSpec, notefileSpec; |
| 82 |
unsigned int compressed, maybeCompressed, uncompressed; |
| 83 |
|
| 84 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 85 |
|
| 86 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 87 |
OPTENT3(0, "title", OPT_STRING, &cmdlineP->title, |
| 88 |
&titleSpec, 0); |
| 89 |
OPTENT3(0, "notefile", OPT_STRING, &cmdlineP->notefile, |
| 90 |
¬efileSpec, 0); |
| 91 |
OPTENT3(0, "compressed", OPT_FLAG, NULL, |
| 92 |
&compressed, 0); |
| 93 |
OPTENT3(0, "maybecompressed", OPT_FLAG, NULL, |
| 94 |
&maybeCompressed, 0); |
| 95 |
OPTENT3(0, "uncompressed", OPT_FLAG, NULL, |
| 96 |
&uncompressed, 0); |
| 97 |
OPTENT3(0, "4depth", OPT_FLAG, NULL, |
| 98 |
&cmdlineP->depth4, 0); |
| 99 |
OPTENT3(0, "fixedtime", OPT_FLAG, NULL, |
| 100 |
&cmdlineP->fixedtime, 0); |
| 101 |
|
| 102 |
opt.opt_table = option_def; |
| 103 |
opt.short_allowed = false; /* We have no short (old-fashioned) options */ |
| 104 |
opt.allowNegNum = false; /* We have no parms that are negative numbers */ |
| 105 |
|
| 106 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 107 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 108 |
|
| 109 |
|
| 110 |
if (!titleSpec) |
| 111 |
cmdlineP->title = "unnamed"; |
| 112 |
|
| 113 |
if (!notefileSpec) |
| 114 |
cmdlineP->notefile = NULL; |
| 115 |
|
| 116 |
if (compressed + uncompressed + maybeCompressed > 1) |
| 117 |
pm_error("You may specify only one of -compressed, -uncompressed, " |
| 118 |
"-maybecompressed"); |
| 119 |
if (compressed) |
| 120 |
cmdlineP->compMode = COMPRESSED; |
| 121 |
else if (uncompressed) |
| 122 |
cmdlineP->compMode = UNCOMPRESSED; |
| 123 |
else if (maybeCompressed) |
| 124 |
cmdlineP->compMode = MAYBE; |
| 125 |
else |
| 126 |
cmdlineP->compMode = MAYBE; |
| 127 |
|
| 128 |
if (argc-1 < 1) |
| 129 |
cmdlineP->inputFileName = "-"; |
| 130 |
else if (argc-1 == 1) |
| 131 |
cmdlineP->inputFileName = argv[1]; |
| 132 |
else |
| 133 |
pm_error("Program takes at most one argument: input file name"); |
| 134 |
|
| 135 |
free(option_def); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
/* |
| 141 |
* Pixel setting macros. |
| 142 |
*/ |
| 143 |
#define setg16pixel(b,v,o) ((b) |= ((v) << (4 - 4*(o)))) |
| 144 |
#define setgpixel(b,v,o) ((b) |= ((v) << (6 - 2*(o)))) |
| 145 |
#define setmpixelblack(b,o) ((b) |= (1 << (7 - (o)))) |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
static uint32_t const unixepoch = (66*365+17)*24*3600; |
| 150 |
/* The unix epoch in Mac time (the Mac epoch is 00:00 UTC 1904.01.01). |
| 151 |
The 17 is the number of leap years. |
| 152 |
*/ |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
static void |
| 157 |
setPdbHeader(PDBHEAD * const pdbHeadP, |
| 158 |
const char * const name, |
| 159 |
bool const wantFixedTime) { |
| 160 |
|
| 161 |
STRSCPY(pdbHeadP->name, name); |
| 162 |
|
| 163 |
{ |
| 164 |
/* |
| 165 |
* All of the Image Viewer pdb files that I've come across have |
| 166 |
* 3510939142U (1997.08.16 14:38:22 UTC) here. I don't know where |
| 167 |
* this bizarre datetime comes from but the real date works fine so |
| 168 |
* I'm using it unless user asked for a fixed time (probably just so he |
| 169 |
* gets repeatable output). |
| 170 |
*/ |
| 171 |
|
| 172 |
uint32_t const stdTime = 3510939142U; |
| 173 |
|
| 174 |
uint32_t const hdrDt = |
| 175 |
wantFixedTime ? stdTime : (uint32_t)time(NULL) + unixepoch; |
| 176 |
|
| 177 |
pdbHeadP->ctime = pdbHeadP->mtime = hdrDt; |
| 178 |
} |
| 179 |
MEMSCPY(&pdbHeadP->type, IPDB_vIMG); |
| 180 |
MEMSCPY(&pdbHeadP->id, IPDB_View); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
static int |
| 186 |
pdbheadWrite(PDBHEAD * const pdbheadP, |
| 187 |
FILE * const fileP) { |
| 188 |
|
| 189 |
fwrite(pdbheadP->name, 1, 32, fileP); |
| 190 |
pm_writebigshort(fileP, pdbheadP->flags); |
| 191 |
pm_writebigshort(fileP, pdbheadP->version); |
| 192 |
pm_writebiglong(fileP, pdbheadP->ctime); |
| 193 |
pm_writebiglong(fileP, pdbheadP->mtime); |
| 194 |
pm_writebiglong(fileP, pdbheadP->btime); |
| 195 |
pm_writebiglong(fileP, pdbheadP->mod_num); |
| 196 |
pm_writebiglong(fileP, pdbheadP->app_info); |
| 197 |
pm_writebiglong(fileP, pdbheadP->sort_info); |
| 198 |
fwrite(pdbheadP->type, 1, 4, fileP); |
| 199 |
fwrite(pdbheadP->id, 1, 4, fileP); |
| 200 |
pm_writebiglong(fileP, pdbheadP->uniq_seed); |
| 201 |
pm_writebiglong(fileP, pdbheadP->next_rec); |
| 202 |
pm_writebigshort(fileP, pdbheadP->num_recs); |
| 203 |
|
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
static int |
| 210 |
rechdrWrite(RECHDR * const rechdrP, |
| 211 |
FILE * const fileP) { |
| 212 |
|
| 213 |
if (rechdrP) { |
| 214 |
pm_writebiglong(fileP, rechdrP->offset); |
| 215 |
fwrite(rechdrP->unknown, 1, 3, fileP); |
| 216 |
fwrite(&rechdrP->rec_type, 1, 1, fileP); |
| 217 |
|
| 218 |
if (rechdrP->n_extra != 0) |
| 219 |
fwrite(rechdrP->extra, 1, rechdrP->n_extra, fileP); |
| 220 |
} |
| 221 |
return 0; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
static void |
| 227 |
imageWriteHeader(IMAGE * const imgP, |
| 228 |
FILE * const fileP) { |
| 229 |
|
| 230 |
fwrite(imgP->name, 1, 32, fileP); |
| 231 |
fwrite(&imgP->version, 1, 1, fileP); |
| 232 |
fwrite(&imgP->type, 1, 1, fileP); |
| 233 |
fwrite(imgP->reserved1, 1, 4, fileP); |
| 234 |
fwrite(imgP->note, 1, 4, fileP); |
| 235 |
pm_writebigshort(fileP, imgP->x_last); |
| 236 |
pm_writebigshort(fileP, imgP->y_last); |
| 237 |
fwrite(imgP->reserved2, 1, 4, fileP); |
| 238 |
pm_writebigshort(fileP, imgP->x_anchor); |
| 239 |
pm_writebigshort(fileP, imgP->y_anchor); |
| 240 |
pm_writebigshort(fileP, imgP->width); |
| 241 |
pm_writebigshort(fileP, imgP->height); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
static void |
| 247 |
imageWriteData(IMAGE * const imgP, |
| 248 |
const uint8_t * const data, |
| 249 |
size_t const dataSize, |
| 250 |
FILE * const fileP) { |
| 251 |
|
| 252 |
fwrite(data, 1, dataSize, fileP); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
static void |
| 258 |
imageWrite(IMAGE * const imgP, |
| 259 |
uint8_t * const data, |
| 260 |
size_t const dataSize, |
| 261 |
FILE * const fileP) { |
| 262 |
|
| 263 |
imageWriteHeader(imgP, fileP); |
| 264 |
|
| 265 |
imageWriteData(imgP, data, dataSize, fileP); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
static int |
| 271 |
textWrite(TEXT * const textP, |
| 272 |
FILE * const fileP) { |
| 273 |
|
| 274 |
if (textP) |
| 275 |
fwrite(textP->data, 1, strlen(textP->data), fileP); |
| 276 |
|
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
static void |
| 283 |
compressIfRequired(IPDB * const pdbP, |
| 284 |
int const comp, |
| 285 |
uint8_t ** const compressedDataP, |
| 286 |
size_t * const compressedSizeP) { |
| 287 |
|
| 288 |
if (comp == IPDB_NOCOMPRESS) { |
| 289 |
*compressedDataP = pdbP->i->data; |
| 290 |
*compressedSizeP = ipdb_imgSize(pdbP->i); |
| 291 |
} else { |
| 292 |
int const uncompressedSz = ipdb_imgSize(pdbP->i); |
| 293 |
|
| 294 |
unsigned char * outbuf; |
| 295 |
size_t compressedSz; |
| 296 |
|
| 297 |
pm_rlenc_allocoutbuf(&outbuf, uncompressedSz, PM_RLE_PALMPDB); |
| 298 |
|
| 299 |
pm_rlenc_compressbyte(pdbP->i->data, outbuf, PM_RLE_PALMPDB, |
| 300 |
uncompressedSz, &compressedSz); |
| 301 |
if (comp == IPDB_COMPMAYBE && compressedSz >= uncompressedSz) { |
| 302 |
/* Return the uncompressed data */ |
| 303 |
free(outbuf); |
| 304 |
*compressedDataP = pdbP->i->data; |
| 305 |
*compressedSizeP = uncompressedSz; |
| 306 |
} else { |
| 307 |
pdbP->i->compressed = TRUE; |
| 308 |
if (pdbP->i->type == IMG_GRAY16) |
| 309 |
pdbP->i->version = 9; |
| 310 |
else |
| 311 |
pdbP->i->version = 1; |
| 312 |
if (pdbP->t != NULL) |
| 313 |
pdbP->t->r->offset -= uncompressedSz - compressedSz; |
| 314 |
*compressedDataP = outbuf; |
| 315 |
*compressedSizeP = compressedSz; |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
static void |
| 323 |
ipdbWrite(IPDB * const pdbP, |
| 324 |
int const comp, |
| 325 |
FILE * const fileP) { |
| 326 |
|
| 327 |
RECHDR * const trP = pdbP->t == NULL ? NULL : pdbP->t->r; |
| 328 |
RECHDR * const irP = pdbP->i->r; |
| 329 |
|
| 330 |
int rc; |
| 331 |
uint8_t * compressedData; |
| 332 |
/* This is the image raster, compressed as required. |
| 333 |
(I.e. if it doesn't have to be compressed, it isn't). |
| 334 |
*/ |
| 335 |
size_t compressedSize; |
| 336 |
|
| 337 |
assert(pdbP->i); |
| 338 |
|
| 339 |
compressIfRequired(pdbP, comp, &compressedData, &compressedSize); |
| 340 |
|
| 341 |
rc = pdbheadWrite(pdbP->p, fileP); |
| 342 |
if (rc != 0) |
| 343 |
pm_error("Failed to write PDB header. %s", ipdb_err(rc)); |
| 344 |
|
| 345 |
rc = rechdrWrite(irP, fileP); |
| 346 |
if (rc != 0) |
| 347 |
pm_error("Failed to write image record header. %s", ipdb_err(rc)); |
| 348 |
|
| 349 |
rc = rechdrWrite(trP, fileP); |
| 350 |
if (rc != 0) |
| 351 |
pm_error("Failed to write text record header. %s", ipdb_err(rc)); |
| 352 |
|
| 353 |
imageWrite(pdbP->i, compressedData, compressedSize, fileP); |
| 354 |
|
| 355 |
rc = textWrite(pdbP->t, fileP); |
| 356 |
if (rc != 0) |
| 357 |
pm_error("Failed to write text. %s", ipdb_err(rc)); |
| 358 |
|
| 359 |
/* Oh, gross. compressIfRequired() might have returned a pointer to |
| 360 |
storage that was already allocated, or it might have returned a |
| 361 |
pointer to newly malloc'ed storage. In the latter case, we have |
| 362 |
to free the storage. |
| 363 |
*/ |
| 364 |
if (compressedData != pdbP->i->data) |
| 365 |
free(compressedData); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
static void |
| 371 |
g16pack(tuple * const tupleRow, |
| 372 |
struct pam * const pamP, |
| 373 |
uint8_t * const outData, |
| 374 |
unsigned int const paddedWidth) { |
| 375 |
/*---------------------------------------------------------------------------- |
| 376 |
Pack a row of 16-level graysacle pixels 'tupleRow', described by *pamP into |
| 377 |
'outData', padding it to 'paddedWidth' with white. |
| 378 |
|
| 379 |
We pack 2 input pixels into one output byte. |
| 380 |
-----------------------------------------------------------------------------*/ |
| 381 |
unsigned int col; |
| 382 |
unsigned int off; |
| 383 |
uint8_t * seg; |
| 384 |
|
| 385 |
for (col = 0, off = 0, seg = &outData[0]; col < paddedWidth; ++col) { |
| 386 |
if (col < pamP->width) |
| 387 |
setg16pixel(*seg, 15 - tupleRow[col][0] * 15 / pamP->maxval, off); |
| 388 |
else |
| 389 |
/* Pad on the right with white */ |
| 390 |
setgpixel(*seg, 0, off); |
| 391 |
|
| 392 |
if (++off == 2) { |
| 393 |
++seg; |
| 394 |
off = 0; |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
static void |
| 402 |
gpack(tuple * const tupleRow, |
| 403 |
struct pam * const pamP, |
| 404 |
uint8_t * const outData, |
| 405 |
unsigned int const paddedWidth) { |
| 406 |
/*---------------------------------------------------------------------------- |
| 407 |
Pack a row of 4-level graysacle pixels 'tupleRow', described by *pamP into |
| 408 |
'outData', padding it to 'paddedWidth' with white. |
| 409 |
|
| 410 |
We pack 4 input pixels into one output byte. |
| 411 |
-----------------------------------------------------------------------------*/ |
| 412 |
unsigned int col; |
| 413 |
unsigned int off; |
| 414 |
uint8_t * seg; |
| 415 |
|
| 416 |
for (col = 0, off = 0, seg = &outData[0]; col < paddedWidth; ++col) { |
| 417 |
if (col < pamP->width) |
| 418 |
setgpixel(*seg, 3 - tupleRow[col][0] * 3 / pamP->maxval, off); |
| 419 |
else |
| 420 |
/* Pad on the right with white */ |
| 421 |
setgpixel(*seg, 0, off); |
| 422 |
|
| 423 |
if (++off == 4) { |
| 424 |
++seg; |
| 425 |
off = 0; |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
static void |
| 433 |
mpack(tuple * const tupleRow, |
| 434 |
struct pam * const pamP, |
| 435 |
uint8_t * const outData, |
| 436 |
unsigned int const paddedWidth) { |
| 437 |
/*---------------------------------------------------------------------------- |
| 438 |
Pack a row of monochrome pixels 'tupleRow', described by *pamP into |
| 439 |
'outData', padding it to 'paddedWidth' with white. |
| 440 |
|
| 441 |
We pack 8 input pixels into one output byte. |
| 442 |
-----------------------------------------------------------------------------*/ |
| 443 |
unsigned int col; |
| 444 |
unsigned int off; |
| 445 |
uint8_t * seg; |
| 446 |
|
| 447 |
assert(paddedWidth % 8 == 0); |
| 448 |
|
| 449 |
/* Initialize row to white, then set necessary pixels black */ |
| 450 |
memset(outData, 0, paddedWidth/8); |
| 451 |
|
| 452 |
for (col = 0, off = 0, seg = &outData[0]; col < paddedWidth; ++col) { |
| 453 |
if (col < pamP->width && tupleRow[col][0] == PAM_BLACK) |
| 454 |
setmpixelblack(*seg, off); |
| 455 |
if (++off == 8) { |
| 456 |
++seg; |
| 457 |
off = 0; |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
static int |
| 465 |
adjustDimensions(unsigned int const w, |
| 466 |
unsigned int const h, |
| 467 |
unsigned int * const awP, |
| 468 |
unsigned int * const ahP) { |
| 469 |
|
| 470 |
unsigned int provW, provH; |
| 471 |
|
| 472 |
provW = w; |
| 473 |
provH = h; |
| 474 |
if (provW % 16 != 0) |
| 475 |
provW += 16 - (provW % 16); |
| 476 |
if (provW < 160) |
| 477 |
provW = 160; |
| 478 |
if (provH < 160) |
| 479 |
provH = 160; |
| 480 |
|
| 481 |
*awP = provW; |
| 482 |
*ahP = provH; |
| 483 |
|
| 484 |
return w == provW && h == provH; |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
/* |
| 490 |
* You can allocate only 64k chunks of memory on the pilot and that |
| 491 |
* supplies an image size limit. |
| 492 |
*/ |
| 493 |
#define MAX_SIZE(t) ((1 << 16)*((t) == IMG_GRAY ? 4 : 8)) |
| 494 |
|
| 495 |
static void |
| 496 |
imageInsertInit(IPDB * const pdbP, |
| 497 |
int const uw, |
| 498 |
int const uh, |
| 499 |
int const type) { |
| 500 |
|
| 501 |
char * const name = pdbP->p->name; |
| 502 |
unsigned int adjustedWidth, adjustedHeight; |
| 503 |
|
| 504 |
if (pdbP->p->num_recs != 0) |
| 505 |
pm_error("Image record already present, logic error."); |
| 506 |
else { |
| 507 |
adjustDimensions(uw, uh, &adjustedWidth, &adjustedHeight); |
| 508 |
pm_message("Output dimensions: %uw x %uh", |
| 509 |
adjustedWidth, adjustedHeight); |
| 510 |
if (adjustedWidth * adjustedHeight > MAX_SIZE(type)) |
| 511 |
pm_error("Image too large. Maximum number of pixels allowed " |
| 512 |
"for a %s image is %u", |
| 513 |
ipdb_typeName(type), MAX_SIZE(type)); |
| 514 |
else { |
| 515 |
pdbP->i = |
| 516 |
ipdb_imageCreate(name, type, adjustedWidth, adjustedHeight); |
| 517 |
if (pdbP->i == NULL) |
| 518 |
pm_message("Could not get memory for %u x %u image", |
| 519 |
adjustedWidth, adjustedHeight); |
| 520 |
else |
| 521 |
pdbP->p->num_recs = 1; |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
static void |
| 529 |
insertG16image(IPDB * const pdbP, |
| 530 |
struct pam * const pamP, |
| 531 |
tuple ** const tuples) { |
| 532 |
/*---------------------------------------------------------------------------- |
| 533 |
Insert into the PDB an image in 16-level grayscale format. |
| 534 |
|
| 535 |
The pixels of the image to insert are 'tuples', described by *pamP. |
| 536 |
Note that the image inserted may be padded up to larger dimensions. |
| 537 |
-----------------------------------------------------------------------------*/ |
| 538 |
imageInsertInit(pdbP, pamP->width, pamP->height, IMG_GRAY16); |
| 539 |
{ |
| 540 |
int const rowSize = ipdb_width(pdbP)/2; |
| 541 |
/* The size in bytes of a packed, padded row */ |
| 542 |
|
| 543 |
uint8_t * outP; |
| 544 |
unsigned int row; |
| 545 |
|
| 546 |
for (row = 0, outP = &pdbP->i->data[0]; |
| 547 |
row < pamP->height; |
| 548 |
++row, outP += rowSize) |
| 549 |
g16pack(tuples[row], pamP, outP, ipdb_width(pdbP)); |
| 550 |
|
| 551 |
/* Pad with white on the bottom */ |
| 552 |
for (; row < ipdb_height(pdbP); ++row) |
| 553 |
memset(outP, 0, rowSize); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
static void |
| 560 |
insertGimage(IPDB * const pdbP, |
| 561 |
struct pam * const pamP, |
| 562 |
tuple ** const tuples) { |
| 563 |
/*---------------------------------------------------------------------------- |
| 564 |
Insert into the PDB an image in 4-level grayscale format. |
| 565 |
|
| 566 |
The pixels of the image to insert are 'tuples', described by *pamP. |
| 567 |
Note that the image inserted may be padded up to larger dimensions. |
| 568 |
-----------------------------------------------------------------------------*/ |
| 569 |
imageInsertInit(pdbP, pamP->width, pamP->height, IMG_GRAY); |
| 570 |
{ |
| 571 |
int const rowSize = ipdb_width(pdbP)/4; |
| 572 |
/* The size in bytes of a packed, padded row */ |
| 573 |
|
| 574 |
uint8_t * outP; |
| 575 |
unsigned int row; |
| 576 |
|
| 577 |
for (row = 0, outP = &pdbP->i->data[0]; |
| 578 |
row < pamP->height; |
| 579 |
++row, outP += rowSize) |
| 580 |
gpack(tuples[row], pamP, outP, ipdb_width(pdbP)); |
| 581 |
|
| 582 |
/* Pad with white on the bottom */ |
| 583 |
for (; row < ipdb_height(pdbP); ++row) |
| 584 |
memset(outP, 0, rowSize); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
static void |
| 591 |
insertMimage(IPDB * const pdbP, |
| 592 |
struct pam * const pamP, |
| 593 |
tuple ** const tuples) { |
| 594 |
/*---------------------------------------------------------------------------- |
| 595 |
Insert into the PDB an image in monochrome format. |
| 596 |
|
| 597 |
The pixels of the image to insert are 'tuples', described by *pamP. |
| 598 |
Note that the image inserted may be padded up to larger dimensions. |
| 599 |
-----------------------------------------------------------------------------*/ |
| 600 |
imageInsertInit(pdbP, pamP->width, pamP->height, IMG_MONO); |
| 601 |
{ |
| 602 |
int const rowSize = ipdb_width(pdbP)/8; |
| 603 |
/* The size in bytes of a packed, padded row */ |
| 604 |
|
| 605 |
uint8_t * outP; |
| 606 |
unsigned int row; |
| 607 |
|
| 608 |
for (row = 0, outP = &pdbP->i->data[0]; |
| 609 |
row < pamP->height; |
| 610 |
++row, outP += rowSize) |
| 611 |
mpack(tuples[row], pamP, outP, ipdb_width(pdbP)); |
| 612 |
|
| 613 |
/* Pad with white on the bottom */ |
| 614 |
for (; row < ipdb_height(pdbP); ++row) |
| 615 |
memset(outP, 0, rowSize); |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
static int |
| 622 |
insertText(IPDB * const pdbP, |
| 623 |
const char * const content) { |
| 624 |
|
| 625 |
int retval; |
| 626 |
|
| 627 |
if (pdbP->i == NULL) |
| 628 |
retval = E_IMAGENOTTHERE; |
| 629 |
else if (pdbP->p->num_recs == 2) |
| 630 |
retval = E_TEXTTHERE; |
| 631 |
else { |
| 632 |
pdbP->t = ipdb_textAlloc(); |
| 633 |
if (pdbP->t == NULL) |
| 634 |
retval = ENOMEM; |
| 635 |
else { |
| 636 |
pdbP->t->data = strdup(content); |
| 637 |
|
| 638 |
if (pdbP->t->data == NULL) |
| 639 |
retval = ENOMEM; |
| 640 |
else { |
| 641 |
pdbP->p->num_recs = 2; |
| 642 |
|
| 643 |
pdbP->i->r->offset += 8; |
| 644 |
pdbP->t->r->offset = |
| 645 |
pdbP->i->r->offset + IMAGESIZE + ipdb_imgSize(pdbP->i); |
| 646 |
|
| 647 |
retval = 0; |
| 648 |
} |
| 649 |
} |
| 650 |
} |
| 651 |
return retval; |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
static void |
| 657 |
readimg(IPDB * const pdbP, |
| 658 |
FILE * const ifP, |
| 659 |
bool const depth4) { |
| 660 |
|
| 661 |
struct pam inpam; |
| 662 |
tuple ** tuples; |
| 663 |
|
| 664 |
tuples = pnm_readpam(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type)); |
| 665 |
|
| 666 |
if (strneq(inpam.tuple_type, "RGB", 3)) |
| 667 |
pm_error("Input image is color. Cannot make a Palm color image."); |
| 668 |
|
| 669 |
if (inpam.maxval == 1) |
| 670 |
insertMimage(pdbP, &inpam, tuples); |
| 671 |
else if (depth4) |
| 672 |
insertG16image(pdbP, &inpam, tuples); |
| 673 |
else |
| 674 |
insertGimage(pdbP, &inpam, tuples); |
| 675 |
|
| 676 |
pnm_freepamarray(tuples, &inpam); |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
static void |
| 682 |
readtxt(IPDB * const pdbP, |
| 683 |
const char * const noteFileName) { |
| 684 |
|
| 685 |
struct stat st; |
| 686 |
char * fileContent; |
| 687 |
FILE * fP; |
| 688 |
int n; |
| 689 |
int rc; |
| 690 |
size_t bytesRead; |
| 691 |
|
| 692 |
rc = stat(noteFileName, &st); |
| 693 |
|
| 694 |
if (rc != 0) |
| 695 |
pm_error("stat of '%s' failed, errno = %d (%s)", |
| 696 |
noteFileName, errno, strerror(errno)); |
| 697 |
|
| 698 |
/* The maximum size of a memory block that a Palm can allocate is 64K. |
| 699 |
Abort with error if specified note file is any larger. |
| 700 |
*/ |
| 701 |
|
| 702 |
if (st.st_size + 1 >= 65535) |
| 703 |
pm_error("Note file is too large: %lu bytes", |
| 704 |
(unsigned long) st.st_size); |
| 705 |
|
| 706 |
fP = pm_openr(noteFileName); |
| 707 |
|
| 708 |
MALLOCARRAY(fileContent, st.st_size + 1); |
| 709 |
|
| 710 |
if (fileContent == NULL) |
| 711 |
pm_error("Couldn't get %lu bytes of storage to read in note file", |
| 712 |
(unsigned long) st.st_size); |
| 713 |
|
| 714 |
bytesRead = fread(fileContent, 1, st.st_size, fP); |
| 715 |
|
| 716 |
if (bytesRead != st.st_size) |
| 717 |
pm_error("Failed to read note file '%s'. Errno = %d (%s)", |
| 718 |
noteFileName, errno, strerror(errno)); |
| 719 |
|
| 720 |
pm_close(fP); |
| 721 |
|
| 722 |
fileContent[st.st_size] = 0x00; /* add terminating NUL char */ |
| 723 |
|
| 724 |
/* Chop of trailing newlines */ |
| 725 |
for (n = strlen(fileContent) - 1; n >= 0 && fileContent[n] == '\n'; --n) |
| 726 |
fileContent[n] = '\0'; |
| 727 |
|
| 728 |
insertText(pdbP, fileContent); |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
int |
| 734 |
main(int argc, const char **argv) { |
| 735 |
|
| 736 |
struct CmdlineInfo cmdline; |
| 737 |
IPDB * pdbP; |
| 738 |
FILE * ifP; |
| 739 |
int comp; |
| 740 |
|
| 741 |
pm_proginit(&argc, argv); |
| 742 |
|
| 743 |
parseCommandLine(argc, argv, &cmdline); |
| 744 |
|
| 745 |
ifP = pm_openr(cmdline.inputFileName); |
| 746 |
|
| 747 |
switch (cmdline.compMode) { |
| 748 |
case COMPRESSED: comp = IPDB_COMPRESS; break; |
| 749 |
case UNCOMPRESSED: comp = IPDB_NOCOMPRESS; break; |
| 750 |
case MAYBE: comp = IPDB_COMPMAYBE; break; |
| 751 |
} |
| 752 |
|
| 753 |
if (strlen(cmdline.title) > 31) |
| 754 |
pm_error("Title too long. Max length is 31 characters."); |
| 755 |
|
| 756 |
pdbP = ipdb_alloc(); |
| 757 |
|
| 758 |
if (pdbP == NULL) |
| 759 |
pm_error("Failed to allocate IPDB structure"); |
| 760 |
|
| 761 |
setPdbHeader(pdbP->p, cmdline.title, cmdline.fixedtime); |
| 762 |
|
| 763 |
readimg(pdbP, ifP, cmdline.depth4); |
| 764 |
|
| 765 |
if (cmdline.notefile) |
| 766 |
readtxt(pdbP, cmdline.notefile); |
| 767 |
|
| 768 |
ipdbWrite(pdbP, comp, stdout); |
| 769 |
|
| 770 |
if (comp == IPDB_COMPMAYBE && !ipdb_compressed(pdbP)) |
| 771 |
pm_message("Image too complex to be compressed."); |
| 772 |
|
| 773 |
ipdb_free(pdbP); |
| 774 |
|
| 775 |
pm_close(ifP); |
| 776 |
|
| 777 |
return EXIT_SUCCESS; |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|