/src/mupdf/source/fitz/load-tiff.c
Line | Count | Source |
1 | | // Copyright (C) 2004-2025 Artifex Software, Inc. |
2 | | // |
3 | | // This file is part of MuPDF. |
4 | | // |
5 | | // MuPDF is free software: you can redistribute it and/or modify it under the |
6 | | // terms of the GNU Affero General Public License as published by the Free |
7 | | // Software Foundation, either version 3 of the License, or (at your option) |
8 | | // any later version. |
9 | | // |
10 | | // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY |
11 | | // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
12 | | // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
13 | | // details. |
14 | | // |
15 | | // You should have received a copy of the GNU Affero General Public License |
16 | | // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html> |
17 | | // |
18 | | // Alternative licensing terms are available from the licensor. |
19 | | // For commercial licensing, see <https://www.artifex.com/> or contact |
20 | | // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
21 | | // CA 94129, USA, for further information. |
22 | | |
23 | | #include "mupdf/fitz.h" |
24 | | |
25 | | #include "pixmap-imp.h" |
26 | | |
27 | | #include <limits.h> |
28 | | #include <assert.h> |
29 | | #include <string.h> |
30 | | |
31 | | /* |
32 | | * TIFF image loader. Should be enough to support TIFF files in XPS. |
33 | | * Baseline TIFF 6.0 plus CMYK, LZW, Flate and JPEG support. |
34 | | * Limited bit depths (1,2,4,8). |
35 | | * Limited planar configurations (1=chunky). |
36 | | * TODO: RGBPal images |
37 | | */ |
38 | | |
39 | | struct tiff |
40 | | { |
41 | | /* "file" */ |
42 | | const unsigned char *bp, *rp, *ep; |
43 | | |
44 | | /* byte order */ |
45 | | unsigned order; |
46 | | |
47 | | /* offset of first ifd */ |
48 | | unsigned *ifd_offsets; |
49 | | int ifds; |
50 | | |
51 | | /* where we can find the strips of image data */ |
52 | | unsigned rowsperstrip; |
53 | | unsigned *stripoffsets; |
54 | | unsigned *stripbytecounts; |
55 | | unsigned stripoffsetslen; |
56 | | unsigned stripbytecountslen; |
57 | | unsigned maxstrips; |
58 | | |
59 | | /* where we can find the tiles of image data */ |
60 | | unsigned tilelength; |
61 | | unsigned tilewidth; |
62 | | unsigned *tileoffsets; |
63 | | unsigned *tilebytecounts; |
64 | | unsigned tileoffsetslen; |
65 | | unsigned tilebytecountslen; |
66 | | unsigned maxtiles; |
67 | | |
68 | | /* colormap */ |
69 | | unsigned *colormap; |
70 | | unsigned colormaplen; |
71 | | unsigned maxcolors; |
72 | | |
73 | | /* assorted tags */ |
74 | | unsigned subfiletype; |
75 | | unsigned photometric; |
76 | | unsigned compression; |
77 | | unsigned imagewidth; |
78 | | unsigned imagelength; |
79 | | unsigned samplesperpixel; |
80 | | unsigned bitspersample; |
81 | | unsigned planar; |
82 | | unsigned extrasamples; |
83 | | unsigned xresolution; |
84 | | unsigned yresolution; |
85 | | unsigned resolutionunit; |
86 | | unsigned fillorder; |
87 | | unsigned g3opts; |
88 | | unsigned g4opts; |
89 | | unsigned predictor; |
90 | | |
91 | | unsigned ycbcrsubsamp[2]; |
92 | | |
93 | | const unsigned char *jpegtables; /* point into "file" buffer */ |
94 | | unsigned jpegtableslen; |
95 | | unsigned jpegofs; |
96 | | unsigned jpeglen; |
97 | | |
98 | | unsigned char *profile; |
99 | | int profilesize; |
100 | | |
101 | | /* decoded data */ |
102 | | fz_colorspace *colorspace; |
103 | | unsigned char *samples; |
104 | | unsigned char *data; |
105 | | unsigned tilestride; |
106 | | unsigned stride; |
107 | | }; |
108 | | |
109 | | enum |
110 | | { |
111 | | TII = 0x4949, /* 'II' */ |
112 | | TMM = 0x4d4d, /* 'MM' */ |
113 | | TBYTE = 1, |
114 | | TASCII = 2, |
115 | | TSHORT = 3, |
116 | | TLONG = 4, |
117 | | TRATIONAL = 5 |
118 | | }; |
119 | | |
120 | 0 | #define NewSubfileType 254 |
121 | 0 | #define ImageWidth 256 |
122 | 0 | #define ImageLength 257 |
123 | 0 | #define BitsPerSample 258 |
124 | 0 | #define Compression 259 |
125 | 0 | #define PhotometricInterpretation 262 |
126 | 0 | #define FillOrder 266 |
127 | 0 | #define StripOffsets 273 |
128 | 0 | #define SamplesPerPixel 277 |
129 | 0 | #define RowsPerStrip 278 |
130 | 0 | #define StripByteCounts 279 |
131 | 0 | #define XResolution 282 |
132 | 0 | #define YResolution 283 |
133 | 0 | #define PlanarConfiguration 284 |
134 | 0 | #define T4Options 292 |
135 | 0 | #define T6Options 293 |
136 | 0 | #define ResolutionUnit 296 |
137 | 0 | #define Predictor 317 |
138 | 0 | #define ColorMap 320 |
139 | 0 | #define TileWidth 322 |
140 | 0 | #define TileLength 323 |
141 | 0 | #define TileOffsets 324 |
142 | 0 | #define TileByteCounts 325 |
143 | 0 | #define ExtraSamples 338 |
144 | 0 | #define JPEGTables 347 |
145 | 0 | #define JPEGInterchangeFormat 513 |
146 | 0 | #define JPEGInterchangeFormatLength 514 |
147 | 0 | #define YCbCrSubSampling 530 |
148 | 0 | #define ICCProfile 34675 |
149 | | |
150 | | static const unsigned char bitrev[256] = |
151 | | { |
152 | | 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, |
153 | | 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, |
154 | | 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, |
155 | | 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, |
156 | | 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, |
157 | | 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, |
158 | | 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, |
159 | | 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, |
160 | | 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, |
161 | | 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, |
162 | | 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, |
163 | | 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, |
164 | | 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, |
165 | | 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, |
166 | | 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, |
167 | | 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, |
168 | | 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, |
169 | | 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, |
170 | | 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, |
171 | | 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, |
172 | | 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, |
173 | | 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, |
174 | | 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, |
175 | | 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, |
176 | | 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, |
177 | | 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, |
178 | | 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, |
179 | | 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, |
180 | | 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, |
181 | | 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, |
182 | | 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, |
183 | | 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff |
184 | | }; |
185 | | |
186 | | /* coverity[ +tainted_data_sanitize ] */ |
187 | | static inline int tiff_getcomp(unsigned char *line, int x, int bpc) |
188 | 0 | { |
189 | 0 | switch (bpc) |
190 | 0 | { |
191 | 0 | case 1: return (line[x >> 3] >> ( 7 - (x & 7) ) ) & 1; |
192 | 0 | case 2: return (line[x >> 2] >> ( ( 3 - (x & 3) ) << 1 ) ) & 3; |
193 | 0 | case 4: return (line[x >> 1] >> ( ( 1 - (x & 1) ) << 2 ) ) & 15; |
194 | 0 | case 8: return line[x]; |
195 | 0 | case 16: return line[x << 1] << 8 | line[(x << 1) + 1]; |
196 | 0 | } |
197 | 0 | return 0; |
198 | 0 | } |
199 | | |
200 | | static inline void tiff_putcomp(unsigned char *line, int x, int bpc, int value) |
201 | 0 | { |
202 | 0 | int maxval = (1 << bpc) - 1; |
203 | |
|
204 | 0 | switch (bpc) |
205 | 0 | { |
206 | 0 | case 1: line[x >> 3] &= ~(maxval << (7 - (x & 7))); break; |
207 | 0 | case 2: line[x >> 2] &= ~(maxval << ((3 - (x & 3)) << 1)); break; |
208 | 0 | case 4: line[x >> 1] &= ~(maxval << ((1 - (x & 1)) << 2)); break; |
209 | 0 | } |
210 | | |
211 | 0 | switch (bpc) |
212 | 0 | { |
213 | 0 | case 1: line[x >> 3] |= value << (7 - (x & 7)); break; |
214 | 0 | case 2: line[x >> 2] |= value << ((3 - (x & 3)) << 1); break; |
215 | 0 | case 4: line[x >> 1] |= value << ((1 - (x & 1)) << 2); break; |
216 | 0 | case 8: line[x] = value; break; |
217 | 0 | case 16: line[x << 1] = value >> 8; line[(x << 1) + 1] = value & 0xFF; break; |
218 | 0 | } |
219 | 0 | } |
220 | | |
221 | | static void |
222 | | tiff_unpredict_line(unsigned char *line, int width, int comps, int bits) |
223 | 0 | { |
224 | 0 | unsigned char left[FZ_MAX_COLORS]; |
225 | 0 | int i, k, v; |
226 | |
|
227 | 0 | for (k = 0; k < comps; k++) |
228 | 0 | left[k] = 0; |
229 | |
|
230 | 0 | for (i = 0; i < width; i++) |
231 | 0 | { |
232 | 0 | for (k = 0; k < comps; k++) |
233 | 0 | { |
234 | 0 | v = tiff_getcomp(line, i * comps + k, bits); |
235 | 0 | v = v + left[k]; |
236 | 0 | v = v % (1 << bits); |
237 | 0 | tiff_putcomp(line, i * comps + k, bits, v); |
238 | 0 | left[k] = v; |
239 | 0 | } |
240 | 0 | } |
241 | 0 | } |
242 | | |
243 | | static void |
244 | | tiff_invert_line(unsigned char *line, int width, int comps, int bits, int alpha) |
245 | 0 | { |
246 | 0 | int i, k, v; |
247 | 0 | int m = (1 << bits) - 1; |
248 | |
|
249 | 0 | for (i = 0; i < width; i++) |
250 | 0 | { |
251 | 0 | for (k = 0; k < comps; k++) |
252 | 0 | { |
253 | 0 | v = tiff_getcomp(line, i * comps + k, bits); |
254 | 0 | if (!alpha || k < comps - 1) |
255 | 0 | v = m - v; |
256 | 0 | tiff_putcomp(line, i * comps + k, bits, v); |
257 | 0 | } |
258 | 0 | } |
259 | 0 | } |
260 | | |
261 | | static void |
262 | | tiff_expand_colormap(fz_context *ctx, struct tiff *tiff) |
263 | 0 | { |
264 | 0 | int maxval = 1 << tiff->bitspersample; |
265 | 0 | unsigned char *samples; |
266 | 0 | unsigned char *src, *dst; |
267 | 0 | unsigned int x, y; |
268 | 0 | unsigned int stride; |
269 | 0 | unsigned int srcstride; |
270 | 0 | unsigned int size; |
271 | | |
272 | | /* colormap has first all red, then all green, then all blue values */ |
273 | | /* colormap values are 0..65535, bits is 4 or 8 */ |
274 | | /* image can be with or without extrasamples: comps is 1 or 2 */ |
275 | |
|
276 | 0 | if (!(tiff->samplesperpixel == 1 && tiff->extrasamples == 0) && !(tiff->samplesperpixel == 2 && tiff->extrasamples)) |
277 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid number of samples for RGBPal"); |
278 | | |
279 | 0 | if (tiff->bitspersample != 1 && tiff->bitspersample != 2 && tiff->bitspersample != 4 && tiff->bitspersample != 8 && tiff->bitspersample != 16) |
280 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid number of bits for RGBPal"); |
281 | | |
282 | 0 | if (tiff->colormaplen < (unsigned)maxval * 3) |
283 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "insufficient colormap data"); |
284 | | |
285 | 0 | if (fz_ckd_mul_uint(&srcstride, tiff->imagewidth, tiff->bitspersample * (1 + tiff->extrasamples))) |
286 | 0 | fz_throw(ctx, FZ_ERROR_LIMIT, "image too wide"); |
287 | 0 | srcstride = (srcstride + 7) / 8; |
288 | | |
289 | | /* tiff->stride >= 0 as unsigned */ |
290 | 0 | if (srcstride > (unsigned int)tiff->stride) |
291 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "insufficient data for format"); |
292 | | |
293 | | /* Multiplying by two at the end because each component value in the colormap is 16 bits wide. */ |
294 | 0 | if (fz_ckd_mul_uint(&stride, tiff->imagewidth, (3 + !!tiff->extrasamples) * 2)) |
295 | 0 | fz_throw(ctx, FZ_ERROR_LIMIT, "image too wide"); |
296 | | |
297 | 0 | if (fz_ckd_mul_uint(&size, stride, tiff->imagelength)) |
298 | 0 | fz_throw(ctx, FZ_ERROR_LIMIT, "image too large"); |
299 | | |
300 | 0 | samples = Memento_label(fz_malloc(ctx, size), "tiff_samples"); |
301 | |
|
302 | 0 | for (y = 0; y < tiff->imagelength; y++) |
303 | 0 | { |
304 | 0 | int s = 0; |
305 | 0 | src = tiff->samples + (unsigned int)(tiff->stride * y); |
306 | 0 | dst = samples + (unsigned int)(stride * y); |
307 | |
|
308 | 0 | for (x = 0; x < tiff->imagewidth; x++) |
309 | 0 | { |
310 | 0 | int c = tiff_getcomp(src, s++, tiff->bitspersample); |
311 | 0 | *dst++ = tiff->colormap[c + 0] >> 8; |
312 | 0 | *dst++ = tiff->colormap[c + 0]; |
313 | 0 | *dst++ = tiff->colormap[c + maxval] >> 8; |
314 | 0 | *dst++ = tiff->colormap[c + maxval]; |
315 | 0 | *dst++ = tiff->colormap[c + maxval * 2] >> 8; |
316 | 0 | *dst++ = tiff->colormap[c + maxval * 2]; |
317 | 0 | if (tiff->extrasamples) |
318 | 0 | { |
319 | | /* Assume the first is alpha, and skip the rest. */ |
320 | 0 | int a = tiff_getcomp(src, s++, tiff->bitspersample); |
321 | 0 | if (tiff->bitspersample <= 16) |
322 | 0 | a = a << (16 - tiff->bitspersample); |
323 | 0 | else |
324 | 0 | a = a >> (tiff->bitspersample - 16); |
325 | 0 | *dst++ = a >> 8; |
326 | 0 | *dst++ = a; |
327 | 0 | s += tiff->extrasamples-1; |
328 | 0 | } |
329 | 0 | } |
330 | 0 | } |
331 | |
|
332 | 0 | tiff->samplesperpixel += 2; |
333 | 0 | tiff->bitspersample = 16; |
334 | 0 | tiff->stride = stride; |
335 | 0 | fz_free(ctx, tiff->samples); |
336 | 0 | tiff->samples = samples; |
337 | 0 | } |
338 | | |
339 | | static unsigned |
340 | | tiff_decode_data(fz_context *ctx, struct tiff *tiff, const unsigned char *rp, unsigned int rlen, unsigned char *wp, unsigned int wlen) |
341 | 0 | { |
342 | 0 | fz_stream *encstm = NULL; |
343 | 0 | fz_stream *stm = NULL; |
344 | 0 | unsigned i, size = 0; |
345 | 0 | unsigned char *reversed = NULL; |
346 | 0 | fz_stream *jpegtables = NULL; |
347 | 0 | int old_tiff; |
348 | |
|
349 | 0 | if (rp + rlen > tiff->ep) |
350 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "strip extends beyond the end of the file"); |
351 | | |
352 | | /* the bits are in un-natural order */ |
353 | 0 | if (tiff->fillorder == 2) |
354 | 0 | { |
355 | 0 | reversed = fz_malloc(ctx, rlen); |
356 | 0 | for (i = 0; i < rlen; i++) |
357 | 0 | reversed[i] = bitrev[rp[i]]; |
358 | 0 | rp = reversed; |
359 | 0 | } |
360 | |
|
361 | 0 | fz_var(jpegtables); |
362 | 0 | fz_var(encstm); |
363 | 0 | fz_var(stm); |
364 | |
|
365 | 0 | fz_try(ctx) |
366 | 0 | { |
367 | 0 | encstm = fz_open_memory(ctx, rp, rlen); |
368 | | |
369 | | /* switch on compression to create a filter */ |
370 | | /* feed each chunk (strip or tile) to the filter */ |
371 | | /* read out the data into a buffer */ |
372 | | /* the level above packs the chunk's samples into a pixmap */ |
373 | | |
374 | | /* type 32773 / packbits -- nothing special (same row-padding as PDF) */ |
375 | | /* type 2 / ccitt rle -- no EOL, no RTC, rows are byte-aligned */ |
376 | | /* type 3 and 4 / g3 and g4 -- each strip starts new section */ |
377 | | /* type 5 / lzw -- each strip is handled separately */ |
378 | |
|
379 | 0 | switch (tiff->compression) |
380 | 0 | { |
381 | 0 | case 1: |
382 | | /* stm already open and reading uncompressed data */ |
383 | 0 | stm = fz_keep_stream(ctx, encstm); |
384 | 0 | break; |
385 | 0 | case 2: |
386 | 0 | case 3: |
387 | 0 | case 4: |
388 | 0 | stm = fz_open_faxd(ctx, encstm, |
389 | 0 | tiff->compression == 4 ? -1 : |
390 | 0 | tiff->compression == 2 ? 0 : |
391 | 0 | (int) (tiff->g3opts & 1), |
392 | 0 | 0, |
393 | 0 | tiff->compression == 2, |
394 | 0 | tiff->imagewidth, |
395 | 0 | tiff->imagelength, |
396 | 0 | 0, |
397 | 0 | 1); |
398 | 0 | break; |
399 | 0 | case 5: |
400 | 0 | old_tiff = rp[0] == 0 && (rp[1] & 1); |
401 | 0 | stm = fz_open_lzwd(ctx, encstm, old_tiff ? 0 : 1, 9, old_tiff ? 1 : 0, old_tiff); |
402 | 0 | break; |
403 | 0 | case 6: |
404 | 0 | fz_warn(ctx, "deprecated JPEG in TIFF compression not fully supported"); |
405 | | /* fall through */ |
406 | 0 | case 7: |
407 | 0 | if (tiff->jpegtables && (int)tiff->jpegtableslen > 0) |
408 | 0 | jpegtables = fz_open_memory(ctx, tiff->jpegtables, tiff->jpegtableslen); |
409 | |
|
410 | 0 | stm = fz_open_dctd(ctx, encstm, |
411 | 0 | tiff->photometric == 2 || tiff->photometric == 3 ? 0 : -1, |
412 | 0 | 1, |
413 | 0 | 0, |
414 | 0 | jpegtables); |
415 | 0 | break; |
416 | 0 | case 8: |
417 | 0 | case 32946: |
418 | 0 | stm = fz_open_flated(ctx, encstm, 15); |
419 | 0 | break; |
420 | 0 | case 32773: |
421 | 0 | stm = fz_open_rld(ctx, encstm); |
422 | 0 | break; |
423 | 0 | case 34676: |
424 | 0 | if (tiff->photometric == 32845) |
425 | 0 | stm = fz_open_sgilog32(ctx, encstm, tiff->imagewidth); |
426 | 0 | else |
427 | 0 | stm = fz_open_sgilog16(ctx, encstm, tiff->imagewidth); |
428 | 0 | break; |
429 | 0 | case 34677: |
430 | 0 | stm = fz_open_sgilog24(ctx, encstm, tiff->imagewidth); |
431 | 0 | break; |
432 | 0 | case 32809: |
433 | 0 | if (tiff->bitspersample != 4) |
434 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid bits per pixel in thunder encoding"); |
435 | 0 | stm = fz_open_thunder(ctx, encstm, tiff->imagewidth); |
436 | 0 | break; |
437 | 0 | default: |
438 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "unknown TIFF compression: %d", tiff->compression); |
439 | 0 | } |
440 | | |
441 | 0 | size = (unsigned)fz_read(ctx, stm, wp, wlen); |
442 | 0 | } |
443 | 0 | fz_always(ctx) |
444 | 0 | { |
445 | 0 | fz_drop_stream(ctx, jpegtables); |
446 | 0 | fz_drop_stream(ctx, encstm); |
447 | 0 | fz_drop_stream(ctx, stm); |
448 | 0 | fz_free(ctx, reversed); |
449 | 0 | } |
450 | 0 | fz_catch(ctx) |
451 | 0 | fz_rethrow(ctx); |
452 | | |
453 | 0 | return size; |
454 | 0 | } |
455 | | |
456 | | static void |
457 | | tiff_paste_tile(fz_context *ctx, struct tiff *tiff, unsigned char *tile, unsigned int row, unsigned int col) |
458 | 0 | { |
459 | 0 | unsigned int x, y, k; |
460 | |
|
461 | 0 | for (y = 0; y < tiff->tilelength && row + y < tiff->imagelength; y++) |
462 | 0 | { |
463 | 0 | for (x = 0; x < tiff->tilewidth && col + x < tiff->imagewidth; x++) |
464 | 0 | { |
465 | 0 | for (k = 0; k < tiff->samplesperpixel; k++) |
466 | 0 | { |
467 | 0 | unsigned char *dst, *src; |
468 | 0 | unsigned shift; |
469 | |
|
470 | 0 | dst = tiff->samples; |
471 | 0 | dst += (row + y) * tiff->stride; |
472 | 0 | dst += (((col + x) * tiff->samplesperpixel + k) * tiff->bitspersample) / 8; |
473 | 0 | shift = 8 - ((((col + x) * tiff->samplesperpixel + k) * tiff->bitspersample) % 8) - tiff->bitspersample; |
474 | |
|
475 | 0 | src = tile; |
476 | 0 | src += y * tiff->tilestride; |
477 | 0 | src += ((x * tiff->samplesperpixel + k) * tiff->bitspersample) / 8; |
478 | |
|
479 | 0 | switch (tiff->bitspersample) |
480 | 0 | { |
481 | 0 | case 1: *dst |= ((*src >> (7 - 1 * ((col + x) % 8))) & 0x1) << shift; break; |
482 | 0 | case 2: *dst |= ((*src >> (6 - 2 * ((col + x) % 4))) & 0x3) << shift; break; |
483 | 0 | case 4: *dst |= ((*src >> (4 - 4 * ((col + x) % 2))) & 0xf) << shift; break; |
484 | 0 | case 8: *dst = *src; break; |
485 | 0 | case 16: dst[0] = src[0]; dst[1] = src[1]; break; |
486 | 0 | case 24: dst[0] = src[0]; dst[1] = src[1]; dst[2] = src[2]; break; |
487 | 0 | case 32: dst[0] = src[0]; dst[1] = src[1]; dst[2] = src[2]; dst[3] = src[3]; break; |
488 | 0 | } |
489 | 0 | } |
490 | 0 | } |
491 | 0 | } |
492 | 0 | } |
493 | | |
494 | | static void |
495 | | tiff_paste_subsampled_tile(fz_context *ctx, struct tiff *tiff, unsigned char *tile, unsigned len, unsigned tw, unsigned th, unsigned row, unsigned col) |
496 | 0 | { |
497 | | /* |
498 | | This explains how the samples are laid out in tiff data, the spec example is non-obvious. |
499 | | The y, cb, cr indices follow the spec, i.e. y17 is the y sample at row 1, column 7. |
500 | | All indices start at 0. |
501 | | |
502 | | hexlookup = (horizontalsubsampling & 0xf) << 4 | (verticalsubsampling & 0xf) |
503 | | |
504 | | 0x11 y00 cb00 cr00 0x21 y00 y01 cb00 cr00 0x41 y00 y01 y02 y03 cb00 cr00 |
505 | | y01 cb01 cr01 y10 y11 cb01 cr01 y04 y05 y06 y07 cb01 cr01 |
506 | | .... ... ... |
507 | | y10 cb10 cr10 y20 y21 cb10 cr10 y10 y11 y12 y13 cb10 cr10 |
508 | | y11 cb11 cr11 y30 y31 cb11 cr11 y14 y15 y16 y17 cb11 cr11 |
509 | | |
510 | | 0x12 y00 0x22 y00 y01 0x42 y00 y01 y02 y03 |
511 | | y10 cb00 cr00 y10 y11 cb00 cr00 y10 y11 y12 y13 cb00 cr00 |
512 | | y01 y02 y03 y04 y05 y06 y07 |
513 | | y11 cb01 cr01 y12 y13 cb01 cr01 y14 y15 y16 y17 cb01 cr01 |
514 | | .... ... ... |
515 | | y20 y20 y21 y20 y21 y22 y23 |
516 | | y30 cb10 cr10 y30 y31 cb10 cr10 y30 y31 y32 y33 cb10 cr10 |
517 | | y21 y22 y23 y24 y25 y26 y27 |
518 | | y31 cb11 cr11 y32 y33 cb11 cr11 y34 y35 y36 y37 cb11 cr11 |
519 | | |
520 | | 0x14 y00 0x24 y00 y01 0x44 y00 y01 y02 y03 |
521 | | y10 y10 y11 y10 y11 y12 y13 |
522 | | y20 y20 y21 y20 y21 y22 y23 |
523 | | y30 cb00 cr00 y30 y31 cb00 cr00 y30 y31 y32 y33 cb00 cr00 |
524 | | y01 y02 y03 y04 y05 y06 y07 |
525 | | y11 y12 y13 y14 y15 y16 y17 |
526 | | y21 y22 y23 y24 y25 y26 y27 |
527 | | y31 cb01 cr01 y32 y33 cb01 cr01 y34 y35 y36 y37 cb01 cr01 |
528 | | .... ... ... |
529 | | y40 y40 y41 y40 y41 y42 y43 |
530 | | y50 y50 y51 y50 y51 y52 y53 |
531 | | y60 y60 y61 y60 y61 y62 y63 |
532 | | y70 cb10 cr10 y70 y71 cb10 cr10 y70 y71 y72 y73 cb10 cr10 |
533 | | y41 y42 y43 y44 y45 y46 y47 |
534 | | y51 y52 y53 y54 y55 y56 y57 |
535 | | y61 y62 y63 y64 y65 y66 y67 |
536 | | y71 cb11 cr11 y72 y73 cb11 cr11 y74 y75 y76 y77 cb11 cr11 |
537 | | */ |
538 | |
|
539 | 0 | unsigned char *src = tile; |
540 | 0 | unsigned char *dst; |
541 | 0 | unsigned x, y, w, h; /* coordinates and dimensions of entire image */ |
542 | 0 | unsigned sx, sy, sw, sh; /* coordinates and dimensions of a single subsample region, i.e. max 4 x 4 samples */ |
543 | 0 | int k; |
544 | 0 | int offsets[4 * 4 * 3]; /* for a pixel position, these point to all pixel components in a subsample region */ |
545 | 0 | int *offset = offsets; |
546 | |
|
547 | 0 | assert(tiff->samplesperpixel == 3); |
548 | 0 | assert(tiff->bitspersample == 8); |
549 | |
|
550 | 0 | w = tiff->imagewidth; |
551 | 0 | h = tiff->imagelength; |
552 | |
|
553 | 0 | sx = 0; |
554 | 0 | sy = 0; |
555 | 0 | sw = tiff->ycbcrsubsamp[0]; |
556 | 0 | sh = tiff->ycbcrsubsamp[1]; |
557 | 0 | if (sw > 4 || sh > 4 || !fz_is_pow2(sw) || !fz_is_pow2(sh)) |
558 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "Illegal TIFF Subsample values %d %d", sw, sh); |
559 | | |
560 | 0 | for (k = 0; k < 3; k++) |
561 | 0 | for (y = 0; y < sh; y++) |
562 | 0 | for (x = 0; x < sw; x++) |
563 | 0 | *offset++ = k + y * tiff->stride + x * 3; |
564 | |
|
565 | 0 | offset = offsets; |
566 | 0 | x = col; |
567 | 0 | y = row; |
568 | 0 | k = 0; |
569 | |
|
570 | 0 | dst = &tiff->samples[row * tiff->stride + col * 3]; |
571 | |
|
572 | 0 | while (src < tile + len) |
573 | 0 | { |
574 | 0 | if (k == 0) |
575 | 0 | { /* put all Y samples for a subsample region at the correct image pixel */ |
576 | 0 | if (y + sy < h && y + sy < row + th && x + sx < w && x + sx < col + tw) |
577 | 0 | dst[*offset] = *src; |
578 | 0 | offset++; |
579 | |
|
580 | 0 | if (++sx >= sw) |
581 | 0 | { |
582 | 0 | sx = 0; |
583 | 0 | if (++sy >= sh) |
584 | 0 | { |
585 | 0 | sy = 0; |
586 | 0 | k++; |
587 | 0 | } |
588 | 0 | } |
589 | 0 | } |
590 | 0 | else |
591 | 0 | { /* put all Cb/Cr samples for a subsample region at the correct image pixel */ |
592 | 0 | for (sy = 0; sy < sh; sy++) |
593 | 0 | for (sx = 0; sx < sw; sx++) |
594 | 0 | { |
595 | 0 | if (y + sy < h && y + sy < row + th && x + sx < w && x + sx < col + tw) |
596 | 0 | dst[*offset] = *src; |
597 | 0 | offset++; |
598 | 0 | } |
599 | |
|
600 | 0 | if (++k >= 3) |
601 | 0 | { /* we're done with this subsample region, on to the next one */ |
602 | 0 | k = sx = sy = 0; |
603 | 0 | offset = offsets; |
604 | |
|
605 | 0 | x += sw; |
606 | 0 | if (x < w) |
607 | 0 | dst += sw * 3; |
608 | |
|
609 | 0 | if (x >= col + tw) |
610 | 0 | { |
611 | 0 | x = col; |
612 | 0 | y += sh; |
613 | 0 | dst = &tiff->samples[y * tiff->stride + x * 3]; |
614 | 0 | } |
615 | 0 | } |
616 | 0 | } |
617 | |
|
618 | 0 | src++; |
619 | 0 | } |
620 | 0 | } |
621 | | |
622 | | static void |
623 | | tiff_decode_tiles(fz_context *ctx, struct tiff *tiff) |
624 | 0 | { |
625 | 0 | unsigned char *data; |
626 | 0 | unsigned x, y, wlen, tile; |
627 | 0 | unsigned tiles, tilesacross, tilesdown; |
628 | |
|
629 | 0 | tilesdown = (tiff->imagelength + tiff->tilelength - 1) / tiff->tilelength; |
630 | 0 | tilesacross = (tiff->imagewidth + tiff->tilewidth - 1) / tiff->tilewidth; |
631 | 0 | tiles = tilesacross * tilesdown; |
632 | 0 | if (tiff->tileoffsetslen < tiles || tiff->tilebytecountslen < tiles) |
633 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "insufficient tile metadata"); |
634 | | |
635 | | /* JPEG can handle subsampling on its own */ |
636 | 0 | if (tiff->photometric == 6 && tiff->compression != 6 && tiff->compression != 7) |
637 | 0 | { |
638 | | /* regardless of how this is subsampled, a tile is never larger */ |
639 | 0 | if (tiff->tilelength >= tiff->ycbcrsubsamp[1]) |
640 | 0 | { |
641 | 0 | if (fz_ckd_mul_uint(&wlen, tiff->tilestride, tiff->tilelength)) |
642 | 0 | fz_throw(ctx, FZ_ERROR_LIMIT, "tile buffer too large"); |
643 | 0 | } |
644 | 0 | else |
645 | 0 | { |
646 | 0 | if (fz_ckd_mul_uint(&wlen, tiff->tilestride, tiff->ycbcrsubsamp[1])) |
647 | 0 | fz_throw(ctx, FZ_ERROR_LIMIT, "tile buffer too large"); |
648 | 0 | } |
649 | | |
650 | 0 | data = tiff->data = Memento_label(fz_malloc(ctx, wlen), "tiff_tile_jpg"); |
651 | |
|
652 | 0 | tile = 0; |
653 | 0 | for (y = 0; y < tiff->imagelength; y += tiff->tilelength) |
654 | 0 | { |
655 | 0 | for (x = 0; x < tiff->imagewidth; x += tiff->tilewidth) |
656 | 0 | { |
657 | 0 | unsigned int offset = tiff->tileoffsets[tile]; |
658 | 0 | unsigned int rlen = tiff->tilebytecounts[tile]; |
659 | 0 | const unsigned char *rp = tiff->bp + offset; |
660 | 0 | unsigned decoded; |
661 | |
|
662 | 0 | if (offset > (unsigned)(tiff->ep - tiff->bp)) |
663 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid tile offset %u", offset); |
664 | 0 | if (rlen > (unsigned)(tiff->ep - rp)) |
665 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid tile byte count %u", rlen); |
666 | 0 | if (rlen == 0) |
667 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "tile byte count zero"); |
668 | | |
669 | 0 | decoded = tiff_decode_data(ctx, tiff, rp, rlen, data, wlen); |
670 | 0 | tiff_paste_subsampled_tile(ctx, tiff, data, decoded, tiff->tilewidth, tiff->tilelength, y, x); |
671 | 0 | tile++; |
672 | 0 | } |
673 | 0 | } |
674 | 0 | } |
675 | 0 | else |
676 | 0 | { |
677 | 0 | if (fz_ckd_mul_uint(&wlen, tiff->tilelength, tiff->tilestride)) |
678 | 0 | fz_throw(ctx, FZ_ERROR_LIMIT, "tile buffer too large"); |
679 | 0 | data = tiff->data = Memento_label(fz_malloc(ctx, wlen), "tiff_tile"); |
680 | |
|
681 | 0 | tile = 0; |
682 | 0 | for (y = 0; y < tiff->imagelength; y += tiff->tilelength) |
683 | 0 | { |
684 | 0 | for (x = 0; x < tiff->imagewidth; x += tiff->tilewidth) |
685 | 0 | { |
686 | 0 | unsigned int offset = tiff->tileoffsets[tile]; |
687 | 0 | unsigned int rlen = tiff->tilebytecounts[tile]; |
688 | 0 | const unsigned char *rp = tiff->bp + offset; |
689 | |
|
690 | 0 | if (offset > (unsigned)(tiff->ep - tiff->bp)) |
691 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid tile offset %u", offset); |
692 | 0 | if (rlen > (unsigned)(tiff->ep - rp)) |
693 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid tile byte count %u", rlen); |
694 | 0 | if (rlen == 0) |
695 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "tile byte count zero"); |
696 | | |
697 | 0 | if (tiff_decode_data(ctx, tiff, rp, rlen, data, wlen) != wlen) |
698 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "decoded tile is the wrong size"); |
699 | | |
700 | 0 | tiff_paste_tile(ctx, tiff, data, y, x); |
701 | 0 | tile++; |
702 | 0 | } |
703 | 0 | } |
704 | 0 | } |
705 | 0 | } |
706 | | |
707 | | static void |
708 | | tiff_decode_strips(fz_context *ctx, struct tiff *tiff) |
709 | 0 | { |
710 | 0 | unsigned char *data; |
711 | 0 | unsigned strips; |
712 | 0 | unsigned strip; |
713 | 0 | unsigned y; |
714 | |
|
715 | 0 | strips = (tiff->imagelength + tiff->rowsperstrip - 1) / tiff->rowsperstrip; |
716 | 0 | if (tiff->stripoffsetslen < strips || tiff->stripbytecountslen < strips) |
717 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "insufficient strip metadata"); |
718 | | |
719 | 0 | data = tiff->samples; |
720 | | |
721 | | /* JPEG can handle subsampling on its own */ |
722 | 0 | if (tiff->photometric == 6 && tiff->compression != 6 && tiff->compression != 7) |
723 | 0 | { |
724 | 0 | unsigned wlen; |
725 | 0 | unsigned rowsperstrip; |
726 | | |
727 | | /* regardless of how this is subsampled, a strip is never taller */ |
728 | 0 | if (tiff->rowsperstrip >= tiff->ycbcrsubsamp[1]) |
729 | 0 | rowsperstrip = tiff->rowsperstrip; |
730 | 0 | else |
731 | 0 | rowsperstrip = tiff->ycbcrsubsamp[1]; |
732 | |
|
733 | 0 | wlen = rowsperstrip * tiff->stride; |
734 | 0 | data = tiff->data = Memento_label(fz_malloc(ctx, wlen), "tiff_strip_jpg"); |
735 | |
|
736 | 0 | strip = 0; |
737 | 0 | for (y = 0; y < tiff->imagelength; y += rowsperstrip) |
738 | 0 | { |
739 | 0 | unsigned offset = tiff->stripoffsets[strip]; |
740 | 0 | unsigned rlen = tiff->stripbytecounts[strip]; |
741 | 0 | const unsigned char *rp = tiff->bp + offset; |
742 | 0 | int decoded; |
743 | |
|
744 | 0 | if (offset > (unsigned)(tiff->ep - tiff->bp)) |
745 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid strip offset %u", offset); |
746 | 0 | if (rlen > (unsigned)(tiff->ep - rp)) |
747 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid strip byte count %u", rlen); |
748 | 0 | if (rlen == 0) |
749 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "strip byte count zero"); |
750 | | |
751 | 0 | decoded = tiff_decode_data(ctx, tiff, rp, rlen, data, wlen); |
752 | 0 | tiff_paste_subsampled_tile(ctx, tiff, data, decoded, tiff->imagewidth, tiff->rowsperstrip, y, 0); |
753 | 0 | strip++; |
754 | 0 | } |
755 | 0 | } |
756 | 0 | else |
757 | 0 | { |
758 | 0 | strip = 0; |
759 | 0 | for (y = 0; y < tiff->imagelength; y += tiff->rowsperstrip) |
760 | 0 | { |
761 | 0 | unsigned offset = tiff->stripoffsets[strip]; |
762 | 0 | unsigned rlen = tiff->stripbytecounts[strip]; |
763 | 0 | unsigned wlen = tiff->stride * tiff->rowsperstrip; |
764 | 0 | const unsigned char *rp = tiff->bp + offset; |
765 | |
|
766 | 0 | if (offset > (unsigned)(tiff->ep - tiff->bp)) |
767 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid strip offset %u", offset); |
768 | 0 | if (rlen > (unsigned)(tiff->ep - rp)) |
769 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid strip byte count %u", rlen); |
770 | 0 | if (rlen == 0) |
771 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "strip byte count zero"); |
772 | | |
773 | | /* if imagelength is not a multiple of rowsperstrip, adjust the expectation of the size of the decoded data */ |
774 | 0 | if (y + tiff->rowsperstrip >= tiff->imagelength) |
775 | 0 | wlen = tiff->stride * (tiff->imagelength - y); |
776 | |
|
777 | 0 | if (tiff_decode_data(ctx, tiff, rp, rlen, data, wlen) < wlen) |
778 | 0 | { |
779 | 0 | fz_warn(ctx, "premature end of data in decoded strip"); |
780 | 0 | break; |
781 | 0 | } |
782 | | |
783 | 0 | data += wlen; |
784 | 0 | strip ++; |
785 | 0 | } |
786 | 0 | } |
787 | 0 | } |
788 | | |
789 | | static inline int tiff_readbyte(struct tiff *tiff) |
790 | 0 | { |
791 | 0 | if (tiff->rp < tiff->ep) |
792 | 0 | return *tiff->rp++; |
793 | 0 | return EOF; |
794 | 0 | } |
795 | | |
796 | | static inline unsigned readshort(struct tiff *tiff) |
797 | 0 | { |
798 | 0 | int a = tiff_readbyte(tiff); |
799 | 0 | int b = tiff_readbyte(tiff); |
800 | 0 | if (tiff->order == TII) |
801 | 0 | return (unsigned)((b << 8) | a); |
802 | 0 | return (unsigned)((a << 8) | b); |
803 | 0 | } |
804 | | |
805 | | static inline unsigned tiff_readlong(struct tiff *tiff) |
806 | 0 | { |
807 | 0 | unsigned int a = tiff_readbyte(tiff); |
808 | 0 | unsigned int b = tiff_readbyte(tiff); |
809 | 0 | unsigned int c = tiff_readbyte(tiff); |
810 | 0 | unsigned int d = tiff_readbyte(tiff); |
811 | 0 | if (tiff->order == TII) |
812 | 0 | return (d << 24) | (c << 16) | (b << 8) | a; |
813 | 0 | return (a << 24) | (b << 16) | (c << 8) | d; |
814 | 0 | } |
815 | | |
816 | | static void |
817 | | tiff_read_bytes(unsigned char *p, struct tiff *tiff, unsigned ofs, unsigned n) |
818 | 0 | { |
819 | 0 | if (ofs > (unsigned)(tiff->ep - tiff->bp)) |
820 | 0 | ofs = (unsigned)(tiff->ep - tiff->bp); |
821 | 0 | tiff->rp = tiff->bp + ofs; |
822 | |
|
823 | 0 | while (n--) |
824 | 0 | *p++ = tiff_readbyte(tiff); |
825 | 0 | } |
826 | | |
827 | | static void |
828 | | tiff_read_tag_value(fz_context *ctx, unsigned *p, struct tiff *tiff, unsigned type, unsigned ofs, unsigned n) |
829 | 0 | { |
830 | 0 | unsigned den; |
831 | 0 | unsigned divisor; |
832 | |
|
833 | 0 | if (ofs > (unsigned)(tiff->ep - tiff->bp)) |
834 | 0 | { |
835 | 0 | ofs = (unsigned)(tiff->ep - tiff->bp); |
836 | 0 | fz_warn(ctx, "TIFF tag offset beyond end of file, truncating offset"); |
837 | 0 | } |
838 | 0 | tiff->rp = tiff->bp + ofs; |
839 | |
|
840 | 0 | switch (type) |
841 | 0 | { |
842 | 0 | default: |
843 | 0 | case TBYTE: divisor = 1; break; |
844 | 0 | case TSHORT: divisor = 2; break; |
845 | 0 | case TLONG: divisor = 4; break; |
846 | 0 | case TRATIONAL: divisor = 8; break; |
847 | 0 | } |
848 | | |
849 | 0 | if (n > (tiff->ep - tiff->rp) / divisor) |
850 | 0 | { |
851 | 0 | unsigned newn = (tiff->ep - tiff->rp) / divisor; |
852 | 0 | memset(&p[newn], 0, (n - newn) * sizeof (unsigned)); |
853 | 0 | fz_warn(ctx, "TIFF tag extends beyond end of file, truncating tag"); |
854 | 0 | n = newn; |
855 | 0 | } |
856 | |
|
857 | 0 | while (n--) |
858 | 0 | { |
859 | 0 | switch (type) |
860 | 0 | { |
861 | 0 | case TRATIONAL: |
862 | 0 | *p = tiff_readlong(tiff); |
863 | 0 | den = tiff_readlong(tiff); |
864 | 0 | if (den) |
865 | 0 | *p = *p / den; |
866 | 0 | else |
867 | 0 | *p = UINT_MAX; |
868 | 0 | p ++; |
869 | 0 | break; |
870 | 0 | case TBYTE: *p++ = tiff_readbyte(tiff); break; |
871 | 0 | case TSHORT: *p++ = readshort(tiff); break; |
872 | 0 | case TLONG: *p++ = tiff_readlong(tiff); break; |
873 | 0 | default: *p++ = 0; break; |
874 | 0 | } |
875 | 0 | } |
876 | 0 | } |
877 | | |
878 | | static void |
879 | | tiff_read_tag(fz_context *ctx, struct tiff *tiff, unsigned offset) |
880 | 0 | { |
881 | 0 | unsigned tag; |
882 | 0 | unsigned type; |
883 | 0 | unsigned count; |
884 | 0 | unsigned value; |
885 | |
|
886 | 0 | tiff->rp = tiff->bp + offset; |
887 | |
|
888 | 0 | tag = readshort(tiff); |
889 | 0 | type = readshort(tiff); |
890 | 0 | count = tiff_readlong(tiff); |
891 | |
|
892 | 0 | if ((type == TBYTE && count <= 4) || |
893 | 0 | (type == TSHORT && count <= 2) || |
894 | 0 | (type == TLONG && count <= 1)) |
895 | 0 | value = tiff->rp - tiff->bp; |
896 | 0 | else |
897 | 0 | value = tiff_readlong(tiff); |
898 | |
|
899 | 0 | switch (tag) |
900 | 0 | { |
901 | 0 | case NewSubfileType: |
902 | 0 | tiff_read_tag_value(ctx, &tiff->subfiletype, tiff, type, value, 1); |
903 | 0 | break; |
904 | 0 | case ImageWidth: |
905 | 0 | tiff_read_tag_value(ctx, &tiff->imagewidth, tiff, type, value, 1); |
906 | 0 | break; |
907 | 0 | case ImageLength: |
908 | 0 | tiff_read_tag_value(ctx, &tiff->imagelength, tiff, type, value, 1); |
909 | 0 | break; |
910 | 0 | case BitsPerSample: |
911 | 0 | tiff_read_tag_value(ctx, &tiff->bitspersample, tiff, type, value, 1); |
912 | 0 | break; |
913 | 0 | case Compression: |
914 | 0 | tiff_read_tag_value(ctx, &tiff->compression, tiff, type, value, 1); |
915 | 0 | break; |
916 | 0 | case PhotometricInterpretation: |
917 | 0 | tiff_read_tag_value(ctx, &tiff->photometric, tiff, type, value, 1); |
918 | 0 | break; |
919 | 0 | case FillOrder: |
920 | 0 | tiff_read_tag_value(ctx, &tiff->fillorder, tiff, type, value, 1); |
921 | 0 | break; |
922 | 0 | case StripOffsets: |
923 | 0 | tiff->stripoffsetslen = count; |
924 | 0 | break; |
925 | 0 | case SamplesPerPixel: |
926 | 0 | tiff_read_tag_value(ctx, &tiff->samplesperpixel, tiff, type, value, 1); |
927 | 0 | break; |
928 | 0 | case RowsPerStrip: |
929 | 0 | tiff_read_tag_value(ctx, &tiff->rowsperstrip, tiff, type, value, 1); |
930 | 0 | break; |
931 | 0 | case StripByteCounts: |
932 | 0 | tiff->stripbytecountslen = count; |
933 | 0 | break; |
934 | 0 | case XResolution: |
935 | 0 | tiff_read_tag_value(ctx, &tiff->xresolution, tiff, type, value, 1); |
936 | 0 | break; |
937 | 0 | case YResolution: |
938 | 0 | tiff_read_tag_value(ctx, &tiff->yresolution, tiff, type, value, 1); |
939 | 0 | break; |
940 | 0 | case PlanarConfiguration: |
941 | 0 | tiff_read_tag_value(ctx, &tiff->planar, tiff, type, value, 1); |
942 | 0 | break; |
943 | 0 | case T4Options: |
944 | 0 | tiff_read_tag_value(ctx, &tiff->g3opts, tiff, type, value, 1); |
945 | 0 | break; |
946 | 0 | case T6Options: |
947 | 0 | tiff_read_tag_value(ctx, &tiff->g4opts, tiff, type, value, 1); |
948 | 0 | break; |
949 | 0 | case ResolutionUnit: |
950 | 0 | tiff_read_tag_value(ctx, &tiff->resolutionunit, tiff, type, value, 1); |
951 | 0 | break; |
952 | 0 | case Predictor: |
953 | 0 | tiff_read_tag_value(ctx, &tiff->predictor, tiff, type, value, 1); |
954 | 0 | break; |
955 | 0 | case ColorMap: |
956 | 0 | tiff->colormaplen = count; |
957 | 0 | break; |
958 | 0 | case TileWidth: |
959 | 0 | tiff_read_tag_value(ctx, &tiff->tilewidth, tiff, type, value, 1); |
960 | 0 | break; |
961 | 0 | case TileLength: |
962 | 0 | tiff_read_tag_value(ctx, &tiff->tilelength, tiff, type, value, 1); |
963 | 0 | break; |
964 | 0 | case TileOffsets: |
965 | 0 | tiff->tileoffsetslen = count; |
966 | 0 | break; |
967 | 0 | case TileByteCounts: |
968 | 0 | tiff->tilebytecountslen = count; |
969 | 0 | break; |
970 | 0 | case ExtraSamples: |
971 | 0 | tiff_read_tag_value(ctx, &tiff->extrasamples, tiff, type, value, 1); |
972 | 0 | break; |
973 | 0 | case JPEGTables: |
974 | 0 | tiff->jpegtableslen = count; |
975 | 0 | break; |
976 | 0 | case YCbCrSubSampling: |
977 | 0 | tiff_read_tag_value(ctx, tiff->ycbcrsubsamp, tiff, type, value, 2); |
978 | 0 | break; |
979 | 0 | case ICCProfile: |
980 | 0 | tiff->profilesize = count; |
981 | 0 | break; |
982 | 0 | case JPEGInterchangeFormat: |
983 | 0 | tiff_read_tag_value(ctx, &tiff->jpegofs, tiff, type, value, 1); |
984 | 0 | break; |
985 | 0 | case JPEGInterchangeFormatLength: |
986 | 0 | tiff_read_tag_value(ctx, &tiff->jpeglen, tiff, type, value, 1); |
987 | 0 | break; |
988 | 0 | default: |
989 | | /* fz_warn(ctx, "unknown tag: %d t=%d n=%d", tag, type, count); */ |
990 | 0 | break; |
991 | 0 | } |
992 | 0 | } |
993 | | |
994 | | static void |
995 | | tiff_read_tag_array(fz_context *ctx, struct tiff *tiff, unsigned offset) |
996 | 0 | { |
997 | 0 | unsigned tag; |
998 | 0 | unsigned type; |
999 | 0 | unsigned count; |
1000 | 0 | unsigned value; |
1001 | |
|
1002 | 0 | tiff->rp = tiff->bp + offset; |
1003 | |
|
1004 | 0 | tag = readshort(tiff); |
1005 | 0 | type = readshort(tiff); |
1006 | 0 | count = tiff_readlong(tiff); |
1007 | |
|
1008 | 0 | if ((type == TBYTE && count <= 4) || |
1009 | 0 | (type == TSHORT && count <= 2) || |
1010 | 0 | (type == TLONG && count <= 1)) |
1011 | 0 | value = tiff->rp - tiff->bp; |
1012 | 0 | else |
1013 | 0 | value = tiff_readlong(tiff); |
1014 | |
|
1015 | 0 | switch (tag) |
1016 | 0 | { |
1017 | 0 | case StripOffsets: |
1018 | 0 | if (tiff->stripoffsets) |
1019 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "at most one strip offsets tag allowed"); |
1020 | 0 | if (tiff->rowsperstrip == 0) |
1021 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid strip dimensions"); |
1022 | 0 | if (count > tiff->maxstrips) |
1023 | 0 | count = tiff->maxstrips; |
1024 | 0 | tiff->stripoffsets = Memento_label(fz_malloc_array(ctx, count, unsigned), "tiff_stripoffsets"); |
1025 | 0 | tiff_read_tag_value(ctx, tiff->stripoffsets, tiff, type, value, count); |
1026 | 0 | tiff->stripoffsetslen = count; |
1027 | 0 | break; |
1028 | | |
1029 | 0 | case StripByteCounts: |
1030 | 0 | if (tiff->stripbytecounts) |
1031 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "at most one strip byte counts tag allowed"); |
1032 | 0 | if (tiff->rowsperstrip == 0) |
1033 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid strip dimensions"); |
1034 | 0 | if (count > tiff->maxstrips) |
1035 | 0 | count = tiff->maxstrips; |
1036 | 0 | tiff->stripbytecounts = Memento_label(fz_malloc_array(ctx, count, unsigned), "tiff_stripbytecounts"); |
1037 | 0 | tiff_read_tag_value(ctx, tiff->stripbytecounts, tiff, type, value, count); |
1038 | 0 | tiff->stripbytecountslen = count; |
1039 | 0 | break; |
1040 | | |
1041 | 0 | case ColorMap: |
1042 | 0 | if (tiff->colormap) |
1043 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "at most one color map allowed"); |
1044 | 0 | if (type != TSHORT) |
1045 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "unexpected element type for color map"); |
1046 | 0 | if (count > tiff->maxcolors) |
1047 | 0 | count = tiff->maxcolors; |
1048 | 0 | tiff->colormap = Memento_label(fz_malloc_array(ctx, count, unsigned), "tiff_colormap"); |
1049 | 0 | tiff_read_tag_value(ctx, tiff->colormap, tiff, type, value, count); |
1050 | 0 | tiff->colormaplen = count; |
1051 | 0 | break; |
1052 | | |
1053 | 0 | case TileOffsets: |
1054 | 0 | if (tiff->tileoffsets) |
1055 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "at most one tile offsets tag allowed"); |
1056 | 0 | if (tiff->tilelength == 0 || tiff->tilewidth == 0) |
1057 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid tile dimensions"); |
1058 | 0 | if (count > tiff->maxtiles) |
1059 | 0 | count = tiff->maxtiles; |
1060 | 0 | tiff->tileoffsets = Memento_label(fz_malloc_array(ctx, count, unsigned), "tiff_tileoffsets"); |
1061 | 0 | tiff_read_tag_value(ctx, tiff->tileoffsets, tiff, type, value, count); |
1062 | 0 | tiff->tileoffsetslen = count; |
1063 | 0 | break; |
1064 | | |
1065 | 0 | case TileByteCounts: |
1066 | 0 | if (tiff->tilebytecounts) |
1067 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "at most one tile byte counts tag allowed"); |
1068 | 0 | if (tiff->tilelength == 0 || tiff->tilewidth == 0) |
1069 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid tile dimensions"); |
1070 | 0 | if (count > tiff->maxtiles) |
1071 | 0 | count = tiff->maxtiles; |
1072 | 0 | tiff->tilebytecounts = Memento_label(fz_malloc_array(ctx, count, unsigned), "tiff_tilebytecounts"); |
1073 | 0 | tiff_read_tag_value(ctx, tiff->tilebytecounts, tiff, type, value, count); |
1074 | 0 | tiff->tilebytecountslen = count; |
1075 | 0 | break; |
1076 | | |
1077 | 0 | case JPEGTables: |
1078 | | /* Check both value and value + count to allow for overflow */ |
1079 | 0 | if (value > (size_t)(tiff->ep - tiff->bp)) |
1080 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "TIFF JPEG tables offset out of range"); |
1081 | 0 | if (value + count > (size_t)(tiff->ep - tiff->bp)) |
1082 | 0 | count = (unsigned int)(tiff->ep - tiff->bp) - value; |
1083 | 0 | tiff->jpegtables = tiff->bp + value; |
1084 | 0 | tiff->jpegtableslen = count; |
1085 | 0 | break; |
1086 | | |
1087 | 0 | case ICCProfile: |
1088 | 0 | if (tiff->profile) |
1089 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "at most one ICC profile tag allowed"); |
1090 | 0 | if (value > (size_t)(tiff->ep - tiff->bp)) |
1091 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "TIFF profile offset out of range"); |
1092 | 0 | if (value + count > (size_t)(tiff->ep - tiff->bp)) |
1093 | 0 | count = (unsigned int)(tiff->ep - tiff->bp) - value; |
1094 | 0 | tiff->profile = Memento_label(fz_malloc(ctx, count), "tiff_profile"); |
1095 | | /* ICC profile data type is set to UNDEFINED. |
1096 | | * TBYTE reading not correct in tiff_read_tag_value */ |
1097 | 0 | tiff_read_bytes(tiff->profile, tiff, value, count); |
1098 | 0 | tiff->profilesize = count; |
1099 | 0 | break; |
1100 | | |
1101 | 0 | default: |
1102 | | /* fz_warn(ctx, "unknown tag: %d t=%d n=%d", tag, type, count); */ |
1103 | 0 | break; |
1104 | 0 | } |
1105 | 0 | } |
1106 | | |
1107 | | static void |
1108 | | tiff_swap_byte_order(unsigned char *buf, int n) |
1109 | 0 | { |
1110 | 0 | int i, t; |
1111 | 0 | for (i = 0; i < n; i++) |
1112 | 0 | { |
1113 | 0 | t = buf[i * 2 + 0]; |
1114 | 0 | buf[i * 2 + 0] = buf[i * 2 + 1]; |
1115 | 0 | buf[i * 2 + 1] = t; |
1116 | 0 | } |
1117 | 0 | } |
1118 | | |
1119 | | static void |
1120 | | tiff_scale_lab_samples(fz_context *ctx, unsigned char *buf, int bps, int n) |
1121 | 0 | { |
1122 | 0 | int i; |
1123 | 0 | if (bps == 8) |
1124 | 0 | for (i = 0; i < n; i++, buf += 3) |
1125 | 0 | { |
1126 | 0 | buf[1] ^= 128; |
1127 | 0 | buf[2] ^= 128; |
1128 | 0 | } |
1129 | 0 | else if (bps == 16) |
1130 | 0 | for (i = 0; i < n; i++, buf += 6) |
1131 | 0 | { |
1132 | 0 | buf[2] ^= 128; |
1133 | 0 | buf[4] ^= 128; |
1134 | 0 | } |
1135 | 0 | } |
1136 | | |
1137 | | static void |
1138 | | tiff_read_header(fz_context *ctx, struct tiff *tiff, const unsigned char *buf, size_t len) |
1139 | 0 | { |
1140 | 0 | unsigned version; |
1141 | |
|
1142 | 0 | memset(tiff, 0, sizeof(struct tiff)); |
1143 | 0 | tiff->bp = buf; |
1144 | 0 | tiff->rp = buf; |
1145 | 0 | tiff->ep = buf + len; |
1146 | | |
1147 | | /* tag defaults, where applicable */ |
1148 | 0 | tiff->bitspersample = 1; |
1149 | 0 | tiff->compression = 1; |
1150 | 0 | tiff->samplesperpixel = 1; |
1151 | 0 | tiff->resolutionunit = 2; |
1152 | 0 | tiff->rowsperstrip = 0xFFFFFFFF; |
1153 | 0 | tiff->fillorder = 1; |
1154 | 0 | tiff->planar = 1; |
1155 | 0 | tiff->subfiletype = 0; |
1156 | 0 | tiff->predictor = 1; |
1157 | 0 | tiff->ycbcrsubsamp[0] = 2; |
1158 | 0 | tiff->ycbcrsubsamp[1] = 2; |
1159 | | |
1160 | | /* |
1161 | | * Read IFH |
1162 | | */ |
1163 | | |
1164 | | /* get byte order marker */ |
1165 | 0 | tiff->order = readshort(tiff); |
1166 | 0 | if (tiff->order != TII && tiff->order != TMM) |
1167 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "not a TIFF file, wrong magic marker"); |
1168 | | |
1169 | | /* check version */ |
1170 | 0 | version = readshort(tiff); |
1171 | 0 | if (version != 42) |
1172 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "not a TIFF file, wrong version marker"); |
1173 | | |
1174 | | /* get offset of IFD */ |
1175 | 0 | tiff->ifd_offsets = Memento_label(fz_malloc_array(ctx, 1, unsigned), "tiff_ifd_offsets"); |
1176 | 0 | tiff->ifd_offsets[0] = tiff_readlong(tiff); |
1177 | 0 | tiff->ifds = 1; |
1178 | 0 | } |
1179 | | |
1180 | | static unsigned |
1181 | | tiff_next_ifd(fz_context *ctx, struct tiff *tiff, unsigned offset) |
1182 | 0 | { |
1183 | 0 | unsigned count; |
1184 | 0 | int i; |
1185 | |
|
1186 | 0 | if (offset > (unsigned)(tiff->ep - tiff->bp)) |
1187 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid IFD offset %u", offset); |
1188 | | |
1189 | 0 | tiff->rp = tiff->bp + offset; |
1190 | 0 | count = readshort(tiff); |
1191 | |
|
1192 | 0 | if (count * 12 > (unsigned)(tiff->ep - tiff->rp)) |
1193 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "overlarge IFD entry count %u", count); |
1194 | | |
1195 | 0 | tiff->rp += count * 12; |
1196 | 0 | offset = tiff_readlong(tiff); |
1197 | |
|
1198 | 0 | for (i = 0; i < tiff->ifds; i++) |
1199 | 0 | if (tiff->ifd_offsets[i] == offset) |
1200 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in IFDs detected"); |
1201 | | |
1202 | 0 | tiff->ifd_offsets = fz_realloc_array(ctx, tiff->ifd_offsets, tiff->ifds + 1, unsigned); |
1203 | 0 | tiff->ifd_offsets[tiff->ifds] = offset; |
1204 | 0 | tiff->ifds++; |
1205 | |
|
1206 | 0 | return offset; |
1207 | 0 | } |
1208 | | |
1209 | | static void |
1210 | | tiff_seek_ifd(fz_context *ctx, struct tiff *tiff, int subimage) |
1211 | 0 | { |
1212 | 0 | unsigned offset = tiff->ifd_offsets[0]; |
1213 | |
|
1214 | 0 | while (subimage--) |
1215 | 0 | { |
1216 | 0 | offset = tiff_next_ifd(ctx, tiff, offset); |
1217 | |
|
1218 | 0 | if (offset == 0) |
1219 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "subimage index %i out of range", subimage); |
1220 | 0 | } |
1221 | | |
1222 | 0 | tiff->rp = tiff->bp + offset; |
1223 | |
|
1224 | 0 | if (tiff->rp < tiff->bp || tiff->rp > tiff->ep) |
1225 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid IFD offset %u", offset); |
1226 | 0 | } |
1227 | | |
1228 | | static void |
1229 | | tiff_read_ifd(fz_context *ctx, struct tiff *tiff) |
1230 | 0 | { |
1231 | 0 | unsigned offset; |
1232 | 0 | unsigned count; |
1233 | 0 | unsigned i; |
1234 | 0 | unsigned original; |
1235 | 0 | const unsigned char *original_rp; |
1236 | |
|
1237 | 0 | offset = tiff->rp - tiff->bp; |
1238 | 0 | count = readshort(tiff); |
1239 | 0 | if (count > (unsigned)(tiff->ep - tiff->rp) / 12) |
1240 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "overlarge IFD entry count %u", count); |
1241 | 0 | original = offset + 2; |
1242 | 0 | original_rp = tiff->rp; |
1243 | |
|
1244 | 0 | offset = original; |
1245 | 0 | tiff->rp = original_rp; |
1246 | 0 | for (i = 0; i < count; i++) |
1247 | 0 | { |
1248 | 0 | tiff_read_tag(ctx, tiff, offset); |
1249 | 0 | offset += 12; |
1250 | 0 | } |
1251 | |
|
1252 | 0 | if (tiff->bitspersample > 16) |
1253 | 0 | { |
1254 | 0 | fz_warn(ctx, "limiting bits per component to 16 in TIFF image"); |
1255 | 0 | tiff->bitspersample = 16; |
1256 | 0 | } |
1257 | |
|
1258 | 0 | tiff->maxcolors = tiff->colormaplen; |
1259 | 0 | if (tiff->maxcolors) |
1260 | 0 | { |
1261 | 0 | unsigned comps; |
1262 | 0 | switch (tiff->photometric) |
1263 | 0 | { |
1264 | 0 | case 0: /* WhiteIsZero -- inverted */ |
1265 | 0 | case 1: /* BlackIsZero */ |
1266 | 0 | case 4: /* Transparency mask */ |
1267 | 0 | case 32844: /* SGI CIE Log 2 L (16bpp Greyscale) */ |
1268 | 0 | comps = 1; |
1269 | 0 | break; |
1270 | 0 | default: |
1271 | 0 | case 2: /* RGB */ |
1272 | 0 | case 3: /* RGBPal */ |
1273 | 0 | case 6: /* YCbCr */ |
1274 | 0 | case 8: /* Direct L*a*b* encoding. a*, b* signed values */ |
1275 | 0 | case 9: /* ICC Style L*a*b* encoding */ |
1276 | 0 | case 32845: /* SGI CIE Log 2 L, u, v (24bpp or 32bpp) */ |
1277 | 0 | comps = 3; |
1278 | 0 | break; |
1279 | 0 | case 5: /* CMYK */ |
1280 | 0 | comps = 4; |
1281 | 0 | break; |
1282 | 0 | } |
1283 | 0 | if (tiff->maxcolors > (unsigned) (comps * (1 << tiff->bitspersample))) |
1284 | 0 | tiff->maxcolors = (unsigned) (comps * (1 << tiff->bitspersample)); |
1285 | 0 | } |
1286 | | |
1287 | 0 | tiff->maxtiles = 0; |
1288 | 0 | if (tiff->tilelength && tiff->tilewidth) |
1289 | 0 | { |
1290 | 0 | unsigned tilesdown = (tiff->imagelength + tiff->tilelength - 1) / tiff->tilelength; |
1291 | 0 | unsigned tilesacross = (tiff->imagewidth + tiff->tilewidth - 1) / tiff->tilewidth; |
1292 | 0 | unsigned tilesperimage = tilesacross * tilesdown; |
1293 | 0 | if (tiff->planar == 1) |
1294 | 0 | tiff->maxtiles = tilesperimage; |
1295 | 0 | else |
1296 | 0 | tiff->maxtiles = tiff->samplesperpixel * tilesperimage; |
1297 | 0 | } |
1298 | |
|
1299 | 0 | tiff->maxstrips = tiff->imagelength; |
1300 | 0 | if (tiff->rowsperstrip && tiff->rowsperstrip != 0xFFFFFFFF) |
1301 | 0 | { |
1302 | 0 | if (tiff->rowsperstrip < tiff->imagelength) |
1303 | 0 | tiff->maxstrips = (tiff->imagelength + tiff->rowsperstrip - 1) / tiff->rowsperstrip; |
1304 | 0 | } |
1305 | | |
1306 | | /* some creators write strip tags when they meant to write tile tags... */ |
1307 | 0 | if (tiff->tilelength && tiff->tilewidth) |
1308 | 0 | tiff->maxstrips = tiff->maxtiles; |
1309 | |
|
1310 | 0 | offset = original; |
1311 | 0 | tiff->rp = original_rp; |
1312 | 0 | for (i = 0; i < count; i++) |
1313 | 0 | { |
1314 | 0 | tiff_read_tag_array(ctx, tiff, offset); |
1315 | 0 | offset += 12; |
1316 | 0 | } |
1317 | 0 | } |
1318 | | |
1319 | | static void |
1320 | | tiff_ycc_to_rgb(fz_context *ctx, struct tiff *tiff) |
1321 | 0 | { |
1322 | 0 | unsigned x, y; |
1323 | 0 | int offset = tiff->samplesperpixel; |
1324 | |
|
1325 | 0 | for (y = 0; y < tiff->imagelength; y++) |
1326 | 0 | { |
1327 | 0 | unsigned char * row = &tiff->samples[tiff->stride * y]; |
1328 | 0 | for (x = 0; x < tiff->imagewidth; x++) |
1329 | 0 | { |
1330 | 0 | int ycc[3]; |
1331 | 0 | ycc[0] = row[x * offset + 0]; |
1332 | 0 | ycc[1] = row[x * offset + 1] - 128; |
1333 | 0 | ycc[2] = row[x * offset + 2] - 128; |
1334 | |
|
1335 | 0 | row[x * offset + 0] = fz_clampi(ycc[0] + 1.402f * ycc[2], 0, 255); |
1336 | 0 | row[x * offset + 1] = fz_clampi(ycc[0] - 0.34413f * ycc[1] - 0.71414f * ycc[2], 0, 255); |
1337 | 0 | row[x * offset + 2] = fz_clampi(ycc[0] + 1.772f * ycc[1], 0, 255); |
1338 | 0 | } |
1339 | 0 | } |
1340 | 0 | } |
1341 | | |
1342 | | static void |
1343 | | tiff_decode_ifd(fz_context *ctx, struct tiff *tiff) |
1344 | 0 | { |
1345 | 0 | unsigned i; |
1346 | |
|
1347 | 0 | if (tiff->imagelength <= 0) |
1348 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "image height must be > 0"); |
1349 | 0 | if (tiff->imagewidth <= 0) |
1350 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "image width must be > 0"); |
1351 | 0 | if (tiff->bitspersample > 16 || !fz_is_pow2(tiff->bitspersample)) |
1352 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "bits per sample illegal %d", tiff->bitspersample); |
1353 | 0 | if (tiff->samplesperpixel == 0 || tiff->samplesperpixel >= FZ_MAX_COLORS) |
1354 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "components per pixel out of range"); |
1355 | 0 | if (tiff->samplesperpixel < tiff->extrasamples) |
1356 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "components per pixel out of range when compared to extra samples"); |
1357 | | /* Bug 706071: Check for overflow in the stride calculation separately. */ |
1358 | 0 | if (tiff->imagewidth > (UINT_MAX - 7) / tiff->samplesperpixel / tiff->bitspersample) |
1359 | 0 | fz_throw(ctx, FZ_ERROR_LIMIT, "image too large"); |
1360 | 0 | if (tiff->imagelength > UINT_MAX / tiff->imagewidth / (tiff->samplesperpixel + 2) / (tiff->bitspersample / 8 + 1)) |
1361 | 0 | fz_throw(ctx, FZ_ERROR_LIMIT, "image too large"); |
1362 | 0 | if (tiff->tilewidth & 0xf) |
1363 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "tile width not a multiple of 16"); |
1364 | 0 | if (tiff->tilelength & 0xf) |
1365 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "tile height not a multiple of 16"); |
1366 | | |
1367 | 0 | if (tiff->planar != 1) |
1368 | 0 | fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "image data is not in chunky format"); |
1369 | | |
1370 | 0 | if (tiff->photometric == 6) |
1371 | 0 | { |
1372 | 0 | if (tiff->samplesperpixel != 3) |
1373 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid samples per pixel for YCbCr"); |
1374 | 0 | if (tiff->bitspersample != 8) |
1375 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid bits per sample when subsampling"); |
1376 | 0 | if (tiff->ycbcrsubsamp[0] != 1 && tiff->ycbcrsubsamp[0] != 2 && tiff->ycbcrsubsamp[0] != 4) |
1377 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid subsampling factor"); |
1378 | 0 | if (tiff->ycbcrsubsamp[1] != 1 && tiff->ycbcrsubsamp[1] != 2 && tiff->ycbcrsubsamp[1] != 4) |
1379 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid subsampling factor"); |
1380 | 0 | } |
1381 | | |
1382 | 0 | recalculate: |
1383 | 0 | if ( |
1384 | 0 | fz_ckd_mul_uint(&tiff->stride, tiff->imagewidth, tiff->samplesperpixel) || |
1385 | 0 | fz_ckd_mul_uint(&tiff->stride, tiff->stride, tiff->bitspersample) || |
1386 | 0 | fz_ckd_add_uint(&tiff->stride, tiff->stride, 7) |
1387 | 0 | ) |
1388 | 0 | fz_throw(ctx, FZ_ERROR_LIMIT, "invalid image size (integer overflow)"); |
1389 | 0 | tiff->stride >>= 3; |
1390 | |
|
1391 | 0 | if ( |
1392 | 0 | fz_ckd_mul_uint(&tiff->tilestride, tiff->tilewidth, tiff->samplesperpixel) || |
1393 | 0 | fz_ckd_mul_uint(&tiff->tilestride, tiff->tilestride, tiff->bitspersample) || |
1394 | 0 | fz_ckd_add_uint(&tiff->tilestride, tiff->tilestride, 7) |
1395 | 0 | ) |
1396 | 0 | fz_throw(ctx, FZ_ERROR_LIMIT, "invalid image size (integer overflow)"); |
1397 | 0 | tiff->tilestride >>= 3; |
1398 | |
|
1399 | 0 | switch (tiff->photometric) |
1400 | 0 | { |
1401 | 0 | case 0: /* WhiteIsZero -- inverted */ |
1402 | 0 | if (tiff->samplesperpixel - !!tiff->extrasamples < 1) |
1403 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid samples per pixel for bw tiff"); |
1404 | 0 | tiff->colorspace = fz_keep_colorspace(ctx, fz_device_gray(ctx)); |
1405 | 0 | break; |
1406 | 0 | case 1: /* BlackIsZero */ |
1407 | 0 | if (tiff->samplesperpixel - !!tiff->extrasamples < 1) |
1408 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid samples per pixel for bw tiff"); |
1409 | 0 | tiff->colorspace = fz_keep_colorspace(ctx, fz_device_gray(ctx)); |
1410 | 0 | break; |
1411 | 0 | case 2: /* RGB */ |
1412 | 0 | if (tiff->samplesperpixel - !!tiff->extrasamples < 3) |
1413 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid samples per pixel for rgb tiff"); |
1414 | 0 | tiff->colorspace = fz_keep_colorspace(ctx, fz_device_rgb(ctx)); |
1415 | 0 | break; |
1416 | 0 | case 3: /* RGBPal */ |
1417 | 0 | if (tiff->samplesperpixel - !!tiff->extrasamples < 1) |
1418 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid samples per pixel for palettized tiff"); |
1419 | 0 | tiff->colorspace = fz_keep_colorspace(ctx, fz_device_rgb(ctx)); |
1420 | 0 | break; |
1421 | 0 | case 4: /* Transparency mask */ |
1422 | 0 | if (tiff->samplesperpixel - !!tiff->extrasamples < 1) |
1423 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid samples per pixel for transparency mask tiff"); |
1424 | 0 | tiff->colorspace = NULL; |
1425 | 0 | break; |
1426 | 0 | case 5: /* CMYK */ |
1427 | 0 | if (tiff->samplesperpixel - !!tiff->extrasamples < 4) |
1428 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid samples per pixel for cmyk tiff"); |
1429 | 0 | tiff->colorspace = fz_keep_colorspace(ctx, fz_device_cmyk(ctx)); |
1430 | 0 | break; |
1431 | 0 | case 6: /* YCbCr */ |
1432 | 0 | if (tiff->samplesperpixel - !!tiff->extrasamples < 3) |
1433 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid samples per pixel for ycbcr tiff"); |
1434 | | /* it's probably a jpeg ... we let jpeg convert to rgb */ |
1435 | 0 | tiff->colorspace = fz_keep_colorspace(ctx, fz_device_rgb(ctx)); |
1436 | 0 | break; |
1437 | 0 | case 8: /* Direct L*a*b* encoding. a*, b* signed values */ |
1438 | 0 | case 9: /* ICC Style L*a*b* encoding */ |
1439 | 0 | if (tiff->samplesperpixel - !!tiff->extrasamples < 3) |
1440 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid samples per pixel for lab tiff"); |
1441 | 0 | tiff->colorspace = fz_keep_colorspace(ctx, fz_device_lab(ctx)); |
1442 | 0 | break; |
1443 | 0 | case 32844: /* SGI CIE Log 2 L (16bpp Greyscale) */ |
1444 | 0 | if (tiff->samplesperpixel - !!tiff->extrasamples < 1) |
1445 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid samples per pixel for l tiff"); |
1446 | 0 | if (tiff->bitspersample != 8) |
1447 | 0 | { |
1448 | 0 | tiff->bitspersample = 8; |
1449 | 0 | goto recalculate; |
1450 | 0 | } |
1451 | 0 | tiff->colorspace = fz_keep_colorspace(ctx, fz_device_gray(ctx)); |
1452 | 0 | break; |
1453 | 0 | case 32845: /* SGI CIE Log 2 L, u, v (24bpp or 32bpp) */ |
1454 | 0 | if (tiff->samplesperpixel - !!tiff->extrasamples < 3) |
1455 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "invalid samples per pixel for luv tiff"); |
1456 | 0 | if (tiff->bitspersample != 8) |
1457 | 0 | { |
1458 | 0 | tiff->bitspersample = 8; |
1459 | 0 | goto recalculate; |
1460 | 0 | } |
1461 | 0 | tiff->colorspace = fz_keep_colorspace(ctx, fz_device_rgb(ctx)); |
1462 | 0 | break; |
1463 | 0 | default: |
1464 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "unknown photometric: %d", tiff->photometric); |
1465 | 0 | } |
1466 | | |
1467 | 0 | #if FZ_ENABLE_ICC |
1468 | 0 | if (tiff->profile && tiff->profilesize > 0) |
1469 | 0 | { |
1470 | 0 | fz_buffer *buff = NULL; |
1471 | 0 | fz_colorspace *icc = NULL; |
1472 | 0 | fz_var(buff); |
1473 | 0 | fz_try(ctx) |
1474 | 0 | { |
1475 | 0 | buff = fz_new_buffer_from_copied_data(ctx, tiff->profile, tiff->profilesize); |
1476 | 0 | icc = fz_new_icc_colorspace(ctx, fz_colorspace_type(ctx, tiff->colorspace), 0, NULL, buff); |
1477 | 0 | fz_drop_colorspace(ctx, tiff->colorspace); |
1478 | 0 | tiff->colorspace = icc; |
1479 | 0 | } |
1480 | 0 | fz_always(ctx) |
1481 | 0 | fz_drop_buffer(ctx, buff); |
1482 | 0 | fz_catch(ctx) |
1483 | 0 | { |
1484 | 0 | fz_rethrow_if(ctx, FZ_ERROR_SYSTEM); |
1485 | 0 | fz_report_error(ctx); |
1486 | 0 | fz_warn(ctx, "ignoring embedded ICC profile"); |
1487 | 0 | } |
1488 | 0 | } |
1489 | 0 | #endif |
1490 | |
|
1491 | 0 | if (!tiff->colorspace && tiff->samplesperpixel < 1) |
1492 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "too few components for transparency mask"); |
1493 | 0 | if (tiff->colorspace && tiff->colormap && tiff->samplesperpixel < 1) |
1494 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "too few components for RGBPal"); |
1495 | 0 | if (tiff->colorspace && !tiff->colormap && tiff->samplesperpixel < (unsigned) fz_colorspace_n(ctx, tiff->colorspace)) |
1496 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "fewer components per pixel than indicated by colorspace"); |
1497 | | |
1498 | 0 | switch (tiff->resolutionunit) |
1499 | 0 | { |
1500 | 0 | case 2: |
1501 | | /* no unit conversion needed */ |
1502 | 0 | break; |
1503 | 0 | case 3: |
1504 | 0 | tiff->xresolution = tiff->xresolution * 254 / 100; |
1505 | 0 | tiff->yresolution = tiff->yresolution * 254 / 100; |
1506 | 0 | break; |
1507 | 0 | default: |
1508 | 0 | tiff->xresolution = 96; |
1509 | 0 | tiff->yresolution = 96; |
1510 | 0 | break; |
1511 | 0 | } |
1512 | | |
1513 | | /* Note xres and yres could be 0 even if unit was set. If so default to 96dpi. */ |
1514 | 0 | if (tiff->xresolution == 0 || tiff->yresolution == 0) |
1515 | 0 | { |
1516 | 0 | tiff->xresolution = 96; |
1517 | 0 | tiff->yresolution = 96; |
1518 | 0 | } |
1519 | |
|
1520 | 0 | if (tiff->rowsperstrip > tiff->imagelength) |
1521 | 0 | tiff->rowsperstrip = tiff->imagelength; |
1522 | | |
1523 | | /* some creators don't write byte counts for uncompressed images */ |
1524 | 0 | if (tiff->compression == 1) |
1525 | 0 | { |
1526 | 0 | if (tiff->rowsperstrip == 0) |
1527 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "rowsperstrip cannot be 0"); |
1528 | 0 | if (!tiff->tilelength && !tiff->tilewidth && !tiff->stripbytecounts) |
1529 | 0 | { |
1530 | 0 | tiff->stripbytecountslen = (tiff->imagelength + tiff->rowsperstrip - 1) / tiff->rowsperstrip; |
1531 | 0 | tiff->stripbytecounts = Memento_label(fz_malloc_array(ctx, tiff->stripbytecountslen, unsigned), "tiff_stripbytecounts"); |
1532 | 0 | for (i = 0; i < tiff->stripbytecountslen; i++) |
1533 | 0 | tiff->stripbytecounts[i] = tiff->rowsperstrip * tiff->stride; |
1534 | 0 | } |
1535 | 0 | if (tiff->tilelength && tiff->tilewidth && !tiff->tilebytecounts) |
1536 | 0 | { |
1537 | 0 | unsigned tilesdown = (tiff->imagelength + tiff->tilelength - 1) / tiff->tilelength; |
1538 | 0 | unsigned tilesacross = (tiff->imagewidth + tiff->tilewidth - 1) / tiff->tilewidth; |
1539 | 0 | tiff->tilebytecountslen = tilesacross * tilesdown; |
1540 | 0 | tiff->tilebytecounts = Memento_label(fz_malloc_array(ctx, tiff->tilebytecountslen, unsigned), "tiff_tilebytecounts"); |
1541 | 0 | { |
1542 | 0 | unsigned tbc; |
1543 | 0 | if (fz_ckd_mul_uint(&tbc, tiff->tilelength, tiff->tilestride)) |
1544 | 0 | fz_throw(ctx, FZ_ERROR_LIMIT, "tile buffer too large"); |
1545 | 0 | for (i = 0; i < tiff->tilebytecountslen; i++) |
1546 | 0 | tiff->tilebytecounts[i] = tbc; |
1547 | 0 | } |
1548 | 0 | } |
1549 | 0 | } |
1550 | | |
1551 | | /* some creators write strip tags when they meant to write tile tags... */ |
1552 | 0 | if (tiff->tilelength && tiff->tilewidth) |
1553 | 0 | { |
1554 | 0 | if (!tiff->tileoffsets && !tiff->tileoffsetslen && |
1555 | 0 | tiff->stripoffsets && tiff->stripoffsetslen) |
1556 | 0 | { |
1557 | 0 | tiff->tileoffsets = tiff->stripoffsets; |
1558 | 0 | tiff->tileoffsetslen = tiff->stripoffsetslen; |
1559 | 0 | tiff->stripoffsets = NULL; |
1560 | 0 | tiff->stripoffsetslen = 0; |
1561 | 0 | } |
1562 | 0 | if (!tiff->tilebytecounts && !tiff->tilebytecountslen && |
1563 | 0 | tiff->stripbytecounts && tiff->stripbytecountslen) |
1564 | 0 | { |
1565 | 0 | tiff->tilebytecounts = tiff->stripbytecounts; |
1566 | 0 | tiff->tilebytecountslen = tiff->stripbytecountslen; |
1567 | 0 | tiff->stripbytecounts = NULL; |
1568 | 0 | tiff->stripbytecountslen = 0; |
1569 | 0 | } |
1570 | 0 | } |
1571 | 0 | } |
1572 | | |
1573 | | static void |
1574 | | tiff_decode_jpeg(fz_context *ctx, struct tiff *tiff) |
1575 | 0 | { |
1576 | 0 | size_t wlen = (size_t)tiff->imagelength * tiff->stride; |
1577 | 0 | size_t size = 0; |
1578 | 0 | fz_stream *rawstm = NULL; |
1579 | 0 | fz_stream *stm = NULL; |
1580 | |
|
1581 | 0 | fz_var(rawstm); |
1582 | 0 | fz_var(stm); |
1583 | |
|
1584 | 0 | if (tiff->jpegofs > (size_t)(tiff->ep - tiff->bp)) |
1585 | 0 | { |
1586 | 0 | fz_warn(ctx, "TIFF JPEG image offset too large, capping"); |
1587 | 0 | tiff->jpegofs = (unsigned int)(tiff->ep - tiff->bp); |
1588 | 0 | } |
1589 | 0 | if (tiff->jpeglen > (size_t)(tiff->ep - tiff->bp) - tiff->jpegofs) |
1590 | 0 | { |
1591 | 0 | fz_warn(ctx, "TIFF JPEG image length too long, capping"); |
1592 | 0 | tiff->jpeglen = (unsigned int)(tiff->ep - tiff->bp) - tiff->jpegofs; |
1593 | 0 | } |
1594 | |
|
1595 | 0 | fz_try(ctx) |
1596 | 0 | { |
1597 | 0 | rawstm = fz_open_memory(ctx, tiff->bp + tiff->jpegofs, tiff->jpeglen); |
1598 | 0 | stm = fz_open_dctd(ctx, rawstm, -1, 1, 0, NULL); |
1599 | 0 | size = (unsigned)fz_read(ctx, stm, tiff->samples, wlen); |
1600 | 0 | } |
1601 | 0 | fz_always(ctx) |
1602 | 0 | { |
1603 | 0 | fz_drop_stream(ctx, stm); |
1604 | 0 | fz_drop_stream(ctx, rawstm); |
1605 | 0 | } |
1606 | 0 | fz_catch(ctx) |
1607 | 0 | fz_rethrow(ctx); |
1608 | | |
1609 | 0 | if (size < wlen) |
1610 | 0 | fz_warn(ctx, "premature end of data in jpeg"); |
1611 | 0 | } |
1612 | | |
1613 | | static void |
1614 | | tiff_decode_samples(fz_context *ctx, struct tiff *tiff) |
1615 | 0 | { |
1616 | 0 | unsigned i; |
1617 | |
|
1618 | 0 | tiff->samples = Memento_label(fz_calloc(ctx, tiff->stride, tiff->imagelength), "tiff_samples"); |
1619 | |
|
1620 | 0 | if (tiff->tilelength && tiff->tilewidth && tiff->tileoffsets && tiff->tilebytecounts) |
1621 | 0 | tiff_decode_tiles(ctx, tiff); |
1622 | 0 | else if (tiff->rowsperstrip && tiff->stripoffsets && tiff->stripbytecounts) |
1623 | 0 | tiff_decode_strips(ctx, tiff); |
1624 | 0 | else if (tiff->jpegofs && tiff->jpeglen) |
1625 | 0 | tiff_decode_jpeg(ctx, tiff); |
1626 | 0 | else |
1627 | 0 | fz_throw(ctx, FZ_ERROR_FORMAT, "image is missing strip, tile and jpeg data"); |
1628 | | |
1629 | | /* Predictor (only for LZW and Flate) */ |
1630 | 0 | if ((tiff->compression == 5 || tiff->compression == 8 || tiff->compression == 32946) && tiff->predictor == 2) |
1631 | 0 | { |
1632 | 0 | unsigned char *p = tiff->samples; |
1633 | 0 | for (i = 0; i < tiff->imagelength; i++) |
1634 | 0 | { |
1635 | 0 | tiff_unpredict_line(p, tiff->imagewidth, tiff->samplesperpixel, tiff->bitspersample); |
1636 | 0 | p += tiff->stride; |
1637 | 0 | } |
1638 | 0 | } |
1639 | | |
1640 | | /* YCbCr -> RGB, but JPEG already has done this conversion */ |
1641 | 0 | if (tiff->photometric == 6 && tiff->compression != 6 && tiff->compression != 7) |
1642 | 0 | tiff_ycc_to_rgb(ctx, tiff); |
1643 | | |
1644 | | /* RGBPal */ |
1645 | 0 | if (tiff->photometric == 3 && tiff->colormap) |
1646 | 0 | tiff_expand_colormap(ctx, tiff); |
1647 | | |
1648 | | /* WhiteIsZero .. invert */ |
1649 | 0 | if (tiff->photometric == 0) |
1650 | 0 | { |
1651 | 0 | unsigned char *p = tiff->samples; |
1652 | 0 | for (i = 0; i < tiff->imagelength; i++) |
1653 | 0 | { |
1654 | 0 | tiff_invert_line(p, tiff->imagewidth, tiff->samplesperpixel, tiff->bitspersample, tiff->extrasamples); |
1655 | 0 | p += tiff->stride; |
1656 | 0 | } |
1657 | 0 | } |
1658 | | |
1659 | | /* Premultiplied transparency */ |
1660 | 0 | if (tiff->extrasamples == 1) |
1661 | 0 | { |
1662 | | /* In GhostXPS we undo the premultiplication here; muxps holds |
1663 | | * all our images premultiplied by default, so nothing to do. |
1664 | | */ |
1665 | 0 | } |
1666 | | |
1667 | | /* Non-premultiplied transparency */ |
1668 | 0 | if (tiff->extrasamples == 2) |
1669 | 0 | { |
1670 | | /* Premultiplied files are corrected for elsewhere */ |
1671 | 0 | } |
1672 | | |
1673 | | /* Byte swap 16-bit images to big endian if necessary */ |
1674 | 0 | if (tiff->bitspersample == 16 && tiff->order == TII && !tiff->colormap) |
1675 | 0 | tiff_swap_byte_order(tiff->samples, tiff->imagewidth * tiff->imagelength * tiff->samplesperpixel); |
1676 | | |
1677 | | /* Lab colorspace expects all sample components 0..255. |
1678 | | TIFF supplies them as L = 0..255, a/b = -128..127 (for |
1679 | | 8 bits per sample, -32768..32767 for 16 bits per sample) |
1680 | | Scale them to the colorspace's expectations. */ |
1681 | 0 | if (tiff->photometric == 8 && tiff->samplesperpixel == 3) |
1682 | 0 | tiff_scale_lab_samples(ctx, tiff->samples, tiff->bitspersample, tiff->imagewidth * tiff->imagelength); |
1683 | 0 | } |
1684 | | |
1685 | | fz_pixmap * |
1686 | | fz_load_tiff_subimage(fz_context *ctx, const unsigned char *buf, size_t len, int subimage) |
1687 | 0 | { |
1688 | 0 | fz_pixmap *image = NULL; |
1689 | 0 | struct tiff tiff = { 0 }; |
1690 | 0 | int alpha; |
1691 | |
|
1692 | 0 | fz_var(image); |
1693 | |
|
1694 | 0 | fz_try(ctx) |
1695 | 0 | { |
1696 | 0 | tiff_read_header(ctx, &tiff, buf, len); |
1697 | 0 | tiff_seek_ifd(ctx, &tiff, subimage); |
1698 | 0 | tiff_read_ifd(ctx, &tiff); |
1699 | | |
1700 | | /* Decode the image data */ |
1701 | 0 | tiff_decode_ifd(ctx, &tiff); |
1702 | 0 | tiff_decode_samples(ctx, &tiff); |
1703 | | |
1704 | | /* Expand into fz_pixmap struct */ |
1705 | 0 | alpha = tiff.extrasamples != 0 || tiff.colorspace == NULL; |
1706 | 0 | image = fz_new_pixmap(ctx, tiff.colorspace, tiff.imagewidth, tiff.imagelength, NULL, alpha); |
1707 | 0 | image->xres = tiff.xresolution; |
1708 | 0 | image->yres = tiff.yresolution; |
1709 | |
|
1710 | 0 | fz_unpack_tile(ctx, image, tiff.samples, tiff.samplesperpixel, tiff.bitspersample, tiff.stride, 0); |
1711 | | |
1712 | | /* We should only do this on non-pre-multiplied images, but files in the wild are bad */ |
1713 | | /* TODO: check if any samples are non-premul to detect bad files */ |
1714 | 0 | if (tiff.extrasamples /* == 2 */) |
1715 | 0 | fz_premultiply_pixmap(ctx, image); |
1716 | 0 | } |
1717 | 0 | fz_always(ctx) |
1718 | 0 | { |
1719 | | /* Clean up scratch memory */ |
1720 | 0 | fz_drop_colorspace(ctx, tiff.colorspace); |
1721 | 0 | fz_free(ctx, tiff.colormap); |
1722 | 0 | fz_free(ctx, tiff.stripoffsets); |
1723 | 0 | fz_free(ctx, tiff.stripbytecounts); |
1724 | 0 | fz_free(ctx, tiff.tileoffsets); |
1725 | 0 | fz_free(ctx, tiff.tilebytecounts); |
1726 | 0 | fz_free(ctx, tiff.data); |
1727 | 0 | fz_free(ctx, tiff.samples); |
1728 | 0 | fz_free(ctx, tiff.profile); |
1729 | 0 | fz_free(ctx, tiff.ifd_offsets); |
1730 | 0 | } |
1731 | 0 | fz_catch(ctx) |
1732 | 0 | { |
1733 | 0 | fz_drop_pixmap(ctx, image); |
1734 | 0 | fz_rethrow(ctx); |
1735 | 0 | } |
1736 | | |
1737 | 0 | return image; |
1738 | 0 | } |
1739 | | |
1740 | | fz_pixmap * |
1741 | | fz_load_tiff(fz_context *ctx, const unsigned char *buf, size_t len) |
1742 | 0 | { |
1743 | 0 | return fz_load_tiff_subimage(ctx, buf, len, 0); |
1744 | 0 | } |
1745 | | |
1746 | | void |
1747 | | fz_load_tiff_info_subimage(fz_context *ctx, const unsigned char *buf, size_t len, int *wp, int *hp, int *xresp, int *yresp, fz_colorspace **cspacep, int subimage) |
1748 | 0 | { |
1749 | 0 | struct tiff tiff = { 0 }; |
1750 | |
|
1751 | 0 | fz_try(ctx) |
1752 | 0 | { |
1753 | 0 | tiff_read_header(ctx, &tiff, buf, len); |
1754 | 0 | tiff_seek_ifd(ctx, &tiff, subimage); |
1755 | 0 | tiff_read_ifd(ctx, &tiff); |
1756 | |
|
1757 | 0 | tiff_decode_ifd(ctx, &tiff); |
1758 | |
|
1759 | 0 | *wp = tiff.imagewidth; |
1760 | 0 | *hp = tiff.imagelength; |
1761 | 0 | *xresp = (tiff.xresolution ? tiff.xresolution : 96); |
1762 | 0 | *yresp = (tiff.yresolution ? tiff.yresolution : 96); |
1763 | 0 | *cspacep = fz_keep_colorspace(ctx, tiff.colorspace); |
1764 | 0 | } |
1765 | 0 | fz_always(ctx) |
1766 | 0 | { |
1767 | | /* Clean up scratch memory */ |
1768 | 0 | fz_drop_colorspace(ctx, tiff.colorspace); |
1769 | 0 | fz_free(ctx, tiff.colormap); |
1770 | 0 | fz_free(ctx, tiff.stripoffsets); |
1771 | 0 | fz_free(ctx, tiff.stripbytecounts); |
1772 | 0 | fz_free(ctx, tiff.tileoffsets); |
1773 | 0 | fz_free(ctx, tiff.tilebytecounts); |
1774 | 0 | fz_free(ctx, tiff.data); |
1775 | 0 | fz_free(ctx, tiff.samples); |
1776 | 0 | fz_free(ctx, tiff.profile); |
1777 | 0 | fz_free(ctx, tiff.ifd_offsets); |
1778 | 0 | } |
1779 | 0 | fz_catch(ctx) |
1780 | 0 | { |
1781 | 0 | fz_rethrow(ctx); |
1782 | 0 | } |
1783 | 0 | } |
1784 | | |
1785 | | void |
1786 | | fz_load_tiff_info(fz_context *ctx, const unsigned char *buf, size_t len, int *wp, int *hp, int *xresp, int *yresp, fz_colorspace **cspacep) |
1787 | 0 | { |
1788 | 0 | fz_load_tiff_info_subimage(ctx, buf, len, wp, hp, xresp, yresp, cspacep, 0); |
1789 | 0 | } |
1790 | | |
1791 | | int |
1792 | | fz_load_tiff_subimage_count(fz_context *ctx, const unsigned char *buf, size_t len) |
1793 | 0 | { |
1794 | 0 | unsigned offset; |
1795 | 0 | unsigned subimage_count = 0; |
1796 | 0 | struct tiff tiff = { 0 }; |
1797 | |
|
1798 | 0 | fz_try(ctx) |
1799 | 0 | { |
1800 | 0 | tiff_read_header(ctx, &tiff, buf, len); |
1801 | |
|
1802 | 0 | offset = tiff.ifd_offsets[0]; |
1803 | |
|
1804 | 0 | do { |
1805 | 0 | subimage_count++; |
1806 | 0 | offset = tiff_next_ifd(ctx, &tiff, offset); |
1807 | 0 | } while (offset != 0); |
1808 | 0 | } |
1809 | 0 | fz_always(ctx) |
1810 | 0 | fz_free(ctx, tiff.ifd_offsets); |
1811 | 0 | fz_catch(ctx) |
1812 | 0 | fz_rethrow(ctx); |
1813 | | |
1814 | 0 | return subimage_count; |
1815 | 0 | } |