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