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