| 1 |
/* |
| 2 |
** pnmtotiff.c - converts a PNM image to a TIFF (Tagged Image File) image |
| 3 |
** |
| 4 |
** Derived by Jef Poskanzer from ras2tif.c, which is: |
| 5 |
** |
| 6 |
** Copyright (c) 1990 by Sun Microsystems, Inc. |
| 7 |
** |
| 8 |
** Author: Patrick J. Naughton |
| 9 |
** [email protected] |
| 10 |
** |
| 11 |
** Permission to use, copy, modify, and distribute this software and its |
| 12 |
** documentation for any purpose and without fee is hereby granted, |
| 13 |
** provided that the above copyright notice appear in all copies and that |
| 14 |
** both that copyright notice and this permission notice appear in |
| 15 |
** supporting documentation. |
| 16 |
** |
| 17 |
** This file is provided AS IS with no warranties of any kind. The author |
| 18 |
** shall have no liability with respect to the infringement of copyrights, |
| 19 |
** trade secrets or any patents by this file or any part thereof. In no |
| 20 |
** event will the author be liable for any lost revenue or profits or |
| 21 |
** other special, indirect and consequential damages. |
| 22 |
*/ |
| 23 |
|
| 24 |
#define _XOPEN_SOURCE /* Make sure stdio.h contains fileno() */ |
| 25 |
|
| 26 |
#include <unistd.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <fcntl.h> |
| 29 |
/* tiffio.h has a weird problem on AIX. tiffio.h wrongly typedefs |
| 30 |
"int32". That's wrong because such a name is likely to be used in |
| 31 |
other parts of the program that includes tiffio.h. And in fact, on |
| 32 |
AIX, <inttypes.h> defines it in some cases. One case in which <inttypes.h> |
| 33 |
does _not_ define int32 is where _XOPEN_SOURCE is declared. So we always |
| 34 |
do that when including tiffio.h. 06.01.05 |
| 35 |
*/ |
| 36 |
#include <tiffio.h> |
| 37 |
|
| 38 |
#include <string.h> |
| 39 |
|
| 40 |
#include "pm_c_util.h" |
| 41 |
#include "shhopt.h" |
| 42 |
#include "mallocvar.h" |
| 43 |
#include "nstring.h" |
| 44 |
#include "pam.h" |
| 45 |
#include "pammap.h" |
| 46 |
#include "pm_tiff.h" |
| 47 |
|
| 48 |
#define MAXCOLORS 256 |
| 49 |
|
| 50 |
/* 2001.09.03: Old tiff.h doesn't contain these values: */ |
| 51 |
#ifndef COMPRESSION_ADOBE_DEFLATE |
| 52 |
#define COMPRESSION_ADOBE_DEFLATE 8 |
| 53 |
#endif |
| 54 |
|
| 55 |
typedef struct { |
| 56 |
bool b1, b2, b4, b8; |
| 57 |
} SizeSet; |
| 58 |
|
| 59 |
typedef enum { TMPFILE, DIRECT_CREATE, DIRECT_APPEND } WriteMethod; |
| 60 |
|
| 61 |
typedef enum { MUST_EXIST, MAY_CREATE } CreatePolicy; |
| 62 |
|
| 63 |
typedef struct { |
| 64 |
/* All the information the user supplied in the command line, |
| 65 |
in a form easy for the program to use. |
| 66 |
*/ |
| 67 |
const char * inputFileName; |
| 68 |
int compression; |
| 69 |
/* COMPRESSION Tiff tag value, that corresponds to the compression |
| 70 |
option the user specified, or -1 if he didn't specify any. |
| 71 |
The -tag option doesn't count. |
| 72 |
*/ |
| 73 |
int fillorder; /* FILLORDER Tiff tag value */ |
| 74 |
int g3options; /* G3OPTIONS Tiff tag value or 0 for none */ |
| 75 |
int predictor; /* PREDICTOR Tiff tag value or -1 for none */ |
| 76 |
int rowsperstrip; /* -1 = unspecified */ |
| 77 |
unsigned int minisblack; /* logical: User wants MINISBLACK photometric */ |
| 78 |
unsigned int miniswhite; /* logical: User wants MINISWHITE photometric */ |
| 79 |
unsigned int truecolor; /* logical: User wants truecolor, not cmap */ |
| 80 |
unsigned int color; /* logical: assume not grayscale */ |
| 81 |
float xresolution; /* XRESOLUTION Tiff tag value or -1 for none */ |
| 82 |
float yresolution; /* YRESOLUTION Tiff tag value or -1 for none */ |
| 83 |
int resolutionunit; /* RESOLUTIONUNIT Tiff tag value */ |
| 84 |
SizeSet indexsizeAllowed; |
| 85 |
/* Which bit widths are allowable in a raster of palette indices */ |
| 86 |
unsigned int verbose; |
| 87 |
WriteMethod writeMethod; /* Output mode */ |
| 88 |
const char * output; /* -output option value. NULL if none. */ |
| 89 |
float resolution; /* X and Y resolution */ |
| 90 |
struct optNameValue * taglist; |
| 91 |
} CmdlineInfo; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
static void |
| 96 |
validateTagList(struct optNameValue const taglist[]) { |
| 97 |
|
| 98 |
unsigned int i; |
| 99 |
for (i = 0; taglist[i].name; ++i) { |
| 100 |
const char * const tagName = taglist[i].name; |
| 101 |
const tagDefinition * tagDefP = tagDefFind(tagName); |
| 102 |
|
| 103 |
if (!tagDefP) |
| 104 |
pm_error("Unknown tag name '%s'", tagName); |
| 105 |
else { |
| 106 |
/* We don't allow the user to set directly any tag that we |
| 107 |
control explicitly. These are normally tags that have to be |
| 108 |
consistent with other stuff we put in the image. But in some |
| 109 |
cases, they're just tags we had special options for before |
| 110 |
-tag existed. The latter should probably be converted |
| 111 |
eventually to prefer -tag. |
| 112 |
*/ |
| 113 |
switch (tagDefP->tagnum) { |
| 114 |
case TIFFTAG_IMAGEWIDTH: |
| 115 |
case TIFFTAG_IMAGELENGTH: |
| 116 |
case TIFFTAG_BITSPERSAMPLE: |
| 117 |
case TIFFTAG_COMPRESSION: |
| 118 |
case TIFFTAG_GROUP3OPTIONS: |
| 119 |
case TIFFTAG_PREDICTOR: |
| 120 |
case TIFFTAG_PHOTOMETRIC: |
| 121 |
case TIFFTAG_FILLORDER: |
| 122 |
case TIFFTAG_SAMPLESPERPIXEL: |
| 123 |
case TIFFTAG_ROWSPERSTRIP: |
| 124 |
case TIFFTAG_PLANARCONFIG: |
| 125 |
case TIFFTAG_COLORMAP: |
| 126 |
pm_error("You cannot specify a '%s' tag with -tag. " |
| 127 |
"Pamtotiff controls that internally or via other " |
| 128 |
"options.", tagName); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
static void |
| 137 |
parseIndexbits(bool const indexbitsSpec, |
| 138 |
char ** const indexbits, |
| 139 |
CmdlineInfo * const cmdlineP) { |
| 140 |
|
| 141 |
if (indexbitsSpec) { |
| 142 |
unsigned int i; |
| 143 |
|
| 144 |
/* Set initial values */ |
| 145 |
cmdlineP->indexsizeAllowed.b1 = false; |
| 146 |
cmdlineP->indexsizeAllowed.b2 = false; |
| 147 |
cmdlineP->indexsizeAllowed.b4 = false; |
| 148 |
cmdlineP->indexsizeAllowed.b8 = false; |
| 149 |
|
| 150 |
for (i = 0; indexbits[i]; ++i) { |
| 151 |
const char * const thisItem = indexbits[i]; |
| 152 |
if (streq(thisItem, "1")) |
| 153 |
cmdlineP->indexsizeAllowed.b1 = true; |
| 154 |
else if (streq(thisItem, "2")) |
| 155 |
cmdlineP->indexsizeAllowed.b2 = true; |
| 156 |
else if (streq(thisItem, "4")) |
| 157 |
cmdlineP->indexsizeAllowed.b4 = true; |
| 158 |
else if (streq(thisItem, "8")) |
| 159 |
cmdlineP->indexsizeAllowed.b8 = true; |
| 160 |
else |
| 161 |
pm_error("Invalid item in -indexbits list: '%s'. " |
| 162 |
"We recognize only 1, 2, 4, and 8", thisItem); |
| 163 |
} |
| 164 |
} else { |
| 165 |
cmdlineP->indexsizeAllowed.b1 = false; |
| 166 |
cmdlineP->indexsizeAllowed.b2 = false; |
| 167 |
cmdlineP->indexsizeAllowed.b4 = false; |
| 168 |
cmdlineP->indexsizeAllowed.b8 = true; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static void |
| 175 |
parseCommandLine(int argc, |
| 176 |
const char ** const argv, |
| 177 |
CmdlineInfo * const cmdlineP) { |
| 178 |
/*---------------------------------------------------------------------------- |
| 179 |
Note that the file spec array we return is stored in the storage that |
| 180 |
was passed to us as the argv array. |
| 181 |
-----------------------------------------------------------------------------*/ |
| 182 |
optEntry * option_def; |
| 183 |
|
| 184 |
optStruct3 opt; |
| 185 |
|
| 186 |
unsigned int none, packbits, lzw, g3, g4, msb2lsb, lsb2msb, opt_2d, fill; |
| 187 |
unsigned int flate, adobeflate; |
| 188 |
char ** indexbits; |
| 189 |
char * resolutionunit; |
| 190 |
|
| 191 |
unsigned int appendSpec, outputSpec, predictorSpec, rowsperstripSpec, |
| 192 |
xresolutionSpec, yresolutionSpec, indexbitsSpec, |
| 193 |
resolutionunitSpec, tagSpec; |
| 194 |
|
| 195 |
unsigned int option_def_index; |
| 196 |
|
| 197 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 198 |
|
| 199 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 200 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, &cmdlineP->verbose, 0); |
| 201 |
OPTENT3(0, "none", OPT_FLAG, NULL, &none, 0); |
| 202 |
OPTENT3(0, "packbits", OPT_FLAG, NULL, &packbits, 0); |
| 203 |
OPTENT3(0, "lzw", OPT_FLAG, NULL, &lzw, 0); |
| 204 |
OPTENT3(0, "g3", OPT_FLAG, NULL, &g3, 0); |
| 205 |
OPTENT3(0, "g4", OPT_FLAG, NULL, &g4, 0); |
| 206 |
OPTENT3(0, "flate", OPT_FLAG, NULL, &flate, 0); |
| 207 |
OPTENT3(0, "adobeflate", OPT_FLAG, NULL, &adobeflate, 0); |
| 208 |
OPTENT3(0, "msb2lsb", OPT_FLAG, NULL, &msb2lsb, 0); |
| 209 |
OPTENT3(0, "lsb2msb", OPT_FLAG, NULL, &lsb2msb, 0); |
| 210 |
OPTENT3(0, "2d", OPT_FLAG, NULL, &opt_2d, 0); |
| 211 |
OPTENT3(0, "fill", OPT_FLAG, NULL, &fill, 0); |
| 212 |
OPTENT3(0, "minisblack", OPT_FLAG, NULL, &cmdlineP->minisblack, 0); |
| 213 |
OPTENT3(0, "mb", OPT_FLAG, NULL, &cmdlineP->minisblack, 0); |
| 214 |
OPTENT3(0, "miniswhite", OPT_FLAG, NULL, &cmdlineP->miniswhite, 0); |
| 215 |
OPTENT3(0, "mw", OPT_FLAG, NULL, &cmdlineP->miniswhite, 0); |
| 216 |
OPTENT3(0, "truecolor", OPT_FLAG, NULL, &cmdlineP->truecolor, 0); |
| 217 |
OPTENT3(0, "color", OPT_FLAG, NULL, &cmdlineP->color, 0); |
| 218 |
OPTENT3(0, "append", OPT_FLAG, NULL, &appendSpec, 0); |
| 219 |
OPTENT3(0, "output", OPT_STRING, &cmdlineP->output, |
| 220 |
&outputSpec, 0); |
| 221 |
OPTENT3(0, "predictor", OPT_UINT, &cmdlineP->predictor, |
| 222 |
&predictorSpec, 0); |
| 223 |
OPTENT3(0, "rowsperstrip", OPT_UINT, &cmdlineP->rowsperstrip, |
| 224 |
&rowsperstripSpec, 0); |
| 225 |
OPTENT3(0, "xresolution", OPT_FLOAT, &cmdlineP->xresolution, |
| 226 |
&xresolutionSpec, 0); |
| 227 |
OPTENT3(0, "yresolution", OPT_FLOAT, &cmdlineP->yresolution, |
| 228 |
&yresolutionSpec, 0); |
| 229 |
OPTENT3(0, "resolutionunit", OPT_STRING, &resolutionunit, |
| 230 |
&resolutionunitSpec, 0); |
| 231 |
OPTENT3(0, "indexbits", OPT_STRINGLIST, &indexbits, |
| 232 |
&indexbitsSpec, 0); |
| 233 |
OPTENT3(0, "tag", OPT_NAMELIST, &cmdlineP->taglist, &tagSpec, 0); |
| 234 |
|
| 235 |
opt.opt_table = option_def; |
| 236 |
opt.short_allowed = false; /* We have no short (old-fashioned) options */ |
| 237 |
opt.allowNegNum = false; /* We have no parms that are negative numbers */ |
| 238 |
|
| 239 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 240 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 241 |
|
| 242 |
if (none + packbits + lzw + g3 + g4 + flate + adobeflate > 1) |
| 243 |
pm_error("You specified more than one compression option. " |
| 244 |
"Only one of -none, -packbits, -lze, -g3, and -g4 " |
| 245 |
"is allowed."); |
| 246 |
|
| 247 |
if (none) |
| 248 |
cmdlineP->compression = COMPRESSION_NONE; |
| 249 |
else if (packbits) |
| 250 |
cmdlineP->compression = COMPRESSION_PACKBITS; |
| 251 |
else if (lzw) |
| 252 |
cmdlineP->compression = COMPRESSION_LZW; |
| 253 |
else if (g3) |
| 254 |
cmdlineP->compression = COMPRESSION_CCITTFAX3; |
| 255 |
else if (g4) |
| 256 |
cmdlineP->compression = COMPRESSION_CCITTFAX4; |
| 257 |
else if (adobeflate) |
| 258 |
cmdlineP->compression = COMPRESSION_ADOBE_DEFLATE; |
| 259 |
else if (flate) |
| 260 |
cmdlineP->compression = COMPRESSION_DEFLATE; |
| 261 |
else |
| 262 |
cmdlineP->compression = COMPRESSION_NONE; |
| 263 |
|
| 264 |
if (msb2lsb + lsb2msb > 1) |
| 265 |
pm_error("You specified both -msb2lsb and -lsb2msb. " |
| 266 |
"These are conflicting options."); |
| 267 |
|
| 268 |
if (msb2lsb) |
| 269 |
cmdlineP->fillorder = FILLORDER_MSB2LSB; |
| 270 |
else if (lsb2msb) |
| 271 |
cmdlineP->fillorder = FILLORDER_LSB2MSB; |
| 272 |
else |
| 273 |
cmdlineP->fillorder = FILLORDER_MSB2LSB; |
| 274 |
|
| 275 |
|
| 276 |
if (cmdlineP->miniswhite && cmdlineP->minisblack) |
| 277 |
pm_error("You cannot specify both -miniswhite and -minisblack"); |
| 278 |
|
| 279 |
cmdlineP->g3options = 0; /* initial value */ |
| 280 |
if (opt_2d) |
| 281 |
cmdlineP->g3options |= GROUP3OPT_2DENCODING; |
| 282 |
if (fill) |
| 283 |
cmdlineP->g3options |= GROUP3OPT_FILLBITS; |
| 284 |
|
| 285 |
if (outputSpec) { |
| 286 |
if (appendSpec) |
| 287 |
cmdlineP->writeMethod = DIRECT_APPEND; |
| 288 |
else |
| 289 |
cmdlineP->writeMethod = DIRECT_CREATE; |
| 290 |
} else |
| 291 |
cmdlineP->writeMethod = TMPFILE; |
| 292 |
|
| 293 |
if (predictorSpec) { |
| 294 |
if (cmdlineP->predictor != 1 && cmdlineP->predictor != 2) |
| 295 |
pm_error("-predictor may be only 1 or 2. You specified %d.", |
| 296 |
cmdlineP->predictor); |
| 297 |
} else |
| 298 |
cmdlineP->predictor = -1; |
| 299 |
|
| 300 |
if (rowsperstripSpec) { |
| 301 |
if (cmdlineP->rowsperstrip < 1) |
| 302 |
pm_error("-rowsperstrip must be positive. You specified %d.", |
| 303 |
cmdlineP->rowsperstrip); |
| 304 |
} else |
| 305 |
cmdlineP->rowsperstrip = -1; |
| 306 |
|
| 307 |
if (xresolutionSpec) { |
| 308 |
if (cmdlineP->xresolution < 1) |
| 309 |
pm_error("-xresolution must be positive. You specified %f.", |
| 310 |
cmdlineP->xresolution); |
| 311 |
} else |
| 312 |
cmdlineP->xresolution = -1; |
| 313 |
|
| 314 |
if (yresolutionSpec) { |
| 315 |
if (cmdlineP->yresolution < 1) |
| 316 |
pm_error("-yresolution must be positive. You specified %f.", |
| 317 |
cmdlineP->yresolution); |
| 318 |
} else |
| 319 |
cmdlineP->yresolution = -1; |
| 320 |
|
| 321 |
if (resolutionunitSpec) { |
| 322 |
if (streq(resolutionunit, "inch")) |
| 323 |
cmdlineP->resolutionunit = RESUNIT_INCH; |
| 324 |
else if (streq(resolutionunit, "in")) |
| 325 |
cmdlineP->resolutionunit = RESUNIT_INCH; |
| 326 |
else if (streq(resolutionunit, "centimeter")) |
| 327 |
cmdlineP->resolutionunit = RESUNIT_CENTIMETER; |
| 328 |
else if (streq(resolutionunit, "cm")) |
| 329 |
cmdlineP->resolutionunit = RESUNIT_CENTIMETER; |
| 330 |
else if (streq(resolutionunit, "none")) |
| 331 |
cmdlineP->resolutionunit = RESUNIT_NONE; |
| 332 |
else if (streq(resolutionunit, "no")) |
| 333 |
cmdlineP->resolutionunit = RESUNIT_NONE; |
| 334 |
else |
| 335 |
pm_error("The only acceptable values for -resolutionunit are " |
| 336 |
"inch, centimeter, none, in, cm, and no. " |
| 337 |
"You specified '%s'.", resolutionunit); |
| 338 |
} else |
| 339 |
cmdlineP->resolutionunit = RESUNIT_INCH; |
| 340 |
|
| 341 |
parseIndexbits(indexbitsSpec, indexbits, cmdlineP); |
| 342 |
|
| 343 |
if (indexbitsSpec) |
| 344 |
free(indexbits); |
| 345 |
|
| 346 |
if (tagSpec) |
| 347 |
validateTagList(cmdlineP->taglist); |
| 348 |
else { |
| 349 |
MALLOCARRAY_NOFAIL(cmdlineP->taglist, 1); |
| 350 |
cmdlineP->taglist[0].name = NULL; |
| 351 |
cmdlineP->taglist[0].value = NULL; |
| 352 |
} |
| 353 |
|
| 354 |
if (argc-1 == 0) |
| 355 |
cmdlineP->inputFileName = "-"; |
| 356 |
else if (argc-1 != 1) |
| 357 |
pm_error("Program takes zero or one argument (filename). You " |
| 358 |
"specified %d", argc-1); |
| 359 |
else |
| 360 |
cmdlineP->inputFileName = argv[1]; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
static void |
| 366 |
freeCmdline(CmdlineInfo const cmdline) { |
| 367 |
|
| 368 |
pm_optDestroyNameValueList(cmdline.taglist); |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
static void |
| 374 |
fillRowOfSubBytePixels(struct pam * const pamP, |
| 375 |
const tuple * const tuplerow, |
| 376 |
unsigned char * const buf, |
| 377 |
unsigned short const tiff_maxval, |
| 378 |
unsigned short const photometric, |
| 379 |
int const fillorder, |
| 380 |
int const bitspersample, |
| 381 |
tuplehash const cht) { |
| 382 |
/*---------------------------------------------------------------------------- |
| 383 |
This subroutine deals with cases where multiple pixels are packed into |
| 384 |
a single byte of the Tiff output image, i.e. bits per pixel < 8. |
| 385 |
-----------------------------------------------------------------------------*/ |
| 386 |
int col; |
| 387 |
int bitshift; |
| 388 |
/* The number of bits we have to shift a pixel value left to line |
| 389 |
it up with where the current pixel goes in the current byte of |
| 390 |
the output buffer. |
| 391 |
*/ |
| 392 |
int const firstbitshift = |
| 393 |
(fillorder == FILLORDER_MSB2LSB) ? 8 - bitspersample : 0; |
| 394 |
/* The value of 'bitshift' for the first pixel into a |
| 395 |
byte of the output buffer. (MSB2LSB is normal). |
| 396 |
*/ |
| 397 |
sample s; |
| 398 |
/* The value of the current sample */ |
| 399 |
unsigned char* tP; |
| 400 |
/* The address of the byte in the output buffer in which |
| 401 |
the current pixel goes. |
| 402 |
*/ |
| 403 |
unsigned char byte; |
| 404 |
/* The under-construction value of the byte pointed to by |
| 405 |
tP, above. |
| 406 |
*/ |
| 407 |
|
| 408 |
bitshift = firstbitshift; |
| 409 |
byte = 0; |
| 410 |
for (col = 0, tP = buf; col < pamP->width; ++col) { |
| 411 |
if (cht == NULL) { |
| 412 |
/* It's grayscale */ |
| 413 |
s = tuplerow[col][0]; |
| 414 |
if (pamP->maxval != tiff_maxval ) |
| 415 |
s = (long) s * tiff_maxval / pamP->maxval; |
| 416 |
|
| 417 |
if (photometric == PHOTOMETRIC_MINISWHITE) |
| 418 |
s = tiff_maxval - s; |
| 419 |
} else { |
| 420 |
/* It's a colormapped raster */ |
| 421 |
int si; |
| 422 |
int found; |
| 423 |
pnm_lookuptuple(pamP, cht, tuplerow[col], &found, &si); |
| 424 |
if (!found) |
| 425 |
pm_error("INTERNAL ERROR. We made a color map, and a " |
| 426 |
"color map we need is not in it!. " |
| 427 |
"col=%d r=%lu g=%lu b=%lu", |
| 428 |
col, tuplerow[col][0], tuplerow[col][1], |
| 429 |
tuplerow[col][2]); |
| 430 |
s = (unsigned char) si; |
| 431 |
} |
| 432 |
byte |= s << bitshift; |
| 433 |
if (fillorder == FILLORDER_MSB2LSB) |
| 434 |
bitshift -= bitspersample; /* normal case */ |
| 435 |
else |
| 436 |
bitshift += bitspersample; |
| 437 |
if (bitshift < 0 || bitshift > 7) { |
| 438 |
*tP++ = byte; |
| 439 |
bitshift = firstbitshift; |
| 440 |
byte = 0; |
| 441 |
} |
| 442 |
} |
| 443 |
if (bitshift != firstbitshift) |
| 444 |
*tP++ = byte; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
static void |
| 450 |
putSample(sample const s, |
| 451 |
sample const maxval, |
| 452 |
sample const tiffMaxval, |
| 453 |
unsigned int const bitspersample, |
| 454 |
bool const minIsWhite, |
| 455 |
unsigned char ** const tPP) { |
| 456 |
|
| 457 |
/* Until release 10.48 (September 2009), we ignored the min-is-white |
| 458 |
photometric (i.e. treated it like min-is-black). Nobody has ever |
| 459 |
complained, but it seems clear to me that that was wrong, so I |
| 460 |
changed it. We have always respected it for sub-byte samples, |
| 461 |
and have always respected it going the other direction, in |
| 462 |
Tifftopnm. |
| 463 |
- Bryan. |
| 464 |
*/ |
| 465 |
|
| 466 |
xelval const s2 = maxval == tiffMaxval ? s : s * tiffMaxval / maxval; |
| 467 |
|
| 468 |
xelval const s3 = minIsWhite ? tiffMaxval - s2 : s2; |
| 469 |
|
| 470 |
if (bitspersample > 8) { |
| 471 |
*((unsigned short *)(*tPP)) = s3; |
| 472 |
(*tPP) += sizeof(short); |
| 473 |
} else |
| 474 |
*(*tPP)++ = s3 & 0xff; |
| 475 |
} |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
static void |
| 480 |
fillRowOfWholeBytePixels(struct pam * const pamP, |
| 481 |
tuple * const tuplerow, |
| 482 |
unsigned char * const buf, |
| 483 |
unsigned short const photometric, |
| 484 |
unsigned short const tiffMaxval, |
| 485 |
unsigned int const bitsPerSample) { |
| 486 |
|
| 487 |
bool const minIsWhite = (photometric == PHOTOMETRIC_MINISWHITE); |
| 488 |
|
| 489 |
unsigned int col; |
| 490 |
unsigned char * tP; |
| 491 |
unsigned int planes; |
| 492 |
|
| 493 |
if (photometric == PHOTOMETRIC_RGB) |
| 494 |
planes = pamP->depth; |
| 495 |
else |
| 496 |
/* Write only the first plane from the PAM, even if there |
| 497 |
are more. |
| 498 |
*/ |
| 499 |
planes = 1; |
| 500 |
|
| 501 |
for (col = 0, tP = buf; col < pamP->width; ++col) { |
| 502 |
unsigned int plane; |
| 503 |
for (plane = 0; plane < planes; ++plane) { |
| 504 |
putSample(tuplerow[col][plane], pamP->maxval, |
| 505 |
tiffMaxval, bitsPerSample, minIsWhite, &tP); |
| 506 |
/* Advances tP */ |
| 507 |
} |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
static void |
| 514 |
writeScanLines(struct pam * const pamP, |
| 515 |
TIFF * const tif, |
| 516 |
tuplehash const cht, |
| 517 |
unsigned short const tiffMaxval, |
| 518 |
unsigned short const bitspersample, |
| 519 |
unsigned short const photometric, |
| 520 |
int const bytesperrow, |
| 521 |
int const fillorder) { |
| 522 |
/*---------------------------------------------------------------------------- |
| 523 |
Write out the raster for the input image described by 'pamP', whose |
| 524 |
file is positioned to the raster of the image. |
| 525 |
-----------------------------------------------------------------------------*/ |
| 526 |
tuple * tuplerow; /* malloc'ed */ |
| 527 |
unsigned char * buf; /* malloc'ed */ |
| 528 |
int row; |
| 529 |
|
| 530 |
/* The man page for TIFFWriteScanLine doesn't tell the format of |
| 531 |
it's 'buf' parameter, but here it is: Its format depends on the |
| 532 |
bits per pixel of the TIFF image. If it's 16, 'buf' is an |
| 533 |
array of short (16 bit) integers, one per raster column. If |
| 534 |
it's 8, 'buf' is an array of 8 bit unsigned integers, one |
| 535 |
per pixel sample. If it's less than 8, it's an array of bytes, |
| 536 |
each of which represents 1-8 pixel samples, packed |
| 537 |
into it in the order specified by the TIFF image's fill order, |
| 538 |
with don't-care bits on the right such that each byte contains only |
| 539 |
whole samples. |
| 540 |
|
| 541 |
In all cases, the array elements are in order left to right going |
| 542 |
from low array indices to high array indices. |
| 543 |
|
| 544 |
The samples form pixel values according to the pixel format indicated |
| 545 |
by the TIFF photometric. E.g. if it is MINISWHITE, then a pixel is |
| 546 |
grayscale, composed of one sample where a value of 0 means white. |
| 547 |
*/ |
| 548 |
MALLOCARRAY(buf, bytesperrow); |
| 549 |
|
| 550 |
if (buf == NULL) |
| 551 |
pm_error("can't allocate memory for row buffer"); |
| 552 |
|
| 553 |
tuplerow = pnm_allocpamrow(pamP); |
| 554 |
|
| 555 |
for (row = 0; row < pamP->height; ++row) { |
| 556 |
int col; |
| 557 |
|
| 558 |
pnm_readpamrow(pamP, tuplerow); |
| 559 |
|
| 560 |
if (cht == NULL) { |
| 561 |
/* It's a direct, non-colormapped raster */ |
| 562 |
|
| 563 |
if (bitspersample == 8 || bitspersample == 16) |
| 564 |
fillRowOfWholeBytePixels(pamP, tuplerow, buf, photometric, |
| 565 |
tiffMaxval, bitspersample); |
| 566 |
else |
| 567 |
fillRowOfSubBytePixels(pamP, tuplerow, buf, |
| 568 |
tiffMaxval, photometric, |
| 569 |
fillorder, bitspersample, NULL); |
| 570 |
} else { |
| 571 |
/* It's a colormapped raster */ |
| 572 |
if (bitspersample == 8) { |
| 573 |
for (col = 0; col < pamP->width; ++col) { |
| 574 |
int si; |
| 575 |
int found; |
| 576 |
|
| 577 |
pnm_lookuptuple(pamP, cht, tuplerow[col], &found, &si); |
| 578 |
|
| 579 |
if (!found) |
| 580 |
pm_error("INTERNAL ERROR. We made a color map, and a " |
| 581 |
"color map we need is not in it! " |
| 582 |
"col=%d r=%lu g=%lu b=%lu", |
| 583 |
col, tuplerow[col][0], tuplerow[col][1], |
| 584 |
tuplerow[col][2]); |
| 585 |
buf[col] = (unsigned char) si; |
| 586 |
} |
| 587 |
} else { |
| 588 |
fillRowOfSubBytePixels(pamP, tuplerow, buf, 0, 0, fillorder, |
| 589 |
bitspersample, cht); |
| 590 |
} |
| 591 |
} |
| 592 |
if (TIFFWriteScanline(tif, buf, row, 0) < 0) |
| 593 |
pm_error("failed a scanline write on row %d", row); |
| 594 |
} |
| 595 |
pnm_freepamrow(tuplerow); |
| 596 |
free(buf); |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
static void |
| 602 |
analyzeColorsInRgbInput(struct pam * const pamP, |
| 603 |
CmdlineInfo const cmdline, |
| 604 |
int const maxcolors, |
| 605 |
tupletable * const chvP, |
| 606 |
unsigned int * const colorCtP, |
| 607 |
bool * const grayscaleP) { |
| 608 |
/*---------------------------------------------------------------------------- |
| 609 |
Same as analyzeColors(), except assuming input image has R/G/B tuples. |
| 610 |
-----------------------------------------------------------------------------*/ |
| 611 |
if (cmdline.color && cmdline.truecolor) { |
| 612 |
*chvP = NULL; |
| 613 |
*grayscaleP = false; |
| 614 |
} else { |
| 615 |
tupletable chv; |
| 616 |
bool grayscale; |
| 617 |
|
| 618 |
pm_message("computing colormap..."); |
| 619 |
chv = pnm_computetuplefreqtable2(pamP, NULL, maxcolors, |
| 620 |
pamP->maxval, |
| 621 |
colorCtP); |
| 622 |
if (chv == NULL) { |
| 623 |
grayscale = false; |
| 624 |
} else { |
| 625 |
unsigned int i; |
| 626 |
pm_message("%u color%s found", |
| 627 |
*colorCtP, *colorCtP == 1 ? "" : "s"); |
| 628 |
grayscale = true; /* initial assumption */ |
| 629 |
for (i = 0; i < *colorCtP && grayscale; ++i) { |
| 630 |
if (!pnm_rgbtupleisgray(chv[i]->tuple)) |
| 631 |
grayscale = false; |
| 632 |
} |
| 633 |
} |
| 634 |
*grayscaleP = grayscale; |
| 635 |
|
| 636 |
if (grayscale || cmdline.truecolor) { |
| 637 |
*chvP = NULL; |
| 638 |
if (chv) |
| 639 |
pnm_freetupletable(pamP, chv); |
| 640 |
} else { |
| 641 |
/* He wants a colormap. But can we give it to him? */ |
| 642 |
if (chv) |
| 643 |
*chvP = chv; |
| 644 |
else { |
| 645 |
pm_message("Too many colors - " |
| 646 |
"proceeding to write a 24-bit RGB file."); |
| 647 |
pm_message("If you want an 8-bit palette file, " |
| 648 |
"try doing a 'pnmquant %d'.", |
| 649 |
maxcolors); |
| 650 |
*chvP = NULL; |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
static void |
| 659 |
analyzeColors(struct pam * const pamP, |
| 660 |
CmdlineInfo const cmdline, |
| 661 |
int const maxcolors, |
| 662 |
tupletable * const chvP, |
| 663 |
unsigned int * const colorCtP, |
| 664 |
bool * const grayscaleP) { |
| 665 |
/*---------------------------------------------------------------------------- |
| 666 |
Analyze the colors in the input image described by 'pamP', whose file |
| 667 |
is positioned to the raster. |
| 668 |
|
| 669 |
Return *grayscaleP == true iff the image should be stored as a grayscale |
| 670 |
image (which means the image is monochromatic and the user doesn't |
| 671 |
insist on color format). |
| 672 |
|
| 673 |
If *grayscaleP is false and the colors, combined with command line options |
| 674 |
'cmdline', indicate a colormapped TIFF should be generated, return as *chvP |
| 675 |
the address of a color map (in newly malloc'ed space) and the number of |
| 676 |
colors in it as *colorCtP. If a colormapped color TIFF is not indicated, |
| 677 |
return *chvP == NULL and nothing as *colorCtP. |
| 678 |
|
| 679 |
Leave the file position undefined. |
| 680 |
-----------------------------------------------------------------------------*/ |
| 681 |
if (pamP->depth >= 3) |
| 682 |
/* Assume it's a color-capable input image |
| 683 |
(tuple type RGB or RGB_ALPHA) |
| 684 |
*/ |
| 685 |
analyzeColorsInRgbInput(pamP, cmdline, maxcolors, |
| 686 |
chvP, colorCtP, grayscaleP); |
| 687 |
else { |
| 688 |
*chvP = NULL; |
| 689 |
*grayscaleP = true; |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
static void |
| 696 |
reportTiffType(bool const grayscale, |
| 697 |
bool const colormapped, |
| 698 |
unsigned int const colorCt, |
| 699 |
bool const verbose) { |
| 700 |
|
| 701 |
if (verbose) { |
| 702 |
pm_message("Generating %s TIFF", grayscale ? "grayscale" : "color"); |
| 703 |
|
| 704 |
if (colormapped) |
| 705 |
pm_message("TIFF will have palette of %u colors", colorCt); |
| 706 |
else |
| 707 |
pm_message("TIFF will be truecolor (24 bit RGB)"); |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
static void |
| 714 |
computeRasterParm(struct pam * const pamP, |
| 715 |
tupletable const chv, |
| 716 |
int const colorCt, |
| 717 |
bool const grayscale, |
| 718 |
int const compression, |
| 719 |
bool const minisblack, |
| 720 |
bool const miniswhite, |
| 721 |
SizeSet const indexsizeAllowed, |
| 722 |
unsigned short * const samplesperpixelP, |
| 723 |
unsigned short * const bitspersampleP, |
| 724 |
unsigned short * const photometricP, |
| 725 |
int * const bytesperrowP) { |
| 726 |
/*---------------------------------------------------------------------------- |
| 727 |
Compute the parameters of the raster portion of the TIFF image. |
| 728 |
|
| 729 |
'minisblack' and 'miniswhite' mean the user requests the corresponding |
| 730 |
photometric. Both false means user has no explicit requirement. |
| 731 |
-----------------------------------------------------------------------------*/ |
| 732 |
unsigned short defaultPhotometric; |
| 733 |
/* The photometric we use if the user specified no preference */ |
| 734 |
|
| 735 |
/* We determine the bits per sample by the maxval, except that if |
| 736 |
it would be more than 8, we just use 16. I don't know if bits |
| 737 |
per sample between 8 and 16 are legal, but they aren't very |
| 738 |
nice in any case. If users want them, we should provide an |
| 739 |
option. It is not clear why we don't use bits per pixel < 8 |
| 740 |
for RGB images. Note that code to handle maxvals <= 255 was |
| 741 |
written long before maxval > 255 was possible and there are |
| 742 |
backward compatibility requirements. |
| 743 |
*/ |
| 744 |
|
| 745 |
if (pamP->depth == 1 && pamP->maxval == 1) { |
| 746 |
*samplesperpixelP = 1; |
| 747 |
*bitspersampleP = 1; |
| 748 |
if ((compression == COMPRESSION_CCITTFAX3) || |
| 749 |
(compression == COMPRESSION_CCITTFAX4)) |
| 750 |
defaultPhotometric = PHOTOMETRIC_MINISWHITE; |
| 751 |
else |
| 752 |
defaultPhotometric = PHOTOMETRIC_MINISBLACK; |
| 753 |
} else { |
| 754 |
if (chv) { |
| 755 |
*samplesperpixelP = 1; /* Pixel is just the one index value */ |
| 756 |
*bitspersampleP = |
| 757 |
colorCt <= 2 && indexsizeAllowed.b1 ? 1 : |
| 758 |
colorCt <= 4 && indexsizeAllowed.b2 ? 2 : |
| 759 |
colorCt <= 16 && indexsizeAllowed.b4 ? 4 : |
| 760 |
colorCt <= 256 && indexsizeAllowed.b8 ? 8 : |
| 761 |
0; |
| 762 |
if (*bitspersampleP == 0) |
| 763 |
pm_error("Your -indexbits option is insufficient for the " |
| 764 |
"%d colors in this image.", colorCt); |
| 765 |
|
| 766 |
defaultPhotometric = PHOTOMETRIC_PALETTE; |
| 767 |
} else { |
| 768 |
if (grayscale) { |
| 769 |
/* We'll write just the first plane regardless of how many are |
| 770 |
in the input PAM. |
| 771 |
*/ |
| 772 |
*samplesperpixelP = 1; |
| 773 |
*bitspersampleP = |
| 774 |
pamP->maxval > 255 ? 16 : pm_maxvaltobits(pamP->maxval); |
| 775 |
defaultPhotometric = PHOTOMETRIC_MINISBLACK; |
| 776 |
} else { |
| 777 |
*samplesperpixelP = pamP->depth; |
| 778 |
*bitspersampleP = pamP->maxval > 255 ? 16 : 8; |
| 779 |
defaultPhotometric = PHOTOMETRIC_RGB; |
| 780 |
} |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
if (miniswhite) { |
| 785 |
if (!grayscale) |
| 786 |
pm_error("Image is color, so -miniswhite is invalid"); |
| 787 |
*photometricP = PHOTOMETRIC_MINISWHITE; |
| 788 |
} else if (minisblack) { |
| 789 |
if (!grayscale) |
| 790 |
pm_error("Image is color, so -minisblack is invalid"); |
| 791 |
*photometricP = PHOTOMETRIC_MINISBLACK; |
| 792 |
} else |
| 793 |
*photometricP = defaultPhotometric; |
| 794 |
|
| 795 |
{ |
| 796 |
unsigned int const samplesPerRow = pamP->width * *samplesperpixelP; |
| 797 |
|
| 798 |
if (*bitspersampleP < 8) { |
| 799 |
int samplesperbyte; |
| 800 |
samplesperbyte = 8 / *bitspersampleP; |
| 801 |
*bytesperrowP = |
| 802 |
(samplesPerRow + samplesperbyte-1) / samplesperbyte; |
| 803 |
} else { |
| 804 |
if (UINT_MAX / *bitspersampleP < samplesPerRow) |
| 805 |
pm_error("Too many bytes per row to compute"); |
| 806 |
*bytesperrowP = (samplesPerRow * *bitspersampleP) / 8; |
| 807 |
} |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
/*---------------------------------------------------------------------------- |
| 814 |
WRITE MODES |
| 815 |
----------- |
| 816 |
|
| 817 |
The Tiff library does all output. There are several issues: |
| 818 |
|
| 819 |
1) The manner of output is opaque to the library client. I.e. we cannot |
| 820 |
see or control it. |
| 821 |
|
| 822 |
2) The output file must be random-access. |
| 823 |
|
| 824 |
3) The output file must be writable and readable for multiple-image |
| 825 |
streams. (This includes append operations.) |
| 826 |
|
| 827 |
4) The Tiff library produces unhelpful error messages when the above |
| 828 |
conditions are not met. |
| 829 |
|
| 830 |
We provide two modes for output: |
| 831 |
|
| 832 |
1. Tmpfile mode (default) |
| 833 |
|
| 834 |
We have the Tiff library direct output to an unnamed temporary file we |
| 835 |
create which is seekable and readable. When output is complete, we copy |
| 836 |
the file's contents to Standard Output. |
| 837 |
|
| 838 |
2. Direct mode (specified with -output) |
| 839 |
|
| 840 |
We have the Tiff library write output to the specified file. As the Tiff |
| 841 |
library requires that it be be seekable and readable, we fail the program |
| 842 |
rather than ask the Tiff library to use the file if it does not meet |
| 843 |
these requirements. |
| 844 |
|
| 845 |
Direct mode is further divided into append and create. They are the same |
| 846 |
except that in append mode, we insist that the file already exist, |
| 847 |
whereas with create mode, we create it if necessary. In either case, if |
| 848 |
the file already exists, he Tiff library appends the output to it. |
| 849 |
-----------------------------------------------------------------------------*/ |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
static bool |
| 854 |
fileIsSeekable(int const ofd, |
| 855 |
const char * const outFileName) { |
| 856 |
/*---------------------------------------------------------------------------- |
| 857 |
The file represented by 'ofd' iscapable of seeking. |
| 858 |
|
| 859 |
As a side effect, we position the file to the beginning. |
| 860 |
-----------------------------------------------------------------------------*/ |
| 861 |
int rc; |
| 862 |
|
| 863 |
/* We'd like to get the current position and leave the file positioned |
| 864 |
where we found it. But that entails the mess with some systems |
| 865 |
having 32 bit file offsets and some having 64 bit file offsets. |
| 866 |
*/ |
| 867 |
|
| 868 |
/* Some files can seek ahead, but not back. So we test for the |
| 869 |
more difficult backward seek. |
| 870 |
*/ |
| 871 |
lseek(ofd, 1, SEEK_SET); |
| 872 |
rc = lseek(ofd, 0, SEEK_SET); |
| 873 |
|
| 874 |
return rc >= 0; |
| 875 |
|
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
static void |
| 881 |
validateReadableOutputFile(int const ofd) { |
| 882 |
/*---------------------------------------------------------------------------- |
| 883 |
Validate that file 'ofd' is readable and fail the program if it isn't. |
| 884 |
|
| 885 |
This is useful because there are situations in which the TIFF library must |
| 886 |
read the output file and if it can't, it fails with an unhelpful error |
| 887 |
message about a file I/O error. We, on the other hand, produce a helpful |
| 888 |
error message. |
| 889 |
-----------------------------------------------------------------------------*/ |
| 890 |
#if !MSVCRT |
| 891 |
|
| 892 |
int flags; |
| 893 |
|
| 894 |
flags = fcntl(ofd, F_GETFL); |
| 895 |
|
| 896 |
if (flags < 0) { |
| 897 |
/* We couldn't get the flags. So just assume the file's OK */ |
| 898 |
} else { |
| 899 |
if ((flags & O_RDONLY) || (flags & O_RDWR)) { |
| 900 |
/* File is readable. All is well. */ |
| 901 |
} else |
| 902 |
pm_error("Output is not opened for reading. " |
| 903 |
"In order to create a multi-image TIFF stream, " |
| 904 |
"output must be both readable and writable."); |
| 905 |
} |
| 906 |
#endif |
| 907 |
} |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
static void |
| 912 |
createTiffGeneratorDirect(const char * const outputFileName, |
| 913 |
CreatePolicy const createPolicy, |
| 914 |
TIFF ** const tifPP, |
| 915 |
int * const ofdP) { |
| 916 |
/*---------------------------------------------------------------------------- |
| 917 |
Create a TIFF generator that writes its output to the specified file. |
| 918 |
|
| 919 |
If the file doesn't already exist and 'createPolicy' is MayCreate, |
| 920 |
create the file; otherwise fail the program. |
| 921 |
|
| 922 |
Fail the program if the specified file is not seekable and readable. |
| 923 |
|
| 924 |
Return the handle of the TIFF generator as *tifPP. Also return the |
| 925 |
file descriptor for the output file as *ofdP. |
| 926 |
-----------------------------------------------------------------------------*/ |
| 927 |
int fd; |
| 928 |
|
| 929 |
if (createPolicy == MUST_EXIST) |
| 930 |
fd = open(outputFileName, O_RDWR); |
| 931 |
else |
| 932 |
fd = open(outputFileName, (O_RDWR | O_CREAT), 00644); |
| 933 |
|
| 934 |
if (fd == -1) { |
| 935 |
if (errno == ENOENT) /* Possible only if MustExist */ |
| 936 |
pm_error ("Cannot open file : '%s'. File does not exist.", |
| 937 |
outputFileName); |
| 938 |
else |
| 939 |
pm_error ("Cannot open file : '%s'. open() failed with " |
| 940 |
"errno %d (%s). ", |
| 941 |
outputFileName, errno, strerror(errno)); |
| 942 |
} |
| 943 |
|
| 944 |
if (!fileIsSeekable(fd, outputFileName)) |
| 945 |
pm_error("Output file (%s) is not seekable. " |
| 946 |
"lseek() returned errno %d (%s). " |
| 947 |
"The TIFF library can write only to " |
| 948 |
"a seekable file.", |
| 949 |
outputFileName, errno, strerror(errno)); |
| 950 |
|
| 951 |
*tifPP = TIFFFdOpen(fd, outputFileName, "a"); |
| 952 |
if (*tifPP == NULL) |
| 953 |
pm_error("error opening file %s as TIFF file. " |
| 954 |
"TIFFFdOpen() failed.", outputFileName); |
| 955 |
|
| 956 |
*ofdP = fd; |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
static void |
| 962 |
createTiffGeneratorTmpfile(TIFF ** const tifPP, |
| 963 |
int * const ofdP) { |
| 964 |
/*---------------------------------------------------------------------------- |
| 965 |
Create a TIFF generator that writes its output to an unnnamed temporary file |
| 966 |
we create. |
| 967 |
|
| 968 |
Return the handle of the TIFF generator as *tifPP. Also return the file |
| 969 |
descriptor for the temporary file as *ofdP. |
| 970 |
|
| 971 |
The TIFF generator has a file name attribute, but it is just for messages; |
| 972 |
it is not the name of a file. We use "Internal Temporary File". |
| 973 |
-----------------------------------------------------------------------------*/ |
| 974 |
int fd; |
| 975 |
|
| 976 |
fd = pm_tmpfile_fd(); |
| 977 |
|
| 978 |
*tifPP = TIFFFdOpen(fd, "Internal Temporary File", "w"); |
| 979 |
|
| 980 |
if (*tifPP == NULL) |
| 981 |
pm_error("error opening temporary file as TIFF file. " |
| 982 |
"TIFFFdOpen() failed."); |
| 983 |
|
| 984 |
*ofdP = fd; |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
static void |
| 990 |
copyBufferToStdout(int const tmpfileFd) { |
| 991 |
|
| 992 |
FILE * tmpfileP; |
| 993 |
|
| 994 |
tmpfileP = fdopen(tmpfileFd, "rb"); |
| 995 |
|
| 996 |
fseek(tmpfileP, 0, SEEK_SET); |
| 997 |
|
| 998 |
while (!feof(tmpfileP) && !ferror(tmpfileP) && !ferror(stdout)) { |
| 999 |
char buffer[4096]; |
| 1000 |
size_t bytesReadCt; |
| 1001 |
|
| 1002 |
bytesReadCt = fread(buffer, 1, sizeof(buffer), tmpfileP); |
| 1003 |
|
| 1004 |
if (ferror(tmpfileP)) |
| 1005 |
pm_error("Error reading from temporary file. " |
| 1006 |
"Incomplete output. " |
| 1007 |
"Errno = %s (%d)", strerror(errno), errno); |
| 1008 |
else |
| 1009 |
fwrite(buffer, 1, bytesReadCt, stdout); |
| 1010 |
} |
| 1011 |
|
| 1012 |
/* POSIX lets us create a FILE from an existing file descriptor, but |
| 1013 |
does not provide a way to destroy the FILE and keep the file |
| 1014 |
descriptor. The following fclose() closes the file. Caller |
| 1015 |
must not access the file again, and if he attempts to close it, |
| 1016 |
must ignore the failure of close |
| 1017 |
*/ |
| 1018 |
fclose(tmpfileP); |
| 1019 |
} |
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
static void |
| 1024 |
closeTiffGenerator(WriteMethod const writeMethod, |
| 1025 |
TIFF * const tifP, |
| 1026 |
int const ofd) { |
| 1027 |
|
| 1028 |
TIFFFlushData(tifP); |
| 1029 |
|
| 1030 |
if (writeMethod == TMPFILE) |
| 1031 |
copyBufferToStdout(ofd); |
| 1032 |
|
| 1033 |
/* If we copied the buffer above, the buffer file is already closed |
| 1034 |
(copyBufferToStdout closes it), TIFFClose appears to tolerate that - |
| 1035 |
all it does is a close() and doesn't mind that it fails. |
| 1036 |
*/ |
| 1037 |
TIFFClose(tifP); |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
static void |
| 1043 |
createTiffColorMap(struct pam * const pamP, |
| 1044 |
unsigned int const bitspersample, |
| 1045 |
tupletable const chv, |
| 1046 |
unsigned int const colorCt, |
| 1047 |
unsigned short *** const tiffColorMapP) { |
| 1048 |
|
| 1049 |
unsigned int const colorMapSize = 1 << bitspersample; |
| 1050 |
unsigned short ** tiffColorMap; |
| 1051 |
unsigned int plane; |
| 1052 |
unsigned int i; |
| 1053 |
|
| 1054 |
MALLOCARRAY_NOFAIL(tiffColorMap, pamP->depth); |
| 1055 |
for (plane = 0; plane < pamP->depth; ++plane) |
| 1056 |
MALLOCARRAY_NOFAIL(tiffColorMap[plane], colorMapSize); |
| 1057 |
|
| 1058 |
for (i = 0; i < colorMapSize; ++i) { |
| 1059 |
unsigned int plane; |
| 1060 |
for (plane = 0; plane < pamP->depth; ++plane) { |
| 1061 |
if (i < colorCt) |
| 1062 |
tiffColorMap[plane][i] = |
| 1063 |
chv[i]->tuple[plane] * 65535L / pamP->maxval; |
| 1064 |
else |
| 1065 |
tiffColorMap[plane][i] = 0; |
| 1066 |
} |
| 1067 |
} |
| 1068 |
*tiffColorMapP = tiffColorMap; |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
static void |
| 1074 |
destroyTiffColorMap(struct pam * const pamP, |
| 1075 |
unsigned short ** const tiffColorMap) { |
| 1076 |
|
| 1077 |
unsigned int plane; |
| 1078 |
|
| 1079 |
for (plane = 0; plane < pamP->depth; ++plane) |
| 1080 |
free(tiffColorMap[plane]); |
| 1081 |
|
| 1082 |
free(tiffColorMap); |
| 1083 |
} |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
static void |
| 1088 |
setTagListFields(const struct optNameValue * const taglist, |
| 1089 |
TIFF * const tifP) { |
| 1090 |
|
| 1091 |
unsigned int i; |
| 1092 |
|
| 1093 |
for (i = 0; taglist[i].name; ++i) { |
| 1094 |
const tagDefinition * const tagDefP = tagDefFind(taglist[i].name); |
| 1095 |
|
| 1096 |
if (tagDefP->put) |
| 1097 |
tagDefP->put(tifP, tagDefP->tagnum, taglist[i].value, |
| 1098 |
tagDefP->choices); |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
static void |
| 1105 |
setTiffFields(TIFF * const tifP, |
| 1106 |
CmdlineInfo const cmdline, |
| 1107 |
struct pam * const pamP, |
| 1108 |
unsigned short const bitspersample, |
| 1109 |
unsigned short const photometric, |
| 1110 |
unsigned short const samplesperpixel, |
| 1111 |
unsigned short ** const tiffColorMap, |
| 1112 |
const char * const inputFileDescription, |
| 1113 |
struct optNameValue const taglist[]) { |
| 1114 |
|
| 1115 |
/* TODO: It would be cleaner to set all tags in a tag list and |
| 1116 |
then call setTagListFields() on that list. That way, |
| 1117 |
TIFFSetField() is in only one place and all tags get set the |
| 1118 |
same way and we don't have this icky setting of default and |
| 1119 |
then resetting with user choice (e.g. DOCUMENTNAME). |
| 1120 |
*/ |
| 1121 |
|
| 1122 |
/* Set TIFF parameters. */ |
| 1123 |
TIFFSetField(tifP, TIFFTAG_IMAGEWIDTH, pamP->width); |
| 1124 |
TIFFSetField(tifP, TIFFTAG_IMAGELENGTH, pamP->height); |
| 1125 |
TIFFSetField(tifP, TIFFTAG_BITSPERSAMPLE, bitspersample); |
| 1126 |
if (cmdline.compression != -1) |
| 1127 |
TIFFSetField(tifP, TIFFTAG_COMPRESSION, cmdline.compression); |
| 1128 |
if (cmdline.compression == COMPRESSION_CCITTFAX3 && cmdline.g3options != 0) |
| 1129 |
TIFFSetField( tifP, TIFFTAG_GROUP3OPTIONS, cmdline.g3options); |
| 1130 |
if (cmdline.compression == COMPRESSION_LZW && cmdline.predictor != -1) |
| 1131 |
TIFFSetField( tifP, TIFFTAG_PREDICTOR, cmdline.predictor); |
| 1132 |
TIFFSetField(tifP, TIFFTAG_PHOTOMETRIC, photometric); |
| 1133 |
if (cmdline.fillorder != -1) |
| 1134 |
TIFFSetField(tifP, TIFFTAG_FILLORDER, cmdline.fillorder); |
| 1135 |
TIFFSetField(tifP, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel); |
| 1136 |
if (cmdline.rowsperstrip != -1) |
| 1137 |
TIFFSetField(tifP, TIFFTAG_ROWSPERSTRIP, cmdline.rowsperstrip); |
| 1138 |
else |
| 1139 |
TIFFSetField(tifP, TIFFTAG_ROWSPERSTRIP, |
| 1140 |
TIFFDefaultStripSize(tifP, 0)); |
| 1141 |
/* Since Netpbm 10.31, we prefer that the user use -tags to specify |
| 1142 |
RESOLUTIONUNIT, XRESOLUTION, and YRESOLUTION, but retain |
| 1143 |
-xresolution, -yresolution, and -resolutionunit for backward |
| 1144 |
compatibility |
| 1145 |
*/ |
| 1146 |
if (cmdline.xresolution != -1 || |
| 1147 |
cmdline.yresolution != -1 || |
| 1148 |
cmdline.resolutionunit != -1) { |
| 1149 |
TIFFSetField(tifP, TIFFTAG_RESOLUTIONUNIT, |
| 1150 |
cmdline.resolutionunit != -1 ? |
| 1151 |
cmdline.resolutionunit : RESUNIT_NONE); |
| 1152 |
} |
| 1153 |
if (cmdline.xresolution > 0) |
| 1154 |
TIFFSetField(tifP, TIFFTAG_XRESOLUTION, cmdline.xresolution); |
| 1155 |
if ( cmdline.yresolution > 0) |
| 1156 |
TIFFSetField(tifP, TIFFTAG_YRESOLUTION, cmdline.yresolution); |
| 1157 |
TIFFSetField(tifP, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
| 1158 |
|
| 1159 |
if (tiffColorMap) |
| 1160 |
TIFFSetField(tifP, TIFFTAG_COLORMAP, |
| 1161 |
tiffColorMap[PAM_RED_PLANE], |
| 1162 |
tiffColorMap[PAM_GRN_PLANE], |
| 1163 |
tiffColorMap[PAM_BLU_PLANE]); |
| 1164 |
|
| 1165 |
|
| 1166 |
TIFFSetField(tifP, TIFFTAG_DOCUMENTNAME, inputFileDescription); |
| 1167 |
TIFFSetField(tifP, TIFFTAG_IMAGEDESCRIPTION, "converted PNM file"); |
| 1168 |
|
| 1169 |
/* Some of taglist[] overrides defaults we set above. But taglist[] |
| 1170 |
is defined not to specify any tag types that are not purely user |
| 1171 |
choice. |
| 1172 |
*/ |
| 1173 |
setTagListFields(taglist, tifP); |
| 1174 |
} |
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
static void |
| 1179 |
convertImage(FILE * const ifP, |
| 1180 |
TIFF * const tifP, |
| 1181 |
const char * const inputFileDescription, |
| 1182 |
CmdlineInfo const cmdline) { |
| 1183 |
|
| 1184 |
tupletable chv; |
| 1185 |
tuplehash cht; |
| 1186 |
unsigned short ** tiffColorMap; /* malloc'ed */ |
| 1187 |
struct pam pam; |
| 1188 |
unsigned int colorCt; |
| 1189 |
bool grayscale; |
| 1190 |
unsigned short photometric; |
| 1191 |
unsigned short samplesperpixel; |
| 1192 |
unsigned short bitspersample; |
| 1193 |
unsigned short tiff_maxval; |
| 1194 |
/* This is the maxval of the samples in the tiff file. It is |
| 1195 |
determined solely by the bits per sample ('bitspersample'). |
| 1196 |
*/ |
| 1197 |
int bytesperrow; |
| 1198 |
pm_filepos rasterPos; |
| 1199 |
|
| 1200 |
pnm_readpaminit(ifP, &pam, PAM_STRUCT_SIZE(tuple_type)); |
| 1201 |
|
| 1202 |
pm_tell2(ifP, &rasterPos, sizeof(rasterPos)); |
| 1203 |
|
| 1204 |
analyzeColors(&pam, cmdline, MAXCOLORS, &chv, &colorCt, &grayscale); |
| 1205 |
|
| 1206 |
reportTiffType(grayscale, chv != NULL, colorCt, cmdline.verbose); |
| 1207 |
|
| 1208 |
/* Go back to beginning of raster */ |
| 1209 |
pm_seek2(ifP, &rasterPos, sizeof(rasterPos)); |
| 1210 |
|
| 1211 |
/* Figure out TIFF parameters. */ |
| 1212 |
|
| 1213 |
computeRasterParm(&pam, chv, colorCt, grayscale, |
| 1214 |
cmdline.compression, |
| 1215 |
cmdline.minisblack, cmdline.miniswhite, |
| 1216 |
cmdline.indexsizeAllowed, |
| 1217 |
&samplesperpixel, &bitspersample, &photometric, |
| 1218 |
&bytesperrow); |
| 1219 |
|
| 1220 |
tiff_maxval = pm_bitstomaxval(bitspersample); |
| 1221 |
|
| 1222 |
if (!chv) { |
| 1223 |
cht = NULL; |
| 1224 |
tiffColorMap = NULL; |
| 1225 |
} else { |
| 1226 |
createTiffColorMap(&pam, bitspersample, chv, colorCt, &tiffColorMap); |
| 1227 |
|
| 1228 |
/* Convert color vector to color hash table, for fast lookup. */ |
| 1229 |
cht = pnm_computetupletablehash(&pam, chv, colorCt); |
| 1230 |
pnm_freetupletable(&pam, chv); |
| 1231 |
} |
| 1232 |
|
| 1233 |
setTiffFields(tifP, cmdline, &pam, bitspersample, photometric, |
| 1234 |
samplesperpixel, tiffColorMap, inputFileDescription, |
| 1235 |
cmdline.taglist); |
| 1236 |
|
| 1237 |
writeScanLines(&pam, tifP, cht, |
| 1238 |
tiff_maxval, bitspersample, photometric, bytesperrow, |
| 1239 |
cmdline.fillorder); |
| 1240 |
|
| 1241 |
if (tiffColorMap) |
| 1242 |
destroyTiffColorMap(&pam, tiffColorMap); |
| 1243 |
} |
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
int |
| 1248 |
main(int argc, const char *argv[]) { |
| 1249 |
CmdlineInfo cmdline; |
| 1250 |
const char * inputFileDescription; |
| 1251 |
FILE * ifP; |
| 1252 |
TIFF * tifP; |
| 1253 |
int ofd; |
| 1254 |
int eof; |
| 1255 |
unsigned int imageSeq; |
| 1256 |
|
| 1257 |
pm_proginit(&argc, argv); |
| 1258 |
|
| 1259 |
parseCommandLine(argc, argv, &cmdline); |
| 1260 |
|
| 1261 |
ifP = pm_openr_seekable(cmdline.inputFileName); |
| 1262 |
|
| 1263 |
if (streq(cmdline.inputFileName, "-")) |
| 1264 |
inputFileDescription = "Standard Input"; |
| 1265 |
else |
| 1266 |
inputFileDescription = cmdline.inputFileName; |
| 1267 |
|
| 1268 |
switch (cmdline.writeMethod) { |
| 1269 |
case DIRECT_APPEND: |
| 1270 |
createTiffGeneratorDirect(cmdline.output, MUST_EXIST, &tifP, &ofd); |
| 1271 |
break; |
| 1272 |
case DIRECT_CREATE: |
| 1273 |
createTiffGeneratorDirect(cmdline.output, MAY_CREATE, &tifP, &ofd); |
| 1274 |
break; |
| 1275 |
case TMPFILE: |
| 1276 |
createTiffGeneratorTmpfile(&tifP, &ofd); |
| 1277 |
break; |
| 1278 |
} |
| 1279 |
|
| 1280 |
eof = false; /* initial assumption */ |
| 1281 |
imageSeq = 0; |
| 1282 |
|
| 1283 |
while (!eof) { |
| 1284 |
bool success; |
| 1285 |
|
| 1286 |
pnm_nextimage(ifP, &eof); |
| 1287 |
|
| 1288 |
if (!eof) { |
| 1289 |
if (imageSeq > 0) |
| 1290 |
validateReadableOutputFile(ofd); |
| 1291 |
|
| 1292 |
if (cmdline.verbose) |
| 1293 |
pm_message("Converting Image %u", imageSeq); |
| 1294 |
|
| 1295 |
convertImage(ifP, tifP, inputFileDescription, cmdline); |
| 1296 |
|
| 1297 |
success = TIFFWriteDirectory(tifP); |
| 1298 |
if (!success) |
| 1299 |
pm_error("Unable to write TIFF image %u to file. " |
| 1300 |
"tifWriteDirectory() failed.", imageSeq); |
| 1301 |
++imageSeq; |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
closeTiffGenerator(cmdline.writeMethod, tifP, ofd); |
| 1306 |
pm_close(ifP); |
| 1307 |
|
| 1308 |
freeCmdline(cmdline); |
| 1309 |
|
| 1310 |
return 0; |
| 1311 |
} |
| 1312 |
|
| 1313 |
|
| 1314 |
|