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