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