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