1/*---------------------------------------------------------------------------* 2 | PDFlib - A library for generating PDF on the fly | 3 +---------------------------------------------------------------------------+ 4 | Copyright (c) 1997-2004 Thomas Merz and PDFlib GmbH. All rights reserved. | 5 +---------------------------------------------------------------------------+ 6 | | 7 | This software is subject to the PDFlib license. It is NOT in the | 8 | public domain. Extended versions and commercial licenses are | 9 | available, please check http://www.pdflib.com. | 10 | | 11 *---------------------------------------------------------------------------*/ 12 13/* $Id: p_tiff.c 14574 2005-10-29 16:27:43Z bonefish $ 14 * 15 * TIFF processing for PDFlib 16 * 17 */ 18 19#include "p_intern.h" 20#include "p_image.h" 21 22#ifndef HAVE_LIBTIFF 23 24pdc_bool 25pdf_is_TIFF_file(PDF *p, pdc_file *fp, pdf_tiff_info *tiff, pdc_false) 26{ 27 (void) p; 28 (void) fp; 29 (void) tiff; 30 31 return pdc_false; 32} 33 34int 35pdf_process_TIFF_data( 36 PDF *p, 37 int imageslot) 38{ 39 (void) imageslot; 40 41 pdc_warning(p->pdc, PDF_E_UNSUPP_IMAGE, "TIFF", 0, 0, 0); 42 return -1; 43} 44 45#else 46 47#include "tiffiop.h" 48static tsize_t 49pdf_libtiff_read(void* fd, tdata_t buf, tsize_t size) 50{ 51 pdc_file *fp = (pdc_file *) fd; 52 53 return ((tsize_t) pdc_fread(buf, 1, (size_t) size, fp)); 54} 55 56static toff_t 57pdf_libtiff_seek(void* fd, toff_t off, int whence) 58{ 59 pdc_file *fp = (pdc_file *) fd; 60 61 return ((toff_t) pdc_fseek(fp, (long) off, whence)); 62} 63 64static int 65pdf_libtiff_close(void* fd) 66{ 67 (void) fd; 68 69 /* pdc_fclose(fp); this happens in caller function */ 70 71 return 0; 72} 73 74static toff_t 75pdf_libtiff_size(void* fd) 76{ 77 pdc_file *fp = (pdc_file *) fd; 78 79 return (toff_t) pdc_file_size(fp); 80} 81 82static void * 83pdf_libtiff_malloc(TIFF *t, tsize_t size) 84{ 85 PDF *p = (PDF*) t->pdflib_opaque; 86 return pdc_malloc(p->pdc, (size_t)size, "libtiff"); 87} 88 89static void * 90pdf_libtiff_realloc(TIFF *t, tdata_t mem, tsize_t size) 91{ 92 PDF *p = (PDF*) t->pdflib_opaque; 93 return(pdc_realloc(p->pdc, (void*)mem, (size_t)size, "libtiff")); 94} 95 96static void 97pdf_libtiff_free(TIFF *t, tdata_t mem) 98{ 99 PDF *p = (PDF*) t->pdflib_opaque; 100 pdc_free(p->pdc, (void*)mem); 101} 102 103static void 104pdf_data_source_TIFF_init(PDF *p, PDF_data_source *src) 105{ 106 pdf_image *image; 107 108 image = (pdf_image *) src->private_data; 109 110 if (image->strips == 1) 111 image->info.tiff.cur_line = 0; 112 113 if (image->use_raw) { 114 /* malloc is done in the fill function */ 115 src->buffer_length = (size_t) 0; 116 src->buffer_start = (pdc_byte *) NULL; 117 } else { 118 src->buffer_length = (size_t) (image->components * image->width); 119 src->buffer_start = (pdc_byte *) 120 pdc_malloc(p->pdc, src->buffer_length, "pdf_data_source_TIFF_init"); 121 } 122} 123 124static pdc_bool 125pdf_data_source_TIFF_fill(PDF *p, PDF_data_source *src) 126{ 127 pdf_image *image; 128 int col; 129 pdc_byte *dest; 130 uint16 fillorder; 131 uint32 *s, *bc; 132 133 image = (pdf_image *) src->private_data; 134 135 if (image->use_raw) { 136 if (image->info.tiff.cur_line == image->strips) 137 return pdc_false; 138 139 TIFFGetField(image->info.tiff.tif, TIFFTAG_STRIPBYTECOUNTS, &bc); 140 141 if (bc[image->info.tiff.cur_line] > src->buffer_length) { 142 src->buffer_length = bc[image->info.tiff.cur_line]; 143 src->buffer_start = (pdc_byte *) 144 pdc_realloc(p->pdc, src->buffer_start, 145 src->buffer_length, "pdf_data_source_TIFF_fill"); 146 } 147 148 if (TIFFReadRawStrip(image->info.tiff.tif, 149 (tstrip_t) image->info.tiff.cur_line, 150 (tdata_t) src->buffer_start, 151 (tsize_t) bc[image->info.tiff.cur_line]) == -1) { 152 153 pdc_free(p->pdc, (void *) src->buffer_start); 154 TIFFClose(image->info.tiff.tif); 155 image->fp = NULL; 156 pdc_error(p->pdc, PDF_E_IMAGE_CORRUPT, "TIFF", "?", 0, 0); 157 } 158 159 src->next_byte = src->buffer_start; 160 src->bytes_available = bc[image->info.tiff.cur_line]; 161 162 if (TIFFGetField(image->info.tiff.tif, TIFFTAG_FILLORDER, &fillorder) 163 && (fillorder == FILLORDER_LSB2MSB)) { 164 TIFFReverseBits((unsigned char *) src->buffer_start, 165 (unsigned long) src->bytes_available); 166 } 167 168 if (image->strips > 1) { 169 /* only a single strip of a multi-strip image */ 170 image->info.tiff.cur_line = image->strips; 171 } else 172 image->info.tiff.cur_line++; 173 174 } else { 175 if (image->info.tiff.cur_line++ == image->height) 176 return pdc_false; 177 178 src->next_byte = src->buffer_start; 179 src->bytes_available = src->buffer_length; 180 181 dest = src->buffer_start; 182 s = image->info.tiff.raster + 183 ((int)image->height - image->info.tiff.cur_line) * 184 (int) image->width; 185 186 switch (image->components) { 187 case 1: 188 for (col = 0; col < image->width; col++, s++) { 189 *dest++ = (pdc_byte) TIFFGetR(*s); 190 } 191 break; 192 193 case 3: 194 for (col = 0; col < image->width; col++, s++) { 195 *dest++ = (pdc_byte) TIFFGetR(*s); 196 *dest++ = (pdc_byte) TIFFGetG(*s); 197 *dest++ = (pdc_byte) TIFFGetB(*s); 198 } 199 break; 200 201 case 4: 202 for (col = 0; col < image->width; col++, s++) { 203 unsigned char* t = (unsigned char*)&(*s); 204 *dest++ = (pdc_byte) t[0]; 205 *dest++ = (pdc_byte) t[1]; 206 *dest++ = (pdc_byte) t[2]; 207 *dest++ = (pdc_byte) t[3]; 208 } 209 break; 210 211 default: 212 pdc_error(p->pdc, PDF_E_IMAGE_BADCOMP, 213 pdc_errprintf(p->pdc, "%d", image->components), 214 image->filename, 0, 0); 215 } 216 } 217 218 return pdc_true; 219} 220 221static void 222pdf_data_source_TIFF_terminate(PDF *p, PDF_data_source *src) 223{ 224 pdc_free(p->pdc, (void *) src->buffer_start); 225} 226 227static int 228pdf_check_colormap(int n, uint16* r, uint16* g, uint16* b) 229{ 230 while (n-- > 0) 231 if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256) 232 return(16); 233 return(8); 234} 235 236pdc_bool 237pdf_is_TIFF_file(PDF *p, pdc_file *fp, pdf_tiff_info *tiff_info, pdc_bool check) 238{ 239 const char *filename; 240 241 /* Suppress all warnings and error messages */ 242 (void) TIFFSetErrorHandler(NULL); 243 (void) TIFFSetWarningHandler(NULL); 244 245 filename = pdc_file_name(fp); 246 tiff_info->tif = TIFFClientOpen(filename, "r", 247 (void *)fp, 248 pdf_libtiff_read, NULL, 249 pdf_libtiff_seek, pdf_libtiff_close, pdf_libtiff_size, 250 NULL, NULL, (void *)p, 251 pdf_libtiff_malloc, pdf_libtiff_realloc, pdf_libtiff_free, 252 NULL, NULL); 253 if (tiff_info->tif == NULL) { 254 pdc_fseek(fp, 0L, SEEK_SET); 255 return pdc_false; 256 } 257 if (check) 258 TIFFClose(tiff_info->tif); 259 return pdc_true; 260} 261 262int 263pdf_process_TIFF_data( 264 PDF *p, 265 int imageslot) 266{ 267 static const char *fn = "pdf_process_TIFF_data"; 268 uint32 w, h; 269 uint16 unit, bpc, compression, photometric, inkset, extra, *sinfo; 270 uint16 planarconfig, predictor; 271 uint16 *rmap, *gmap, *bmap; 272 uint32 group3opts; 273 tsample_t components; 274 size_t npixels; 275 pdf_image *image; 276 float res_x, res_y; 277 pdf_colorspace cs; 278 int slot; 279 int strip; 280 int errint = 0; 281 int errcode = 0; 282 pdc_bool isopen = pdc_false; 283 284 image = &p->images[imageslot]; 285 286 if (pdf_is_TIFF_file(p, image->fp, &image->info.tiff, pdc_false) 287 == pdc_false) { 288 errcode = PDF_E_IMAGE_CORRUPT; 289 goto PDF_TIFF_ERROR; 290 } 291 image->info.tiff.tif->tif_fd = (FILE*)image->fp; 292 isopen = pdc_true; 293 294 if (image->page != 1) { 295 if (TIFFSetDirectory(image->info.tiff.tif, (tdir_t) (image->page - 1)) 296 != 1) { 297 errint = image->page; 298 errcode = PDF_E_IMAGE_NOPAGE; 299 goto PDF_TIFF_ERROR; 300 } 301 } 302 303 TIFFGetField(image->info.tiff.tif, TIFFTAG_COMPRESSION, &compression); 304 305 if (compression == COMPRESSION_OJPEG || 306 compression == COMPRESSION_JPEG) { 307 /* can't handle these yet */ 308 errint = compression; 309 errcode = PDF_E_TIFF_UNSUPP_JPEG; 310 goto PDF_TIFF_ERROR; 311 } 312 313 photometric = 255; /* dummy value */ 314 315 if (TIFFGetField(image->info.tiff.tif, TIFFTAG_PHOTOMETRIC, &photometric) && 316 (photometric == PHOTOMETRIC_YCBCR || 317 photometric == PHOTOMETRIC_CIELAB || 318 photometric == PHOTOMETRIC_MASK)) { 319 /* can't handle these */ 320 errint = photometric; 321 errcode = PDF_E_TIFF_UNSUPP_CS; 322 goto PDF_TIFF_ERROR; 323 } 324 325 TIFFGetField(image->info.tiff.tif, TIFFTAG_IMAGEWIDTH, &w); 326 TIFFGetField(image->info.tiff.tif, TIFFTAG_IMAGELENGTH, &h); 327 TIFFGetFieldDefaulted(image->info.tiff.tif, TIFFTAG_BITSPERSAMPLE, &bpc); 328 TIFFGetFieldDefaulted(image->info.tiff.tif, TIFFTAG_SAMPLESPERPIXEL, 329 &components); 330 TIFFGetFieldDefaulted(image->info.tiff.tif, TIFFTAG_EXTRASAMPLES, 331 &extra, &sinfo); 332 TIFFGetFieldDefaulted(image->info.tiff.tif, TIFFTAG_PLANARCONFIG, 333 &planarconfig); 334 335 if (bpc != 1 && bpc != 2 && bpc != 4 && bpc != 8) { 336 /* PDF doesn't support other values of the color depth */ 337 errint = bpc; 338 errcode = PDF_E_IMAGE_BADDEPTH; 339 goto PDF_TIFF_ERROR; 340 } 341 342 image->width = (float) w; 343 image->height = (float) h; 344 image->components = components; 345 image->strips = 1; 346 image->bpc = bpc; 347 348 /* fetch the resolution values if found in the file */ 349 if (TIFFGetField(image->info.tiff.tif, TIFFTAG_XRESOLUTION, &res_x) && 350 TIFFGetField(image->info.tiff.tif, TIFFTAG_YRESOLUTION, &res_y) && 351 TIFFGetFieldDefaulted(image->info.tiff.tif, TIFFTAG_RESOLUTIONUNIT, 352 &unit) && 353 res_x > 0 && res_y > 0) { 354 355 if (unit == RESUNIT_INCH) { 356 image->dpi_x = res_x; 357 image->dpi_y = res_y; 358 359 } else if (unit == RESUNIT_CENTIMETER) { 360 image->dpi_x = res_x * (float) 2.54; 361 image->dpi_y = res_y * (float) 2.54; 362 363 } else if (unit == RESUNIT_NONE) { 364 image->dpi_x = -res_x; 365 image->dpi_y = -res_y; 366 } 367 } 368 369 if (compression != COMPRESSION_LZW && p->debug['P']) 370 compression = (uint16) 0; /* dummy: disable pass-through mode */ 371 372 /* find out whether we can use the compressed raw data directly */ 373 switch ((int) compression) { 374 case COMPRESSION_CCITTRLE: 375 case COMPRESSION_CCITTRLEW: 376 if (TIFFIsTiled(image->info.tiff.tif)) { 377 image->use_raw = pdc_false; 378 image->bpc = 8; 379 break; 380 } 381 382 image->params = (char *) pdc_malloc(p->pdc, PDF_MAX_PARAMSTRING, 383 fn); 384 385 strcpy(image->params, "/EndOfBlock false"); 386 strcat(image->params, "/EncodedByteAlign true"); 387 388 if (photometric == PHOTOMETRIC_MINISBLACK) 389 strcat(image->params, "/BlackIs1 true"); 390 391 image->strips = (int) TIFFNumberOfStrips(image->info.tiff.tif); 392 image->compression = ccitt; 393 image->bpc = 1; 394 image->use_raw = pdc_true; 395 break; 396 397 case COMPRESSION_CCITTFAX3: 398 if (TIFFIsTiled(image->info.tiff.tif)) { 399 image->use_raw = pdc_false; 400 image->bpc = 8; 401 break; 402 } 403 404 image->params = (char *) pdc_malloc(p->pdc, PDF_MAX_PARAMSTRING, 405 fn); 406 strcpy(image->params, ""); 407 408 /* The following contains three code segments which are 409 * disabled. 410 * Apparently, and contrary to my reading of the specs, 411 * the following can not be deduced from the respective 412 * TIFF entry or option: 413 * - I expected /EndOfBlock and /EndOfLine to be always 414 * true for CCITTFAX3 images. 415 * 416 * - /EncodedByteAlign can not reliably be deduced from 417 * GROUP3OPT_FILLBITS; 418 * 419 * - /BlackIs1 can not reliably be deduced from 420 * PHOTOMETRIC_MINISBLACK; 421 * 422 * From practical experience, the respective lines are 423 * disabled, but I don't have any clear explanation for this. 424 * A few TIFF images still don't work with this setting, 425 * unfortunately. 426 */ 427 428 /* SEE ABOVE! 429 strcpy(image->params, "/EndOfBlock false"); 430 strcat(image->params, "/EndOfLine true"); 431 */ 432 /* 433 strcat(image->params, "/DamagedRowsBeforeError 1"); 434 */ 435 436 if (TIFFGetField(image->info.tiff.tif, TIFFTAG_GROUP3OPTIONS, 437 &group3opts)) { 438 /* /K = 0 (= G3,1D) is default */ 439 if (group3opts & GROUP3OPT_2DENCODING) 440 strcat(image->params, "/K 1"); 441 442 /* SEE ABOVE! 443 if (group3opts & GROUP3OPT_FILLBITS) 444 strcat(image->params, "/EncodedByteAlign true"); 445 */ 446 } 447 448 /* SEE ABOVE! 449 if ((photometric == PHOTOMETRIC_MINISBLACK)) 450 strcat(image->params, "/BlackIs1 true"); 451 */ 452 453 image->strips = (int) TIFFNumberOfStrips(image->info.tiff.tif); 454 image->compression = ccitt; 455 image->bpc = 1; 456 image->use_raw = pdc_true; 457 break; 458 459 case COMPRESSION_CCITTFAX4: 460 if (TIFFIsTiled(image->info.tiff.tif)) { 461 image->use_raw = pdc_false; 462 image->bpc = 8; 463 break; 464 } 465 466 image->params = (char *) pdc_malloc(p->pdc, PDF_MAX_PARAMSTRING, 467 fn); 468 469 strcpy(image->params, "/K -1"); 470 471 if (photometric == PHOTOMETRIC_MINISBLACK) 472 strcat(image->params, "/BlackIs1 true"); 473 474 image->strips = (int) TIFFNumberOfStrips(image->info.tiff.tif); 475 image->compression = ccitt; 476 image->bpc = 1; 477 image->use_raw = pdc_true; 478 break; 479 480 case COMPRESSION_NONE: 481 /* 482 * can't use multiple data sources in raw mode, or deal with tiles 483 */ 484 if ((planarconfig == PLANARCONFIG_SEPARATE && components > 1) || 485 TIFFIsTiled(image->info.tiff.tif)) { 486 image->use_raw = pdc_false; 487 image->bpc = 8; 488 break; 489 } 490 491 if (extra != 0) 492 image->components -= extra; /* ignore the extra channels */ 493 494 image->use_raw = pdc_false; 495 image->bpc = 8; 496 break; 497 498 case COMPRESSION_LZW: 499 500 if (TIFFGetField(image->info.tiff.tif, TIFFTAG_PREDICTOR, 501 &predictor)) { 502 if (predictor != pred_default && predictor != pred_tiff) { 503 errint = predictor; 504 errcode = PDF_E_TIFF_UNSUPP_PREDICT; 505 goto PDF_TIFF_ERROR; 506 } else 507 image->predictor = (pdf_predictor) predictor; 508 } 509 510 if (TIFFIsTiled(image->info.tiff.tif)) { 511 errcode = PDF_E_TIFF_UNSUPP_LZW_TILED; 512 goto PDF_TIFF_ERROR; 513 } 514 515 if (planarconfig == PLANARCONFIG_SEPARATE && components > 1) { 516 errcode = PDF_E_TIFF_UNSUPP_LZW_PLANES; 517 goto PDF_TIFF_ERROR; 518 } 519 520 if (extra != 0) { 521 errcode = PDF_E_TIFF_UNSUPP_LZW_ALPHA; 522 goto PDF_TIFF_ERROR; 523 } 524 525 if (photometric == PHOTOMETRIC_MINISWHITE) 526 image->invert = !image->invert; 527 528 image->strips = (int) TIFFNumberOfStrips(image->info.tiff.tif); 529 image->compression = lzw; 530 image->use_raw = pdc_true; 531 break; 532 533 case COMPRESSION_PACKBITS: 534 /* 535 * can't pass through extra bits or use multiple data sources 536 * in raw mode 537 */ 538 if (extra != 0 || 539 (planarconfig == PLANARCONFIG_SEPARATE && components > 1) || 540 TIFFIsTiled(image->info.tiff.tif)) { 541 image->use_raw = pdc_false; 542 image->bpc = 8; 543 break; 544 } 545 546 if (photometric == PHOTOMETRIC_MINISWHITE) 547 image->invert = !image->invert; 548 549 image->strips = (int) TIFFNumberOfStrips(image->info.tiff.tif); 550 image->compression = runlength; 551 image->use_raw = pdc_true; 552 break; 553 554 case COMPRESSION_DEFLATE: 555 case COMPRESSION_ADOBE_DEFLATE: 556 if (extra != 0 || 557 (planarconfig == PLANARCONFIG_SEPARATE && components > 1) || 558 TIFFIsTiled(image->info.tiff.tif)) { 559 image->components -= extra; /* ignore the extra channels */ 560 image->use_raw = pdc_false; 561 image->bpc = 8; 562 break; 563 } 564 565 if (TIFFGetField(image->info.tiff.tif, TIFFTAG_PREDICTOR, 566 &predictor)) { 567 if (predictor != pred_default && predictor != pred_tiff) { 568 errint = predictor; 569 errcode = PDF_E_TIFF_UNSUPP_PREDICT; 570 goto PDF_TIFF_ERROR; 571 } else 572 image->predictor = (pdf_predictor) predictor; 573 } 574 575 image->strips = (int) TIFFNumberOfStrips(image->info.tiff.tif); 576 image->compression = flate; 577 image->use_raw = pdc_true; 578 break; 579 580#ifdef PDF_NYI 581 /* dead code, doesn't work yet */ 582 case COMPRESSION_OJPEG: 583 case COMPRESSION_JPEG: 584 if (TIFFIsTiled(image->info.tiff.tif)) { 585 errcode = PDF_E_TIFF_UNSUPP_JPEG_TILED; 586 goto PDF_TIFF_ERROR; 587 } 588 589 if (extra != 0 || 590 (planarconfig == PLANARCONFIG_SEPARATE && components > 1) || 591 TIFFIsTiled(image->info.tiff.tif)) { 592 errcode = PDF_E_UNSUPP_JPEG_ALPHA; 593 goto PDF_TIFF_ERROR; 594 } 595 596 image->strips = (int) TIFFNumberOfStrips(image->info.tiff.tif); 597 image->compression = dct; 598 image->use_raw = pdc_true; 599 break; 600#endif /* !PDF_NYI */ 601 602 default: 603 image->use_raw = pdc_false; 604 image->bpc = 8; 605 } 606 607 /* palette images are automatically converted to RGB by tifflib */ 608 if (image->components == 1 && !image->use_raw && 609 TIFFGetField(image->info.tiff.tif, TIFFTAG_COLORMAP, 610 &rmap, &gmap, &bmap)) { 611 image->components = 3; 612 } 613 614 if (image->imagemask) 615 { 616 if (image->components != 1) { 617 errcode = PDF_E_IMAGE_BADMASK; 618 goto PDF_TIFF_ERROR; 619 } 620 621 if (p->compatibility <= PDC_1_3) { 622 if (image->components != 1 || image->bpc != 1) { 623 errcode = PDF_E_IMAGE_MASK1BIT13; 624 goto PDF_TIFF_ERROR; 625 } 626 } else if (image->bpc > 1) { 627 /* images with more than one bit will be written as /SMask, 628 * and don't require an /ImageMask entry. 629 */ 630 image->imagemask = pdc_false; 631 } 632 } 633 634 if (image->mask != pdc_undef) 635 { 636 if (image->strips != 1) { 637 errcode = PDF_E_TIFF_MASK_MULTISTRIP; 638 goto PDF_TIFF_ERROR; 639 } 640 } 641 642 if (image->colorspace == pdc_undef) 643 { 644 switch (image->components) { 645 case 1: 646 image->colorspace = DeviceGray; 647 break; 648 649 case 3: 650 if (photometric == PHOTOMETRIC_CIELAB) { 651 errint = photometric; 652 errcode = PDF_E_TIFF_UNSUPP_CS; 653 goto PDF_TIFF_ERROR; 654 } else 655 image->colorspace = DeviceRGB; 656 break; 657 658 case 4: 659 if (photometric == PHOTOMETRIC_SEPARATED) { 660 TIFFGetFieldDefaulted(image->info.tiff.tif, TIFFTAG_INKSET, 661 &inkset); 662 if (inkset != INKSET_CMYK) { 663 errint = inkset; 664 errcode = PDF_E_TIFF_UNSUPP_SEP_NONCMYK; 665 goto PDF_TIFF_ERROR; 666 } 667 image->colorspace = DeviceCMYK; 668 } else { 669 /* if it's not separated it must be RGB with alpha */ 670 image->components = 3; 671 image->colorspace = DeviceRGB; 672 image->compression = none; 673 } 674 break; 675 676 default: 677 errint = image->components; 678 errcode = PDF_E_IMAGE_BADCOMP; 679 goto PDF_TIFF_ERROR; 680 } 681 } 682 683 684 image->src.private_data = (void *) image; 685 image->src.init = pdf_data_source_TIFF_init; 686 image->src.fill = pdf_data_source_TIFF_fill; 687 image->src.terminate = pdf_data_source_TIFF_terminate; 688 689 if (image->use_raw) { 690 uint32 row, rowsperstrip; 691 692 /* must handle colormap ourselves */ 693 if (photometric == PHOTOMETRIC_PALETTE) { 694 int i; 695 pdf_colormap colormap; 696 697 if (!TIFFGetField(image->info.tiff.tif, TIFFTAG_COLORMAP, 698 &rmap, &gmap, &bmap)) { 699 errcode = PDF_E_IMAGE_COLORMAP; 700 goto PDF_TIFF_ERROR; 701 } 702 703 cs.type = Indexed; 704 cs.val.indexed.palette_size = 1 << bpc; 705 cs.val.indexed.colormap = &colormap; 706 cs.val.indexed.colormap_id = PDC_BAD_ID; 707 708 cs.val.indexed.base = DeviceRGB; 709 710#define CVT(x) (uint16) (((x) * 255) / ((1L<<16)-1)) 711 if (pdf_check_colormap(cs.val.indexed.palette_size, 712 rmap, gmap, bmap) == 16) 713 { 714 /* convert colormap to 8 bit values */ 715 for (i = 0; i < cs.val.indexed.palette_size; i++) { 716 rmap[i] = CVT(rmap[i]); 717 gmap[i] = CVT(gmap[i]); 718 bmap[i] = CVT(bmap[i]); 719 } 720 } 721#undef CVT 722 723 for (i = 0; i < cs.val.indexed.palette_size; i++) { 724 colormap[i][0] = (pdc_byte) rmap[i]; 725 colormap[i][1] = (pdc_byte) gmap[i]; 726 colormap[i][2] = (pdc_byte) bmap[i]; 727 } 728 729 image->components = 1; 730 731 slot = pdf_add_colorspace(p, &cs, pdc_false); 732 image->colorspace = (pdf_colorspacetype) slot; 733 734 735 } 736 737 738 if (image->strips > image->height) 739 image->strips = (int) image->height; 740 741 if (TIFFGetFieldDefaulted(image->info.tiff.tif, 742 TIFFTAG_ROWSPERSTRIP, &rowsperstrip) == 1 && (int) rowsperstrip 743 != -1) 744 image->rowsperstrip = (int) rowsperstrip; 745 else 746 image->rowsperstrip = (int) image->height; 747 748 /* 749 * The first strip must be handled separately because it carries the 750 * colormap for indexed images. Other strips reuse this colormap. 751 */ 752 image->info.tiff.cur_line = 0; 753 image->height = (float) 754 (image->rowsperstrip > (int) h ? (int) h : image->rowsperstrip); 755 pdf_put_image(p, imageslot, pdc_true); 756 757 for (row = (uint32) image->rowsperstrip, strip = 1; 758 row < h; row += (uint32) image->rowsperstrip, strip++) { 759 760 image->height = (float) (row+image->rowsperstrip > h ? 761 (int) (h - row) : image->rowsperstrip); 762 763 /* 764 * tell pdf_data_source_TIFF_fill() to read only data of the 765 * current strip 766 */ 767 image->info.tiff.cur_line = strip; 768 pdf_put_image(p, imageslot, pdc_false); 769 } 770 771 image->height = (float) h; 772 image->no -= (image->strips - 1); /* number of first strip */ 773 774 } else { 775 776 777 image->info.tiff.raster = (uint32 *) NULL; 778 779 if (p->colorspaces[image->colorspace].type != DeviceCMYK && 780 !(p->colorspaces[image->colorspace].type == ICCBased && 781 image->components == 4) && 782 p->colorspaces[image->colorspace].type != Lab) { 783 npixels = (size_t) (w * h); 784 image->info.tiff.raster = (uint32 *) pdc_malloc(p->pdc, 785 (size_t) (npixels * sizeof (uint32)), "pdf_open_TIFF"); 786 787 if (!TIFFReadRGBAImage(image->info.tiff.tif, 788 w, h, image->info.tiff.raster, 1)) { 789 pdc_free(p->pdc, (void *) image->info.tiff.raster); 790 errcode = PDC_E_IO_NODATA; 791 goto PDF_TIFF_ERROR; 792 } 793 } else { 794 int linecounter = 0; 795 796 npixels = (size_t) (TIFFScanlineSize(image->info.tiff.tif) * h); 797 image->info.tiff.raster = (uint32 *) 798 pdc_malloc(p->pdc, (size_t) npixels, "pdf_open_TIFF"); 799 800 while (linecounter < image->height) { 801 if (TIFFReadScanline(image->info.tiff.tif, 802 (tdata_t) (image->info.tiff.raster + 803 ((int)image->height - linecounter - 1) * (int)image->width), 804 (uint32) linecounter, (tsample_t) 0) == -1) { 805 806 pdc_free(p->pdc, (void *) image->info.tiff.raster); 807 errcode = PDC_E_IO_NODATA; 808 goto PDF_TIFF_ERROR; 809 } 810 linecounter++; 811 } 812 } 813 814 pdf_put_image(p, imageslot, pdc_true); 815 816 if (image->info.tiff.raster) 817 pdc_free(p->pdc, (void *) image->info.tiff.raster); 818 } 819 820 image->in_use = pdc_true; /* mark slot as used */ 821 822 TIFFClose(image->info.tiff.tif); 823 824 return imageslot; 825 826 PDF_TIFF_ERROR: 827 if (isopen) 828 TIFFClose(image->info.tiff.tif); 829 { 830 const char *stemp = pdc_errprintf(p->pdc, "%s", image->filename); 831 switch (errcode) 832 { 833 case PDC_E_IO_NODATA: 834 case PDF_E_IMAGE_ICC: 835 case PDF_E_IMAGE_ICC2: 836 case PDF_E_IMAGE_MASK1BIT13: 837 case PDF_E_TIFF_UNSUPP_LZW_TILED: 838 case PDF_E_TIFF_UNSUPP_LZW_PLANES: 839 case PDF_E_TIFF_UNSUPP_LZW_ALPHA: 840 case PDF_E_TIFF_UNSUPP_JPEG_TILED: 841 case PDF_E_TIFF_UNSUPP_JPEG_ALPHA: 842 case PDF_E_IMAGE_COLORIZE: 843 case PDF_E_TIFF_MASK_MULTISTRIP: 844 case PDF_E_IMAGE_COLORMAP: 845 case PDF_E_IMAGE_BADMASK: 846 pdc_set_errmsg(p->pdc, errcode, stemp, 0, 0, 0); 847 break; 848 849 case PDF_E_IMAGE_CORRUPT: 850 pdc_set_errmsg(p->pdc, errcode, "TIFF", stemp, 0, 0); 851 break; 852 853 case PDF_E_IMAGE_BADDEPTH: 854 case PDF_E_IMAGE_BADCOMP: 855 pdc_set_errmsg(p->pdc, errcode, 856 pdc_errprintf(p->pdc, "%d", errint), stemp, 0, 0); 857 break; 858 859 case PDF_E_IMAGE_NOPAGE: 860 pdc_set_errmsg(p->pdc, errcode, 861 pdc_errprintf(p->pdc, "%d", errint), "TIFF", stemp, 0); 862 break; 863 864 case PDF_E_TIFF_UNSUPP_JPEG: 865 case PDF_E_TIFF_UNSUPP_CS: 866 case PDF_E_TIFF_UNSUPP_PREDICT: 867 case PDF_E_TIFF_UNSUPP_SEP_NONCMYK: 868 pdc_set_errmsg(p->pdc, errcode, 869 stemp, pdc_errprintf(p->pdc, "%d", errint), 0, 0); 870 break; 871 872 case 0: /* error code and message already set */ 873 break; 874 } 875 } 876 877 if (image->verbose) 878 pdc_error(p->pdc, -1, 0, 0, 0, 0); 879 880 return -1; 881} 882 883#endif /* HAVE_LIBTIFF */ 884