image.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  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 "context-imp.h"
  24. #include "image-imp.h"
  25. #include "pixmap-imp.h"
  26. #include <string.h>
  27. #include <math.h>
  28. #include <assert.h>
  29. /* TODO: here or public? */
  30. static int
  31. fz_key_storable_needs_reaping(fz_context *ctx, const fz_key_storable *ks)
  32. {
  33. return ks == NULL ? 0 : (ks->store_key_refs == ks->storable.refs);
  34. }
  35. #define SANE_DPI 72.0f
  36. #define INSANE_DPI 4800.0f
  37. #define SCALABLE_IMAGE_DPI 96
  38. struct fz_compressed_image
  39. {
  40. fz_image super;
  41. fz_compressed_buffer *buffer;
  42. };
  43. struct fz_pixmap_image
  44. {
  45. fz_image super;
  46. fz_pixmap *tile;
  47. };
  48. typedef struct
  49. {
  50. int refs;
  51. fz_image *image;
  52. int l2factor;
  53. fz_irect rect;
  54. } fz_image_key;
  55. fz_image *
  56. fz_keep_image(fz_context *ctx, fz_image *image)
  57. {
  58. return fz_keep_key_storable(ctx, &image->key_storable);
  59. }
  60. fz_image *
  61. fz_keep_image_store_key(fz_context *ctx, fz_image *image)
  62. {
  63. return fz_keep_key_storable_key(ctx, &image->key_storable);
  64. }
  65. void
  66. fz_drop_image_store_key(fz_context *ctx, fz_image *image)
  67. {
  68. fz_drop_key_storable_key(ctx, &image->key_storable);
  69. }
  70. static int
  71. fz_make_hash_image_key(fz_context *ctx, fz_store_hash *hash, void *key_)
  72. {
  73. fz_image_key *key = (fz_image_key *)key_;
  74. hash->u.pir.ptr = key->image;
  75. hash->u.pir.i = key->l2factor;
  76. hash->u.pir.r = key->rect;
  77. return 1;
  78. }
  79. static void *
  80. fz_keep_image_key(fz_context *ctx, void *key_)
  81. {
  82. fz_image_key *key = (fz_image_key *)key_;
  83. return fz_keep_imp(ctx, key, &key->refs);
  84. }
  85. static void
  86. fz_drop_image_key(fz_context *ctx, void *key_)
  87. {
  88. fz_image_key *key = (fz_image_key *)key_;
  89. if (fz_drop_imp(ctx, key, &key->refs))
  90. {
  91. fz_drop_image_store_key(ctx, key->image);
  92. fz_free(ctx, key);
  93. }
  94. }
  95. static int
  96. fz_cmp_image_key(fz_context *ctx, void *k0_, void *k1_)
  97. {
  98. fz_image_key *k0 = (fz_image_key *)k0_;
  99. fz_image_key *k1 = (fz_image_key *)k1_;
  100. return k0->image == k1->image && k0->l2factor == k1->l2factor && k0->rect.x0 == k1->rect.x0 && k0->rect.y0 == k1->rect.y0 && k0->rect.x1 == k1->rect.x1 && k0->rect.y1 == k1->rect.y1;
  101. }
  102. static void
  103. fz_format_image_key(fz_context *ctx, char *s, size_t n, void *key_)
  104. {
  105. fz_image_key *key = (fz_image_key *)key_;
  106. fz_snprintf(s, n, "(image %d x %d sf=%d)", key->image->w, key->image->h, key->l2factor);
  107. }
  108. static int
  109. fz_needs_reap_image_key(fz_context *ctx, void *key_)
  110. {
  111. fz_image_key *key = (fz_image_key *)key_;
  112. return fz_key_storable_needs_reaping(ctx, &key->image->key_storable);
  113. }
  114. static const fz_store_type fz_image_store_type =
  115. {
  116. "fz_image",
  117. fz_make_hash_image_key,
  118. fz_keep_image_key,
  119. fz_drop_image_key,
  120. fz_cmp_image_key,
  121. fz_format_image_key,
  122. fz_needs_reap_image_key
  123. };
  124. void
  125. fz_drop_image(fz_context *ctx, fz_image *image)
  126. {
  127. fz_drop_key_storable(ctx, &image->key_storable);
  128. }
  129. static void
  130. fz_mask_color_key(fz_context *ctx, fz_pixmap *pix, int n, int bpc, const int *colorkey_in, int indexed)
  131. {
  132. unsigned char *p = pix->samples;
  133. int w;
  134. int k, t;
  135. int h = pix->h;
  136. size_t stride = pix->stride - pix->w * (size_t)pix->n;
  137. int colorkey[FZ_MAX_COLORS * 2];
  138. int scale, shift, max;
  139. if (pix->w == 0)
  140. return;
  141. if (indexed)
  142. {
  143. /* no upscaling or downshifting needed for indexed images */
  144. scale = 1;
  145. shift = 0;
  146. }
  147. else
  148. {
  149. switch (bpc)
  150. {
  151. case 1: scale = 255; shift = 0; break;
  152. case 2: scale = 85; shift = 0; break;
  153. case 4: scale = 17; shift = 0; break;
  154. default:
  155. case 8: scale = 1; shift = 0; break;
  156. case 16: scale = 1; shift = 8; break;
  157. case 24: scale = 1; shift = 16; break;
  158. case 32: scale = 1; shift = 24; break;
  159. }
  160. }
  161. switch (bpc)
  162. {
  163. case 1: max = 1; break;
  164. case 2: max = 3; break;
  165. case 4: max = 15; break;
  166. default:
  167. case 8: max = 0xff; break;
  168. case 16: max = 0xffff; break;
  169. case 24: max = 0xffffff; break;
  170. case 32: max = 0xffffffff; break;
  171. }
  172. for (k = 0; k < 2 * n; k++)
  173. {
  174. colorkey[k] = colorkey_in[k];
  175. if (colorkey[k] > max)
  176. {
  177. if (indexed && bpc == 1)
  178. {
  179. if (k == 0)
  180. {
  181. fz_warn(ctx, "first color key masking value out of range in 1bpc indexed image, ignoring color key masking");
  182. return;
  183. }
  184. fz_warn(ctx, "later color key masking value out of range in 1bpc indexed image, assumed to be 1");
  185. colorkey[k] = 1;
  186. }
  187. else if (bpc != 1)
  188. {
  189. fz_warn(ctx, "color key masking value out of range, masking to valid range");
  190. colorkey[k] &= max;
  191. }
  192. }
  193. if (colorkey[k] < 0 || colorkey[k] > max)
  194. {
  195. fz_warn(ctx, "color key masking value out of range, clamping to valid range");
  196. colorkey[k] = fz_clampi(colorkey[k], 0, max);
  197. }
  198. if (scale > 1)
  199. {
  200. /* scale up color key masking value so it can be compared with samples. */
  201. colorkey[k] *= scale;
  202. }
  203. else if (shift > 0)
  204. {
  205. /* shifting down color key masking value so it can be compared with samples. */
  206. colorkey[k] >>= shift;
  207. }
  208. }
  209. while (h--)
  210. {
  211. w = pix->w;
  212. do
  213. {
  214. t = 1;
  215. for (k = 0; k < n; k++)
  216. if (p[k] < colorkey[k * 2] || p[k] > colorkey[k * 2 + 1])
  217. t = 0;
  218. if (t)
  219. for (k = 0; k < pix->n; k++)
  220. p[k] = 0;
  221. p += pix->n;
  222. }
  223. while (--w);
  224. p += stride;
  225. }
  226. }
  227. static void
  228. fz_unblend_masked_tile(fz_context *ctx, fz_pixmap *tile, fz_image *image, const fz_irect *isa)
  229. {
  230. fz_pixmap *mask;
  231. unsigned char *s, *d = tile->samples;
  232. int n = tile->n;
  233. int k;
  234. size_t sstride, dstride = tile->stride - tile->w * (size_t)tile->n;
  235. int h;
  236. fz_irect subarea;
  237. /* We need at least as much of the mask as there was of the tile. */
  238. if (isa)
  239. subarea = *isa;
  240. else
  241. {
  242. subarea.x0 = 0;
  243. subarea.y0 = 0;
  244. subarea.x1 = tile->w;
  245. subarea.y1 = tile->h;
  246. }
  247. mask = fz_get_pixmap_from_image(ctx, image->mask, &subarea, NULL, NULL, NULL);
  248. s = mask->samples;
  249. /* RJW: Urgh, bit of nastiness here. fz_pixmap_from_image will either return
  250. * an exact match for the subarea we asked for, or the full image, and the
  251. * normal way to know is that the matrix will be updated. That doesn't help
  252. * us here. */
  253. if (image->mask->w == mask->w && image->mask->h == mask->h) {
  254. subarea.x0 = 0;
  255. subarea.y0 = 0;
  256. }
  257. if (isa)
  258. s += (isa->x0 - subarea.x0) * (size_t)mask->n + (isa->y0 - subarea.y0) * (size_t)mask->stride;
  259. sstride = mask->stride - tile->w * (size_t)mask->n;
  260. h = tile->h;
  261. if (tile->w != 0)
  262. {
  263. while (h--)
  264. {
  265. int w = tile->w;
  266. do
  267. {
  268. if (*s == 0)
  269. for (k = 0; k < image->n; k++)
  270. d[k] = image->colorkey[k];
  271. else
  272. for (k = 0; k < image->n; k++)
  273. d[k] = fz_clampi(image->colorkey[k] + (d[k] - image->colorkey[k]) * 255 / *s, 0, 255);
  274. s++;
  275. d += n;
  276. }
  277. while (--w);
  278. s += sstride;
  279. d += dstride;
  280. }
  281. }
  282. fz_drop_pixmap(ctx, mask);
  283. }
  284. static void fz_adjust_image_subarea(fz_context *ctx, fz_image *image, fz_irect *subarea, int l2factor)
  285. {
  286. int f = 1<<l2factor;
  287. int bpp = image->bpc * image->n;
  288. int mask;
  289. switch (bpp)
  290. {
  291. case 1: mask = 8*f; break;
  292. case 2: mask = 4*f; break;
  293. case 4: mask = 2*f; break;
  294. default: mask = (bpp & 7) == 0 ? f : 0; break;
  295. }
  296. if (mask != 0)
  297. {
  298. subarea->x0 &= ~(mask - 1);
  299. subarea->x1 = (subarea->x1 + mask - 1) & ~(mask - 1);
  300. }
  301. else
  302. {
  303. /* Awkward case - mask cannot be a power of 2. */
  304. mask = bpp*f;
  305. switch (bpp)
  306. {
  307. case 3:
  308. case 5:
  309. case 7:
  310. case 9:
  311. case 11:
  312. case 13:
  313. case 15:
  314. default:
  315. mask *= 8;
  316. break;
  317. case 6:
  318. case 10:
  319. case 14:
  320. mask *= 4;
  321. break;
  322. case 12:
  323. mask *= 2;
  324. break;
  325. }
  326. subarea->x0 = (subarea->x0 / mask) * mask;
  327. subarea->x1 = ((subarea->x1 + mask - 1) / mask) * mask;
  328. }
  329. subarea->y0 &= ~(f - 1);
  330. if (subarea->x1 > image->w)
  331. subarea->x1 = image->w;
  332. subarea->y1 = (subarea->y1 + f - 1) & ~(f - 1);
  333. if (subarea->y1 > image->h)
  334. subarea->y1 = image->h;
  335. }
  336. static void fz_compute_image_key(fz_context *ctx, fz_image *image, fz_matrix *ctm,
  337. fz_image_key *key, const fz_irect *subarea, int l2factor, int *w, int *h, int *dw, int *dh)
  338. {
  339. key->refs = 1;
  340. key->image = image;
  341. key->l2factor = l2factor;
  342. if (subarea == NULL)
  343. {
  344. key->rect.x0 = 0;
  345. key->rect.y0 = 0;
  346. key->rect.x1 = image->w;
  347. key->rect.y1 = image->h;
  348. }
  349. else
  350. {
  351. key->rect = *subarea;
  352. ctx->tuning->image_decode(ctx->tuning->image_decode_arg, image->w, image->h, key->l2factor, &key->rect);
  353. fz_adjust_image_subarea(ctx, image, &key->rect, key->l2factor);
  354. }
  355. /* Based on that subarea, recalculate the extents */
  356. if (ctm)
  357. {
  358. float frac_w = (float) (key->rect.x1 - key->rect.x0) / image->w;
  359. float frac_h = (float) (key->rect.y1 - key->rect.y0) / image->h;
  360. float a = ctm->a * frac_w;
  361. float b = ctm->b * frac_w;
  362. float c = ctm->c * frac_h;
  363. float d = ctm->d * frac_h;
  364. *w = sqrtf(a * a + b * b);
  365. *h = sqrtf(c * c + d * d);
  366. }
  367. else
  368. {
  369. *w = image->w;
  370. *h = image->h;
  371. }
  372. /* Return the true sizes to the caller */
  373. if (dw)
  374. *dw = *w;
  375. if (dh)
  376. *dh = *h;
  377. if (*w > image->w)
  378. *w = image->w;
  379. if (*h > image->h)
  380. *h = image->h;
  381. if (*w == 0 || *h == 0)
  382. key->l2factor = 0;
  383. }
  384. typedef struct {
  385. fz_stream *src;
  386. size_t l_skip; /* Number of bytes to skip on the left. */
  387. size_t r_skip; /* Number of bytes to skip on the right. */
  388. size_t b_skip; /* Number of bytes to skip on the bottom. */
  389. int lines; /* Number of lines left to copy. */
  390. size_t stride; /* Number of bytes to read in the image. */
  391. size_t nskip; /* Number of bytes left to skip on this line. */
  392. size_t nread; /* Number of bytes left to read on this line. */
  393. } subarea_state;
  394. static int
  395. subarea_next(fz_context *ctx, fz_stream *stm, size_t len)
  396. {
  397. subarea_state *state = stm->state;
  398. size_t n;
  399. stm->wp = stm->rp = NULL;
  400. while (state->nskip > 0)
  401. {
  402. n = fz_skip(ctx, state->src, state->nskip);
  403. if (n == 0)
  404. return EOF;
  405. state->nskip -= n;
  406. }
  407. if (state->lines == 0)
  408. return EOF;
  409. n = fz_available(ctx, state->src, state->nread);
  410. if (n > state->nread)
  411. n = state->nread;
  412. if (n == 0)
  413. return EOF;
  414. stm->rp = state->src->rp;
  415. stm->wp = stm->rp + n;
  416. stm->pos += n;
  417. state->src->rp = stm->wp;
  418. state->nread -= n;
  419. if (state->nread == 0)
  420. {
  421. state->lines--;
  422. if (state->lines == 0)
  423. state->nskip = state->r_skip + state->b_skip;
  424. else
  425. state->nskip = state->l_skip + state->r_skip;
  426. state->nread = state->stride;
  427. }
  428. return *stm->rp++;
  429. }
  430. static void
  431. subarea_drop(fz_context *ctx, void *state)
  432. {
  433. fz_free(ctx, state);
  434. }
  435. static fz_stream *
  436. subarea_stream(fz_context *ctx, fz_stream *stm, fz_image *image, const fz_irect *subarea, int l2factor)
  437. {
  438. subarea_state *state;
  439. int f = 1<<l2factor;
  440. int stream_w = (image->w + f - 1)>>l2factor;
  441. size_t stream_stride = (stream_w * (size_t)image->n * image->bpc + 7) / 8;
  442. int l_margin = subarea->x0 >> l2factor;
  443. int t_margin = subarea->y0 >> l2factor;
  444. int r_margin = (image->w + f - 1 - subarea->x1) >> l2factor;
  445. int b_margin = (image->h + f - 1 - subarea->y1) >> l2factor;
  446. size_t l_skip = (l_margin * (size_t)image->n * image->bpc)/8;
  447. size_t r_skip = (r_margin * (size_t)image->n * image->bpc + 7)/8;
  448. size_t t_skip = t_margin * stream_stride;
  449. size_t b_skip = b_margin * stream_stride;
  450. int h = (subarea->y1 - subarea->y0 + f - 1) >> l2factor;
  451. int w = (subarea->x1 - subarea->x0 + f - 1) >> l2factor;
  452. size_t stride = (w * (size_t)image->n * image->bpc + 7) / 8;
  453. state = fz_malloc_struct(ctx, subarea_state);
  454. state->src = stm;
  455. state->l_skip = l_skip;
  456. state->r_skip = r_skip;
  457. state->b_skip = b_skip;
  458. state->lines = h;
  459. state->nskip = l_skip+t_skip;
  460. state->stride = stride;
  461. state->nread = stride;
  462. return fz_new_stream(ctx, state, subarea_next, subarea_drop);
  463. }
  464. typedef struct
  465. {
  466. fz_stream *src;
  467. int w; /* Width in source pixels. */
  468. int h; /* Height (remaining) in scanlines. */
  469. int n; /* Number of components. */
  470. int f; /* Fill level (how many scanlines we've copied in). */
  471. size_t r; /* How many samples Remain to be filled in this line. */
  472. int l2; /* The amount of subsampling we're doing. */
  473. unsigned char data[1];
  474. } l2sub_state;
  475. static void
  476. subsample_drop(fz_context *ctx, void *state)
  477. {
  478. fz_free(ctx, state);
  479. }
  480. static int
  481. subsample_next(fz_context *ctx, fz_stream *stm, size_t len)
  482. {
  483. l2sub_state *state = (l2sub_state *)stm->state;
  484. size_t fill;
  485. stm->rp = stm->wp = &state->data[0];
  486. if (state->h == 0)
  487. return EOF;
  488. /* Copy in data */
  489. do
  490. {
  491. if (state->r == 0)
  492. state->r = state->w * (size_t)state->n;
  493. while (state->r > 0)
  494. {
  495. size_t a;
  496. a = fz_available(ctx, state->src, state->r);
  497. if (a == 0)
  498. return EOF;
  499. if (a > state->r)
  500. a = state->r;
  501. memcpy(&state->data[state->w * (size_t)state->n * (state->f+1) - state->r],
  502. state->src->rp, a);
  503. state->src->rp += a;
  504. state->r -= a;
  505. }
  506. state->f++;
  507. state->h--;
  508. }
  509. while (state->h > 0 && state->f != (1<<state->l2));
  510. /* Perform the subsample */
  511. fz_subsample_pixblock(state->data, state->w, state->f, state->n, state->l2, state->w * (size_t)state->n);
  512. state->f = 0;
  513. /* Update data pointers. */
  514. fill = ((state->w + (1<<state->l2) - 1)>>state->l2) * (size_t)state->n;
  515. stm->pos += fill;
  516. stm->rp = &state->data[0];
  517. stm->wp = &state->data[fill];
  518. return *stm->rp++;
  519. }
  520. static fz_stream *
  521. subsample_stream(fz_context *ctx, fz_stream *src, int w, int h, int n, int l2extra)
  522. {
  523. l2sub_state *state = fz_malloc(ctx, sizeof(l2sub_state) + w*(size_t)(n<<l2extra));
  524. state->src = src;
  525. state->w = w;
  526. state->h = h;
  527. state->n = n;
  528. state->f = 0;
  529. state->r = 0;
  530. state->l2 = l2extra;
  531. return fz_new_stream(ctx, state, subsample_next, subsample_drop);
  532. }
  533. /* l2factor is the amount of subsampling that the decoder is going to be
  534. * doing for us already. (So for JPEG 0,1,2,3 corresponding to 1, 2, 4,
  535. * 8. For other formats, probably 0.). l2extra is the additional amount
  536. * of subsampling we should perform here. */
  537. fz_pixmap *
  538. fz_decomp_image_from_stream(fz_context *ctx, fz_stream *stm, fz_compressed_image *cimg, fz_irect *subarea, int indexed, int l2factor, int *l2extra)
  539. {
  540. fz_image *image = &cimg->super;
  541. fz_pixmap *tile = NULL;
  542. size_t stride, len, i;
  543. unsigned char *samples = NULL;
  544. int f = 1<<l2factor;
  545. int w = image->w;
  546. int h = image->h;
  547. int matte = image->use_colorkey && image->mask;
  548. fz_stream *read_stream = stm;
  549. fz_stream *sstream = NULL;
  550. fz_stream *l2stream = NULL;
  551. fz_stream *unpstream = NULL;
  552. if (matte)
  553. {
  554. /* Can't do l2factor decoding */
  555. if (image->w != image->mask->w || image->h != image->mask->h)
  556. {
  557. fz_warn(ctx, "mask must be of same size as image for /Matte");
  558. matte = 0;
  559. }
  560. assert(l2factor == 0);
  561. }
  562. if (subarea)
  563. {
  564. if (subarea->x0 == 0 && subarea->x1 == image->w &&
  565. subarea->y0 == 0 && subarea->y1 == image->h)
  566. subarea = NULL;
  567. else
  568. {
  569. fz_adjust_image_subarea(ctx, image, subarea, l2factor);
  570. w = (subarea->x1 - subarea->x0);
  571. h = (subarea->y1 - subarea->y0);
  572. }
  573. }
  574. w = (w + f - 1) >> l2factor;
  575. h = (h + f - 1) >> l2factor;
  576. fz_var(tile);
  577. fz_var(samples);
  578. fz_var(sstream);
  579. fz_var(unpstream);
  580. fz_var(l2stream);
  581. fz_try(ctx)
  582. {
  583. int alpha = (image->colorspace == NULL);
  584. if (image->use_colorkey)
  585. alpha = 1;
  586. if (subarea)
  587. read_stream = sstream = subarea_stream(ctx, stm, image, subarea, l2factor);
  588. if (image->bpc != 8 || image->use_colorkey)
  589. read_stream = unpstream = fz_unpack_stream(ctx, read_stream, image->bpc, w, h, image->n, indexed, image->use_colorkey, 0);
  590. if (l2extra && *l2extra && !indexed)
  591. {
  592. read_stream = l2stream = subsample_stream(ctx, read_stream, w, h, image->n + image->use_colorkey, *l2extra);
  593. w = (w + (1<<*l2extra) - 1)>>*l2extra;
  594. h = (h + (1<<*l2extra) - 1)>>*l2extra;
  595. *l2extra = 0;
  596. }
  597. tile = fz_new_pixmap(ctx, image->colorspace, w, h, NULL, alpha);
  598. if (image->interpolate & FZ_PIXMAP_FLAG_INTERPOLATE)
  599. tile->flags |= FZ_PIXMAP_FLAG_INTERPOLATE;
  600. else
  601. tile->flags &= ~FZ_PIXMAP_FLAG_INTERPOLATE;
  602. samples = tile->samples;
  603. stride = tile->stride;
  604. len = fz_read(ctx, read_stream, samples, h * stride);
  605. /* Pad truncated images */
  606. if (len < stride * h)
  607. {
  608. fz_warn(ctx, "padding truncated image");
  609. memset(samples + len, 0, stride * h - len);
  610. }
  611. /* Invert 1-bit image masks */
  612. if (image->imagemask)
  613. {
  614. /* 0=opaque and 1=transparent so we need to invert */
  615. unsigned char *p = samples;
  616. len = h * stride;
  617. for (i = 0; i < len; i++)
  618. p[i] = ~p[i];
  619. }
  620. /* color keyed transparency */
  621. if (image->use_colorkey && !image->mask)
  622. fz_mask_color_key(ctx, tile, image->n, image->bpc, image->colorkey, indexed);
  623. if (indexed)
  624. {
  625. fz_pixmap *conv;
  626. fz_decode_indexed_tile(ctx, tile, image->decode, (1 << image->bpc) - 1);
  627. conv = fz_convert_indexed_pixmap_to_base(ctx, tile);
  628. fz_drop_pixmap(ctx, tile);
  629. tile = conv;
  630. }
  631. else if (image->use_decode)
  632. {
  633. fz_decode_tile(ctx, tile, image->decode);
  634. }
  635. /* pre-blended matte color */
  636. if (matte)
  637. fz_unblend_masked_tile(ctx, tile, image, subarea);
  638. }
  639. fz_always(ctx)
  640. {
  641. fz_drop_stream(ctx, sstream);
  642. fz_drop_stream(ctx, unpstream);
  643. fz_drop_stream(ctx, l2stream);
  644. }
  645. fz_catch(ctx)
  646. {
  647. fz_drop_pixmap(ctx, tile);
  648. fz_rethrow(ctx);
  649. }
  650. return tile;
  651. }
  652. void
  653. fz_drop_image_base(fz_context *ctx, fz_image *image)
  654. {
  655. fz_drop_colorspace(ctx, image->colorspace);
  656. fz_drop_image(ctx, image->mask);
  657. fz_free(ctx, image);
  658. }
  659. void
  660. fz_drop_image_imp(fz_context *ctx, fz_storable *image_)
  661. {
  662. fz_image *image = (fz_image *)image_;
  663. image->drop_image(ctx, image);
  664. fz_drop_image_base(ctx, image);
  665. }
  666. static void
  667. drop_compressed_image(fz_context *ctx, fz_image *image_)
  668. {
  669. fz_compressed_image *image = (fz_compressed_image *)image_;
  670. fz_drop_compressed_buffer(ctx, image->buffer);
  671. }
  672. static void
  673. drop_pixmap_image(fz_context *ctx, fz_image *image_)
  674. {
  675. fz_pixmap_image *image = (fz_pixmap_image *)image_;
  676. fz_drop_pixmap(ctx, image->tile);
  677. }
  678. static fz_pixmap *
  679. compressed_image_get_pixmap(fz_context *ctx, fz_image *image_, fz_irect *subarea, int w, int h, int *l2factor)
  680. {
  681. fz_compressed_image *image = (fz_compressed_image *)image_;
  682. int native_l2factor;
  683. fz_stream *stm;
  684. int indexed;
  685. fz_pixmap *tile;
  686. int can_sub = 0;
  687. int local_l2factor;
  688. /* If we are using matte, then the decode code requires both image and tile sizes
  689. * to match. The simplest way to ensure this is to do no native l2factor decoding.
  690. */
  691. if (image->super.use_colorkey && image->super.mask)
  692. {
  693. local_l2factor = 0;
  694. l2factor = &local_l2factor;
  695. }
  696. /* We need to make a new one. */
  697. /* First check for ones that we can't decode using streams */
  698. switch (image->buffer->params.type)
  699. {
  700. case FZ_IMAGE_PNG:
  701. tile = fz_load_png(ctx, image->buffer->buffer->data, image->buffer->buffer->len);
  702. break;
  703. case FZ_IMAGE_GIF:
  704. tile = fz_load_gif(ctx, image->buffer->buffer->data, image->buffer->buffer->len);
  705. break;
  706. case FZ_IMAGE_BMP:
  707. tile = fz_load_bmp(ctx, image->buffer->buffer->data, image->buffer->buffer->len);
  708. break;
  709. case FZ_IMAGE_TIFF:
  710. tile = fz_load_tiff(ctx, image->buffer->buffer->data, image->buffer->buffer->len);
  711. break;
  712. case FZ_IMAGE_PNM:
  713. tile = fz_load_pnm(ctx, image->buffer->buffer->data, image->buffer->buffer->len);
  714. break;
  715. case FZ_IMAGE_JXR:
  716. tile = fz_load_jxr(ctx, image->buffer->buffer->data, image->buffer->buffer->len);
  717. break;
  718. case FZ_IMAGE_JPX:
  719. tile = fz_load_jpx(ctx, image->buffer->buffer->data, image->buffer->buffer->len, image->super.colorspace);
  720. break;
  721. case FZ_IMAGE_PSD:
  722. tile = fz_load_psd(ctx, image->buffer->buffer->data, image->buffer->buffer->len);
  723. break;
  724. case FZ_IMAGE_JPEG:
  725. /* Scan JPEG stream and patch missing height values in header */
  726. {
  727. unsigned char *s = image->buffer->buffer->data;
  728. unsigned char *e = s + image->buffer->buffer->len;
  729. unsigned char *d;
  730. for (d = s + 2; s < d && d + 9 < e && d[0] == 0xFF; d += (d[2] << 8 | d[3]) + 2)
  731. {
  732. if (d[1] < 0xC0 || (0xC3 < d[1] && d[1] < 0xC9) || 0xCB < d[1])
  733. continue;
  734. if ((d[5] == 0 && d[6] == 0) || ((d[5] << 8) | d[6]) > image->super.h)
  735. {
  736. d[5] = (image->super.h >> 8) & 0xFF;
  737. d[6] = image->super.h & 0xFF;
  738. }
  739. }
  740. }
  741. /* fall through */
  742. default:
  743. native_l2factor = l2factor ? *l2factor : 0;
  744. stm = fz_open_image_decomp_stream_from_buffer(ctx, image->buffer, l2factor);
  745. fz_try(ctx)
  746. {
  747. if (l2factor)
  748. native_l2factor -= *l2factor;
  749. indexed = fz_colorspace_is_indexed(ctx, image->super.colorspace);
  750. can_sub = 1;
  751. tile = fz_decomp_image_from_stream(ctx, stm, image, subarea, indexed, native_l2factor, l2factor);
  752. }
  753. fz_always(ctx)
  754. fz_drop_stream(ctx, stm);
  755. fz_catch(ctx)
  756. fz_rethrow(ctx);
  757. break;
  758. }
  759. if (can_sub == 0 && subarea != NULL)
  760. {
  761. subarea->x0 = 0;
  762. subarea->y0 = 0;
  763. subarea->x1 = image->super.w;
  764. subarea->y1 = image->super.h;
  765. }
  766. return tile;
  767. }
  768. static fz_pixmap *
  769. pixmap_image_get_pixmap(fz_context *ctx, fz_image *image_, fz_irect *subarea, int w, int h, int *l2factor)
  770. {
  771. fz_pixmap_image *image = (fz_pixmap_image *)image_;
  772. /* 'Simple' images created direct from pixmaps will have no buffer
  773. * of compressed data. We cannot do any better than just returning
  774. * a pointer to the original 'tile'.
  775. */
  776. return fz_keep_pixmap(ctx, image->tile); /* That's all we can give you! */
  777. }
  778. static void
  779. update_ctm_for_subarea(fz_matrix *ctm, const fz_irect *subarea, int w, int h)
  780. {
  781. fz_matrix m;
  782. if (ctm == NULL || (subarea->x0 == 0 && subarea->y0 == 0 && subarea->x1 == w && subarea->y1 == h))
  783. return;
  784. m.a = (float) (subarea->x1 - subarea->x0) / w;
  785. m.b = 0;
  786. m.c = 0;
  787. m.d = (float) (subarea->y1 - subarea->y0) / h;
  788. m.e = (float) subarea->x0 / w;
  789. m.f = (float) subarea->y0 / h;
  790. *ctm = fz_concat(m, *ctm);
  791. }
  792. void fz_default_image_decode(void *arg, int w, int h, int l2factor, fz_irect *subarea)
  793. {
  794. (void)arg;
  795. if ((subarea->x1-subarea->x0)*(subarea->y1-subarea->y0) >= (w*h/10)*9)
  796. {
  797. /* Either no subarea specified, or a subarea 90% or more of the
  798. * whole area specified. Use the whole image. */
  799. subarea->x0 = 0;
  800. subarea->y0 = 0;
  801. subarea->x1 = w;
  802. subarea->y1 = h;
  803. }
  804. else
  805. {
  806. /* Clip to the edges if they are within 1% */
  807. if (subarea->x0 <= w/100)
  808. subarea->x0 = 0;
  809. if (subarea->y0 <= h/100)
  810. subarea->y0 = 0;
  811. if (subarea->x1 >= w*99/100)
  812. subarea->x1 = w;
  813. if (subarea->y1 >= h*99/100)
  814. subarea->y1 = h;
  815. }
  816. }
  817. static fz_pixmap *
  818. fz_find_image_tile(fz_context *ctx, fz_image *image, fz_image_key *key, fz_matrix *ctm)
  819. {
  820. fz_pixmap *tile;
  821. do
  822. {
  823. tile = fz_find_item(ctx, fz_drop_pixmap_imp, key, &fz_image_store_type);
  824. if (tile)
  825. {
  826. update_ctm_for_subarea(ctm, &key->rect, image->w, image->h);
  827. return tile;
  828. }
  829. key->l2factor--;
  830. }
  831. while (key->l2factor >= 0);
  832. return NULL;
  833. }
  834. fz_pixmap *
  835. fz_get_pixmap_from_image(fz_context *ctx, fz_image *image, const fz_irect *subarea, fz_matrix *ctm, int *dw, int *dh)
  836. {
  837. fz_pixmap *tile;
  838. int l2factor, l2factor_remaining;
  839. fz_image_key key;
  840. fz_image_key *keyp = NULL;
  841. int w;
  842. int h;
  843. fz_var(keyp);
  844. if (!image)
  845. return NULL;
  846. /* Figure out the extent. */
  847. if (ctm)
  848. {
  849. w = sqrtf(ctm->a * ctm->a + ctm->b * ctm->b);
  850. h = sqrtf(ctm->c * ctm->c + ctm->d * ctm->d);
  851. }
  852. else
  853. {
  854. w = image->w;
  855. h = image->h;
  856. }
  857. if (image->scalable)
  858. {
  859. /* If the image is scalable, we always want to re-render and never cache. */
  860. fz_irect subarea_copy;
  861. if (subarea)
  862. subarea_copy = *subarea;
  863. l2factor_remaining = 0;
  864. if (dw) *dw = w;
  865. if (dh) *dh = h;
  866. return image->get_pixmap(ctx, image, subarea ? &subarea_copy : NULL, image->w, image->h, &l2factor_remaining);
  867. }
  868. /* Clamp requested image size, since we never want to magnify images here. */
  869. if (w > image->w)
  870. w = image->w;
  871. if (h > image->h)
  872. h = image->h;
  873. if (image->decoded)
  874. {
  875. /* If the image is already decoded, then we can't offer a subarea,
  876. * or l2factor, and we don't want to cache. */
  877. l2factor_remaining = 0;
  878. if (dw) *dw = w;
  879. if (dh) *dh = h;
  880. return image->get_pixmap(ctx, image, NULL, image->w, image->h, &l2factor_remaining);
  881. }
  882. /* What is our ideal factor? We search for the largest factor where
  883. * we can subdivide and stay larger than the required size. We add
  884. * a fudge factor of +2 here to allow for the possibility of
  885. * expansion due to grid fitting. */
  886. l2factor = 0;
  887. if (w > 0 && h > 0)
  888. {
  889. while (image->w>>(l2factor+1) >= w+2 && image->h>>(l2factor+1) >= h+2 && l2factor < 6)
  890. l2factor++;
  891. }
  892. /* First, look through the store for existing tiles */
  893. if (subarea)
  894. {
  895. fz_compute_image_key(ctx, image, ctm, &key, subarea, l2factor, &w, &h, dw, dh);
  896. tile = fz_find_image_tile(ctx, image, &key, ctm);
  897. if (tile)
  898. return tile;
  899. }
  900. /* No subarea given, or no tile for subarea found; try entire image */
  901. fz_compute_image_key(ctx, image, ctm, &key, NULL, l2factor, &w, &h, dw, dh);
  902. tile = fz_find_image_tile(ctx, image, &key, ctm);
  903. if (tile)
  904. return tile;
  905. /* Neither subarea nor full image tile found; prepare the subarea key again */
  906. if (subarea)
  907. fz_compute_image_key(ctx, image, ctm, &key, subarea, l2factor, &w, &h, dw, dh);
  908. /* We'll have to decode the image; request the correct amount of downscaling. */
  909. l2factor_remaining = l2factor;
  910. tile = image->get_pixmap(ctx, image, &key.rect, w, h, &l2factor_remaining);
  911. /* Update the ctm to allow for subareas. */
  912. update_ctm_for_subarea(ctm, &key.rect, image->w, image->h);
  913. /* l2factor_remaining is updated to the amount of subscaling left to do */
  914. assert(l2factor_remaining >= 0 && l2factor_remaining <= 6);
  915. if (l2factor_remaining)
  916. {
  917. fz_try(ctx)
  918. fz_subsample_pixmap(ctx, tile, l2factor_remaining);
  919. fz_catch(ctx)
  920. {
  921. fz_drop_pixmap(ctx, tile);
  922. fz_rethrow(ctx);
  923. }
  924. }
  925. fz_try(ctx)
  926. {
  927. fz_pixmap *existing_tile;
  928. /* Now we try to cache the pixmap. Any failure here will just result
  929. * in us not caching. */
  930. keyp = fz_malloc_struct(ctx, fz_image_key);
  931. keyp->refs = 1;
  932. keyp->image = fz_keep_image_store_key(ctx, image);
  933. keyp->l2factor = l2factor;
  934. keyp->rect = key.rect;
  935. existing_tile = fz_store_item(ctx, keyp, tile, fz_pixmap_size(ctx, tile), &fz_image_store_type);
  936. if (existing_tile)
  937. {
  938. /* We already have a tile. This must have been produced by a
  939. * racing thread. We'll throw away ours and use that one. */
  940. fz_drop_pixmap(ctx, tile);
  941. tile = existing_tile;
  942. }
  943. }
  944. fz_always(ctx)
  945. {
  946. fz_drop_image_key(ctx, keyp);
  947. }
  948. fz_catch(ctx)
  949. {
  950. /* Do nothing */
  951. fz_rethrow_if(ctx, FZ_ERROR_SYSTEM);
  952. fz_report_error(ctx);
  953. }
  954. return tile;
  955. }
  956. fz_pixmap *
  957. fz_get_unscaled_pixmap_from_image(fz_context *ctx, fz_image *image)
  958. {
  959. return fz_get_pixmap_from_image(ctx, image, NULL /*subarea*/, NULL /*ctm*/, NULL /*dw*/, NULL /*dh*/);
  960. }
  961. static size_t
  962. pixmap_image_get_size(fz_context *ctx, fz_image *image)
  963. {
  964. fz_pixmap_image *im = (fz_pixmap_image *)image;
  965. if (image == NULL)
  966. return 0;
  967. return sizeof(fz_pixmap_image) + fz_pixmap_size(ctx, im->tile);
  968. }
  969. size_t fz_image_size(fz_context *ctx, fz_image *im)
  970. {
  971. if (im == NULL)
  972. return 0;
  973. return im->get_size(ctx, im);
  974. }
  975. fz_image *
  976. fz_new_image_from_pixmap(fz_context *ctx, fz_pixmap *pixmap, fz_image *mask)
  977. {
  978. fz_pixmap_image *image;
  979. image = fz_new_derived_image(ctx, pixmap->w, pixmap->h, 8, pixmap->colorspace,
  980. pixmap->xres, pixmap->yres, 0, 0,
  981. NULL, NULL, mask, fz_pixmap_image,
  982. pixmap_image_get_pixmap,
  983. pixmap_image_get_size,
  984. drop_pixmap_image);
  985. image->tile = fz_keep_pixmap(ctx, pixmap);
  986. image->super.decoded = 1;
  987. return &image->super;
  988. }
  989. fz_image *
  990. fz_new_image_of_size(fz_context *ctx, int w, int h, int bpc, fz_colorspace *colorspace,
  991. int xres, int yres, int interpolate, int imagemask, const float *decode,
  992. const int *colorkey, fz_image *mask, size_t size,
  993. fz_image_get_pixmap_fn *get_pixmap,
  994. fz_image_get_size_fn *get_size,
  995. fz_drop_image_fn *drop)
  996. {
  997. fz_image *image;
  998. int i;
  999. assert(mask == NULL || mask->mask == NULL);
  1000. assert(size >= sizeof(fz_image));
  1001. image = Memento_label(fz_calloc(ctx, 1, size), "fz_image");
  1002. FZ_INIT_KEY_STORABLE(image, 1, fz_drop_image_imp);
  1003. image->drop_image = drop;
  1004. image->get_pixmap = get_pixmap;
  1005. image->get_size = get_size;
  1006. image->w = w;
  1007. image->h = h;
  1008. image->xres = xres;
  1009. image->yres = yres;
  1010. image->bpc = bpc;
  1011. image->n = (colorspace ? fz_colorspace_n(ctx, colorspace) : 1);
  1012. image->colorspace = fz_keep_colorspace(ctx, colorspace);
  1013. image->interpolate = interpolate;
  1014. image->imagemask = imagemask;
  1015. image->intent = 0;
  1016. image->has_intent = 0;
  1017. image->use_colorkey = (colorkey != NULL);
  1018. if (colorkey)
  1019. memcpy(image->colorkey, colorkey, sizeof(int)*image->n*2);
  1020. image->use_decode = 0;
  1021. if (decode)
  1022. {
  1023. memcpy(image->decode, decode, sizeof(float)*image->n*2);
  1024. }
  1025. else
  1026. {
  1027. float maxval = fz_colorspace_is_indexed(ctx, colorspace) ? (1 << bpc) - 1 : 1;
  1028. for (i = 0; i < image->n; i++)
  1029. {
  1030. image->decode[2*i] = 0;
  1031. image->decode[2*i+1] = maxval;
  1032. }
  1033. }
  1034. /* ICC spaces have the default decode arrays pickled into them.
  1035. * For most spaces this is fine, because [ 0 1 0 1 0 1 ] is
  1036. * idempotent. For Lab, however, we need to adjust it. */
  1037. if (fz_colorspace_is_lab_icc(ctx, colorspace))
  1038. {
  1039. /* Undo the default decode array of [0 100 -128 127 -128 127] */
  1040. image->decode[0] = image->decode[0]/100.0f;
  1041. image->decode[1] = image->decode[1]/100.0f;
  1042. image->decode[2] = (image->decode[2]+128)/255.0f;
  1043. image->decode[3] = (image->decode[3]+128)/255.0f;
  1044. image->decode[4] = (image->decode[4]+128)/255.0f;
  1045. image->decode[5] = (image->decode[5]+128)/255.0f;
  1046. }
  1047. for (i = 0; i < image->n; i++)
  1048. {
  1049. if (image->decode[i * 2] != 0 || image->decode[i * 2 + 1] != 1)
  1050. break;
  1051. }
  1052. if (i != image->n)
  1053. image->use_decode = 1;
  1054. image->mask = fz_keep_image(ctx, mask);
  1055. return image;
  1056. }
  1057. static size_t
  1058. compressed_image_get_size(fz_context *ctx, fz_image *image)
  1059. {
  1060. fz_compressed_image *im = (fz_compressed_image *)image;
  1061. if (image == NULL)
  1062. return 0;
  1063. return sizeof(fz_pixmap_image) + (im->buffer && im->buffer->buffer ? im->buffer->buffer->cap : 0);
  1064. }
  1065. fz_image *
  1066. fz_new_image_from_compressed_buffer(fz_context *ctx, int w, int h,
  1067. int bpc, fz_colorspace *colorspace,
  1068. int xres, int yres, int interpolate, int imagemask, const float *decode,
  1069. const int *colorkey, fz_compressed_buffer *buffer, fz_image *mask)
  1070. {
  1071. fz_compressed_image *image;
  1072. fz_try(ctx)
  1073. {
  1074. image = fz_new_derived_image(ctx, w, h, bpc,
  1075. colorspace, xres, yres,
  1076. interpolate, imagemask, decode,
  1077. colorkey, mask, fz_compressed_image,
  1078. compressed_image_get_pixmap,
  1079. compressed_image_get_size,
  1080. drop_compressed_image);
  1081. image->buffer = buffer;
  1082. }
  1083. fz_catch(ctx)
  1084. {
  1085. fz_drop_compressed_buffer(ctx, buffer);
  1086. fz_rethrow(ctx);
  1087. }
  1088. return &image->super;
  1089. }
  1090. fz_compressed_buffer *fz_compressed_image_buffer(fz_context *ctx, fz_image *image)
  1091. {
  1092. if (image == NULL || image->get_pixmap != compressed_image_get_pixmap)
  1093. return NULL;
  1094. return ((fz_compressed_image *)image)->buffer;
  1095. }
  1096. void fz_set_compressed_image_buffer(fz_context *ctx, fz_compressed_image *image, fz_compressed_buffer *buf)
  1097. {
  1098. assert(image != NULL && image->super.get_pixmap == compressed_image_get_pixmap);
  1099. ((fz_compressed_image *)image)->buffer = buf; /* Note: compressed buffers are not reference counted */
  1100. }
  1101. fz_pixmap *fz_pixmap_image_tile(fz_context *ctx, fz_pixmap_image *image)
  1102. {
  1103. if (image == NULL || image->super.get_pixmap != pixmap_image_get_pixmap)
  1104. return NULL;
  1105. return ((fz_pixmap_image *)image)->tile;
  1106. }
  1107. void fz_set_pixmap_image_tile(fz_context *ctx, fz_pixmap_image *image, fz_pixmap *pix)
  1108. {
  1109. assert(image != NULL && image->super.get_pixmap == pixmap_image_get_pixmap);
  1110. ((fz_pixmap_image *)image)->tile = pix;
  1111. }
  1112. const char *
  1113. fz_image_type_name(int type)
  1114. {
  1115. switch (type)
  1116. {
  1117. default:
  1118. case FZ_IMAGE_UNKNOWN: return "unknown";
  1119. case FZ_IMAGE_RAW: return "raw";
  1120. case FZ_IMAGE_FAX: return "fax";
  1121. case FZ_IMAGE_FLATE: return "flate";
  1122. case FZ_IMAGE_LZW: return "lzw";
  1123. case FZ_IMAGE_RLD: return "rld";
  1124. case FZ_IMAGE_BROTLI: return "brotli";
  1125. case FZ_IMAGE_BMP: return "bmp";
  1126. case FZ_IMAGE_GIF: return "gif";
  1127. case FZ_IMAGE_JBIG2: return "jbig2";
  1128. case FZ_IMAGE_JPEG: return "jpeg";
  1129. case FZ_IMAGE_JPX: return "jpx";
  1130. case FZ_IMAGE_JXR: return "jxr";
  1131. case FZ_IMAGE_PNG: return "png";
  1132. case FZ_IMAGE_PNM: return "pnm";
  1133. case FZ_IMAGE_TIFF: return "tiff";
  1134. }
  1135. }
  1136. int
  1137. fz_lookup_image_type(const char *type)
  1138. {
  1139. if (type == NULL) return FZ_IMAGE_UNKNOWN;
  1140. if (!strcmp(type, "raw")) return FZ_IMAGE_RAW;
  1141. if (!strcmp(type, "fax")) return FZ_IMAGE_FAX;
  1142. if (!strcmp(type, "flate")) return FZ_IMAGE_FLATE;
  1143. if (!strcmp(type, "lzw")) return FZ_IMAGE_LZW;
  1144. if (!strcmp(type, "rld")) return FZ_IMAGE_RLD;
  1145. if (!strcmp(type, "brotli")) return FZ_IMAGE_BROTLI;
  1146. if (!strcmp(type, "bmp")) return FZ_IMAGE_BMP;
  1147. if (!strcmp(type, "gif")) return FZ_IMAGE_GIF;
  1148. if (!strcmp(type, "jbig2")) return FZ_IMAGE_JBIG2;
  1149. if (!strcmp(type, "jpeg")) return FZ_IMAGE_JPEG;
  1150. if (!strcmp(type, "jpx")) return FZ_IMAGE_JPX;
  1151. if (!strcmp(type, "jxr")) return FZ_IMAGE_JXR;
  1152. if (!strcmp(type, "png")) return FZ_IMAGE_PNG;
  1153. if (!strcmp(type, "pnm")) return FZ_IMAGE_PNM;
  1154. if (!strcmp(type, "tiff")) return FZ_IMAGE_TIFF;
  1155. return FZ_IMAGE_UNKNOWN;
  1156. }
  1157. int
  1158. fz_recognize_image_format(fz_context *ctx, unsigned char p[8])
  1159. {
  1160. if (p[0] == 'P' && p[1] >= '1' && p[1] <= '7')
  1161. return FZ_IMAGE_PNM;
  1162. if (p[0] == 'P' && (p[1] == 'F' || p[1] == 'f'))
  1163. return FZ_IMAGE_PNM;
  1164. if (p[0] == 0xff && p[1] == 0x4f)
  1165. return FZ_IMAGE_JPX;
  1166. if (p[0] == 0x00 && p[1] == 0x00 && p[2] == 0x00 && p[3] == 0x0c &&
  1167. p[4] == 0x6a && p[5] == 0x50 && p[6] == 0x20 && p[7] == 0x20)
  1168. return FZ_IMAGE_JPX;
  1169. if (p[0] == 0xff && p[1] == 0xd8)
  1170. return FZ_IMAGE_JPEG;
  1171. if (p[0] == 137 && p[1] == 80 && p[2] == 78 && p[3] == 71 &&
  1172. p[4] == 13 && p[5] == 10 && p[6] == 26 && p[7] == 10)
  1173. return FZ_IMAGE_PNG;
  1174. if (p[0] == 'I' && p[1] == 'I' && p[2] == 0xBC)
  1175. return FZ_IMAGE_JXR;
  1176. if (p[0] == 'I' && p[1] == 'I' && p[2] == 42 && p[3] == 0)
  1177. return FZ_IMAGE_TIFF;
  1178. if (p[0] == 'M' && p[1] == 'M' && p[2] == 0 && p[3] == 42)
  1179. return FZ_IMAGE_TIFF;
  1180. if (p[0] == 'G' && p[1] == 'I' && p[2] == 'F')
  1181. return FZ_IMAGE_GIF;
  1182. if (p[0] == 'B' && p[1] == 'M')
  1183. return FZ_IMAGE_BMP;
  1184. if (p[0] == 'B' && p[1] == 'A')
  1185. return FZ_IMAGE_BMP;
  1186. if (p[0] == 0x97 && p[1] == 'J' && p[2] == 'B' && p[3] == '2' &&
  1187. p[4] == '\r' && p[5] == '\n' && p[6] == 0x1a && p[7] == '\n')
  1188. return FZ_IMAGE_JBIG2;
  1189. if (p[0] == '8' && p[1] == 'B' && p[2] == 'P' && p[3] == 'S')
  1190. return FZ_IMAGE_PSD;
  1191. return FZ_IMAGE_UNKNOWN;
  1192. }
  1193. fz_image *
  1194. fz_new_image_from_buffer(fz_context *ctx, fz_buffer *buffer)
  1195. {
  1196. fz_compressed_buffer *bc;
  1197. int w, h, xres, yres;
  1198. fz_colorspace *cspace;
  1199. size_t len = buffer->len;
  1200. unsigned char *buf = buffer->data;
  1201. fz_image *image = NULL;
  1202. int type;
  1203. int bpc;
  1204. uint8_t orientation = 0;
  1205. if (len < 8)
  1206. fz_throw(ctx, FZ_ERROR_FORMAT, "unknown image file format");
  1207. type = fz_recognize_image_format(ctx, buf);
  1208. bpc = 8;
  1209. switch (type)
  1210. {
  1211. case FZ_IMAGE_PNM:
  1212. fz_load_pnm_info(ctx, buf, len, &w, &h, &xres, &yres, &cspace);
  1213. break;
  1214. case FZ_IMAGE_JPX:
  1215. fz_load_jpx_info(ctx, buf, len, &w, &h, &xres, &yres, &cspace);
  1216. break;
  1217. case FZ_IMAGE_JPEG:
  1218. fz_load_jpeg_info(ctx, buf, len, &w, &h, &xres, &yres, &cspace, &orientation);
  1219. break;
  1220. case FZ_IMAGE_PNG:
  1221. fz_load_png_info(ctx, buf, len, &w, &h, &xres, &yres, &cspace);
  1222. break;
  1223. case FZ_IMAGE_PSD:
  1224. fz_load_psd_info(ctx, buf, len, &w, &h, &xres, &yres, &cspace);
  1225. break;
  1226. case FZ_IMAGE_JXR:
  1227. fz_load_jxr_info(ctx, buf, len, &w, &h, &xres, &yres, &cspace);
  1228. break;
  1229. case FZ_IMAGE_TIFF:
  1230. fz_load_tiff_info(ctx, buf, len, &w, &h, &xres, &yres, &cspace);
  1231. break;
  1232. case FZ_IMAGE_GIF:
  1233. fz_load_gif_info(ctx, buf, len, &w, &h, &xres, &yres, &cspace);
  1234. break;
  1235. case FZ_IMAGE_BMP:
  1236. fz_load_bmp_info(ctx, buf, len, &w, &h, &xres, &yres, &cspace);
  1237. break;
  1238. case FZ_IMAGE_JBIG2:
  1239. fz_load_jbig2_info(ctx, buf, len, &w, &h, &xres, &yres, &cspace);
  1240. bpc = 1;
  1241. break;
  1242. default:
  1243. fz_throw(ctx, FZ_ERROR_FORMAT, "unknown image file format");
  1244. }
  1245. fz_try(ctx)
  1246. {
  1247. bc = fz_new_compressed_buffer(ctx);
  1248. bc->buffer = fz_keep_buffer(ctx, buffer);
  1249. bc->params.type = type;
  1250. if (type == FZ_IMAGE_JPEG)
  1251. {
  1252. bc->params.u.jpeg.color_transform = -1;
  1253. bc->params.u.jpeg.invert_cmyk = 1;
  1254. }
  1255. image = fz_new_image_from_compressed_buffer(ctx, w, h, bpc, cspace, xres, yres, 0, 0, NULL, NULL, bc, NULL);
  1256. image->orientation = orientation;
  1257. }
  1258. fz_always(ctx)
  1259. fz_drop_colorspace(ctx, cspace);
  1260. fz_catch(ctx)
  1261. fz_rethrow(ctx);
  1262. return image;
  1263. }
  1264. int
  1265. fz_compressed_image_type(fz_context *ctx, fz_image *image)
  1266. {
  1267. fz_compressed_image *cim;
  1268. if (image == NULL || image->drop_image != drop_compressed_image)
  1269. return FZ_IMAGE_UNKNOWN;
  1270. cim = (fz_compressed_image *)image;
  1271. return cim->buffer->params.type;
  1272. }
  1273. fz_image *
  1274. fz_new_image_from_file(fz_context *ctx, const char *path)
  1275. {
  1276. fz_buffer *buffer;
  1277. fz_image *image = NULL;
  1278. buffer = fz_read_file(ctx, path);
  1279. fz_try(ctx)
  1280. image = fz_new_image_from_buffer(ctx, buffer);
  1281. fz_always(ctx)
  1282. fz_drop_buffer(ctx, buffer);
  1283. fz_catch(ctx)
  1284. fz_rethrow(ctx);
  1285. return image;
  1286. }
  1287. void
  1288. fz_image_resolution(fz_image *image, int *xres, int *yres)
  1289. {
  1290. *xres = image->xres;
  1291. *yres = image->yres;
  1292. if (*xres < 0 || *yres < 0 || (*xres == 0 && *yres == 0))
  1293. {
  1294. /* If neither xres or yres is sane, pick a sane value */
  1295. *xres = SANE_DPI; *yres = SANE_DPI;
  1296. }
  1297. else if (*xres == 0)
  1298. {
  1299. *xres = *yres;
  1300. }
  1301. else if (*yres == 0)
  1302. {
  1303. *yres = *xres;
  1304. }
  1305. /* Scale xres and yres up until we get believable values */
  1306. if (*xres < SANE_DPI || *yres < SANE_DPI || *xres > INSANE_DPI || *yres > INSANE_DPI)
  1307. {
  1308. if (*xres < *yres)
  1309. {
  1310. *yres = *yres * SANE_DPI / *xres;
  1311. *xres = SANE_DPI;
  1312. }
  1313. else
  1314. {
  1315. *xres = *xres * SANE_DPI / *yres;
  1316. *yres = SANE_DPI;
  1317. }
  1318. if (*xres == *yres || *xres < SANE_DPI || *yres < SANE_DPI || *xres > INSANE_DPI || *yres > INSANE_DPI)
  1319. {
  1320. *xres = SANE_DPI;
  1321. *yres = SANE_DPI;
  1322. }
  1323. }
  1324. }
  1325. uint8_t fz_image_orientation(fz_context *ctx, fz_image *image)
  1326. {
  1327. return image ? image->orientation : 0;
  1328. }
  1329. fz_matrix fz_image_orientation_matrix(fz_context *ctx, fz_image *image)
  1330. {
  1331. fz_matrix m;
  1332. switch (image ? image->orientation : 0)
  1333. {
  1334. case 0:
  1335. case 1: /* 0 degree rotation */
  1336. m.a = 1; m.b = 0;
  1337. m.c = 0; m.d = 1;
  1338. m.e = 0; m.f = 0;
  1339. break;
  1340. case 2: /* 90 degree ccw */
  1341. m.a = 0; m.b = -1;
  1342. m.c = 1; m.d = 0;
  1343. m.e = 0; m.f = 1;
  1344. break;
  1345. case 3: /* 180 degree ccw */
  1346. m.a = -1; m.b = 0;
  1347. m.c = 0; m.d = -1;
  1348. m.e = 1; m.f = 1;
  1349. break;
  1350. case 4: /* 270 degree ccw */
  1351. m.a = 0; m.b = 1;
  1352. m.c = -1; m.d = 0;
  1353. m.e = 1; m.f = 0;
  1354. break;
  1355. case 5: /* flip on X */
  1356. m.a = -1; m.b = 0;
  1357. m.c = 0; m.d = 1;
  1358. m.e = 1; m.f = 0;
  1359. break;
  1360. case 6: /* flip on X, then rotate ccw by 90 degrees */
  1361. m.a = 0; m.b = 1;
  1362. m.c = 1; m.d = 0;
  1363. m.e = 0; m.f = 0;
  1364. break;
  1365. case 7: /* flip on X, then rotate ccw by 180 degrees */
  1366. m.a = 1; m.b = 0;
  1367. m.c = 0; m.d = -1;
  1368. m.e = 0; m.f = 1;
  1369. break;
  1370. case 8: /* flip on X, then rotate ccw by 270 degrees */
  1371. m.a = 0; m.b = -1;
  1372. m.c = -1; m.d = 0;
  1373. m.e = 1; m.f = 1;
  1374. break;
  1375. }
  1376. return m;
  1377. }
  1378. typedef struct fz_display_list_image_s
  1379. {
  1380. fz_image super;
  1381. fz_matrix transform;
  1382. fz_display_list *list;
  1383. } fz_display_list_image;
  1384. static fz_pixmap *
  1385. display_list_image_get_pixmap(fz_context *ctx, fz_image *image_, fz_irect *subarea, int w, int h, int *l2factor)
  1386. {
  1387. fz_display_list_image *image = (fz_display_list_image *)image_;
  1388. fz_matrix ctm;
  1389. fz_device *dev;
  1390. fz_pixmap *pix;
  1391. fz_var(dev);
  1392. if (subarea)
  1393. {
  1394. /* So, the whole image should be scaled to w * h, but we only want the
  1395. * given subarea of it. */
  1396. int l = (subarea->x0 * w) / image->super.w;
  1397. int t = (subarea->y0 * h) / image->super.h;
  1398. int r = (subarea->x1 * w + image->super.w - 1) / image->super.w;
  1399. int b = (subarea->y1 * h + image->super.h - 1) / image->super.h;
  1400. pix = fz_new_pixmap(ctx, image->super.colorspace, r-l, b-t, NULL, 0);
  1401. pix->x = l;
  1402. pix->y = t;
  1403. }
  1404. else
  1405. {
  1406. pix = fz_new_pixmap(ctx, image->super.colorspace, w, h, NULL, 0);
  1407. }
  1408. /* If we render the display list into pix with the image matrix, we'll get a unit
  1409. * square result. Therefore scale by w, h. */
  1410. ctm = fz_pre_scale(image->transform, w, h);
  1411. fz_clear_pixmap(ctx, pix); /* clear to transparent */
  1412. fz_try(ctx)
  1413. {
  1414. dev = fz_new_draw_device(ctx, ctm, pix);
  1415. fz_run_display_list(ctx, image->list, dev, fz_identity, fz_infinite_rect, NULL);
  1416. fz_close_device(ctx, dev);
  1417. }
  1418. fz_always(ctx)
  1419. fz_drop_device(ctx, dev);
  1420. fz_catch(ctx)
  1421. {
  1422. fz_drop_pixmap(ctx, pix);
  1423. fz_rethrow(ctx);
  1424. }
  1425. /* Never do more subsampling, cos we've already given them the right size */
  1426. if (l2factor)
  1427. *l2factor = 0;
  1428. return pix;
  1429. }
  1430. static void drop_display_list_image(fz_context *ctx, fz_image *image_)
  1431. {
  1432. fz_display_list_image *image = (fz_display_list_image *)image_;
  1433. if (image == NULL)
  1434. return;
  1435. fz_drop_display_list(ctx, image->list);
  1436. }
  1437. static size_t
  1438. display_list_image_get_size(fz_context *ctx, fz_image *image_)
  1439. {
  1440. fz_display_list_image *image = (fz_display_list_image *)image_;
  1441. if (image == NULL)
  1442. return 0;
  1443. return sizeof(fz_display_list_image) + 4096; /* FIXME */
  1444. }
  1445. fz_image *fz_new_image_from_display_list(fz_context *ctx, float w, float h, fz_display_list *list)
  1446. {
  1447. fz_display_list_image *image;
  1448. int iw, ih;
  1449. iw = w * SCALABLE_IMAGE_DPI / 72;
  1450. ih = h * SCALABLE_IMAGE_DPI / 72;
  1451. image = fz_new_derived_image(ctx, iw, ih, 8, fz_device_rgb(ctx),
  1452. SCALABLE_IMAGE_DPI, SCALABLE_IMAGE_DPI, 0, 0,
  1453. NULL, NULL, NULL, fz_display_list_image,
  1454. display_list_image_get_pixmap,
  1455. display_list_image_get_size,
  1456. drop_display_list_image);
  1457. image->super.scalable = 1;
  1458. image->transform = fz_scale(1 / w, 1 / h);
  1459. image->list = fz_keep_display_list(ctx, list);
  1460. return &image->super;
  1461. }