3 * tiff2pdf - converts a TIFF image to a PDF document
5 * Copyright (c) 2003 Ross Finlayson
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.
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.
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
27 #include "tif_config.h"
42 #define NULL ((void*)0)
48 #if defined(_WIN32) && defined(USE_WIN32_FILEIO)
51 #define unlink DeleteFileA
54 #define TIFF2PDF_MODULE "tiff2pdf"
55 #define T2P_VERSION "d"
57 /* This type is of PDF color spaces. */
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 */
70 /* This type is of PDF compression types. */
72 T2P_COMPRESS_NONE
=0x00
74 , T2P_COMPRESS_G4
=0x01
76 #if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT)
77 , T2P_COMPRESS_JPEG
=0x02
80 , T2P_COMPRESS_ZIP
=0x04
84 /* This type is whether TIFF image data can be used in PDF without transcoding. */
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 */
90 /* This type is of information about the data samples of the input image. */
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 */
104 /* This type is of error status of the T2P struct. */
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 */
110 /* This struct defines a logical page of a TIFF. */
112 tdir_t page_directory
;
114 ttile_t page_tilecount
;
118 /* This struct defines a PDF rectangle's coordinates. */
127 /* This struct defines a tile of a PDF. */
132 /* This struct defines information about the tiles on a PDF page. */
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
;
144 /* This struct is the context of a function to generate PDF from a TIFF. */
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
;
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
;
167 uint16 pdf_centimeters
;
168 uint16 pdf_overrideres
;
169 uint16 pdf_overridepagesize
;
170 float pdf_defaultxres
;
171 float pdf_defaultyres
;
174 float pdf_defaultpagewidth
;
175 float pdf_defaultpagelength
;
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
;
187 uint32 pdf_palettecs
;
188 uint16 pdf_fitwindow
;
189 uint32 pdf_startxref
;
197 t2p_cs_t pdf_colorspace
;
198 uint16 pdf_colorspace_invert
;
199 uint16 pdf_switchdecode
;
200 uint16 pdf_palettesize
;
201 unsigned char* pdf_palette
;
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
;
214 tdata_t pdf_ojpegdata
;
215 uint32 pdf_ojpegdatalength
;
216 uint32 pdf_ojpegiflength
;
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,
224 uint16 tiff_transferfunctioncount
;
226 uint32 tiff_iccprofilelength
;
227 tdata_t tiff_iccprofile
;
230 /* These functions are called by main. */
232 void tiff2pdf_usage(void);
233 int tiff2pdf_match_paper_size(float*, float*, char*);
235 /* These functions are used to generate a PDF from a TIFF. */
242 void t2p_validate(T2P
*);
243 tsize_t
t2p_write_pdf(T2P
*, TIFF
*, TIFF
*);
250 tsize_t
t2p_empty_readproc(thandle_t
, tdata_t
, tsize_t
);
251 tsize_t
t2p_empty_writeproc(thandle_t
, tdata_t
, tsize_t
);
252 toff_t
t2p_empty_seekproc(thandle_t
, toff_t
, int);
253 int t2p_empty_closeproc(thandle_t
);
254 void t2p_read_tiff_init(T2P
*, TIFF
*);
255 int t2p_cmp_t2p_page(const void*, const void*);
256 void t2p_read_tiff_data(T2P
*, TIFF
*);
257 void t2p_read_tiff_size(T2P
*, TIFF
*);
258 void t2p_read_tiff_size_tile(T2P
*, TIFF
*, ttile_t
);
259 int t2p_tile_is_right_edge(T2P_TILES
, ttile_t
);
260 int t2p_tile_is_bottom_edge(T2P_TILES
, ttile_t
);
261 int t2p_tile_is_edge(T2P_TILES
, ttile_t
);
262 int t2p_tile_is_corner_edge(T2P_TILES
, ttile_t
);
263 tsize_t
t2p_readwrite_pdf_image(T2P
*, TIFF
*, TIFF
*);
264 tsize_t
t2p_readwrite_pdf_image_tile(T2P
*, TIFF
*, TIFF
*, ttile_t
);
266 int t2p_process_ojpeg_tables(T2P
*, TIFF
*);
269 int t2p_process_jpeg_strip(unsigned char*, tsize_t
*, unsigned char*, tsize_t
*, tstrip_t
, uint32
);
271 void t2p_tile_collapse_left(tdata_t
, tsize_t
, uint32
, uint32
, uint32
);
272 void t2p_write_advance_directory(T2P
*, TIFF
*);
273 tsize_t
t2p_sample_planar_separate_to_contig(T2P
*, unsigned char*, unsigned char*, tsize_t
);
274 tsize_t
t2p_sample_realize_palette(T2P
*, unsigned char*);
275 tsize_t
t2p_sample_abgr_to_rgb(tdata_t
, uint32
);
276 tsize_t
t2p_sample_rgba_to_rgb(tdata_t
, uint32
);
277 tsize_t
t2p_sample_rgbaa_to_rgb(tdata_t
, uint32
);
278 tsize_t
t2p_sample_lab_signed_to_unsigned(tdata_t
, uint32
);
279 tsize_t
t2p_write_pdf_header(T2P
*, TIFF
*);
280 tsize_t
t2p_write_pdf_obj_start(uint32
, TIFF
*);
281 tsize_t
t2p_write_pdf_obj_end(TIFF
*);
282 tsize_t
t2p_write_pdf_name(char*, TIFF
*);
283 tsize_t
t2p_write_pdf_string(char*, TIFF
*);
284 tsize_t
t2p_write_pdf_stream(tdata_t
, tsize_t
, TIFF
*);
285 tsize_t
t2p_write_pdf_stream_start(TIFF
*);
286 tsize_t
t2p_write_pdf_stream_end(TIFF
*);
287 tsize_t
t2p_write_pdf_stream_dict(tsize_t
, uint32
, TIFF
*);
288 tsize_t
t2p_write_pdf_stream_dict_start(TIFF
*);
289 tsize_t
t2p_write_pdf_stream_dict_end(TIFF
*);
290 tsize_t
t2p_write_pdf_stream_length(tsize_t
, TIFF
*);
291 tsize_t
t2p_write_pdf_catalog(T2P
*, TIFF
*);
292 tsize_t
t2p_write_pdf_info(T2P
*, TIFF
*, TIFF
*);
293 void t2p_pdf_currenttime(T2P
*);
294 void t2p_pdf_tifftime(T2P
*, TIFF
*);
295 tsize_t
t2p_write_pdf_pages(T2P
*, TIFF
*);
296 tsize_t
t2p_write_pdf_page(uint32
, T2P
*, TIFF
*);
297 void t2p_compose_pdf_page(T2P
*);
298 void t2p_compose_pdf_page_orient(T2P_BOX
*, uint16
);
299 void t2p_compose_pdf_page_orient_flip(T2P_BOX
*, uint16
);
300 tsize_t
t2p_write_pdf_page_content(T2P
*, TIFF
*);
301 tsize_t
t2p_write_pdf_xobject_stream_dict(ttile_t
, T2P
*, TIFF
*);
302 tsize_t
t2p_write_pdf_xobject_cs(T2P
*, TIFF
*);
303 tsize_t
t2p_write_pdf_transfer(T2P
*, TIFF
*);
304 tsize_t
t2p_write_pdf_transfer_dict(T2P
*, TIFF
*, uint16
);
305 tsize_t
t2p_write_pdf_transfer_stream(T2P
*, TIFF
*, uint16
);
306 tsize_t
t2p_write_pdf_xobject_calcs(T2P
*, TIFF
*);
307 tsize_t
t2p_write_pdf_xobject_icccs(T2P
*, TIFF
*);
308 tsize_t
t2p_write_pdf_xobject_icccs_dict(T2P
*, TIFF
*);
309 tsize_t
t2p_write_pdf_xobject_icccs_stream(T2P
*, TIFF
*);
310 tsize_t
t2p_write_pdf_xobject_cs_stream(T2P
*, TIFF
*);
311 tsize_t
t2p_write_pdf_xobject_decode(T2P
*, TIFF
*);
312 tsize_t
t2p_write_pdf_xobject_stream_filter(ttile_t
, T2P
*, TIFF
*);
313 tsize_t
t2p_write_pdf_xreftable(T2P
*, TIFF
*);
314 tsize_t
t2p_write_pdf_trailer(T2P
*, TIFF
*);
318 This is the main function.
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
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.
331 The standard output is standard output. Set the output file name with the
332 "-o output.pdf" option.
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
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.
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.
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
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.
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.
373 The output PDF file conforms to the PDF 1.1 specification or PDF 1.2 if using
374 Zip/Deflate compression.
376 The Portable Document Format (PDF) specification is copyrighted by Adobe
377 Systems, Incorporated. Todos derechos reservados.
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.
383 usage: tiff2pdf [options] input.tif
386 -o: output to file name
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)
396 -u: set distance unit, 'i' for inch, 'm' for centimeter
397 -x: set x resolution default
398 -y: set y resolution default
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
416 tiff2pdf -o output.pdf input.tiff
418 The above example would generate the file output.pdf from input.tiff.
422 The above example would generate PDF output from input.tiff and write it
425 tiff2pdf -j -p letter -o output.pdf input.tiff
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
431 Please report bugs through:
433 http://bugzilla.remotesensing.org/buglist.cgi?product=libtiff
435 See also libtiff.3t, tiffcp.
438 int main(int argc
, char** argv
){
443 TIFF
*input
= NULL
, *output
= NULL
;
444 const char *outfilename
= NULL
;
453 "Can't initialize context");
457 while ((c
= getopt(argc
, argv
, "o:q:u:x:y:w:l:r:p:e:c:a:t:s:k:jzndifbh")) != -1){
460 outfilename
= optarg
;
464 t2p
->pdf_defaultcompression
=T2P_COMPRESS_JPEG
;
471 "JPEG support in libtiff required for JPEG compression, ignoring option");
476 t2p
->pdf_defaultcompression
=T2P_COMPRESS_ZIP
;
483 "Zip support in libtiff required for Zip compression, ignoring option");
487 t2p
->pdf_defaultcompressionquality
=atoi(optarg
);
490 t2p
->pdf_nopassthrough
=1;
493 t2p
->pdf_defaultcompression
=T2P_COMPRESS_NONE
;
497 t2p
->pdf_centimeters
=1;
501 t2p
->pdf_defaultxres
=
502 (float)atof(optarg
) / (t2p
->pdf_centimeters
?2.54F
:1.0F
);
505 t2p
->pdf_defaultyres
=
506 (float)atof(optarg
) / (t2p
->pdf_centimeters
?2.54F
:1.0F
);
509 t2p
->pdf_overridepagesize
=1;
510 t2p
->pdf_defaultpagewidth
=
511 ((float)atof(optarg
) * 72.0F
) / (t2p
->pdf_centimeters
?2.54F
:1.0F
);
514 t2p
->pdf_overridepagesize
=1;
515 t2p
->pdf_defaultpagelength
=
516 ((float)atof(optarg
) * 72.0F
) / (t2p
->pdf_centimeters
?2.54F
:1.0F
);
520 t2p
->pdf_overrideres
=1;
524 if(tiff2pdf_match_paper_size(
525 &(t2p
->pdf_defaultpagewidth
),
526 &(t2p
->pdf_defaultpagelength
),
528 t2p
->pdf_overridepagesize
=1;
530 TIFFWarning(TIFF2PDF_MODULE
,
531 "Unknown paper size %s, ignoring option",
536 t2p
->pdf_colorspace_invert
=1;
539 t2p
->pdf_fitwindow
=1;
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",
549 if(strlen(optarg
)==0){
550 t2p
->pdf_datetime
[0]=0;
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
);
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",
567 strcpy(t2p
->pdf_creator
, optarg
);
568 t2p
->pdf_creator
[strlen(optarg
)]=0;
572 (char *)_TIFFmalloc(strlen(optarg
) + 1);
573 if(t2p
->pdf_author
==NULL
){
576 "Can't allocate %u bytes of memory for main",
580 strcpy(t2p
->pdf_author
, optarg
);
581 t2p
->pdf_author
[strlen(optarg
)]=0;
584 t2p
->pdf_title
= (char*)_TIFFmalloc(strlen(optarg
)+1);
585 if(t2p
->pdf_title
==NULL
){
588 "Can't allocate %u bytes of memory for main",
592 strcpy(t2p
->pdf_title
, optarg
);
593 t2p
->pdf_title
[strlen(optarg
)]=0;
596 t2p
->pdf_subject
= (char*)_TIFFmalloc(strlen(optarg
)+1);
597 if(t2p
->pdf_subject
==NULL
){
600 "Can't allocate %u bytes of memory for main",
604 strcpy(t2p
->pdf_subject
, optarg
);
605 t2p
->pdf_subject
[strlen(optarg
)]=0;
608 t2p
->pdf_keywords
= (char*)_TIFFmalloc(strlen(optarg
)+1);
609 if(t2p
->pdf_keywords
==NULL
){
612 "Can't allocate %u bytes of memory for main",
616 strcpy(t2p
->pdf_keywords
, optarg
);
617 t2p
->pdf_keywords
[strlen(optarg
)]=0;
620 t2p
->pdf_image_interpolate
= 1;
633 input
= TIFFOpen(argv
[optind
++], "r");
637 "Can't open input file %s for reading",
644 "No input file specified");
652 "No support for multiple input files");
658 output
= TIFFOpen(outfilename
, "w");
660 TIFFError(TIFF2PDF_MODULE
,
661 "Can't open output file %s for writing",
665 if(output
->tif_seekproc
!= NULL
) {
666 TIFFSeekFile(output
, (toff_t
) 0, SEEK_SET
);
669 #if !defined(_WIN32) || defined(AVOID_WIN32_FILEIO)
670 output
= TIFFFdOpen((int)fileno(tmpfile()), "-", "w");
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(
683 FILE_FLAG_DELETE_ON_CLOSE
,
689 TIFFError(TIFF2PDF_MODULE
,
690 "Can't open temporary output file for writing to stdout",
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
);
702 CloseHandle((HANDLE
) output
->tif_fd
);
703 output
->tif_fd
=(int)GetStdHandle(STD_OUTPUT_HANDLE
);
705 output
->tif_clientdata
=(thandle_t
)output
->tif_fd
;
708 written
= t2p_write_pdf(t2p
, input
, output
);
710 if(t2p
->t2p_error
!= 0){
713 "An error occurred in converting TIFF %s to PDF %s",
730 return(EXIT_SUCCESS
);
741 return(EXIT_FAILURE
);
744 void tiff2pdf_usage(){
746 "usage: tiff2pdf [options] input.tiff",
748 " -o: output to file name",
750 " -j compress with JPEG",
753 " -z compress with Zip/Deflate",
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",
778 fprintf(stderr
, "%s\n\n", TIFFGetVersion());
779 for (i
=0;lines
[i
]!=NULL
;i
++){
780 fprintf(stderr
, "%s\n", lines
[i
]);
786 int tiff2pdf_match_paper_size(float* width
, float* length
, char* papersize
){
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",
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,
818 const int lengths
[]={
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,
832 len
=strlen(papersize
);
834 papersize
[i
]=toupper(papersize
[i
]);
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
];
848 This function allocates and initializes a T2P context struct pointer.
853 T2P
* t2p
= (T2P
*) _TIFFmalloc(sizeof(T2P
));
857 "Can't allocate %u bytes of memory for t2p_init",
859 return( (T2P
*) NULL
);
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 */
874 This function frees a T2P context struct pointer and any allocated data fields of it.
877 void t2p_free(T2P
* t2p
){
882 if(t2p
->pdf_xrefoffsets
!= NULL
){
883 _TIFFfree( (tdata_t
) t2p
->pdf_xrefoffsets
);
885 if(t2p
->tiff_pages
!= NULL
){
886 _TIFFfree( (tdata_t
) t2p
->tiff_pages
);
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
);
893 if(t2p
->tiff_tiles
!= NULL
){
894 _TIFFfree( (tdata_t
) t2p
->tiff_tiles
);
896 if(t2p
->pdf_palette
!= NULL
){
897 _TIFFfree( (tdata_t
) t2p
->pdf_palette
);
899 if(t2p
->pdf_fileid
!= NULL
){
900 _TIFFfree( (tdata_t
) t2p
->pdf_fileid
);
902 if(t2p
->pdf_datetime
!= NULL
){
903 _TIFFfree( (tdata_t
) t2p
->pdf_datetime
);
905 if(t2p
->pdf_creator
!= NULL
){
906 _TIFFfree( (tdata_t
) t2p
->pdf_creator
);
908 if(t2p
->pdf_author
!= NULL
){
909 _TIFFfree( (tdata_t
) t2p
->pdf_author
);
911 if(t2p
->pdf_title
!= NULL
){
912 _TIFFfree( (tdata_t
) t2p
->pdf_title
);
914 if(t2p
->pdf_subject
!= NULL
){
915 _TIFFfree( (tdata_t
) t2p
->pdf_subject
);
917 if(t2p
->pdf_keywords
!= NULL
){
918 _TIFFfree( (tdata_t
) t2p
->pdf_keywords
);
921 if(t2p
->pdf_ojpegdata
!= NULL
){
922 _TIFFfree( (tdata_t
) t2p
->pdf_ojpegdata
);
925 _TIFFfree( (tdata_t
) t2p
);
932 This function validates the values of a T2P context struct pointer
933 before calling t2p_write_pdf with it.
936 void t2p_validate(T2P
* t2p
){
939 if(t2p
->pdf_defaultcompression
==T2P_COMPRESS_JPEG
){
940 if(t2p
->pdf_defaultcompressionquality
<100 ||
941 t2p
->pdf_defaultcompressionquality
<1){
942 t2p
->pdf_defaultcompressionquality
=0;
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:
961 t2p
->pdf_defaultcompressionquality
=0;
963 if(t2p
->pdf_defaultcompressionquality%100
!=0){
966 "PNG Group predictor differencing not implemented, assuming compresion quality %u",
967 t2p
->pdf_defaultcompressionquality
);
969 t2p
->pdf_defaultcompressionquality
%=100;
970 if(t2p
->pdf_minorversion
<2){t2p
->pdf_minorversion
=2;}
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.
986 void t2p_read_tiff_init(T2P
* t2p
, TIFF
* input
){
988 tdir_t directorycount
=0;
994 directorycount
=TIFFNumberOfDirectories(input
);
995 t2p
->tiff_pages
= (T2P_PAGE
*) _TIFFmalloc(directorycount
* sizeof(T2P_PAGE
));
996 if(t2p
->tiff_pages
==NULL
){
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
;
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
){
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
;
1016 _TIFFmemset( t2p
->tiff_tiles
, 0x00, directorycount
* sizeof(T2P_TILES
));
1017 for(i
=0;i
<directorycount
;i
++){
1018 uint32 subfiletype
= 0;
1020 if(!TIFFSetDirectory(input
, i
)){
1023 "Can't set directory %u of input file %s",
1025 TIFFFileName(input
));
1028 if(TIFFGetField(input
, TIFFTAG_PAGENUMBER
, &pagen
, &paged
)){
1029 if((pagen
>paged
) && (paged
!= 0)){
1030 t2p
->tiff_pages
[t2p
->tiff_pagecount
].page_number
=
1033 t2p
->tiff_pages
[t2p
->tiff_pagecount
].page_number
=
1038 if(TIFFGetField(input
, TIFFTAG_SUBFILETYPE
, &subfiletype
)){
1039 if ( ((subfiletype
& FILETYPE_PAGE
) != 0)
1040 || (subfiletype
== 0)){
1046 if(TIFFGetField(input
, TIFFTAG_OSUBFILETYPE
, &subfiletype
)){
1047 if ((subfiletype
== OFILETYPE_IMAGE
)
1048 || (subfiletype
== OFILETYPE_PAGE
)
1049 || (subfiletype
== 0) ){
1056 t2p
->tiff_pages
[t2p
->tiff_pagecount
].page_number
=t2p
->tiff_pagecount
;
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
);
1063 t2p
->tiff_pagecount
++;
1068 qsort((void*) t2p
->tiff_pages
, t2p
->tiff_pagecount
,
1069 sizeof(T2P_PAGE
), t2p_cmp_t2p_page
);
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
++;
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;}
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;
1101 t2p
->tiff_transferfunctioncount
= 1;
1102 t2p
->tiff_pages
[i
].page_extra
+= 2;
1103 t2p
->pdf_xrefcount
+= 2;
1105 if(t2p
->pdf_minorversion
< 2)
1106 t2p
->pdf_minorversion
= 2;
1108 t2p
->tiff_transferfunctioncount
=0;
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;}
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
;
1126 if( t2p
->tiff_tiles
[i
].tiles_tilecount
> 0){
1127 t2p
->pdf_xrefcount
+=
1128 (t2p
->tiff_tiles
[i
].tiles_tilecount
-1)*2;
1131 &( t2p
->tiff_tiles
[i
].tiles_tilewidth
) );
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
){
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
;
1155 This function is used by qsort to sort a T2P_PAGE* array of page structures by page number.
1158 int t2p_cmp_t2p_page(const void* e1
, const void* e2
){
1160 return( ((T2P_PAGE
*)e1
)->page_number
- ((T2P_PAGE
*)e2
)->page_number
);
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.
1171 It determines if the image can be converted as raw data without
1172 requiring transcoding of the image data.
1175 void t2p_read_tiff_data(T2P
* t2p
, TIFF
* input
){
1186 t2p
->pdf_transcode
= T2P_TRANSCODE_ENCODE
;
1187 t2p
->pdf_sample
= T2P_SAMPLE_NOTHING
;
1188 t2p
->pdf_switchdecode
= t2p
->pdf_colorspace_invert
;
1191 TIFFSetDirectory(input
, t2p
->tiff_pages
[t2p
->pdf_page
].page_directory
);
1193 TIFFGetField(input
, TIFFTAG_IMAGEWIDTH
, &(t2p
->tiff_width
));
1194 if(t2p
->tiff_width
== 0){
1197 "No support for %s with zero width",
1198 TIFFFileName(input
) );
1199 t2p
->t2p_error
= T2P_ERR_ERROR
;
1203 TIFFGetField(input
, TIFFTAG_IMAGELENGTH
, &(t2p
->tiff_length
));
1204 if(t2p
->tiff_length
== 0){
1207 "No support for %s with zero length",
1208 TIFFFileName(input
) );
1209 t2p
->t2p_error
= T2P_ERR_ERROR
;
1213 if(TIFFGetField(input
, TIFFTAG_COMPRESSION
, &(t2p
->tiff_compression
)) == 0){
1216 "No support for %s with no compression tag",
1217 TIFFFileName(input
) );
1218 t2p
->t2p_error
= T2P_ERR_ERROR
;
1222 if( TIFFIsCODECConfigured(t2p
->tiff_compression
) == 0){
1225 "No support for %s with compression type %u: not configured",
1226 TIFFFileName(input
),
1227 t2p
->tiff_compression
1229 t2p
->t2p_error
= T2P_ERR_ERROR
;
1234 TIFFGetFieldDefaulted(input
, TIFFTAG_BITSPERSAMPLE
, &(t2p
->tiff_bitspersample
));
1235 switch(t2p
->tiff_bitspersample
){
1244 "Image %s has 0 bits per sample, assuming 1",
1245 TIFFFileName(input
));
1246 t2p
->tiff_bitspersample
=1;
1251 "No support for %s with %u bits per sample",
1252 TIFFFileName(input
),
1253 t2p
->tiff_bitspersample
);
1254 t2p
->t2p_error
= T2P_ERR_ERROR
;
1258 TIFFGetFieldDefaulted(input
, TIFFTAG_SAMPLESPERPIXEL
, &(t2p
->tiff_samplesperpixel
));
1259 if(t2p
->tiff_samplesperpixel
>4){
1262 "No support for %s with %u samples per pixel",
1263 TIFFFileName(input
),
1264 t2p
->tiff_samplesperpixel
);
1265 t2p
->t2p_error
= T2P_ERR_ERROR
;
1268 if(t2p
->tiff_samplesperpixel
==0){
1271 "Image %s has 0 samples per pixel, assuming 1",
1272 TIFFFileName(input
));
1273 t2p
->tiff_samplesperpixel
=1;
1276 if(TIFFGetField(input
, TIFFTAG_SAMPLEFORMAT
, &xuint16
) != 0 ){
1285 "No support for %s with sample format %u",
1286 TIFFFileName(input
),
1288 t2p
->t2p_error
= T2P_ERR_ERROR
;
1294 TIFFGetFieldDefaulted(input
, TIFFTAG_FILLORDER
, &(t2p
->tiff_fillorder
));
1296 if(TIFFGetField(input
, TIFFTAG_PHOTOMETRIC
, &(t2p
->tiff_photometric
)) == 0){
1299 "No support for %s with no photometric interpretation tag",
1300 TIFFFileName(input
) );
1301 t2p
->t2p_error
= T2P_ERR_ERROR
;
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;
1315 t2p
->pdf_colorspace
=T2P_CS_GRAY
;
1316 if(t2p
->tiff_photometric
==PHOTOMETRIC_MINISWHITE
){
1317 t2p
->pdf_switchdecode
^= 1;
1321 case PHOTOMETRIC_RGB
:
1322 t2p
->pdf_colorspace
=T2P_CS_RGB
;
1323 if(t2p
->tiff_samplesperpixel
== 3){
1326 if(TIFFGetField(input
, TIFFTAG_INDEXED
, &xuint16
)){
1328 goto photometric_palette
;
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
)
1337 if(xuint16p
[0] == EXTRASAMPLE_ASSOCALPHA
){
1338 t2p
->pdf_sample
=T2P_SAMPLE_RGBAA_TO_RGB
;
1341 if(xuint16p
[0] == EXTRASAMPLE_UNASSALPHA
){
1342 t2p
->pdf_sample
=T2P_SAMPLE_RGBA_TO_RGB
;
1347 "RGB image %s has 4 samples per pixel, assuming RGBA",
1348 TIFFFileName(input
));
1351 t2p
->pdf_colorspace
=T2P_CS_CMYK
;
1352 t2p
->pdf_switchdecode
^= 1;
1355 "RGB image %s has 4 samples per pixel, assuming inverse CMYK",
1356 TIFFFileName(input
));
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
;
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
;
1376 case PHOTOMETRIC_PALETTE
:
1377 photometric_palette
:
1378 if(t2p
->tiff_samplesperpixel
!=1){
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
;
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
)){
1392 "Palettized image %s has no color map",
1393 TIFFFileName(input
));
1394 t2p
->t2p_error
= T2P_ERR_ERROR
;
1397 if(t2p
->pdf_palette
!= NULL
){
1398 _TIFFfree(t2p
->pdf_palette
);
1399 t2p
->pdf_palette
=NULL
;
1401 t2p
->pdf_palette
= (unsigned char*)
1402 _TIFFmalloc(t2p
->pdf_palettesize
*3);
1403 if(t2p
->pdf_palette
==NULL
){
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
;
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);
1417 t2p
->pdf_palettesize
*= 3;
1419 case PHOTOMETRIC_SEPARATED
:
1420 if(TIFFGetField(input
, TIFFTAG_INDEXED
, &xuint16
)){
1422 goto photometric_palette_cmyk
;
1425 if( TIFFGetField(input
, TIFFTAG_INKSET
, &xuint16
) ){
1426 if(xuint16
!= INKSET_CMYK
){
1429 "No support for %s because its inkset is not CMYK",
1430 TIFFFileName(input
) );
1431 t2p
->t2p_error
= T2P_ERR_ERROR
;
1435 if(t2p
->tiff_samplesperpixel
==4){
1436 t2p
->pdf_colorspace
=T2P_CS_CMYK
;
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
;
1447 photometric_palette_cmyk
:
1448 if(t2p
->tiff_samplesperpixel
!=1){
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
;
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
)){
1462 "Palettized image %s has no color map",
1463 TIFFFileName(input
));
1464 t2p
->t2p_error
= T2P_ERR_ERROR
;
1467 if(t2p
->pdf_palette
!= NULL
){
1468 _TIFFfree(t2p
->pdf_palette
);
1469 t2p
->pdf_palette
=NULL
;
1471 t2p
->pdf_palette
= (unsigned char*)
1472 _TIFFmalloc(t2p
->pdf_palettesize
*4);
1473 if(t2p
->pdf_palette
==NULL
){
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
;
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);
1488 t2p
->pdf_palettesize
*= 4;
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
;
1497 t2p
->pdf_sample
=T2P_SAMPLE_YCBCR_TO_RGB
;
1499 if(t2p
->pdf_defaultcompression
==T2P_COMPRESS_JPEG
){
1500 t2p
->pdf_sample
=T2P_SAMPLE_NOTHING
;
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
;
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
;
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
;
1527 case PHOTOMETRIC_LOGL
:
1528 case PHOTOMETRIC_LOGLUV
:
1531 "No support for %s with photometric interpretation LogL/LogLuv",
1532 TIFFFileName(input
),
1533 t2p
->tiff_photometric
);
1534 t2p
->t2p_error
= T2P_ERR_ERROR
;
1539 "No support for %s with photometric interpretation %u",
1540 TIFFFileName(input
),
1541 t2p
->tiff_photometric
);
1542 t2p
->t2p_error
= T2P_ERR_ERROR
;
1546 if(TIFFGetField(input
, TIFFTAG_PLANARCONFIG
, &(t2p
->tiff_planar
))){
1547 switch(t2p
->tiff_planar
){
1551 "Image %s has planar configuration 0, assuming 1",
1552 TIFFFileName(input
));
1553 t2p
->tiff_planar
=PLANARCONFIG_CONTIG
;
1554 case PLANARCONFIG_CONTIG
:
1556 case PLANARCONFIG_SEPARATE
:
1557 t2p
->pdf_sample
=T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG
;
1558 if(t2p
->tiff_bitspersample
!=8){
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
;
1571 "No support for %s with planar configuration %u",
1572 TIFFFileName(input
),
1574 t2p
->t2p_error
= T2P_ERR_ERROR
;
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;
1588 if(TIFFGetField(input
, TIFFTAG_XRESOLUTION
, &(t2p
->tiff_xres
) ) == 0){
1591 if(TIFFGetField(input
, TIFFTAG_YRESOLUTION
, &(t2p
->tiff_yres
) ) == 0){
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
;
1603 t2p_compose_pdf_page(t2p
);
1605 t2p
->pdf_transcode
= T2P_TRANSCODE_ENCODE
;
1606 if(t2p
->pdf_nopassthrough
==0){
1607 #ifdef CCITT_SUPPORT
1608 if(t2p
->tiff_compression
==COMPRESSION_CCITTFAX4
1610 if(TIFFIsTiled(input
) || (TIFFNumberOfStrips(input
)==1) ){
1611 t2p
->pdf_transcode
= T2P_TRANSCODE_RAW
;
1612 t2p
->pdf_compression
=T2P_COMPRESS_G4
;
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
;
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
);
1633 if(t2p
->tiff_compression
==COMPRESSION_JPEG
){
1634 t2p
->pdf_transcode
= T2P_TRANSCODE_RAW
;
1635 t2p
->pdf_compression
=T2P_COMPRESS_JPEG
;
1641 if(t2p
->pdf_transcode
!=T2P_TRANSCODE_RAW
){
1642 t2p
->pdf_compression
= t2p
->pdf_defaultcompression
;
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
--;
1653 if(t2p
->tiff_compression
==COMPRESSION_JPEG
){
1654 if(t2p
->tiff_planar
==PLANARCONFIG_SEPARATE
){
1657 "No support for %s with JPEG compression and separated planar configuration",
1658 TIFFFileName(input
));
1659 t2p
->t2p_error
=T2P_ERR_ERROR
;
1664 #ifdef OJPEG_SUPPORT
1665 if(t2p
->tiff_compression
==COMPRESSION_OJPEG
){
1666 if(t2p
->tiff_planar
==PLANARCONFIG_SEPARATE
){
1669 "No support for %s with OJPEG compression and separated planar configuration",
1670 TIFFFileName(input
));
1671 t2p
->t2p_error
=T2P_ERR_ERROR
;
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
;
1682 t2p
->tiff_samplesperpixel
=3;
1683 t2p
->tiff_photometric
=PHOTOMETRIC_RGB
;
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;
1695 t2p
->tiff_transferfunctioncount
=1;
1698 t2p
->tiff_transferfunctioncount
=0;
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
;
1706 if(t2p
->pdf_colorspace
& T2P_CS_RGB
){
1707 t2p
->pdf_colorspace
|= T2P_CS_CALRGB
;
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
;
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];
1726 t2p
->tiff_whitechromaticities
[0]=0.3457F
; /* 0.3127F; */
1727 t2p
->tiff_whitechromaticities
[1]=0.3585F
; /* 0.3290F; */
1730 if(TIFFGetField(input
,
1732 &(t2p
->tiff_iccprofilelength
),
1733 &(t2p
->tiff_iccprofile
))!=0){
1734 t2p
->pdf_colorspace
|= T2P_CS_ICCBASED
;
1736 t2p
->tiff_iccprofilelength
=0;
1737 t2p
->tiff_iccprofile
=NULL
;
1740 #ifdef CCITT_SUPPORT
1741 if( t2p
->tiff_bitspersample
==1 &&
1742 t2p
->tiff_samplesperpixel
==1){
1743 t2p
->pdf_compression
= T2P_COMPRESS_G4
;
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.
1756 void t2p_read_tiff_size(T2P
* t2p
, TIFF
* input
){
1759 #if defined(JPEG_SUPPORT) || defined (OJPEG_SUPPORT)
1760 unsigned char* jpt
=NULL
;
1763 tstrip_t stripcount
=0;
1765 #ifdef OJPEG_SUPPORT
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];
1778 if(t2p
->pdf_compression
== T2P_COMPRESS_ZIP
){
1779 TIFFGetField(input
, TIFFTAG_STRIPBYTECOUNTS
, &sbc
);
1780 t2p
->tiff_datasize
=sbc
[0];
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
;
1793 stripcount
=TIFFNumberOfStrips(input
);
1794 for(i
=0;i
<stripcount
;i
++){
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
));
1812 TIFFError(TIFF2PDF_MODULE
,
1813 "Input file %s missing field: TIFFTAG_JPEGIFBYTECOUNT",
1814 TIFFFileName(input
));
1815 t2p
->t2p_error
= T2P_ERR_ERROR
;
1820 t2p
->tiff_datasize
+=k
;
1821 t2p
->tiff_datasize
+=2*stripcount
;
1822 t2p
->tiff_datasize
+=2048;
1827 if(t2p
->tiff_compression
== COMPRESSION_JPEG
){
1828 if(TIFFGetField(input
, TIFFTAG_JPEGTABLES
, &xuint16
, &jpt
) != 0 ){
1830 t2p
->tiff_datasize
+= xuint16
;
1831 t2p
->tiff_datasize
-=2; /* don't use EOI of header */
1834 t2p
->tiff_datasize
=2; /* SOI for first strip */
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
;
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 */
1848 t2p
->tiff_datasize
+=2; /* use EOI of last strip */
1853 t2p
->tiff_datasize
=TIFFScanlineSize(input
) * t2p
->tiff_length
;
1854 if(t2p
->tiff_planar
==PLANARCONFIG_SEPARATE
){
1855 t2p
->tiff_datasize
*= t2p
->tiff_samplesperpixel
;
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.
1866 void t2p_read_tiff_size_tile(T2P
* t2p
, TIFF
* input
, ttile_t tile
){
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
);
1878 if(t2p
->pdf_transcode
==T2P_TRANSCODE_RAW
){
1880 #if defined(JPEG_SUPPORT) || defined(OJPEG_SUPPORT)
1881 && !(t2p
->pdf_compression
==T2P_COMPRESS_JPEG
)
1884 t2p
->tiff_datasize
=TIFFTileSize(input
);
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;
1896 if(t2p
->tiff_compression
==COMPRESSION_JPEG
){
1897 if(TIFFGetField(input
, TIFFTAG_JPEGTABLES
, &xuint16
, &jpt
)!=0){
1899 t2p
->tiff_datasize
+=xuint16
;
1900 t2p
->tiff_datasize
-=4; /* don't use EOI of header or SOI of tile */
1908 t2p
->tiff_datasize
=TIFFTileSize(input
);
1909 if(t2p
->tiff_planar
==PLANARCONFIG_SEPARATE
){
1910 t2p
->tiff_datasize
*= t2p
->tiff_samplesperpixel
;
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.
1921 int t2p_tile_is_right_edge(T2P_TILES tiles
, ttile_t tile
){
1923 if( ((tile
+1) % tiles
.tiles_tilecountx
== 0)
1924 && (tiles
.tiles_edgetilewidth
!= 0) ){
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.
1939 int t2p_tile_is_bottom_edge(T2P_TILES tiles
, ttile_t tile
){
1941 if( ((tile
+1) > (tiles
.tiles_tilecount
-tiles
.tiles_tilecountx
) )
1942 && (tiles
.tiles_edgetilelength
!= 0) ){
1952 This function returns a non-zero value when the tile is a right edge tile or a bottom
1956 int t2p_tile_is_edge(T2P_TILES tiles
, ttile_t tile
){
1958 return(t2p_tile_is_right_edge(tiles
, tile
) | t2p_tile_is_bottom_edge(tiles
, tile
) );
1962 This function returns a non-zero value when the tile is a right edge tile and a bottom
1966 int t2p_tile_is_corner_edge(T2P_TILES tiles
, ttile_t tile
){
1968 return(t2p_tile_is_right_edge(tiles
, tile
) & t2p_tile_is_bottom_edge(tiles
, tile
) );
1972 This function is an empty (dummy) TIFFReadWriteProc that returns the amount
1973 requested to be read without reading anything.
1976 tsize_t
t2p_empty_readproc(thandle_t fd
, tdata_t buf
, tsize_t size
){
1978 (void) fd
; (void) buf
; (void) size
;
1984 This function is an empty (dummy) TIFFReadWriteProc that returns the amount
1985 requested to be written without writing anything.
1988 tsize_t
t2p_empty_writeproc(thandle_t fd
, tdata_t buf
, tsize_t size
){
1990 (void) fd
; (void) buf
; (void) size
;
1996 This function is an empty (dummy) TIFFSeekProc that returns off.
1999 toff_t
t2p_empty_seekproc(thandle_t fd
, toff_t off
, int whence
){
2001 (void) fd
; (void) off
; (void) whence
;
2007 This function is an empty (dummy) TIFFCloseProc that returns 0.
2010 int t2p_empty_closeproc(thandle_t fd
){
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
2024 tsize_t
t2p_readwrite_pdf_image(T2P
* t2p
, TIFF
* input
, TIFF
* output
){
2027 unsigned char* buffer
=NULL
;
2028 unsigned char* samplebuffer
=NULL
;
2029 tsize_t bufferoffset
=0;
2030 tsize_t samplebufferoffset
=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;
2051 unsigned char* stripbuffer
;
2052 tsize_t striplength
=0;
2053 uint32 max_striplength
=0;
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
);
2061 TIFFError(TIFF2PDF_MODULE
,
2062 "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s",
2064 TIFFFileName(input
));
2065 t2p
->t2p_error
= T2P_ERR_ERROR
;
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
);
2073 TIFFWriteFile(output
, (tdata_t
) buffer
, t2p
->tiff_datasize
);
2075 return(t2p
->tiff_datasize
);
2079 if(t2p
->pdf_compression
== T2P_COMPRESS_ZIP
){
2080 buffer
= (unsigned char*) _TIFFmalloc(t2p
->tiff_datasize
);
2081 memset(buffer
, 0, t2p
->tiff_datasize
);
2083 TIFFError(TIFF2PDF_MODULE
,
2084 "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s",
2086 TIFFFileName(input
));
2087 t2p
->t2p_error
= T2P_ERR_ERROR
;
2090 TIFFReadRawStrip(input
, 0, (tdata_t
) buffer
, t2p
->tiff_datasize
);
2091 if (t2p
->tiff_fillorder
==FILLORDER_LSB2MSB
){
2092 TIFFReverseBits(buffer
, t2p
->tiff_datasize
);
2094 TIFFWriteFile(output
, (tdata_t
) buffer
, t2p
->tiff_datasize
);
2096 return(t2p
->tiff_datasize
);
2099 #ifdef OJPEG_SUPPORT
2100 if(t2p
->tiff_compression
== COMPRESSION_OJPEG
){
2102 if(t2p
->tiff_dataoffset
!= 0){
2103 buffer
= (unsigned char*) _TIFFmalloc(t2p
->tiff_datasize
);
2104 memset(buffer
, 0, t2p
->tiff_datasize
);
2106 TIFFError(TIFF2PDF_MODULE
,
2107 "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s",
2109 TIFFFileName(input
));
2110 t2p
->t2p_error
= T2P_ERR_ERROR
;
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
);
2120 return(t2p
->tiff_datasize
);
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;
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
++){
2142 buffer
[bufferoffset
++]=0xff;
2143 buffer
[bufferoffset
++]=(0xd0 | ((i
-1)%8
));
2145 bufferoffset
+=TIFFReadRawStrip(input
,
2147 (tdata_t
) &(((unsigned char*)buffer
)[bufferoffset
]),
2150 TIFFWriteFile(output
, (tdata_t
) buffer
, bufferoffset
);
2152 return(bufferoffset
);
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
;
2162 buffer
=(unsigned char*) _TIFFmalloc(t2p
->tiff_datasize
);
2163 memset(buffer
, 0, t2p
->tiff_datasize
);
2165 TIFFError(TIFF2PDF_MODULE
,
2166 "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s",
2168 TIFFFileName(input
));
2169 t2p
->t2p_error
= T2P_ERR_ERROR
;
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
++){
2177 buffer
[bufferoffset
++]=0xff;
2178 buffer
[bufferoffset
++]=(0xd0 | ((i
-1)%8
));
2180 bufferoffset
+=TIFFReadRawStrip(input
,
2182 (tdata_t
) &(((unsigned char*)buffer
)[bufferoffset
]),
2185 if( ! ( (buffer
[bufferoffset
-1]==0xd9) && (buffer
[bufferoffset
-2]==0xff) ) ){
2186 buffer
[bufferoffset
++]=0xff;
2187 buffer
[bufferoffset
++]=0xd9;
2189 TIFFWriteFile(output
, (tdata_t
) buffer
, bufferoffset
);
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
;
2198 return(t2p
->tiff_datasize
);
2202 if(t2p
->tiff_compression
== COMPRESSION_JPEG
){
2203 buffer
= (unsigned char*) _TIFFmalloc(t2p
->tiff_datasize
);
2204 memset(buffer
, 0, t2p
->tiff_datasize
);
2206 TIFFError(TIFF2PDF_MODULE
,
2207 "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s",
2209 TIFFFileName(input
));
2210 t2p
->t2p_error
= T2P_ERR_ERROR
;
2213 if(TIFFGetField(input
, TIFFTAG_JPEGTABLES
, &xuint16_1
, &jpt
) != 0){
2215 _TIFFmemcpy(buffer
, jpt
, xuint16_1
);
2216 bufferoffset
+=xuint16_1
-2;
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
];
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",
2229 TIFFFileName(input
));
2231 t2p
->t2p_error
= T2P_ERR_ERROR
;
2234 for(i
=0;i
<stripcount
;i
++){
2235 striplength
=TIFFReadRawStrip(input
, i
, (tdata_t
) stripbuffer
, -1);
2236 if(!t2p_process_jpeg_strip(
2243 TIFFError(TIFF2PDF_MODULE
,
2244 "Can't process JPEG data in input file %s",
2245 TIFFFileName(input
));
2246 _TIFFfree(samplebuffer
);
2248 t2p
->t2p_error
= T2P_ERR_ERROR
;
2252 buffer
[bufferoffset
++]=0xff;
2253 buffer
[bufferoffset
++]=0xd9;
2254 TIFFWriteFile(output
, (tdata_t
) buffer
, bufferoffset
);
2255 _TIFFfree(stripbuffer
);
2257 return(bufferoffset
);
2263 if(t2p
->pdf_sample
==T2P_SAMPLE_NOTHING
){
2264 buffer
= (unsigned char*) _TIFFmalloc(t2p
->tiff_datasize
);
2265 memset(buffer
, 0, t2p
->tiff_datasize
);
2267 TIFFError(TIFF2PDF_MODULE
,
2268 "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s",
2270 TIFFFileName(input
));
2271 t2p
->t2p_error
= T2P_ERR_ERROR
;
2274 stripsize
=TIFFStripSize(input
);
2275 stripcount
=TIFFNumberOfStrips(input
);
2276 for(i
=0;i
<stripcount
;i
++){
2278 TIFFReadEncodedStrip(input
,
2280 (tdata_t
) &buffer
[bufferoffset
],
2283 TIFFError(TIFF2PDF_MODULE
,
2284 "Error on decoding strip %u of %s",
2286 TIFFFileName(input
));
2288 t2p
->t2p_error
=T2P_ERR_ERROR
;
2294 if(t2p
->pdf_sample
& T2P_SAMPLE_PLANAR_SEPARATE_TO_CONTIG
){
2296 sepstripsize
=TIFFStripSize(input
);
2297 sepstripcount
=TIFFNumberOfStrips(input
);
2299 stripsize
=sepstripsize
*t2p
->tiff_samplesperpixel
;
2300 stripcount
=sepstripcount
/t2p
->tiff_samplesperpixel
;
2302 buffer
= (unsigned char*) _TIFFmalloc(t2p
->tiff_datasize
);
2303 memset(buffer
, 0, t2p
->tiff_datasize
);
2305 TIFFError(TIFF2PDF_MODULE
,
2306 "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s",
2308 TIFFFileName(input
));
2309 t2p
->t2p_error
= T2P_ERR_ERROR
;
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",
2317 TIFFFileName(input
));
2318 t2p
->t2p_error
= T2P_ERR_ERROR
;
2321 for(i
=0;i
<stripcount
;i
++){
2322 samplebufferoffset
=0;
2323 for(j
=0;j
<t2p
->tiff_samplesperpixel
;j
++){
2325 TIFFReadEncodedStrip(input
,
2327 (tdata_t
) &(samplebuffer
[samplebufferoffset
]),
2330 TIFFError(TIFF2PDF_MODULE
,
2331 "Error on decoding strip %u of %s",
2333 TIFFFileName(input
));
2335 t2p
->t2p_error
=T2P_ERR_ERROR
;
2338 samplebufferoffset
+=read
;
2340 t2p_sample_planar_separate_to_contig(
2342 &(buffer
[bufferoffset
]),
2344 samplebufferoffset
);
2345 bufferoffset
+=samplebufferoffset
;
2347 _TIFFfree(samplebuffer
);
2351 buffer
= (unsigned char*) _TIFFmalloc(t2p
->tiff_datasize
);
2352 memset(buffer
, 0, t2p
->tiff_datasize
);
2354 TIFFError(TIFF2PDF_MODULE
,
2355 "Can't allocate %u bytes of memory for t2p_readwrite_pdf_image, %s",
2357 TIFFFileName(input
));
2358 t2p
->t2p_error
= T2P_ERR_ERROR
;
2361 stripsize
=TIFFStripSize(input
);
2362 stripcount
=TIFFNumberOfStrips(input
);
2363 for(i
=0;i
<stripcount
;i
++){
2365 TIFFReadEncodedStrip(input
,
2367 (tdata_t
) &buffer
[bufferoffset
],
2370 TIFFError(TIFF2PDF_MODULE
,
2371 "Error on decoding strip %u of %s",
2373 TIFFFileName(input
));
2374 _TIFFfree(samplebuffer
);
2376 t2p
->t2p_error
=T2P_ERR_ERROR
;
2382 if(t2p
->pdf_sample
& T2P_SAMPLE_REALIZE_PALETTE
){
2383 samplebuffer
=(unsigned char*)_TIFFrealloc(
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",
2390 TIFFFileName(input
));
2391 t2p
->t2p_error
= T2P_ERR_ERROR
;
2394 buffer
=samplebuffer
;
2395 t2p
->tiff_datasize
*= t2p
->tiff_samplesperpixel
;
2397 t2p_sample_realize_palette(t2p
, buffer
);
2400 if(t2p
->pdf_sample
& T2P_SAMPLE_RGBA_TO_RGB
){
2401 t2p
->tiff_datasize
=t2p_sample_rgba_to_rgb(
2403 t2p
->tiff_width
*t2p
->tiff_length
);
2406 if(t2p
->pdf_sample
& T2P_SAMPLE_RGBAA_TO_RGB
){
2407 t2p
->tiff_datasize
=t2p_sample_rgbaa_to_rgb(
2409 t2p
->tiff_width
*t2p
->tiff_length
);
2412 if(t2p
->pdf_sample
& T2P_SAMPLE_YCBCR_TO_RGB
){
2413 samplebuffer
=(unsigned char*)_TIFFrealloc(
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",
2420 TIFFFileName(input
));
2421 t2p
->t2p_error
= T2P_ERR_ERROR
;
2425 buffer
=samplebuffer
;
2427 if(!TIFFReadRGBAImageOriented(
2432 ORIENTATION_TOPLEFT
,
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
;
2440 t2p
->tiff_datasize
=t2p_sample_abgr_to_rgb(
2442 t2p
->tiff_width
*t2p
->tiff_length
);
2446 if(t2p
->pdf_sample
& T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED
){
2447 t2p
->tiff_datasize
=t2p_sample_lab_signed_to_unsigned(
2449 t2p
->tiff_width
*t2p
->tiff_length
);
2455 t2p
->tiff_writeproc
=output
->tif_writeproc
;
2456 output
->tif_writeproc
=t2p_empty_writeproc
;
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
);
2467 switch(t2p
->pdf_compression
){
2468 case T2P_COMPRESS_NONE
:
2469 TIFFSetField(output
, TIFFTAG_COMPRESSION
, COMPRESSION_NONE
);
2471 #ifdef CCITT_SUPPORT
2472 case T2P_COMPRESS_G4
:
2473 TIFFSetField(output
, TIFFTAG_COMPRESSION
, COMPRESSION_CCITTFAX4
);
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
);
2484 if(TIFFGetField(input
, TIFFTAG_REFERENCEBLACKWHITE
, &xfloatp
)!=0){
2485 TIFFSetField(output
, TIFFTAG_REFERENCEBLACKWHITE
, xfloatp
);
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
));
2494 t2p
->t2p_error
= T2P_ERR_ERROR
;
2497 TIFFSetField(output
, TIFFTAG_JPEGTABLESMODE
, 0);
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
);
2504 TIFFSetField(output
, TIFFTAG_JPEGCOLORMODE
, JPEGCOLORMODE_RAW
);
2507 if(t2p
->pdf_colorspace
& T2P_CS_GRAY
){
2510 if(t2p
->pdf_colorspace
& T2P_CS_CMYK
){
2513 if(t2p
->pdf_defaultcompressionquality
!= 0){
2514 TIFFSetField(output
,
2515 TIFFTAG_JPEGQUALITY
,
2516 t2p
->pdf_defaultcompressionquality
);
2522 case T2P_COMPRESS_ZIP
:
2523 TIFFSetField(output
, TIFFTAG_COMPRESSION
, COMPRESSION_DEFLATE
);
2524 if(t2p
->pdf_defaultcompressionquality%100
!= 0){
2525 TIFFSetField(output
,
2527 t2p
->pdf_defaultcompressionquality
% 100);
2529 if(t2p
->pdf_defaultcompressionquality
/100 != 0){
2530 TIFFSetField(output
,
2532 (t2p
->pdf_defaultcompressionquality
/ 100));
2540 output
->tif_writeproc
=t2p
->tiff_writeproc
;
2542 if(t2p
->pdf_compression
==T2P_COMPRESS_JPEG
&& t2p
->tiff_photometric
==PHOTOMETRIC_YCBCR
){
2543 bufferoffset
=TIFFWriteEncodedStrip(output
, (tstrip_t
)0, buffer
,stripsize
*stripcount
);
2546 bufferoffset
=TIFFWriteEncodedStrip(output
, (tstrip_t
)0, buffer
, t2p
->tiff_datasize
);
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
;
2560 written
= output
->tif_dir
.td_stripbytecount
[0];
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.
2571 tsize_t
t2p_readwrite_pdf_image_tile(T2P
* t2p
, TIFF
* input
, TIFF
* output
, ttile_t tile
){
2575 unsigned char* buffer
=NULL
;
2576 tsize_t bufferoffset
=0;
2577 unsigned char* samplebuffer
=NULL
;
2578 tsize_t samplebufferoffset
=0;
2581 ttile_t tilecount
=0;
2583 ttile_t septilecount
=0;
2584 tsize_t septilesize
=0;
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
);
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
)
2602 #ifdef CCITT_SUPPORT
2603 if(t2p
->pdf_compression
== T2P_COMPRESS_G4
){
2604 buffer
= (unsigned char*) _TIFFmalloc(t2p
->tiff_datasize
);
2606 TIFFError(TIFF2PDF_MODULE
,
2607 "Can't allocate %u bytes of memory "
2608 "for t2p_readwrite_pdf_image_tile, %s",
2610 TIFFFileName(input
));
2611 t2p
->t2p_error
= T2P_ERR_ERROR
;
2614 TIFFReadRawTile(input
, tile
, (tdata_t
) buffer
, t2p
->tiff_datasize
);
2615 if (t2p
->tiff_fillorder
==FILLORDER_LSB2MSB
){
2616 TIFFReverseBits(buffer
, t2p
->tiff_datasize
);
2618 TIFFWriteFile(output
, (tdata_t
) buffer
, t2p
->tiff_datasize
);
2620 return(t2p
->tiff_datasize
);
2624 if(t2p
->pdf_compression
== T2P_COMPRESS_ZIP
){
2625 buffer
= (unsigned char*) _TIFFmalloc(t2p
->tiff_datasize
);
2627 TIFFError(TIFF2PDF_MODULE
,
2628 "Can't allocate %u bytes of memory "
2629 "for t2p_readwrite_pdf_image_tile, %s",
2631 TIFFFileName(input
));
2632 t2p
->t2p_error
= T2P_ERR_ERROR
;
2635 TIFFReadRawTile(input
, tile
, (tdata_t
) buffer
, t2p
->tiff_datasize
);
2636 if (t2p
->tiff_fillorder
==FILLORDER_LSB2MSB
){
2637 TIFFReverseBits(buffer
, t2p
->tiff_datasize
);
2639 TIFFWriteFile(output
, (tdata_t
) buffer
, t2p
->tiff_datasize
);
2641 return(t2p
->tiff_datasize
);
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 "
2650 TIFFFileName(input
));
2651 t2p
->t2p_error
= T2P_ERR_ERROR
;
2654 buffer
=(unsigned char*) _TIFFmalloc(t2p
->tiff_datasize
);
2656 TIFFError(TIFF2PDF_MODULE
,
2657 "Can't allocate %u bytes of memory "
2658 "for t2p_readwrite_pdf_image, %s",
2660 TIFFFileName(input
));
2661 t2p
->t2p_error
= T2P_ERR_ERROR
;
2664 _TIFFmemcpy(buffer
, t2p
->pdf_ojpegdata
, t2p
->pdf_ojpegdatalength
);
2666 if(t2p_tile_is_bottom_edge(t2p
->tiff_tiles
[t2p
->pdf_page
], tile
)){
2668 (t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_edgetilelength
>> 8) & 0xff;
2670 (t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_edgetilelength
) & 0xff;
2672 if(t2p_tile_is_right_edge(t2p
->tiff_tiles
[t2p
->pdf_page
], tile
)){
2674 (t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_edgetilewidth
>> 8) & 0xff;
2676 (t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_edgetilewidth
) & 0xff;
2679 bufferoffset
=t2p
->pdf_ojpegdatalength
;
2680 bufferoffset
+=TIFFReadRawTile(input
,
2682 (tdata_t
) &(((unsigned char*)buffer
)[bufferoffset
]),
2684 ((unsigned char*)buffer
)[bufferoffset
++]=0xff;
2685 ((unsigned char*)buffer
)[bufferoffset
++]=0xd9;
2686 TIFFWriteFile(output
, (tdata_t
) buffer
, bufferoffset
);
2688 return(bufferoffset
);
2692 if(t2p
->tiff_compression
== COMPRESSION_JPEG
){
2693 unsigned char table_end
[2];
2694 buffer
= (unsigned char*) _TIFFmalloc(t2p
->tiff_datasize
);
2696 TIFFError(TIFF2PDF_MODULE
,
2697 "Can't allocate %u bytes of memory "
2698 "for t2p_readwrite_pdf_image_tile, %s",
2700 TIFFFileName(input
));
2701 t2p
->t2p_error
= T2P_ERR_ERROR
;
2704 if(TIFFGetField(input
, TIFFTAG_JPEGTABLES
, &xuint16_1
, &jpt
) != 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];
2712 xuint32
= bufferoffset
;
2713 bufferoffset
+= TIFFReadRawTile(
2716 (tdata_t
) &(((unsigned char*)buffer
)[bufferoffset
-2]),
2718 buffer
[xuint32
-2]=table_end
[0];
2719 buffer
[xuint32
-1]=table_end
[1];
2721 bufferoffset
+= TIFFReadRawTile(
2724 (tdata_t
) &(((unsigned char*)buffer
)[bufferoffset
]),
2728 TIFFWriteFile(output
, (tdata_t
) buffer
, bufferoffset
);
2730 return(bufferoffset
);
2736 if(t2p
->pdf_sample
==T2P_SAMPLE_NOTHING
){
2737 buffer
= (unsigned char*) _TIFFmalloc(t2p
->tiff_datasize
);
2739 TIFFError(TIFF2PDF_MODULE
,
2740 "Can't allocate %u bytes of memory for "
2741 "t2p_readwrite_pdf_image_tile, %s",
2743 TIFFFileName(input
));
2744 t2p
->t2p_error
= T2P_ERR_ERROR
;
2748 read
= TIFFReadEncodedTile(
2751 (tdata_t
) &buffer
[bufferoffset
],
2752 t2p
->tiff_datasize
);
2754 TIFFError(TIFF2PDF_MODULE
,
2755 "Error on decoding tile %u of %s",
2757 TIFFFileName(input
));
2759 t2p
->t2p_error
=T2P_ERR_ERROR
;
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
);
2772 TIFFError(TIFF2PDF_MODULE
,
2773 "Can't allocate %u bytes of memory "
2774 "for t2p_readwrite_pdf_image_tile, %s",
2776 TIFFFileName(input
));
2777 t2p
->t2p_error
= T2P_ERR_ERROR
;
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",
2786 TIFFFileName(input
));
2787 t2p
->t2p_error
= T2P_ERR_ERROR
;
2790 samplebufferoffset
=0;
2791 for(i
=0;i
<t2p
->tiff_samplesperpixel
;i
++){
2793 TIFFReadEncodedTile(input
,
2795 (tdata_t
) &(samplebuffer
[samplebufferoffset
]),
2798 TIFFError(TIFF2PDF_MODULE
,
2799 "Error on decoding tile %u of %s",
2801 TIFFFileName(input
));
2802 _TIFFfree(samplebuffer
);
2804 t2p
->t2p_error
=T2P_ERR_ERROR
;
2807 samplebufferoffset
+=read
;
2809 t2p_sample_planar_separate_to_contig(
2811 &(buffer
[bufferoffset
]),
2813 samplebufferoffset
);
2814 bufferoffset
+=samplebufferoffset
;
2815 _TIFFfree(samplebuffer
);
2819 buffer
= (unsigned char*) _TIFFmalloc(t2p
->tiff_datasize
);
2821 TIFFError(TIFF2PDF_MODULE
,
2822 "Can't allocate %u bytes of memory "
2823 "for t2p_readwrite_pdf_image_tile, %s",
2825 TIFFFileName(input
));
2826 t2p
->t2p_error
= T2P_ERR_ERROR
;
2829 read
= TIFFReadEncodedTile(
2832 (tdata_t
) &buffer
[bufferoffset
],
2833 t2p
->tiff_datasize
);
2835 TIFFError(TIFF2PDF_MODULE
,
2836 "Error on decoding tile %u of %s",
2838 TIFFFileName(input
));
2840 t2p
->t2p_error
=T2P_ERR_ERROR
;
2845 if(t2p
->pdf_sample
& T2P_SAMPLE_RGBA_TO_RGB
){
2846 t2p
->tiff_datasize
=t2p_sample_rgba_to_rgb(
2848 t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_tilewidth
2849 *t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_tilelength
);
2852 if(t2p
->pdf_sample
& T2P_SAMPLE_RGBAA_TO_RGB
){
2853 t2p
->tiff_datasize
=t2p_sample_rgbaa_to_rgb(
2855 t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_tilewidth
2856 *t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_tilelength
);
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
));
2864 t2p
->t2p_error
= T2P_ERR_ERROR
;
2868 if(t2p
->pdf_sample
& T2P_SAMPLE_LAB_SIGNED_TO_UNSIGNED
){
2869 t2p
->tiff_datasize
=t2p_sample_lab_signed_to_unsigned(
2871 t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_tilewidth
2872 *t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_tilelength
);
2876 if(t2p_tile_is_right_edge(t2p
->tiff_tiles
[t2p
->pdf_page
], tile
) != 0){
2877 t2p_tile_collapse_left(
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
);
2885 t2p
->tiff_writeproc
=output
->tif_writeproc
;
2886 output
->tif_writeproc
=t2p_empty_writeproc
;
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){
2895 t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_tilewidth
);
2900 t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_edgetilewidth
);
2902 if(t2p_tile_is_bottom_edge(t2p
->tiff_tiles
[t2p
->pdf_page
], tile
) == 0){
2905 TIFFTAG_IMAGELENGTH
,
2906 t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_tilelength
);
2909 TIFFTAG_ROWSPERSTRIP
,
2910 t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_tilelength
);
2914 TIFFTAG_IMAGELENGTH
,
2915 t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_edgetilelength
);
2918 TIFFTAG_ROWSPERSTRIP
,
2919 t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_edgetilelength
);
2921 TIFFSetField(output
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
);
2922 TIFFSetField(output
, TIFFTAG_FILLORDER
, FILLORDER_MSB2LSB
);
2924 switch(t2p
->pdf_compression
){
2925 case T2P_COMPRESS_NONE
:
2926 TIFFSetField(output
, TIFFTAG_COMPRESSION
, COMPRESSION_NONE
);
2928 #ifdef CCITT_SUPPORT
2929 case T2P_COMPRESS_G4
:
2930 TIFFSetField(output
, TIFFTAG_COMPRESSION
, COMPRESSION_CCITTFAX4
);
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
);
2941 if(TIFFGetField(input
, TIFFTAG_REFERENCEBLACKWHITE
, &xfloatp
)!=0){
2942 TIFFSetField(output
, TIFFTAG_REFERENCEBLACKWHITE
, xfloatp
);
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
);
2952 TIFFSetField(output
, TIFFTAG_JPEGCOLORMODE
, JPEGCOLORMODE_RAW
);
2955 if(t2p
->pdf_colorspace
& T2P_CS_GRAY
){
2958 if(t2p
->pdf_colorspace
& T2P_CS_CMYK
){
2961 if(t2p
->pdf_defaultcompressionquality
!= 0){
2962 TIFFSetField(output
,
2963 TIFFTAG_JPEGQUALITY
,
2964 t2p
->pdf_defaultcompressionquality
);
2969 case T2P_COMPRESS_ZIP
:
2970 TIFFSetField(output
, TIFFTAG_COMPRESSION
, COMPRESSION_DEFLATE
);
2971 if(t2p
->pdf_defaultcompressionquality%100
!= 0){
2972 TIFFSetField(output
,
2974 t2p
->pdf_defaultcompressionquality
% 100);
2976 if(t2p
->pdf_defaultcompressionquality
/100 != 0){
2977 TIFFSetField(output
,
2979 (t2p
->pdf_defaultcompressionquality
/ 100));
2987 output
->tif_writeproc
=t2p
->tiff_writeproc
;
2988 bufferoffset
=TIFFWriteEncodedStrip(output
, (tstrip_t
) 0, buffer
, TIFFStripSize(output
));
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
;
3001 written
= output
->tif_dir
.td_stripbytecount
[0];
3006 #ifdef OJPEG_SUPPORT
3007 int t2p_process_ojpeg_tables(T2P
* t2p
, TIFF
* input
){
3019 unsigned char* ojpegdata
;
3021 uint32 offset_table
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
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
;
3088 if(!TIFFGetField(input
, TIFFTAG_YCBCRSUBSAMPLING
, &h_samp
, &v_samp
)){
3092 if(t2p
->pdf_ojpegdata
!= NULL
){
3093 _TIFFfree(t2p
->pdf_ojpegdata
);
3094 t2p
->pdf_ojpegdata
=NULL
;
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",
3101 TIFFFileName(input
));
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;
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;
3117 ojpegdata
[t2p
->pdf_ojpegdatalength
++]=0xc3;
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;
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;
3141 ojpegdata
[t2p
->pdf_ojpegdatalength
++]=(t2p
->tiff_samplesperpixel
& 0xff);
3142 for(i
=0;i
<t2p
->tiff_samplesperpixel
;i
++){
3143 ojpegdata
[t2p
->pdf_ojpegdatalength
++]=i
;
3145 ojpegdata
[t2p
->pdf_ojpegdatalength
] |= h_samp
<<4 & 0xf0;;
3146 ojpegdata
[t2p
->pdf_ojpegdatalength
++] |= v_samp
& 0x0f;
3148 ojpegdata
[t2p
->pdf_ojpegdatalength
++]= 0x11;
3150 ojpegdata
[t2p
->pdf_ojpegdatalength
++]=i
;
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;
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);
3174 code_count
+=ojpegdata
[t2p
->pdf_ojpegdatalength
++];
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
;
3183 if(proc
==JPEGPROC_BASELINE
){
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);
3197 code_count
+=ojpegdata
[t2p
->pdf_ojpegdatalength
++];
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
;
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;
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;
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;
3233 ojpegdata
[t2p
->pdf_ojpegdatalength
++] = (i
<< 4) & 0xf0;
3236 if(proc
==JPEGPROC_BASELINE
){
3237 t2p
->pdf_ojpegdatalength
++;
3238 ojpegdata
[t2p
->pdf_ojpegdatalength
++]=0x3f;
3239 t2p
->pdf_ojpegdatalength
++;
3241 ojpegdata
[t2p
->pdf_ojpegdatalength
++]= (lp
[0] & 0xff);
3242 t2p
->pdf_ojpegdatalength
++;
3243 ojpegdata
[t2p
->pdf_ojpegdatalength
++]= (pt
[0] & 0x0f);
3251 int t2p_process_jpeg_strip(
3252 unsigned char* strip
,
3253 tsize_t
* striplength
,
3254 unsigned char* buffer
,
3255 tsize_t
* bufferoffset
,
3267 while(i
<(*striplength
)){
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);
3287 ri
=((( ((uint16
)(buffer
[*bufferoffset
+5])<<8) |
3288 (uint16
)(buffer
[*bufferoffset
+6]) )+v_samp
-1)/
3290 ri
*=((( ((uint16
)(buffer
[*bufferoffset
+7])<<8) |
3291 (uint16
)(buffer
[*bufferoffset
+8]) )+h_samp
-1)/
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;
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;
3312 _TIFFmemcpy(&(buffer
[*bufferoffset
]), &(strip
[i
-1]), strip
[i
+2]+2);
3313 *bufferoffset
+=strip
[i
+2]+2;
3318 _TIFFmemcpy(&(buffer
[*bufferoffset
]), &(strip
[i
-1]), strip
[i
+2]+2);
3319 *bufferoffset
+=strip
[i
+2]+2;
3322 buffer
[(*bufferoffset
)++]=0xff;
3323 buffer
[(*bufferoffset
)++]=
3324 (unsigned char)(0xd0 | ((no
-1)%8
));
3327 _TIFFmemcpy(&(buffer
[*bufferoffset
]), &(strip
[i
-1]), (*striplength
)-i
-1);
3328 *bufferoffset
+=(*striplength
)-i
-1;
3341 This functions converts a tilewidth x tilelength buffer of samples into an edgetilewidth x
3342 tilelength buffer of samples.
3344 void t2p_tile_collapse_left(
3348 uint32 edgetilewidth
,
3352 tsize_t edgescanwidth
=0;
3354 edgescanwidth
= (scanwidth
* edgetilewidth
+ (tilewidth
- 1))/ tilewidth
;
3355 for(i
=i
;i
<tilelength
;i
++){
3357 &(((char*)buffer
)[edgescanwidth
*i
]),
3358 &(((char*)buffer
)[scanwidth
*i
]),
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
3372 void t2p_write_advance_directory(T2P
* t2p
, TIFF
* output
){
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
;
3388 output
->tif_writeproc
=t2p
->tiff_writeproc
;
3389 output
->tif_readproc
=t2p
->tiff_readproc
;
3390 output
->tif_seekproc
=t2p
->tiff_seekproc
;
3395 tsize_t
t2p_sample_planar_separate_to_contig(
3397 unsigned char* buffer
,
3398 unsigned char* samplebuffer
,
3399 tsize_t samplebuffersize
){
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
];
3412 return(samplebuffersize
);
3415 tsize_t
t2p_sample_realize_palette(T2P
* t2p
, unsigned char* buffer
){
3417 uint32 sample_count
=0;
3418 uint16 component_count
=0;
3419 uint32 palette_offset
=0;
3420 uint32 sample_offset
=0;
3423 sample_count
=t2p
->tiff_width
*t2p
->tiff_length
;
3424 component_count
=t2p
->tiff_samplesperpixel
;
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
];
3438 This functions converts in place a buffer of ABGR interleaved data
3439 into RGB interleaved data, discarding A.
3442 tsize_t
t2p_sample_abgr_to_rgb(tdata_t data
, uint32 samplecount
)
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);
3458 * This functions converts in place a buffer of RGBA interleaved data
3459 * into RGB interleaved data, discarding A.
3463 t2p_sample_rgbaa_to_rgb(tdata_t data
, uint32 samplecount
)
3467 for(i
= 0; i
< samplecount
; i
++)
3468 memcpy((uint8
*)data
+ i
* 3, (uint8
*)data
+ i
* 4, 3);
3474 * This functions converts in place a buffer of RGBA interleaved data
3475 * into RGB interleaved data, adding 255-A to each component sample.
3479 t2p_sample_rgba_to_rgb(tdata_t data
, uint32 samplecount
)
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
;
3498 This function converts the a and b samples of Lab data from signed
3502 tsize_t
t2p_sample_lab_signed_to_unsigned(tdata_t buffer
, uint32 samplecount
){
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]);
3511 ((unsigned char*)buffer
)[(i
*3)+1] |= 0x80;
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]);
3517 ((unsigned char*)buffer
)[(i
*3)+2] |= 0x80;
3521 return(samplecount
*3);
3525 This function writes the PDF header to output.
3528 tsize_t
t2p_write_pdf_header(T2P
* t2p
, TIFF
* output
){
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);
3542 This function writes the beginning of a PDF object to output.
3545 tsize_t
t2p_write_pdf_obj_start(uint32 number
, TIFF
* output
){
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);
3559 This function writes the end of a PDF object to output.
3562 tsize_t
t2p_write_pdf_obj_end(TIFF
* output
){
3566 written
+= TIFFWriteFile(output
, (tdata_t
) "endobj\r", 7);
3572 This function writes a PDF name object to output.
3575 tsize_t
t2p_write_pdf_name(char* name
, TIFF
* output
){
3583 namelen
=strlen(name
);
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);
3594 if ( ((unsigned char)name
[i
]) > 0x7E){
3595 sprintf(buffer
, "#%.2X", name
[i
]);
3596 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, 3);
3602 sprintf(buffer
, "#%.2X", name
[i
]);
3603 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, 3);
3606 sprintf(buffer
, "#%.2X", name
[i
]);
3607 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, 3);
3610 sprintf(buffer
, "#%.2X", name
[i
]);
3611 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, 3);
3614 sprintf(buffer
, "#%.2X", name
[i
]);
3615 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, 3);
3618 sprintf(buffer
, "#%.2X", name
[i
]);
3619 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, 3);
3622 sprintf(buffer
, "#%.2X", name
[i
]);
3623 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, 3);
3626 sprintf(buffer
, "#%.2X", name
[i
]);
3627 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, 3);
3630 sprintf(buffer
, "#%.2X", name
[i
]);
3631 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, 3);
3634 sprintf(buffer
, "#%.2X", name
[i
]);
3635 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, 3);
3638 sprintf(buffer
, "#%.2X", name
[i
]);
3639 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, 3);
3642 sprintf(buffer
, "#%.2X", name
[i
]);
3643 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, 3);
3646 written
+= TIFFWriteFile(output
, (tdata_t
) &name
[i
], 1);
3651 written
+= TIFFWriteFile(output
, (tdata_t
) " ", 1);
3657 This function writes a PDF string object to output.
3660 tsize_t
t2p_write_pdf_string(char* pdfstr
, TIFF
* output
){
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);
3676 written
+= TIFFWriteFile(output
, (tdata_t
) "\\b", 2);
3679 written
+= TIFFWriteFile(output
, (tdata_t
) "\\t", 2);
3682 written
+= TIFFWriteFile(output
, (tdata_t
) "\\n", 2);
3685 written
+= TIFFWriteFile(output
, (tdata_t
) "\\f", 2);
3688 written
+= TIFFWriteFile(output
, (tdata_t
) "\\r", 2);
3691 written
+= TIFFWriteFile(output
, (tdata_t
) "\\(", 2);
3694 written
+= TIFFWriteFile(output
, (tdata_t
) "\\)", 2);
3697 written
+= TIFFWriteFile(output
, (tdata_t
) "\\\\", 2);
3700 written
+= TIFFWriteFile(output
, (tdata_t
) &pdfstr
[i
], 1);
3704 written
+= TIFFWriteFile(output
, (tdata_t
) ") ", 1);
3711 This function writes a buffer of data to output.
3714 tsize_t
t2p_write_pdf_stream(tdata_t buffer
, tsize_t len
, TIFF
* output
){
3718 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, len
);
3724 This functions writes the beginning of a PDF stream to output.
3727 tsize_t
t2p_write_pdf_stream_start(TIFF
* output
){
3731 written
+= TIFFWriteFile(output
, (tdata_t
) "stream\r\n", 8);
3737 This function writes the end of a PDF stream to output.
3740 tsize_t
t2p_write_pdf_stream_end(TIFF
* output
){
3744 written
+= TIFFWriteFile(output
, (tdata_t
) "\rendstream\r", 11);
3750 This function writes a stream dictionary for a PDF stream to output.
3753 tsize_t
t2p_write_pdf_stream_dict(tsize_t len
, uint32 number
, TIFF
* output
){
3759 written
+= TIFFWriteFile(output
, (tdata_t
) "/Length ", 8);
3761 written
+= t2p_write_pdf_stream_length(len
, output
);
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);
3772 This functions writes the beginning of a PDF stream dictionary to output.
3775 tsize_t
t2p_write_pdf_stream_dict_start(TIFF
* output
){
3779 written
+= TIFFWriteFile(output
, (tdata_t
) "<< \r", 4);
3785 This function writes the end of a PDF stream dictionary to output.
3788 tsize_t
t2p_write_pdf_stream_dict_end(TIFF
* output
){
3792 written
+= TIFFWriteFile(output
, (tdata_t
) " >>\r", 4);
3798 This function writes a number to output.
3801 tsize_t
t2p_write_pdf_stream_length(tsize_t len
, TIFF
* output
){
3807 buflen
=sprintf(buffer
, "%lu", (unsigned long)len
);
3808 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, buflen
);
3809 written
+= TIFFWriteFile(output
, (tdata_t
) "\r", 1);
3815 This function writes the PDF Catalog structure to output.
3818 tsize_t
t2p_write_pdf_catalog(T2P
* t2p
, TIFF
* output
){
3824 written
+= TIFFWriteFile(output
,
3825 (tdata_t
)"<< \r/Type /Catalog \r/Pages ",
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",
3835 written
+= TIFFWriteFile(output
, (tdata_t
)">>\r", 3);
3841 This function writes the PDF Info structure to output.
3844 tsize_t
t2p_write_pdf_info(T2P
* t2p
, TIFF
* input
, TIFF
* output
){
3851 if(t2p
->pdf_datetime
==NULL
){
3852 t2p_pdf_tifftime(t2p
, input
);
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
);
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);
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);
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);
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);
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);
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);
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);
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);
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);
3938 written
+= TIFFWriteFile(output
, (tdata_t
) ">> \r", 4);
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.
3948 void t2p_pdf_currenttime(T2P
* t2p
){
3950 struct tm
* currenttime
;
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);
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.
3971 void t2p_pdf_tifftime(T2P
* t2p
, TIFF
* input
){
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",
3980 t2p
->t2p_error
= T2P_ERR_ERROR
;
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];
4003 t2p_pdf_currenttime(t2p
);
4010 This function writes a PDF Pages Tree structure to output.
4013 tsize_t
t2p_write_pdf_pages(T2P
* t2p
,
4022 written
+= TIFFWriteFile(output
,
4023 (tdata_t
) "<< \r/Type /Pages \r/Kids [ ",
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);
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
);
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);
4051 This function writes a PDF Page structure to output.
4054 tsize_t
t2p_write_pdf_page(uint32 object
, T2P
* t2p
, TIFF
* output
){
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);
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);
4100 written
+= TIFFWriteFile(output
, (tdata_t
) "\r", 1);
4103 written
+= TIFFWriteFile(output
, (tdata_t
) ">>\r", 3);
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);
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);
4118 if(t2p
->tiff_transferfunctioncount
!= 0) {
4119 written
+= TIFFWriteFile(output
, (tdata_t
) "/ExtGState <<", 13);
4120 TIFFWriteFile(output
, (tdata_t
) "/GS1 ", 5);
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);
4129 written
+= TIFFWriteFile(output
, (tdata_t
) "/ProcSet [ ", 11);
4130 if(t2p
->pdf_colorspace
== T2P_CS_BILEVEL
4131 || t2p
->pdf_colorspace
== T2P_CS_GRAY
4133 written
+= TIFFWriteFile(output
, (tdata_t
) "/ImageB ", 8);
4135 written
+= TIFFWriteFile(output
, (tdata_t
) "/ImageC ", 8);
4136 if(t2p
->pdf_colorspace
& T2P_CS_PALETTE
){
4137 written
+= TIFFWriteFile(output
, (tdata_t
) "/ImageI ", 8);
4140 written
+= TIFFWriteFile(output
, (tdata_t
) "]\r>>\r>>\r", 8);
4146 This function composes the page size and image and tile locations on a page.
4149 void t2p_compose_pdf_page(T2P
* t2p
){
4153 T2P_TILE
* tiles
=NULL
;
4155 uint32 tilecountx
=0;
4156 uint32 tilecounty
=0;
4158 uint32 tilelength
=0;
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
;
4168 if(t2p
->pdf_xres
==0.0){
4169 t2p
->pdf_xres
= t2p
->pdf_defaultxres
;
4171 if(t2p
->pdf_yres
==0.0){
4172 t2p
->pdf_yres
= t2p
->pdf_defaultyres
;
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
;
4180 t2p
->pdf_pagewidth
= t2p
->pdf_imagewidth
;
4181 t2p
->pdf_pagelength
= t2p
->pdf_imagelength
;
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
);
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
;
4202 istiled
=((t2p
->tiff_tiles
[t2p
->pdf_page
]).tiles_tilecount
==0) ? 0 : 1;
4204 t2p_compose_pdf_page_orient(&(t2p
->pdf_imagebox
), t2p
->tiff_orientation
);
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
+
4212 (t2p
->tiff_tiles
[t2p
->pdf_page
]).tiles_tilecountx
=tilecountx
;
4213 tilecounty
=(t2p
->tiff_length
+
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
);
4226 t2p
->pdf_imagebox
.x1
4227 + ((float)(t2p
->pdf_imagewidth
* i
* tilewidth
)
4228 / (float)t2p
->tiff_width
);
4230 t2p
->pdf_imagebox
.x1
4231 + ((float)(t2p
->pdf_imagewidth
* (i
+1) * tilewidth
)
4232 / (float)t2p
->tiff_width
);
4234 t2p
->pdf_imagebox
.y2
4235 - ((float)(t2p
->pdf_imagelength
* (i2
+1) * tilelength
)
4236 / (float)t2p
->tiff_length
);
4238 t2p
->pdf_imagebox
.y2
4239 - ((float)(t2p
->pdf_imagelength
* i2
* tilelength
)
4240 / (float)t2p
->tiff_length
);
4242 boxp
=&(tiles
[i2
*tilecountx
+i
].tile_box
);
4244 t2p
->pdf_imagebox
.x1
4245 + ((float)(t2p
->pdf_imagewidth
* i
* tilewidth
)
4246 / (float)t2p
->tiff_width
);
4247 boxp
->x2
= t2p
->pdf_imagebox
.x2
;
4249 t2p
->pdf_imagebox
.y2
4250 - ((float)(t2p
->pdf_imagelength
* (i2
+1) * tilelength
)
4251 / (float)t2p
->tiff_length
);
4253 t2p
->pdf_imagebox
.y2
4254 - ((float)(t2p
->pdf_imagelength
* i2
* tilelength
)
4255 / (float)t2p
->tiff_length
);
4257 for(i
=0;i
<tilecountx
-1;i
++){
4258 boxp
=&(tiles
[i2
*tilecountx
+i
].tile_box
);
4260 t2p
->pdf_imagebox
.x1
4261 + ((float)(t2p
->pdf_imagewidth
* i
* tilewidth
)
4262 / (float)t2p
->tiff_width
);
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
;
4269 t2p
->pdf_imagebox
.y2
4270 - ((float)(t2p
->pdf_imagelength
* i2
* tilelength
)
4271 / (float)t2p
->tiff_length
);
4273 boxp
=&(tiles
[i2
*tilecountx
+i
].tile_box
);
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
;
4281 t2p
->pdf_imagebox
.y2
4282 - ((float)(t2p
->pdf_imagelength
* i2
* tilelength
)
4283 / (float)t2p
->tiff_length
);
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);
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
;
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
;
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
;
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
;
4313 if(t2p
->tiff_orientation
> 4){
4315 boxp
->x1
= boxp
->y1
;
4318 boxp
->x2
= boxp
->y2
;
4320 t2p_compose_pdf_page_orient_flip(boxp
, t2p
->tiff_orientation
);
4322 t2p_compose_pdf_page_orient(boxp
, t2p
->tiff_orientation
);
4330 void t2p_compose_pdf_page_orient(T2P_BOX
* boxp
, uint16 orientation
){
4335 if( boxp
->x1
> boxp
->x2
){
4340 if( boxp
->y1
> boxp
->y2
){
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
){
4359 boxp
->mat
[0]=0.0F
-m1
[0];
4360 boxp
->mat
[6]+=m1
[0];
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];
4369 boxp
->mat
[4]=0.0F
-m1
[4];
4370 boxp
->mat
[7]+=m1
[4];
4374 boxp
->mat
[1]=0.0F
-m1
[0];
4375 boxp
->mat
[3]=0.0F
-m1
[4];
4377 boxp
->mat
[6]+=m1
[4];
4378 boxp
->mat
[7]+=m1
[0];
4382 boxp
->mat
[1]=0.0F
-m1
[0];
4385 boxp
->mat
[7]+=m1
[0];
4396 boxp
->mat
[3]=0.0F
-m1
[4];
4398 boxp
->mat
[6]+=m1
[4];
4405 void t2p_compose_pdf_page_orient_flip(T2P_BOX
* boxp
, uint16 orientation
){
4410 if( boxp
->x1
> boxp
->x2
){
4415 if( boxp
->y1
> boxp
->y2
){
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
){
4432 boxp
->mat
[1]=0.0F
-m1
[4];
4433 boxp
->mat
[3]=0.0F
-m1
[0];
4435 boxp
->mat
[6]+=m1
[0];
4436 boxp
->mat
[7]+=m1
[4];
4440 boxp
->mat
[1]=0.0F
-m1
[4];
4443 boxp
->mat
[7]+=m1
[4];
4454 boxp
->mat
[3]=0.0F
-m1
[0];
4456 boxp
->mat
[6]+=m1
[0];
4464 This function writes a PDF Contents stream to output.
4467 tsize_t
t2p_write_pdf_page_content_stream(T2P
* t2p
, TIFF
* output
){
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 ":"",
4489 written
+= t2p_write_pdf_stream(buffer
, buflen
, output
);
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 ":"",
4503 written
+= t2p_write_pdf_stream(buffer
, buflen
, output
);
4510 This function writes a PDF Image XObject stream dictionary to output.
4513 tsize_t
t2p_write_pdf_xobject_stream_dict(ttile_t tile
,
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",
4525 buflen
=sprintf(buffer
, "%u", t2p
->pdf_page
+1);
4526 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, buflen
);
4528 written
+= TIFFWriteFile(output
, (tdata_t
) "_", 1);
4529 buflen
=sprintf(buffer
, "%lu", (unsigned long)tile
);
4530 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, buflen
);
4532 written
+= TIFFWriteFile(output
, (tdata_t
) "\r/Width ", 8);
4533 _TIFFmemset((tdata_t
)buffer
, 0x00, 16);
4535 buflen
=sprintf(buffer
, "%lu", (unsigned long)t2p
->tiff_width
);
4537 if(t2p_tile_is_right_edge(t2p
->tiff_tiles
[t2p
->pdf_page
], tile
-1)!=0){
4541 (unsigned long)t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_edgetilewidth
);
4546 (unsigned long)t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_tilewidth
);
4549 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, buflen
);
4550 written
+= TIFFWriteFile(output
, (tdata_t
) "\r/Height ", 9);
4551 _TIFFmemset((tdata_t
)buffer
, 0x00, 16);
4553 buflen
=sprintf(buffer
, "%lu", (unsigned long)t2p
->tiff_length
);
4555 if(t2p_tile_is_bottom_edge(t2p
->tiff_tiles
[t2p
->pdf_page
], tile
-1)!=0){
4559 (unsigned long)t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_edgetilelength
);
4564 (unsigned long)t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_tilelength
);
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
)
4583 written
+= t2p_write_pdf_xobject_decode(t2p
, output
);
4585 written
+= t2p_write_pdf_xobject_stream_filter(tile
, t2p
, output
);
4591 * This function writes a PDF Image XObject Colorspace name to output.
4595 tsize_t
t2p_write_pdf_xobject_cs(T2P
* t2p
, TIFF
* output
){
4605 if( (t2p
->pdf_colorspace
& T2P_CS_ICCBASED
) != 0){
4606 written
+= t2p_write_pdf_xobject_icccs(t2p
, output
);
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);
4623 if(t2p
->pdf_colorspace
& T2P_CS_BILEVEL
){
4624 written
+= TIFFWriteFile(output
, (tdata_t
) "/DeviceGray \r", 13);
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
);
4630 written
+= TIFFWriteFile(output
, (tdata_t
) "/DeviceGray \r", 13);
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
);
4637 written
+= TIFFWriteFile(output
, (tdata_t
) "/DeviceRGB \r", 12);
4640 if(t2p
->pdf_colorspace
& T2P_CS_CMYK
){
4641 written
+= TIFFWriteFile(output
, (tdata_t
) "/DeviceCMYK \r", 13);
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
);
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
);
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);
4676 tsize_t
t2p_write_pdf_transfer(T2P
* t2p
, TIFF
* output
){
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);
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);
4705 written
+= TIFFWriteFile(output
, (tdata_t
) " >> \r", 5);
4710 tsize_t
t2p_write_pdf_transfer_dict(T2P
* t2p
, TIFF
* output
, uint16 i
){
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
);
4728 tsize_t
t2p_write_pdf_transfer_stream(T2P
* t2p
, TIFF
* output
, uint16 i
){
4732 written
+= t2p_write_pdf_stream(
4733 t2p
->tiff_transferfunction
[i
],
4734 (1<<(t2p
->tiff_bitspersample
+1)),
4741 This function writes a PDF Image XObject Colorspace array to output.
4744 tsize_t
t2p_write_pdf_xobject_calcs(T2P
* t2p
, TIFF
* output
){
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
);
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
);
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);
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",
4828 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, buflen
);
4829 written
+= TIFFWriteFile(output
, (tdata_t
) "/Gamma [2.2 2.2 2.2] \r", 22);
4831 written
+= TIFFWriteFile(output
, (tdata_t
) ">>] \r", 5);
4837 This function writes a PDF Image XObject Colorspace array to output.
4840 tsize_t
t2p_write_pdf_xobject_icccs(T2P
* t2p
, TIFF
* output
){
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);
4854 tsize_t
t2p_write_pdf_xobject_icccs_dict(T2P
* t2p
, TIFF
* output
){
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
);
4872 tsize_t
t2p_write_pdf_xobject_icccs_stream(T2P
* t2p
, TIFF
* output
){
4876 written
+= t2p_write_pdf_stream(
4877 (tdata_t
) t2p
->tiff_iccprofile
,
4878 (tsize_t
) t2p
->tiff_iccprofilelength
,
4885 This function writes a palette stream for an indexed color space to output.
4888 tsize_t
t2p_write_pdf_xobject_palettecs_stream(T2P
* t2p
, TIFF
* output
){
4892 written
+= t2p_write_pdf_stream(
4893 (tdata_t
) t2p
->pdf_palette
,
4894 (tsize_t
) t2p
->pdf_palettesize
,
4901 This function writes a PDF Image XObject Decode array to output.
4904 tsize_t
t2p_write_pdf_xobject_decode(T2P
* t2p
, TIFF
* output
){
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);
4913 written
+= TIFFWriteFile(output
, (tdata_t
) "]\r", 2);
4919 This function writes a PDF Image XObject stream filter name and parameters to
4923 tsize_t
t2p_write_pdf_xobject_stream_filter(ttile_t tile
, T2P
* t2p
, TIFF
* output
){
4929 if(t2p
->pdf_compression
==T2P_COMPRESS_NONE
){
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);
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
);
4949 if(t2p_tile_is_right_edge(t2p
->tiff_tiles
[t2p
->pdf_page
], tile
-1)==0){
4950 written
+= TIFFWriteFile(output
, (tdata_t
) "/Columns ", 9);
4954 (unsigned long)t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_tilewidth
);
4955 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, buflen
);
4957 written
+= TIFFWriteFile(output
, (tdata_t
) "/Columns ", 9);
4961 (unsigned long)t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_edgetilewidth
);
4962 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, buflen
);
4964 if(t2p_tile_is_bottom_edge(t2p
->tiff_tiles
[t2p
->pdf_page
], tile
-1)==0){
4965 written
+= TIFFWriteFile(output
, (tdata_t
) " /Rows ", 7);
4969 (unsigned long)t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_tilelength
);
4970 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, buflen
);
4972 written
+= TIFFWriteFile(output
, (tdata_t
) " /Rows ", 7);
4976 (unsigned long)t2p
->tiff_tiles
[t2p
->pdf_page
].tiles_edgetilelength
);
4977 written
+= TIFFWriteFile(output
, (tdata_t
) buffer
, buflen
);
4980 if(t2p
->pdf_switchdecode
== 0){
4981 written
+= TIFFWriteFile(output
, (tdata_t
) " /BlackIs1 true ", 16);
4983 written
+= TIFFWriteFile(output
, (tdata_t
) ">>\r", 3);
4987 case T2P_COMPRESS_JPEG
:
4988 written
+= TIFFWriteFile(output
, (tdata_t
) "/DCTDecode ", 11);
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);
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);
5030 This function writes a PDF xref table to output.
5033 tsize_t
t2p_write_pdf_xreftable(T2P
* t2p
, TIFF
* output
){
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);
5054 * This function writes a PDF trailer to output.
5057 tsize_t
t2p_write_pdf_trailer(T2P
* t2p
, TIFF
* output
)
5060 tsize_t written
= 0;
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
) {
5074 "Can't allocate %u bytes of memory for t2p_write_pdf_trailer",
5076 t2p
->t2p_error
= T2P_ERR_ERROR
;
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);
5109 This function writes a PDF to a file given a pointer to a TIFF.
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.
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.
5121 The first argument to the function is an initialized and validated T2P
5122 context struct pointer.
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.
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.
5133 TIFF* output = TIFFOpen("output.pdf", "w");
5134 assert(output != NULL);
5136 if(output->tif_seekproc != NULL){
5137 TIFFSeekFile(output, (toff_t) 0, SEEK_SET);
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.
5143 After this function completes, call t2p_free on t2p, TIFFClose on input,
5144 and TIFFClose on output.
5147 tsize_t
t2p_write_pdf(T2P
* t2p
, TIFF
* input
, TIFF
* output
){
5151 tsize_t streamlen
=0;
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
){
5160 "Can't allocate %lu bytes of memory for t2p_write_pdf",
5161 t2p
->pdf_xrefcount
* sizeof(uint32
) );
5164 t2p
->pdf_xrefcount
=0;
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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(
5262 written
+= t2p_write_pdf_stream_dict_end(output
);
5263 written
+= t2p_write_pdf_stream_start(output
);
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
);
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(
5285 written
+= t2p_write_pdf_stream_dict_end(output
);
5286 written
+= t2p_write_pdf_stream_start(output
);
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
);
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
;
5310 /* vim: set ts=8 sts=8 sw=8 noet: */