load-tiff.c 52 KB

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