Merge lp:~noskcaj/ubuntu/trusty/tiff/tiff into lp:ubuntu/trusty/tiff

Proposed by Jackson Doak
Status: Merged
Merge reported by: Marc Deslauriers
Merged at revision: not available
Proposed branch: lp:~noskcaj/ubuntu/trusty/tiff/tiff
Merge into: lp:ubuntu/trusty/tiff
Diff against target: 46531 lines (+4917/-35989)
175 files modified
.pc/CVE-2012-3401.patch/tools/tiff2pdf.c (+0/-5385)
.pc/CVE-2012-4447.patch/libtiff/tif_pixarlog.c (+0/-1426)
.pc/CVE-2012-4564.patch/tools/ppm2tiff.c (+0/-366)
.pc/CVE-2013-1960.patch/tools/tiff2pdf.c (+0/-5386)
.pc/CVE-2013-1961.patch/contrib/dbs/xtiff/xtiff.c (+0/-1290)
.pc/CVE-2013-1961.patch/libtiff/tif_codec.c (+0/-165)
.pc/CVE-2013-1961.patch/libtiff/tif_dirinfo.c (+0/-907)
.pc/CVE-2013-1961.patch/tools/rgb2ycbcr.c (+0/-390)
.pc/CVE-2013-1961.patch/tools/tiff2bw.c (+0/-471)
.pc/CVE-2013-1961.patch/tools/tiff2pdf.c (+0/-5407)
.pc/CVE-2013-1961.patch/tools/tiff2ps.c (+0/-3073)
.pc/CVE-2013-1961.patch/tools/tiffcrop.c (+0/-9066)
.pc/CVE-2013-1961.patch/tools/tiffdither.c (+0/-333)
.pc/applied-patches (+0/-6)
.pc/opengl.patch/tools/tiffgt.c (+0/-466)
ChangeLog (+82/-0)
HOWTO-RELEASE (+3/-2)
Makefile.am (+1/-1)
Makefile.in (+118/-85)
Makefile.vc (+1/-1)
RELEASE-DATE (+1/-1)
SConstruct (+1/-1)
TODO (+1/-1)
VERSION (+1/-1)
aclocal.m4 (+183/-212)
build/Makefile.in (+81/-64)
config/ltmain.sh (+1/-1)
config/test-driver (+127/-0)
configure (+272/-272)
configure.ac (+11/-7)
configure.com (+13/-1)
contrib/Makefile.in (+81/-64)
contrib/addtiffo/Makefile.am (+1/-1)
contrib/addtiffo/Makefile.in (+61/-15)
contrib/dbs/Makefile.am (+1/-1)
contrib/dbs/Makefile.in (+94/-73)
contrib/dbs/xtiff/Makefile.in (+36/-6)
contrib/dbs/xtiff/xtiff.c (+2/-2)
contrib/iptcutil/Makefile.am (+1/-1)
contrib/iptcutil/Makefile.in (+61/-15)
contrib/mfs/Makefile.in (+36/-6)
contrib/pds/Makefile.in (+36/-6)
contrib/ras/Makefile.in (+36/-6)
contrib/stream/Makefile.in (+36/-6)
contrib/tags/Makefile.in (+36/-6)
contrib/win_dib/Makefile.in (+36/-6)
debian/all-preinst (+2/-3)
debian/changelog (+68/-0)
debian/control (+6/-2)
debian/libtiff5.shlibs (+0/-1)
debian/libtiff5.symbols (+252/-0)
debian/patches/CVE-2012-3401.patch (+0/-16)
debian/patches/CVE-2012-4447.patch (+0/-13)
debian/patches/CVE-2012-4564.patch (+3/-6)
debian/patches/CVE-2013-1960.patch (+5/-10)
debian/patches/CVE-2013-1961.patch (+90/-95)
debian/patches/CVE-2013-4231.patch (+17/-0)
debian/patches/CVE-2013-4232.patch (+18/-0)
debian/patches/CVE-2013-4244.patch (+18/-0)
debian/patches/opengl.patch (+3/-3)
debian/patches/series (+3/-2)
debian/rules (+4/-3)
html/Makefile.in (+85/-66)
html/addingtags.html (+1/-1)
html/bugs.html (+1/-1)
html/contrib.html (+1/-1)
html/document.html (+1/-1)
html/images.html (+1/-1)
html/images/Makefile.in (+39/-7)
html/index.html (+1/-1)
html/internals.html (+1/-1)
html/intro.html (+1/-1)
html/libtiff.html (+1/-1)
html/man/Makefile.am (+7/-0)
html/man/Makefile.in (+46/-7)
html/man/TIFFFieldDataType.3tiff.html (+89/-0)
html/man/TIFFFieldName.3tiff.html (+86/-0)
html/man/TIFFFieldPassCount.3tiff.html (+98/-0)
html/man/TIFFFieldReadCount.3tiff.html (+101/-0)
html/man/TIFFFieldTag.3tiff.html (+88/-0)
html/man/TIFFFieldWriteCount.3tiff.html (+108/-0)
html/man/bmp2tiff.1.html (+121/-0)
html/misc.html (+1/-1)
html/support.html (+1/-1)
html/v3.4beta007.html (+1/-1)
html/v3.4beta016.html (+1/-1)
html/v3.4beta018.html (+1/-1)
html/v3.4beta024.html (+1/-1)
html/v3.4beta028.html (+1/-1)
html/v3.4beta029.html (+1/-1)
html/v3.4beta031.html (+1/-1)
html/v3.4beta032.html (+1/-1)
html/v3.4beta033.html (+1/-1)
html/v3.4beta034.html (+1/-1)
html/v3.4beta035.html (+1/-1)
html/v3.4beta036.html (+1/-1)
html/v3.5.1.html (+1/-1)
html/v3.5.2.html (+1/-1)
html/v3.5.3.html (+1/-1)
html/v3.5.4.html (+1/-1)
html/v3.5.5.html (+1/-1)
html/v3.5.6-beta.html (+1/-1)
html/v3.5.7.html (+1/-1)
html/v3.6.0.html (+1/-1)
html/v3.6.1.html (+1/-1)
html/v3.7.0.html (+1/-1)
html/v3.7.0alpha.html (+1/-1)
html/v3.7.0beta.html (+1/-1)
html/v3.7.0beta2.html (+1/-1)
html/v3.7.1.html (+1/-1)
html/v3.7.2.html (+1/-1)
html/v3.7.3.html (+1/-1)
html/v3.7.4.html (+1/-1)
html/v3.8.0.html (+1/-1)
html/v3.8.1.html (+1/-1)
html/v3.8.2.html (+1/-1)
html/v4.0.2.html (+1/-1)
libtiff/Makefile.in (+88/-31)
libtiff/SConstruct (+1/-1)
libtiff/libtiff.def (+7/-0)
libtiff/tif_codec.c (+1/-2)
libtiff/tif_config.h.in (+5/-3)
libtiff/tif_dirinfo.c (+56/-6)
libtiff/tif_dirread.c (+21/-8)
libtiff/tif_dirwrite.c (+3/-3)
libtiff/tif_fax3.c (+2/-2)
libtiff/tif_jpeg.c (+6/-3)
libtiff/tif_packbits.c (+9/-3)
libtiff/tif_pixarlog.c (+1/-1)
libtiff/tif_print.c (+4/-2)
libtiff/tif_read.c (+3/-3)
libtiff/tif_write.c (+31/-9)
libtiff/tiff.h (+29/-1)
libtiff/tiffio.h (+8/-1)
libtiff/tiffvers.h (+2/-2)
m4/libtool.m4 (+1/-1)
man/Makefile.am (+6/-0)
man/Makefile.in (+68/-14)
man/TIFFFieldDataType.3tiff (+53/-0)
man/TIFFFieldName.3tiff (+52/-0)
man/TIFFFieldPassCount.3tiff (+73/-0)
man/TIFFFieldReadCount.3tiff (+77/-0)
man/TIFFFieldTag.3tiff (+56/-0)
man/TIFFFieldWriteCount.3tiff (+88/-0)
man/libtiff.3tiff (+10/-1)
nmake.opt (+1/-1)
port/Makefile.in (+80/-31)
port/Makefile.vc (+1/-1)
port/dummy.c (+1/-1)
port/lfind.c (+1/-1)
port/strtoul.c (+1/-1)
test/Makefile.am (+2/-2)
test/Makefile.in (+853/-301)
test/ascii_tag.c (+1/-1)
test/check_tag.c (+1/-1)
test/long_tag.c (+1/-1)
test/raw_decode.c (+60/-22)
test/short_tag.c (+1/-1)
test/strip.c (+1/-1)
test/strip_rw.c (+1/-1)
test/test_arrays.c (+1/-1)
test/test_arrays.h (+1/-1)
test/tifftest.h (+1/-1)
tools/Makefile.am (+1/-1)
tools/Makefile.in (+76/-27)
tools/ppm2tiff.c (+2/-11)
tools/rasterfile.h (+1/-1)
tools/rgb2ycbcr.c (+1/-2)
tools/tiff2bw.c (+1/-1)
tools/tiff2pdf.c (+171/-156)
tools/tiff2ps.c (+4/-4)
tools/tiffcrop.c (+5/-5)
tools/tiffdither.c (+1/-1)
tools/tiffgt.c (+0/-1)
tools/tiffset.c (+32/-33)
To merge this branch: bzr merge lp:~noskcaj/ubuntu/trusty/tiff/tiff
Reviewer Review Type Date Requested Status
Marc Deslauriers Approve
Review via email: mp+192569@code.launchpad.net

Description of the change

Merge from debian

To post a comment you must log in.
Revision history for this message
Marc Deslauriers (mdeslaur) wrote :

Looks good, uploaded.

Thanks!

review: Approve

Preview Diff

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

Subscribers

People subscribed via source and target branches

to all changes: