load-psd.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. // Copyright (C) 2004-2024 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 <string.h>
  26. struct info
  27. {
  28. unsigned int width, height, n;
  29. int xres, yres;
  30. fz_colorspace *cs;
  31. };
  32. typedef struct
  33. {
  34. fz_context *ctx;
  35. const unsigned char *p;
  36. size_t total;
  37. int packbits;
  38. int packbits_n;
  39. int packbits_rep;
  40. } source_t;
  41. static int
  42. get8(source_t *source)
  43. {
  44. if (source->total < 1)
  45. fz_throw(source->ctx, FZ_ERROR_FORMAT, "Truncated PSD");
  46. source->total--;
  47. return *source->p++;
  48. }
  49. static int
  50. get16be(source_t *source)
  51. {
  52. int v;
  53. if (source->total < 2)
  54. {
  55. source->total = 0;
  56. fz_throw(source->ctx, FZ_ERROR_FORMAT, "Truncated PSD");
  57. }
  58. source->total -= 2;
  59. v = *source->p++;
  60. v = (v<<8) | *source->p++;
  61. return v;
  62. }
  63. static int
  64. get32be(source_t *source)
  65. {
  66. int v;
  67. if (source->total < 4)
  68. {
  69. source->total = 0;
  70. fz_throw(source->ctx, FZ_ERROR_FORMAT, "Truncated PSD");
  71. }
  72. source->total -= 4;
  73. v = *source->p++;
  74. v = (v<<8) | *source->p++;
  75. v = (v<<8) | *source->p++;
  76. v = (v<<8) | *source->p++;
  77. return v;
  78. }
  79. static uint32_t
  80. getu32be(source_t *source)
  81. {
  82. return (uint32_t)get32be(source);
  83. }
  84. static int
  85. unpack8(source_t *source)
  86. {
  87. int i;
  88. if (source->packbits == 0)
  89. return get8(source);
  90. i = source->packbits_n;
  91. if (i == 128)
  92. {
  93. do
  94. {
  95. i = source->packbits_n = get8(source);
  96. }
  97. while (i == 128);
  98. if (i > 128)
  99. source->packbits_rep = get8(source);
  100. }
  101. if (i < 128)
  102. {
  103. /* Literal n+1 */
  104. i--;
  105. if (i < 0)
  106. i = 128;
  107. source->packbits_n = i;
  108. return get8(source);
  109. }
  110. else
  111. {
  112. i++;
  113. if (i == 257)
  114. i = 128;
  115. source->packbits_n = i;
  116. return source->packbits_rep;
  117. }
  118. }
  119. static char *getString(source_t *source)
  120. {
  121. size_t len = get8(source);
  122. size_t odd = !(len & 1);
  123. char *s;
  124. if (source->total < len + odd)
  125. {
  126. source->total = 0;
  127. fz_throw(source->ctx, FZ_ERROR_FORMAT, "Truncated string in PSD");
  128. }
  129. s = fz_malloc(source->ctx, len+1);
  130. memcpy(s, source->p, len);
  131. s[len] = 0;
  132. source->p += len + odd;
  133. source->total -= len + odd;
  134. return s;
  135. }
  136. static fz_pixmap *
  137. psd_read_image(fz_context *ctx, struct info *info, const unsigned char *p, size_t total, int only_metadata)
  138. {
  139. int v, bpc, c, n;
  140. source_t source;
  141. size_t ir_len, data_len;
  142. fz_separations *seps = NULL;
  143. fz_pixmap *image = NULL;
  144. size_t m;
  145. unsigned char *q;
  146. int alpha = 0;
  147. source.ctx = ctx;
  148. source.p = p;
  149. source.total = total;
  150. source.packbits = 0;
  151. memset(info, 0, sizeof(*info));
  152. fz_var(image);
  153. fz_var(seps);
  154. fz_try(ctx)
  155. {
  156. info->xres = 96;
  157. info->yres = 96;
  158. v = get32be(&source);
  159. /* Read signature */
  160. if (v != 0x38425053 /* 8BPS */)
  161. fz_throw(ctx, FZ_ERROR_FORMAT, "not a psd image (wrong signature)");
  162. /* Version */
  163. v = get16be(&source);
  164. if (v != 1)
  165. fz_throw(ctx, FZ_ERROR_FORMAT, "Bad PSD version");
  166. (void)get16be(&source);
  167. (void)get32be(&source);
  168. info->n = n = get16be(&source);
  169. info->height = getu32be(&source);
  170. info->width = getu32be(&source);
  171. bpc = get16be(&source);
  172. if (bpc != 8 && bpc != 16)
  173. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "Only 8 or 16 bpc PSD files supported!");
  174. c = get16be(&source);
  175. if (c == 4) /* CMYK (+ Spots?) */
  176. {
  177. if (n != 4)
  178. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "CMYK PSD with %d chans not supported!", n);
  179. info->cs = fz_keep_colorspace(ctx, fz_device_cmyk(ctx));
  180. }
  181. else if (c == 3) /* RGB */
  182. {
  183. if (n == 4)
  184. alpha = 1;
  185. else if (n != 3)
  186. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "RGB PSD with %d chans not supported!", n);
  187. info->cs = fz_keep_colorspace(ctx, fz_device_rgb(ctx));
  188. }
  189. else if (c == 1) /* Greyscale */
  190. {
  191. if (n != 1)
  192. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "Greyscale PSD with %d chans not supported!", n);
  193. info->cs = fz_keep_colorspace(ctx, fz_device_gray(ctx));
  194. }
  195. else
  196. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "Unsupported PSD colorspace (%d)!", c);
  197. v = get32be(&source);
  198. if (v != 0)
  199. fz_throw(ctx, FZ_ERROR_FORMAT, "Unexpected color data in PSD!");
  200. /* Now read image resources... */
  201. ir_len = getu32be(&source);
  202. while (ir_len >= 12)
  203. {
  204. size_t start = source.p - p;
  205. v = get32be(&source);
  206. if (v != 0x3842494d) /* 8BIM */
  207. fz_throw(ctx, FZ_ERROR_FORMAT, "Failed to find expected 8BIM in PSD");
  208. v = get16be(&source);
  209. fz_free(ctx, getString(&source));
  210. data_len = getu32be(&source);
  211. ir_len -= (source.p - p) - start;
  212. switch (v)
  213. {
  214. case 0x3ef: /* Spot */
  215. {
  216. int spots = 0;
  217. int alpha_found = 0;
  218. while (data_len > 0)
  219. {
  220. int C, M, Y, K;
  221. char text[32];
  222. v = get16be(&source);
  223. if (v == 0 && alpha_found == 0)
  224. alpha_found = 1, alpha = 1;
  225. else if (v != 2)
  226. fz_throw(ctx, FZ_ERROR_FORMAT, "Non CMYK spot found in PSD");
  227. C = 0xff - (get16be(&source)>>8);
  228. M = 0xff - (get16be(&source)>>8);
  229. Y = 0xff - (get16be(&source)>>8);
  230. K = 0xff - (get16be(&source)>>8);
  231. (void)get16be(&source); /* opacity */
  232. (void)get8(&source); /* kind */
  233. (void)get8(&source); /* padding */
  234. if (v == 2)
  235. {
  236. uint32_t cmyk = C | (M<<8) | (Y<<16) | (K<<24);
  237. int R = fz_clampi(255-C-K, 0, 255);
  238. int G = fz_clampi(255-M-K, 0, 255);
  239. int B = fz_clampi(255-Y-K, 0, 255);
  240. uint32_t rgba = R | (G<<8) | (B<<16);
  241. if (seps == NULL)
  242. seps = fz_new_separations(ctx, 1);
  243. snprintf(text, sizeof(text), "s%d", spots);
  244. /* Use the old entry-point until we fix the new one */
  245. fz_add_separation_equivalents(ctx, seps, rgba, cmyk, text);
  246. spots++;
  247. }
  248. data_len -= 14;
  249. ir_len -= 14;
  250. }
  251. }
  252. }
  253. /* Skip any unread data */
  254. if (data_len & 1)
  255. data_len++;
  256. ir_len -= data_len;
  257. while (data_len--)
  258. get8(&source);
  259. }
  260. if (fz_count_separations(ctx, seps) + info->cs->n + 1 == n && alpha == 0)
  261. alpha = 1;
  262. if (fz_count_separations(ctx, seps) + info->cs->n + alpha != n)
  263. fz_throw(ctx, FZ_ERROR_FORMAT, "PSD contains mismatching spot/alpha data");
  264. /* Skip over the Layer data. */
  265. v = get32be(&source);
  266. if (v != 0)
  267. {
  268. if (source.total < (size_t)v)
  269. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated PSD");
  270. source.total -= v;
  271. source.p += v;
  272. }
  273. if (source.total == 0)
  274. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "Unflattened PSD not supported");
  275. v = get16be(&source);
  276. switch (v)
  277. {
  278. case 0:
  279. /* No compression */
  280. break;
  281. case 1:
  282. /* Packbits */
  283. source.packbits = 1;
  284. source.packbits_n = 128;
  285. /* Skip over rows * channels * byte counts. */
  286. m = ((size_t)info->height) * info->n * 2;
  287. if (m > source.total)
  288. fz_throw(ctx, FZ_ERROR_FORMAT, "Truncated RLE PSD");
  289. source.total -= m;
  290. source.p += m;
  291. break;
  292. case 2: /* Deflate */
  293. case 3: /* Deflate with prediction */
  294. fz_throw(ctx, FZ_ERROR_UNSUPPORTED, "Deflate PSD not supported");
  295. default:
  296. fz_throw(ctx, FZ_ERROR_FORMAT, "Unexpected compression (%d) found in PSD", v);
  297. }
  298. if (only_metadata)
  299. break;
  300. m = ((size_t)info->width) * info->height;
  301. image = fz_new_pixmap(ctx, info->cs, info->width, info->height, seps, alpha);
  302. q = image->samples;
  303. if (bpc == 8)
  304. {
  305. if (n == 1)
  306. {
  307. while (m--)
  308. {
  309. *q++ = 255 - unpack8(&source);
  310. }
  311. }
  312. else if (n - alpha == 3)
  313. {
  314. int N = n;
  315. while (N--)
  316. {
  317. size_t M = m;
  318. while (M--)
  319. {
  320. *q = unpack8(&source);
  321. q += n;
  322. }
  323. q -= m*n - 1;
  324. }
  325. }
  326. else
  327. {
  328. int N = n - alpha;
  329. /* CMYK is inverted */
  330. while (N--)
  331. {
  332. size_t M = m;
  333. while (M--)
  334. {
  335. *q = 255 - unpack8(&source);
  336. q += n;
  337. }
  338. q -= m*n - 1;
  339. }
  340. /* But alpha is not */
  341. if (alpha)
  342. {
  343. size_t M = m;
  344. while (M--)
  345. {
  346. *q = unpack8(&source);
  347. q += n;
  348. }
  349. q -= m*n - 1;
  350. }
  351. }
  352. }
  353. else
  354. {
  355. if (n == 1)
  356. {
  357. while (m--)
  358. {
  359. *q++ = 255 - unpack8(&source);
  360. (void)unpack8(&source);
  361. }
  362. }
  363. else if (n - alpha == 3)
  364. {
  365. int N = n;
  366. while (N--)
  367. {
  368. size_t M = m;
  369. while (M--)
  370. {
  371. *q = unpack8(&source);
  372. (void)unpack8(&source);
  373. q += n;
  374. }
  375. q -= m*n - 1;
  376. }
  377. }
  378. else
  379. {
  380. int N = n - alpha;
  381. /* CMYK is inverted */
  382. while (N--)
  383. {
  384. size_t M = m;
  385. while (M--)
  386. {
  387. *q = 255 - unpack8(&source);
  388. (void)unpack8(&source);
  389. q += n;
  390. }
  391. q -= m*n - 1;
  392. }
  393. /* But alpha is not */
  394. if (alpha)
  395. {
  396. size_t M = m;
  397. while (M--)
  398. {
  399. *q = unpack8(&source);
  400. (void)unpack8(&source);
  401. q += n;
  402. }
  403. q -= m*n - 1;
  404. }
  405. }
  406. }
  407. if (alpha)
  408. fz_premultiply_pixmap(ctx, image);
  409. }
  410. fz_always(ctx)
  411. {
  412. fz_drop_separations(ctx, seps);
  413. }
  414. fz_catch(ctx)
  415. {
  416. fz_drop_pixmap(ctx, image);
  417. fz_drop_colorspace(ctx, info->cs);
  418. fz_rethrow(ctx);
  419. }
  420. return image;
  421. }
  422. fz_pixmap *
  423. fz_load_psd(fz_context *ctx, const unsigned char *p, size_t total)
  424. {
  425. fz_pixmap *image = NULL;
  426. struct info psd;
  427. image = psd_read_image(ctx, &psd, p, total, 0);
  428. fz_drop_colorspace(ctx, psd.cs);
  429. return image;
  430. }
  431. void
  432. fz_load_psd_info(fz_context *ctx, const unsigned char *p, size_t total, int *wp, int *hp, int *xresp, int *yresp, fz_colorspace **cspacep)
  433. {
  434. struct info psd;
  435. psd_read_image(ctx, &psd, p, total, 1);
  436. *cspacep = psd.cs;
  437. *wp = psd.width;
  438. *hp = psd.height;
  439. *xresp = psd.xres;
  440. *yresp = psd.xres;
  441. }