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