| 1 |
/* |
| 2 |
* Copyright (c) 1991-1997 Sam Leffler |
| 3 |
* Copyright (c) 1991-1997 Silicon Graphics, Inc. |
| 4 |
* |
| 5 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 6 |
* its documentation for any purpose is hereby granted without fee, provided |
| 7 |
* that (i) the above copyright notices and this permission notice appear in |
| 8 |
* all copies of the software and related documentation, and (ii) the names of |
| 9 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 10 |
* publicity relating to the software without the specific, prior written |
| 11 |
* permission of Sam Leffler and Silicon Graphics. |
| 12 |
* |
| 13 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 14 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 15 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 16 |
* |
| 17 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 18 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 19 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 20 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 21 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 22 |
* OF THIS SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "libport.h" |
| 26 |
#include "tif_config.h" |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#ifdef HAVE_UNISTD_H |
| 33 |
#include <unistd.h> |
| 34 |
#endif |
| 35 |
|
| 36 |
#include "tiffio.h" |
| 37 |
#include "tiffiop.h" |
| 38 |
|
| 39 |
#ifndef EXIT_SUCCESS |
| 40 |
#define EXIT_SUCCESS 0 |
| 41 |
#endif |
| 42 |
#ifndef EXIT_FAILURE |
| 43 |
#define EXIT_FAILURE 1 |
| 44 |
#endif |
| 45 |
|
| 46 |
#define streq(a, b) (strcmp(a, b) == 0) |
| 47 |
#define CopyField(tag, v) \ |
| 48 |
if (TIFFGetField(in, tag, &v)) \ |
| 49 |
TIFFSetField(out, tag, v) |
| 50 |
|
| 51 |
#ifndef howmany |
| 52 |
#define howmany(x, y) (((x) + ((y)-1)) / (y)) |
| 53 |
#endif |
| 54 |
#define roundup(x, y) (howmany(x, y) * ((uint32_t)(y))) |
| 55 |
|
| 56 |
static uint16_t compression = COMPRESSION_PACKBITS; |
| 57 |
static uint32_t rowsperstrip = (uint32_t)-1; |
| 58 |
static int process_by_block = 0; /* default is whole image at once */ |
| 59 |
static int no_alpha = 0; |
| 60 |
static int background = 0; |
| 61 |
static int bigtiff_output = 0; |
| 62 |
#define DEFAULT_MAX_MALLOC (256 * 1024 * 1024) |
| 63 |
/* malloc size limit (in bytes) |
| 64 |
* disabled when set to 0 */ |
| 65 |
static tmsize_t maxMalloc = DEFAULT_MAX_MALLOC; |
| 66 |
|
| 67 |
static int tiffcvt(TIFF *in, TIFF *out); |
| 68 |
static void usage(int code); |
| 69 |
|
| 70 |
int main(int argc, char *argv[]) |
| 71 |
{ |
| 72 |
TIFF *in, *out; |
| 73 |
int c; |
| 74 |
#if !HAVE_DECL_OPTARG |
| 75 |
extern int optind; |
| 76 |
extern char *optarg; |
| 77 |
#endif |
| 78 |
|
| 79 |
while ((c = getopt(argc, argv, "c:r:t:B:bn8hM:")) != -1) |
| 80 |
switch (c) |
| 81 |
{ |
| 82 |
case 'M': |
| 83 |
maxMalloc = (tmsize_t)strtoul(optarg, NULL, 0) << 20; |
| 84 |
break; |
| 85 |
case 'b': |
| 86 |
process_by_block = 1; |
| 87 |
break; |
| 88 |
|
| 89 |
case 'c': |
| 90 |
if (streq(optarg, "none")) |
| 91 |
compression = COMPRESSION_NONE; |
| 92 |
else if (streq(optarg, "packbits")) |
| 93 |
compression = COMPRESSION_PACKBITS; |
| 94 |
else if (streq(optarg, "lzw")) |
| 95 |
compression = COMPRESSION_LZW; |
| 96 |
else if (streq(optarg, "jpeg")) |
| 97 |
compression = COMPRESSION_JPEG; |
| 98 |
else if (streq(optarg, "zip")) |
| 99 |
compression = COMPRESSION_ADOBE_DEFLATE; |
| 100 |
else |
| 101 |
usage(EXIT_FAILURE); |
| 102 |
break; |
| 103 |
|
| 104 |
case 'r': |
| 105 |
rowsperstrip = atoi(optarg); |
| 106 |
break; |
| 107 |
|
| 108 |
case 't': |
| 109 |
rowsperstrip = atoi(optarg); |
| 110 |
break; |
| 111 |
|
| 112 |
case 'B': |
| 113 |
background = atoi(optarg) & 0xFF; |
| 114 |
break; |
| 115 |
|
| 116 |
case 'n': |
| 117 |
no_alpha = 1; |
| 118 |
break; |
| 119 |
|
| 120 |
case '8': |
| 121 |
bigtiff_output = 1; |
| 122 |
break; |
| 123 |
|
| 124 |
case 'h': |
| 125 |
usage(EXIT_SUCCESS); |
| 126 |
/*NOTREACHED*/ |
| 127 |
break; |
| 128 |
case '?': |
| 129 |
usage(EXIT_FAILURE); |
| 130 |
/*NOTREACHED*/ |
| 131 |
break; |
| 132 |
} |
| 133 |
|
| 134 |
if (argc - optind < 2) |
| 135 |
usage(EXIT_FAILURE); |
| 136 |
|
| 137 |
TIFFOpenOptions *opts = TIFFOpenOptionsAlloc(); |
| 138 |
if (opts == NULL) |
| 139 |
{ |
| 140 |
return EXIT_FAILURE; |
| 141 |
} |
| 142 |
TIFFOpenOptionsSetMaxSingleMemAlloc(opts, maxMalloc); |
| 143 |
out = TIFFOpenExt(argv[argc - 1], bigtiff_output ? "w8" : "w", opts); |
| 144 |
if (out == NULL) |
| 145 |
{ |
| 146 |
TIFFOpenOptionsFree(opts); |
| 147 |
return (EXIT_FAILURE); |
| 148 |
} |
| 149 |
|
| 150 |
for (; optind < argc - 1; optind++) |
| 151 |
{ |
| 152 |
in = TIFFOpenExt(argv[optind], "r", opts); |
| 153 |
if (in != NULL) |
| 154 |
{ |
| 155 |
do |
| 156 |
{ |
| 157 |
if (!tiffcvt(in, out) || !TIFFWriteDirectory(out)) |
| 158 |
{ |
| 159 |
(void)TIFFClose(out); |
| 160 |
(void)TIFFClose(in); |
| 161 |
TIFFOpenOptionsFree(opts); |
| 162 |
return (1); |
| 163 |
} |
| 164 |
} while (TIFFReadDirectory(in)); |
| 165 |
(void)TIFFClose(in); |
| 166 |
} |
| 167 |
} |
| 168 |
TIFFOpenOptionsFree(opts); |
| 169 |
(void)TIFFClose(out); |
| 170 |
return (EXIT_SUCCESS); |
| 171 |
} |
| 172 |
|
| 173 |
static int cvt_by_tile(TIFF *in, TIFF *out) |
| 174 |
|
| 175 |
{ |
| 176 |
uint32_t *raster; /* retrieve RGBA image */ |
| 177 |
uint32_t width, height; /* image width & height */ |
| 178 |
uint32_t tile_width, tile_height; |
| 179 |
uint32_t row, col; |
| 180 |
uint32_t *wrk_line; |
| 181 |
int ok = 1; |
| 182 |
uint32_t rastersize, wrk_linesize; |
| 183 |
|
| 184 |
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width); |
| 185 |
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height); |
| 186 |
|
| 187 |
if (!TIFFGetField(in, TIFFTAG_TILEWIDTH, &tile_width) || |
| 188 |
!TIFFGetField(in, TIFFTAG_TILELENGTH, &tile_height)) |
| 189 |
{ |
| 190 |
TIFFError(TIFFFileName(in), "Source image not tiled"); |
| 191 |
return (0); |
| 192 |
} |
| 193 |
|
| 194 |
TIFFSetField(out, TIFFTAG_TILEWIDTH, tile_width); |
| 195 |
TIFFSetField(out, TIFFTAG_TILELENGTH, tile_height); |
| 196 |
|
| 197 |
/* |
| 198 |
* Allocate tile buffer |
| 199 |
*/ |
| 200 |
rastersize = tile_width * tile_height * sizeof(uint32_t); |
| 201 |
if (tile_width != (rastersize / tile_height) / sizeof(uint32_t)) |
| 202 |
{ |
| 203 |
TIFFError(TIFFFileName(in), |
| 204 |
"Integer overflow when calculating raster buffer"); |
| 205 |
exit(EXIT_FAILURE); |
| 206 |
} |
| 207 |
raster = (uint32_t *)_TIFFmalloc(rastersize); |
| 208 |
if (raster == 0) |
| 209 |
{ |
| 210 |
TIFFError(TIFFFileName(in), "No space for raster buffer"); |
| 211 |
return (0); |
| 212 |
} |
| 213 |
|
| 214 |
/* |
| 215 |
* Allocate a scanline buffer for swapping during the vertical |
| 216 |
* mirroring pass. |
| 217 |
*/ |
| 218 |
wrk_linesize = tile_width * sizeof(uint32_t); |
| 219 |
if (tile_width != wrk_linesize / sizeof(uint32_t)) |
| 220 |
{ |
| 221 |
TIFFError(TIFFFileName(in), |
| 222 |
"Integer overflow when calculating wrk_line buffer"); |
| 223 |
exit(EXIT_FAILURE); |
| 224 |
} |
| 225 |
wrk_line = (uint32_t *)_TIFFmalloc(wrk_linesize); |
| 226 |
if (!wrk_line) |
| 227 |
{ |
| 228 |
TIFFError(TIFFFileName(in), "No space for raster scanline buffer"); |
| 229 |
ok = 0; |
| 230 |
} |
| 231 |
|
| 232 |
/* |
| 233 |
* Loop over the tiles. |
| 234 |
*/ |
| 235 |
for (row = 0; ok && row < height; row += tile_height) |
| 236 |
{ |
| 237 |
for (col = 0; ok && col < width; col += tile_width) |
| 238 |
{ |
| 239 |
uint32_t i_row; |
| 240 |
|
| 241 |
/* Read the tile into an RGBA array */ |
| 242 |
if (!TIFFReadRGBATile(in, col, row, raster)) |
| 243 |
{ |
| 244 |
ok = 0; |
| 245 |
break; |
| 246 |
} |
| 247 |
|
| 248 |
/* |
| 249 |
* XXX: raster array has 4-byte unsigned integer type, that is why |
| 250 |
* we should rearrange it here. |
| 251 |
*/ |
| 252 |
#if HOST_BIGENDIAN |
| 253 |
TIFFSwabArrayOfLong(raster, tile_width * tile_height); |
| 254 |
#endif |
| 255 |
|
| 256 |
/* |
| 257 |
* For some reason the TIFFReadRGBATile() function chooses the |
| 258 |
* lower left corner as the origin. Vertically mirror scanlines. |
| 259 |
*/ |
| 260 |
for (i_row = 0; i_row < tile_height / 2; i_row++) |
| 261 |
{ |
| 262 |
uint32_t *top_line, *bottom_line; |
| 263 |
|
| 264 |
top_line = raster + tile_width * i_row; |
| 265 |
bottom_line = raster + tile_width * (tile_height - i_row - 1); |
| 266 |
|
| 267 |
_TIFFmemcpy(wrk_line, top_line, 4 * tile_width); |
| 268 |
_TIFFmemcpy(top_line, bottom_line, 4 * tile_width); |
| 269 |
_TIFFmemcpy(bottom_line, wrk_line, 4 * tile_width); |
| 270 |
} |
| 271 |
|
| 272 |
/* |
| 273 |
* Write out the result in a tile. |
| 274 |
*/ |
| 275 |
|
| 276 |
if (TIFFWriteEncodedTile(out, TIFFComputeTile(out, col, row, 0, 0), |
| 277 |
raster, |
| 278 |
4 * tile_width * tile_height) == -1) |
| 279 |
{ |
| 280 |
ok = 0; |
| 281 |
break; |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
_TIFFfree(raster); |
| 287 |
_TIFFfree(wrk_line); |
| 288 |
|
| 289 |
return ok; |
| 290 |
} |
| 291 |
|
| 292 |
static int cvt_by_strip(TIFF *in, TIFF *out) |
| 293 |
|
| 294 |
{ |
| 295 |
uint32_t *raster; /* retrieve RGBA image */ |
| 296 |
uint32_t width, height; /* image width & height */ |
| 297 |
uint32_t row; |
| 298 |
uint32_t *wrk_line; |
| 299 |
int ok = 1; |
| 300 |
uint32_t rastersize, wrk_linesize; |
| 301 |
|
| 302 |
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width); |
| 303 |
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height); |
| 304 |
|
| 305 |
if (!TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip)) |
| 306 |
{ |
| 307 |
TIFFError(TIFFFileName(in), "Source image not in strips"); |
| 308 |
return (0); |
| 309 |
} |
| 310 |
|
| 311 |
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip); |
| 312 |
|
| 313 |
/* |
| 314 |
* Allocate strip buffer |
| 315 |
*/ |
| 316 |
rastersize = width * rowsperstrip * sizeof(uint32_t); |
| 317 |
if (width != (rastersize / rowsperstrip) / sizeof(uint32_t)) |
| 318 |
{ |
| 319 |
TIFFError(TIFFFileName(in), |
| 320 |
"Integer overflow when calculating raster buffer"); |
| 321 |
exit(EXIT_FAILURE); |
| 322 |
} |
| 323 |
raster = (uint32_t *)_TIFFmalloc(rastersize); |
| 324 |
if (raster == 0) |
| 325 |
{ |
| 326 |
TIFFError(TIFFFileName(in), "No space for raster buffer"); |
| 327 |
return (0); |
| 328 |
} |
| 329 |
|
| 330 |
/* |
| 331 |
* Allocate a scanline buffer for swapping during the vertical |
| 332 |
* mirroring pass. |
| 333 |
*/ |
| 334 |
wrk_linesize = width * sizeof(uint32_t); |
| 335 |
if (width != wrk_linesize / sizeof(uint32_t)) |
| 336 |
{ |
| 337 |
TIFFError(TIFFFileName(in), |
| 338 |
"Integer overflow when calculating wrk_line buffer"); |
| 339 |
exit(EXIT_FAILURE); |
| 340 |
} |
| 341 |
wrk_line = (uint32_t *)_TIFFmalloc(wrk_linesize); |
| 342 |
if (!wrk_line) |
| 343 |
{ |
| 344 |
TIFFError(TIFFFileName(in), "No space for raster scanline buffer"); |
| 345 |
ok = 0; |
| 346 |
} |
| 347 |
|
| 348 |
/* |
| 349 |
* Loop over the strips. |
| 350 |
*/ |
| 351 |
for (row = 0; ok && row < height; row += rowsperstrip) |
| 352 |
{ |
| 353 |
int rows_to_write, i_row; |
| 354 |
|
| 355 |
/* Read the strip into an RGBA array */ |
| 356 |
if (!TIFFReadRGBAStrip(in, row, raster)) |
| 357 |
{ |
| 358 |
ok = 0; |
| 359 |
break; |
| 360 |
} |
| 361 |
|
| 362 |
/* |
| 363 |
* XXX: raster array has 4-byte unsigned integer type, that is why |
| 364 |
* we should rearrange it here. |
| 365 |
*/ |
| 366 |
#if HOST_BIGENDIAN |
| 367 |
TIFFSwabArrayOfLong(raster, width * rowsperstrip); |
| 368 |
#endif |
| 369 |
|
| 370 |
/* |
| 371 |
* Figure out the number of scanlines actually in this strip. |
| 372 |
*/ |
| 373 |
if (row + rowsperstrip > height) |
| 374 |
rows_to_write = height - row; |
| 375 |
else |
| 376 |
rows_to_write = rowsperstrip; |
| 377 |
|
| 378 |
/* |
| 379 |
* For some reason the TIFFReadRGBAStrip() function chooses the |
| 380 |
* lower left corner as the origin. Vertically mirror scanlines. |
| 381 |
*/ |
| 382 |
|
| 383 |
for (i_row = 0; i_row < rows_to_write / 2; i_row++) |
| 384 |
{ |
| 385 |
uint32_t *top_line, *bottom_line; |
| 386 |
|
| 387 |
top_line = raster + width * i_row; |
| 388 |
bottom_line = raster + width * (rows_to_write - i_row - 1); |
| 389 |
|
| 390 |
_TIFFmemcpy(wrk_line, top_line, 4 * width); |
| 391 |
_TIFFmemcpy(top_line, bottom_line, 4 * width); |
| 392 |
_TIFFmemcpy(bottom_line, wrk_line, 4 * width); |
| 393 |
} |
| 394 |
|
| 395 |
/* |
| 396 |
* Write out the result in a strip |
| 397 |
*/ |
| 398 |
|
| 399 |
if (TIFFWriteEncodedStrip(out, row / rowsperstrip, raster, |
| 400 |
4 * rows_to_write * width) == -1) |
| 401 |
{ |
| 402 |
ok = 0; |
| 403 |
break; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
_TIFFfree(raster); |
| 408 |
_TIFFfree(wrk_line); |
| 409 |
|
| 410 |
return ok; |
| 411 |
} |
| 412 |
|
| 413 |
/* |
| 414 |
* cvt_whole_image() |
| 415 |
* |
| 416 |
* read the whole image into one big RGBA buffer and then write out |
| 417 |
* strips from that. This is using the traditional TIFFReadRGBAImage() |
| 418 |
* API that we trust. |
| 419 |
*/ |
| 420 |
|
| 421 |
static int cvt_whole_image(TIFF *in, TIFF *out) |
| 422 |
|
| 423 |
{ |
| 424 |
uint32_t *raster; /* retrieve RGBA image */ |
| 425 |
uint32_t width, height; /* image width & height */ |
| 426 |
uint32_t row; |
| 427 |
size_t pixel_count; |
| 428 |
|
| 429 |
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width); |
| 430 |
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height); |
| 431 |
pixel_count = width * height; |
| 432 |
|
| 433 |
/* XXX: Check the integer overflow. */ |
| 434 |
if (!width || !height || pixel_count / width != height) |
| 435 |
{ |
| 436 |
TIFFError( |
| 437 |
TIFFFileName(in), |
| 438 |
"Malformed input file; can't allocate buffer for raster of %" PRIu32 |
| 439 |
"x%" PRIu32 " size", |
| 440 |
width, height); |
| 441 |
return 0; |
| 442 |
} |
| 443 |
if (maxMalloc != 0 && |
| 444 |
(tmsize_t)pixel_count * (tmsize_t)sizeof(uint32_t) > maxMalloc) |
| 445 |
{ |
| 446 |
TIFFError(TIFFFileName(in), |
| 447 |
"Raster size %" TIFF_SIZE_FORMAT |
| 448 |
" over memory limit (%" TIFF_SSIZE_FORMAT "), try -b option.", |
| 449 |
pixel_count * sizeof(uint32_t), maxMalloc); |
| 450 |
return 0; |
| 451 |
} |
| 452 |
|
| 453 |
rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip); |
| 454 |
TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip); |
| 455 |
|
| 456 |
raster = (uint32_t *)_TIFFCheckMalloc(in, pixel_count, sizeof(uint32_t), |
| 457 |
"raster buffer"); |
| 458 |
if (raster == 0) |
| 459 |
{ |
| 460 |
TIFFError(TIFFFileName(in), |
| 461 |
"Failed to allocate buffer (%" TIFF_SIZE_FORMAT |
| 462 |
" elements of %" TIFF_SIZE_FORMAT " each)", |
| 463 |
pixel_count, sizeof(uint32_t)); |
| 464 |
return (0); |
| 465 |
} |
| 466 |
|
| 467 |
/* Read the image in one chunk into an RGBA array */ |
| 468 |
if (!TIFFReadRGBAImageOriented(in, width, height, raster, |
| 469 |
ORIENTATION_TOPLEFT, 0)) |
| 470 |
{ |
| 471 |
_TIFFfree(raster); |
| 472 |
return (0); |
| 473 |
} |
| 474 |
|
| 475 |
/* |
| 476 |
* XXX: raster array has 4-byte unsigned integer type, that is why |
| 477 |
* we should rearrange it here. |
| 478 |
*/ |
| 479 |
#if HOST_BIGENDIAN |
| 480 |
TIFFSwabArrayOfLong(raster, width * height); |
| 481 |
#endif |
| 482 |
|
| 483 |
/* |
| 484 |
* Do we want to strip away alpha components? |
| 485 |
*/ |
| 486 |
if (no_alpha) |
| 487 |
{ |
| 488 |
size_t count = pixel_count; |
| 489 |
unsigned char *src, *dst; |
| 490 |
|
| 491 |
src = dst = (unsigned char *)raster; |
| 492 |
while (count > 0) |
| 493 |
{ |
| 494 |
/* do alpha compositing */ |
| 495 |
const int src_alpha = src[3]; |
| 496 |
const int background_contribution = background * (0xFF - src_alpha); |
| 497 |
*(dst++) = (*(src)*src_alpha + background_contribution) / 0xFF; |
| 498 |
src++; |
| 499 |
*(dst++) = (*(src)*src_alpha + background_contribution) / 0xFF; |
| 500 |
src++; |
| 501 |
*(dst++) = (*(src)*src_alpha + background_contribution) / 0xFF; |
| 502 |
src++; |
| 503 |
src++; |
| 504 |
count--; |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
/* |
| 509 |
* Write out the result in strips |
| 510 |
*/ |
| 511 |
for (row = 0; row < height; row += rowsperstrip) |
| 512 |
{ |
| 513 |
unsigned char *raster_strip; |
| 514 |
int rows_to_write; |
| 515 |
int bytes_per_pixel; |
| 516 |
|
| 517 |
if (no_alpha) |
| 518 |
{ |
| 519 |
raster_strip = ((unsigned char *)raster) + 3 * row * width; |
| 520 |
bytes_per_pixel = 3; |
| 521 |
} |
| 522 |
else |
| 523 |
{ |
| 524 |
raster_strip = (unsigned char *)(raster + row * width); |
| 525 |
bytes_per_pixel = 4; |
| 526 |
} |
| 527 |
|
| 528 |
if (row + rowsperstrip > height) |
| 529 |
rows_to_write = height - row; |
| 530 |
else |
| 531 |
rows_to_write = rowsperstrip; |
| 532 |
|
| 533 |
if (TIFFWriteEncodedStrip(out, row / rowsperstrip, raster_strip, |
| 534 |
bytes_per_pixel * rows_to_write * width) == |
| 535 |
-1) |
| 536 |
{ |
| 537 |
_TIFFfree(raster); |
| 538 |
return 0; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
_TIFFfree(raster); |
| 543 |
|
| 544 |
return 1; |
| 545 |
} |
| 546 |
|
| 547 |
static int tiffcvt(TIFF *in, TIFF *out) |
| 548 |
{ |
| 549 |
uint32_t width, height; /* image width & height */ |
| 550 |
uint16_t shortv; |
| 551 |
float floatv; |
| 552 |
char *stringv; |
| 553 |
uint32_t longv; |
| 554 |
uint16_t v[1]; |
| 555 |
|
| 556 |
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &width); |
| 557 |
TIFFGetField(in, TIFFTAG_IMAGELENGTH, &height); |
| 558 |
|
| 559 |
CopyField(TIFFTAG_SUBFILETYPE, longv); |
| 560 |
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width); |
| 561 |
TIFFSetField(out, TIFFTAG_IMAGELENGTH, height); |
| 562 |
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8); |
| 563 |
TIFFSetField(out, TIFFTAG_COMPRESSION, compression); |
| 564 |
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); |
| 565 |
|
| 566 |
CopyField(TIFFTAG_FILLORDER, shortv); |
| 567 |
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT); |
| 568 |
|
| 569 |
if (no_alpha) |
| 570 |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3); |
| 571 |
else |
| 572 |
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 4); |
| 573 |
|
| 574 |
if (!no_alpha) |
| 575 |
{ |
| 576 |
v[0] = EXTRASAMPLE_ASSOCALPHA; |
| 577 |
TIFFSetField(out, TIFFTAG_EXTRASAMPLES, 1, v); |
| 578 |
} |
| 579 |
|
| 580 |
CopyField(TIFFTAG_XRESOLUTION, floatv); |
| 581 |
CopyField(TIFFTAG_YRESOLUTION, floatv); |
| 582 |
CopyField(TIFFTAG_RESOLUTIONUNIT, shortv); |
| 583 |
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); |
| 584 |
TIFFSetField(out, TIFFTAG_SOFTWARE, TIFFGetVersion()); |
| 585 |
CopyField(TIFFTAG_DOCUMENTNAME, stringv); |
| 586 |
|
| 587 |
if (maxMalloc != 0 && TIFFStripSize(in) > maxMalloc) |
| 588 |
{ |
| 589 |
TIFFError(TIFFFileName(in), |
| 590 |
"Strip Size %" TIFF_SSIZE_FORMAT |
| 591 |
" over memory limit (%" TIFF_SSIZE_FORMAT ")", |
| 592 |
TIFFStripSize(in), maxMalloc); |
| 593 |
return 0; |
| 594 |
} |
| 595 |
if (process_by_block && TIFFIsTiled(in)) |
| 596 |
return (cvt_by_tile(in, out)); |
| 597 |
else if (process_by_block) |
| 598 |
return (cvt_by_strip(in, out)); |
| 599 |
else |
| 600 |
return (cvt_whole_image(in, out)); |
| 601 |
} |
| 602 |
|
| 603 |
static const char usage_info[] = |
| 604 |
/* Help information format modified for the sake of consistency with the |
| 605 |
other tiff tools */ |
| 606 |
/* "usage: tiff2rgba [-c comp] [-r rows] [-b] [-n] [-8] [-M size] |
| 607 |
input... output" */ |
| 608 |
/* "where comp is one of the following compression algorithms:" */ |
| 609 |
"Convert a TIFF image to RGBA color space\n\n" |
| 610 |
"usage: tiff2rgba [options] input output\n" |
| 611 |
"where options are:\n" |
| 612 |
#ifdef JPEG_SUPPORT |
| 613 |
" -c jpeg JPEG encoding\n" |
| 614 |
#endif |
| 615 |
#ifdef ZIP_SUPPORT |
| 616 |
" -c zip Zip/Deflate encoding\n" |
| 617 |
#endif |
| 618 |
#ifdef LZW_SUPPORT |
| 619 |
" -c lzw Lempel-Ziv & Welch encoding\n" |
| 620 |
#endif |
| 621 |
#ifdef PACKBITS_SUPPORT |
| 622 |
" -c packbits PackBits encoding\n" |
| 623 |
#endif |
| 624 |
#if defined(JPEG_SUPPORT) || defined(ZIP_SUPPORT) || defined(LZW_SUPPORT) || \ |
| 625 |
defined(PACKBITS_SUPPORT) |
| 626 |
" -c none no compression\n" |
| 627 |
#endif |
| 628 |
"\n" |
| 629 |
/* "and the other options are:\n" */ |
| 630 |
" -r rows/strip\n" |
| 631 |
" -t rows/strip (same as -r)\n" |
| 632 |
" -b (progress by block rather than as a whole image)\n" |
| 633 |
" -n don't emit alpha component.\n" |
| 634 |
" -B use this value as the background when doing alpha compositing\n" |
| 635 |
" -8 write BigTIFF file instead of ClassicTIFF\n" |
| 636 |
" -M set the memory allocation limit in MiB. 0 to disable limit\n"; |
| 637 |
|
| 638 |
static void usage(int code) |
| 639 |
{ |
| 640 |
FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr; |
| 641 |
|
| 642 |
fprintf(out, "%s\n\n", TIFFGetVersion()); |
| 643 |
fprintf(out, "%s", usage_info); |
| 644 |
exit(code); |
| 645 |
} |