| 1 |
/* |
| 2 |
* |
| 3 |
* tiff2pdf - converts a TIFF image to a PDF document |
| 4 |
* |
| 5 |
* Copyright (c) 2003 Ross Finlayson |
| 6 |
* |
| 7 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 8 |
* its documentation for any purpose is hereby granted without fee, provided |
| 9 |
* that (i) the above copyright notices and this permission notice appear in |
| 10 |
* all copies of the software and related documentation, and (ii) the name of |
| 11 |
* Ross Finlayson may not be used in any advertising or |
| 12 |
* publicity relating to the software without the specific, prior written |
| 13 |
* permission of Ross Finlayson. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 16 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 17 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 18 |
* |
| 19 |
* IN NO EVENT SHALL ROSS FINLAYSON BE LIABLE FOR |
| 20 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 21 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 22 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 23 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 24 |
* OF THIS SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <ctype.h> |
| 28 |
#include <errno.h> |
| 29 |
#include <limits.h> |
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <string.h> |
| 33 |
#include <time.h> |
| 34 |
|
| 35 |
#if HAVE_UNISTD_H |
| 36 |
#include <unistd.h> |
| 37 |
#endif |
| 38 |
|
| 39 |
#ifdef HAVE_FCNTL_H |
| 40 |
#include <fcntl.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
#ifdef HAVE_IO_H |
| 44 |
#include <io.h> |
| 45 |
#endif |
| 46 |
|
| 47 |
#include "libport.h" |
| 48 |
#include "tif_config.h" |
| 49 |
#include "tiffio.h" |
| 50 |
#include "tiffiop.h" |
| 51 |
|
| 52 |
#ifndef EXIT_SUCCESS |
| 53 |
#define EXIT_SUCCESS 0 |
| 54 |
#endif |
| 55 |
#ifndef EXIT_FAILURE |
| 56 |
#define EXIT_FAILURE 1 |
| 57 |
#endif |
| 58 |
|
| 59 |
#define TIFF2PDF_MODULE "tiff2pdf" |
| 60 |
|
| 61 |
#define PS_UNIT_SIZE 72.0F |
| 62 |
|
| 63 |
#define TIFF_DIR_MAX 65534 |
| 64 |
|
| 65 |
#define DEFAULT_MAX_MALLOC (256 * 1024 * 1024) |
| 66 |
|
| 67 |
/* This type is of PDF color spaces. */ |
| 68 |
typedef enum |
| 69 |
{ |
| 70 |
T2P_CS_BILEVEL = 0x01, /* Bilevel, black and white */ |
| 71 |
T2P_CS_GRAY = 0x02, /* Single channel */ |
| 72 |
T2P_CS_RGB = 0x04, /* Three channel tristimulus RGB */ |
| 73 |
T2P_CS_CMYK = 0x08, /* Four channel CMYK print inkset */ |
| 74 |
T2P_CS_LAB = 0x10, /* Three channel L*a*b* color space */ |
| 75 |
T2P_CS_PALETTE = 0x1000, /* One of the above with a color map */ |
| 76 |
T2P_CS_CALGRAY = 0x20, /* Calibrated single channel */ |
| 77 |
T2P_CS_CALRGB = 0x40, /* Calibrated three channel tristimulus RGB */ |
| 78 |
T2P_CS_ICCBASED = 0x80 /* ICC profile color specification */ |
| 79 |
} t2p_cs_t; |
| 80 |
|
| 81 |
/* This type is of PDF compression types. */ |
| 82 |
typedef enum |
| 83 |
{ |
| 84 |
T2P_COMPRESS_NONE = 0x00 |
| 85 |
#ifdef CCITT_SUPPORT |
| 86 |
, |
| 87 |
T2P_COMPRESS_G4 = 0x01 |
| 88 |
#endif |
| 89 |
#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT) |
| 90 |
, |
| 91 |
T2P_COMPRESS_JPEG = 0x02 |
| 92 |
#endif |
| 93 |
#ifdef ZIP_SUPPORT |
| 94 |
, |
| 95 |
T2P_COMPRESS_ZIP = 0x04 |
| 96 |
#endif |
| 97 |
} t2p_compress_t; |
| 98 |
|
| 99 |
/* This type is whether TIFF image data can be used in PDF without transcoding. |
| 100 |
*/ |
| 101 |
typedef enum |
| 102 |
{ |
| 103 |
T2P_TRANSCODE_RAW = 0x01, /* The raw data from the input can be used without |
| 104 |
recompressing */ |
| 105 |
T2P_TRANSCODE_ENCODE = |
| 106 |
0x02 /* The data from the input is perhaps unencoded and reencoded */ |
| 107 |
} t2p_transcode_t; |
| 108 |
|
| 109 |
/* This type is of information about the data samples of the input image. */ |
| 110 |
typedef enum |
| 111 |
{ |
| 112 |
T2P_SAMPLE_NOTHING = |
| 113 |
0x0000, /* The unencoded samples are normal for the output colorspace */ |
| 114 |
T2P_SAMPLE_ABGR_TO_RGB = |
| 115 |
0x0001, /* The unencoded samples are the result of ReadRGBAImage */ |
| 116 |
T2P_SAMPLE_RGBA_TO_RGB = |
| 117 |
0x0002, /* The unencoded samples are contiguous RGBA */ |
| 118 |
T2P_SAMPLE_RGBAA_TO_RGB = |
| 119 |
0x0004, /* The unencoded samples are RGBA with premultiplied alpha */ |
| 120 |
T2P_SAMPLE_YCBCR_TO_RGB = 0x0008, |
| 121 |
T2P_SAMPLE_YCBCR_TO_LAB = 0x0010, |
| 122 |
T2P_SAMPLE_REALIZE_PALETTE = |
| 123 |
0x0020, /* The unencoded samples are indexes into the color map */ |
| 124 |
T2P_SAMPLE_SIGNED_TO_UNSIGNED = |
| 125 |
0x0040, /* The unencoded samples are signed instead of unsignd */ |
| 126 |
T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED = |
| 127 |
0x0040, /* The L*a*b* samples have a* and b* signed */ |
| 128 |
T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG = |
| 129 |
0x0100 /* The unencoded samples are separate instead of contiguous */ |
| 130 |
} t2p_sample_t; |
| 131 |
|
| 132 |
/* This type is of error status of the T2P struct. */ |
| 133 |
typedef enum |
| 134 |
{ |
| 135 |
T2P_ERR_OK = |
| 136 |
0, /* This is the value of t2p->t2p_error when there is no error */ |
| 137 |
T2P_ERR_ERROR = |
| 138 |
1 /* This is the value of t2p->t2p_error when there was an error */ |
| 139 |
} t2p_err_t; |
| 140 |
|
| 141 |
/* This struct defines a logical page of a TIFF. */ |
| 142 |
typedef struct |
| 143 |
{ |
| 144 |
tdir_t page_directory; |
| 145 |
uint32_t page_number; |
| 146 |
ttile_t page_tilecount; |
| 147 |
uint32_t page_extra; |
| 148 |
} T2P_PAGE; |
| 149 |
|
| 150 |
/* This struct defines a PDF rectangle's coordinates. */ |
| 151 |
typedef struct |
| 152 |
{ |
| 153 |
float x1; |
| 154 |
float y1; |
| 155 |
float x2; |
| 156 |
float y2; |
| 157 |
float mat[9]; |
| 158 |
} T2P_BOX; |
| 159 |
|
| 160 |
/* This struct defines a tile of a PDF. */ |
| 161 |
typedef struct |
| 162 |
{ |
| 163 |
T2P_BOX tile_box; |
| 164 |
} T2P_TILE; |
| 165 |
|
| 166 |
/* This struct defines information about the tiles on a PDF page. */ |
| 167 |
typedef struct |
| 168 |
{ |
| 169 |
ttile_t tiles_tilecount; |
| 170 |
uint32_t tiles_tilewidth; |
| 171 |
uint32_t tiles_tilelength; |
| 172 |
uint32_t tiles_tilecountx; |
| 173 |
uint32_t tiles_tilecounty; |
| 174 |
uint32_t tiles_edgetilewidth; |
| 175 |
uint32_t tiles_edgetilelength; |
| 176 |
T2P_TILE *tiles_tiles; |
| 177 |
} T2P_TILES; |
| 178 |
|
| 179 |
/* This struct is the context of a function to generate PDF from a TIFF. */ |
| 180 |
typedef struct |
| 181 |
{ |
| 182 |
t2p_err_t t2p_error; |
| 183 |
T2P_PAGE *tiff_pages; |
| 184 |
T2P_TILES *tiff_tiles; |
| 185 |
tdir_t tiff_pagecount; |
| 186 |
uint16_t tiff_compression; |
| 187 |
uint16_t tiff_photometric; |
| 188 |
uint16_t tiff_fillorder; |
| 189 |
uint16_t tiff_bitspersample; |
| 190 |
uint16_t tiff_samplesperpixel; |
| 191 |
uint16_t tiff_planar; |
| 192 |
uint32_t tiff_width; |
| 193 |
uint32_t tiff_length; |
| 194 |
float tiff_xres; |
| 195 |
float tiff_yres; |
| 196 |
uint16_t tiff_orientation; |
| 197 |
toff_t tiff_dataoffset; |
| 198 |
tsize_t tiff_datasize; |
| 199 |
tsize_t tiff_maxdatasize; |
| 200 |
uint16_t tiff_resunit; |
| 201 |
uint16_t pdf_centimeters; |
| 202 |
uint16_t pdf_overrideres; |
| 203 |
uint16_t pdf_overridepagesize; |
| 204 |
float pdf_defaultxres; |
| 205 |
float pdf_defaultyres; |
| 206 |
float pdf_xres; |
| 207 |
float pdf_yres; |
| 208 |
float pdf_defaultpagewidth; |
| 209 |
float pdf_defaultpagelength; |
| 210 |
float pdf_pagewidth; |
| 211 |
float pdf_pagelength; |
| 212 |
float pdf_imagewidth; |
| 213 |
float pdf_imagelength; |
| 214 |
int pdf_image_fillpage; /* 0 (default: no scaling, 1:scale imagesize to |
| 215 |
pagesize */ |
| 216 |
T2P_BOX pdf_mediabox; |
| 217 |
T2P_BOX pdf_imagebox; |
| 218 |
uint16_t pdf_majorversion; |
| 219 |
uint16_t pdf_minorversion; |
| 220 |
uint32_t pdf_catalog; |
| 221 |
uint32_t pdf_pages; |
| 222 |
uint32_t pdf_info; |
| 223 |
uint32_t pdf_palettecs; |
| 224 |
uint16_t pdf_fitwindow; |
| 225 |
uint32_t pdf_startxref; |
| 226 |
#define TIFF2PDF_FILEID_SIZE 33 |
| 227 |
char pdf_fileid[TIFF2PDF_FILEID_SIZE]; |
| 228 |
#define TIFF2PDF_DATETIME_SIZE 17 |
| 229 |
char pdf_datetime[TIFF2PDF_DATETIME_SIZE]; |
| 230 |
#define TIFF2PDF_CREATOR_SIZE 512 |
| 231 |
char pdf_creator[TIFF2PDF_CREATOR_SIZE]; |
| 232 |
int pdf_creator_set; |
| 233 |
#define TIFF2PDF_AUTHOR_SIZE 512 |
| 234 |
char pdf_author[TIFF2PDF_AUTHOR_SIZE]; |
| 235 |
int pdf_author_set; |
| 236 |
#define TIFF2PDF_TITLE_SIZE 512 |
| 237 |
char pdf_title[TIFF2PDF_TITLE_SIZE]; |
| 238 |
int pdf_title_set; |
| 239 |
#define TIFF2PDF_SUBJECT_SIZE 512 |
| 240 |
char pdf_subject[TIFF2PDF_SUBJECT_SIZE]; |
| 241 |
int pdf_subject_set; |
| 242 |
#define TIFF2PDF_KEYWORDS_SIZE 512 |
| 243 |
char pdf_keywords[TIFF2PDF_KEYWORDS_SIZE]; |
| 244 |
int pdf_keywords_set; |
| 245 |
t2p_cs_t pdf_colorspace; |
| 246 |
uint16_t pdf_colorspace_invert; |
| 247 |
uint16_t pdf_switchdecode; |
| 248 |
uint16_t pdf_palettesize; |
| 249 |
unsigned char *pdf_palette; |
| 250 |
int pdf_labrange[4]; |
| 251 |
t2p_compress_t pdf_defaultcompression; |
| 252 |
uint16_t pdf_defaultcompressionquality; |
| 253 |
t2p_compress_t pdf_compression; |
| 254 |
uint16_t |
| 255 |
pdf_compressionquality; /* for deflate : 100 * zipquality + predictor */ |
| 256 |
uint16_t pdf_nopassthrough; |
| 257 |
t2p_transcode_t pdf_transcode; |
| 258 |
t2p_sample_t pdf_sample; |
| 259 |
uint32_t *pdf_xrefoffsets; |
| 260 |
uint32_t pdf_xrefcount; |
| 261 |
tdir_t pdf_page; |
| 262 |
#ifdef OJPEG_SUPPORT |
| 263 |
tdata_t pdf_ojpegdata; |
| 264 |
uint32_t pdf_ojpegdatalength; |
| 265 |
uint32_t pdf_ojpegiflength; |
| 266 |
#endif |
| 267 |
float tiff_whitechromaticities[2]; |
| 268 |
float tiff_primarychromaticities[6]; |
| 269 |
float tiff_referenceblackwhite[2]; |
| 270 |
uint16_t *tiff_transferfunction[3]; |
| 271 |
int pdf_image_interpolate; /* 0 (default) : do not interpolate, |
| 272 |
1 : interpolate */ |
| 273 |
uint16_t tiff_transferfunctioncount; |
| 274 |
uint32_t pdf_icccs; |
| 275 |
uint32_t tiff_iccprofilelength; |
| 276 |
tdata_t tiff_iccprofile; |
| 277 |
|
| 278 |
/* fields for custom read/write procedures */ |
| 279 |
FILE *outputfile; |
| 280 |
int outputdisable; |
| 281 |
tsize_t outputwritten; |
| 282 |
} T2P; |
| 283 |
|
| 284 |
/* These functions are called by main. */ |
| 285 |
|
| 286 |
static void usage_info(int); |
| 287 |
int tiff2pdf_match_paper_size(float *, float *, char *); |
| 288 |
|
| 289 |
/* These functions are used to generate a PDF from a TIFF. */ |
| 290 |
|
| 291 |
#ifdef __cplusplus |
| 292 |
extern "C" |
| 293 |
{ |
| 294 |
#endif |
| 295 |
|
| 296 |
T2P *t2p_init(void); |
| 297 |
void t2p_validate(T2P *); |
| 298 |
tsize_t t2p_write_pdf(T2P *, TIFF *, TIFF *); |
| 299 |
void t2p_free(T2P *); |
| 300 |
|
| 301 |
#ifdef __cplusplus |
| 302 |
} |
| 303 |
#endif |
| 304 |
|
| 305 |
void t2p_read_tiff_init(T2P *, TIFF *); |
| 306 |
int t2p_cmp_t2p_page(const void *, const void *); |
| 307 |
void t2p_read_tiff_data(T2P *, TIFF *); |
| 308 |
void t2p_read_tiff_size(T2P *, TIFF *); |
| 309 |
void t2p_read_tiff_size_tile(T2P *, TIFF *, ttile_t); |
| 310 |
int t2p_tile_is_right_edge(T2P_TILES, ttile_t); |
| 311 |
int t2p_tile_is_bottom_edge(T2P_TILES, ttile_t); |
| 312 |
int t2p_tile_is_edge(T2P_TILES, ttile_t); |
| 313 |
int t2p_tile_is_corner_edge(T2P_TILES, ttile_t); |
| 314 |
tsize_t t2p_readwrite_pdf_image(T2P *, TIFF *, TIFF *); |
| 315 |
tsize_t t2p_readwrite_pdf_image_tile(T2P *, TIFF *, TIFF *, ttile_t); |
| 316 |
#ifdef OJPEG_SUPPORT |
| 317 |
int t2p_process_ojpeg_tables(T2P *, TIFF *); |
| 318 |
#endif |
| 319 |
#ifdef JPEG_SUPPORT |
| 320 |
int t2p_process_jpeg_strip(unsigned char *, tsize_t *, unsigned char *, tsize_t, |
| 321 |
tsize_t *, tstrip_t, uint32_t); |
| 322 |
#endif |
| 323 |
void t2p_tile_collapse_left(tdata_t, tsize_t, uint32_t, uint32_t, uint32_t); |
| 324 |
void t2p_write_advance_directory(T2P *, TIFF *); |
| 325 |
tsize_t t2p_sample_planar_separate_to_contig(T2P *, unsigned char *, |
| 326 |
unsigned char *, tsize_t); |
| 327 |
tsize_t t2p_sample_realize_palette(T2P *, unsigned char *); |
| 328 |
tsize_t t2p_sample_abgr_to_rgb(tdata_t, uint32_t); |
| 329 |
tsize_t t2p_sample_rgba_to_rgb(tdata_t, uint32_t); |
| 330 |
tsize_t t2p_sample_rgbaa_to_rgb(tdata_t, uint32_t); |
| 331 |
tsize_t t2p_sample_lab_signed_to_unsigned(tdata_t, uint32_t); |
| 332 |
tsize_t t2p_write_pdf_header(T2P *, TIFF *); |
| 333 |
tsize_t t2p_write_pdf_obj_start(uint32_t, TIFF *); |
| 334 |
tsize_t t2p_write_pdf_obj_end(TIFF *); |
| 335 |
tsize_t t2p_write_pdf_name(const unsigned char *, TIFF *); |
| 336 |
tsize_t t2p_write_pdf_string(const char *, TIFF *); |
| 337 |
tsize_t t2p_write_pdf_stream(tdata_t, tsize_t, TIFF *); |
| 338 |
tsize_t t2p_write_pdf_stream_start(TIFF *); |
| 339 |
tsize_t t2p_write_pdf_stream_end(TIFF *); |
| 340 |
tsize_t t2p_write_pdf_stream_dict(tsize_t, uint32_t, TIFF *); |
| 341 |
tsize_t t2p_write_pdf_stream_dict_start(TIFF *); |
| 342 |
tsize_t t2p_write_pdf_stream_dict_end(TIFF *); |
| 343 |
tsize_t t2p_write_pdf_stream_length(tsize_t, TIFF *); |
| 344 |
tsize_t t2p_write_pdf_catalog(T2P *, TIFF *); |
| 345 |
tsize_t t2p_write_pdf_info(T2P *, TIFF *, TIFF *); |
| 346 |
void t2p_pdf_currenttime(T2P *); |
| 347 |
void t2p_pdf_tifftime(T2P *, TIFF *); |
| 348 |
tsize_t t2p_write_pdf_pages(T2P *, TIFF *); |
| 349 |
tsize_t t2p_write_pdf_page(uint32_t, T2P *, TIFF *); |
| 350 |
void t2p_compose_pdf_page(T2P *); |
| 351 |
void t2p_compose_pdf_page_orient(T2P_BOX *, uint16_t); |
| 352 |
void t2p_compose_pdf_page_orient_flip(T2P_BOX *, uint16_t); |
| 353 |
tsize_t t2p_write_pdf_page_content(T2P *, TIFF *); |
| 354 |
tsize_t t2p_write_pdf_xobject_stream_dict(ttile_t, T2P *, TIFF *); |
| 355 |
tsize_t t2p_write_pdf_xobject_cs(T2P *, TIFF *); |
| 356 |
tsize_t t2p_write_pdf_transfer(T2P *, TIFF *); |
| 357 |
tsize_t t2p_write_pdf_transfer_dict(T2P *, TIFF *, uint16_t); |
| 358 |
tsize_t t2p_write_pdf_transfer_stream(T2P *, TIFF *, uint16_t); |
| 359 |
tsize_t t2p_write_pdf_xobject_calcs(T2P *, TIFF *); |
| 360 |
tsize_t t2p_write_pdf_xobject_icccs(T2P *, TIFF *); |
| 361 |
tsize_t t2p_write_pdf_xobject_icccs_dict(T2P *, TIFF *); |
| 362 |
tsize_t t2p_write_pdf_xobject_icccs_stream(T2P *, TIFF *); |
| 363 |
tsize_t t2p_write_pdf_xobject_cs_stream(T2P *, TIFF *); |
| 364 |
tsize_t t2p_write_pdf_xobject_decode(T2P *, TIFF *); |
| 365 |
tsize_t t2p_write_pdf_xobject_stream_filter(ttile_t, T2P *, TIFF *); |
| 366 |
tsize_t t2p_write_pdf_xreftable(T2P *, TIFF *); |
| 367 |
tsize_t t2p_write_pdf_trailer(T2P *, TIFF *); |
| 368 |
|
| 369 |
#define check_snprintf_ret(t2p, rv, buf) \ |
| 370 |
do \ |
| 371 |
{ \ |
| 372 |
if ((rv) < 0) \ |
| 373 |
rv = 0; \ |
| 374 |
else if ((rv) >= (int)sizeof(buf)) \ |
| 375 |
(rv) = sizeof(buf) - 1; \ |
| 376 |
else \ |
| 377 |
break; \ |
| 378 |
if ((t2p) != NULL) \ |
| 379 |
(t2p)->t2p_error = T2P_ERR_ERROR; \ |
| 380 |
} while (0) |
| 381 |
|
| 382 |
/* Avoid Coverity issues: Don't add negative error return values to counter of |
| 383 |
* written bytes. */ |
| 384 |
#define add_t2pWriteFile_check(tiff, data, size, modulename, addwrittento) \ |
| 385 |
do \ |
| 386 |
{ \ |
| 387 |
tmsize_t retwritevalue = t2pWriteFile(tiff, data, size); \ |
| 388 |
if (retwritevalue < 0) \ |
| 389 |
{ \ |
| 390 |
TIFFError(TIFF2PDF_MODULE, "%s", modulename); \ |
| 391 |
t2p->t2p_error = T2P_ERR_ERROR; \ |
| 392 |
} \ |
| 393 |
else \ |
| 394 |
{ \ |
| 395 |
addwrittento += retwritevalue; \ |
| 396 |
} \ |
| 397 |
} while (0) |
| 398 |
|
| 399 |
/* A second macro(with suffix "_2") is necessary because in some subroutines |
| 400 |
* the parameter "t2p" is not available. */ |
| 401 |
#define add_t2pWriteFile_check_2(tiff, data, size, modulename, addwrittento) \ |
| 402 |
do \ |
| 403 |
{ \ |
| 404 |
tmsize_t retwritevalue = t2pWriteFile(tiff, data, size); \ |
| 405 |
if (retwritevalue < 0) \ |
| 406 |
{ \ |
| 407 |
TIFFError(TIFF2PDF_MODULE, "%s", modulename); \ |
| 408 |
} \ |
| 409 |
else \ |
| 410 |
{ \ |
| 411 |
addwrittento += retwritevalue; \ |
| 412 |
} \ |
| 413 |
} while (0) |
| 414 |
|
| 415 |
static void t2p_disable(TIFF *tif) |
| 416 |
{ |
| 417 |
T2P *t2p = (T2P *)TIFFClientdata(tif); |
| 418 |
t2p->outputdisable = 1; |
| 419 |
} |
| 420 |
|
| 421 |
static void t2p_enable(TIFF *tif) |
| 422 |
{ |
| 423 |
T2P *t2p = (T2P *)TIFFClientdata(tif); |
| 424 |
t2p->outputdisable = 0; |
| 425 |
} |
| 426 |
|
| 427 |
/* |
| 428 |
* Procs for TIFFClientOpen |
| 429 |
*/ |
| 430 |
|
| 431 |
#ifdef OJPEG_SUPPORT |
| 432 |
static tmsize_t t2pReadFile(TIFF *tif, tdata_t data, tmsize_t size) |
| 433 |
{ |
| 434 |
thandle_t client = TIFFClientdata(tif); |
| 435 |
TIFFReadWriteProc proc = TIFFGetReadProc(tif); |
| 436 |
if (proc) |
| 437 |
return proc(client, data, size); |
| 438 |
return -1; |
| 439 |
} |
| 440 |
#endif /* OJPEG_SUPPORT */ |
| 441 |
|
| 442 |
static tmsize_t t2pWriteFile(TIFF *tif, tdata_t data, tmsize_t size) |
| 443 |
{ |
| 444 |
thandle_t client = TIFFClientdata(tif); |
| 445 |
TIFFReadWriteProc proc = TIFFGetWriteProc(tif); |
| 446 |
if (proc) |
| 447 |
return proc(client, data, size); |
| 448 |
return -1; |
| 449 |
} |
| 450 |
|
| 451 |
static uint64_t t2pSeekFile(TIFF *tif, toff_t offset, int whence) |
| 452 |
{ |
| 453 |
thandle_t client = TIFFClientdata(tif); |
| 454 |
TIFFSeekProc proc = TIFFGetSeekProc(tif); |
| 455 |
if (proc) |
| 456 |
return proc(client, offset, whence); |
| 457 |
return -1; |
| 458 |
} |
| 459 |
|
| 460 |
static tmsize_t t2p_readproc(thandle_t handle, tdata_t data, tmsize_t size) |
| 461 |
{ |
| 462 |
(void)handle, (void)data, (void)size; |
| 463 |
return -1; |
| 464 |
} |
| 465 |
|
| 466 |
static tmsize_t t2p_writeproc(thandle_t handle, tdata_t data, tmsize_t size) |
| 467 |
{ |
| 468 |
T2P *t2p = (T2P *)handle; |
| 469 |
if (t2p->outputdisable <= 0 && t2p->outputfile) |
| 470 |
{ |
| 471 |
tsize_t written = fwrite(data, 1, size, t2p->outputfile); |
| 472 |
t2p->outputwritten += written; |
| 473 |
return written; |
| 474 |
} |
| 475 |
return size; |
| 476 |
} |
| 477 |
|
| 478 |
static uint64_t t2p_seekproc(thandle_t handle, uint64_t offset, int whence) |
| 479 |
{ |
| 480 |
T2P *t2p = (T2P *)handle; |
| 481 |
if (t2p->outputdisable <= 0 && t2p->outputfile != stdout && t2p->outputfile) |
| 482 |
return _TIFF_fseek_f(t2p->outputfile, (_TIFF_off_t)offset, whence); |
| 483 |
return offset; |
| 484 |
} |
| 485 |
|
| 486 |
static int t2p_closeproc(thandle_t handle) |
| 487 |
{ |
| 488 |
T2P *t2p = (T2P *)handle; |
| 489 |
return fclose(t2p->outputfile); |
| 490 |
} |
| 491 |
|
| 492 |
static uint64_t t2p_sizeproc(thandle_t handle) |
| 493 |
{ |
| 494 |
(void)handle; |
| 495 |
return -1; |
| 496 |
} |
| 497 |
|
| 498 |
static int t2p_mapproc(thandle_t handle, void **data, toff_t *offset) |
| 499 |
{ |
| 500 |
(void)handle, (void)data, (void)offset; |
| 501 |
return -1; |
| 502 |
} |
| 503 |
|
| 504 |
static void t2p_unmapproc(thandle_t handle, void *data, toff_t offset) |
| 505 |
{ |
| 506 |
(void)handle, (void)data, (void)offset; |
| 507 |
} |
| 508 |
|
| 509 |
#if defined(OJPEG_SUPPORT) || defined(JPEG_SUPPORT) |
| 510 |
static uint64_t checkAdd64(uint64_t summand1, uint64_t summand2, T2P *t2p) |
| 511 |
{ |
| 512 |
uint64_t bytes = summand1 + summand2; |
| 513 |
|
| 514 |
if (bytes < summand1) |
| 515 |
{ |
| 516 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 517 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 518 |
bytes = 0; |
| 519 |
} |
| 520 |
|
| 521 |
return bytes; |
| 522 |
} |
| 523 |
#endif /* defined(OJPEG_SUPPORT) || defined(JPEG_SUPPORT) */ |
| 524 |
|
| 525 |
static uint64_t checkMultiply64(uint64_t first, uint64_t second, T2P *t2p) |
| 526 |
{ |
| 527 |
uint64_t bytes = first * second; |
| 528 |
|
| 529 |
if (second && bytes / second != first) |
| 530 |
{ |
| 531 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 532 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 533 |
bytes = 0; |
| 534 |
} |
| 535 |
|
| 536 |
return bytes; |
| 537 |
} |
| 538 |
|
| 539 |
/* |
| 540 |
|
| 541 |
This is the main function. |
| 542 |
|
| 543 |
The program converts one TIFF file to one PDF file, including multiple page |
| 544 |
TIFF files, tiled TIFF files, black and white. grayscale, and color TIFF |
| 545 |
files that contain data of TIFF photometric interpretations of bilevel, |
| 546 |
grayscale, RGB, YCbCr, CMYK separation, and ICC L*a*b* as supported by |
| 547 |
libtiff and PDF. |
| 548 |
|
| 549 |
If you have multiple TIFF files to convert into one PDF file then use tiffcp |
| 550 |
or other program to concatenate the files into a multiple page TIFF file. |
| 551 |
If the input TIFF file is of huge dimensions (greater than 10000 pixels height |
| 552 |
or width) convert the input image to a tiled TIFF if it is not already. |
| 553 |
|
| 554 |
The standard output is standard output. Set the output file name with the |
| 555 |
"-o output.pdf" option. |
| 556 |
|
| 557 |
All black and white files are compressed into a single strip CCITT G4 Fax |
| 558 |
compressed PDF, unless tiled, where tiled black and white images are |
| 559 |
compressed into tiled CCITT G4 Fax compressed PDF, libtiff CCITT support |
| 560 |
is assumed. |
| 561 |
|
| 562 |
Color and grayscale data can be compressed using either JPEG compression, |
| 563 |
ITU-T T.81, or Zip/Deflate LZ77 compression, per PNG 1.2 and RFC 1951. Set |
| 564 |
the compression type using the -j or -z options. JPEG compression support |
| 565 |
requires that libtiff be configured with JPEG support, and Zip/Deflate |
| 566 |
compression support requires that libtiff is configured with Zip support, |
| 567 |
in tiffconf.h. Use only one or the other of -j and -z. The -q option |
| 568 |
sets the image compression quality, that is 1-100 with libjpeg JPEG |
| 569 |
compression and one of 1, 10, 11, 12, 13, 14, or 15 for PNG group compression |
| 570 |
predictor methods, add 100, 200, ..., 900 to set zlib compression quality 1-9. |
| 571 |
PNG Group differencing predictor methods are not currently implemented. |
| 572 |
|
| 573 |
If the input TIFF contains single strip CCITT G4 Fax compressed information, |
| 574 |
then that is written to the PDF file without transcoding, unless the options |
| 575 |
of no compression and no passthrough are set, -d and -n. |
| 576 |
|
| 577 |
If the input TIFF contains JPEG or single strip Zip/Deflate compressed |
| 578 |
information, and they are configured, then that is written to the PDF file |
| 579 |
without transcoding, unless the options of no compression and no passthrough |
| 580 |
are set. |
| 581 |
|
| 582 |
The default page size upon which the TIFF image is placed is determined by |
| 583 |
the resolution and extent of the image data. Default values for the TIFF |
| 584 |
image resolution can be set using the -x and -y options. The page size can |
| 585 |
be set using the -p option for paper size, or -w and -l for paper width and |
| 586 |
length, then each page of the TIFF image is centered on its page. The |
| 587 |
distance unit for default resolution and page width and length can be set |
| 588 |
by the -u option, the default unit is inch. |
| 589 |
|
| 590 |
Various items of the output document information can be set with the -e, -c, |
| 591 |
-a, -t, -s, and -k tags. Setting the argument of the option to "" for these |
| 592 |
tags causes the relevant document information field to be not written. Some |
| 593 |
of the document information values otherwise get their information from the |
| 594 |
input TIFF image, the software, author, document name, and image description. |
| 595 |
|
| 596 |
The output PDF file conforms to the PDF 1.1 specification or PDF 1.2 if using |
| 597 |
Zip/Deflate compression. |
| 598 |
|
| 599 |
The Portable Document Format (PDF) specification is copyrighted by Adobe |
| 600 |
Systems, Incorporated. Todos derechos reservados. |
| 601 |
|
| 602 |
Here is a listing of the usage example and the options to the tiff2pdf |
| 603 |
program that is part of the libtiff distribution. Options followed by |
| 604 |
a colon have a required argument. |
| 605 |
|
| 606 |
usage: tiff2pdf [options] input.tif |
| 607 |
|
| 608 |
options: |
| 609 |
-o: output to file name |
| 610 |
|
| 611 |
#ifdef JPEG_SUPPORT |
| 612 |
-j: compress with JPEG (requires libjpeg configured with libtiff) |
| 613 |
#endif |
| 614 |
#ifdef ZIP_SUPPORT |
| 615 |
printf (-z: compress with Zip/Deflate (requires zlib configured with libtiff)); |
| 616 |
#endif |
| 617 |
-q: compression quality |
| 618 |
-n: no compressed data passthrough |
| 619 |
-d: do not compress (decompress) - except monochrome to CCITT Group 4 |
| 620 |
-i: invert colors |
| 621 |
-u: set distance unit, 'i' for inch, 'm' for centimeter |
| 622 |
-x: set x resolution default |
| 623 |
-y: set y resolution default |
| 624 |
-w: width in units |
| 625 |
-l: length in units |
| 626 |
-r: 'd' for resolution default, 'o' for resolution override |
| 627 |
-p: paper size, eg "letter", "legal", "a4" |
| 628 |
-F: make the tiff fill the PDF page |
| 629 |
-f: set pdf "fit window" user preference |
| 630 |
-b: set PDF "Interpolate" user preference |
| 631 |
-e: date, overrides image or current date/time default, YYYYMMDDHHMMSS |
| 632 |
-c: creator, overrides image software default |
| 633 |
-a: author, overrides image artist default |
| 634 |
-t: title, overrides image document name default |
| 635 |
-s: subject, overrides image image description default |
| 636 |
-k: keywords |
| 637 |
|
| 638 |
-h: usage |
| 639 |
|
| 640 |
examples: |
| 641 |
|
| 642 |
tiff2pdf -o output.pdf input.tiff |
| 643 |
|
| 644 |
The above example would generate the file output.pdf from input.tiff. |
| 645 |
|
| 646 |
tiff2pdf input.tiff |
| 647 |
|
| 648 |
The above example would generate PDF output from input.tiff and write it |
| 649 |
to standard output. |
| 650 |
|
| 651 |
tiff2pdf -j -p letter -o output.pdf input.tiff |
| 652 |
|
| 653 |
The above example would generate the file output.pdf from input.tiff, |
| 654 |
putting the image pages on a letter sized page, compressing the output |
| 655 |
with JPEG. |
| 656 |
|
| 657 |
Please report bugs through: |
| 658 |
|
| 659 |
http://bugzilla.remotesensing.org/buglist.cgi?product=libtiff |
| 660 |
|
| 661 |
See also libtiff.3t, tiffcp. |
| 662 |
*/ |
| 663 |
|
| 664 |
int main(int argc, char **argv) |
| 665 |
{ |
| 666 |
#if !HAVE_DECL_OPTARG |
| 667 |
extern char *optarg; |
| 668 |
extern int optind; |
| 669 |
#endif |
| 670 |
const char *outfilename = NULL; |
| 671 |
T2P *t2p = NULL; |
| 672 |
TIFF *input = NULL, *output = NULL; |
| 673 |
int c, ret = EXIT_SUCCESS; |
| 674 |
|
| 675 |
t2p = t2p_init(); |
| 676 |
|
| 677 |
if (t2p == NULL) |
| 678 |
{ |
| 679 |
TIFFError(TIFF2PDF_MODULE, "Can't initialize context"); |
| 680 |
goto fail; |
| 681 |
} |
| 682 |
|
| 683 |
while (argv && |
| 684 |
(c = getopt(argc, argv, |
| 685 |
"m:o:q:u:x:y:w:l:r:p:e:c:a:t:s:k:jzndifbhF")) != -1) |
| 686 |
{ |
| 687 |
switch (c) |
| 688 |
{ |
| 689 |
case 'm': |
| 690 |
t2p->tiff_maxdatasize = (tsize_t)strtoul(optarg, NULL, 0) << 20; |
| 691 |
break; |
| 692 |
case 'o': |
| 693 |
outfilename = optarg; |
| 694 |
break; |
| 695 |
#ifdef JPEG_SUPPORT |
| 696 |
case 'j': |
| 697 |
t2p->pdf_defaultcompression = T2P_COMPRESS_JPEG; |
| 698 |
break; |
| 699 |
#endif |
| 700 |
#ifndef JPEG_SUPPORT |
| 701 |
case 'j': |
| 702 |
TIFFWarning(TIFF2PDF_MODULE, |
| 703 |
"JPEG support in libtiff required for JPEG " |
| 704 |
"compression, ignoring option"); |
| 705 |
break; |
| 706 |
#endif |
| 707 |
#ifdef ZIP_SUPPORT |
| 708 |
case 'z': |
| 709 |
t2p->pdf_defaultcompression = T2P_COMPRESS_ZIP; |
| 710 |
break; |
| 711 |
#endif |
| 712 |
#ifndef ZIP_SUPPORT |
| 713 |
case 'z': |
| 714 |
TIFFWarning(TIFF2PDF_MODULE, |
| 715 |
"Zip support in libtiff required for Zip " |
| 716 |
"compression, ignoring option"); |
| 717 |
break; |
| 718 |
#endif |
| 719 |
case 'q': |
| 720 |
t2p->pdf_defaultcompressionquality = atoi(optarg); |
| 721 |
break; |
| 722 |
case 'n': |
| 723 |
t2p->pdf_nopassthrough = 1; |
| 724 |
break; |
| 725 |
case 'd': |
| 726 |
t2p->pdf_defaultcompression = T2P_COMPRESS_NONE; |
| 727 |
break; |
| 728 |
case 'u': |
| 729 |
if (optarg[0] == 'm') |
| 730 |
{ |
| 731 |
t2p->pdf_centimeters = 1; |
| 732 |
} |
| 733 |
break; |
| 734 |
case 'x': |
| 735 |
t2p->pdf_defaultxres = |
| 736 |
(float)atof(optarg) / (t2p->pdf_centimeters ? 2.54F : 1.0F); |
| 737 |
break; |
| 738 |
case 'y': |
| 739 |
t2p->pdf_defaultyres = |
| 740 |
(float)atof(optarg) / (t2p->pdf_centimeters ? 2.54F : 1.0F); |
| 741 |
break; |
| 742 |
case 'w': |
| 743 |
t2p->pdf_overridepagesize = 1; |
| 744 |
t2p->pdf_defaultpagewidth = |
| 745 |
((float)atof(optarg) * PS_UNIT_SIZE) / |
| 746 |
(t2p->pdf_centimeters ? 2.54F : 1.0F); |
| 747 |
break; |
| 748 |
case 'l': |
| 749 |
t2p->pdf_overridepagesize = 1; |
| 750 |
t2p->pdf_defaultpagelength = |
| 751 |
((float)atof(optarg) * PS_UNIT_SIZE) / |
| 752 |
(t2p->pdf_centimeters ? 2.54F : 1.0F); |
| 753 |
break; |
| 754 |
case 'r': |
| 755 |
if (optarg[0] == 'o') |
| 756 |
{ |
| 757 |
t2p->pdf_overrideres = 1; |
| 758 |
} |
| 759 |
break; |
| 760 |
case 'p': |
| 761 |
if (tiff2pdf_match_paper_size(&(t2p->pdf_defaultpagewidth), |
| 762 |
&(t2p->pdf_defaultpagelength), |
| 763 |
optarg)) |
| 764 |
{ |
| 765 |
t2p->pdf_overridepagesize = 1; |
| 766 |
} |
| 767 |
else |
| 768 |
{ |
| 769 |
TIFFWarning(TIFF2PDF_MODULE, |
| 770 |
"Unknown paper size %s, ignoring option", |
| 771 |
optarg); |
| 772 |
} |
| 773 |
break; |
| 774 |
case 'i': |
| 775 |
t2p->pdf_colorspace_invert = 1; |
| 776 |
break; |
| 777 |
case 'F': |
| 778 |
t2p->pdf_image_fillpage = 1; |
| 779 |
break; |
| 780 |
case 'f': |
| 781 |
t2p->pdf_fitwindow = 1; |
| 782 |
break; |
| 783 |
case 'e': |
| 784 |
if (strlen(optarg) == 0) |
| 785 |
{ |
| 786 |
t2p->pdf_datetime[0] = '\0'; |
| 787 |
} |
| 788 |
else |
| 789 |
{ |
| 790 |
t2p->pdf_datetime[0] = 'D'; |
| 791 |
t2p->pdf_datetime[1] = ':'; |
| 792 |
strncpy(t2p->pdf_datetime + 2, optarg, |
| 793 |
sizeof(t2p->pdf_datetime) - 3); |
| 794 |
t2p->pdf_datetime[sizeof(t2p->pdf_datetime) - 1] = '\0'; |
| 795 |
} |
| 796 |
break; |
| 797 |
case 'c': |
| 798 |
strncpy(t2p->pdf_creator, optarg, sizeof(t2p->pdf_creator) - 1); |
| 799 |
t2p->pdf_creator[sizeof(t2p->pdf_creator) - 1] = '\0'; |
| 800 |
t2p->pdf_creator_set = 1; |
| 801 |
break; |
| 802 |
case 'a': |
| 803 |
strncpy(t2p->pdf_author, optarg, sizeof(t2p->pdf_author) - 1); |
| 804 |
t2p->pdf_author[sizeof(t2p->pdf_author) - 1] = '\0'; |
| 805 |
t2p->pdf_author_set = 1; |
| 806 |
break; |
| 807 |
case 't': |
| 808 |
strncpy(t2p->pdf_title, optarg, sizeof(t2p->pdf_title) - 1); |
| 809 |
t2p->pdf_title[sizeof(t2p->pdf_title) - 1] = '\0'; |
| 810 |
t2p->pdf_title_set = 1; |
| 811 |
break; |
| 812 |
case 's': |
| 813 |
strncpy(t2p->pdf_subject, optarg, sizeof(t2p->pdf_subject) - 1); |
| 814 |
t2p->pdf_subject[sizeof(t2p->pdf_subject) - 1] = '\0'; |
| 815 |
t2p->pdf_subject_set = 1; |
| 816 |
break; |
| 817 |
case 'k': |
| 818 |
strncpy(t2p->pdf_keywords, optarg, |
| 819 |
sizeof(t2p->pdf_keywords) - 1); |
| 820 |
t2p->pdf_keywords[sizeof(t2p->pdf_keywords) - 1] = '\0'; |
| 821 |
t2p->pdf_keywords_set = 1; |
| 822 |
break; |
| 823 |
case 'b': |
| 824 |
t2p->pdf_image_interpolate = 1; |
| 825 |
break; |
| 826 |
case 'h': |
| 827 |
usage_info(EXIT_SUCCESS); |
| 828 |
goto success; |
| 829 |
case '?': |
| 830 |
usage_info(EXIT_FAILURE); |
| 831 |
goto fail; |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
/* |
| 836 |
* Input |
| 837 |
*/ |
| 838 |
if (argc > optind) |
| 839 |
{ |
| 840 |
input = TIFFOpen(argv[optind++], "r"); |
| 841 |
if (input == NULL) |
| 842 |
{ |
| 843 |
TIFFError(TIFF2PDF_MODULE, "Can't open input file %s for reading", |
| 844 |
argv[optind - 1]); |
| 845 |
goto fail; |
| 846 |
} |
| 847 |
} |
| 848 |
else |
| 849 |
{ |
| 850 |
TIFFError(TIFF2PDF_MODULE, "No input file specified"); |
| 851 |
usage_info(EXIT_FAILURE); |
| 852 |
goto fail; |
| 853 |
} |
| 854 |
|
| 855 |
if (argc > optind) |
| 856 |
{ |
| 857 |
TIFFError(TIFF2PDF_MODULE, "No support for multiple input files"); |
| 858 |
usage_info(EXIT_FAILURE); |
| 859 |
goto fail; |
| 860 |
} |
| 861 |
|
| 862 |
/* |
| 863 |
* Output |
| 864 |
*/ |
| 865 |
t2p->outputdisable = 1; |
| 866 |
if (outfilename) |
| 867 |
{ |
| 868 |
t2p->outputfile = fopen(outfilename, "wb"); |
| 869 |
if (t2p->outputfile == NULL) |
| 870 |
{ |
| 871 |
TIFFError(TIFF2PDF_MODULE, "Can't open output file %s for writing", |
| 872 |
outfilename); |
| 873 |
goto fail; |
| 874 |
} |
| 875 |
} |
| 876 |
else |
| 877 |
{ |
| 878 |
outfilename = "-"; |
| 879 |
t2p->outputfile = stdout; |
| 880 |
} |
| 881 |
|
| 882 |
output = TIFFClientOpen(outfilename, "w", (thandle_t)t2p, t2p_readproc, |
| 883 |
t2p_writeproc, t2p_seekproc, t2p_closeproc, |
| 884 |
t2p_sizeproc, t2p_mapproc, t2p_unmapproc); |
| 885 |
t2p->outputdisable = 0; |
| 886 |
if (output == NULL) |
| 887 |
{ |
| 888 |
TIFFError(TIFF2PDF_MODULE, "Can't initialize output descriptor"); |
| 889 |
goto fail; |
| 890 |
} |
| 891 |
|
| 892 |
/* |
| 893 |
* Validate |
| 894 |
*/ |
| 895 |
t2p_validate(t2p); |
| 896 |
t2pSeekFile(output, (toff_t)0, SEEK_SET); |
| 897 |
|
| 898 |
/* |
| 899 |
* Write |
| 900 |
*/ |
| 901 |
t2p_write_pdf(t2p, input, output); |
| 902 |
if (t2p->t2p_error != 0) |
| 903 |
{ |
| 904 |
TIFFError(TIFF2PDF_MODULE, |
| 905 |
"An error occurred creating output PDF file"); |
| 906 |
goto fail; |
| 907 |
} |
| 908 |
|
| 909 |
goto success; |
| 910 |
fail: |
| 911 |
ret = EXIT_FAILURE; |
| 912 |
success: |
| 913 |
if (input != NULL) |
| 914 |
TIFFClose(input); |
| 915 |
if (output != NULL) |
| 916 |
TIFFClose(output); |
| 917 |
if (t2p != NULL) |
| 918 |
t2p_free(t2p); |
| 919 |
return ret; |
| 920 |
} |
| 921 |
|
| 922 |
static void usage_info(int code) |
| 923 |
{ |
| 924 |
static const char lines[] = |
| 925 |
"Convert a TIFF image to a PDF document\n\n" |
| 926 |
"usage: tiff2pdf [options] input.tiff\n" |
| 927 |
"where options are:\n" |
| 928 |
" -o: output to file name\n" |
| 929 |
#ifdef JPEG_SUPPORT |
| 930 |
" -j: compress with JPEG\n" |
| 931 |
#endif |
| 932 |
#ifdef ZIP_SUPPORT |
| 933 |
" -z: compress with Zip/Deflate\n" |
| 934 |
#endif |
| 935 |
#if defined(JPEG_SUPPORT) || defined(ZIP_SUPPORT) |
| 936 |
" -q: compression quality\n" |
| 937 |
" -n: no compressed data passthrough\n" |
| 938 |
" -d: do not compress (decompress) - except monochrome to CCITT Group " |
| 939 |
"4\n" |
| 940 |
#endif |
| 941 |
" -i: invert colors\n" |
| 942 |
" -u: set distance unit, 'i' for inch, 'm' for centimeter\n" |
| 943 |
" -x: set x resolution default in dots per unit\n" |
| 944 |
" -y: set y resolution default in dots per unit\n" |
| 945 |
" -w: width in units\n" |
| 946 |
" -l: length in units\n" |
| 947 |
" -r: 'd' for resolution default, 'o' for resolution override\n" |
| 948 |
" -p: paper size, eg \"letter\", \"legal\", \"A4\"\n" |
| 949 |
" -F: make the tiff fill the PDF page\n" |
| 950 |
" -f: set PDF \"Fit Window\" user preference\n" |
| 951 |
" -e: date, overrides image or current date/time default, " |
| 952 |
"YYYYMMDDHHMMSS\n" |
| 953 |
" -c: sets document creator, overrides image software default\n" |
| 954 |
" -a: sets document author, overrides image artist default\n" |
| 955 |
" -t: sets document title, overrides image document name default\n" |
| 956 |
" -s: sets document subject, overrides image image description " |
| 957 |
"default\n" |
| 958 |
" -k: sets document keywords\n" |
| 959 |
" -b: set PDF \"Interpolate\" user preference\n" |
| 960 |
" -m: set memory allocation limit (in MiB). set to 0 to disable limit\n" |
| 961 |
" -h: usage\n"; |
| 962 |
FILE *out = (code == EXIT_SUCCESS) ? stdout : stderr; |
| 963 |
|
| 964 |
fprintf(out, "%s\n\n", TIFFGetVersion()); |
| 965 |
fprintf(out, "%s", lines); |
| 966 |
|
| 967 |
return; |
| 968 |
} |
| 969 |
|
| 970 |
int tiff2pdf_match_paper_size(float *width, float *length, char *papersize) |
| 971 |
{ |
| 972 |
|
| 973 |
size_t i, len; |
| 974 |
const char *sizes[] = { |
| 975 |
"LETTER", "A4", "LEGAL", "EXECUTIVE", "LETTER", "LEGAL", |
| 976 |
"LEDGER", "TABLOID", "A", "B", "C", "D", |
| 977 |
"E", "F", "G", "H", "J", "K", |
| 978 |
"A10", "A9", "A8", "A7", "A6", "A5", |
| 979 |
"A4", "A3", "A2", "A1", "A0", "2A0", |
| 980 |
"4A0", "2A", "4A", "B10", "B9", "B8", |
| 981 |
"B7", "B6", "B5", "B4", "B3", "B2", |
| 982 |
"B1", "B0", "JISB10", "JISB9", "JISB8", "JISB7", |
| 983 |
"JISB6", "JISB5", "JISB4", "JISB3", "JISB2", "JISB1", |
| 984 |
"JISB0", "C10", "C9", "C8", "C7", "C6", |
| 985 |
"C5", "C4", "C3", "C2", "C1", "C0", |
| 986 |
"RA2", "RA1", "RA0", "SRA4", "SRA3", "SRA2", |
| 987 |
"SRA1", "SRA0", "A3EXTRA", "A4EXTRA", "STATEMENT", "FOLIO", |
| 988 |
"QUARTO", NULL}; |
| 989 |
const int widths[] = { |
| 990 |
612, 595, 612, 522, 612, 612, 792, 792, 612, 792, 1224, 1584, |
| 991 |
2448, 2016, 792, 2016, 2448, 2880, 74, 105, 147, 210, 298, 420, |
| 992 |
595, 842, 1191, 1684, 2384, 3370, 4768, 3370, 4768, 88, 125, 176, |
| 993 |
249, 354, 499, 709, 1001, 1417, 2004, 2835, 91, 128, 181, 258, |
| 994 |
363, 516, 729, 1032, 1460, 2064, 2920, 79, 113, 162, 230, 323, |
| 995 |
459, 649, 918, 1298, 1298, 2599, 1219, 1729, 2438, 638, 907, 1276, |
| 996 |
1814, 2551, 914, 667, 396, 612, 609, 0}; |
| 997 |
const int lengths[] = { |
| 998 |
792, 842, 1008, 756, 792, 1008, 1224, 1224, 792, 1224, |
| 999 |
1584, 2448, 3168, 2880, 6480, 10296, 12672, 10296, 105, 147, |
| 1000 |
210, 298, 420, 595, 842, 1191, 1684, 2384, 3370, 4768, |
| 1001 |
6741, 4768, 6741, 125, 176, 249, 354, 499, 709, 1001, |
| 1002 |
1417, 2004, 2835, 4008, 128, 181, 258, 363, 516, 729, |
| 1003 |
1032, 1460, 2064, 2920, 4127, 113, 162, 230, 323, 459, |
| 1004 |
649, 918, 1298, 1837, 1837, 3677, 1729, 2438, 3458, 907, |
| 1005 |
1276, 1814, 2551, 3628, 1262, 914, 612, 936, 780, 0}; |
| 1006 |
|
| 1007 |
len = strlen(papersize); |
| 1008 |
for (i = 0; i < len; i++) |
| 1009 |
{ |
| 1010 |
papersize[i] = toupper((int)papersize[i]); |
| 1011 |
} |
| 1012 |
for (i = 0; sizes[i] != NULL; i++) |
| 1013 |
{ |
| 1014 |
if (strcmp((const char *)papersize, sizes[i]) == 0) |
| 1015 |
{ |
| 1016 |
*width = (float)widths[i]; |
| 1017 |
*length = (float)lengths[i]; |
| 1018 |
return (1); |
| 1019 |
} |
| 1020 |
} |
| 1021 |
|
| 1022 |
return (0); |
| 1023 |
} |
| 1024 |
|
| 1025 |
/* |
| 1026 |
* This function allocates and initializes a T2P context struct pointer. |
| 1027 |
*/ |
| 1028 |
|
| 1029 |
T2P *t2p_init() |
| 1030 |
{ |
| 1031 |
T2P *t2p = (T2P *)_TIFFmalloc(sizeof(T2P)); |
| 1032 |
if (t2p == NULL) |
| 1033 |
{ |
| 1034 |
TIFFError(TIFF2PDF_MODULE, |
| 1035 |
"Can't allocate %" TIFF_SIZE_FORMAT |
| 1036 |
" bytes of memory for t2p_init", |
| 1037 |
sizeof(T2P)); |
| 1038 |
return ((T2P *)NULL); |
| 1039 |
} |
| 1040 |
_TIFFmemset(t2p, 0x00, sizeof(T2P)); |
| 1041 |
t2p->pdf_majorversion = 1; |
| 1042 |
t2p->pdf_minorversion = 1; |
| 1043 |
t2p->pdf_defaultxres = 300.0; |
| 1044 |
t2p->pdf_defaultyres = 300.0; |
| 1045 |
t2p->pdf_defaultpagewidth = 612.0; |
| 1046 |
t2p->pdf_defaultpagelength = 792.0; |
| 1047 |
t2p->pdf_xrefcount = 3; /* Catalog, Info, Pages */ |
| 1048 |
t2p->tiff_maxdatasize = DEFAULT_MAX_MALLOC; |
| 1049 |
|
| 1050 |
return (t2p); |
| 1051 |
} |
| 1052 |
|
| 1053 |
/* |
| 1054 |
* This function frees a T2P context struct pointer and any allocated data |
| 1055 |
* fields of it. |
| 1056 |
*/ |
| 1057 |
|
| 1058 |
void t2p_free(T2P *t2p) |
| 1059 |
{ |
| 1060 |
unsigned int i = 0; |
| 1061 |
|
| 1062 |
if (t2p != NULL) |
| 1063 |
{ |
| 1064 |
if (t2p->pdf_xrefoffsets != NULL) |
| 1065 |
{ |
| 1066 |
_TIFFfree((tdata_t)t2p->pdf_xrefoffsets); |
| 1067 |
} |
| 1068 |
if (t2p->tiff_pages != NULL) |
| 1069 |
{ |
| 1070 |
_TIFFfree((tdata_t)t2p->tiff_pages); |
| 1071 |
} |
| 1072 |
for (i = 0; i < t2p->tiff_pagecount; i++) |
| 1073 |
{ |
| 1074 |
if (t2p->tiff_tiles[i].tiles_tiles != NULL) |
| 1075 |
{ |
| 1076 |
_TIFFfree((tdata_t)t2p->tiff_tiles[i].tiles_tiles); |
| 1077 |
} |
| 1078 |
} |
| 1079 |
if (t2p->tiff_tiles != NULL) |
| 1080 |
{ |
| 1081 |
_TIFFfree((tdata_t)t2p->tiff_tiles); |
| 1082 |
} |
| 1083 |
if (t2p->pdf_palette != NULL) |
| 1084 |
{ |
| 1085 |
_TIFFfree((tdata_t)t2p->pdf_palette); |
| 1086 |
} |
| 1087 |
#ifdef OJPEG_SUPPORT |
| 1088 |
if (t2p->pdf_ojpegdata != NULL) |
| 1089 |
{ |
| 1090 |
_TIFFfree((tdata_t)t2p->pdf_ojpegdata); |
| 1091 |
} |
| 1092 |
#endif |
| 1093 |
_TIFFfree((tdata_t)t2p); |
| 1094 |
} |
| 1095 |
|
| 1096 |
return; |
| 1097 |
} |
| 1098 |
|
| 1099 |
/* |
| 1100 |
This function validates the values of a T2P context struct pointer |
| 1101 |
before calling t2p_write_pdf with it. |
| 1102 |
*/ |
| 1103 |
|
| 1104 |
void t2p_validate(T2P *t2p) |
| 1105 |
{ |
| 1106 |
(void)t2p; |
| 1107 |
|
| 1108 |
#ifdef JPEG_SUPPORT |
| 1109 |
if (t2p->pdf_defaultcompression == T2P_COMPRESS_JPEG) |
| 1110 |
{ |
| 1111 |
if (t2p->pdf_defaultcompressionquality > 100 || |
| 1112 |
t2p->pdf_defaultcompressionquality < 1) |
| 1113 |
{ |
| 1114 |
t2p->pdf_defaultcompressionquality = 0; |
| 1115 |
} |
| 1116 |
} |
| 1117 |
#endif |
| 1118 |
#ifdef ZIP_SUPPORT |
| 1119 |
if (t2p->pdf_defaultcompression == T2P_COMPRESS_ZIP) |
| 1120 |
{ |
| 1121 |
uint16_t m = t2p->pdf_defaultcompressionquality % 100; |
| 1122 |
if (t2p->pdf_defaultcompressionquality / 100 > 9 || (m > 1 && m < 10) || |
| 1123 |
m > 15) |
| 1124 |
{ |
| 1125 |
t2p->pdf_defaultcompressionquality = 0; |
| 1126 |
} |
| 1127 |
if (t2p->pdf_defaultcompressionquality % 100 != 0) |
| 1128 |
{ |
| 1129 |
t2p->pdf_defaultcompressionquality /= 100; |
| 1130 |
t2p->pdf_defaultcompressionquality *= 100; |
| 1131 |
TIFFError(TIFF2PDF_MODULE, |
| 1132 |
"PNG Group predictor differencing not implemented, " |
| 1133 |
"assuming compression quality %" PRIu16, |
| 1134 |
t2p->pdf_defaultcompressionquality); |
| 1135 |
} |
| 1136 |
t2p->pdf_defaultcompressionquality %= 100; |
| 1137 |
if (t2p->pdf_minorversion < 2) |
| 1138 |
{ |
| 1139 |
t2p->pdf_minorversion = 2; |
| 1140 |
} |
| 1141 |
} |
| 1142 |
#endif |
| 1143 |
|
| 1144 |
return; |
| 1145 |
} |
| 1146 |
|
| 1147 |
/* |
| 1148 |
This function scans the input TIFF file for pages. It attempts |
| 1149 |
to determine which IFD's of the TIFF file contain image document |
| 1150 |
pages. For each, it gathers some information that has to do |
| 1151 |
with the output of the PDF document as a whole. |
| 1152 |
*/ |
| 1153 |
|
| 1154 |
void t2p_read_tiff_init(T2P *t2p, TIFF *input) |
| 1155 |
{ |
| 1156 |
|
| 1157 |
tdir_t directorycount = 0; |
| 1158 |
tdir_t i = 0; |
| 1159 |
uint16_t pagen = 0; |
| 1160 |
uint16_t paged = 0; |
| 1161 |
uint16_t xuint16 = 0; |
| 1162 |
uint16_t tiff_transferfunctioncount = 0; |
| 1163 |
uint16_t *tiff_transferfunction[3]; |
| 1164 |
|
| 1165 |
directorycount = TIFFNumberOfDirectories(input); |
| 1166 |
if (directorycount > TIFF_DIR_MAX) |
| 1167 |
{ |
| 1168 |
TIFFError(TIFF2PDF_MODULE, "TIFF contains too many directories, %s", |
| 1169 |
TIFFFileName(input)); |
| 1170 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1171 |
return; |
| 1172 |
} |
| 1173 |
t2p->tiff_pages = (T2P_PAGE *)_TIFFmalloc( |
| 1174 |
TIFFSafeMultiply(tmsize_t, directorycount, sizeof(T2P_PAGE))); |
| 1175 |
if (t2p->tiff_pages == NULL) |
| 1176 |
{ |
| 1177 |
TIFFError(TIFF2PDF_MODULE, |
| 1178 |
"Can't allocate %" TIFF_SIZE_FORMAT |
| 1179 |
" bytes of memory for tiff_pages array, %s", |
| 1180 |
sizeof(T2P_PAGE) * directorycount, TIFFFileName(input)); |
| 1181 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1182 |
return; |
| 1183 |
} |
| 1184 |
_TIFFmemset(t2p->tiff_pages, 0x00, directorycount * sizeof(T2P_PAGE)); |
| 1185 |
t2p->tiff_tiles = (T2P_TILES *)_TIFFmalloc( |
| 1186 |
TIFFSafeMultiply(tmsize_t, directorycount, sizeof(T2P_TILES))); |
| 1187 |
if (t2p->tiff_tiles == NULL) |
| 1188 |
{ |
| 1189 |
TIFFError(TIFF2PDF_MODULE, |
| 1190 |
"Can't allocate %" TIFF_SIZE_FORMAT |
| 1191 |
" bytes of memory for tiff_tiles array, %s", |
| 1192 |
sizeof(T2P_TILES) * directorycount, TIFFFileName(input)); |
| 1193 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1194 |
return; |
| 1195 |
} |
| 1196 |
_TIFFmemset(t2p->tiff_tiles, 0x00, directorycount * sizeof(T2P_TILES)); |
| 1197 |
for (i = 0; i < directorycount; i++) |
| 1198 |
{ |
| 1199 |
uint32_t subfiletype = 0; |
| 1200 |
|
| 1201 |
if (!TIFFSetDirectory(input, i)) |
| 1202 |
{ |
| 1203 |
TIFFError(TIFF2PDF_MODULE, |
| 1204 |
"Can't set directory %" PRIu16 " of input file %s", i, |
| 1205 |
TIFFFileName(input)); |
| 1206 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1207 |
return; |
| 1208 |
} |
| 1209 |
if (TIFFGetField(input, TIFFTAG_PAGENUMBER, &pagen, &paged)) |
| 1210 |
{ |
| 1211 |
if ((pagen > paged) && (paged != 0)) |
| 1212 |
{ |
| 1213 |
t2p->tiff_pages[t2p->tiff_pagecount].page_number = paged; |
| 1214 |
} |
| 1215 |
else |
| 1216 |
{ |
| 1217 |
t2p->tiff_pages[t2p->tiff_pagecount].page_number = pagen; |
| 1218 |
} |
| 1219 |
goto ispage2; |
| 1220 |
} |
| 1221 |
if (TIFFGetField(input, TIFFTAG_SUBFILETYPE, &subfiletype)) |
| 1222 |
{ |
| 1223 |
if (((subfiletype & FILETYPE_PAGE) != 0) || (subfiletype == 0)) |
| 1224 |
{ |
| 1225 |
goto ispage; |
| 1226 |
} |
| 1227 |
else |
| 1228 |
{ |
| 1229 |
goto isnotpage; |
| 1230 |
} |
| 1231 |
} |
| 1232 |
if (TIFFGetField(input, TIFFTAG_OSUBFILETYPE, &subfiletype)) |
| 1233 |
{ |
| 1234 |
if ((subfiletype == OFILETYPE_IMAGE) || |
| 1235 |
(subfiletype == OFILETYPE_PAGE) || (subfiletype == 0)) |
| 1236 |
{ |
| 1237 |
goto ispage; |
| 1238 |
} |
| 1239 |
else |
| 1240 |
{ |
| 1241 |
goto isnotpage; |
| 1242 |
} |
| 1243 |
} |
| 1244 |
ispage: |
| 1245 |
t2p->tiff_pages[t2p->tiff_pagecount].page_number = t2p->tiff_pagecount; |
| 1246 |
ispage2: |
| 1247 |
t2p->tiff_pages[t2p->tiff_pagecount].page_directory = i; |
| 1248 |
if (TIFFIsTiled(input)) |
| 1249 |
{ |
| 1250 |
t2p->tiff_pages[t2p->tiff_pagecount].page_tilecount = |
| 1251 |
TIFFNumberOfTiles(input); |
| 1252 |
} |
| 1253 |
t2p->tiff_pagecount++; |
| 1254 |
isnotpage: |
| 1255 |
(void)0; |
| 1256 |
} |
| 1257 |
|
| 1258 |
qsort((void *)t2p->tiff_pages, t2p->tiff_pagecount, sizeof(T2P_PAGE), |
| 1259 |
t2p_cmp_t2p_page); |
| 1260 |
|
| 1261 |
for (i = 0; i < t2p->tiff_pagecount; i++) |
| 1262 |
{ |
| 1263 |
t2p->pdf_xrefcount += 5; |
| 1264 |
TIFFSetDirectory(input, t2p->tiff_pages[i].page_directory); |
| 1265 |
if ((TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &xuint16) && |
| 1266 |
(xuint16 == PHOTOMETRIC_PALETTE)) || |
| 1267 |
TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)) |
| 1268 |
{ |
| 1269 |
t2p->tiff_pages[i].page_extra++; |
| 1270 |
t2p->pdf_xrefcount++; |
| 1271 |
} |
| 1272 |
#ifdef ZIP_SUPPORT |
| 1273 |
if (TIFFGetField(input, TIFFTAG_COMPRESSION, &xuint16)) |
| 1274 |
{ |
| 1275 |
if ((xuint16 == COMPRESSION_DEFLATE || |
| 1276 |
xuint16 == COMPRESSION_ADOBE_DEFLATE) && |
| 1277 |
((t2p->tiff_pages[i].page_tilecount != 0) || |
| 1278 |
TIFFNumberOfStrips(input) == 1) && |
| 1279 |
(t2p->pdf_nopassthrough == 0)) |
| 1280 |
{ |
| 1281 |
if (t2p->pdf_minorversion < 2) |
| 1282 |
{ |
| 1283 |
t2p->pdf_minorversion = 2; |
| 1284 |
} |
| 1285 |
} |
| 1286 |
} |
| 1287 |
#endif |
| 1288 |
if (TIFFGetField( |
| 1289 |
input, TIFFTAG_TRANSFERFUNCTION, &(tiff_transferfunction[0]), |
| 1290 |
&(tiff_transferfunction[1]), &(tiff_transferfunction[2]))) |
| 1291 |
{ |
| 1292 |
|
| 1293 |
if ((tiff_transferfunction[1] != (uint16_t *)NULL) && |
| 1294 |
(tiff_transferfunction[2] != (uint16_t *)NULL)) |
| 1295 |
{ |
| 1296 |
tiff_transferfunctioncount = 3; |
| 1297 |
} |
| 1298 |
else |
| 1299 |
{ |
| 1300 |
tiff_transferfunctioncount = 1; |
| 1301 |
} |
| 1302 |
} |
| 1303 |
else |
| 1304 |
{ |
| 1305 |
tiff_transferfunctioncount = 0; |
| 1306 |
} |
| 1307 |
|
| 1308 |
if (i > 0) |
| 1309 |
{ |
| 1310 |
if (tiff_transferfunctioncount != t2p->tiff_transferfunctioncount) |
| 1311 |
{ |
| 1312 |
TIFFError(TIFF2PDF_MODULE, |
| 1313 |
"Different transfer function on page %" PRIu16, i); |
| 1314 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1315 |
return; |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
t2p->tiff_transferfunctioncount = tiff_transferfunctioncount; |
| 1320 |
t2p->tiff_transferfunction[0] = tiff_transferfunction[0]; |
| 1321 |
t2p->tiff_transferfunction[1] = tiff_transferfunction[1]; |
| 1322 |
t2p->tiff_transferfunction[2] = tiff_transferfunction[2]; |
| 1323 |
if (tiff_transferfunctioncount == 3) |
| 1324 |
{ |
| 1325 |
t2p->tiff_pages[i].page_extra += 4; |
| 1326 |
t2p->pdf_xrefcount += 4; |
| 1327 |
if (t2p->pdf_minorversion < 2) |
| 1328 |
t2p->pdf_minorversion = 2; |
| 1329 |
} |
| 1330 |
else if (tiff_transferfunctioncount == 1) |
| 1331 |
{ |
| 1332 |
t2p->tiff_pages[i].page_extra += 2; |
| 1333 |
t2p->pdf_xrefcount += 2; |
| 1334 |
if (t2p->pdf_minorversion < 2) |
| 1335 |
t2p->pdf_minorversion = 2; |
| 1336 |
} |
| 1337 |
|
| 1338 |
if (TIFFGetField(input, TIFFTAG_ICCPROFILE, |
| 1339 |
&(t2p->tiff_iccprofilelength), |
| 1340 |
&(t2p->tiff_iccprofile)) != 0) |
| 1341 |
{ |
| 1342 |
t2p->tiff_pages[i].page_extra++; |
| 1343 |
t2p->pdf_xrefcount++; |
| 1344 |
if (t2p->pdf_minorversion < 3) |
| 1345 |
{ |
| 1346 |
t2p->pdf_minorversion = 3; |
| 1347 |
} |
| 1348 |
} |
| 1349 |
t2p->tiff_tiles[i].tiles_tilecount = t2p->tiff_pages[i].page_tilecount; |
| 1350 |
if ((TIFFGetField(input, TIFFTAG_PLANARCONFIG, &xuint16) != 0) && |
| 1351 |
(xuint16 == PLANARCONFIG_SEPARATE)) |
| 1352 |
{ |
| 1353 |
if (!TIFFGetField(input, TIFFTAG_SAMPLESPERPIXEL, &xuint16)) |
| 1354 |
{ |
| 1355 |
TIFFError(TIFF2PDF_MODULE, "Missing SamplesPerPixel, %s", |
| 1356 |
TIFFFileName(input)); |
| 1357 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1358 |
return; |
| 1359 |
} |
| 1360 |
if ((t2p->tiff_tiles[i].tiles_tilecount % xuint16) != 0) |
| 1361 |
{ |
| 1362 |
TIFFError(TIFF2PDF_MODULE, "Invalid tile count, %s", |
| 1363 |
TIFFFileName(input)); |
| 1364 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1365 |
return; |
| 1366 |
} |
| 1367 |
t2p->tiff_tiles[i].tiles_tilecount /= xuint16; |
| 1368 |
} |
| 1369 |
if (t2p->tiff_tiles[i].tiles_tilecount > 0) |
| 1370 |
{ |
| 1371 |
t2p->pdf_xrefcount += (t2p->tiff_tiles[i].tiles_tilecount - 1) * 2; |
| 1372 |
TIFFGetField(input, TIFFTAG_TILEWIDTH, |
| 1373 |
&(t2p->tiff_tiles[i].tiles_tilewidth)); |
| 1374 |
if (t2p->tiff_tiles[i].tiles_tilewidth < 1) |
| 1375 |
{ |
| 1376 |
TIFFError(TIFF2PDF_MODULE, "Invalid tile width (%d), %s", |
| 1377 |
t2p->tiff_tiles[i].tiles_tilewidth, |
| 1378 |
TIFFFileName(input)); |
| 1379 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1380 |
return; |
| 1381 |
} |
| 1382 |
TIFFGetField(input, TIFFTAG_TILELENGTH, |
| 1383 |
&(t2p->tiff_tiles[i].tiles_tilelength)); |
| 1384 |
if (t2p->tiff_tiles[i].tiles_tilelength < 1) |
| 1385 |
{ |
| 1386 |
TIFFError(TIFF2PDF_MODULE, "Invalid tile length (%d), %s", |
| 1387 |
t2p->tiff_tiles[i].tiles_tilelength, |
| 1388 |
TIFFFileName(input)); |
| 1389 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1390 |
return; |
| 1391 |
} |
| 1392 |
t2p->tiff_tiles[i].tiles_tiles = (T2P_TILE *)_TIFFmalloc( |
| 1393 |
TIFFSafeMultiply(tmsize_t, t2p->tiff_tiles[i].tiles_tilecount, |
| 1394 |
sizeof(T2P_TILE))); |
| 1395 |
if (t2p->tiff_tiles[i].tiles_tiles == NULL) |
| 1396 |
{ |
| 1397 |
TIFFError(TIFF2PDF_MODULE, |
| 1398 |
"Can't allocate %" TIFF_SIZE_FORMAT |
| 1399 |
" bytes of memory for t2p_read_tiff_init, %s", |
| 1400 |
sizeof(T2P_TILE) * t2p->tiff_tiles[i].tiles_tilecount, |
| 1401 |
TIFFFileName(input)); |
| 1402 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1403 |
return; |
| 1404 |
} |
| 1405 |
} |
| 1406 |
} |
| 1407 |
|
| 1408 |
return; |
| 1409 |
} |
| 1410 |
|
| 1411 |
/* |
| 1412 |
* This function is used by qsort to sort a T2P_PAGE* array of page structures |
| 1413 |
* by page number. If the page numbers are the same, we fall back to comparing |
| 1414 |
* directory numbers to preserve the order of the input file. |
| 1415 |
*/ |
| 1416 |
|
| 1417 |
int t2p_cmp_t2p_page(const void *e1, const void *e2) |
| 1418 |
{ |
| 1419 |
|
| 1420 |
int d; |
| 1421 |
d = (int32_t)(((T2P_PAGE *)e1)->page_number) - |
| 1422 |
(int32_t)(((T2P_PAGE *)e2)->page_number); |
| 1423 |
if (d == 0) |
| 1424 |
{ |
| 1425 |
d = (int32_t)(((T2P_PAGE *)e1)->page_directory) - |
| 1426 |
(int32_t)(((T2P_PAGE *)e2)->page_directory); |
| 1427 |
} |
| 1428 |
return d; |
| 1429 |
} |
| 1430 |
|
| 1431 |
/* |
| 1432 |
This function sets the input directory to the directory of a given |
| 1433 |
page and determines information about the image. It checks |
| 1434 |
the image characteristics to determine if it is possible to convert |
| 1435 |
the image data into a page of PDF output, setting values of the T2P |
| 1436 |
struct for this page. It determines what color space is used in |
| 1437 |
the output PDF to represent the image. |
| 1438 |
|
| 1439 |
It determines if the image can be converted as raw data without |
| 1440 |
requiring transcoding of the image data. |
| 1441 |
*/ |
| 1442 |
|
| 1443 |
void t2p_read_tiff_data(T2P *t2p, TIFF *input) |
| 1444 |
{ |
| 1445 |
|
| 1446 |
int i = 0; |
| 1447 |
uint16_t *r = NULL; |
| 1448 |
uint16_t *g = NULL; |
| 1449 |
uint16_t *b = NULL; |
| 1450 |
uint16_t *a = NULL; |
| 1451 |
uint16_t xuint16; |
| 1452 |
uint16_t *xuint16p; |
| 1453 |
float *xfloatp; |
| 1454 |
int palette_16bit = 0; |
| 1455 |
int palette_shift = 8; |
| 1456 |
|
| 1457 |
t2p->pdf_transcode = T2P_TRANSCODE_ENCODE; |
| 1458 |
t2p->pdf_sample = T2P_SAMPLE_NOTHING; |
| 1459 |
t2p->pdf_switchdecode = t2p->pdf_colorspace_invert; |
| 1460 |
|
| 1461 |
TIFFSetDirectory(input, t2p->tiff_pages[t2p->pdf_page].page_directory); |
| 1462 |
|
| 1463 |
TIFFGetField(input, TIFFTAG_IMAGEWIDTH, &(t2p->tiff_width)); |
| 1464 |
if (t2p->tiff_width == 0) |
| 1465 |
{ |
| 1466 |
TIFFError(TIFF2PDF_MODULE, "No support for %s with zero width", |
| 1467 |
TIFFFileName(input)); |
| 1468 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1469 |
return; |
| 1470 |
} |
| 1471 |
|
| 1472 |
TIFFGetField(input, TIFFTAG_IMAGELENGTH, &(t2p->tiff_length)); |
| 1473 |
if (t2p->tiff_length == 0) |
| 1474 |
{ |
| 1475 |
TIFFError(TIFF2PDF_MODULE, "No support for %s with zero length", |
| 1476 |
TIFFFileName(input)); |
| 1477 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1478 |
return; |
| 1479 |
} |
| 1480 |
|
| 1481 |
if (TIFFGetField(input, TIFFTAG_COMPRESSION, &(t2p->tiff_compression)) == 0) |
| 1482 |
{ |
| 1483 |
TIFFError(TIFF2PDF_MODULE, "No support for %s with no compression tag", |
| 1484 |
TIFFFileName(input)); |
| 1485 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1486 |
return; |
| 1487 |
} |
| 1488 |
if (TIFFIsCODECConfigured(t2p->tiff_compression) == 0) |
| 1489 |
{ |
| 1490 |
TIFFError(TIFF2PDF_MODULE, |
| 1491 |
"No support for %s with compression type %" PRIu16 |
| 1492 |
": not configured", |
| 1493 |
TIFFFileName(input), t2p->tiff_compression); |
| 1494 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1495 |
return; |
| 1496 |
} |
| 1497 |
|
| 1498 |
TIFFGetFieldDefaulted(input, TIFFTAG_BITSPERSAMPLE, |
| 1499 |
&(t2p->tiff_bitspersample)); |
| 1500 |
switch (t2p->tiff_bitspersample) |
| 1501 |
{ |
| 1502 |
case 1: |
| 1503 |
case 2: |
| 1504 |
case 4: |
| 1505 |
case 8: |
| 1506 |
break; |
| 1507 |
case 0: |
| 1508 |
TIFFWarning(TIFF2PDF_MODULE, |
| 1509 |
"Image %s has 0 bits per sample, assuming 1", |
| 1510 |
TIFFFileName(input)); |
| 1511 |
t2p->tiff_bitspersample = 1; |
| 1512 |
break; |
| 1513 |
default: |
| 1514 |
TIFFError(TIFF2PDF_MODULE, |
| 1515 |
"No support for %s with %" PRIu16 " bits per sample", |
| 1516 |
TIFFFileName(input), t2p->tiff_bitspersample); |
| 1517 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1518 |
return; |
| 1519 |
} |
| 1520 |
|
| 1521 |
TIFFGetFieldDefaulted(input, TIFFTAG_SAMPLESPERPIXEL, |
| 1522 |
&(t2p->tiff_samplesperpixel)); |
| 1523 |
if (t2p->tiff_samplesperpixel > 4) |
| 1524 |
{ |
| 1525 |
TIFFError(TIFF2PDF_MODULE, |
| 1526 |
"No support for %s with %" PRIu16 " samples per pixel", |
| 1527 |
TIFFFileName(input), t2p->tiff_samplesperpixel); |
| 1528 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1529 |
return; |
| 1530 |
} |
| 1531 |
if (t2p->tiff_samplesperpixel == 0) |
| 1532 |
{ |
| 1533 |
TIFFWarning(TIFF2PDF_MODULE, |
| 1534 |
"Image %s has 0 samples per pixel, assuming 1", |
| 1535 |
TIFFFileName(input)); |
| 1536 |
t2p->tiff_samplesperpixel = 1; |
| 1537 |
} |
| 1538 |
|
| 1539 |
if (TIFFGetField(input, TIFFTAG_SAMPLEFORMAT, &xuint16) != 0) |
| 1540 |
{ |
| 1541 |
switch (xuint16) |
| 1542 |
{ |
| 1543 |
case 0: |
| 1544 |
case 1: |
| 1545 |
case 4: |
| 1546 |
break; |
| 1547 |
default: |
| 1548 |
TIFFError(TIFF2PDF_MODULE, |
| 1549 |
"No support for %s with sample format %" PRIu16, |
| 1550 |
TIFFFileName(input), xuint16); |
| 1551 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1552 |
return; |
| 1553 |
break; |
| 1554 |
} |
| 1555 |
} |
| 1556 |
|
| 1557 |
TIFFGetFieldDefaulted(input, TIFFTAG_FILLORDER, &(t2p->tiff_fillorder)); |
| 1558 |
|
| 1559 |
if (TIFFGetField(input, TIFFTAG_PHOTOMETRIC, &(t2p->tiff_photometric)) == 0) |
| 1560 |
{ |
| 1561 |
TIFFError(TIFF2PDF_MODULE, |
| 1562 |
"No support for %s with no photometric interpretation tag", |
| 1563 |
TIFFFileName(input)); |
| 1564 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1565 |
return; |
| 1566 |
} |
| 1567 |
|
| 1568 |
switch (t2p->tiff_photometric) |
| 1569 |
{ |
| 1570 |
case PHOTOMETRIC_MINISWHITE: |
| 1571 |
case PHOTOMETRIC_MINISBLACK: |
| 1572 |
if (t2p->tiff_bitspersample == 1) |
| 1573 |
{ |
| 1574 |
t2p->pdf_colorspace = T2P_CS_BILEVEL; |
| 1575 |
if (t2p->tiff_photometric == PHOTOMETRIC_MINISWHITE) |
| 1576 |
{ |
| 1577 |
t2p->pdf_switchdecode ^= 1; |
| 1578 |
} |
| 1579 |
} |
| 1580 |
else |
| 1581 |
{ |
| 1582 |
t2p->pdf_colorspace = T2P_CS_GRAY; |
| 1583 |
if (t2p->tiff_photometric == PHOTOMETRIC_MINISWHITE) |
| 1584 |
{ |
| 1585 |
t2p->pdf_switchdecode ^= 1; |
| 1586 |
} |
| 1587 |
} |
| 1588 |
break; |
| 1589 |
case PHOTOMETRIC_RGB: |
| 1590 |
t2p->pdf_colorspace = T2P_CS_RGB; |
| 1591 |
if (t2p->tiff_samplesperpixel == 3) |
| 1592 |
{ |
| 1593 |
break; |
| 1594 |
} |
| 1595 |
if (TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)) |
| 1596 |
{ |
| 1597 |
if (xuint16 == 1) |
| 1598 |
goto photometric_palette; |
| 1599 |
} |
| 1600 |
if (t2p->tiff_samplesperpixel > 3) |
| 1601 |
{ |
| 1602 |
if (t2p->tiff_samplesperpixel == 4) |
| 1603 |
{ |
| 1604 |
t2p->pdf_colorspace = T2P_CS_RGB; |
| 1605 |
if (TIFFGetField(input, TIFFTAG_EXTRASAMPLES, &xuint16, |
| 1606 |
&xuint16p) && |
| 1607 |
xuint16 == 1) |
| 1608 |
{ |
| 1609 |
if (xuint16p[0] == EXTRASAMPLE_ASSOCALPHA) |
| 1610 |
{ |
| 1611 |
if (t2p->tiff_bitspersample != 8) |
| 1612 |
{ |
| 1613 |
TIFFError( |
| 1614 |
TIFF2PDF_MODULE, |
| 1615 |
"No support for BitsPerSample=%" PRIu16 |
| 1616 |
" for RGBA", |
| 1617 |
t2p->tiff_bitspersample); |
| 1618 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1619 |
return; |
| 1620 |
} |
| 1621 |
t2p->pdf_sample = T2P_SAMPLE_RGBAA_TO_RGB; |
| 1622 |
break; |
| 1623 |
} |
| 1624 |
if (xuint16p[0] == EXTRASAMPLE_UNASSALPHA) |
| 1625 |
{ |
| 1626 |
if (t2p->tiff_bitspersample != 8) |
| 1627 |
{ |
| 1628 |
TIFFError( |
| 1629 |
TIFF2PDF_MODULE, |
| 1630 |
"No support for BitsPerSample=%" PRIu16 |
| 1631 |
" for RGBA", |
| 1632 |
t2p->tiff_bitspersample); |
| 1633 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1634 |
return; |
| 1635 |
} |
| 1636 |
t2p->pdf_sample = T2P_SAMPLE_RGBA_TO_RGB; |
| 1637 |
break; |
| 1638 |
} |
| 1639 |
TIFFWarning(TIFF2PDF_MODULE, |
| 1640 |
"RGB image %s has 4 samples per pixel, " |
| 1641 |
"assuming RGBA", |
| 1642 |
TIFFFileName(input)); |
| 1643 |
break; |
| 1644 |
} |
| 1645 |
t2p->pdf_colorspace = T2P_CS_CMYK; |
| 1646 |
t2p->pdf_switchdecode ^= 1; |
| 1647 |
TIFFWarning(TIFF2PDF_MODULE, |
| 1648 |
"RGB image %s has 4 samples per pixel, " |
| 1649 |
"assuming inverse CMYK", |
| 1650 |
TIFFFileName(input)); |
| 1651 |
break; |
| 1652 |
} |
| 1653 |
else |
| 1654 |
{ |
| 1655 |
TIFFError(TIFF2PDF_MODULE, |
| 1656 |
"No support for RGB image %s with %" PRIu16 |
| 1657 |
" samples per pixel", |
| 1658 |
TIFFFileName(input), t2p->tiff_samplesperpixel); |
| 1659 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1660 |
break; |
| 1661 |
} |
| 1662 |
} |
| 1663 |
else |
| 1664 |
{ |
| 1665 |
TIFFError(TIFF2PDF_MODULE, |
| 1666 |
"No support for RGB image %s with %" PRIu16 |
| 1667 |
" samples per pixel", |
| 1668 |
TIFFFileName(input), t2p->tiff_samplesperpixel); |
| 1669 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1670 |
break; |
| 1671 |
} |
| 1672 |
case PHOTOMETRIC_PALETTE: |
| 1673 |
photometric_palette: |
| 1674 |
if (t2p->tiff_samplesperpixel != 1) |
| 1675 |
{ |
| 1676 |
TIFFError(TIFF2PDF_MODULE, |
| 1677 |
"No support for palettized image %s with not one " |
| 1678 |
"sample per pixel", |
| 1679 |
TIFFFileName(input)); |
| 1680 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1681 |
return; |
| 1682 |
} |
| 1683 |
t2p->pdf_colorspace = T2P_CS_RGB | T2P_CS_PALETTE; |
| 1684 |
t2p->pdf_palettesize = 0x0001 << t2p->tiff_bitspersample; |
| 1685 |
if (!TIFFGetField(input, TIFFTAG_COLORMAP, &r, &g, &b)) |
| 1686 |
{ |
| 1687 |
TIFFError(TIFF2PDF_MODULE, |
| 1688 |
"Palettized image %s has no color map", |
| 1689 |
TIFFFileName(input)); |
| 1690 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1691 |
return; |
| 1692 |
} |
| 1693 |
if (r == NULL || g == NULL || b == NULL) |
| 1694 |
{ |
| 1695 |
TIFFError(TIFF2PDF_MODULE, |
| 1696 |
"Error getting 3 components from color map"); |
| 1697 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1698 |
return; |
| 1699 |
} |
| 1700 |
if (t2p->pdf_palette != NULL) |
| 1701 |
{ |
| 1702 |
_TIFFfree(t2p->pdf_palette); |
| 1703 |
t2p->pdf_palette = NULL; |
| 1704 |
} |
| 1705 |
t2p->pdf_palette = (unsigned char *)_TIFFmalloc( |
| 1706 |
TIFFSafeMultiply(tmsize_t, t2p->pdf_palettesize, 3)); |
| 1707 |
if (t2p->pdf_palette == NULL) |
| 1708 |
{ |
| 1709 |
TIFFError(TIFF2PDF_MODULE, |
| 1710 |
"Can't allocate %" PRIu16 |
| 1711 |
" bytes of memory for t2p_read_tiff_image, %s", |
| 1712 |
t2p->pdf_palettesize, TIFFFileName(input)); |
| 1713 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1714 |
return; |
| 1715 |
} |
| 1716 |
/* Some older tiffs may have colors in the palette |
| 1717 |
* ranging from 0 to 255 rather than 0 to 65535. */ |
| 1718 |
for (i = 0; i < t2p->pdf_palettesize; i++) |
| 1719 |
{ |
| 1720 |
if ((r[i] > 255) || (g[i] > 255) || (b[i] > 255)) |
| 1721 |
{ |
| 1722 |
palette_16bit = 1; |
| 1723 |
break; |
| 1724 |
} |
| 1725 |
} |
| 1726 |
if (palette_16bit == 0) |
| 1727 |
{ |
| 1728 |
TIFFWarning(TIFFFileName(input), "Assuming 8-bit colormap"); |
| 1729 |
palette_shift = 0; |
| 1730 |
} |
| 1731 |
for (i = 0; i < t2p->pdf_palettesize; i++) |
| 1732 |
{ |
| 1733 |
t2p->pdf_palette[(i * 3)] = |
| 1734 |
(unsigned char)(r[i] >> palette_shift); |
| 1735 |
t2p->pdf_palette[(i * 3) + 1] = |
| 1736 |
(unsigned char)(g[i] >> palette_shift); |
| 1737 |
t2p->pdf_palette[(i * 3) + 2] = |
| 1738 |
(unsigned char)(b[i] >> palette_shift); |
| 1739 |
} |
| 1740 |
t2p->pdf_palettesize *= 3; |
| 1741 |
break; |
| 1742 |
case PHOTOMETRIC_SEPARATED: |
| 1743 |
if (TIFFGetField(input, TIFFTAG_INDEXED, &xuint16)) |
| 1744 |
{ |
| 1745 |
if (xuint16 == 1) |
| 1746 |
{ |
| 1747 |
goto photometric_palette_cmyk; |
| 1748 |
} |
| 1749 |
} |
| 1750 |
if (TIFFGetField(input, TIFFTAG_INKSET, &xuint16)) |
| 1751 |
{ |
| 1752 |
if (xuint16 != INKSET_CMYK) |
| 1753 |
{ |
| 1754 |
TIFFError( |
| 1755 |
TIFF2PDF_MODULE, |
| 1756 |
"No support for %s because its inkset is not CMYK", |
| 1757 |
TIFFFileName(input)); |
| 1758 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1759 |
return; |
| 1760 |
} |
| 1761 |
} |
| 1762 |
if (t2p->tiff_samplesperpixel == 4) |
| 1763 |
{ |
| 1764 |
t2p->pdf_colorspace = T2P_CS_CMYK; |
| 1765 |
} |
| 1766 |
else |
| 1767 |
{ |
| 1768 |
TIFFError(TIFF2PDF_MODULE, |
| 1769 |
"No support for %s because it has %" PRIu16 |
| 1770 |
" samples per pixel", |
| 1771 |
TIFFFileName(input), t2p->tiff_samplesperpixel); |
| 1772 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1773 |
return; |
| 1774 |
} |
| 1775 |
break; |
| 1776 |
photometric_palette_cmyk: |
| 1777 |
if (t2p->tiff_samplesperpixel != 1) |
| 1778 |
{ |
| 1779 |
TIFFError(TIFF2PDF_MODULE, |
| 1780 |
"No support for palettized CMYK image %s with not " |
| 1781 |
"one sample per pixel", |
| 1782 |
TIFFFileName(input)); |
| 1783 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1784 |
return; |
| 1785 |
} |
| 1786 |
t2p->pdf_colorspace = T2P_CS_CMYK | T2P_CS_PALETTE; |
| 1787 |
t2p->pdf_palettesize = 0x0001 << t2p->tiff_bitspersample; |
| 1788 |
if (!TIFFGetField(input, TIFFTAG_COLORMAP, &r, &g, &b, &a)) |
| 1789 |
{ |
| 1790 |
TIFFError(TIFF2PDF_MODULE, |
| 1791 |
"Palettized image %s has no color map", |
| 1792 |
TIFFFileName(input)); |
| 1793 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1794 |
return; |
| 1795 |
} |
| 1796 |
if (r == NULL || g == NULL || b == NULL || a == NULL) |
| 1797 |
{ |
| 1798 |
TIFFError(TIFF2PDF_MODULE, |
| 1799 |
"Error getting 4 components from color map"); |
| 1800 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1801 |
return; |
| 1802 |
} |
| 1803 |
if (t2p->pdf_palette != NULL) |
| 1804 |
{ |
| 1805 |
_TIFFfree(t2p->pdf_palette); |
| 1806 |
t2p->pdf_palette = NULL; |
| 1807 |
} |
| 1808 |
t2p->pdf_palette = (unsigned char *)_TIFFmalloc( |
| 1809 |
TIFFSafeMultiply(tmsize_t, t2p->pdf_palettesize, 4)); |
| 1810 |
if (t2p->pdf_palette == NULL) |
| 1811 |
{ |
| 1812 |
TIFFError(TIFF2PDF_MODULE, |
| 1813 |
"Can't allocate %" PRIu16 |
| 1814 |
" bytes of memory for t2p_read_tiff_image, %s", |
| 1815 |
t2p->pdf_palettesize, TIFFFileName(input)); |
| 1816 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1817 |
return; |
| 1818 |
} |
| 1819 |
for (i = 0; i < t2p->pdf_palettesize; i++) |
| 1820 |
{ |
| 1821 |
t2p->pdf_palette[(i * 4)] = (unsigned char)(r[i] >> 8); |
| 1822 |
t2p->pdf_palette[(i * 4) + 1] = (unsigned char)(g[i] >> 8); |
| 1823 |
t2p->pdf_palette[(i * 4) + 2] = (unsigned char)(b[i] >> 8); |
| 1824 |
t2p->pdf_palette[(i * 4) + 3] = (unsigned char)(a[i] >> 8); |
| 1825 |
} |
| 1826 |
t2p->pdf_palettesize *= 4; |
| 1827 |
break; |
| 1828 |
case PHOTOMETRIC_YCBCR: |
| 1829 |
t2p->pdf_colorspace = T2P_CS_RGB; |
| 1830 |
if (t2p->tiff_samplesperpixel == 1) |
| 1831 |
{ |
| 1832 |
t2p->pdf_colorspace = T2P_CS_GRAY; |
| 1833 |
t2p->tiff_photometric = PHOTOMETRIC_MINISBLACK; |
| 1834 |
break; |
| 1835 |
} |
| 1836 |
t2p->pdf_sample = T2P_SAMPLE_YCBCR_TO_RGB; |
| 1837 |
#ifdef JPEG_SUPPORT |
| 1838 |
if (t2p->pdf_defaultcompression == T2P_COMPRESS_JPEG) |
| 1839 |
{ |
| 1840 |
t2p->pdf_sample = T2P_SAMPLE_NOTHING; |
| 1841 |
} |
| 1842 |
#endif |
| 1843 |
break; |
| 1844 |
case PHOTOMETRIC_CIELAB: |
| 1845 |
if (t2p->tiff_samplesperpixel != 3) |
| 1846 |
{ |
| 1847 |
TIFFError(TIFF2PDF_MODULE, |
| 1848 |
"Unsupported samplesperpixel = %" PRIu16 |
| 1849 |
" for CIELAB", |
| 1850 |
t2p->tiff_samplesperpixel); |
| 1851 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1852 |
return; |
| 1853 |
} |
| 1854 |
if (t2p->tiff_bitspersample != 8) |
| 1855 |
{ |
| 1856 |
TIFFError(TIFF2PDF_MODULE, |
| 1857 |
"Invalid bitspersample = %" PRIu16 " for CIELAB", |
| 1858 |
t2p->tiff_bitspersample); |
| 1859 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1860 |
return; |
| 1861 |
} |
| 1862 |
t2p->pdf_labrange[0] = -127; |
| 1863 |
t2p->pdf_labrange[1] = 127; |
| 1864 |
t2p->pdf_labrange[2] = -127; |
| 1865 |
t2p->pdf_labrange[3] = 127; |
| 1866 |
t2p->pdf_sample = T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED; |
| 1867 |
t2p->pdf_colorspace = T2P_CS_LAB; |
| 1868 |
break; |
| 1869 |
case PHOTOMETRIC_ICCLAB: |
| 1870 |
t2p->pdf_labrange[0] = 0; |
| 1871 |
t2p->pdf_labrange[1] = 255; |
| 1872 |
t2p->pdf_labrange[2] = 0; |
| 1873 |
t2p->pdf_labrange[3] = 255; |
| 1874 |
t2p->pdf_colorspace = T2P_CS_LAB; |
| 1875 |
break; |
| 1876 |
case PHOTOMETRIC_ITULAB: |
| 1877 |
if (t2p->tiff_samplesperpixel != 3) |
| 1878 |
{ |
| 1879 |
TIFFError(TIFF2PDF_MODULE, |
| 1880 |
"Unsupported samplesperpixel = %" PRIu16 |
| 1881 |
" for ITULAB", |
| 1882 |
t2p->tiff_samplesperpixel); |
| 1883 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1884 |
return; |
| 1885 |
} |
| 1886 |
if (t2p->tiff_bitspersample != 8) |
| 1887 |
{ |
| 1888 |
TIFFError(TIFF2PDF_MODULE, |
| 1889 |
"Invalid bitspersample = %" PRIu16 " for ITULAB", |
| 1890 |
t2p->tiff_bitspersample); |
| 1891 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1892 |
return; |
| 1893 |
} |
| 1894 |
t2p->pdf_labrange[0] = -85; |
| 1895 |
t2p->pdf_labrange[1] = 85; |
| 1896 |
t2p->pdf_labrange[2] = -75; |
| 1897 |
t2p->pdf_labrange[3] = 124; |
| 1898 |
t2p->pdf_sample = T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED; |
| 1899 |
t2p->pdf_colorspace = T2P_CS_LAB; |
| 1900 |
break; |
| 1901 |
case PHOTOMETRIC_LOGL: |
| 1902 |
case PHOTOMETRIC_LOGLUV: |
| 1903 |
TIFFError( |
| 1904 |
TIFF2PDF_MODULE, |
| 1905 |
"No support for %s with photometric interpretation LogL/LogLuv", |
| 1906 |
TIFFFileName(input)); |
| 1907 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1908 |
return; |
| 1909 |
default: |
| 1910 |
TIFFError( |
| 1911 |
TIFF2PDF_MODULE, |
| 1912 |
"No support for %s with photometric interpretation %" PRIu16, |
| 1913 |
TIFFFileName(input), t2p->tiff_photometric); |
| 1914 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1915 |
return; |
| 1916 |
} |
| 1917 |
|
| 1918 |
if (TIFFGetField(input, TIFFTAG_PLANARCONFIG, &(t2p->tiff_planar))) |
| 1919 |
{ |
| 1920 |
switch (t2p->tiff_planar) |
| 1921 |
{ |
| 1922 |
case 0: |
| 1923 |
TIFFWarning(TIFF2PDF_MODULE, |
| 1924 |
"Image %s has planar configuration 0, assuming 1", |
| 1925 |
TIFFFileName(input)); |
| 1926 |
t2p->tiff_planar = PLANARCONFIG_CONTIG; |
| 1927 |
case PLANARCONFIG_CONTIG: |
| 1928 |
break; |
| 1929 |
case PLANARCONFIG_SEPARATE: |
| 1930 |
t2p->pdf_sample = T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG; |
| 1931 |
if (t2p->tiff_bitspersample != 8) |
| 1932 |
{ |
| 1933 |
TIFFError(TIFF2PDF_MODULE, |
| 1934 |
"No support for %s with separated planar " |
| 1935 |
"configuration and %" PRIu16 " bits per sample", |
| 1936 |
TIFFFileName(input), t2p->tiff_bitspersample); |
| 1937 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1938 |
return; |
| 1939 |
} |
| 1940 |
break; |
| 1941 |
default: |
| 1942 |
TIFFError( |
| 1943 |
TIFF2PDF_MODULE, |
| 1944 |
"No support for %s with planar configuration %" PRIu16, |
| 1945 |
TIFFFileName(input), t2p->tiff_planar); |
| 1946 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 1947 |
return; |
| 1948 |
} |
| 1949 |
} |
| 1950 |
|
| 1951 |
TIFFGetFieldDefaulted(input, TIFFTAG_ORIENTATION, &(t2p->tiff_orientation)); |
| 1952 |
if (t2p->tiff_orientation > 8) |
| 1953 |
{ |
| 1954 |
TIFFWarning(TIFF2PDF_MODULE, |
| 1955 |
"Image %s has orientation %" PRIu16 ", assuming 0", |
| 1956 |
TIFFFileName(input), t2p->tiff_orientation); |
| 1957 |
t2p->tiff_orientation = 0; |
| 1958 |
} |
| 1959 |
|
| 1960 |
if (TIFFGetField(input, TIFFTAG_XRESOLUTION, &(t2p->tiff_xres)) == 0) |
| 1961 |
{ |
| 1962 |
t2p->tiff_xres = 0.0; |
| 1963 |
} |
| 1964 |
if (TIFFGetField(input, TIFFTAG_YRESOLUTION, &(t2p->tiff_yres)) == 0) |
| 1965 |
{ |
| 1966 |
t2p->tiff_yres = 0.0; |
| 1967 |
} |
| 1968 |
TIFFGetFieldDefaulted(input, TIFFTAG_RESOLUTIONUNIT, &(t2p->tiff_resunit)); |
| 1969 |
if (t2p->tiff_resunit == RESUNIT_CENTIMETER) |
| 1970 |
{ |
| 1971 |
t2p->tiff_xres *= 2.54F; |
| 1972 |
t2p->tiff_yres *= 2.54F; |
| 1973 |
} |
| 1974 |
else if (t2p->tiff_resunit != RESUNIT_INCH && t2p->pdf_centimeters != 0) |
| 1975 |
{ |
| 1976 |
t2p->tiff_xres *= 2.54F; |
| 1977 |
t2p->tiff_yres *= 2.54F; |
| 1978 |
} |
| 1979 |
|
| 1980 |
t2p_compose_pdf_page(t2p); |
| 1981 |
if (t2p->t2p_error == T2P_ERR_ERROR) |
| 1982 |
return; |
| 1983 |
|
| 1984 |
t2p->pdf_transcode = T2P_TRANSCODE_ENCODE; |
| 1985 |
/* It seems that T2P_TRANSCODE_RAW mode doesn't support separate->contig */ |
| 1986 |
/* conversion. At least t2p_read_tiff_size and t2p_read_tiff_size_tile */ |
| 1987 |
/* do not take into account the number of samples, and thus */ |
| 1988 |
/* that can cause heap buffer overflows such as in */ |
| 1989 |
/* http://bugzilla.maptools.org/show_bug.cgi?id=2715 */ |
| 1990 |
if (t2p->pdf_nopassthrough == 0 && |
| 1991 |
t2p->tiff_planar != PLANARCONFIG_SEPARATE) |
| 1992 |
{ |
| 1993 |
#ifdef CCITT_SUPPORT |
| 1994 |
if (t2p->tiff_compression == COMPRESSION_CCITTFAX4) |
| 1995 |
{ |
| 1996 |
if (TIFFIsTiled(input) || (TIFFNumberOfStrips(input) == 1)) |
| 1997 |
{ |
| 1998 |
t2p->pdf_transcode = T2P_TRANSCODE_RAW; |
| 1999 |
t2p->pdf_compression = T2P_COMPRESS_G4; |
| 2000 |
} |
| 2001 |
} |
| 2002 |
#endif |
| 2003 |
#ifdef ZIP_SUPPORT |
| 2004 |
if (t2p->tiff_compression == COMPRESSION_ADOBE_DEFLATE || |
| 2005 |
t2p->tiff_compression == COMPRESSION_DEFLATE) |
| 2006 |
{ |
| 2007 |
if (TIFFIsTiled(input) || (TIFFNumberOfStrips(input) == 1)) |
| 2008 |
{ |
| 2009 |
uint16_t predictor; |
| 2010 |
t2p->pdf_transcode = T2P_TRANSCODE_RAW; |
| 2011 |
t2p->pdf_compression = T2P_COMPRESS_ZIP; |
| 2012 |
if (TIFFGetField(input, TIFFTAG_PREDICTOR, &predictor)) |
| 2013 |
{ |
| 2014 |
t2p->pdf_compressionquality = predictor; |
| 2015 |
} |
| 2016 |
else |
| 2017 |
{ |
| 2018 |
t2p->pdf_compressionquality = PREDICTOR_NONE; |
| 2019 |
} |
| 2020 |
/* TIFFTAG_ZIPQUALITY is always Z_DEFAULT_COMPRESSION on reading |
| 2021 |
*/ |
| 2022 |
} |
| 2023 |
} |
| 2024 |
#endif |
| 2025 |
#ifdef OJPEG_SUPPORT |
| 2026 |
if (t2p->tiff_compression == COMPRESSION_OJPEG) |
| 2027 |
{ |
| 2028 |
t2p->pdf_transcode = T2P_TRANSCODE_RAW; |
| 2029 |
t2p->pdf_compression = T2P_COMPRESS_JPEG; |
| 2030 |
t2p_process_ojpeg_tables(t2p, input); |
| 2031 |
} |
| 2032 |
#endif |
| 2033 |
#ifdef JPEG_SUPPORT |
| 2034 |
if (t2p->tiff_compression == COMPRESSION_JPEG) |
| 2035 |
{ |
| 2036 |
t2p->pdf_transcode = T2P_TRANSCODE_RAW; |
| 2037 |
t2p->pdf_compression = T2P_COMPRESS_JPEG; |
| 2038 |
} |
| 2039 |
#endif |
| 2040 |
(void)0; |
| 2041 |
} |
| 2042 |
|
| 2043 |
if (t2p->pdf_transcode != T2P_TRANSCODE_RAW) |
| 2044 |
{ |
| 2045 |
t2p->pdf_compression = t2p->pdf_defaultcompression; |
| 2046 |
} |
| 2047 |
|
| 2048 |
#ifdef JPEG_SUPPORT |
| 2049 |
if (t2p->pdf_defaultcompression == T2P_COMPRESS_JPEG) |
| 2050 |
{ |
| 2051 |
if (t2p->pdf_colorspace & T2P_CS_PALETTE) |
| 2052 |
{ |
| 2053 |
t2p->pdf_sample |= T2P_SAMPLE_REALIZE_PALETTE; |
| 2054 |
t2p->pdf_colorspace ^= T2P_CS_PALETTE; |
| 2055 |
t2p->tiff_pages[t2p->pdf_page].page_extra--; |
| 2056 |
} |
| 2057 |
} |
| 2058 |
if (t2p->tiff_compression == COMPRESSION_JPEG) |
| 2059 |
{ |
| 2060 |
if (t2p->tiff_planar == PLANARCONFIG_SEPARATE) |
| 2061 |
{ |
| 2062 |
TIFFError(TIFF2PDF_MODULE, |
| 2063 |
"No support for %s with JPEG compression and separated " |
| 2064 |
"planar configuration", |
| 2065 |
TIFFFileName(input)); |
| 2066 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2067 |
return; |
| 2068 |
} |
| 2069 |
} |
| 2070 |
#endif |
| 2071 |
#ifdef OJPEG_SUPPORT |
| 2072 |
if (t2p->tiff_compression == COMPRESSION_OJPEG) |
| 2073 |
{ |
| 2074 |
if (t2p->tiff_planar == PLANARCONFIG_SEPARATE) |
| 2075 |
{ |
| 2076 |
TIFFError(TIFF2PDF_MODULE, |
| 2077 |
"No support for %s with OJPEG compression and separated " |
| 2078 |
"planar configuration", |
| 2079 |
TIFFFileName(input)); |
| 2080 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2081 |
return; |
| 2082 |
} |
| 2083 |
} |
| 2084 |
#endif |
| 2085 |
|
| 2086 |
if (t2p->pdf_sample & T2P_SAMPLE_REALIZE_PALETTE) |
| 2087 |
{ |
| 2088 |
if (t2p->pdf_colorspace & T2P_CS_CMYK) |
| 2089 |
{ |
| 2090 |
t2p->tiff_samplesperpixel = 4; |
| 2091 |
t2p->tiff_photometric = PHOTOMETRIC_SEPARATED; |
| 2092 |
} |
| 2093 |
else |
| 2094 |
{ |
| 2095 |
t2p->tiff_samplesperpixel = 3; |
| 2096 |
t2p->tiff_photometric = PHOTOMETRIC_RGB; |
| 2097 |
} |
| 2098 |
} |
| 2099 |
|
| 2100 |
if (TIFFGetField( |
| 2101 |
input, TIFFTAG_TRANSFERFUNCTION, &(t2p->tiff_transferfunction[0]), |
| 2102 |
&(t2p->tiff_transferfunction[1]), &(t2p->tiff_transferfunction[2]))) |
| 2103 |
{ |
| 2104 |
if ((t2p->tiff_transferfunction[1] != (uint16_t *)NULL) && |
| 2105 |
(t2p->tiff_transferfunction[2] != (uint16_t *)NULL)) |
| 2106 |
{ |
| 2107 |
t2p->tiff_transferfunctioncount = 3; |
| 2108 |
} |
| 2109 |
else |
| 2110 |
{ |
| 2111 |
t2p->tiff_transferfunctioncount = 1; |
| 2112 |
} |
| 2113 |
} |
| 2114 |
else |
| 2115 |
{ |
| 2116 |
t2p->tiff_transferfunctioncount = 0; |
| 2117 |
} |
| 2118 |
if (TIFFGetField(input, TIFFTAG_WHITEPOINT, &xfloatp) != 0) |
| 2119 |
{ |
| 2120 |
t2p->tiff_whitechromaticities[0] = xfloatp[0]; |
| 2121 |
t2p->tiff_whitechromaticities[1] = xfloatp[1]; |
| 2122 |
if (t2p->pdf_colorspace & T2P_CS_GRAY) |
| 2123 |
{ |
| 2124 |
t2p->pdf_colorspace |= T2P_CS_CALGRAY; |
| 2125 |
} |
| 2126 |
if (t2p->pdf_colorspace & T2P_CS_RGB) |
| 2127 |
{ |
| 2128 |
t2p->pdf_colorspace |= T2P_CS_CALRGB; |
| 2129 |
} |
| 2130 |
} |
| 2131 |
if (TIFFGetField(input, TIFFTAG_PRIMARYCHROMATICITIES, &xfloatp) != 0) |
| 2132 |
{ |
| 2133 |
t2p->tiff_primarychromaticities[0] = xfloatp[0]; |
| 2134 |
t2p->tiff_primarychromaticities[1] = xfloatp[1]; |
| 2135 |
t2p->tiff_primarychromaticities[2] = xfloatp[2]; |
| 2136 |
t2p->tiff_primarychromaticities[3] = xfloatp[3]; |
| 2137 |
t2p->tiff_primarychromaticities[4] = xfloatp[4]; |
| 2138 |
t2p->tiff_primarychromaticities[5] = xfloatp[5]; |
| 2139 |
if (t2p->pdf_colorspace & T2P_CS_RGB) |
| 2140 |
{ |
| 2141 |
t2p->pdf_colorspace |= T2P_CS_CALRGB; |
| 2142 |
} |
| 2143 |
} |
| 2144 |
if (t2p->pdf_colorspace & T2P_CS_LAB) |
| 2145 |
{ |
| 2146 |
if (TIFFGetField(input, TIFFTAG_WHITEPOINT, &xfloatp) != 0) |
| 2147 |
{ |
| 2148 |
t2p->tiff_whitechromaticities[0] = xfloatp[0]; |
| 2149 |
t2p->tiff_whitechromaticities[1] = xfloatp[1]; |
| 2150 |
} |
| 2151 |
else |
| 2152 |
{ |
| 2153 |
t2p->tiff_whitechromaticities[0] = 0.3457F; /* 0.3127F; */ |
| 2154 |
t2p->tiff_whitechromaticities[1] = 0.3585F; /* 0.3290F; */ |
| 2155 |
} |
| 2156 |
} |
| 2157 |
if (TIFFGetField(input, TIFFTAG_ICCPROFILE, &(t2p->tiff_iccprofilelength), |
| 2158 |
&(t2p->tiff_iccprofile)) != 0) |
| 2159 |
{ |
| 2160 |
t2p->pdf_colorspace |= T2P_CS_ICCBASED; |
| 2161 |
} |
| 2162 |
else |
| 2163 |
{ |
| 2164 |
t2p->tiff_iccprofilelength = 0; |
| 2165 |
t2p->tiff_iccprofile = NULL; |
| 2166 |
} |
| 2167 |
|
| 2168 |
#ifdef CCITT_SUPPORT |
| 2169 |
if (t2p->tiff_bitspersample == 1 && t2p->tiff_samplesperpixel == 1) |
| 2170 |
{ |
| 2171 |
t2p->pdf_compression = T2P_COMPRESS_G4; |
| 2172 |
} |
| 2173 |
#endif |
| 2174 |
|
| 2175 |
return; |
| 2176 |
} |
| 2177 |
|
| 2178 |
/** |
| 2179 |
* checks for overflow |
| 2180 |
*/ |
| 2181 |
static void t2p_set_tiff_datasize(T2P *t2p, uint64_t k) |
| 2182 |
{ |
| 2183 |
if (k != (uint64_t)(tmsize_t)k || (tmsize_t)k < 0) |
| 2184 |
{ |
| 2185 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 2186 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2187 |
return; |
| 2188 |
} |
| 2189 |
t2p->tiff_datasize = (tmsize_t)k; |
| 2190 |
} |
| 2191 |
|
| 2192 |
/** |
| 2193 |
This function returns the necessary size of a data buffer to contain the |
| 2194 |
raw or uncompressed image data from the input TIFF for a page. |
| 2195 |
*/ |
| 2196 |
void t2p_read_tiff_size(T2P *t2p, TIFF *input) |
| 2197 |
{ |
| 2198 |
|
| 2199 |
uint64_t *sbc = NULL; |
| 2200 |
#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT) |
| 2201 |
unsigned char *jpt = NULL; |
| 2202 |
tstrip_t i = 0; |
| 2203 |
tstrip_t stripcount = 0; |
| 2204 |
#endif |
| 2205 |
uint64_t k = 0; |
| 2206 |
|
| 2207 |
if (t2p->pdf_transcode == T2P_TRANSCODE_RAW) |
| 2208 |
{ |
| 2209 |
#if defined(CCITT_SUPPORT) || defined(ZIP_SUPPORT) |
| 2210 |
#if defined(CCITT_SUPPORT) && defined(ZIP_SUPPORT) |
| 2211 |
if (t2p->pdf_compression == T2P_COMPRESS_G4 || |
| 2212 |
t2p->pdf_compression == T2P_COMPRESS_ZIP) |
| 2213 |
#elif defined(CCITT_SUPPORT) |
| 2214 |
if (t2p->pdf_compression == T2P_COMPRESS_G4) |
| 2215 |
#else |
| 2216 |
if (t2p->pdf_compression == T2P_COMPRESS_ZIP) |
| 2217 |
#endif |
| 2218 |
{ |
| 2219 |
if (!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)) |
| 2220 |
{ |
| 2221 |
TIFFError( |
| 2222 |
TIFF2PDF_MODULE, |
| 2223 |
"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS", |
| 2224 |
TIFFFileName(input)); |
| 2225 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2226 |
return; |
| 2227 |
} |
| 2228 |
t2p_set_tiff_datasize(t2p, sbc[0]); |
| 2229 |
return; |
| 2230 |
} |
| 2231 |
#endif |
| 2232 |
#ifdef OJPEG_SUPPORT |
| 2233 |
if (t2p->tiff_compression == COMPRESSION_OJPEG) |
| 2234 |
{ |
| 2235 |
if (!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)) |
| 2236 |
{ |
| 2237 |
TIFFError( |
| 2238 |
TIFF2PDF_MODULE, |
| 2239 |
"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS", |
| 2240 |
TIFFFileName(input)); |
| 2241 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2242 |
return; |
| 2243 |
} |
| 2244 |
stripcount = TIFFNumberOfStrips(input); |
| 2245 |
for (i = 0; i < stripcount; i++) |
| 2246 |
{ |
| 2247 |
k = checkAdd64(k, sbc[i], t2p); |
| 2248 |
} |
| 2249 |
if (TIFFGetField(input, TIFFTAG_JPEGIFOFFSET, |
| 2250 |
&(t2p->tiff_dataoffset))) |
| 2251 |
{ |
| 2252 |
if (t2p->tiff_dataoffset != 0) |
| 2253 |
{ |
| 2254 |
if (TIFFGetField(input, TIFFTAG_JPEGIFBYTECOUNT, |
| 2255 |
&(t2p->tiff_datasize)) != 0) |
| 2256 |
{ |
| 2257 |
if ((uint64_t)t2p->tiff_datasize < k) |
| 2258 |
{ |
| 2259 |
TIFFWarning(TIFF2PDF_MODULE, |
| 2260 |
"Input file %s has short JPEG " |
| 2261 |
"interchange file byte count", |
| 2262 |
TIFFFileName(input)); |
| 2263 |
t2p->pdf_ojpegiflength = t2p->tiff_datasize; |
| 2264 |
k = checkAdd64(k, t2p->tiff_datasize, t2p); |
| 2265 |
k = checkAdd64(k, 6, t2p); |
| 2266 |
k = checkAdd64(k, stripcount, t2p); |
| 2267 |
k = checkAdd64(k, stripcount, t2p); |
| 2268 |
t2p_set_tiff_datasize(t2p, k); |
| 2269 |
return; |
| 2270 |
} |
| 2271 |
return; |
| 2272 |
} |
| 2273 |
else |
| 2274 |
{ |
| 2275 |
TIFFError(TIFF2PDF_MODULE, |
| 2276 |
"Input file %s missing field: " |
| 2277 |
"TIFFTAG_JPEGIFBYTECOUNT", |
| 2278 |
TIFFFileName(input)); |
| 2279 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2280 |
return; |
| 2281 |
} |
| 2282 |
} |
| 2283 |
} |
| 2284 |
k = checkAdd64(k, stripcount, t2p); |
| 2285 |
k = checkAdd64(k, stripcount, t2p); |
| 2286 |
k = checkAdd64(k, 2048, t2p); |
| 2287 |
t2p_set_tiff_datasize(t2p, k); |
| 2288 |
return; |
| 2289 |
} |
| 2290 |
#endif |
| 2291 |
#ifdef JPEG_SUPPORT |
| 2292 |
if (t2p->tiff_compression == COMPRESSION_JPEG) |
| 2293 |
{ |
| 2294 |
uint32_t count = 0; |
| 2295 |
if (TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) |
| 2296 |
{ |
| 2297 |
if (count > 4) |
| 2298 |
{ |
| 2299 |
k += count; |
| 2300 |
k -= 2; /* don't use EOI of header */ |
| 2301 |
} |
| 2302 |
} |
| 2303 |
else |
| 2304 |
{ |
| 2305 |
k = 2; /* SOI for first strip */ |
| 2306 |
} |
| 2307 |
stripcount = TIFFNumberOfStrips(input); |
| 2308 |
if (!TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc)) |
| 2309 |
{ |
| 2310 |
TIFFError( |
| 2311 |
TIFF2PDF_MODULE, |
| 2312 |
"Input file %s missing field: TIFFTAG_STRIPBYTECOUNTS", |
| 2313 |
TIFFFileName(input)); |
| 2314 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2315 |
return; |
| 2316 |
} |
| 2317 |
for (i = 0; i < stripcount; i++) |
| 2318 |
{ |
| 2319 |
k = checkAdd64(k, sbc[i], t2p); |
| 2320 |
/* Keep next two comments just for information: */ |
| 2321 |
/* k -= 2; don't use EOI of strip */ |
| 2322 |
/* k += 2; add space for restart marker */ |
| 2323 |
} |
| 2324 |
k = checkAdd64(k, 2, t2p); /* use EOI of last strip */ |
| 2325 |
t2p_set_tiff_datasize(t2p, k); |
| 2326 |
return; |
| 2327 |
} |
| 2328 |
#endif |
| 2329 |
(void)0; |
| 2330 |
} |
| 2331 |
#ifdef JPEG_SUPPORT |
| 2332 |
if (t2p->pdf_compression == T2P_COMPRESS_JPEG && |
| 2333 |
t2p->tiff_photometric == PHOTOMETRIC_YCBCR) |
| 2334 |
{ |
| 2335 |
k = checkMultiply64(TIFFNumberOfStrips(input), TIFFStripSize(input), |
| 2336 |
t2p); |
| 2337 |
} |
| 2338 |
else |
| 2339 |
#endif |
| 2340 |
{ |
| 2341 |
k = checkMultiply64(TIFFScanlineSize(input), t2p->tiff_length, t2p); |
| 2342 |
if (t2p->tiff_planar == PLANARCONFIG_SEPARATE) |
| 2343 |
{ |
| 2344 |
k = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p); |
| 2345 |
} |
| 2346 |
} |
| 2347 |
if (k == 0) |
| 2348 |
{ |
| 2349 |
/* Assume we had overflow inside TIFFScanlineSize */ |
| 2350 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2351 |
} |
| 2352 |
|
| 2353 |
t2p_set_tiff_datasize(t2p, k); |
| 2354 |
|
| 2355 |
return; |
| 2356 |
} |
| 2357 |
|
| 2358 |
/* |
| 2359 |
This function returns the necessary size of a data buffer to contain the |
| 2360 |
raw or uncompressed image data from the input TIFF for a tile of a page. |
| 2361 |
*/ |
| 2362 |
|
| 2363 |
void t2p_read_tiff_size_tile(T2P *t2p, TIFF *input, ttile_t tile) |
| 2364 |
{ |
| 2365 |
|
| 2366 |
uint64_t *tbc = NULL; |
| 2367 |
uint16_t edge = 0; |
| 2368 |
#ifdef JPEG_SUPPORT |
| 2369 |
unsigned char *jpt; |
| 2370 |
#endif |
| 2371 |
uint64_t k; |
| 2372 |
|
| 2373 |
edge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile); |
| 2374 |
edge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile); |
| 2375 |
|
| 2376 |
if (t2p->pdf_transcode == T2P_TRANSCODE_RAW) |
| 2377 |
{ |
| 2378 |
if (edge |
| 2379 |
#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT) |
| 2380 |
&& !(t2p->pdf_compression == T2P_COMPRESS_JPEG) |
| 2381 |
#endif |
| 2382 |
) |
| 2383 |
{ |
| 2384 |
t2p->tiff_datasize = TIFFTileSize(input); |
| 2385 |
if (t2p->tiff_datasize == 0) |
| 2386 |
{ |
| 2387 |
/* Assume we had overflow inside TIFFTileSize */ |
| 2388 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2389 |
} |
| 2390 |
return; |
| 2391 |
} |
| 2392 |
else |
| 2393 |
{ |
| 2394 |
TIFFGetField(input, TIFFTAG_TILEBYTECOUNTS, &tbc); |
| 2395 |
k = tbc[tile]; |
| 2396 |
#ifdef OJPEG_SUPPORT |
| 2397 |
if (t2p->tiff_compression == COMPRESSION_OJPEG) |
| 2398 |
{ |
| 2399 |
k = checkAdd64(k, 2048, t2p); |
| 2400 |
} |
| 2401 |
#endif |
| 2402 |
#ifdef JPEG_SUPPORT |
| 2403 |
if (t2p->tiff_compression == COMPRESSION_JPEG) |
| 2404 |
{ |
| 2405 |
uint32_t count = 0; |
| 2406 |
if (TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) |
| 2407 |
{ |
| 2408 |
if (count > 4) |
| 2409 |
{ |
| 2410 |
k = checkAdd64(k, count, t2p); |
| 2411 |
k -= 2; /* don't use EOI of header or SOI of tile */ |
| 2412 |
} |
| 2413 |
} |
| 2414 |
} |
| 2415 |
#endif |
| 2416 |
t2p_set_tiff_datasize(t2p, k); |
| 2417 |
return; |
| 2418 |
} |
| 2419 |
} |
| 2420 |
k = TIFFTileSize(input); |
| 2421 |
if (t2p->tiff_planar == PLANARCONFIG_SEPARATE) |
| 2422 |
{ |
| 2423 |
k = checkMultiply64(k, t2p->tiff_samplesperpixel, t2p); |
| 2424 |
} |
| 2425 |
if (k == 0) |
| 2426 |
{ |
| 2427 |
/* Assume we had overflow inside TIFFTileSize */ |
| 2428 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2429 |
} |
| 2430 |
|
| 2431 |
t2p_set_tiff_datasize(t2p, k); |
| 2432 |
|
| 2433 |
return; |
| 2434 |
} |
| 2435 |
|
| 2436 |
/* |
| 2437 |
* This functions returns a non-zero value when the tile is on the right edge |
| 2438 |
* and does not have full imaged tile width. |
| 2439 |
*/ |
| 2440 |
|
| 2441 |
int t2p_tile_is_right_edge(T2P_TILES tiles, ttile_t tile) |
| 2442 |
{ |
| 2443 |
|
| 2444 |
if (((tile + 1) % tiles.tiles_tilecountx == 0) && |
| 2445 |
(tiles.tiles_edgetilewidth != 0)) |
| 2446 |
{ |
| 2447 |
return (1); |
| 2448 |
} |
| 2449 |
else |
| 2450 |
{ |
| 2451 |
return (0); |
| 2452 |
} |
| 2453 |
} |
| 2454 |
|
| 2455 |
/* |
| 2456 |
* This functions returns a non-zero value when the tile is on the bottom edge |
| 2457 |
* and does not have full imaged tile length. |
| 2458 |
*/ |
| 2459 |
|
| 2460 |
int t2p_tile_is_bottom_edge(T2P_TILES tiles, ttile_t tile) |
| 2461 |
{ |
| 2462 |
|
| 2463 |
if (((tile + 1) > (tiles.tiles_tilecount - tiles.tiles_tilecountx)) && |
| 2464 |
(tiles.tiles_edgetilelength != 0)) |
| 2465 |
{ |
| 2466 |
return (1); |
| 2467 |
} |
| 2468 |
else |
| 2469 |
{ |
| 2470 |
return (0); |
| 2471 |
} |
| 2472 |
} |
| 2473 |
|
| 2474 |
/* |
| 2475 |
* This function returns a non-zero value when the tile is a right edge tile |
| 2476 |
* or a bottom edge tile. |
| 2477 |
*/ |
| 2478 |
|
| 2479 |
int t2p_tile_is_edge(T2P_TILES tiles, ttile_t tile) |
| 2480 |
{ |
| 2481 |
|
| 2482 |
return (t2p_tile_is_right_edge(tiles, tile) | |
| 2483 |
t2p_tile_is_bottom_edge(tiles, tile)); |
| 2484 |
} |
| 2485 |
|
| 2486 |
/* |
| 2487 |
This function returns a non-zero value when the tile is a right edge |
| 2488 |
tile and a bottom edge tile. |
| 2489 |
*/ |
| 2490 |
|
| 2491 |
int t2p_tile_is_corner_edge(T2P_TILES tiles, ttile_t tile) |
| 2492 |
{ |
| 2493 |
|
| 2494 |
return (t2p_tile_is_right_edge(tiles, tile) & |
| 2495 |
t2p_tile_is_bottom_edge(tiles, tile)); |
| 2496 |
} |
| 2497 |
|
| 2498 |
#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT) |
| 2499 |
static const unsigned char jpeg_eof_marker[] = {0xff, 0xd9}; |
| 2500 |
#endif |
| 2501 |
|
| 2502 |
/* |
| 2503 |
This function reads the raster image data from the input TIFF for an |
| 2504 |
image and writes the data to the output PDF XObject image dictionary stream. |
| 2505 |
It returns the amount written or zero on error. |
| 2506 |
*/ |
| 2507 |
|
| 2508 |
tsize_t t2p_readwrite_pdf_image(T2P *t2p, TIFF *input, TIFF *output) |
| 2509 |
{ |
| 2510 |
|
| 2511 |
tsize_t written = 0; |
| 2512 |
unsigned char *buffer = NULL; |
| 2513 |
unsigned char *samplebuffer = NULL; |
| 2514 |
tsize_t bufferoffset = 0; |
| 2515 |
tsize_t samplebufferoffset = 0; |
| 2516 |
tsize_t read = 0; |
| 2517 |
tstrip_t i = 0; |
| 2518 |
tstrip_t j = 0; |
| 2519 |
tstrip_t stripcount = 0; |
| 2520 |
tsize_t stripsize = 0; |
| 2521 |
tsize_t sepstripcount = 0; |
| 2522 |
tsize_t sepstripsize = 0; |
| 2523 |
#ifdef OJPEG_SUPPORT |
| 2524 |
toff_t inputoffset = 0; |
| 2525 |
uint16_t h_samp = 1; |
| 2526 |
uint16_t v_samp = 1; |
| 2527 |
uint16_t ri = 1; |
| 2528 |
uint32_t rows = 0; |
| 2529 |
#endif /* ifdef OJPEG_SUPPORT */ |
| 2530 |
#ifdef JPEG_SUPPORT |
| 2531 |
unsigned char *jpt; |
| 2532 |
float *xfloatp; |
| 2533 |
uint64_t *sbc; |
| 2534 |
unsigned char *stripbuffer; |
| 2535 |
tsize_t striplength = 0; |
| 2536 |
uint32_t max_striplength = 0; |
| 2537 |
#endif /* ifdef JPEG_SUPPORT */ |
| 2538 |
const char mod[] = "t2p_readwrite_pdf_image()"; |
| 2539 |
tsize_t tsdummy = 0; |
| 2540 |
|
| 2541 |
/* Fail if prior error (in particular, can't trust tiff_datasize) */ |
| 2542 |
if (t2p->t2p_error != T2P_ERR_OK) |
| 2543 |
return (0); |
| 2544 |
|
| 2545 |
if (t2p->pdf_transcode == T2P_TRANSCODE_RAW) |
| 2546 |
{ |
| 2547 |
#ifdef CCITT_SUPPORT |
| 2548 |
if (t2p->pdf_compression == T2P_COMPRESS_G4) |
| 2549 |
{ |
| 2550 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 2551 |
if (buffer == NULL) |
| 2552 |
{ |
| 2553 |
TIFFError(TIFF2PDF_MODULE, |
| 2554 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 2555 |
" bytes of memory for " |
| 2556 |
"t2p_readwrite_pdf_image, %s", |
| 2557 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 2558 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2559 |
return (0); |
| 2560 |
} |
| 2561 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2562 |
if (TIFFReadRawStrip(input, 0, (tdata_t)buffer, |
| 2563 |
t2p->tiff_datasize) < 0) |
| 2564 |
{ |
| 2565 |
TIFFError(TIFF2PDF_MODULE, "TIFFReadRawStrip() failed"); |
| 2566 |
_TIFFfree(buffer); |
| 2567 |
return (0); |
| 2568 |
} |
| 2569 |
if (t2p->tiff_fillorder == FILLORDER_LSB2MSB) |
| 2570 |
{ |
| 2571 |
/* |
| 2572 |
* make sure is lsb-to-msb |
| 2573 |
* bit-endianness fill order |
| 2574 |
*/ |
| 2575 |
TIFFReverseBits(buffer, t2p->tiff_datasize); |
| 2576 |
} |
| 2577 |
add_t2pWriteFile_check(output, (tdata_t)buffer, t2p->tiff_datasize, |
| 2578 |
mod, tsdummy); |
| 2579 |
_TIFFfree(buffer); |
| 2580 |
return (t2p->tiff_datasize); |
| 2581 |
} |
| 2582 |
#endif /* ifdef CCITT_SUPPORT */ |
| 2583 |
#ifdef ZIP_SUPPORT |
| 2584 |
if (t2p->pdf_compression == T2P_COMPRESS_ZIP) |
| 2585 |
{ |
| 2586 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 2587 |
if (buffer == NULL) |
| 2588 |
{ |
| 2589 |
TIFFError(TIFF2PDF_MODULE, |
| 2590 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 2591 |
" bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2592 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 2593 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2594 |
return (0); |
| 2595 |
} |
| 2596 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2597 |
if (TIFFReadRawStrip(input, 0, (tdata_t)buffer, |
| 2598 |
t2p->tiff_datasize) < 0) |
| 2599 |
{ |
| 2600 |
TIFFError(TIFF2PDF_MODULE, "TIFFReadRawStrip() failed"); |
| 2601 |
_TIFFfree(buffer); |
| 2602 |
return (0); |
| 2603 |
} |
| 2604 |
if (t2p->tiff_fillorder == FILLORDER_LSB2MSB) |
| 2605 |
{ |
| 2606 |
TIFFReverseBits(buffer, t2p->tiff_datasize); |
| 2607 |
} |
| 2608 |
add_t2pWriteFile_check(output, (tdata_t)buffer, t2p->tiff_datasize, |
| 2609 |
mod, tsdummy); |
| 2610 |
_TIFFfree(buffer); |
| 2611 |
return (t2p->tiff_datasize); |
| 2612 |
} |
| 2613 |
#endif /* ifdef ZIP_SUPPORT */ |
| 2614 |
#ifdef OJPEG_SUPPORT |
| 2615 |
if (t2p->tiff_compression == COMPRESSION_OJPEG) |
| 2616 |
{ |
| 2617 |
|
| 2618 |
if (t2p->tiff_dataoffset != 0) |
| 2619 |
{ |
| 2620 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 2621 |
if (buffer == NULL) |
| 2622 |
{ |
| 2623 |
TIFFError( |
| 2624 |
TIFF2PDF_MODULE, |
| 2625 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 2626 |
" bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2627 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 2628 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2629 |
return (0); |
| 2630 |
} |
| 2631 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2632 |
if (t2p->pdf_ojpegiflength == 0) |
| 2633 |
{ |
| 2634 |
inputoffset = t2pSeekFile(input, 0, SEEK_CUR); |
| 2635 |
t2pSeekFile(input, t2p->tiff_dataoffset, SEEK_SET); |
| 2636 |
t2pReadFile(input, (tdata_t)buffer, t2p->tiff_datasize); |
| 2637 |
t2pSeekFile(input, inputoffset, SEEK_SET); |
| 2638 |
add_t2pWriteFile_check(output, (tdata_t)buffer, |
| 2639 |
t2p->tiff_datasize, mod, tsdummy); |
| 2640 |
_TIFFfree(buffer); |
| 2641 |
return (t2p->tiff_datasize); |
| 2642 |
} |
| 2643 |
else |
| 2644 |
{ |
| 2645 |
inputoffset = t2pSeekFile(input, 0, SEEK_CUR); |
| 2646 |
t2pSeekFile(input, t2p->tiff_dataoffset, SEEK_SET); |
| 2647 |
bufferoffset = t2pReadFile(input, (tdata_t)buffer, |
| 2648 |
t2p->pdf_ojpegiflength); |
| 2649 |
t2p->pdf_ojpegiflength = 0; |
| 2650 |
t2pSeekFile(input, inputoffset, SEEK_SET); |
| 2651 |
TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &h_samp, |
| 2652 |
&v_samp); |
| 2653 |
buffer[bufferoffset++] = 0xff; |
| 2654 |
buffer[bufferoffset++] = 0xdd; |
| 2655 |
buffer[bufferoffset++] = 0x00; |
| 2656 |
buffer[bufferoffset++] = 0x04; |
| 2657 |
h_samp *= 8; |
| 2658 |
v_samp *= 8; |
| 2659 |
ri = (t2p->tiff_width + h_samp - 1) / h_samp; |
| 2660 |
TIFFGetField(input, TIFFTAG_ROWSPERSTRIP, &rows); |
| 2661 |
ri *= (rows + v_samp - 1) / v_samp; |
| 2662 |
buffer[bufferoffset++] = (ri >> 8) & 0xff; |
| 2663 |
buffer[bufferoffset++] = ri & 0xff; |
| 2664 |
stripcount = TIFFNumberOfStrips(input); |
| 2665 |
for (i = 0; i < stripcount; i++) |
| 2666 |
{ |
| 2667 |
tsize_t retTIFFReadRawStrip; |
| 2668 |
if (i != 0) |
| 2669 |
{ |
| 2670 |
buffer[bufferoffset++] = 0xff; |
| 2671 |
buffer[bufferoffset++] = (0xd0 | ((i - 1) % 8)); |
| 2672 |
} |
| 2673 |
retTIFFReadRawStrip = TIFFReadRawStrip( |
| 2674 |
input, i, |
| 2675 |
(tdata_t) & |
| 2676 |
(((unsigned char *)buffer)[bufferoffset]), |
| 2677 |
-1); |
| 2678 |
if (retTIFFReadRawStrip < 0) |
| 2679 |
{ |
| 2680 |
TIFFError(TIFF2PDF_MODULE, "TIFFReadRawStrip()"); |
| 2681 |
_TIFFfree(buffer); |
| 2682 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2683 |
return (0); |
| 2684 |
} |
| 2685 |
bufferoffset += retTIFFReadRawStrip; |
| 2686 |
} |
| 2687 |
add_t2pWriteFile_check(output, (tdata_t)buffer, |
| 2688 |
bufferoffset, mod, tsdummy); |
| 2689 |
_TIFFfree(buffer); |
| 2690 |
return (bufferoffset); |
| 2691 |
} |
| 2692 |
} |
| 2693 |
else |
| 2694 |
{ |
| 2695 |
if (!t2p->pdf_ojpegdata) |
| 2696 |
{ |
| 2697 |
TIFFError(TIFF2PDF_MODULE, |
| 2698 |
"No support for OJPEG image %s with bad tables", |
| 2699 |
TIFFFileName(input)); |
| 2700 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2701 |
return (0); |
| 2702 |
} |
| 2703 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 2704 |
if (buffer == NULL) |
| 2705 |
{ |
| 2706 |
TIFFError( |
| 2707 |
TIFF2PDF_MODULE, |
| 2708 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 2709 |
" bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2710 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 2711 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2712 |
return (0); |
| 2713 |
} |
| 2714 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2715 |
_TIFFmemcpy(buffer, t2p->pdf_ojpegdata, |
| 2716 |
t2p->pdf_ojpegdatalength); |
| 2717 |
bufferoffset = t2p->pdf_ojpegdatalength; |
| 2718 |
stripcount = TIFFNumberOfStrips(input); |
| 2719 |
for (i = 0; i < stripcount; i++) |
| 2720 |
{ |
| 2721 |
tsize_t retTIFFReadRawStrip; |
| 2722 |
if (i != 0) |
| 2723 |
{ |
| 2724 |
buffer[bufferoffset++] = 0xff; |
| 2725 |
buffer[bufferoffset++] = (0xd0 | ((i - 1) % 8)); |
| 2726 |
} |
| 2727 |
retTIFFReadRawStrip = TIFFReadRawStrip( |
| 2728 |
input, i, |
| 2729 |
(tdata_t) & (((unsigned char *)buffer)[bufferoffset]), |
| 2730 |
-1); |
| 2731 |
if (retTIFFReadRawStrip < 0) |
| 2732 |
{ |
| 2733 |
TIFFError(TIFF2PDF_MODULE, "TIFFReadRawStrip()"); |
| 2734 |
_TIFFfree(buffer); |
| 2735 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2736 |
return (0); |
| 2737 |
} |
| 2738 |
bufferoffset += retTIFFReadRawStrip; |
| 2739 |
} |
| 2740 |
add_t2pWriteFile_check(output, (tdata_t)buffer, bufferoffset, |
| 2741 |
mod, tsdummy); |
| 2742 |
if (!((buffer[bufferoffset - 1] == 0xd9) && |
| 2743 |
(buffer[bufferoffset - 2] == 0xff))) |
| 2744 |
{ |
| 2745 |
add_t2pWriteFile_check(output, (tdata_t)jpeg_eof_marker, |
| 2746 |
sizeof(jpeg_eof_marker), mod, |
| 2747 |
tsdummy); |
| 2748 |
} |
| 2749 |
_TIFFfree(buffer); |
| 2750 |
return (bufferoffset); |
| 2751 |
#if 0 |
| 2752 |
/* |
| 2753 |
This hunk of code removed code is clearly |
| 2754 |
mis-placed and we are not sure where it |
| 2755 |
should be (if anywhere) |
| 2756 |
*/ |
| 2757 |
TIFFError(TIFF2PDF_MODULE, |
| 2758 |
"No support for OJPEG image %s with no JPEG File Interchange offset", |
| 2759 |
TIFFFileName(input)); |
| 2760 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2761 |
return(0); |
| 2762 |
#endif |
| 2763 |
} |
| 2764 |
} |
| 2765 |
#endif /* ifdef OJPEG_SUPPORT */ |
| 2766 |
#ifdef JPEG_SUPPORT |
| 2767 |
if (t2p->tiff_compression == COMPRESSION_JPEG) |
| 2768 |
{ |
| 2769 |
uint32_t count = 0; |
| 2770 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 2771 |
if (buffer == NULL) |
| 2772 |
{ |
| 2773 |
TIFFError(TIFF2PDF_MODULE, |
| 2774 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 2775 |
" bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2776 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 2777 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2778 |
return (0); |
| 2779 |
} |
| 2780 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2781 |
if (TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) |
| 2782 |
{ |
| 2783 |
if (count > 4) |
| 2784 |
{ |
| 2785 |
_TIFFmemcpy(buffer, jpt, count); |
| 2786 |
bufferoffset += count - 2; |
| 2787 |
} |
| 2788 |
} |
| 2789 |
stripcount = TIFFNumberOfStrips(input); |
| 2790 |
TIFFGetField(input, TIFFTAG_STRIPBYTECOUNTS, &sbc); |
| 2791 |
for (i = 0; i < stripcount; i++) |
| 2792 |
{ |
| 2793 |
if (sbc[i] > max_striplength) |
| 2794 |
max_striplength = sbc[i]; |
| 2795 |
} |
| 2796 |
stripbuffer = (unsigned char *)_TIFFmalloc(max_striplength); |
| 2797 |
if (stripbuffer == NULL) |
| 2798 |
{ |
| 2799 |
TIFFError(TIFF2PDF_MODULE, |
| 2800 |
"Can't allocate %" PRId32 |
| 2801 |
" bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2802 |
max_striplength, TIFFFileName(input)); |
| 2803 |
_TIFFfree(buffer); |
| 2804 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2805 |
return (0); |
| 2806 |
} |
| 2807 |
memset(stripbuffer, 0, max_striplength); |
| 2808 |
for (i = 0; i < stripcount; i++) |
| 2809 |
{ |
| 2810 |
striplength = |
| 2811 |
TIFFReadRawStrip(input, i, (tdata_t)stripbuffer, -1); |
| 2812 |
if (striplength < 0) |
| 2813 |
{ |
| 2814 |
TIFFError(TIFF2PDF_MODULE, "TIFFReadRawStrip() failed"); |
| 2815 |
_TIFFfree(samplebuffer); |
| 2816 |
_TIFFfree(buffer); |
| 2817 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2818 |
return (0); |
| 2819 |
} |
| 2820 |
if (!t2p_process_jpeg_strip(stripbuffer, &striplength, buffer, |
| 2821 |
t2p->tiff_datasize, &bufferoffset, |
| 2822 |
i, t2p->tiff_length)) |
| 2823 |
{ |
| 2824 |
TIFFError(TIFF2PDF_MODULE, |
| 2825 |
"Can't process JPEG data in input file %s", |
| 2826 |
TIFFFileName(input)); |
| 2827 |
_TIFFfree(samplebuffer); |
| 2828 |
_TIFFfree(buffer); |
| 2829 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2830 |
return (0); |
| 2831 |
} |
| 2832 |
} |
| 2833 |
add_t2pWriteFile_check(output, (tdata_t)buffer, bufferoffset, mod, |
| 2834 |
tsdummy); |
| 2835 |
add_t2pWriteFile_check(output, (tdata_t)jpeg_eof_marker, |
| 2836 |
sizeof(jpeg_eof_marker), mod, tsdummy); |
| 2837 |
_TIFFfree(stripbuffer); |
| 2838 |
_TIFFfree(buffer); |
| 2839 |
return (bufferoffset); |
| 2840 |
} |
| 2841 |
#endif /* ifdef JPEG_SUPPORT */ |
| 2842 |
(void)0; |
| 2843 |
} |
| 2844 |
|
| 2845 |
if (t2p->pdf_sample == T2P_SAMPLE_NOTHING) |
| 2846 |
{ |
| 2847 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 2848 |
if (buffer == NULL) |
| 2849 |
{ |
| 2850 |
TIFFError(TIFF2PDF_MODULE, |
| 2851 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 2852 |
" bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2853 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 2854 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2855 |
return (0); |
| 2856 |
} |
| 2857 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2858 |
stripsize = TIFFStripSize(input); |
| 2859 |
stripcount = TIFFNumberOfStrips(input); |
| 2860 |
for (i = 0; i < stripcount; i++) |
| 2861 |
{ |
| 2862 |
read = TIFFReadEncodedStrip( |
| 2863 |
input, i, (tdata_t)&buffer[bufferoffset], |
| 2864 |
TIFFmin(stripsize, t2p->tiff_datasize - bufferoffset)); |
| 2865 |
if (read == -1) |
| 2866 |
{ |
| 2867 |
TIFFError(TIFF2PDF_MODULE, |
| 2868 |
"Error on decoding strip %" PRIu32 " of %s", i, |
| 2869 |
TIFFFileName(input)); |
| 2870 |
_TIFFfree(buffer); |
| 2871 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2872 |
return (0); |
| 2873 |
} |
| 2874 |
bufferoffset += read; |
| 2875 |
} |
| 2876 |
} |
| 2877 |
else |
| 2878 |
{ |
| 2879 |
if (t2p->pdf_sample & T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG) |
| 2880 |
{ |
| 2881 |
|
| 2882 |
sepstripsize = TIFFStripSize(input); |
| 2883 |
sepstripcount = TIFFNumberOfStrips(input); |
| 2884 |
|
| 2885 |
stripsize = sepstripsize * t2p->tiff_samplesperpixel; |
| 2886 |
stripcount = sepstripcount / t2p->tiff_samplesperpixel; |
| 2887 |
|
| 2888 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 2889 |
if (buffer == NULL) |
| 2890 |
{ |
| 2891 |
TIFFError(TIFF2PDF_MODULE, |
| 2892 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 2893 |
" bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2894 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 2895 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2896 |
return (0); |
| 2897 |
} |
| 2898 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2899 |
samplebuffer = (unsigned char *)_TIFFmalloc(stripsize); |
| 2900 |
if (samplebuffer == NULL) |
| 2901 |
{ |
| 2902 |
TIFFError(TIFF2PDF_MODULE, |
| 2903 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 2904 |
" bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2905 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 2906 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2907 |
_TIFFfree(buffer); |
| 2908 |
return (0); |
| 2909 |
} |
| 2910 |
for (i = 0; i < stripcount; i++) |
| 2911 |
{ |
| 2912 |
samplebufferoffset = 0; |
| 2913 |
for (j = 0; j < t2p->tiff_samplesperpixel; j++) |
| 2914 |
{ |
| 2915 |
read = TIFFReadEncodedStrip( |
| 2916 |
input, i + j * stripcount, |
| 2917 |
(tdata_t) & (samplebuffer[samplebufferoffset]), |
| 2918 |
TIFFmin(sepstripsize, stripsize - samplebufferoffset)); |
| 2919 |
if (read == -1) |
| 2920 |
{ |
| 2921 |
TIFFError(TIFF2PDF_MODULE, |
| 2922 |
"Error on decoding strip %" PRIu32 " of %s", |
| 2923 |
i + j * stripcount, TIFFFileName(input)); |
| 2924 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2925 |
_TIFFfree(samplebuffer); |
| 2926 |
_TIFFfree(buffer); |
| 2927 |
return (0); |
| 2928 |
} |
| 2929 |
samplebufferoffset += read; |
| 2930 |
} |
| 2931 |
t2p_sample_planar_separate_to_contig( |
| 2932 |
t2p, &(buffer[bufferoffset]), samplebuffer, |
| 2933 |
samplebufferoffset); |
| 2934 |
bufferoffset += samplebufferoffset; |
| 2935 |
} |
| 2936 |
_TIFFfree(samplebuffer); |
| 2937 |
goto dataready; |
| 2938 |
} |
| 2939 |
|
| 2940 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 2941 |
if (buffer == NULL) |
| 2942 |
{ |
| 2943 |
TIFFError(TIFF2PDF_MODULE, |
| 2944 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 2945 |
" bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2946 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 2947 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2948 |
return (0); |
| 2949 |
} |
| 2950 |
memset(buffer, 0, t2p->tiff_datasize); |
| 2951 |
stripsize = TIFFStripSize(input); |
| 2952 |
stripcount = TIFFNumberOfStrips(input); |
| 2953 |
for (i = 0; i < stripcount; i++) |
| 2954 |
{ |
| 2955 |
read = TIFFReadEncodedStrip( |
| 2956 |
input, i, (tdata_t)&buffer[bufferoffset], |
| 2957 |
TIFFmin(stripsize, t2p->tiff_datasize - bufferoffset)); |
| 2958 |
if (read == -1) |
| 2959 |
{ |
| 2960 |
TIFFError(TIFF2PDF_MODULE, |
| 2961 |
"Error on decoding strip %" PRIu32 " of %s", i, |
| 2962 |
TIFFFileName(input)); |
| 2963 |
_TIFFfree(samplebuffer); |
| 2964 |
_TIFFfree(buffer); |
| 2965 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2966 |
return (0); |
| 2967 |
} |
| 2968 |
bufferoffset += read; |
| 2969 |
} |
| 2970 |
|
| 2971 |
if (t2p->pdf_sample & T2P_SAMPLE_REALIZE_PALETTE) |
| 2972 |
{ |
| 2973 |
// FIXME: overflow? |
| 2974 |
samplebuffer = (unsigned char *)_TIFFrealloc( |
| 2975 |
(tdata_t)buffer, |
| 2976 |
t2p->tiff_datasize * t2p->tiff_samplesperpixel); |
| 2977 |
if (samplebuffer == NULL) |
| 2978 |
{ |
| 2979 |
TIFFError(TIFF2PDF_MODULE, |
| 2980 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 2981 |
" bytes of memory for t2p_readwrite_pdf_image, %s", |
| 2982 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 2983 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 2984 |
_TIFFfree(buffer); |
| 2985 |
return (0); |
| 2986 |
} |
| 2987 |
else |
| 2988 |
{ |
| 2989 |
buffer = samplebuffer; |
| 2990 |
t2p->tiff_datasize *= t2p->tiff_samplesperpixel; |
| 2991 |
} |
| 2992 |
t2p_sample_realize_palette(t2p, buffer); |
| 2993 |
} |
| 2994 |
|
| 2995 |
if (t2p->pdf_sample & T2P_SAMPLE_RGBA_TO_RGB) |
| 2996 |
{ |
| 2997 |
t2p->tiff_datasize = t2p_sample_rgba_to_rgb( |
| 2998 |
(tdata_t)buffer, t2p->tiff_width * t2p->tiff_length); |
| 2999 |
} |
| 3000 |
|
| 3001 |
if (t2p->pdf_sample & T2P_SAMPLE_RGBAA_TO_RGB) |
| 3002 |
{ |
| 3003 |
t2p->tiff_datasize = t2p_sample_rgbaa_to_rgb( |
| 3004 |
(tdata_t)buffer, t2p->tiff_width * t2p->tiff_length); |
| 3005 |
} |
| 3006 |
|
| 3007 |
if (t2p->pdf_sample & T2P_SAMPLE_YCBCR_TO_RGB) |
| 3008 |
{ |
| 3009 |
samplebuffer = (unsigned char *)_TIFFrealloc( |
| 3010 |
(tdata_t)buffer, t2p->tiff_width * t2p->tiff_length * 4); |
| 3011 |
if (samplebuffer == NULL) |
| 3012 |
{ |
| 3013 |
TIFFError(TIFF2PDF_MODULE, |
| 3014 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 3015 |
" bytes of memory for t2p_readwrite_pdf_image, %s", |
| 3016 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 3017 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3018 |
_TIFFfree(buffer); |
| 3019 |
return (0); |
| 3020 |
} |
| 3021 |
else |
| 3022 |
{ |
| 3023 |
buffer = samplebuffer; |
| 3024 |
} |
| 3025 |
if (!TIFFReadRGBAImageOriented(input, t2p->tiff_width, |
| 3026 |
t2p->tiff_length, (uint32_t *)buffer, |
| 3027 |
ORIENTATION_TOPLEFT, 0)) |
| 3028 |
{ |
| 3029 |
TIFFError(TIFF2PDF_MODULE, |
| 3030 |
"Can't use TIFFReadRGBAImageOriented to extract RGB " |
| 3031 |
"image from %s", |
| 3032 |
TIFFFileName(input)); |
| 3033 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3034 |
return (0); |
| 3035 |
} |
| 3036 |
t2p->tiff_datasize = t2p_sample_abgr_to_rgb( |
| 3037 |
(tdata_t)buffer, t2p->tiff_width * t2p->tiff_length); |
| 3038 |
} |
| 3039 |
|
| 3040 |
if (t2p->pdf_sample & T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED) |
| 3041 |
{ |
| 3042 |
t2p->tiff_datasize = t2p_sample_lab_signed_to_unsigned( |
| 3043 |
(tdata_t)buffer, t2p->tiff_width * t2p->tiff_length); |
| 3044 |
} |
| 3045 |
} |
| 3046 |
|
| 3047 |
dataready: |
| 3048 |
|
| 3049 |
t2p_disable(output); |
| 3050 |
TIFFSetField(output, TIFFTAG_PHOTOMETRIC, t2p->tiff_photometric); |
| 3051 |
TIFFSetField(output, TIFFTAG_BITSPERSAMPLE, t2p->tiff_bitspersample); |
| 3052 |
TIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, t2p->tiff_samplesperpixel); |
| 3053 |
TIFFSetField(output, TIFFTAG_IMAGEWIDTH, t2p->tiff_width); |
| 3054 |
TIFFSetField(output, TIFFTAG_IMAGELENGTH, t2p->tiff_length); |
| 3055 |
TIFFSetField(output, TIFFTAG_ROWSPERSTRIP, t2p->tiff_length); |
| 3056 |
TIFFSetField(output, TIFFTAG_PLANARCONFIG, t2p->tiff_planar); |
| 3057 |
TIFFSetField(output, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB); |
| 3058 |
|
| 3059 |
switch (t2p->pdf_compression) |
| 3060 |
{ |
| 3061 |
case T2P_COMPRESS_NONE: |
| 3062 |
TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_NONE); |
| 3063 |
break; |
| 3064 |
#ifdef CCITT_SUPPORT |
| 3065 |
case T2P_COMPRESS_G4: |
| 3066 |
TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); |
| 3067 |
break; |
| 3068 |
#endif /* ifdef CCITT_SUPPORT */ |
| 3069 |
#ifdef JPEG_SUPPORT |
| 3070 |
case T2P_COMPRESS_JPEG: |
| 3071 |
if (t2p->tiff_photometric == PHOTOMETRIC_YCBCR) |
| 3072 |
{ |
| 3073 |
uint16_t hor = 0, ver = 0; |
| 3074 |
if (TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &hor, &ver) != |
| 3075 |
0) |
| 3076 |
{ |
| 3077 |
if (hor != 0 && ver != 0) |
| 3078 |
{ |
| 3079 |
TIFFSetField(output, TIFFTAG_YCBCRSUBSAMPLING, hor, |
| 3080 |
ver); |
| 3081 |
} |
| 3082 |
} |
| 3083 |
if (TIFFGetField(input, TIFFTAG_REFERENCEBLACKWHITE, |
| 3084 |
&xfloatp) != 0) |
| 3085 |
{ |
| 3086 |
TIFFSetField(output, TIFFTAG_REFERENCEBLACKWHITE, xfloatp); |
| 3087 |
} |
| 3088 |
} |
| 3089 |
if (TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_JPEG) == |
| 3090 |
0) |
| 3091 |
{ |
| 3092 |
TIFFError( |
| 3093 |
TIFF2PDF_MODULE, |
| 3094 |
"Unable to use JPEG compression for input %s and output %s", |
| 3095 |
TIFFFileName(input), TIFFFileName(output)); |
| 3096 |
_TIFFfree(buffer); |
| 3097 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3098 |
return (0); |
| 3099 |
} |
| 3100 |
TIFFSetField(output, TIFFTAG_JPEGTABLESMODE, 0); |
| 3101 |
|
| 3102 |
if (t2p->pdf_colorspace & (T2P_CS_RGB | T2P_CS_LAB)) |
| 3103 |
{ |
| 3104 |
TIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR); |
| 3105 |
if (t2p->tiff_photometric != PHOTOMETRIC_YCBCR) |
| 3106 |
{ |
| 3107 |
TIFFSetField(output, TIFFTAG_JPEGCOLORMODE, |
| 3108 |
JPEGCOLORMODE_RGB); |
| 3109 |
} |
| 3110 |
else |
| 3111 |
{ |
| 3112 |
TIFFSetField(output, TIFFTAG_JPEGCOLORMODE, |
| 3113 |
JPEGCOLORMODE_RAW); |
| 3114 |
} |
| 3115 |
} |
| 3116 |
if (t2p->pdf_colorspace & T2P_CS_GRAY) |
| 3117 |
{ |
| 3118 |
(void)0; |
| 3119 |
} |
| 3120 |
if (t2p->pdf_colorspace & T2P_CS_CMYK) |
| 3121 |
{ |
| 3122 |
(void)0; |
| 3123 |
} |
| 3124 |
if (t2p->pdf_defaultcompressionquality != 0) |
| 3125 |
{ |
| 3126 |
TIFFSetField(output, TIFFTAG_JPEGQUALITY, |
| 3127 |
t2p->pdf_defaultcompressionquality); |
| 3128 |
} |
| 3129 |
|
| 3130 |
break; |
| 3131 |
#endif /* ifdef JPEG_SUPPORT */ |
| 3132 |
#ifdef ZIP_SUPPORT |
| 3133 |
case T2P_COMPRESS_ZIP: |
| 3134 |
TIFFSetField(output, TIFFTAG_COMPRESSION, |
| 3135 |
COMPRESSION_ADOBE_DEFLATE); |
| 3136 |
if (t2p->pdf_defaultcompressionquality % 100 != 0) |
| 3137 |
{ |
| 3138 |
TIFFSetField(output, TIFFTAG_PREDICTOR, |
| 3139 |
t2p->pdf_defaultcompressionquality % 100); |
| 3140 |
} |
| 3141 |
if (t2p->pdf_defaultcompressionquality / 100 != 0) |
| 3142 |
{ |
| 3143 |
TIFFSetField(output, TIFFTAG_ZIPQUALITY, |
| 3144 |
(t2p->pdf_defaultcompressionquality / 100)); |
| 3145 |
} |
| 3146 |
break; |
| 3147 |
#endif /* ifdef ZIP_SUPPORT */ |
| 3148 |
default: |
| 3149 |
break; |
| 3150 |
} |
| 3151 |
|
| 3152 |
t2p_enable(output); |
| 3153 |
t2p->outputwritten = 0; |
| 3154 |
#ifdef JPEG_SUPPORT |
| 3155 |
if (t2p->pdf_compression == T2P_COMPRESS_JPEG && |
| 3156 |
t2p->tiff_photometric == PHOTOMETRIC_YCBCR) |
| 3157 |
{ |
| 3158 |
bufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t)0, buffer, |
| 3159 |
stripsize * stripcount); |
| 3160 |
} |
| 3161 |
else |
| 3162 |
#endif /* ifdef JPEG_SUPPORT */ |
| 3163 |
{ |
| 3164 |
bufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t)0, buffer, |
| 3165 |
t2p->tiff_datasize); |
| 3166 |
} |
| 3167 |
if (buffer != NULL) |
| 3168 |
{ |
| 3169 |
_TIFFfree(buffer); |
| 3170 |
buffer = NULL; |
| 3171 |
} |
| 3172 |
|
| 3173 |
if (bufferoffset == (tsize_t)-1) |
| 3174 |
{ |
| 3175 |
TIFFError(TIFF2PDF_MODULE, |
| 3176 |
"Error writing encoded strip to output PDF %s", |
| 3177 |
TIFFFileName(output)); |
| 3178 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3179 |
return (0); |
| 3180 |
} |
| 3181 |
|
| 3182 |
written = t2p->outputwritten; |
| 3183 |
return (written); |
| 3184 |
} |
| 3185 |
|
| 3186 |
/* |
| 3187 |
* This function reads the raster image data from the input TIFF for an image |
| 3188 |
* tile and writes the data to the output PDF XObject image dictionary stream |
| 3189 |
* for the tile. It returns the amount written or zero on error. |
| 3190 |
*/ |
| 3191 |
|
| 3192 |
tsize_t t2p_readwrite_pdf_image_tile(T2P *t2p, TIFF *input, TIFF *output, |
| 3193 |
ttile_t tile) |
| 3194 |
{ |
| 3195 |
|
| 3196 |
uint16_t edge = 0; |
| 3197 |
tsize_t written = 0; |
| 3198 |
unsigned char *buffer = NULL; |
| 3199 |
tsize_t bufferoffset = 0; |
| 3200 |
unsigned char *samplebuffer = NULL; |
| 3201 |
tsize_t samplebufferoffset = 0; |
| 3202 |
tsize_t read = 0; |
| 3203 |
uint16_t i = 0; |
| 3204 |
ttile_t tilecount = 0; |
| 3205 |
/* tsize_t tilesize=0; */ |
| 3206 |
ttile_t septilecount = 0; |
| 3207 |
tsize_t septilesize = 0; |
| 3208 |
#ifdef JPEG_SUPPORT |
| 3209 |
unsigned char *jpt; |
| 3210 |
float *xfloatp; |
| 3211 |
uint32_t xuint32 = 0; |
| 3212 |
#endif |
| 3213 |
const char mod[] = "t2p_readwrite_pdf_image_tile()"; |
| 3214 |
tsize_t tsdummy = 0; |
| 3215 |
|
| 3216 |
/* Fail if prior error (in particular, can't trust tiff_datasize) */ |
| 3217 |
if (t2p->t2p_error != T2P_ERR_OK) |
| 3218 |
return (0); |
| 3219 |
|
| 3220 |
edge |= t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile); |
| 3221 |
edge |= t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile); |
| 3222 |
|
| 3223 |
if ((t2p->pdf_transcode == T2P_TRANSCODE_RAW) && |
| 3224 |
((edge == 0) |
| 3225 |
#if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT) |
| 3226 |
|| (t2p->pdf_compression == T2P_COMPRESS_JPEG) |
| 3227 |
#endif |
| 3228 |
)) |
| 3229 |
{ |
| 3230 |
#ifdef CCITT_SUPPORT |
| 3231 |
if (t2p->pdf_compression == T2P_COMPRESS_G4) |
| 3232 |
{ |
| 3233 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 3234 |
if (buffer == NULL) |
| 3235 |
{ |
| 3236 |
TIFFError(TIFF2PDF_MODULE, |
| 3237 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 3238 |
" bytes of memory " |
| 3239 |
"for t2p_readwrite_pdf_image_tile, %s", |
| 3240 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 3241 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3242 |
return (0); |
| 3243 |
} |
| 3244 |
memset(buffer, 0, t2p->tiff_datasize); |
| 3245 |
if (TIFFReadRawTile(input, tile, (tdata_t)buffer, |
| 3246 |
t2p->tiff_datasize) < 0) |
| 3247 |
{ |
| 3248 |
TIFFError(TIFF2PDF_MODULE, "TIFFReadRawTile() failed"); |
| 3249 |
_TIFFfree(buffer); |
| 3250 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3251 |
return (0); |
| 3252 |
} |
| 3253 |
if (t2p->tiff_fillorder == FILLORDER_LSB2MSB) |
| 3254 |
{ |
| 3255 |
TIFFReverseBits(buffer, t2p->tiff_datasize); |
| 3256 |
} |
| 3257 |
add_t2pWriteFile_check(output, (tdata_t)buffer, t2p->tiff_datasize, |
| 3258 |
mod, tsdummy); |
| 3259 |
_TIFFfree(buffer); |
| 3260 |
return (t2p->tiff_datasize); |
| 3261 |
} |
| 3262 |
#endif |
| 3263 |
#ifdef ZIP_SUPPORT |
| 3264 |
if (t2p->pdf_compression == T2P_COMPRESS_ZIP) |
| 3265 |
{ |
| 3266 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 3267 |
if (buffer == NULL) |
| 3268 |
{ |
| 3269 |
TIFFError(TIFF2PDF_MODULE, |
| 3270 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 3271 |
" bytes of memory " |
| 3272 |
"for t2p_readwrite_pdf_image_tile, %s", |
| 3273 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 3274 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3275 |
return (0); |
| 3276 |
} |
| 3277 |
memset(buffer, 0, t2p->tiff_datasize); |
| 3278 |
if (TIFFReadRawTile(input, tile, (tdata_t)buffer, |
| 3279 |
t2p->tiff_datasize) < 0) |
| 3280 |
{ |
| 3281 |
TIFFError(TIFF2PDF_MODULE, "TIFFReadRawTile() failed"); |
| 3282 |
_TIFFfree(buffer); |
| 3283 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3284 |
return (0); |
| 3285 |
} |
| 3286 |
if (t2p->tiff_fillorder == FILLORDER_LSB2MSB) |
| 3287 |
{ |
| 3288 |
TIFFReverseBits(buffer, t2p->tiff_datasize); |
| 3289 |
} |
| 3290 |
add_t2pWriteFile_check(output, (tdata_t)buffer, t2p->tiff_datasize, |
| 3291 |
mod, tsdummy); |
| 3292 |
_TIFFfree(buffer); |
| 3293 |
return (t2p->tiff_datasize); |
| 3294 |
} |
| 3295 |
#endif |
| 3296 |
#ifdef OJPEG_SUPPORT |
| 3297 |
if (t2p->tiff_compression == COMPRESSION_OJPEG) |
| 3298 |
{ |
| 3299 |
tsize_t retTIFFReadRawTile; |
| 3300 |
if (!t2p->pdf_ojpegdata) |
| 3301 |
{ |
| 3302 |
TIFFError(TIFF2PDF_MODULE, |
| 3303 |
"No support for OJPEG image %s with " |
| 3304 |
"bad tables", |
| 3305 |
TIFFFileName(input)); |
| 3306 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3307 |
return (0); |
| 3308 |
} |
| 3309 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 3310 |
if (buffer == NULL) |
| 3311 |
{ |
| 3312 |
TIFFError(TIFF2PDF_MODULE, |
| 3313 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 3314 |
" bytes of memory " |
| 3315 |
"for t2p_readwrite_pdf_image, %s", |
| 3316 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 3317 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3318 |
return (0); |
| 3319 |
} |
| 3320 |
memset(buffer, 0, t2p->tiff_datasize); |
| 3321 |
_TIFFmemcpy(buffer, t2p->pdf_ojpegdata, t2p->pdf_ojpegdatalength); |
| 3322 |
if (edge != 0) |
| 3323 |
{ |
| 3324 |
if (t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], |
| 3325 |
tile)) |
| 3326 |
{ |
| 3327 |
buffer[7] = |
| 3328 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength >> |
| 3329 |
8) & |
| 3330 |
0xff; |
| 3331 |
buffer[8] = |
| 3332 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength) & |
| 3333 |
0xff; |
| 3334 |
} |
| 3335 |
if (t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], |
| 3336 |
tile)) |
| 3337 |
{ |
| 3338 |
buffer[9] = |
| 3339 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth >> |
| 3340 |
8) & |
| 3341 |
0xff; |
| 3342 |
buffer[10] = |
| 3343 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth) & |
| 3344 |
0xff; |
| 3345 |
} |
| 3346 |
} |
| 3347 |
bufferoffset = t2p->pdf_ojpegdatalength; |
| 3348 |
retTIFFReadRawTile = TIFFReadRawTile( |
| 3349 |
input, tile, |
| 3350 |
(tdata_t) & (((unsigned char *)buffer)[bufferoffset]), -1); |
| 3351 |
if (retTIFFReadRawTile < 0) |
| 3352 |
{ |
| 3353 |
TIFFError(TIFF2PDF_MODULE, "TIFFReadRawTile() failed"); |
| 3354 |
_TIFFfree(buffer); |
| 3355 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3356 |
return (0); |
| 3357 |
} |
| 3358 |
bufferoffset += retTIFFReadRawTile; |
| 3359 |
add_t2pWriteFile_check(output, (tdata_t)buffer, bufferoffset, mod, |
| 3360 |
tsdummy); |
| 3361 |
add_t2pWriteFile_check(output, (tdata_t)jpeg_eof_marker, |
| 3362 |
sizeof(jpeg_eof_marker), mod, tsdummy); |
| 3363 |
_TIFFfree(buffer); |
| 3364 |
return (bufferoffset); |
| 3365 |
} |
| 3366 |
#endif |
| 3367 |
#ifdef JPEG_SUPPORT |
| 3368 |
if (t2p->tiff_compression == COMPRESSION_JPEG) |
| 3369 |
{ |
| 3370 |
unsigned char table_end[2]; |
| 3371 |
uint32_t count = 0; |
| 3372 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 3373 |
if (buffer == NULL) |
| 3374 |
{ |
| 3375 |
TIFFError(TIFF2PDF_MODULE, |
| 3376 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 3377 |
" bytes of memory " |
| 3378 |
"for t2p_readwrite_pdf_image_tile, %s", |
| 3379 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 3380 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3381 |
return (0); |
| 3382 |
} |
| 3383 |
memset(buffer, 0, t2p->tiff_datasize); |
| 3384 |
if (TIFFGetField(input, TIFFTAG_JPEGTABLES, &count, &jpt) != 0) |
| 3385 |
{ |
| 3386 |
if (count > 4) |
| 3387 |
{ |
| 3388 |
tsize_t retTIFFReadRawTile; |
| 3389 |
/* Ignore EOI marker of JpegTables */ |
| 3390 |
_TIFFmemcpy(buffer, jpt, count - 2); |
| 3391 |
bufferoffset += count - 2; |
| 3392 |
/* Store last 2 bytes of the JpegTables */ |
| 3393 |
table_end[0] = buffer[bufferoffset - 2]; |
| 3394 |
table_end[1] = buffer[bufferoffset - 1]; |
| 3395 |
xuint32 = bufferoffset; |
| 3396 |
bufferoffset -= 2; |
| 3397 |
retTIFFReadRawTile = TIFFReadRawTile( |
| 3398 |
input, tile, |
| 3399 |
(tdata_t) & (((unsigned char *)buffer)[bufferoffset]), |
| 3400 |
-1); |
| 3401 |
if (retTIFFReadRawTile < 0) |
| 3402 |
{ |
| 3403 |
_TIFFfree(buffer); |
| 3404 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3405 |
return (0); |
| 3406 |
} |
| 3407 |
bufferoffset += retTIFFReadRawTile; |
| 3408 |
/* Overwrite SOI marker of image scan with previously */ |
| 3409 |
/* saved end of JpegTables */ |
| 3410 |
buffer[xuint32 - 2] = table_end[0]; |
| 3411 |
buffer[xuint32 - 1] = table_end[1]; |
| 3412 |
} |
| 3413 |
} |
| 3414 |
add_t2pWriteFile_check(output, (tdata_t)buffer, bufferoffset, mod, |
| 3415 |
tsdummy); |
| 3416 |
_TIFFfree(buffer); |
| 3417 |
return (bufferoffset); |
| 3418 |
} |
| 3419 |
#endif |
| 3420 |
(void)0; |
| 3421 |
} |
| 3422 |
|
| 3423 |
if (t2p->pdf_sample == T2P_SAMPLE_NOTHING) |
| 3424 |
{ |
| 3425 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 3426 |
if (buffer == NULL) |
| 3427 |
{ |
| 3428 |
TIFFError(TIFF2PDF_MODULE, |
| 3429 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 3430 |
" bytes of memory for " |
| 3431 |
"t2p_readwrite_pdf_image_tile, %s", |
| 3432 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 3433 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3434 |
return (0); |
| 3435 |
} |
| 3436 |
memset(buffer, 0, t2p->tiff_datasize); |
| 3437 |
|
| 3438 |
read = TIFFReadEncodedTile(input, tile, (tdata_t)&buffer[bufferoffset], |
| 3439 |
t2p->tiff_datasize); |
| 3440 |
if (read == -1) |
| 3441 |
{ |
| 3442 |
TIFFError(TIFF2PDF_MODULE, |
| 3443 |
"Error on decoding tile %" PRIu32 " of %s", tile, |
| 3444 |
TIFFFileName(input)); |
| 3445 |
_TIFFfree(buffer); |
| 3446 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3447 |
return (0); |
| 3448 |
} |
| 3449 |
} |
| 3450 |
else |
| 3451 |
{ |
| 3452 |
|
| 3453 |
if (t2p->pdf_sample == T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG) |
| 3454 |
{ |
| 3455 |
septilesize = TIFFTileSize(input); |
| 3456 |
septilecount = TIFFNumberOfTiles(input); |
| 3457 |
/* tilesize=septilesize*t2p->tiff_samplesperpixel; */ |
| 3458 |
tilecount = septilecount / t2p->tiff_samplesperpixel; |
| 3459 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 3460 |
if (buffer == NULL) |
| 3461 |
{ |
| 3462 |
TIFFError(TIFF2PDF_MODULE, |
| 3463 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 3464 |
" bytes of memory " |
| 3465 |
"for t2p_readwrite_pdf_image_tile, %s", |
| 3466 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 3467 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3468 |
return (0); |
| 3469 |
} |
| 3470 |
memset(buffer, 0, t2p->tiff_datasize); |
| 3471 |
samplebuffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 3472 |
if (samplebuffer == NULL) |
| 3473 |
{ |
| 3474 |
TIFFError(TIFF2PDF_MODULE, |
| 3475 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 3476 |
" bytes of memory " |
| 3477 |
"for t2p_readwrite_pdf_image_tile, %s", |
| 3478 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 3479 |
_TIFFfree(buffer); |
| 3480 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3481 |
return (0); |
| 3482 |
} |
| 3483 |
memset(samplebuffer, 0, t2p->tiff_datasize); |
| 3484 |
samplebufferoffset = 0; |
| 3485 |
for (i = 0; i < t2p->tiff_samplesperpixel; i++) |
| 3486 |
{ |
| 3487 |
read = TIFFReadEncodedTile( |
| 3488 |
input, tile + i * tilecount, |
| 3489 |
(tdata_t) & (samplebuffer[samplebufferoffset]), |
| 3490 |
septilesize); |
| 3491 |
if (read == -1) |
| 3492 |
{ |
| 3493 |
TIFFError(TIFF2PDF_MODULE, |
| 3494 |
"Error on decoding tile %" PRIu32 " of %s", |
| 3495 |
tile + i * tilecount, TIFFFileName(input)); |
| 3496 |
_TIFFfree(samplebuffer); |
| 3497 |
_TIFFfree(buffer); |
| 3498 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3499 |
return (0); |
| 3500 |
} |
| 3501 |
samplebufferoffset += read; |
| 3502 |
} |
| 3503 |
t2p_sample_planar_separate_to_contig( |
| 3504 |
t2p, &(buffer[bufferoffset]), samplebuffer, samplebufferoffset); |
| 3505 |
bufferoffset += samplebufferoffset; |
| 3506 |
_TIFFfree(samplebuffer); |
| 3507 |
} |
| 3508 |
|
| 3509 |
if (buffer == NULL) |
| 3510 |
{ |
| 3511 |
buffer = (unsigned char *)_TIFFmalloc(t2p->tiff_datasize); |
| 3512 |
if (buffer == NULL) |
| 3513 |
{ |
| 3514 |
TIFFError(TIFF2PDF_MODULE, |
| 3515 |
"Can't allocate %" TIFF_SSIZE_FORMAT |
| 3516 |
" bytes of memory " |
| 3517 |
"for t2p_readwrite_pdf_image_tile, %s", |
| 3518 |
t2p->tiff_datasize, TIFFFileName(input)); |
| 3519 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3520 |
return (0); |
| 3521 |
} |
| 3522 |
memset(buffer, 0, t2p->tiff_datasize); |
| 3523 |
read = |
| 3524 |
TIFFReadEncodedTile(input, tile, (tdata_t)&buffer[bufferoffset], |
| 3525 |
t2p->tiff_datasize); |
| 3526 |
if (read == -1) |
| 3527 |
{ |
| 3528 |
TIFFError(TIFF2PDF_MODULE, |
| 3529 |
"Error on decoding tile %" PRIu32 " of %s", tile, |
| 3530 |
TIFFFileName(input)); |
| 3531 |
_TIFFfree(buffer); |
| 3532 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3533 |
return (0); |
| 3534 |
} |
| 3535 |
} |
| 3536 |
|
| 3537 |
if (t2p->pdf_sample & T2P_SAMPLE_RGBA_TO_RGB) |
| 3538 |
{ |
| 3539 |
t2p->tiff_datasize = t2p_sample_rgba_to_rgb( |
| 3540 |
(tdata_t)buffer, |
| 3541 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth * |
| 3542 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 3543 |
} |
| 3544 |
|
| 3545 |
if (t2p->pdf_sample & T2P_SAMPLE_RGBAA_TO_RGB) |
| 3546 |
{ |
| 3547 |
t2p->tiff_datasize = t2p_sample_rgbaa_to_rgb( |
| 3548 |
(tdata_t)buffer, |
| 3549 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth * |
| 3550 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 3551 |
} |
| 3552 |
|
| 3553 |
if (t2p->pdf_sample & T2P_SAMPLE_YCBCR_TO_RGB) |
| 3554 |
{ |
| 3555 |
TIFFError(TIFF2PDF_MODULE, |
| 3556 |
"No support for YCbCr to RGB in tile for %s", |
| 3557 |
TIFFFileName(input)); |
| 3558 |
_TIFFfree(buffer); |
| 3559 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3560 |
return (0); |
| 3561 |
} |
| 3562 |
|
| 3563 |
if (t2p->pdf_sample & T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED) |
| 3564 |
{ |
| 3565 |
t2p->tiff_datasize = t2p_sample_lab_signed_to_unsigned( |
| 3566 |
(tdata_t)buffer, |
| 3567 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth * |
| 3568 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 3569 |
} |
| 3570 |
} |
| 3571 |
|
| 3572 |
if (t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile) != 0) |
| 3573 |
{ |
| 3574 |
if ((uint64_t)t2p->tiff_datasize < |
| 3575 |
(uint64_t)TIFFTileRowSize(input) * |
| 3576 |
(uint64_t)t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength) |
| 3577 |
{ |
| 3578 |
/* we don't know how to handle PLANARCONFIG_CONTIG, |
| 3579 |
* PHOTOMETRIC_YCBCR with 3 samples per pixel */ |
| 3580 |
TIFFWarning(TIFF2PDF_MODULE, |
| 3581 |
"Don't know how to collapse tile to the left"); |
| 3582 |
} |
| 3583 |
else |
| 3584 |
{ |
| 3585 |
t2p_tile_collapse_left( |
| 3586 |
buffer, TIFFTileRowSize(input), |
| 3587 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth, |
| 3588 |
t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth, |
| 3589 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 3590 |
} |
| 3591 |
} |
| 3592 |
|
| 3593 |
t2p_disable(output); |
| 3594 |
TIFFSetField(output, TIFFTAG_PHOTOMETRIC, t2p->tiff_photometric); |
| 3595 |
TIFFSetField(output, TIFFTAG_BITSPERSAMPLE, t2p->tiff_bitspersample); |
| 3596 |
TIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, t2p->tiff_samplesperpixel); |
| 3597 |
if (t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile) == 0) |
| 3598 |
{ |
| 3599 |
TIFFSetField(output, TIFFTAG_IMAGEWIDTH, |
| 3600 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth); |
| 3601 |
} |
| 3602 |
else |
| 3603 |
{ |
| 3604 |
TIFFSetField(output, TIFFTAG_IMAGEWIDTH, |
| 3605 |
t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth); |
| 3606 |
} |
| 3607 |
if (t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile) == 0) |
| 3608 |
{ |
| 3609 |
TIFFSetField(output, TIFFTAG_IMAGELENGTH, |
| 3610 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 3611 |
TIFFSetField(output, TIFFTAG_ROWSPERSTRIP, |
| 3612 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 3613 |
} |
| 3614 |
else |
| 3615 |
{ |
| 3616 |
TIFFSetField(output, TIFFTAG_IMAGELENGTH, |
| 3617 |
t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength); |
| 3618 |
TIFFSetField(output, TIFFTAG_ROWSPERSTRIP, |
| 3619 |
t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength); |
| 3620 |
} |
| 3621 |
TIFFSetField(output, TIFFTAG_PLANARCONFIG, t2p->tiff_planar); |
| 3622 |
TIFFSetField(output, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB); |
| 3623 |
|
| 3624 |
switch (t2p->pdf_compression) |
| 3625 |
{ |
| 3626 |
case T2P_COMPRESS_NONE: |
| 3627 |
TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_NONE); |
| 3628 |
break; |
| 3629 |
#ifdef CCITT_SUPPORT |
| 3630 |
case T2P_COMPRESS_G4: |
| 3631 |
TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4); |
| 3632 |
break; |
| 3633 |
#endif |
| 3634 |
#ifdef JPEG_SUPPORT |
| 3635 |
case T2P_COMPRESS_JPEG: |
| 3636 |
if (t2p->tiff_photometric == PHOTOMETRIC_YCBCR) |
| 3637 |
{ |
| 3638 |
uint16_t hor = 0, ver = 0; |
| 3639 |
if (TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &hor, &ver) != |
| 3640 |
0) |
| 3641 |
{ |
| 3642 |
if (hor != 0 && ver != 0) |
| 3643 |
{ |
| 3644 |
TIFFSetField(output, TIFFTAG_YCBCRSUBSAMPLING, hor, |
| 3645 |
ver); |
| 3646 |
} |
| 3647 |
} |
| 3648 |
if (TIFFGetField(input, TIFFTAG_REFERENCEBLACKWHITE, |
| 3649 |
&xfloatp) != 0) |
| 3650 |
{ |
| 3651 |
TIFFSetField(output, TIFFTAG_REFERENCEBLACKWHITE, xfloatp); |
| 3652 |
} |
| 3653 |
} |
| 3654 |
TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_JPEG); |
| 3655 |
TIFFSetField(output, TIFFTAG_JPEGTABLESMODE, |
| 3656 |
0); /* JPEGTABLESMODE_NONE */ |
| 3657 |
if (t2p->pdf_colorspace & (T2P_CS_RGB | T2P_CS_LAB)) |
| 3658 |
{ |
| 3659 |
TIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR); |
| 3660 |
if (t2p->tiff_photometric != PHOTOMETRIC_YCBCR) |
| 3661 |
{ |
| 3662 |
TIFFSetField(output, TIFFTAG_JPEGCOLORMODE, |
| 3663 |
JPEGCOLORMODE_RGB); |
| 3664 |
} |
| 3665 |
else |
| 3666 |
{ |
| 3667 |
TIFFSetField(output, TIFFTAG_JPEGCOLORMODE, |
| 3668 |
JPEGCOLORMODE_RAW); |
| 3669 |
} |
| 3670 |
} |
| 3671 |
if (t2p->pdf_colorspace & T2P_CS_GRAY) |
| 3672 |
{ |
| 3673 |
(void)0; |
| 3674 |
} |
| 3675 |
if (t2p->pdf_colorspace & T2P_CS_CMYK) |
| 3676 |
{ |
| 3677 |
(void)0; |
| 3678 |
} |
| 3679 |
if (t2p->pdf_defaultcompressionquality != 0) |
| 3680 |
{ |
| 3681 |
TIFFSetField(output, TIFFTAG_JPEGQUALITY, |
| 3682 |
t2p->pdf_defaultcompressionquality); |
| 3683 |
} |
| 3684 |
break; |
| 3685 |
#endif |
| 3686 |
#ifdef ZIP_SUPPORT |
| 3687 |
case T2P_COMPRESS_ZIP: |
| 3688 |
TIFFSetField(output, TIFFTAG_COMPRESSION, |
| 3689 |
COMPRESSION_ADOBE_DEFLATE); |
| 3690 |
if (t2p->pdf_defaultcompressionquality % 100 != 0) |
| 3691 |
{ |
| 3692 |
TIFFSetField(output, TIFFTAG_PREDICTOR, |
| 3693 |
t2p->pdf_defaultcompressionquality % 100); |
| 3694 |
} |
| 3695 |
if (t2p->pdf_defaultcompressionquality / 100 != 0) |
| 3696 |
{ |
| 3697 |
TIFFSetField(output, TIFFTAG_ZIPQUALITY, |
| 3698 |
(t2p->pdf_defaultcompressionquality / 100)); |
| 3699 |
} |
| 3700 |
break; |
| 3701 |
#endif |
| 3702 |
default: |
| 3703 |
break; |
| 3704 |
} |
| 3705 |
|
| 3706 |
if (TIFFStripSize(output) > t2p->tiff_datasize) |
| 3707 |
{ |
| 3708 |
TIFFError(TIFF2PDF_MODULE, |
| 3709 |
"Size mismatch input %" PRId64 ", output %" PRId64, |
| 3710 |
(int64_t)t2p->tiff_datasize, (int64_t)TIFFStripSize(output)); |
| 3711 |
_TIFFfree(buffer); |
| 3712 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3713 |
return (0); |
| 3714 |
} |
| 3715 |
t2p_enable(output); |
| 3716 |
t2p->outputwritten = 0; |
| 3717 |
bufferoffset = TIFFWriteEncodedStrip(output, (tstrip_t)0, buffer, |
| 3718 |
TIFFStripSize(output)); |
| 3719 |
if (buffer != NULL) |
| 3720 |
{ |
| 3721 |
_TIFFfree(buffer); |
| 3722 |
buffer = NULL; |
| 3723 |
} |
| 3724 |
if (bufferoffset == -1) |
| 3725 |
{ |
| 3726 |
TIFFError(TIFF2PDF_MODULE, |
| 3727 |
"Error writing encoded tile to output PDF %s", |
| 3728 |
TIFFFileName(output)); |
| 3729 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3730 |
return (0); |
| 3731 |
} |
| 3732 |
|
| 3733 |
written = t2p->outputwritten; |
| 3734 |
|
| 3735 |
return (written); |
| 3736 |
} |
| 3737 |
|
| 3738 |
#ifdef OJPEG_SUPPORT |
| 3739 |
int t2p_process_ojpeg_tables(T2P *t2p, TIFF *input) |
| 3740 |
{ |
| 3741 |
uint16_t proc = 0; |
| 3742 |
void *q; |
| 3743 |
uint32_t q_length = 0; |
| 3744 |
void *dc; |
| 3745 |
uint32_t dc_length = 0; |
| 3746 |
void *ac; |
| 3747 |
uint32_t ac_length = 0; |
| 3748 |
uint16_t *lp; |
| 3749 |
uint16_t *pt; |
| 3750 |
uint16_t h_samp = 1; |
| 3751 |
uint16_t v_samp = 1; |
| 3752 |
unsigned char *ojpegdata; |
| 3753 |
uint16_t table_count; |
| 3754 |
uint32_t offset_table; |
| 3755 |
uint32_t offset_ms_l; |
| 3756 |
uint32_t code_count; |
| 3757 |
uint32_t i = 0; |
| 3758 |
uint32_t dest = 0; |
| 3759 |
uint16_t ri = 0; |
| 3760 |
uint32_t rows = 0; |
| 3761 |
|
| 3762 |
if (!TIFFGetField(input, TIFFTAG_JPEGPROC, &proc)) |
| 3763 |
{ |
| 3764 |
TIFFError(TIFF2PDF_MODULE, "Missing JPEGProc field in OJPEG image %s", |
| 3765 |
TIFFFileName(input)); |
| 3766 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3767 |
return (0); |
| 3768 |
} |
| 3769 |
if (proc != JPEGPROC_BASELINE && proc != JPEGPROC_LOSSLESS) |
| 3770 |
{ |
| 3771 |
TIFFError(TIFF2PDF_MODULE, "Bad JPEGProc field in OJPEG image %s", |
| 3772 |
TIFFFileName(input)); |
| 3773 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3774 |
return (0); |
| 3775 |
} |
| 3776 |
if (!TIFFGetField(input, TIFFTAG_JPEGQTABLES, &q_length, &q)) |
| 3777 |
{ |
| 3778 |
TIFFError(TIFF2PDF_MODULE, |
| 3779 |
"Missing JPEGQTables field in OJPEG image %s", |
| 3780 |
TIFFFileName(input)); |
| 3781 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3782 |
return (0); |
| 3783 |
} |
| 3784 |
if (q_length < (64U * t2p->tiff_samplesperpixel)) |
| 3785 |
{ |
| 3786 |
TIFFError(TIFF2PDF_MODULE, "Bad JPEGQTables field in OJPEG image %s", |
| 3787 |
TIFFFileName(input)); |
| 3788 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3789 |
return (0); |
| 3790 |
} |
| 3791 |
if (!TIFFGetField(input, TIFFTAG_JPEGDCTABLES, &dc_length, &dc)) |
| 3792 |
{ |
| 3793 |
TIFFError(TIFF2PDF_MODULE, |
| 3794 |
"Missing JPEGDCTables field in OJPEG image %s", |
| 3795 |
TIFFFileName(input)); |
| 3796 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3797 |
return (0); |
| 3798 |
} |
| 3799 |
if (proc == JPEGPROC_BASELINE) |
| 3800 |
{ |
| 3801 |
if (!TIFFGetField(input, TIFFTAG_JPEGACTABLES, &ac_length, &ac)) |
| 3802 |
{ |
| 3803 |
TIFFError(TIFF2PDF_MODULE, |
| 3804 |
"Missing JPEGACTables field in OJPEG image %s", |
| 3805 |
TIFFFileName(input)); |
| 3806 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3807 |
return (0); |
| 3808 |
} |
| 3809 |
} |
| 3810 |
else |
| 3811 |
{ |
| 3812 |
if (!TIFFGetField(input, TIFFTAG_JPEGLOSSLESSPREDICTORS, &lp)) |
| 3813 |
{ |
| 3814 |
TIFFError(TIFF2PDF_MODULE, |
| 3815 |
"Missing JPEGLosslessPredictors field in OJPEG image %s", |
| 3816 |
TIFFFileName(input)); |
| 3817 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3818 |
return (0); |
| 3819 |
} |
| 3820 |
if (!TIFFGetField(input, TIFFTAG_JPEGPOINTTRANSFORM, &pt)) |
| 3821 |
{ |
| 3822 |
TIFFError(TIFF2PDF_MODULE, |
| 3823 |
"Missing JPEGPointTransform field in OJPEG image %s", |
| 3824 |
TIFFFileName(input)); |
| 3825 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3826 |
return (0); |
| 3827 |
} |
| 3828 |
} |
| 3829 |
if (!TIFFGetField(input, TIFFTAG_YCBCRSUBSAMPLING, &h_samp, &v_samp)) |
| 3830 |
{ |
| 3831 |
h_samp = 1; |
| 3832 |
v_samp = 1; |
| 3833 |
} |
| 3834 |
if (t2p->pdf_ojpegdata != NULL) |
| 3835 |
{ |
| 3836 |
_TIFFfree(t2p->pdf_ojpegdata); |
| 3837 |
t2p->pdf_ojpegdata = NULL; |
| 3838 |
} |
| 3839 |
t2p->pdf_ojpegdata = _TIFFmalloc(2048); |
| 3840 |
if (t2p->pdf_ojpegdata == NULL) |
| 3841 |
{ |
| 3842 |
TIFFError(TIFF2PDF_MODULE, |
| 3843 |
"Can't allocate %u bytes of memory for " |
| 3844 |
"t2p_process_ojpeg_tables, %s", |
| 3845 |
2048u, TIFFFileName(input)); |
| 3846 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 3847 |
return (0); |
| 3848 |
} |
| 3849 |
_TIFFmemset(t2p->pdf_ojpegdata, 0x00, 2048); |
| 3850 |
t2p->pdf_ojpegdatalength = 0; |
| 3851 |
table_count = t2p->tiff_samplesperpixel; |
| 3852 |
if (proc == JPEGPROC_BASELINE) |
| 3853 |
{ |
| 3854 |
if (table_count > 2) |
| 3855 |
table_count = 2; |
| 3856 |
} |
| 3857 |
ojpegdata = (unsigned char *)t2p->pdf_ojpegdata; |
| 3858 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xff; |
| 3859 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xd8; |
| 3860 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xff; |
| 3861 |
if (proc == JPEGPROC_BASELINE) |
| 3862 |
{ |
| 3863 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xc0; |
| 3864 |
} |
| 3865 |
else |
| 3866 |
{ |
| 3867 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xc3; |
| 3868 |
} |
| 3869 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0x00; |
| 3870 |
ojpegdata[t2p->pdf_ojpegdatalength++] = (8 + 3 * t2p->tiff_samplesperpixel); |
| 3871 |
ojpegdata[t2p->pdf_ojpegdatalength++] = (t2p->tiff_bitspersample & 0xff); |
| 3872 |
if (TIFFIsTiled(input)) |
| 3873 |
{ |
| 3874 |
ojpegdata[t2p->pdf_ojpegdatalength++] = |
| 3875 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength >> 8) & 0xff; |
| 3876 |
ojpegdata[t2p->pdf_ojpegdatalength++] = |
| 3877 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength) & 0xff; |
| 3878 |
ojpegdata[t2p->pdf_ojpegdatalength++] = |
| 3879 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth >> 8) & 0xff; |
| 3880 |
ojpegdata[t2p->pdf_ojpegdatalength++] = |
| 3881 |
(t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth) & 0xff; |
| 3882 |
} |
| 3883 |
else |
| 3884 |
{ |
| 3885 |
ojpegdata[t2p->pdf_ojpegdatalength++] = (t2p->tiff_length >> 8) & 0xff; |
| 3886 |
ojpegdata[t2p->pdf_ojpegdatalength++] = (t2p->tiff_length) & 0xff; |
| 3887 |
ojpegdata[t2p->pdf_ojpegdatalength++] = (t2p->tiff_width >> 8) & 0xff; |
| 3888 |
ojpegdata[t2p->pdf_ojpegdatalength++] = (t2p->tiff_width) & 0xff; |
| 3889 |
} |
| 3890 |
ojpegdata[t2p->pdf_ojpegdatalength++] = (t2p->tiff_samplesperpixel & 0xff); |
| 3891 |
for (i = 0; i < t2p->tiff_samplesperpixel; i++) |
| 3892 |
{ |
| 3893 |
ojpegdata[t2p->pdf_ojpegdatalength++] = i; |
| 3894 |
if (i == 0) |
| 3895 |
{ |
| 3896 |
ojpegdata[t2p->pdf_ojpegdatalength] |= h_samp << 4 & 0xf0; |
| 3897 |
; |
| 3898 |
ojpegdata[t2p->pdf_ojpegdatalength++] |= v_samp & 0x0f; |
| 3899 |
} |
| 3900 |
else |
| 3901 |
{ |
| 3902 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0x11; |
| 3903 |
} |
| 3904 |
ojpegdata[t2p->pdf_ojpegdatalength++] = i; |
| 3905 |
} |
| 3906 |
for (dest = 0; dest < t2p->tiff_samplesperpixel; dest++) |
| 3907 |
{ |
| 3908 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xff; |
| 3909 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xdb; |
| 3910 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0x00; |
| 3911 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0x43; |
| 3912 |
ojpegdata[t2p->pdf_ojpegdatalength++] = dest; |
| 3913 |
_TIFFmemcpy(&(ojpegdata[t2p->pdf_ojpegdatalength++]), |
| 3914 |
&(((unsigned char *)q)[64 * dest]), 64); |
| 3915 |
t2p->pdf_ojpegdatalength += 64; |
| 3916 |
} |
| 3917 |
offset_table = 0; |
| 3918 |
for (dest = 0; dest < table_count; dest++) |
| 3919 |
{ |
| 3920 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xff; |
| 3921 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xc4; |
| 3922 |
offset_ms_l = t2p->pdf_ojpegdatalength; |
| 3923 |
t2p->pdf_ojpegdatalength += 2; |
| 3924 |
ojpegdata[t2p->pdf_ojpegdatalength++] = dest & 0x0f; |
| 3925 |
_TIFFmemcpy(&(ojpegdata[t2p->pdf_ojpegdatalength]), |
| 3926 |
&(((unsigned char *)dc)[offset_table]), 16); |
| 3927 |
code_count = 0; |
| 3928 |
offset_table += 16; |
| 3929 |
for (i = 0; i < 16; i++) |
| 3930 |
{ |
| 3931 |
code_count += ojpegdata[t2p->pdf_ojpegdatalength++]; |
| 3932 |
} |
| 3933 |
ojpegdata[offset_ms_l] = ((19 + code_count) >> 8) & 0xff; |
| 3934 |
ojpegdata[offset_ms_l + 1] = (19 + code_count) & 0xff; |
| 3935 |
_TIFFmemcpy(&(ojpegdata[t2p->pdf_ojpegdatalength]), |
| 3936 |
&(((unsigned char *)dc)[offset_table]), code_count); |
| 3937 |
offset_table += code_count; |
| 3938 |
t2p->pdf_ojpegdatalength += code_count; |
| 3939 |
} |
| 3940 |
if (proc == JPEGPROC_BASELINE) |
| 3941 |
{ |
| 3942 |
offset_table = 0; |
| 3943 |
for (dest = 0; dest < table_count; dest++) |
| 3944 |
{ |
| 3945 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xff; |
| 3946 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xc4; |
| 3947 |
offset_ms_l = t2p->pdf_ojpegdatalength; |
| 3948 |
t2p->pdf_ojpegdatalength += 2; |
| 3949 |
ojpegdata[t2p->pdf_ojpegdatalength] |= 0x10; |
| 3950 |
ojpegdata[t2p->pdf_ojpegdatalength++] |= dest & 0x0f; |
| 3951 |
_TIFFmemcpy(&(ojpegdata[t2p->pdf_ojpegdatalength]), |
| 3952 |
&(((unsigned char *)ac)[offset_table]), 16); |
| 3953 |
code_count = 0; |
| 3954 |
offset_table += 16; |
| 3955 |
for (i = 0; i < 16; i++) |
| 3956 |
{ |
| 3957 |
code_count += ojpegdata[t2p->pdf_ojpegdatalength++]; |
| 3958 |
} |
| 3959 |
ojpegdata[offset_ms_l] = ((19 + code_count) >> 8) & 0xff; |
| 3960 |
ojpegdata[offset_ms_l + 1] = (19 + code_count) & 0xff; |
| 3961 |
_TIFFmemcpy(&(ojpegdata[t2p->pdf_ojpegdatalength]), |
| 3962 |
&(((unsigned char *)ac)[offset_table]), code_count); |
| 3963 |
offset_table += code_count; |
| 3964 |
t2p->pdf_ojpegdatalength += code_count; |
| 3965 |
} |
| 3966 |
} |
| 3967 |
if (TIFFNumberOfStrips(input) > 1) |
| 3968 |
{ |
| 3969 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xff; |
| 3970 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xdd; |
| 3971 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0x00; |
| 3972 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0x04; |
| 3973 |
h_samp *= 8; |
| 3974 |
v_samp *= 8; |
| 3975 |
ri = (t2p->tiff_width + h_samp - 1) / h_samp; |
| 3976 |
TIFFGetField(input, TIFFTAG_ROWSPERSTRIP, &rows); |
| 3977 |
ri *= (rows + v_samp - 1) / v_samp; |
| 3978 |
ojpegdata[t2p->pdf_ojpegdatalength++] = (ri >> 8) & 0xff; |
| 3979 |
ojpegdata[t2p->pdf_ojpegdatalength++] = ri & 0xff; |
| 3980 |
} |
| 3981 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xff; |
| 3982 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0xda; |
| 3983 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0x00; |
| 3984 |
ojpegdata[t2p->pdf_ojpegdatalength++] = (6 + 2 * t2p->tiff_samplesperpixel); |
| 3985 |
ojpegdata[t2p->pdf_ojpegdatalength++] = t2p->tiff_samplesperpixel & 0xff; |
| 3986 |
for (i = 0; i < t2p->tiff_samplesperpixel; i++) |
| 3987 |
{ |
| 3988 |
ojpegdata[t2p->pdf_ojpegdatalength++] = i & 0xff; |
| 3989 |
if (proc == JPEGPROC_BASELINE) |
| 3990 |
{ |
| 3991 |
ojpegdata[t2p->pdf_ojpegdatalength] |= |
| 3992 |
(((i > (table_count - 1U)) ? (table_count - 1U) : i) << 4U) & |
| 3993 |
0xf0; |
| 3994 |
ojpegdata[t2p->pdf_ojpegdatalength++] |= |
| 3995 |
((i > (table_count - 1U)) ? (table_count - 1U) : i) & 0x0f; |
| 3996 |
} |
| 3997 |
else |
| 3998 |
{ |
| 3999 |
ojpegdata[t2p->pdf_ojpegdatalength++] = (i << 4) & 0xf0; |
| 4000 |
} |
| 4001 |
} |
| 4002 |
if (proc == JPEGPROC_BASELINE) |
| 4003 |
{ |
| 4004 |
t2p->pdf_ojpegdatalength++; |
| 4005 |
ojpegdata[t2p->pdf_ojpegdatalength++] = 0x3f; |
| 4006 |
t2p->pdf_ojpegdatalength++; |
| 4007 |
} |
| 4008 |
else |
| 4009 |
{ |
| 4010 |
ojpegdata[t2p->pdf_ojpegdatalength++] = (lp[0] & 0xff); |
| 4011 |
t2p->pdf_ojpegdatalength++; |
| 4012 |
ojpegdata[t2p->pdf_ojpegdatalength++] = (pt[0] & 0x0f); |
| 4013 |
} |
| 4014 |
|
| 4015 |
return (1); |
| 4016 |
} |
| 4017 |
#endif |
| 4018 |
|
| 4019 |
#ifdef JPEG_SUPPORT |
| 4020 |
int t2p_process_jpeg_strip(unsigned char *strip, tsize_t *striplength, |
| 4021 |
unsigned char *buffer, tsize_t buffersize, |
| 4022 |
tsize_t *bufferoffset, tstrip_t no, uint32_t height) |
| 4023 |
{ |
| 4024 |
|
| 4025 |
tsize_t i = 0; |
| 4026 |
|
| 4027 |
while (i < *striplength) |
| 4028 |
{ |
| 4029 |
tsize_t datalen; |
| 4030 |
uint16_t v_samp; |
| 4031 |
uint16_t h_samp; |
| 4032 |
int j; |
| 4033 |
int ncomp; |
| 4034 |
|
| 4035 |
/* marker header: one or more FFs */ |
| 4036 |
if (strip[i] != 0xff) |
| 4037 |
return (0); |
| 4038 |
i++; |
| 4039 |
while (i < *striplength && strip[i] == 0xff) |
| 4040 |
i++; |
| 4041 |
if (i >= *striplength) |
| 4042 |
return (0); |
| 4043 |
/* SOI is the only pre-SOS marker without a length word */ |
| 4044 |
if (strip[i] == 0xd8) |
| 4045 |
datalen = 0; |
| 4046 |
else |
| 4047 |
{ |
| 4048 |
if ((*striplength - i) <= 2) |
| 4049 |
return (0); |
| 4050 |
datalen = (strip[i + 1] << 8) | strip[i + 2]; |
| 4051 |
if (datalen < 2 || datalen >= (*striplength - i)) |
| 4052 |
return (0); |
| 4053 |
} |
| 4054 |
switch (strip[i]) |
| 4055 |
{ |
| 4056 |
case 0xd8: /* SOI - start of image */ |
| 4057 |
if (*bufferoffset + 2 > buffersize) |
| 4058 |
return (0); |
| 4059 |
_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i - 1]), 2); |
| 4060 |
*bufferoffset += 2; |
| 4061 |
break; |
| 4062 |
case 0xc0: /* SOF0 */ |
| 4063 |
case 0xc1: /* SOF1 */ |
| 4064 |
case 0xc3: /* SOF3 */ |
| 4065 |
case 0xc9: /* SOF9 */ |
| 4066 |
case 0xca: /* SOF10 */ |
| 4067 |
if (no == 0) |
| 4068 |
{ |
| 4069 |
if (*bufferoffset + datalen + 2 + 6 > buffersize) |
| 4070 |
return (0); |
| 4071 |
_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i - 1]), |
| 4072 |
datalen + 2); |
| 4073 |
if (*bufferoffset + 9 >= buffersize) |
| 4074 |
return (0); |
| 4075 |
ncomp = buffer[*bufferoffset + 9]; |
| 4076 |
if (ncomp < 1 || ncomp > 4) |
| 4077 |
return (0); |
| 4078 |
v_samp = 1; |
| 4079 |
h_samp = 1; |
| 4080 |
if (*bufferoffset + 11 + 3 * (ncomp - 1) >= buffersize) |
| 4081 |
return (0); |
| 4082 |
for (j = 0; j < ncomp; j++) |
| 4083 |
{ |
| 4084 |
uint16_t samp = buffer[*bufferoffset + 11 + (3 * j)]; |
| 4085 |
if ((samp >> 4) > h_samp) |
| 4086 |
h_samp = (samp >> 4); |
| 4087 |
if ((samp & 0x0f) > v_samp) |
| 4088 |
v_samp = (samp & 0x0f); |
| 4089 |
} |
| 4090 |
v_samp *= 8; |
| 4091 |
h_samp *= 8; |
| 4092 |
buffer[*bufferoffset + 5] = |
| 4093 |
(unsigned char)((height >> 8) & 0xff); |
| 4094 |
buffer[*bufferoffset + 6] = (unsigned char)(height & 0xff); |
| 4095 |
*bufferoffset += datalen + 2; |
| 4096 |
} |
| 4097 |
break; |
| 4098 |
case 0xc4: /* DHT */ |
| 4099 |
case 0xdb: /* DQT */ |
| 4100 |
if (*bufferoffset + datalen + 2 > buffersize) |
| 4101 |
return (0); |
| 4102 |
_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i - 1]), |
| 4103 |
datalen + 2); |
| 4104 |
*bufferoffset += datalen + 2; |
| 4105 |
break; |
| 4106 |
case 0xda: /* SOS */ |
| 4107 |
if (no == 0) |
| 4108 |
{ |
| 4109 |
if (*bufferoffset + datalen + 2 > buffersize) |
| 4110 |
return (0); |
| 4111 |
_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i - 1]), |
| 4112 |
datalen + 2); |
| 4113 |
*bufferoffset += datalen + 2; |
| 4114 |
} |
| 4115 |
else |
| 4116 |
{ |
| 4117 |
if (*bufferoffset + 2 > buffersize) |
| 4118 |
return (0); |
| 4119 |
buffer[(*bufferoffset)++] = 0xff; |
| 4120 |
buffer[(*bufferoffset)++] = |
| 4121 |
(unsigned char)(0xd0 | ((no - 1) % 8)); |
| 4122 |
} |
| 4123 |
i += datalen + 1; |
| 4124 |
/* copy remainder of strip */ |
| 4125 |
if (*bufferoffset + *striplength - i > buffersize) |
| 4126 |
return (0); |
| 4127 |
_TIFFmemcpy(&(buffer[*bufferoffset]), &(strip[i]), |
| 4128 |
*striplength - i); |
| 4129 |
*bufferoffset += *striplength - i; |
| 4130 |
return (1); |
| 4131 |
default: |
| 4132 |
/* ignore any other marker */ |
| 4133 |
break; |
| 4134 |
} |
| 4135 |
i += datalen + 1; |
| 4136 |
} |
| 4137 |
|
| 4138 |
/* failed to find SOS marker */ |
| 4139 |
return (0); |
| 4140 |
} |
| 4141 |
#endif |
| 4142 |
|
| 4143 |
/* |
| 4144 |
This functions converts a tilewidth x tilelength buffer of samples into |
| 4145 |
an edgetilewidth x tilelength buffer of samples. |
| 4146 |
*/ |
| 4147 |
void t2p_tile_collapse_left(tdata_t buffer, tsize_t scanwidth, |
| 4148 |
uint32_t tilewidth, uint32_t edgetilewidth, |
| 4149 |
uint32_t tilelength) |
| 4150 |
{ |
| 4151 |
|
| 4152 |
uint32_t i; |
| 4153 |
tsize_t edgescanwidth = 0; |
| 4154 |
|
| 4155 |
edgescanwidth = (scanwidth * edgetilewidth + (tilewidth - 1)) / tilewidth; |
| 4156 |
for (i = 0; i < tilelength; i++) |
| 4157 |
{ |
| 4158 |
/* We use memmove() since there can be overlaps in src and dst buffers |
| 4159 |
* for the first items */ |
| 4160 |
memmove(&(((char *)buffer)[edgescanwidth * i]), |
| 4161 |
&(((char *)buffer)[scanwidth * i]), edgescanwidth); |
| 4162 |
} |
| 4163 |
|
| 4164 |
return; |
| 4165 |
} |
| 4166 |
|
| 4167 |
/* |
| 4168 |
* This function calls TIFFWriteDirectory on the output after blanking its |
| 4169 |
* output by replacing the read, write, and seek procedures with empty |
| 4170 |
* implementations, then it replaces the original implementations. |
| 4171 |
*/ |
| 4172 |
|
| 4173 |
void t2p_write_advance_directory(T2P *t2p, TIFF *output) |
| 4174 |
{ |
| 4175 |
t2p_disable(output); |
| 4176 |
if (!TIFFWriteDirectory(output)) |
| 4177 |
{ |
| 4178 |
TIFFError(TIFF2PDF_MODULE, |
| 4179 |
"Error writing virtual directory to output PDF %s", |
| 4180 |
TIFFFileName(output)); |
| 4181 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 4182 |
return; |
| 4183 |
} |
| 4184 |
t2p_enable(output); |
| 4185 |
return; |
| 4186 |
} |
| 4187 |
|
| 4188 |
tsize_t t2p_sample_planar_separate_to_contig(T2P *t2p, unsigned char *buffer, |
| 4189 |
unsigned char *samplebuffer, |
| 4190 |
tsize_t samplebuffersize) |
| 4191 |
{ |
| 4192 |
|
| 4193 |
tsize_t stride = 0; |
| 4194 |
tsize_t i = 0; |
| 4195 |
tsize_t j = 0; |
| 4196 |
|
| 4197 |
stride = samplebuffersize / t2p->tiff_samplesperpixel; |
| 4198 |
for (i = 0; i < stride; i++) |
| 4199 |
{ |
| 4200 |
for (j = 0; j < t2p->tiff_samplesperpixel; j++) |
| 4201 |
{ |
| 4202 |
buffer[i * t2p->tiff_samplesperpixel + j] = |
| 4203 |
samplebuffer[i + j * stride]; |
| 4204 |
} |
| 4205 |
} |
| 4206 |
|
| 4207 |
return (samplebuffersize); |
| 4208 |
} |
| 4209 |
|
| 4210 |
tsize_t t2p_sample_realize_palette(T2P *t2p, unsigned char *buffer) |
| 4211 |
{ |
| 4212 |
|
| 4213 |
uint32_t sample_count = 0; |
| 4214 |
uint16_t component_count = 0; |
| 4215 |
uint32_t palette_offset = 0; |
| 4216 |
uint32_t sample_offset = 0; |
| 4217 |
uint32_t i = 0; |
| 4218 |
uint32_t j = 0; |
| 4219 |
size_t data_size; |
| 4220 |
sample_count = t2p->tiff_width * t2p->tiff_length; |
| 4221 |
component_count = t2p->tiff_samplesperpixel; |
| 4222 |
data_size = TIFFSafeMultiply(size_t, sample_count, component_count); |
| 4223 |
if ((data_size == 0U) || (t2p->tiff_datasize < 0) || |
| 4224 |
(data_size > (size_t)t2p->tiff_datasize)) |
| 4225 |
{ |
| 4226 |
TIFFError(TIFF2PDF_MODULE, |
| 4227 |
"Error: sample_count * component_count > t2p->tiff_datasize"); |
| 4228 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 4229 |
return 1; |
| 4230 |
} |
| 4231 |
|
| 4232 |
for (i = sample_count; i > 0; i--) |
| 4233 |
{ |
| 4234 |
palette_offset = buffer[i - 1] * component_count; |
| 4235 |
sample_offset = (i - 1) * component_count; |
| 4236 |
if (palette_offset + component_count > t2p->pdf_palettesize) |
| 4237 |
{ |
| 4238 |
TIFFError(TIFF2PDF_MODULE, |
| 4239 |
"Error: palette_offset + component_count > " |
| 4240 |
"t2p->pdf_palettesize"); |
| 4241 |
return 1; |
| 4242 |
} |
| 4243 |
for (j = 0; j < component_count; j++) |
| 4244 |
{ |
| 4245 |
buffer[sample_offset + j] = t2p->pdf_palette[palette_offset + j]; |
| 4246 |
} |
| 4247 |
} |
| 4248 |
|
| 4249 |
return (0); |
| 4250 |
} |
| 4251 |
|
| 4252 |
/* |
| 4253 |
This functions converts in place a buffer of ABGR interleaved data |
| 4254 |
into RGB interleaved data, discarding A. |
| 4255 |
*/ |
| 4256 |
|
| 4257 |
tsize_t t2p_sample_abgr_to_rgb(tdata_t data, uint32_t samplecount) |
| 4258 |
{ |
| 4259 |
uint32_t i = 0; |
| 4260 |
uint32_t sample = 0; |
| 4261 |
|
| 4262 |
for (i = 0; i < samplecount; i++) |
| 4263 |
{ |
| 4264 |
sample = ((uint32_t *)data)[i]; |
| 4265 |
((char *)data)[i * 3] = (char)(sample & 0xff); |
| 4266 |
((char *)data)[i * 3 + 1] = (char)((sample >> 8) & 0xff); |
| 4267 |
((char *)data)[i * 3 + 2] = (char)((sample >> 16) & 0xff); |
| 4268 |
} |
| 4269 |
|
| 4270 |
return (i * 3); |
| 4271 |
} |
| 4272 |
|
| 4273 |
/* |
| 4274 |
* This functions converts in place a buffer of RGBA interleaved data |
| 4275 |
* into RGB interleaved data, discarding A. |
| 4276 |
*/ |
| 4277 |
|
| 4278 |
tsize_t t2p_sample_rgbaa_to_rgb(tdata_t data, uint32_t samplecount) |
| 4279 |
{ |
| 4280 |
uint32_t i; |
| 4281 |
|
| 4282 |
/* For the 3 first samples, there is overlap between source and |
| 4283 |
* destination, so use memmove(). |
| 4284 |
* See http://bugzilla.maptools.org/show_bug.cgi?id=2577 |
| 4285 |
*/ |
| 4286 |
for (i = 0; i < 3 && i < samplecount; i++) |
| 4287 |
memmove((uint8_t *)data + i * 3, (uint8_t *)data + i * 4, 3); |
| 4288 |
for (; i < samplecount; i++) |
| 4289 |
memcpy((uint8_t *)data + i * 3, (uint8_t *)data + i * 4, 3); |
| 4290 |
|
| 4291 |
return (i * 3); |
| 4292 |
} |
| 4293 |
|
| 4294 |
/* |
| 4295 |
* This functions converts in place a buffer of RGBA interleaved data |
| 4296 |
* into RGB interleaved data, adding 255-A to each component sample. |
| 4297 |
*/ |
| 4298 |
|
| 4299 |
tsize_t t2p_sample_rgba_to_rgb(tdata_t data, uint32_t samplecount) |
| 4300 |
{ |
| 4301 |
uint32_t i = 0; |
| 4302 |
uint32_t sample = 0; |
| 4303 |
uint8_t alpha = 0; |
| 4304 |
|
| 4305 |
for (i = 0; i < samplecount; i++) |
| 4306 |
{ |
| 4307 |
sample = ((uint32_t *)data)[i]; |
| 4308 |
alpha = (uint8_t)((255 - ((sample >> 24) & 0xff))); |
| 4309 |
((uint8_t *)data)[i * 3 + 2] = (uint8_t)((sample >> 16) & 0xff) + alpha; |
| 4310 |
((uint8_t *)data)[i * 3 + 1] = (uint8_t)((sample >> 8) & 0xff) + alpha; |
| 4311 |
((uint8_t *)data)[i * 3] = (uint8_t)(sample & 0xff) + alpha; |
| 4312 |
} |
| 4313 |
|
| 4314 |
return (i * 3); |
| 4315 |
} |
| 4316 |
|
| 4317 |
/* |
| 4318 |
This function converts the a and b samples of Lab data from signed |
| 4319 |
to unsigned. |
| 4320 |
*/ |
| 4321 |
|
| 4322 |
tsize_t t2p_sample_lab_signed_to_unsigned(tdata_t buffer, uint32_t samplecount) |
| 4323 |
{ |
| 4324 |
|
| 4325 |
uint32_t i = 0; |
| 4326 |
|
| 4327 |
for (i = 0; i < samplecount; i++) |
| 4328 |
{ |
| 4329 |
if ((((unsigned char *)buffer)[(i * 3) + 1] & 0x80) != 0) |
| 4330 |
{ |
| 4331 |
((unsigned char *)buffer)[(i * 3) + 1] = |
| 4332 |
(unsigned char)(0x80 + ((char *)buffer)[(i * 3) + 1]); |
| 4333 |
} |
| 4334 |
else |
| 4335 |
{ |
| 4336 |
((unsigned char *)buffer)[(i * 3) + 1] |= 0x80; |
| 4337 |
} |
| 4338 |
if ((((unsigned char *)buffer)[(i * 3) + 2] & 0x80) != 0) |
| 4339 |
{ |
| 4340 |
((unsigned char *)buffer)[(i * 3) + 2] = |
| 4341 |
(unsigned char)(0x80 + ((char *)buffer)[(i * 3) + 2]); |
| 4342 |
} |
| 4343 |
else |
| 4344 |
{ |
| 4345 |
((unsigned char *)buffer)[(i * 3) + 2] |= 0x80; |
| 4346 |
} |
| 4347 |
} |
| 4348 |
|
| 4349 |
return (samplecount * 3); |
| 4350 |
} |
| 4351 |
|
| 4352 |
/* |
| 4353 |
This function writes the PDF header to output. |
| 4354 |
*/ |
| 4355 |
|
| 4356 |
tsize_t t2p_write_pdf_header(T2P *t2p, TIFF *output) |
| 4357 |
{ |
| 4358 |
|
| 4359 |
tsize_t written = 0; |
| 4360 |
char buffer[16]; |
| 4361 |
int buflen = 0; |
| 4362 |
const char mod[] = "t2p_write_pdf_header()"; |
| 4363 |
|
| 4364 |
buflen = snprintf(buffer, sizeof(buffer), "%%PDF-%" PRIu16 ".%" PRIu16 " ", |
| 4365 |
(uint16_t)(t2p->pdf_majorversion & 0xff), |
| 4366 |
(uint16_t)(t2p->pdf_minorversion & 0xff)); |
| 4367 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4368 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 4369 |
add_t2pWriteFile_check(output, (tdata_t) "\n%\342\343\317\323\n", 7, mod, |
| 4370 |
written); |
| 4371 |
|
| 4372 |
return (written); |
| 4373 |
} |
| 4374 |
|
| 4375 |
/* |
| 4376 |
This function writes the beginning of a PDF object to output. |
| 4377 |
*/ |
| 4378 |
|
| 4379 |
tsize_t t2p_write_pdf_obj_start(uint32_t number, TIFF *output) |
| 4380 |
{ |
| 4381 |
|
| 4382 |
tsize_t written = 0; |
| 4383 |
char buffer[32]; |
| 4384 |
int buflen = 0; |
| 4385 |
const char mod[] = "t2p_write_pdf_obj_start()"; |
| 4386 |
|
| 4387 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, number); |
| 4388 |
check_snprintf_ret((T2P *)NULL, buflen, buffer); |
| 4389 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, buflen, mod, written); |
| 4390 |
add_t2pWriteFile_check_2(output, (tdata_t) " 0 obj\n", 7, mod, written); |
| 4391 |
|
| 4392 |
return (written); |
| 4393 |
} |
| 4394 |
|
| 4395 |
/* |
| 4396 |
This function writes the end of a PDF object to output. |
| 4397 |
*/ |
| 4398 |
|
| 4399 |
tsize_t t2p_write_pdf_obj_end(TIFF *output) |
| 4400 |
{ |
| 4401 |
|
| 4402 |
tsize_t written = 0; |
| 4403 |
|
| 4404 |
add_t2pWriteFile_check_2(output, (tdata_t) "endobj\n", 7, |
| 4405 |
"t2p_write_pdf_obj_end()", written); |
| 4406 |
|
| 4407 |
return (written); |
| 4408 |
} |
| 4409 |
|
| 4410 |
/* |
| 4411 |
This function writes a PDF name object to output. |
| 4412 |
*/ |
| 4413 |
|
| 4414 |
tsize_t t2p_write_pdf_name(const unsigned char *name, TIFF *output) |
| 4415 |
{ |
| 4416 |
|
| 4417 |
tsize_t written = 0; |
| 4418 |
uint32_t i = 0; |
| 4419 |
char buffer[64]; |
| 4420 |
uint16_t nextchar = 0; |
| 4421 |
size_t namelen = 0; |
| 4422 |
const char mod[] = "t2p_write_pdf_name()"; |
| 4423 |
|
| 4424 |
namelen = strlen((char *)name); |
| 4425 |
if (namelen > 126) |
| 4426 |
{ |
| 4427 |
namelen = 126; |
| 4428 |
} |
| 4429 |
add_t2pWriteFile_check_2(output, (tdata_t) "/", 1, mod, written); |
| 4430 |
for (i = 0; i < namelen; i++) |
| 4431 |
{ |
| 4432 |
if (((unsigned char)name[i]) < 0x21) |
| 4433 |
{ |
| 4434 |
snprintf(buffer, sizeof(buffer), "#%.2X", name[i]); |
| 4435 |
buffer[sizeof(buffer) - 1] = '\0'; |
| 4436 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, 3, mod, written); |
| 4437 |
nextchar = 1; |
| 4438 |
} |
| 4439 |
if (((unsigned char)name[i]) > 0x7E) |
| 4440 |
{ |
| 4441 |
snprintf(buffer, sizeof(buffer), "#%.2X", name[i]); |
| 4442 |
buffer[sizeof(buffer) - 1] = '\0'; |
| 4443 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, 3, mod, written); |
| 4444 |
nextchar = 1; |
| 4445 |
} |
| 4446 |
if (nextchar == 0) |
| 4447 |
{ |
| 4448 |
switch (name[i]) |
| 4449 |
{ |
| 4450 |
case 0x23: |
| 4451 |
snprintf(buffer, sizeof(buffer), "#%.2X", name[i]); |
| 4452 |
buffer[sizeof(buffer) - 1] = '\0'; |
| 4453 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, 3, mod, |
| 4454 |
written); |
| 4455 |
break; |
| 4456 |
case 0x25: |
| 4457 |
snprintf(buffer, sizeof(buffer), "#%.2X", name[i]); |
| 4458 |
buffer[sizeof(buffer) - 1] = '\0'; |
| 4459 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, 3, mod, |
| 4460 |
written); |
| 4461 |
break; |
| 4462 |
case 0x28: |
| 4463 |
snprintf(buffer, sizeof(buffer), "#%.2X", name[i]); |
| 4464 |
buffer[sizeof(buffer) - 1] = '\0'; |
| 4465 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, 3, mod, |
| 4466 |
written); |
| 4467 |
break; |
| 4468 |
case 0x29: |
| 4469 |
snprintf(buffer, sizeof(buffer), "#%.2X", name[i]); |
| 4470 |
buffer[sizeof(buffer) - 1] = '\0'; |
| 4471 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, 3, mod, |
| 4472 |
written); |
| 4473 |
break; |
| 4474 |
case 0x2F: |
| 4475 |
snprintf(buffer, sizeof(buffer), "#%.2X", name[i]); |
| 4476 |
buffer[sizeof(buffer) - 1] = '\0'; |
| 4477 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, 3, mod, |
| 4478 |
written); |
| 4479 |
break; |
| 4480 |
case 0x3C: |
| 4481 |
snprintf(buffer, sizeof(buffer), "#%.2X", name[i]); |
| 4482 |
buffer[sizeof(buffer) - 1] = '\0'; |
| 4483 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, 3, mod, |
| 4484 |
written); |
| 4485 |
break; |
| 4486 |
case 0x3E: |
| 4487 |
snprintf(buffer, sizeof(buffer), "#%.2X", name[i]); |
| 4488 |
buffer[sizeof(buffer) - 1] = '\0'; |
| 4489 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, 3, mod, |
| 4490 |
written); |
| 4491 |
break; |
| 4492 |
case 0x5B: |
| 4493 |
snprintf(buffer, sizeof(buffer), "#%.2X", name[i]); |
| 4494 |
buffer[sizeof(buffer) - 1] = '\0'; |
| 4495 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, 3, mod, |
| 4496 |
written); |
| 4497 |
break; |
| 4498 |
case 0x5D: |
| 4499 |
snprintf(buffer, sizeof(buffer), "#%.2X", name[i]); |
| 4500 |
buffer[sizeof(buffer) - 1] = '\0'; |
| 4501 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, 3, mod, |
| 4502 |
written); |
| 4503 |
break; |
| 4504 |
case 0x7B: |
| 4505 |
snprintf(buffer, sizeof(buffer), "#%.2X", name[i]); |
| 4506 |
buffer[sizeof(buffer) - 1] = '\0'; |
| 4507 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, 3, mod, |
| 4508 |
written); |
| 4509 |
break; |
| 4510 |
case 0x7D: |
| 4511 |
snprintf(buffer, sizeof(buffer), "#%.2X", name[i]); |
| 4512 |
buffer[sizeof(buffer) - 1] = '\0'; |
| 4513 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, 3, mod, |
| 4514 |
written); |
| 4515 |
break; |
| 4516 |
default: |
| 4517 |
add_t2pWriteFile_check_2(output, (tdata_t)&name[i], 1, mod, |
| 4518 |
written); |
| 4519 |
} |
| 4520 |
} |
| 4521 |
nextchar = 0; |
| 4522 |
} |
| 4523 |
add_t2pWriteFile_check_2(output, (tdata_t) " ", 1, mod, written); |
| 4524 |
|
| 4525 |
return (written); |
| 4526 |
} |
| 4527 |
|
| 4528 |
/* |
| 4529 |
* This function writes a PDF string object to output. |
| 4530 |
*/ |
| 4531 |
|
| 4532 |
tsize_t t2p_write_pdf_string(const char *pdfstr, TIFF *output) |
| 4533 |
{ |
| 4534 |
tsize_t written = 0; |
| 4535 |
uint32_t i = 0; |
| 4536 |
char buffer[64]; |
| 4537 |
size_t len = 0; |
| 4538 |
const char mod[] = "t2p_write_pdf_string()"; |
| 4539 |
|
| 4540 |
len = strlen(pdfstr); |
| 4541 |
add_t2pWriteFile_check_2(output, (tdata_t) "(", 1, mod, written); |
| 4542 |
for (i = 0; i < len; i++) |
| 4543 |
{ |
| 4544 |
if ((pdfstr[i] & 0x80) || (pdfstr[i] == 127) || (pdfstr[i] < 32)) |
| 4545 |
{ |
| 4546 |
snprintf(buffer, sizeof(buffer), "\\%.3o", |
| 4547 |
((unsigned char)pdfstr[i])); |
| 4548 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, 4, mod, written); |
| 4549 |
} |
| 4550 |
else |
| 4551 |
{ |
| 4552 |
switch (pdfstr[i]) |
| 4553 |
{ |
| 4554 |
/* These cases are not reached. They are handled above. |
| 4555 |
case 0x08: |
| 4556 |
written += t2pWriteFile(output, (tdata_t) "\\b", 2); |
| 4557 |
break; |
| 4558 |
case 0x09: |
| 4559 |
written += t2pWriteFile(output, (tdata_t) "\\t", 2); |
| 4560 |
break; |
| 4561 |
case 0x0A: |
| 4562 |
written += t2pWriteFile(output, (tdata_t) "\\n", 2); |
| 4563 |
break; |
| 4564 |
case 0x0C: |
| 4565 |
written += t2pWriteFile(output, (tdata_t) "\\f", 2); |
| 4566 |
break; |
| 4567 |
case 0x0D: |
| 4568 |
written += t2pWriteFile(output, (tdata_t) "\\r", 2); |
| 4569 |
break; |
| 4570 |
*/ |
| 4571 |
case 0x28: |
| 4572 |
add_t2pWriteFile_check_2(output, (tdata_t) "\\(", 2, mod, |
| 4573 |
written); |
| 4574 |
break; |
| 4575 |
case 0x29: |
| 4576 |
add_t2pWriteFile_check_2(output, (tdata_t) "\\)", 2, mod, |
| 4577 |
written); |
| 4578 |
break; |
| 4579 |
case 0x5C: |
| 4580 |
add_t2pWriteFile_check_2(output, (tdata_t) "\\\\", 2, mod, |
| 4581 |
written); |
| 4582 |
break; |
| 4583 |
default: |
| 4584 |
add_t2pWriteFile_check_2(output, (tdata_t)&pdfstr[i], 1, |
| 4585 |
mod, written); |
| 4586 |
} |
| 4587 |
} |
| 4588 |
} |
| 4589 |
add_t2pWriteFile_check_2(output, (tdata_t) ") ", 1, mod, written); |
| 4590 |
|
| 4591 |
return (written); |
| 4592 |
} |
| 4593 |
|
| 4594 |
/* |
| 4595 |
This function writes a buffer of data to output. |
| 4596 |
*/ |
| 4597 |
|
| 4598 |
tsize_t t2p_write_pdf_stream(tdata_t buffer, tsize_t len, TIFF *output) |
| 4599 |
{ |
| 4600 |
|
| 4601 |
tsize_t written = 0; |
| 4602 |
|
| 4603 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, len, |
| 4604 |
"t2p_write_pdf_stream", written); |
| 4605 |
|
| 4606 |
return (written); |
| 4607 |
} |
| 4608 |
|
| 4609 |
/* |
| 4610 |
This functions writes the beginning of a PDF stream to output. |
| 4611 |
*/ |
| 4612 |
|
| 4613 |
tsize_t t2p_write_pdf_stream_start(TIFF *output) |
| 4614 |
{ |
| 4615 |
|
| 4616 |
tsize_t written = 0; |
| 4617 |
|
| 4618 |
add_t2pWriteFile_check_2(output, (tdata_t) "stream\n", 7, |
| 4619 |
"t2p_write_pdf_stream_start", written); |
| 4620 |
|
| 4621 |
return (written); |
| 4622 |
} |
| 4623 |
|
| 4624 |
/* |
| 4625 |
This function writes the end of a PDF stream to output. |
| 4626 |
*/ |
| 4627 |
|
| 4628 |
tsize_t t2p_write_pdf_stream_end(TIFF *output) |
| 4629 |
{ |
| 4630 |
|
| 4631 |
tsize_t written = 0; |
| 4632 |
|
| 4633 |
add_t2pWriteFile_check_2(output, (tdata_t) "\nendstream\n", 11, |
| 4634 |
"t2p_write_pdf_stream_end", written); |
| 4635 |
|
| 4636 |
return (written); |
| 4637 |
} |
| 4638 |
|
| 4639 |
/* |
| 4640 |
This function writes a stream dictionary for a PDF stream to output. |
| 4641 |
*/ |
| 4642 |
|
| 4643 |
tsize_t t2p_write_pdf_stream_dict(tsize_t len, uint32_t number, TIFF *output) |
| 4644 |
{ |
| 4645 |
|
| 4646 |
tsize_t written = 0; |
| 4647 |
char buffer[32]; |
| 4648 |
int buflen = 0; |
| 4649 |
const char mod[] = "t2p_write_pdf_stream_dict()"; |
| 4650 |
|
| 4651 |
add_t2pWriteFile_check_2(output, (tdata_t) "/Length ", 8, mod, written); |
| 4652 |
if (len != 0) |
| 4653 |
{ |
| 4654 |
written += t2p_write_pdf_stream_length(len, output); |
| 4655 |
} |
| 4656 |
else |
| 4657 |
{ |
| 4658 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, number); |
| 4659 |
check_snprintf_ret((T2P *)NULL, buflen, buffer); |
| 4660 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, buflen, mod, written); |
| 4661 |
add_t2pWriteFile_check_2(output, (tdata_t) " 0 R \n", 6, mod, written); |
| 4662 |
} |
| 4663 |
|
| 4664 |
return (written); |
| 4665 |
} |
| 4666 |
|
| 4667 |
/* |
| 4668 |
This functions writes the beginning of a PDF stream dictionary to |
| 4669 |
output. |
| 4670 |
*/ |
| 4671 |
|
| 4672 |
tsize_t t2p_write_pdf_stream_dict_start(TIFF *output) |
| 4673 |
{ |
| 4674 |
|
| 4675 |
tsize_t written = 0; |
| 4676 |
|
| 4677 |
add_t2pWriteFile_check_2(output, (tdata_t) "<< \n", 4, |
| 4678 |
"t2p_write_pdf_stream_dict_start", written); |
| 4679 |
|
| 4680 |
return (written); |
| 4681 |
} |
| 4682 |
|
| 4683 |
/* |
| 4684 |
This function writes the end of a PDF stream dictionary to output. |
| 4685 |
*/ |
| 4686 |
|
| 4687 |
tsize_t t2p_write_pdf_stream_dict_end(TIFF *output) |
| 4688 |
{ |
| 4689 |
|
| 4690 |
tsize_t written = 0; |
| 4691 |
|
| 4692 |
add_t2pWriteFile_check_2(output, (tdata_t) " >>\n", 4, |
| 4693 |
"t2p_write_pdf_stream_dict_end", written); |
| 4694 |
|
| 4695 |
return (written); |
| 4696 |
} |
| 4697 |
|
| 4698 |
/* |
| 4699 |
This function writes a number to output. |
| 4700 |
*/ |
| 4701 |
|
| 4702 |
tsize_t t2p_write_pdf_stream_length(tsize_t len, TIFF *output) |
| 4703 |
{ |
| 4704 |
|
| 4705 |
tsize_t written = 0; |
| 4706 |
char buffer[32]; |
| 4707 |
int buflen = 0; |
| 4708 |
const char mod[] = "t2p_write_pdf_stream_length()"; |
| 4709 |
|
| 4710 |
buflen = snprintf(buffer, sizeof(buffer), "%" TIFF_SSIZE_FORMAT, len); |
| 4711 |
check_snprintf_ret((T2P *)NULL, buflen, buffer); |
| 4712 |
add_t2pWriteFile_check_2(output, (tdata_t)buffer, buflen, mod, written); |
| 4713 |
add_t2pWriteFile_check_2(output, (tdata_t) "\n", 1, mod, written); |
| 4714 |
|
| 4715 |
return (written); |
| 4716 |
} |
| 4717 |
|
| 4718 |
/* |
| 4719 |
* This function writes the PDF Catalog structure to output. |
| 4720 |
*/ |
| 4721 |
|
| 4722 |
tsize_t t2p_write_pdf_catalog(T2P *t2p, TIFF *output) |
| 4723 |
{ |
| 4724 |
tsize_t written = 0; |
| 4725 |
char buffer[32]; |
| 4726 |
int buflen = 0; |
| 4727 |
const char mod[] = "t2p_write_pdf_catalog()"; |
| 4728 |
|
| 4729 |
add_t2pWriteFile_check(output, (tdata_t) "<< \n/Type /Catalog \n/Pages ", |
| 4730 |
27, mod, written); |
| 4731 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, t2p->pdf_pages); |
| 4732 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4733 |
add_t2pWriteFile_check(output, (tdata_t)buffer, |
| 4734 |
TIFFmin((size_t)buflen, sizeof(buffer) - 1), mod, |
| 4735 |
written); |
| 4736 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R \n", 6, mod, written); |
| 4737 |
if (t2p->pdf_fitwindow) |
| 4738 |
{ |
| 4739 |
add_t2pWriteFile_check( |
| 4740 |
output, (tdata_t) "/ViewerPreferences <</FitWindow true>>\n", 39, |
| 4741 |
mod, written); |
| 4742 |
} |
| 4743 |
add_t2pWriteFile_check(output, (tdata_t) ">>\n", 3, mod, written); |
| 4744 |
|
| 4745 |
return (written); |
| 4746 |
} |
| 4747 |
|
| 4748 |
/* |
| 4749 |
This function writes the PDF Info structure to output. |
| 4750 |
*/ |
| 4751 |
|
| 4752 |
tsize_t t2p_write_pdf_info(T2P *t2p, TIFF *input, TIFF *output) |
| 4753 |
{ |
| 4754 |
tsize_t written = 0; |
| 4755 |
const char *info; |
| 4756 |
char buffer[512]; |
| 4757 |
const char mod[] = "t2p_write_pdf_info()"; |
| 4758 |
|
| 4759 |
if (t2p->pdf_datetime[0] == '\0') |
| 4760 |
t2p_pdf_tifftime(t2p, input); |
| 4761 |
if (strlen(t2p->pdf_datetime) > 0) |
| 4762 |
{ |
| 4763 |
add_t2pWriteFile_check(output, (tdata_t) "<< \n/CreationDate ", 18, mod, |
| 4764 |
written); |
| 4765 |
written += t2p_write_pdf_string(t2p->pdf_datetime, output); |
| 4766 |
add_t2pWriteFile_check(output, (tdata_t) "\n/ModDate ", 10, mod, |
| 4767 |
written); |
| 4768 |
written += t2p_write_pdf_string(t2p->pdf_datetime, output); |
| 4769 |
} |
| 4770 |
add_t2pWriteFile_check(output, (tdata_t) "\n/Producer ", 11, mod, written); |
| 4771 |
snprintf(buffer, sizeof(buffer), "libtiff / tiff2pdf - %d", |
| 4772 |
TIFFLIB_VERSION); |
| 4773 |
written += t2p_write_pdf_string(buffer, output); |
| 4774 |
add_t2pWriteFile_check(output, (tdata_t) "\n", 1, mod, written); |
| 4775 |
if (!t2p->pdf_creator_set) |
| 4776 |
{ |
| 4777 |
if (TIFFGetField(input, TIFFTAG_SOFTWARE, &info) != 0 && info) |
| 4778 |
{ |
| 4779 |
strncpy(t2p->pdf_creator, info, sizeof(t2p->pdf_creator) - 1); |
| 4780 |
t2p->pdf_creator[sizeof(t2p->pdf_creator) - 1] = '\0'; |
| 4781 |
} |
| 4782 |
} |
| 4783 |
if (t2p->pdf_creator[0] != '\0') |
| 4784 |
{ |
| 4785 |
add_t2pWriteFile_check(output, (tdata_t) "/Creator ", 9, mod, written); |
| 4786 |
written += t2p_write_pdf_string(t2p->pdf_creator, output); |
| 4787 |
add_t2pWriteFile_check(output, (tdata_t) "\n", 1, mod, written); |
| 4788 |
} |
| 4789 |
if (!t2p->pdf_author_set) |
| 4790 |
{ |
| 4791 |
if ((TIFFGetField(input, TIFFTAG_ARTIST, &info) != 0 || |
| 4792 |
TIFFGetField(input, TIFFTAG_COPYRIGHT, &info) != 0) && |
| 4793 |
info) |
| 4794 |
{ |
| 4795 |
strncpy(t2p->pdf_author, info, sizeof(t2p->pdf_author) - 1); |
| 4796 |
t2p->pdf_author[sizeof(t2p->pdf_author) - 1] = '\0'; |
| 4797 |
} |
| 4798 |
} |
| 4799 |
if (t2p->pdf_author[0] != '\0') |
| 4800 |
{ |
| 4801 |
add_t2pWriteFile_check(output, (tdata_t) "/Author ", 8, mod, written); |
| 4802 |
written += t2p_write_pdf_string(t2p->pdf_author, output); |
| 4803 |
add_t2pWriteFile_check(output, (tdata_t) "\n", 1, mod, written); |
| 4804 |
} |
| 4805 |
if (!t2p->pdf_title_set) |
| 4806 |
{ |
| 4807 |
if (TIFFGetField(input, TIFFTAG_DOCUMENTNAME, &info) != 0 && info) |
| 4808 |
{ |
| 4809 |
strncpy(t2p->pdf_title, info, sizeof(t2p->pdf_title) - 1); |
| 4810 |
t2p->pdf_title[sizeof(t2p->pdf_title) - 1] = '\0'; |
| 4811 |
} |
| 4812 |
} |
| 4813 |
if (t2p->pdf_title[0] != '\0') |
| 4814 |
{ |
| 4815 |
add_t2pWriteFile_check(output, (tdata_t) "/Title ", 7, mod, written); |
| 4816 |
written += t2p_write_pdf_string(t2p->pdf_title, output); |
| 4817 |
add_t2pWriteFile_check(output, (tdata_t) "\n", 1, mod, written); |
| 4818 |
} |
| 4819 |
if (!t2p->pdf_subject_set) |
| 4820 |
{ |
| 4821 |
if (TIFFGetField(input, TIFFTAG_IMAGEDESCRIPTION, &info) != 0 && info) |
| 4822 |
{ |
| 4823 |
strncpy(t2p->pdf_subject, info, sizeof(t2p->pdf_subject) - 1); |
| 4824 |
t2p->pdf_subject[sizeof(t2p->pdf_subject) - 1] = '\0'; |
| 4825 |
} |
| 4826 |
} |
| 4827 |
if (t2p->pdf_subject[0] != '\0') |
| 4828 |
{ |
| 4829 |
add_t2pWriteFile_check(output, (tdata_t) "/Subject ", 9, mod, written); |
| 4830 |
written += t2p_write_pdf_string(t2p->pdf_subject, output); |
| 4831 |
add_t2pWriteFile_check(output, (tdata_t) "\n", 1, mod, written); |
| 4832 |
} |
| 4833 |
if (t2p->pdf_keywords[0] != '\0') |
| 4834 |
{ |
| 4835 |
add_t2pWriteFile_check(output, (tdata_t) "/Keywords ", 10, mod, |
| 4836 |
written); |
| 4837 |
written += t2p_write_pdf_string(t2p->pdf_keywords, output); |
| 4838 |
add_t2pWriteFile_check(output, (tdata_t) "\n", 1, mod, written); |
| 4839 |
} |
| 4840 |
add_t2pWriteFile_check(output, (tdata_t) ">> \n", 4, mod, written); |
| 4841 |
|
| 4842 |
return (written); |
| 4843 |
} |
| 4844 |
|
| 4845 |
/* |
| 4846 |
* This function fills a string of a T2P struct with the current time as a PDF |
| 4847 |
* date string, it is called by t2p_pdf_tifftime. |
| 4848 |
*/ |
| 4849 |
|
| 4850 |
void t2p_pdf_currenttime(T2P *t2p) |
| 4851 |
{ |
| 4852 |
struct tm *currenttime; |
| 4853 |
time_t timenow; |
| 4854 |
|
| 4855 |
if (time(&timenow) == (time_t)-1) |
| 4856 |
{ |
| 4857 |
TIFFError(TIFF2PDF_MODULE, "Can't get the current time: %s", |
| 4858 |
strerror(errno)); |
| 4859 |
timenow = (time_t)0; |
| 4860 |
} |
| 4861 |
|
| 4862 |
currenttime = localtime(&timenow); |
| 4863 |
snprintf(t2p->pdf_datetime, sizeof(t2p->pdf_datetime), |
| 4864 |
"D:%.4u%.2u%.2u%.2u%.2u%.2u", |
| 4865 |
TIFFmin((unsigned)currenttime->tm_year + 1900U, 9999U), |
| 4866 |
TIFFmin((unsigned)currenttime->tm_mon + 1U, 12U), /* 0-11 + 1 */ |
| 4867 |
TIFFmin((unsigned)currenttime->tm_mday, 31U), /* 1-31 */ |
| 4868 |
TIFFmin((unsigned)currenttime->tm_hour, 23U), /* 0-23 */ |
| 4869 |
TIFFmin((unsigned)currenttime->tm_min, 59U), /* 0-59 */ |
| 4870 |
TIFFmin((unsigned)(currenttime->tm_sec), 60U)); /* 0-60 */ |
| 4871 |
|
| 4872 |
return; |
| 4873 |
} |
| 4874 |
|
| 4875 |
/* |
| 4876 |
* This function fills a string of a T2P struct with the date and time of a |
| 4877 |
* TIFF file if it exists or the current time as a PDF date string. |
| 4878 |
*/ |
| 4879 |
|
| 4880 |
void t2p_pdf_tifftime(T2P *t2p, TIFF *input) |
| 4881 |
{ |
| 4882 |
char *datetime; |
| 4883 |
|
| 4884 |
if (TIFFGetField(input, TIFFTAG_DATETIME, &datetime) != 0 && |
| 4885 |
(strlen(datetime) >= 19)) |
| 4886 |
{ |
| 4887 |
t2p->pdf_datetime[0] = 'D'; |
| 4888 |
t2p->pdf_datetime[1] = ':'; |
| 4889 |
t2p->pdf_datetime[2] = datetime[0]; |
| 4890 |
t2p->pdf_datetime[3] = datetime[1]; |
| 4891 |
t2p->pdf_datetime[4] = datetime[2]; |
| 4892 |
t2p->pdf_datetime[5] = datetime[3]; |
| 4893 |
t2p->pdf_datetime[6] = datetime[5]; |
| 4894 |
t2p->pdf_datetime[7] = datetime[6]; |
| 4895 |
t2p->pdf_datetime[8] = datetime[8]; |
| 4896 |
t2p->pdf_datetime[9] = datetime[9]; |
| 4897 |
t2p->pdf_datetime[10] = datetime[11]; |
| 4898 |
t2p->pdf_datetime[11] = datetime[12]; |
| 4899 |
t2p->pdf_datetime[12] = datetime[14]; |
| 4900 |
t2p->pdf_datetime[13] = datetime[15]; |
| 4901 |
t2p->pdf_datetime[14] = datetime[17]; |
| 4902 |
t2p->pdf_datetime[15] = datetime[18]; |
| 4903 |
t2p->pdf_datetime[16] = '\0'; |
| 4904 |
} |
| 4905 |
else |
| 4906 |
{ |
| 4907 |
t2p_pdf_currenttime(t2p); |
| 4908 |
} |
| 4909 |
|
| 4910 |
return; |
| 4911 |
} |
| 4912 |
|
| 4913 |
/* |
| 4914 |
* This function writes a PDF Pages Tree structure to output. |
| 4915 |
*/ |
| 4916 |
tsize_t t2p_write_pdf_pages(T2P *t2p, TIFF *output) |
| 4917 |
{ |
| 4918 |
tsize_t written = 0; |
| 4919 |
tdir_t i = 0; |
| 4920 |
char buffer[32]; |
| 4921 |
int buflen = 0; |
| 4922 |
const char mod[] = "t2p_write_pdf_pages()"; |
| 4923 |
|
| 4924 |
int page = 0; |
| 4925 |
add_t2pWriteFile_check(output, (tdata_t) "<< \n/Type /Pages \n/Kids [ ", 26, |
| 4926 |
mod, written); |
| 4927 |
page = t2p->pdf_pages + 1; |
| 4928 |
for (i = 0; i < t2p->tiff_pagecount; i++) |
| 4929 |
{ |
| 4930 |
buflen = snprintf(buffer, sizeof(buffer), "%d", page); |
| 4931 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4932 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 4933 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R ", 5, mod, written); |
| 4934 |
if (((i + 1) % 8) == 0) |
| 4935 |
{ |
| 4936 |
add_t2pWriteFile_check(output, (tdata_t) "\n", 1, mod, written); |
| 4937 |
} |
| 4938 |
page += 3; |
| 4939 |
page += t2p->tiff_pages[i].page_extra; |
| 4940 |
if (t2p->tiff_pages[i].page_tilecount > 0) |
| 4941 |
{ |
| 4942 |
page += (2 * t2p->tiff_pages[i].page_tilecount); |
| 4943 |
} |
| 4944 |
else |
| 4945 |
{ |
| 4946 |
page += 2; |
| 4947 |
} |
| 4948 |
} |
| 4949 |
add_t2pWriteFile_check(output, (tdata_t) "] \n/Count ", 10, mod, written); |
| 4950 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu16, t2p->tiff_pagecount); |
| 4951 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4952 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 4953 |
add_t2pWriteFile_check(output, (tdata_t) " \n>> \n", 6, mod, written); |
| 4954 |
|
| 4955 |
return (written); |
| 4956 |
} |
| 4957 |
|
| 4958 |
/* |
| 4959 |
This function writes a PDF Page structure to output. |
| 4960 |
*/ |
| 4961 |
tsize_t t2p_write_pdf_page(uint32_t object, T2P *t2p, TIFF *output) |
| 4962 |
{ |
| 4963 |
|
| 4964 |
unsigned int i = 0; |
| 4965 |
tsize_t written = 0; |
| 4966 |
char buffer[256]; |
| 4967 |
int buflen = 0; |
| 4968 |
const char mod[] = "t2p_write_pdf_page()"; |
| 4969 |
|
| 4970 |
add_t2pWriteFile_check(output, (tdata_t) "<<\n/Type /Page \n/Parent ", 24, |
| 4971 |
mod, written); |
| 4972 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, t2p->pdf_pages); |
| 4973 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4974 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 4975 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R \n", 6, mod, written); |
| 4976 |
add_t2pWriteFile_check(output, (tdata_t) "/MediaBox [", 11, mod, written); |
| 4977 |
buflen = snprintf(buffer, sizeof(buffer), "%.4f", t2p->pdf_mediabox.x1); |
| 4978 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4979 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 4980 |
add_t2pWriteFile_check(output, (tdata_t) " ", 1, mod, written); |
| 4981 |
buflen = snprintf(buffer, sizeof(buffer), "%.4f", t2p->pdf_mediabox.y1); |
| 4982 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4983 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 4984 |
add_t2pWriteFile_check(output, (tdata_t) " ", 1, mod, written); |
| 4985 |
buflen = snprintf(buffer, sizeof(buffer), "%.4f", t2p->pdf_mediabox.x2); |
| 4986 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4987 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 4988 |
add_t2pWriteFile_check(output, (tdata_t) " ", 1, mod, written); |
| 4989 |
buflen = snprintf(buffer, sizeof(buffer), "%.4f", t2p->pdf_mediabox.y2); |
| 4990 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4991 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 4992 |
add_t2pWriteFile_check(output, (tdata_t) "] \n", 3, mod, written); |
| 4993 |
add_t2pWriteFile_check(output, (tdata_t) "/Contents ", 10, mod, written); |
| 4994 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, object + 1u); |
| 4995 |
check_snprintf_ret(t2p, buflen, buffer); |
| 4996 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 4997 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R \n", 6, mod, written); |
| 4998 |
add_t2pWriteFile_check(output, (tdata_t) "/Resources << \n", 15, mod, |
| 4999 |
written); |
| 5000 |
if (t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount != 0) |
| 5001 |
{ |
| 5002 |
add_t2pWriteFile_check(output, (tdata_t) "/XObject <<\n", 12, mod, |
| 5003 |
written); |
| 5004 |
for (i = 0; i < t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount; i++) |
| 5005 |
{ |
| 5006 |
add_t2pWriteFile_check(output, (tdata_t) "/Im", 3, mod, written); |
| 5007 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu16, |
| 5008 |
(uint16_t)(t2p->pdf_page + 1u)); |
| 5009 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5010 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, |
| 5011 |
written); |
| 5012 |
add_t2pWriteFile_check(output, (tdata_t) "_", 1, mod, written); |
| 5013 |
buflen = snprintf(buffer, sizeof(buffer), "%u", i + 1u); |
| 5014 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5015 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, |
| 5016 |
written); |
| 5017 |
add_t2pWriteFile_check(output, (tdata_t) " ", 1, mod, written); |
| 5018 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, |
| 5019 |
object + 3u + (2u * i) + |
| 5020 |
t2p->tiff_pages[t2p->pdf_page].page_extra); |
| 5021 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5022 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, |
| 5023 |
written); |
| 5024 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R ", 5, mod, written); |
| 5025 |
if (i % 4 == 3) |
| 5026 |
{ |
| 5027 |
add_t2pWriteFile_check(output, (tdata_t) "\n", 1, mod, written); |
| 5028 |
} |
| 5029 |
} |
| 5030 |
add_t2pWriteFile_check(output, (tdata_t) ">>\n", 3, mod, written); |
| 5031 |
} |
| 5032 |
else |
| 5033 |
{ |
| 5034 |
add_t2pWriteFile_check(output, (tdata_t) "/XObject <<\n", 12, mod, |
| 5035 |
written); |
| 5036 |
add_t2pWriteFile_check(output, (tdata_t) "/Im", 3, mod, written); |
| 5037 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu16, |
| 5038 |
(uint16_t)(t2p->pdf_page + 1u)); |
| 5039 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5040 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5041 |
add_t2pWriteFile_check(output, (tdata_t) " ", 1, mod, written); |
| 5042 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, |
| 5043 |
object + 3u + (2u * i) + |
| 5044 |
t2p->tiff_pages[t2p->pdf_page].page_extra); |
| 5045 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5046 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5047 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R ", 5, mod, written); |
| 5048 |
add_t2pWriteFile_check(output, (tdata_t) ">>\n", 3, mod, written); |
| 5049 |
} |
| 5050 |
if (t2p->tiff_transferfunctioncount != 0) |
| 5051 |
{ |
| 5052 |
add_t2pWriteFile_check(output, (tdata_t) "/ExtGState <<", 13, mod, |
| 5053 |
written); |
| 5054 |
add_t2pWriteFile_check(output, (tdata_t) "/GS1 ", 5, mod, written); |
| 5055 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, object + 3u); |
| 5056 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5057 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5058 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R ", 5, mod, written); |
| 5059 |
add_t2pWriteFile_check(output, (tdata_t) ">> \n", 4, mod, written); |
| 5060 |
} |
| 5061 |
add_t2pWriteFile_check(output, (tdata_t) "/ProcSet [ ", 11, mod, written); |
| 5062 |
if (t2p->pdf_colorspace & T2P_CS_BILEVEL || |
| 5063 |
t2p->pdf_colorspace & T2P_CS_GRAY) |
| 5064 |
{ |
| 5065 |
add_t2pWriteFile_check(output, (tdata_t) "/ImageB ", 8, mod, written); |
| 5066 |
} |
| 5067 |
else |
| 5068 |
{ |
| 5069 |
add_t2pWriteFile_check(output, (tdata_t) "/ImageC ", 8, mod, written); |
| 5070 |
if (t2p->pdf_colorspace & T2P_CS_PALETTE) |
| 5071 |
{ |
| 5072 |
add_t2pWriteFile_check(output, (tdata_t) "/ImageI ", 8, mod, |
| 5073 |
written); |
| 5074 |
} |
| 5075 |
} |
| 5076 |
add_t2pWriteFile_check(output, (tdata_t) "]\n>>\n>>\n", 8, mod, written); |
| 5077 |
|
| 5078 |
return (written); |
| 5079 |
} |
| 5080 |
|
| 5081 |
/* |
| 5082 |
This function composes the page size and image and tile locations on a |
| 5083 |
page. |
| 5084 |
*/ |
| 5085 |
|
| 5086 |
void t2p_compose_pdf_page(T2P *t2p) |
| 5087 |
{ |
| 5088 |
|
| 5089 |
uint32_t i = 0; |
| 5090 |
uint32_t i2 = 0; |
| 5091 |
T2P_TILE *tiles = NULL; |
| 5092 |
T2P_BOX *boxp = NULL; |
| 5093 |
uint32_t tilecountx = 0; |
| 5094 |
uint32_t tilecounty = 0; |
| 5095 |
uint32_t tilewidth = 0; |
| 5096 |
uint32_t tilelength = 0; |
| 5097 |
int istiled = 0; |
| 5098 |
float f = 0; |
| 5099 |
float width_ratio = 0; |
| 5100 |
float length_ratio = 0; |
| 5101 |
|
| 5102 |
t2p->pdf_xres = t2p->tiff_xres; |
| 5103 |
t2p->pdf_yres = t2p->tiff_yres; |
| 5104 |
if (t2p->pdf_overrideres) |
| 5105 |
{ |
| 5106 |
t2p->pdf_xres = t2p->pdf_defaultxres; |
| 5107 |
t2p->pdf_yres = t2p->pdf_defaultyres; |
| 5108 |
} |
| 5109 |
if (t2p->pdf_xres == 0.0) |
| 5110 |
t2p->pdf_xres = t2p->pdf_defaultxres; |
| 5111 |
if (t2p->pdf_yres == 0.0) |
| 5112 |
t2p->pdf_yres = t2p->pdf_defaultyres; |
| 5113 |
if (t2p->pdf_image_fillpage) |
| 5114 |
{ |
| 5115 |
width_ratio = t2p->pdf_defaultpagewidth / t2p->tiff_width; |
| 5116 |
length_ratio = t2p->pdf_defaultpagelength / t2p->tiff_length; |
| 5117 |
if (width_ratio < length_ratio) |
| 5118 |
{ |
| 5119 |
t2p->pdf_imagewidth = t2p->pdf_defaultpagewidth; |
| 5120 |
t2p->pdf_imagelength = t2p->tiff_length * width_ratio; |
| 5121 |
} |
| 5122 |
else |
| 5123 |
{ |
| 5124 |
t2p->pdf_imagewidth = t2p->tiff_width * length_ratio; |
| 5125 |
t2p->pdf_imagelength = t2p->pdf_defaultpagelength; |
| 5126 |
} |
| 5127 |
} |
| 5128 |
else if (t2p->tiff_resunit != RESUNIT_CENTIMETER /* RESUNIT_NONE and */ |
| 5129 |
&& t2p->tiff_resunit != RESUNIT_INCH) |
| 5130 |
{ /* other cases */ |
| 5131 |
t2p->pdf_imagewidth = ((float)(t2p->tiff_width)) / t2p->pdf_xres; |
| 5132 |
t2p->pdf_imagelength = ((float)(t2p->tiff_length)) / t2p->pdf_yres; |
| 5133 |
} |
| 5134 |
else |
| 5135 |
{ |
| 5136 |
t2p->pdf_imagewidth = |
| 5137 |
((float)(t2p->tiff_width)) * PS_UNIT_SIZE / t2p->pdf_xres; |
| 5138 |
t2p->pdf_imagelength = |
| 5139 |
((float)(t2p->tiff_length)) * PS_UNIT_SIZE / t2p->pdf_yres; |
| 5140 |
} |
| 5141 |
if (t2p->pdf_overridepagesize != 0) |
| 5142 |
{ |
| 5143 |
t2p->pdf_pagewidth = t2p->pdf_defaultpagewidth; |
| 5144 |
t2p->pdf_pagelength = t2p->pdf_defaultpagelength; |
| 5145 |
} |
| 5146 |
else |
| 5147 |
{ |
| 5148 |
t2p->pdf_pagewidth = t2p->pdf_imagewidth; |
| 5149 |
t2p->pdf_pagelength = t2p->pdf_imagelength; |
| 5150 |
} |
| 5151 |
t2p->pdf_mediabox.x1 = 0.0; |
| 5152 |
t2p->pdf_mediabox.y1 = 0.0; |
| 5153 |
t2p->pdf_mediabox.x2 = t2p->pdf_pagewidth; |
| 5154 |
t2p->pdf_mediabox.y2 = t2p->pdf_pagelength; |
| 5155 |
t2p->pdf_imagebox.x1 = 0.0; |
| 5156 |
t2p->pdf_imagebox.y1 = 0.0; |
| 5157 |
t2p->pdf_imagebox.x2 = t2p->pdf_imagewidth; |
| 5158 |
t2p->pdf_imagebox.y2 = t2p->pdf_imagelength; |
| 5159 |
if (t2p->pdf_overridepagesize != 0) |
| 5160 |
{ |
| 5161 |
t2p->pdf_imagebox.x1 += |
| 5162 |
((t2p->pdf_pagewidth - t2p->pdf_imagewidth) / 2.0F); |
| 5163 |
t2p->pdf_imagebox.y1 += |
| 5164 |
((t2p->pdf_pagelength - t2p->pdf_imagelength) / 2.0F); |
| 5165 |
t2p->pdf_imagebox.x2 += |
| 5166 |
((t2p->pdf_pagewidth - t2p->pdf_imagewidth) / 2.0F); |
| 5167 |
t2p->pdf_imagebox.y2 += |
| 5168 |
((t2p->pdf_pagelength - t2p->pdf_imagelength) / 2.0F); |
| 5169 |
} |
| 5170 |
if (t2p->tiff_orientation > 4) |
| 5171 |
{ |
| 5172 |
f = t2p->pdf_mediabox.x2; |
| 5173 |
t2p->pdf_mediabox.x2 = t2p->pdf_mediabox.y2; |
| 5174 |
t2p->pdf_mediabox.y2 = f; |
| 5175 |
} |
| 5176 |
istiled = ((t2p->tiff_tiles[t2p->pdf_page]).tiles_tilecount == 0) ? 0 : 1; |
| 5177 |
if (istiled == 0) |
| 5178 |
{ |
| 5179 |
t2p_compose_pdf_page_orient(&(t2p->pdf_imagebox), |
| 5180 |
t2p->tiff_orientation); |
| 5181 |
return; |
| 5182 |
} |
| 5183 |
else |
| 5184 |
{ |
| 5185 |
tilewidth = (t2p->tiff_tiles[t2p->pdf_page]).tiles_tilewidth; |
| 5186 |
tilelength = (t2p->tiff_tiles[t2p->pdf_page]).tiles_tilelength; |
| 5187 |
if (tilewidth > INT_MAX || tilelength > INT_MAX || |
| 5188 |
t2p->tiff_width > INT_MAX - tilewidth || |
| 5189 |
t2p->tiff_length > INT_MAX - tilelength) |
| 5190 |
{ |
| 5191 |
TIFFError(TIFF2PDF_MODULE, "Integer overflow"); |
| 5192 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 5193 |
return; |
| 5194 |
} |
| 5195 |
tilecountx = (t2p->tiff_width + tilewidth - 1) / tilewidth; |
| 5196 |
(t2p->tiff_tiles[t2p->pdf_page]).tiles_tilecountx = tilecountx; |
| 5197 |
tilecounty = (t2p->tiff_length + tilelength - 1) / tilelength; |
| 5198 |
(t2p->tiff_tiles[t2p->pdf_page]).tiles_tilecounty = tilecounty; |
| 5199 |
(t2p->tiff_tiles[t2p->pdf_page]).tiles_edgetilewidth = |
| 5200 |
t2p->tiff_width % tilewidth; |
| 5201 |
(t2p->tiff_tiles[t2p->pdf_page]).tiles_edgetilelength = |
| 5202 |
t2p->tiff_length % tilelength; |
| 5203 |
tiles = (t2p->tiff_tiles[t2p->pdf_page]).tiles_tiles; |
| 5204 |
for (i2 = 0; i2 < tilecounty - 1; i2++) |
| 5205 |
{ |
| 5206 |
for (i = 0; i < tilecountx - 1; i++) |
| 5207 |
{ |
| 5208 |
boxp = &(tiles[i2 * tilecountx + i].tile_box); |
| 5209 |
boxp->x1 = t2p->pdf_imagebox.x1 + |
| 5210 |
((float)(t2p->pdf_imagewidth * i * tilewidth) / |
| 5211 |
(float)t2p->tiff_width); |
| 5212 |
boxp->x2 = t2p->pdf_imagebox.x1 + |
| 5213 |
((float)(t2p->pdf_imagewidth * (i + 1) * tilewidth) / |
| 5214 |
(float)t2p->tiff_width); |
| 5215 |
boxp->y1 = |
| 5216 |
t2p->pdf_imagebox.y2 - |
| 5217 |
((float)(t2p->pdf_imagelength * (i2 + 1) * tilelength) / |
| 5218 |
(float)t2p->tiff_length); |
| 5219 |
boxp->y2 = t2p->pdf_imagebox.y2 - |
| 5220 |
((float)(t2p->pdf_imagelength * i2 * tilelength) / |
| 5221 |
(float)t2p->tiff_length); |
| 5222 |
} |
| 5223 |
boxp = &(tiles[i2 * tilecountx + i].tile_box); |
| 5224 |
boxp->x1 = t2p->pdf_imagebox.x1 + |
| 5225 |
((float)(t2p->pdf_imagewidth * i * tilewidth) / |
| 5226 |
(float)t2p->tiff_width); |
| 5227 |
boxp->x2 = t2p->pdf_imagebox.x2; |
| 5228 |
boxp->y1 = t2p->pdf_imagebox.y2 - |
| 5229 |
((float)(t2p->pdf_imagelength * (i2 + 1) * tilelength) / |
| 5230 |
(float)t2p->tiff_length); |
| 5231 |
boxp->y2 = t2p->pdf_imagebox.y2 - |
| 5232 |
((float)(t2p->pdf_imagelength * i2 * tilelength) / |
| 5233 |
(float)t2p->tiff_length); |
| 5234 |
} |
| 5235 |
for (i = 0; i < tilecountx - 1; i++) |
| 5236 |
{ |
| 5237 |
boxp = &(tiles[i2 * tilecountx + i].tile_box); |
| 5238 |
boxp->x1 = t2p->pdf_imagebox.x1 + |
| 5239 |
((float)(t2p->pdf_imagewidth * i * tilewidth) / |
| 5240 |
(float)t2p->tiff_width); |
| 5241 |
boxp->x2 = t2p->pdf_imagebox.x1 + |
| 5242 |
((float)(t2p->pdf_imagewidth * (i + 1) * tilewidth) / |
| 5243 |
(float)t2p->tiff_width); |
| 5244 |
boxp->y1 = t2p->pdf_imagebox.y1; |
| 5245 |
boxp->y2 = t2p->pdf_imagebox.y2 - |
| 5246 |
((float)(t2p->pdf_imagelength * i2 * tilelength) / |
| 5247 |
(float)t2p->tiff_length); |
| 5248 |
} |
| 5249 |
boxp = &(tiles[i2 * tilecountx + i].tile_box); |
| 5250 |
boxp->x1 = t2p->pdf_imagebox.x1 + |
| 5251 |
((float)(t2p->pdf_imagewidth * i * tilewidth) / |
| 5252 |
(float)t2p->tiff_width); |
| 5253 |
boxp->x2 = t2p->pdf_imagebox.x2; |
| 5254 |
boxp->y1 = t2p->pdf_imagebox.y1; |
| 5255 |
boxp->y2 = t2p->pdf_imagebox.y2 - |
| 5256 |
((float)(t2p->pdf_imagelength * i2 * tilelength) / |
| 5257 |
(float)t2p->tiff_length); |
| 5258 |
} |
| 5259 |
if (t2p->tiff_orientation == 0 || t2p->tiff_orientation == 1) |
| 5260 |
{ |
| 5261 |
for (i = 0; i < (t2p->tiff_tiles[t2p->pdf_page]).tiles_tilecount; i++) |
| 5262 |
{ |
| 5263 |
t2p_compose_pdf_page_orient(&(tiles[i].tile_box), 0); |
| 5264 |
} |
| 5265 |
return; |
| 5266 |
} |
| 5267 |
for (i = 0; i < (t2p->tiff_tiles[t2p->pdf_page]).tiles_tilecount; i++) |
| 5268 |
{ |
| 5269 |
boxp = &(tiles[i].tile_box); |
| 5270 |
boxp->x1 -= t2p->pdf_imagebox.x1; |
| 5271 |
boxp->x2 -= t2p->pdf_imagebox.x1; |
| 5272 |
boxp->y1 -= t2p->pdf_imagebox.y1; |
| 5273 |
boxp->y2 -= t2p->pdf_imagebox.y1; |
| 5274 |
if (t2p->tiff_orientation == 2 || t2p->tiff_orientation == 3) |
| 5275 |
{ |
| 5276 |
boxp->x1 = t2p->pdf_imagebox.x2 - t2p->pdf_imagebox.x1 - boxp->x1; |
| 5277 |
boxp->x2 = t2p->pdf_imagebox.x2 - t2p->pdf_imagebox.x1 - boxp->x2; |
| 5278 |
} |
| 5279 |
if (t2p->tiff_orientation == 3 || t2p->tiff_orientation == 4) |
| 5280 |
{ |
| 5281 |
boxp->y1 = t2p->pdf_imagebox.y2 - t2p->pdf_imagebox.y1 - boxp->y1; |
| 5282 |
boxp->y2 = t2p->pdf_imagebox.y2 - t2p->pdf_imagebox.y1 - boxp->y2; |
| 5283 |
} |
| 5284 |
if (t2p->tiff_orientation == 8 || t2p->tiff_orientation == 5) |
| 5285 |
{ |
| 5286 |
boxp->y1 = t2p->pdf_imagebox.y2 - t2p->pdf_imagebox.y1 - boxp->y1; |
| 5287 |
boxp->y2 = t2p->pdf_imagebox.y2 - t2p->pdf_imagebox.y1 - boxp->y2; |
| 5288 |
} |
| 5289 |
if (t2p->tiff_orientation == 5 || t2p->tiff_orientation == 6) |
| 5290 |
{ |
| 5291 |
boxp->x1 = t2p->pdf_imagebox.x2 - t2p->pdf_imagebox.x1 - boxp->x1; |
| 5292 |
boxp->x2 = t2p->pdf_imagebox.x2 - t2p->pdf_imagebox.x1 - boxp->x2; |
| 5293 |
} |
| 5294 |
if (t2p->tiff_orientation > 4) |
| 5295 |
{ |
| 5296 |
f = boxp->x1; |
| 5297 |
boxp->x1 = boxp->y1; |
| 5298 |
boxp->y1 = f; |
| 5299 |
f = boxp->x2; |
| 5300 |
boxp->x2 = boxp->y2; |
| 5301 |
boxp->y2 = f; |
| 5302 |
t2p_compose_pdf_page_orient_flip(boxp, t2p->tiff_orientation); |
| 5303 |
} |
| 5304 |
else |
| 5305 |
{ |
| 5306 |
t2p_compose_pdf_page_orient(boxp, t2p->tiff_orientation); |
| 5307 |
} |
| 5308 |
} |
| 5309 |
|
| 5310 |
return; |
| 5311 |
} |
| 5312 |
|
| 5313 |
void t2p_compose_pdf_page_orient(T2P_BOX *boxp, uint16_t orientation) |
| 5314 |
{ |
| 5315 |
|
| 5316 |
float m1[9]; |
| 5317 |
float f = 0.0; |
| 5318 |
|
| 5319 |
if (boxp->x1 > boxp->x2) |
| 5320 |
{ |
| 5321 |
f = boxp->x1; |
| 5322 |
boxp->x1 = boxp->x2; |
| 5323 |
boxp->x2 = f; |
| 5324 |
} |
| 5325 |
if (boxp->y1 > boxp->y2) |
| 5326 |
{ |
| 5327 |
f = boxp->y1; |
| 5328 |
boxp->y1 = boxp->y2; |
| 5329 |
boxp->y2 = f; |
| 5330 |
} |
| 5331 |
boxp->mat[0] = m1[0] = boxp->x2 - boxp->x1; |
| 5332 |
boxp->mat[1] = m1[1] = 0.0; |
| 5333 |
boxp->mat[2] = m1[2] = 0.0; |
| 5334 |
boxp->mat[3] = m1[3] = 0.0; |
| 5335 |
boxp->mat[4] = m1[4] = boxp->y2 - boxp->y1; |
| 5336 |
boxp->mat[5] = m1[5] = 0.0; |
| 5337 |
boxp->mat[6] = m1[6] = boxp->x1; |
| 5338 |
boxp->mat[7] = m1[7] = boxp->y1; |
| 5339 |
boxp->mat[8] = m1[8] = 1.0; |
| 5340 |
switch (orientation) |
| 5341 |
{ |
| 5342 |
case 0: |
| 5343 |
case 1: |
| 5344 |
break; |
| 5345 |
case 2: |
| 5346 |
boxp->mat[0] = 0.0F - m1[0]; |
| 5347 |
boxp->mat[6] += m1[0]; |
| 5348 |
break; |
| 5349 |
case 3: |
| 5350 |
boxp->mat[0] = 0.0F - m1[0]; |
| 5351 |
boxp->mat[4] = 0.0F - m1[4]; |
| 5352 |
boxp->mat[6] += m1[0]; |
| 5353 |
boxp->mat[7] += m1[4]; |
| 5354 |
break; |
| 5355 |
case 4: |
| 5356 |
boxp->mat[4] = 0.0F - m1[4]; |
| 5357 |
boxp->mat[7] += m1[4]; |
| 5358 |
break; |
| 5359 |
case 5: |
| 5360 |
boxp->mat[0] = 0.0F; |
| 5361 |
boxp->mat[1] = 0.0F - m1[0]; |
| 5362 |
boxp->mat[3] = 0.0F - m1[4]; |
| 5363 |
boxp->mat[4] = 0.0F; |
| 5364 |
boxp->mat[6] += m1[4]; |
| 5365 |
boxp->mat[7] += m1[0]; |
| 5366 |
break; |
| 5367 |
case 6: |
| 5368 |
boxp->mat[0] = 0.0F; |
| 5369 |
boxp->mat[1] = 0.0F - m1[0]; |
| 5370 |
boxp->mat[3] = m1[4]; |
| 5371 |
boxp->mat[4] = 0.0F; |
| 5372 |
boxp->mat[7] += m1[0]; |
| 5373 |
break; |
| 5374 |
case 7: |
| 5375 |
boxp->mat[0] = 0.0F; |
| 5376 |
boxp->mat[1] = m1[0]; |
| 5377 |
boxp->mat[3] = m1[4]; |
| 5378 |
boxp->mat[4] = 0.0F; |
| 5379 |
break; |
| 5380 |
case 8: |
| 5381 |
boxp->mat[0] = 0.0F; |
| 5382 |
boxp->mat[1] = m1[0]; |
| 5383 |
boxp->mat[3] = 0.0F - m1[4]; |
| 5384 |
boxp->mat[4] = 0.0F; |
| 5385 |
boxp->mat[6] += m1[4]; |
| 5386 |
break; |
| 5387 |
} |
| 5388 |
|
| 5389 |
return; |
| 5390 |
} |
| 5391 |
|
| 5392 |
void t2p_compose_pdf_page_orient_flip(T2P_BOX *boxp, uint16_t orientation) |
| 5393 |
{ |
| 5394 |
|
| 5395 |
float m1[9]; |
| 5396 |
float f = 0.0; |
| 5397 |
|
| 5398 |
if (boxp->x1 > boxp->x2) |
| 5399 |
{ |
| 5400 |
f = boxp->x1; |
| 5401 |
boxp->x1 = boxp->x2; |
| 5402 |
boxp->x2 = f; |
| 5403 |
} |
| 5404 |
if (boxp->y1 > boxp->y2) |
| 5405 |
{ |
| 5406 |
f = boxp->y1; |
| 5407 |
boxp->y1 = boxp->y2; |
| 5408 |
boxp->y2 = f; |
| 5409 |
} |
| 5410 |
boxp->mat[0] = m1[0] = boxp->x2 - boxp->x1; |
| 5411 |
boxp->mat[1] = m1[1] = 0.0F; |
| 5412 |
boxp->mat[2] = m1[2] = 0.0F; |
| 5413 |
boxp->mat[3] = m1[3] = 0.0F; |
| 5414 |
boxp->mat[4] = m1[4] = boxp->y2 - boxp->y1; |
| 5415 |
boxp->mat[5] = m1[5] = 0.0F; |
| 5416 |
boxp->mat[6] = m1[6] = boxp->x1; |
| 5417 |
boxp->mat[7] = m1[7] = boxp->y1; |
| 5418 |
boxp->mat[8] = m1[8] = 1.0F; |
| 5419 |
switch (orientation) |
| 5420 |
{ |
| 5421 |
case 5: |
| 5422 |
boxp->mat[0] = 0.0F; |
| 5423 |
boxp->mat[1] = 0.0F - m1[4]; |
| 5424 |
boxp->mat[3] = 0.0F - m1[0]; |
| 5425 |
boxp->mat[4] = 0.0F; |
| 5426 |
boxp->mat[6] += m1[0]; |
| 5427 |
boxp->mat[7] += m1[4]; |
| 5428 |
break; |
| 5429 |
case 6: |
| 5430 |
boxp->mat[0] = 0.0F; |
| 5431 |
boxp->mat[1] = 0.0F - m1[4]; |
| 5432 |
boxp->mat[3] = m1[0]; |
| 5433 |
boxp->mat[4] = 0.0F; |
| 5434 |
boxp->mat[7] += m1[4]; |
| 5435 |
break; |
| 5436 |
case 7: |
| 5437 |
boxp->mat[0] = 0.0F; |
| 5438 |
boxp->mat[1] = m1[4]; |
| 5439 |
boxp->mat[3] = m1[0]; |
| 5440 |
boxp->mat[4] = 0.0F; |
| 5441 |
break; |
| 5442 |
case 8: |
| 5443 |
boxp->mat[0] = 0.0F; |
| 5444 |
boxp->mat[1] = m1[4]; |
| 5445 |
boxp->mat[3] = 0.0F - m1[0]; |
| 5446 |
boxp->mat[4] = 0.0F; |
| 5447 |
boxp->mat[6] += m1[0]; |
| 5448 |
break; |
| 5449 |
} |
| 5450 |
|
| 5451 |
return; |
| 5452 |
} |
| 5453 |
|
| 5454 |
/* |
| 5455 |
This function writes a PDF Contents stream to output. |
| 5456 |
*/ |
| 5457 |
|
| 5458 |
tsize_t t2p_write_pdf_page_content_stream(T2P *t2p, TIFF *output) |
| 5459 |
{ |
| 5460 |
|
| 5461 |
tsize_t written = 0; |
| 5462 |
ttile_t i = 0; |
| 5463 |
char buffer[512]; |
| 5464 |
int buflen = 0; |
| 5465 |
T2P_BOX box; |
| 5466 |
|
| 5467 |
if (t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount > 0) |
| 5468 |
{ |
| 5469 |
for (i = 0; i < t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount; i++) |
| 5470 |
{ |
| 5471 |
box = t2p->tiff_tiles[t2p->pdf_page].tiles_tiles[i].tile_box; |
| 5472 |
buflen = snprintf( |
| 5473 |
buffer, sizeof(buffer), |
| 5474 |
"q %s %.4f %.4f %.4f %.4f %.4f %.4f cm /Im%" PRIu16 "_%" PRIu32 |
| 5475 |
" Do Q\n", |
| 5476 |
t2p->tiff_transferfunctioncount ? "/GS1 gs " : "", box.mat[0], |
| 5477 |
box.mat[1], box.mat[3], box.mat[4], box.mat[6], box.mat[7], |
| 5478 |
(uint16_t)(t2p->pdf_page + 1u), i + 1u); |
| 5479 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5480 |
written += t2p_write_pdf_stream(buffer, buflen, output); |
| 5481 |
} |
| 5482 |
} |
| 5483 |
else |
| 5484 |
{ |
| 5485 |
box = t2p->pdf_imagebox; |
| 5486 |
buflen = snprintf( |
| 5487 |
buffer, sizeof(buffer), |
| 5488 |
"q %s %.4f %.4f %.4f %.4f %.4f %.4f cm /Im%" PRIu16 " Do Q\n", |
| 5489 |
t2p->tiff_transferfunctioncount ? "/GS1 gs " : "", box.mat[0], |
| 5490 |
box.mat[1], box.mat[3], box.mat[4], box.mat[6], box.mat[7], |
| 5491 |
(uint16_t)(t2p->pdf_page + 1u)); |
| 5492 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5493 |
written += t2p_write_pdf_stream(buffer, buflen, output); |
| 5494 |
} |
| 5495 |
|
| 5496 |
return (written); |
| 5497 |
} |
| 5498 |
|
| 5499 |
/* |
| 5500 |
This function writes a PDF Image XObject stream dictionary to output. |
| 5501 |
*/ |
| 5502 |
|
| 5503 |
tsize_t t2p_write_pdf_xobject_stream_dict(ttile_t tile, T2P *t2p, TIFF *output) |
| 5504 |
{ |
| 5505 |
|
| 5506 |
tsize_t written = 0; |
| 5507 |
char buffer[32]; |
| 5508 |
int buflen = 0; |
| 5509 |
const char mod[] = "t2p_write_pdf_xobject_stream_dict()"; |
| 5510 |
|
| 5511 |
written += t2p_write_pdf_stream_dict(0, t2p->pdf_xrefcount + 1, output); |
| 5512 |
add_t2pWriteFile_check( |
| 5513 |
output, (tdata_t) "/Type /XObject \n/Subtype /Image \n/Name /Im", 42, |
| 5514 |
mod, written); |
| 5515 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu16, |
| 5516 |
(uint16_t)(t2p->pdf_page + 1u)); |
| 5517 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5518 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5519 |
if (tile != 0) |
| 5520 |
{ |
| 5521 |
add_t2pWriteFile_check(output, (tdata_t) "_", 1, mod, written); |
| 5522 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, tile); |
| 5523 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5524 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5525 |
} |
| 5526 |
add_t2pWriteFile_check(output, (tdata_t) "\n/Width ", 8, mod, written); |
| 5527 |
if (tile == 0) |
| 5528 |
{ |
| 5529 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, t2p->tiff_width); |
| 5530 |
} |
| 5531 |
else |
| 5532 |
{ |
| 5533 |
if (t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], tile - 1) != |
| 5534 |
0) |
| 5535 |
{ |
| 5536 |
buflen = |
| 5537 |
snprintf(buffer, sizeof(buffer), "%" PRIu32, |
| 5538 |
t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth); |
| 5539 |
} |
| 5540 |
else |
| 5541 |
{ |
| 5542 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, |
| 5543 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth); |
| 5544 |
} |
| 5545 |
} |
| 5546 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5547 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5548 |
add_t2pWriteFile_check(output, (tdata_t) "\n/Height ", 9, mod, written); |
| 5549 |
if (tile == 0) |
| 5550 |
{ |
| 5551 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, t2p->tiff_length); |
| 5552 |
} |
| 5553 |
else |
| 5554 |
{ |
| 5555 |
if (t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], tile - 1) != |
| 5556 |
0) |
| 5557 |
{ |
| 5558 |
buflen = |
| 5559 |
snprintf(buffer, sizeof(buffer), "%" PRIu32, |
| 5560 |
t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength); |
| 5561 |
} |
| 5562 |
else |
| 5563 |
{ |
| 5564 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, |
| 5565 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 5566 |
} |
| 5567 |
} |
| 5568 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5569 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5570 |
add_t2pWriteFile_check(output, (tdata_t) "\n/BitsPerComponent ", 19, mod, |
| 5571 |
written); |
| 5572 |
buflen = |
| 5573 |
snprintf(buffer, sizeof(buffer), "%" PRIu16, t2p->tiff_bitspersample); |
| 5574 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5575 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5576 |
add_t2pWriteFile_check(output, (tdata_t) "\n/ColorSpace ", 13, mod, |
| 5577 |
written); |
| 5578 |
written += t2p_write_pdf_xobject_cs(t2p, output); |
| 5579 |
if (t2p->pdf_image_interpolate) |
| 5580 |
add_t2pWriteFile_check(output, (tdata_t) "\n/Interpolate true", 18, mod, |
| 5581 |
written); |
| 5582 |
if ((t2p->pdf_switchdecode != 0) |
| 5583 |
#ifdef CCITT_SUPPORT |
| 5584 |
&& !(t2p->pdf_colorspace & T2P_CS_BILEVEL && |
| 5585 |
t2p->pdf_compression == T2P_COMPRESS_G4) |
| 5586 |
#endif |
| 5587 |
) |
| 5588 |
{ |
| 5589 |
written += t2p_write_pdf_xobject_decode(t2p, output); |
| 5590 |
} |
| 5591 |
written += t2p_write_pdf_xobject_stream_filter(tile, t2p, output); |
| 5592 |
|
| 5593 |
return (written); |
| 5594 |
} |
| 5595 |
|
| 5596 |
/* used to normalize the White Point */ |
| 5597 |
#define normalizePoint(x, y, z) \ |
| 5598 |
do \ |
| 5599 |
{ \ |
| 5600 |
if (y != 0.0F) \ |
| 5601 |
{ \ |
| 5602 |
x /= y; \ |
| 5603 |
z /= y; \ |
| 5604 |
y = 1.0F; \ |
| 5605 |
} \ |
| 5606 |
} while (0) |
| 5607 |
|
| 5608 |
/* |
| 5609 |
* This function writes a PDF Image XObject Colorspace name to output. |
| 5610 |
*/ |
| 5611 |
|
| 5612 |
tsize_t t2p_write_pdf_xobject_cs(T2P *t2p, TIFF *output) |
| 5613 |
{ |
| 5614 |
|
| 5615 |
tsize_t written = 0; |
| 5616 |
char buffer[128]; |
| 5617 |
int buflen = 0; |
| 5618 |
|
| 5619 |
float X_W = 1.0; |
| 5620 |
float Y_W = 1.0; |
| 5621 |
float Z_W = 1.0; |
| 5622 |
const char mod[] = "t2p_write_pdf_xobject_cs()"; |
| 5623 |
|
| 5624 |
if ((t2p->pdf_colorspace & T2P_CS_ICCBASED) != 0) |
| 5625 |
{ |
| 5626 |
written += t2p_write_pdf_xobject_icccs(t2p, output); |
| 5627 |
return (written); |
| 5628 |
} |
| 5629 |
if ((t2p->pdf_colorspace & T2P_CS_PALETTE) != 0) |
| 5630 |
{ |
| 5631 |
add_t2pWriteFile_check(output, (tdata_t) "[ /Indexed ", 11, mod, |
| 5632 |
written); |
| 5633 |
t2p->pdf_colorspace ^= T2P_CS_PALETTE; |
| 5634 |
written += t2p_write_pdf_xobject_cs(t2p, output); |
| 5635 |
t2p->pdf_colorspace |= T2P_CS_PALETTE; |
| 5636 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, |
| 5637 |
(uint32_t)(0x1u << t2p->tiff_bitspersample) - 1u); |
| 5638 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5639 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5640 |
add_t2pWriteFile_check(output, (tdata_t) " ", 1, mod, written); |
| 5641 |
buflen = |
| 5642 |
snprintf(buffer, sizeof(buffer), "%" PRIu32, t2p->pdf_palettecs); |
| 5643 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5644 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5645 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R ]\n", 7, mod, written); |
| 5646 |
return (written); |
| 5647 |
} |
| 5648 |
if (t2p->pdf_colorspace & T2P_CS_BILEVEL) |
| 5649 |
{ |
| 5650 |
add_t2pWriteFile_check(output, (tdata_t) "/DeviceGray \n", 13, mod, |
| 5651 |
written); |
| 5652 |
} |
| 5653 |
if (t2p->pdf_colorspace & T2P_CS_GRAY) |
| 5654 |
{ |
| 5655 |
if (t2p->pdf_colorspace & T2P_CS_CALGRAY) |
| 5656 |
{ |
| 5657 |
written += t2p_write_pdf_xobject_calcs(t2p, output); |
| 5658 |
} |
| 5659 |
else |
| 5660 |
{ |
| 5661 |
add_t2pWriteFile_check(output, (tdata_t) "/DeviceGray \n", 13, mod, |
| 5662 |
written); |
| 5663 |
} |
| 5664 |
} |
| 5665 |
if (t2p->pdf_colorspace & T2P_CS_RGB) |
| 5666 |
{ |
| 5667 |
if (t2p->pdf_colorspace & T2P_CS_CALRGB) |
| 5668 |
{ |
| 5669 |
written += t2p_write_pdf_xobject_calcs(t2p, output); |
| 5670 |
} |
| 5671 |
else |
| 5672 |
{ |
| 5673 |
add_t2pWriteFile_check(output, (tdata_t) "/DeviceRGB \n", 12, mod, |
| 5674 |
written); |
| 5675 |
} |
| 5676 |
} |
| 5677 |
if (t2p->pdf_colorspace & T2P_CS_CMYK) |
| 5678 |
{ |
| 5679 |
add_t2pWriteFile_check(output, (tdata_t) "/DeviceCMYK \n", 13, mod, |
| 5680 |
written); |
| 5681 |
} |
| 5682 |
if (t2p->pdf_colorspace & T2P_CS_LAB) |
| 5683 |
{ |
| 5684 |
add_t2pWriteFile_check(output, (tdata_t) "[/Lab << \n", 10, mod, |
| 5685 |
written); |
| 5686 |
add_t2pWriteFile_check(output, (tdata_t) "/WhitePoint ", 12, mod, |
| 5687 |
written); |
| 5688 |
X_W = t2p->tiff_whitechromaticities[0]; |
| 5689 |
Y_W = t2p->tiff_whitechromaticities[1]; |
| 5690 |
Z_W = 1.0F - (X_W + Y_W); |
| 5691 |
normalizePoint(X_W, Y_W, Z_W); |
| 5692 |
buflen = snprintf(buffer, sizeof(buffer), "[%.4f %.4f %.4f] \n", X_W, |
| 5693 |
Y_W, Z_W); |
| 5694 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5695 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5696 |
add_t2pWriteFile_check(output, (tdata_t) "/Range ", 7, mod, written); |
| 5697 |
buflen = snprintf(buffer, sizeof(buffer), "[%d %d %d %d] \n", |
| 5698 |
t2p->pdf_labrange[0], t2p->pdf_labrange[1], |
| 5699 |
t2p->pdf_labrange[2], t2p->pdf_labrange[3]); |
| 5700 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5701 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5702 |
add_t2pWriteFile_check(output, (tdata_t) ">>] \n", 5, mod, written); |
| 5703 |
} |
| 5704 |
|
| 5705 |
return (written); |
| 5706 |
} |
| 5707 |
|
| 5708 |
tsize_t t2p_write_pdf_transfer(T2P *t2p, TIFF *output) |
| 5709 |
{ |
| 5710 |
|
| 5711 |
tsize_t written = 0; |
| 5712 |
char buffer[32]; |
| 5713 |
int buflen = 0; |
| 5714 |
const char mod[] = "t2p_write_pdf_transfer()"; |
| 5715 |
|
| 5716 |
add_t2pWriteFile_check(output, (tdata_t) "<< /Type /ExtGState \n/TR ", 25, |
| 5717 |
mod, written); |
| 5718 |
if (t2p->tiff_transferfunctioncount == 1) |
| 5719 |
{ |
| 5720 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, |
| 5721 |
t2p->pdf_xrefcount + 1u); |
| 5722 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5723 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5724 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R ", 5, mod, written); |
| 5725 |
} |
| 5726 |
else |
| 5727 |
{ |
| 5728 |
add_t2pWriteFile_check(output, (tdata_t) "[ ", 2, mod, written); |
| 5729 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, |
| 5730 |
t2p->pdf_xrefcount + 1u); |
| 5731 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5732 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5733 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R ", 5, mod, written); |
| 5734 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, |
| 5735 |
t2p->pdf_xrefcount + 2u); |
| 5736 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5737 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5738 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R ", 5, mod, written); |
| 5739 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, |
| 5740 |
t2p->pdf_xrefcount + 3u); |
| 5741 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5742 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5743 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R ", 5, mod, written); |
| 5744 |
add_t2pWriteFile_check(output, (tdata_t) "/Identity ] ", 12, mod, |
| 5745 |
written); |
| 5746 |
} |
| 5747 |
|
| 5748 |
add_t2pWriteFile_check(output, (tdata_t) " >> \n", 5, mod, written); |
| 5749 |
|
| 5750 |
return (written); |
| 5751 |
} |
| 5752 |
|
| 5753 |
tsize_t t2p_write_pdf_transfer_dict(T2P *t2p, TIFF *output, uint16_t i) |
| 5754 |
{ |
| 5755 |
|
| 5756 |
tsize_t written = 0; |
| 5757 |
char buffer[32]; |
| 5758 |
int buflen = 0; |
| 5759 |
(void)i; /* XXX */ |
| 5760 |
const char mod[] = "t2p_write_pdf_transfer_dict()"; |
| 5761 |
|
| 5762 |
add_t2pWriteFile_check(output, (tdata_t) "/FunctionType 0 \n", 17, mod, |
| 5763 |
written); |
| 5764 |
add_t2pWriteFile_check(output, (tdata_t) "/Domain [0.0 1.0] \n", 19, mod, |
| 5765 |
written); |
| 5766 |
add_t2pWriteFile_check(output, (tdata_t) "/Range [0.0 1.0] \n", 18, mod, |
| 5767 |
written); |
| 5768 |
buflen = snprintf(buffer, sizeof(buffer), "/Size [%" PRIu16 "] \n", |
| 5769 |
(uint16_t)(1u << t2p->tiff_bitspersample)); |
| 5770 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5771 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5772 |
add_t2pWriteFile_check(output, (tdata_t) "/BitsPerSample 16 \n", 19, mod, |
| 5773 |
written); |
| 5774 |
written += t2p_write_pdf_stream_dict( |
| 5775 |
((tsize_t)1) << (t2p->tiff_bitspersample + 1), 0, output); |
| 5776 |
|
| 5777 |
return (written); |
| 5778 |
} |
| 5779 |
|
| 5780 |
tsize_t t2p_write_pdf_transfer_stream(T2P *t2p, TIFF *output, uint16_t i) |
| 5781 |
{ |
| 5782 |
|
| 5783 |
tsize_t written = 0; |
| 5784 |
|
| 5785 |
written += t2p_write_pdf_stream( |
| 5786 |
t2p->tiff_transferfunction[i], |
| 5787 |
(((tsize_t)1) << (t2p->tiff_bitspersample + 1)), output); |
| 5788 |
|
| 5789 |
return (written); |
| 5790 |
} |
| 5791 |
|
| 5792 |
/* |
| 5793 |
This function writes a PDF Image XObject Colorspace array to output. |
| 5794 |
*/ |
| 5795 |
|
| 5796 |
tsize_t t2p_write_pdf_xobject_calcs(T2P *t2p, TIFF *output) |
| 5797 |
{ |
| 5798 |
|
| 5799 |
tsize_t written = 0; |
| 5800 |
char buffer[256]; |
| 5801 |
int buflen = 0; |
| 5802 |
const char mod[] = "t2p_write_pdf_xobject_calcs()"; |
| 5803 |
|
| 5804 |
float X_W = 0.0; |
| 5805 |
float Y_W = 0.0; |
| 5806 |
float Z_W = 0.0; |
| 5807 |
float X_R = 0.0; |
| 5808 |
float Y_R = 0.0; |
| 5809 |
float Z_R = 0.0; |
| 5810 |
float X_G = 0.0; |
| 5811 |
float Y_G = 0.0; |
| 5812 |
float Z_G = 0.0; |
| 5813 |
float X_B = 0.0; |
| 5814 |
float Y_B = 0.0; |
| 5815 |
float Z_B = 0.0; |
| 5816 |
float x_w = 0.0; |
| 5817 |
float y_w = 0.0; |
| 5818 |
float z_w = 0.0; |
| 5819 |
float x_r = 0.0; |
| 5820 |
float y_r = 0.0; |
| 5821 |
float x_g = 0.0; |
| 5822 |
float y_g = 0.0; |
| 5823 |
float x_b = 0.0; |
| 5824 |
float y_b = 0.0; |
| 5825 |
float R = 1.0; |
| 5826 |
float G = 1.0; |
| 5827 |
float B = 1.0; |
| 5828 |
|
| 5829 |
add_t2pWriteFile_check(output, (tdata_t) "[", 1, mod, written); |
| 5830 |
if (t2p->pdf_colorspace & T2P_CS_CALGRAY) |
| 5831 |
{ |
| 5832 |
add_t2pWriteFile_check(output, (tdata_t) "/CalGray ", 9, mod, written); |
| 5833 |
X_W = t2p->tiff_whitechromaticities[0]; |
| 5834 |
Y_W = t2p->tiff_whitechromaticities[1]; |
| 5835 |
Z_W = 1.0F - (X_W + Y_W); |
| 5836 |
normalizePoint(X_W, Y_W, Z_W); |
| 5837 |
} |
| 5838 |
if (t2p->pdf_colorspace & T2P_CS_CALRGB) |
| 5839 |
{ |
| 5840 |
add_t2pWriteFile_check(output, (tdata_t) "/CalRGB ", 8, mod, written); |
| 5841 |
x_w = t2p->tiff_whitechromaticities[0]; |
| 5842 |
y_w = t2p->tiff_whitechromaticities[1]; |
| 5843 |
x_r = t2p->tiff_primarychromaticities[0]; |
| 5844 |
y_r = t2p->tiff_primarychromaticities[1]; |
| 5845 |
x_g = t2p->tiff_primarychromaticities[2]; |
| 5846 |
y_g = t2p->tiff_primarychromaticities[3]; |
| 5847 |
x_b = t2p->tiff_primarychromaticities[4]; |
| 5848 |
y_b = t2p->tiff_primarychromaticities[5]; |
| 5849 |
z_w = y_w * ((x_g - x_b) * y_r - (x_r - x_b) * y_g + (x_r - x_g) * y_b); |
| 5850 |
Y_R = (y_r / R) * |
| 5851 |
((x_g - x_b) * y_w - (x_w - x_b) * y_g + (x_w - x_g) * y_b) / z_w; |
| 5852 |
X_R = Y_R * x_r / y_r; |
| 5853 |
Z_R = Y_R * (((1 - x_r) / y_r) - 1); |
| 5854 |
Y_G = ((0.0F - (y_g)) / G) * |
| 5855 |
((x_r - x_b) * y_w - (x_w - x_b) * y_r + (x_w - x_r) * y_b) / z_w; |
| 5856 |
X_G = Y_G * x_g / y_g; |
| 5857 |
Z_G = Y_G * (((1 - x_g) / y_g) - 1); |
| 5858 |
Y_B = (y_b / B) * |
| 5859 |
((x_r - x_g) * y_w - (x_w - x_g) * y_r + (x_w - x_r) * y_g) / z_w; |
| 5860 |
X_B = Y_B * x_b / y_b; |
| 5861 |
Z_B = Y_B * (((1 - x_b) / y_b) - 1); |
| 5862 |
X_W = (X_R * R) + (X_G * G) + (X_B * B); |
| 5863 |
Y_W = (Y_R * R) + (Y_G * G) + (Y_B * B); |
| 5864 |
Z_W = (Z_R * R) + (Z_G * G) + (Z_B * B); |
| 5865 |
normalizePoint(X_W, Y_W, Z_W); |
| 5866 |
} |
| 5867 |
add_t2pWriteFile_check(output, (tdata_t) "<< \n", 4, mod, written); |
| 5868 |
if (t2p->pdf_colorspace & T2P_CS_CALGRAY) |
| 5869 |
{ |
| 5870 |
add_t2pWriteFile_check(output, (tdata_t) "/WhitePoint ", 12, mod, |
| 5871 |
written); |
| 5872 |
buflen = snprintf(buffer, sizeof(buffer), "[%.4f %.4f %.4f] \n", X_W, |
| 5873 |
Y_W, Z_W); |
| 5874 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5875 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5876 |
add_t2pWriteFile_check(output, (tdata_t) "/Gamma 2.2 \n", 12, mod, |
| 5877 |
written); |
| 5878 |
} |
| 5879 |
if (t2p->pdf_colorspace & T2P_CS_CALRGB) |
| 5880 |
{ |
| 5881 |
add_t2pWriteFile_check(output, (tdata_t) "/WhitePoint ", 12, mod, |
| 5882 |
written); |
| 5883 |
buflen = snprintf(buffer, sizeof(buffer), "[%.4f %.4f %.4f] \n", X_W, |
| 5884 |
Y_W, Z_W); |
| 5885 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5886 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5887 |
add_t2pWriteFile_check(output, (tdata_t) "/Matrix ", 8, mod, written); |
| 5888 |
buflen = snprintf(buffer, sizeof(buffer), |
| 5889 |
"[%.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f %.4f] \n", |
| 5890 |
X_R, Y_R, Z_R, X_G, Y_G, Z_G, X_B, Y_B, Z_B); |
| 5891 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5892 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5893 |
add_t2pWriteFile_check(output, (tdata_t) "/Gamma [2.2 2.2 2.2] \n", 22, |
| 5894 |
mod, written); |
| 5895 |
} |
| 5896 |
add_t2pWriteFile_check(output, (tdata_t) ">>] \n", 5, mod, written); |
| 5897 |
|
| 5898 |
return (written); |
| 5899 |
} |
| 5900 |
|
| 5901 |
/* |
| 5902 |
This function writes a PDF Image XObject Colorspace array to output. |
| 5903 |
*/ |
| 5904 |
|
| 5905 |
tsize_t t2p_write_pdf_xobject_icccs(T2P *t2p, TIFF *output) |
| 5906 |
{ |
| 5907 |
|
| 5908 |
tsize_t written = 0; |
| 5909 |
char buffer[32]; |
| 5910 |
int buflen = 0; |
| 5911 |
const char mod[] = "t2p_write_pdf_xobject_icccs()"; |
| 5912 |
|
| 5913 |
add_t2pWriteFile_check(output, (tdata_t) "[/ICCBased ", 11, mod, written); |
| 5914 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, t2p->pdf_icccs); |
| 5915 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5916 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5917 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R] \n", 7, mod, written); |
| 5918 |
|
| 5919 |
return (written); |
| 5920 |
} |
| 5921 |
|
| 5922 |
tsize_t t2p_write_pdf_xobject_icccs_dict(T2P *t2p, TIFF *output) |
| 5923 |
{ |
| 5924 |
|
| 5925 |
tsize_t written = 0; |
| 5926 |
char buffer[32]; |
| 5927 |
int buflen = 0; |
| 5928 |
const char mod[] = "t2p_write_pdf_xobject_icccs_dict()"; |
| 5929 |
|
| 5930 |
add_t2pWriteFile_check(output, (tdata_t) "/N ", 3, mod, written); |
| 5931 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu16 " \n", |
| 5932 |
t2p->tiff_samplesperpixel); |
| 5933 |
check_snprintf_ret(t2p, buflen, buffer); |
| 5934 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 5935 |
add_t2pWriteFile_check(output, (tdata_t) "/Alternate ", 11, mod, written); |
| 5936 |
t2p->pdf_colorspace ^= T2P_CS_ICCBASED; |
| 5937 |
written += t2p_write_pdf_xobject_cs(t2p, output); |
| 5938 |
t2p->pdf_colorspace |= T2P_CS_ICCBASED; |
| 5939 |
written += t2p_write_pdf_stream_dict(t2p->tiff_iccprofilelength, 0, output); |
| 5940 |
|
| 5941 |
return (written); |
| 5942 |
} |
| 5943 |
|
| 5944 |
tsize_t t2p_write_pdf_xobject_icccs_stream(T2P *t2p, TIFF *output) |
| 5945 |
{ |
| 5946 |
|
| 5947 |
tsize_t written = 0; |
| 5948 |
|
| 5949 |
written += |
| 5950 |
t2p_write_pdf_stream((tdata_t)t2p->tiff_iccprofile, |
| 5951 |
(tsize_t)t2p->tiff_iccprofilelength, output); |
| 5952 |
|
| 5953 |
return (written); |
| 5954 |
} |
| 5955 |
|
| 5956 |
/* |
| 5957 |
This function writes a palette stream for an indexed color space to |
| 5958 |
output. |
| 5959 |
*/ |
| 5960 |
|
| 5961 |
tsize_t t2p_write_pdf_xobject_palettecs_stream(T2P *t2p, TIFF *output) |
| 5962 |
{ |
| 5963 |
|
| 5964 |
tsize_t written = 0; |
| 5965 |
|
| 5966 |
written += t2p_write_pdf_stream((tdata_t)t2p->pdf_palette, |
| 5967 |
(tsize_t)t2p->pdf_palettesize, output); |
| 5968 |
|
| 5969 |
return (written); |
| 5970 |
} |
| 5971 |
|
| 5972 |
/* |
| 5973 |
This function writes a PDF Image XObject Decode array to output. |
| 5974 |
*/ |
| 5975 |
|
| 5976 |
tsize_t t2p_write_pdf_xobject_decode(T2P *t2p, TIFF *output) |
| 5977 |
{ |
| 5978 |
|
| 5979 |
tsize_t written = 0; |
| 5980 |
int i = 0; |
| 5981 |
const char mod[] = "t2p_write_pdf_xobject_decode()"; |
| 5982 |
|
| 5983 |
add_t2pWriteFile_check(output, (tdata_t) "/Decode [ ", 10, mod, written); |
| 5984 |
for (i = 0; i < t2p->tiff_samplesperpixel; i++) |
| 5985 |
{ |
| 5986 |
add_t2pWriteFile_check(output, (tdata_t) "1 0 ", 4, mod, written); |
| 5987 |
} |
| 5988 |
add_t2pWriteFile_check(output, (tdata_t) "]\n", 2, mod, written); |
| 5989 |
|
| 5990 |
return (written); |
| 5991 |
} |
| 5992 |
|
| 5993 |
/* |
| 5994 |
This function writes a PDF Image XObject stream filter name and |
| 5995 |
parameters to output. |
| 5996 |
*/ |
| 5997 |
|
| 5998 |
tsize_t t2p_write_pdf_xobject_stream_filter(ttile_t tile, T2P *t2p, |
| 5999 |
TIFF *output) |
| 6000 |
{ |
| 6001 |
|
| 6002 |
tsize_t written = 0; |
| 6003 |
char buffer[32]; |
| 6004 |
int buflen = 0; |
| 6005 |
const char mod[] = "t2p_write_pdf_xobject_stream_filter()"; |
| 6006 |
|
| 6007 |
if (t2p->pdf_compression == T2P_COMPRESS_NONE) |
| 6008 |
{ |
| 6009 |
return (written); |
| 6010 |
} |
| 6011 |
add_t2pWriteFile_check(output, (tdata_t) "/Filter ", 8, mod, written); |
| 6012 |
switch (t2p->pdf_compression) |
| 6013 |
{ |
| 6014 |
#ifdef CCITT_SUPPORT |
| 6015 |
case T2P_COMPRESS_G4: |
| 6016 |
add_t2pWriteFile_check(output, (tdata_t) "/CCITTFaxDecode ", 16, |
| 6017 |
mod, written); |
| 6018 |
add_t2pWriteFile_check(output, (tdata_t) "/DecodeParms ", 13, mod, |
| 6019 |
written); |
| 6020 |
add_t2pWriteFile_check(output, (tdata_t) "<< /K -1 ", 9, mod, |
| 6021 |
written); |
| 6022 |
if (tile == 0) |
| 6023 |
{ |
| 6024 |
add_t2pWriteFile_check(output, (tdata_t) "/Columns ", 9, mod, |
| 6025 |
written); |
| 6026 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, |
| 6027 |
t2p->tiff_width); |
| 6028 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6029 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, |
| 6030 |
written); |
| 6031 |
add_t2pWriteFile_check(output, (tdata_t) " /Rows ", 7, mod, |
| 6032 |
written); |
| 6033 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, |
| 6034 |
t2p->tiff_length); |
| 6035 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6036 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, |
| 6037 |
written); |
| 6038 |
} |
| 6039 |
else |
| 6040 |
{ |
| 6041 |
if (t2p_tile_is_right_edge(t2p->tiff_tiles[t2p->pdf_page], |
| 6042 |
tile - 1) == 0) |
| 6043 |
{ |
| 6044 |
add_t2pWriteFile_check(output, (tdata_t) "/Columns ", 9, |
| 6045 |
mod, written); |
| 6046 |
buflen = snprintf( |
| 6047 |
buffer, sizeof(buffer), "%" PRIu32, |
| 6048 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilewidth); |
| 6049 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6050 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, |
| 6051 |
written); |
| 6052 |
} |
| 6053 |
else |
| 6054 |
{ |
| 6055 |
add_t2pWriteFile_check(output, (tdata_t) "/Columns ", 9, |
| 6056 |
mod, written); |
| 6057 |
buflen = snprintf( |
| 6058 |
buffer, sizeof(buffer), "%" PRIu32, |
| 6059 |
t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilewidth); |
| 6060 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6061 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, |
| 6062 |
written); |
| 6063 |
} |
| 6064 |
if (t2p_tile_is_bottom_edge(t2p->tiff_tiles[t2p->pdf_page], |
| 6065 |
tile - 1) == 0) |
| 6066 |
{ |
| 6067 |
add_t2pWriteFile_check(output, (tdata_t) " /Rows ", 7, mod, |
| 6068 |
written); |
| 6069 |
buflen = snprintf( |
| 6070 |
buffer, sizeof(buffer), "%" PRIu32, |
| 6071 |
t2p->tiff_tiles[t2p->pdf_page].tiles_tilelength); |
| 6072 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6073 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, |
| 6074 |
written); |
| 6075 |
} |
| 6076 |
else |
| 6077 |
{ |
| 6078 |
add_t2pWriteFile_check(output, (tdata_t) " /Rows ", 7, mod, |
| 6079 |
written); |
| 6080 |
buflen = snprintf( |
| 6081 |
buffer, sizeof(buffer), "%" PRIu32, |
| 6082 |
t2p->tiff_tiles[t2p->pdf_page].tiles_edgetilelength); |
| 6083 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6084 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, |
| 6085 |
written); |
| 6086 |
} |
| 6087 |
} |
| 6088 |
if (t2p->pdf_switchdecode == 0) |
| 6089 |
{ |
| 6090 |
add_t2pWriteFile_check(output, (tdata_t) " /BlackIs1 true ", 16, |
| 6091 |
mod, written); |
| 6092 |
} |
| 6093 |
add_t2pWriteFile_check(output, (tdata_t) ">>\n", 3, mod, written); |
| 6094 |
break; |
| 6095 |
#endif |
| 6096 |
#ifdef JPEG_SUPPORT |
| 6097 |
case T2P_COMPRESS_JPEG: |
| 6098 |
add_t2pWriteFile_check(output, (tdata_t) "/DCTDecode ", 11, mod, |
| 6099 |
written); |
| 6100 |
|
| 6101 |
if (t2p->tiff_photometric != PHOTOMETRIC_YCBCR) |
| 6102 |
{ |
| 6103 |
add_t2pWriteFile_check(output, (tdata_t) "/DecodeParms ", 13, |
| 6104 |
mod, written); |
| 6105 |
add_t2pWriteFile_check(output, |
| 6106 |
(tdata_t) "<< /ColorTransform 1 >>\n", |
| 6107 |
24, mod, written); |
| 6108 |
} |
| 6109 |
break; |
| 6110 |
#endif |
| 6111 |
#ifdef ZIP_SUPPORT |
| 6112 |
case T2P_COMPRESS_ZIP: |
| 6113 |
add_t2pWriteFile_check(output, (tdata_t) "/FlateDecode ", 13, mod, |
| 6114 |
written); |
| 6115 |
if (t2p->pdf_compressionquality % 100) |
| 6116 |
{ |
| 6117 |
add_t2pWriteFile_check(output, (tdata_t) "/DecodeParms ", 13, |
| 6118 |
mod, written); |
| 6119 |
add_t2pWriteFile_check(output, (tdata_t) "<< /Predictor ", 14, |
| 6120 |
mod, written); |
| 6121 |
buflen = |
| 6122 |
snprintf(buffer, sizeof(buffer), "%" PRIu16, |
| 6123 |
(uint16_t)(t2p->pdf_compressionquality % 100u)); |
| 6124 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6125 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, |
| 6126 |
written); |
| 6127 |
add_t2pWriteFile_check(output, (tdata_t) " /Columns ", 10, mod, |
| 6128 |
written); |
| 6129 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, |
| 6130 |
t2p->tiff_width); |
| 6131 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6132 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, |
| 6133 |
written); |
| 6134 |
add_t2pWriteFile_check(output, (tdata_t) " /Colors ", 9, mod, |
| 6135 |
written); |
| 6136 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu16, |
| 6137 |
t2p->tiff_samplesperpixel); |
| 6138 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6139 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, |
| 6140 |
written); |
| 6141 |
add_t2pWriteFile_check(output, (tdata_t) " /BitsPerComponent ", |
| 6142 |
19, mod, written); |
| 6143 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu16, |
| 6144 |
t2p->tiff_bitspersample); |
| 6145 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6146 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, |
| 6147 |
written); |
| 6148 |
add_t2pWriteFile_check(output, (tdata_t) ">>\n", 3, mod, |
| 6149 |
written); |
| 6150 |
} |
| 6151 |
break; |
| 6152 |
#endif |
| 6153 |
default: |
| 6154 |
break; |
| 6155 |
} |
| 6156 |
|
| 6157 |
return (written); |
| 6158 |
} |
| 6159 |
|
| 6160 |
/* |
| 6161 |
This function writes a PDF xref table to output. |
| 6162 |
*/ |
| 6163 |
|
| 6164 |
tsize_t t2p_write_pdf_xreftable(T2P *t2p, TIFF *output) |
| 6165 |
{ |
| 6166 |
|
| 6167 |
tsize_t written = 0; |
| 6168 |
char buffer[64]; |
| 6169 |
int buflen = 0; |
| 6170 |
uint32_t i = 0; |
| 6171 |
const char mod[] = "t2p_write_pdf_xreftable()"; |
| 6172 |
|
| 6173 |
add_t2pWriteFile_check(output, (tdata_t) "xref\n0 ", 7, mod, written); |
| 6174 |
buflen = |
| 6175 |
snprintf(buffer, sizeof(buffer), "%" PRIu32, t2p->pdf_xrefcount + 1u); |
| 6176 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6177 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 6178 |
add_t2pWriteFile_check(output, (tdata_t) " \n0000000000 65535 f \n", 22, |
| 6179 |
mod, written); |
| 6180 |
for (i = 0; i < t2p->pdf_xrefcount; i++) |
| 6181 |
{ |
| 6182 |
snprintf(buffer, sizeof(buffer), "%.10lu 00000 n \n", |
| 6183 |
(unsigned long)t2p->pdf_xrefoffsets[i]); |
| 6184 |
add_t2pWriteFile_check(output, (tdata_t)buffer, 20, mod, written); |
| 6185 |
} |
| 6186 |
|
| 6187 |
return (written); |
| 6188 |
} |
| 6189 |
|
| 6190 |
/* |
| 6191 |
* This function writes a PDF trailer to output. |
| 6192 |
*/ |
| 6193 |
|
| 6194 |
tsize_t t2p_write_pdf_trailer(T2P *t2p, TIFF *output) |
| 6195 |
{ |
| 6196 |
|
| 6197 |
tsize_t written = 0; |
| 6198 |
char buffer[32]; |
| 6199 |
int buflen = 0; |
| 6200 |
size_t i = 0; |
| 6201 |
const char mod[] = "t2p_write_pdf_trailer()"; |
| 6202 |
|
| 6203 |
/* Silence CoverityScan warning about bad crypto function */ |
| 6204 |
/* coverity[dont_call] */ |
| 6205 |
for (i = 0; i < sizeof(t2p->pdf_fileid) - 8; i += 8) |
| 6206 |
snprintf(t2p->pdf_fileid + i, 9, "%.8X", rand()); |
| 6207 |
|
| 6208 |
add_t2pWriteFile_check(output, (tdata_t) "trailer\n<<\n/Size ", 17, mod, |
| 6209 |
written); |
| 6210 |
buflen = |
| 6211 |
snprintf(buffer, sizeof(buffer), "%" PRIu32, t2p->pdf_xrefcount + 1u); |
| 6212 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6213 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 6214 |
add_t2pWriteFile_check(output, (tdata_t) "\n/Root ", 7, mod, written); |
| 6215 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, t2p->pdf_catalog); |
| 6216 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6217 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 6218 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R \n/Info ", 12, mod, written); |
| 6219 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, t2p->pdf_info); |
| 6220 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6221 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 6222 |
add_t2pWriteFile_check(output, (tdata_t) " 0 R \n/ID[<", 11, mod, written); |
| 6223 |
add_t2pWriteFile_check(output, (tdata_t)t2p->pdf_fileid, |
| 6224 |
sizeof(t2p->pdf_fileid) - 1, mod, written); |
| 6225 |
add_t2pWriteFile_check(output, (tdata_t) "><", 2, mod, written); |
| 6226 |
add_t2pWriteFile_check(output, (tdata_t)t2p->pdf_fileid, |
| 6227 |
sizeof(t2p->pdf_fileid) - 1, mod, written); |
| 6228 |
add_t2pWriteFile_check(output, (tdata_t) ">]\n>>\nstartxref\n", 16, mod, |
| 6229 |
written); |
| 6230 |
buflen = snprintf(buffer, sizeof(buffer), "%" PRIu32, t2p->pdf_startxref); |
| 6231 |
check_snprintf_ret(t2p, buflen, buffer); |
| 6232 |
add_t2pWriteFile_check(output, (tdata_t)buffer, buflen, mod, written); |
| 6233 |
add_t2pWriteFile_check(output, (tdata_t) "\n%%EOF\n", 7, mod, written); |
| 6234 |
|
| 6235 |
return (written); |
| 6236 |
} |
| 6237 |
|
| 6238 |
/* |
| 6239 |
|
| 6240 |
This function writes a PDF to a file given a pointer to a TIFF. |
| 6241 |
|
| 6242 |
The idea with using a TIFF* as output for a PDF file is that the file |
| 6243 |
can be created with TIFFClientOpen for memory-mapped use within the TIFF |
| 6244 |
library, and TIFFWriteEncodedStrip can be used to write compressed data to |
| 6245 |
the output. The output is not actually a TIFF file, it is a PDF file. |
| 6246 |
|
| 6247 |
This function uses only t2pWriteFile and TIFFWriteEncodedStrip to write to |
| 6248 |
the output TIFF file. When libtiff would otherwise be writing data to the |
| 6249 |
output file, the write procedure of the TIFF structure is replaced with an |
| 6250 |
empty implementation. |
| 6251 |
|
| 6252 |
The first argument to the function is an initialized and validated T2P |
| 6253 |
context struct pointer. |
| 6254 |
|
| 6255 |
The second argument to the function is the TIFF* that is the input that has |
| 6256 |
been opened for reading and no other functions have been called upon it. |
| 6257 |
|
| 6258 |
The third argument to the function is the TIFF* that is the output that has |
| 6259 |
been opened for writing. It has to be opened so that it hasn't written any |
| 6260 |
data to the output. If the output is seekable then it's OK to seek to the |
| 6261 |
beginning of the file. The function only writes to the output PDF and does |
| 6262 |
not seek. See the example usage in the main() function. |
| 6263 |
|
| 6264 |
TIFF* output = TIFFOpen("output.pdf", "w"); |
| 6265 |
assert(output != NULL); |
| 6266 |
|
| 6267 |
if(output->tif_seekproc != NULL){ |
| 6268 |
t2pSeekFile(output, (toff_t) 0, SEEK_SET); |
| 6269 |
} |
| 6270 |
|
| 6271 |
This function returns the file size of the output PDF file. On error it |
| 6272 |
returns zero and the t2p->t2p_error variable is set to T2P_ERR_ERROR. |
| 6273 |
|
| 6274 |
After this function completes, call t2p_free on t2p, TIFFClose on input, |
| 6275 |
and TIFFClose on output. |
| 6276 |
*/ |
| 6277 |
|
| 6278 |
tsize_t t2p_write_pdf(T2P *t2p, TIFF *input, TIFF *output) |
| 6279 |
{ |
| 6280 |
|
| 6281 |
tsize_t written = 0; |
| 6282 |
ttile_t i2 = 0; |
| 6283 |
tsize_t streamlen = 0; |
| 6284 |
uint16_t i = 0; |
| 6285 |
|
| 6286 |
t2p_read_tiff_init(t2p, input); |
| 6287 |
if (t2p->t2p_error != T2P_ERR_OK) |
| 6288 |
{ |
| 6289 |
return (0); |
| 6290 |
} |
| 6291 |
t2p->pdf_xrefoffsets = (uint32_t *)_TIFFmalloc( |
| 6292 |
TIFFSafeMultiply(tmsize_t, t2p->pdf_xrefcount, sizeof(uint32_t))); |
| 6293 |
if (t2p->pdf_xrefoffsets == NULL) |
| 6294 |
{ |
| 6295 |
TIFFError(TIFF2PDF_MODULE, |
| 6296 |
"Can't allocate %" TIFF_SIZE_FORMAT |
| 6297 |
" bytes of memory for t2p_write_pdf", |
| 6298 |
sizeof(uint32_t) * t2p->pdf_xrefcount); |
| 6299 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 6300 |
return (written); |
| 6301 |
} |
| 6302 |
t2p->pdf_xrefcount = 0; |
| 6303 |
t2p->pdf_catalog = 1; |
| 6304 |
t2p->pdf_info = 2; |
| 6305 |
t2p->pdf_pages = 3; |
| 6306 |
written += t2p_write_pdf_header(t2p, output); |
| 6307 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++] = written; |
| 6308 |
t2p->pdf_catalog = t2p->pdf_xrefcount; |
| 6309 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 6310 |
written += t2p_write_pdf_catalog(t2p, output); |
| 6311 |
written += t2p_write_pdf_obj_end(output); |
| 6312 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++] = written; |
| 6313 |
t2p->pdf_info = t2p->pdf_xrefcount; |
| 6314 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 6315 |
written += t2p_write_pdf_info(t2p, input, output); |
| 6316 |
written += t2p_write_pdf_obj_end(output); |
| 6317 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++] = written; |
| 6318 |
t2p->pdf_pages = t2p->pdf_xrefcount; |
| 6319 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 6320 |
written += t2p_write_pdf_pages(t2p, output); |
| 6321 |
written += t2p_write_pdf_obj_end(output); |
| 6322 |
for (t2p->pdf_page = 0; t2p->pdf_page < t2p->tiff_pagecount; |
| 6323 |
t2p->pdf_page++) |
| 6324 |
{ |
| 6325 |
t2p_read_tiff_data(t2p, input); |
| 6326 |
if (t2p->t2p_error != T2P_ERR_OK) |
| 6327 |
{ |
| 6328 |
return (0); |
| 6329 |
} |
| 6330 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++] = written; |
| 6331 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 6332 |
written += t2p_write_pdf_page(t2p->pdf_xrefcount, t2p, output); |
| 6333 |
written += t2p_write_pdf_obj_end(output); |
| 6334 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++] = written; |
| 6335 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 6336 |
written += t2p_write_pdf_stream_dict_start(output); |
| 6337 |
written += t2p_write_pdf_stream_dict(0, t2p->pdf_xrefcount + 1, output); |
| 6338 |
written += t2p_write_pdf_stream_dict_end(output); |
| 6339 |
written += t2p_write_pdf_stream_start(output); |
| 6340 |
streamlen = written; |
| 6341 |
written += t2p_write_pdf_page_content_stream(t2p, output); |
| 6342 |
streamlen = written - streamlen; |
| 6343 |
written += t2p_write_pdf_stream_end(output); |
| 6344 |
written += t2p_write_pdf_obj_end(output); |
| 6345 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++] = written; |
| 6346 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 6347 |
written += t2p_write_pdf_stream_length(streamlen, output); |
| 6348 |
written += t2p_write_pdf_obj_end(output); |
| 6349 |
if (t2p->tiff_transferfunctioncount != 0) |
| 6350 |
{ |
| 6351 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++] = written; |
| 6352 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 6353 |
written += t2p_write_pdf_transfer(t2p, output); |
| 6354 |
written += t2p_write_pdf_obj_end(output); |
| 6355 |
for (i = 0; i < t2p->tiff_transferfunctioncount; i++) |
| 6356 |
{ |
| 6357 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++] = written; |
| 6358 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 6359 |
written += t2p_write_pdf_stream_dict_start(output); |
| 6360 |
written += t2p_write_pdf_transfer_dict(t2p, output, i); |
| 6361 |
written += t2p_write_pdf_stream_dict_end(output); |
| 6362 |
written += t2p_write_pdf_stream_start(output); |
| 6363 |
/* streamlen=written; */ /* value not used */ |
| 6364 |
written += t2p_write_pdf_transfer_stream(t2p, output, i); |
| 6365 |
/* streamlen=written-streamlen; */ /* value not used */ |
| 6366 |
written += t2p_write_pdf_stream_end(output); |
| 6367 |
written += t2p_write_pdf_obj_end(output); |
| 6368 |
} |
| 6369 |
} |
| 6370 |
if ((t2p->pdf_colorspace & T2P_CS_PALETTE) != 0) |
| 6371 |
{ |
| 6372 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++] = written; |
| 6373 |
t2p->pdf_palettecs = t2p->pdf_xrefcount; |
| 6374 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 6375 |
written += t2p_write_pdf_stream_dict_start(output); |
| 6376 |
written += |
| 6377 |
t2p_write_pdf_stream_dict(t2p->pdf_palettesize, 0, output); |
| 6378 |
written += t2p_write_pdf_stream_dict_end(output); |
| 6379 |
written += t2p_write_pdf_stream_start(output); |
| 6380 |
/* streamlen=written; */ /* value not used */ |
| 6381 |
written += t2p_write_pdf_xobject_palettecs_stream(t2p, output); |
| 6382 |
/* streamlen=written-streamlen; */ /* value not used */ |
| 6383 |
written += t2p_write_pdf_stream_end(output); |
| 6384 |
written += t2p_write_pdf_obj_end(output); |
| 6385 |
} |
| 6386 |
if ((t2p->pdf_colorspace & T2P_CS_ICCBASED) != 0) |
| 6387 |
{ |
| 6388 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++] = written; |
| 6389 |
t2p->pdf_icccs = t2p->pdf_xrefcount; |
| 6390 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 6391 |
written += t2p_write_pdf_stream_dict_start(output); |
| 6392 |
written += t2p_write_pdf_xobject_icccs_dict(t2p, output); |
| 6393 |
written += t2p_write_pdf_stream_dict_end(output); |
| 6394 |
written += t2p_write_pdf_stream_start(output); |
| 6395 |
/* streamlen=written; */ /* value not used */ |
| 6396 |
written += t2p_write_pdf_xobject_icccs_stream(t2p, output); |
| 6397 |
/* streamlen=written-streamlen; */ /* value not used */ |
| 6398 |
written += t2p_write_pdf_stream_end(output); |
| 6399 |
written += t2p_write_pdf_obj_end(output); |
| 6400 |
} |
| 6401 |
if (t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount != 0) |
| 6402 |
{ |
| 6403 |
for (i2 = 0; i2 < t2p->tiff_tiles[t2p->pdf_page].tiles_tilecount; |
| 6404 |
i2++) |
| 6405 |
{ |
| 6406 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++] = written; |
| 6407 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 6408 |
written += t2p_write_pdf_stream_dict_start(output); |
| 6409 |
written += |
| 6410 |
t2p_write_pdf_xobject_stream_dict(i2 + 1, t2p, output); |
| 6411 |
written += t2p_write_pdf_stream_dict_end(output); |
| 6412 |
written += t2p_write_pdf_stream_start(output); |
| 6413 |
streamlen = written; |
| 6414 |
t2p_read_tiff_size_tile(t2p, input, i2); |
| 6415 |
if (t2p->tiff_maxdatasize && |
| 6416 |
(t2p->tiff_datasize > t2p->tiff_maxdatasize)) |
| 6417 |
{ |
| 6418 |
TIFFError(TIFF2PDF_MODULE, |
| 6419 |
"Allocation of %" PRIu64 |
| 6420 |
" bytes is forbidden. Limit is %" PRIu64 |
| 6421 |
". Use -m option to change limit", |
| 6422 |
(uint64_t)t2p->tiff_datasize, |
| 6423 |
(uint64_t)t2p->tiff_maxdatasize); |
| 6424 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 6425 |
return (0); |
| 6426 |
} |
| 6427 |
written += t2p_readwrite_pdf_image_tile(t2p, input, output, i2); |
| 6428 |
t2p_write_advance_directory(t2p, output); |
| 6429 |
if (t2p->t2p_error != T2P_ERR_OK) |
| 6430 |
{ |
| 6431 |
return (0); |
| 6432 |
} |
| 6433 |
streamlen = written - streamlen; |
| 6434 |
written += t2p_write_pdf_stream_end(output); |
| 6435 |
written += t2p_write_pdf_obj_end(output); |
| 6436 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++] = written; |
| 6437 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 6438 |
written += t2p_write_pdf_stream_length(streamlen, output); |
| 6439 |
written += t2p_write_pdf_obj_end(output); |
| 6440 |
} |
| 6441 |
} |
| 6442 |
else |
| 6443 |
{ |
| 6444 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++] = written; |
| 6445 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 6446 |
written += t2p_write_pdf_stream_dict_start(output); |
| 6447 |
written += t2p_write_pdf_xobject_stream_dict(0, t2p, output); |
| 6448 |
written += t2p_write_pdf_stream_dict_end(output); |
| 6449 |
written += t2p_write_pdf_stream_start(output); |
| 6450 |
streamlen = written; |
| 6451 |
t2p_read_tiff_size(t2p, input); |
| 6452 |
if (t2p->tiff_maxdatasize && |
| 6453 |
(t2p->tiff_datasize > t2p->tiff_maxdatasize)) |
| 6454 |
{ |
| 6455 |
TIFFError(TIFF2PDF_MODULE, |
| 6456 |
"Allocation of %" PRIu64 |
| 6457 |
" bytes is forbidden. Limit is %" PRIu64 |
| 6458 |
". Use -m option to change limit", |
| 6459 |
(uint64_t)t2p->tiff_datasize, |
| 6460 |
(uint64_t)t2p->tiff_maxdatasize); |
| 6461 |
t2p->t2p_error = T2P_ERR_ERROR; |
| 6462 |
return (0); |
| 6463 |
} |
| 6464 |
written += t2p_readwrite_pdf_image(t2p, input, output); |
| 6465 |
t2p_write_advance_directory(t2p, output); |
| 6466 |
if (t2p->t2p_error != T2P_ERR_OK) |
| 6467 |
{ |
| 6468 |
return (0); |
| 6469 |
} |
| 6470 |
streamlen = written - streamlen; |
| 6471 |
written += t2p_write_pdf_stream_end(output); |
| 6472 |
written += t2p_write_pdf_obj_end(output); |
| 6473 |
t2p->pdf_xrefoffsets[t2p->pdf_xrefcount++] = written; |
| 6474 |
written += t2p_write_pdf_obj_start(t2p->pdf_xrefcount, output); |
| 6475 |
written += t2p_write_pdf_stream_length(streamlen, output); |
| 6476 |
written += t2p_write_pdf_obj_end(output); |
| 6477 |
} |
| 6478 |
} |
| 6479 |
t2p->pdf_startxref = written; |
| 6480 |
written += t2p_write_pdf_xreftable(t2p, output); |
| 6481 |
written += t2p_write_pdf_trailer(t2p, output); |
| 6482 |
t2p_disable(output); |
| 6483 |
|
| 6484 |
return (written); |
| 6485 |
} |