load-pnm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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 <string.h>
  25. #include <limits.h>
  26. enum
  27. {
  28. PAM_UNKNOWN = 0,
  29. PAM_BW,
  30. PAM_BWA,
  31. PAM_GRAY,
  32. PAM_GRAYA,
  33. PAM_RGB,
  34. PAM_RGBA,
  35. PAM_CMYK,
  36. PAM_CMYKA,
  37. };
  38. enum
  39. {
  40. TOKEN_UNKNOWN = 0,
  41. TOKEN_WIDTH,
  42. TOKEN_HEIGHT,
  43. TOKEN_DEPTH,
  44. TOKEN_MAXVAL,
  45. TOKEN_TUPLTYPE,
  46. TOKEN_ENDHDR,
  47. };
  48. enum
  49. {
  50. ENDIAN_UNKNOWN = 0,
  51. ENDIAN_LITTLE,
  52. ENDIAN_BIG,
  53. };
  54. struct info
  55. {
  56. int subimages;
  57. fz_colorspace *cs;
  58. int width, height;
  59. int maxval, bitdepth;
  60. int depth, alpha;
  61. int tupletype;
  62. int endian;
  63. float scale;
  64. };
  65. static inline int iswhiteeol(int a)
  66. {
  67. switch (a) {
  68. case ' ': case '\t': case '\r': case '\n':
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. static inline int iswhite(int a)
  74. {
  75. switch (a) {
  76. case ' ': case '\t':
  77. return 1;
  78. }
  79. return 0;
  80. }
  81. static inline int bitdepth_from_maxval(int maxval)
  82. {
  83. int depth = 0;
  84. while (maxval)
  85. {
  86. maxval >>= 1;
  87. depth++;
  88. }
  89. return depth;
  90. }
  91. static const unsigned char *
  92. pnm_read_signature(fz_context *ctx, const unsigned char *p, const unsigned char *e, char *signature)
  93. {
  94. if (e - p < 2)
  95. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot parse magic number in pnm image");
  96. if (p[0] != 'P' || ((p[1] < '1' || p[1] > '7') && p[1] != 'F' && p[1] != 'f'))
  97. fz_throw(ctx, FZ_ERROR_FORMAT, "expected signature in pnm image");
  98. signature[0] = *p++;
  99. signature[1] = *p++;
  100. return p;
  101. }
  102. static const unsigned char *
  103. pnm_read_until_eol(fz_context *ctx, const unsigned char *p, const unsigned char *e, int acceptCR)
  104. {
  105. if (e - p < 1)
  106. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot parse line in pnm image");
  107. while (p < e && ((acceptCR && *p != '\r' && *p != '\n') || (!acceptCR && *p != '\n')))
  108. p++;
  109. return p;
  110. }
  111. static const unsigned char *
  112. pnm_read_eol(fz_context *ctx, const unsigned char *p, const unsigned char *e, int acceptCR)
  113. {
  114. if (e - p < 1)
  115. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot parse end of line in pnm image");
  116. if ((acceptCR && *p != '\r' && *p != '\n') || (!acceptCR && *p != '\n'))
  117. fz_throw(ctx, FZ_ERROR_FORMAT, "expected end of line in pnm image");
  118. /* CR, CRLF or LF depending on acceptCR. */
  119. if (acceptCR && *p == '\r')
  120. p++;
  121. if (p < e && *p == '\n')
  122. p++;
  123. return p;
  124. }
  125. static const unsigned char *
  126. pnm_read_whites(fz_context *ctx, const unsigned char *p, const unsigned char *e, int required)
  127. {
  128. if (required && e - p < 1)
  129. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot parse whitespaces in pnm image");
  130. if (required && !iswhite(*p))
  131. fz_throw(ctx, FZ_ERROR_FORMAT, "expected whitespaces in pnm image");
  132. while (p < e && iswhite(*p))
  133. p++;
  134. return p;
  135. }
  136. static const unsigned char *
  137. pnm_read_white_or_eol(fz_context *ctx, const unsigned char *p, const unsigned char *e)
  138. {
  139. if (e - p < 1)
  140. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot parse whitespace/eol in pnm image");
  141. if (!iswhiteeol(*p))
  142. fz_throw(ctx, FZ_ERROR_FORMAT, "expected whitespace/eol in pnm image");
  143. return ++p;
  144. }
  145. static const unsigned char *
  146. pnm_read_whites_and_eols(fz_context *ctx, const unsigned char *p, const unsigned char *e, int required)
  147. {
  148. if (required && e - p < 1)
  149. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot parse whitespaces/eols in pnm image");
  150. if (required && !iswhiteeol(*p))
  151. fz_throw(ctx, FZ_ERROR_FORMAT, "expected whitespaces/eols in pnm image");
  152. while (p < e && iswhiteeol(*p))
  153. p++;
  154. return p;
  155. }
  156. static const unsigned char *
  157. pnm_read_comment(fz_context *ctx, const unsigned char *p, const unsigned char *e, int acceptCR)
  158. {
  159. if (e - p < 1)
  160. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot parse line in pnm image");
  161. if (*p != '#')
  162. return p;
  163. return pnm_read_until_eol(ctx, p, e, acceptCR);
  164. }
  165. static const unsigned char *
  166. pnm_read_comments(fz_context *ctx, const unsigned char *p, const unsigned char *e, int acceptCR)
  167. {
  168. if (e - p < 1)
  169. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot parse comment in pnm image");
  170. while (p < e && *p == '#')
  171. {
  172. p = pnm_read_comment(ctx, p, e, acceptCR);
  173. p = pnm_read_eol(ctx, p, e, acceptCR);
  174. }
  175. return p;
  176. }
  177. static const unsigned char *
  178. pnm_read_digit(fz_context *ctx, const unsigned char *p, const unsigned char *e, int *number)
  179. {
  180. if (e - p < 1)
  181. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot parse digit in pnm image");
  182. if (*p < '0' || *p > '1')
  183. fz_throw(ctx, FZ_ERROR_FORMAT, "expected digit in pnm image");
  184. if (number)
  185. *number = *p - '0';
  186. p++;
  187. return p;
  188. }
  189. static const unsigned char *
  190. pnm_read_int(fz_context *ctx, const unsigned char *p, const unsigned char *e, int *number)
  191. {
  192. if (e - p < 1)
  193. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot parse integer in pnm image");
  194. if (*p < '0' || *p > '9')
  195. fz_throw(ctx, FZ_ERROR_FORMAT, "expected integer in pnm image");
  196. while (p < e && *p >= '0' && *p <= '9')
  197. {
  198. if (number)
  199. *number = *number * 10 + *p - '0';
  200. p++;
  201. }
  202. return p;
  203. }
  204. static const unsigned char *
  205. pnm_read_real(fz_context *ctx, const unsigned char *p, const unsigned char *e, float *number)
  206. {
  207. const unsigned char *orig = p;
  208. char *buf, *end;
  209. size_t len;
  210. if (e - p < 1)
  211. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot parse real in pnm image");
  212. if (*p != '+' && *p != '-' && (*p < '0' || *p > '9'))
  213. fz_throw(ctx, FZ_ERROR_FORMAT, "expected numeric field in pnm image");
  214. while (p < e && (*p == '+' || *p == '-' || *p == '.' || (*p >= '0' && *p <= '9')))
  215. p++;
  216. len = p - orig + 1;
  217. end = buf = fz_malloc(ctx, len);
  218. fz_try(ctx)
  219. {
  220. memcpy(buf, orig, len - 1);
  221. buf[len - 1] = '\0';
  222. *number = fz_strtof(buf, &end);
  223. p = orig + (end - buf);
  224. }
  225. fz_always(ctx)
  226. fz_free(ctx, buf);
  227. fz_catch(ctx)
  228. fz_rethrow(ctx);
  229. return p;
  230. }
  231. static const unsigned char *
  232. pnm_read_tupletype(fz_context *ctx, const unsigned char *p, const unsigned char *e, int *tupletype)
  233. {
  234. const struct { int len; char *str; int type; } tupletypes[] =
  235. {
  236. {13, "BLACKANDWHITE", PAM_BW},
  237. {19, "BLACKANDWHITE_ALPHA", PAM_BWA},
  238. {9, "GRAYSCALE", PAM_GRAY},
  239. {15, "GRAYSCALE_ALPHA", PAM_GRAYA},
  240. {3, "RGB", PAM_RGB},
  241. {9, "RGB_ALPHA", PAM_RGBA},
  242. {4, "CMYK", PAM_CMYK},
  243. {10, "CMYK_ALPHA", PAM_CMYKA},
  244. };
  245. const unsigned char *s;
  246. int i, len;
  247. if (e - p < 1)
  248. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot parse tuple type in pnm image");
  249. s = p;
  250. while (p < e && !iswhiteeol(*p))
  251. p++;
  252. len = p - s;
  253. for (i = 0; i < (int)nelem(tupletypes); i++)
  254. if (len == tupletypes[i].len && !strncmp((char *) s, tupletypes[i].str, len))
  255. {
  256. *tupletype = tupletypes[i].type;
  257. return p;
  258. }
  259. fz_throw(ctx, FZ_ERROR_FORMAT, "unknown tuple type in pnm image");
  260. }
  261. static const unsigned char *
  262. pnm_read_token(fz_context *ctx, const unsigned char *p, const unsigned char *e, int *token)
  263. {
  264. const struct { int len; char *str; int type; } tokens[] =
  265. {
  266. {5, "WIDTH", TOKEN_WIDTH},
  267. {6, "HEIGHT", TOKEN_HEIGHT},
  268. {5, "DEPTH", TOKEN_DEPTH},
  269. {6, "MAXVAL", TOKEN_MAXVAL},
  270. {8, "TUPLTYPE", TOKEN_TUPLTYPE},
  271. {6, "ENDHDR", TOKEN_ENDHDR},
  272. };
  273. const unsigned char *s;
  274. int i, len;
  275. if (e - p < 1)
  276. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot parse header token in pnm image");
  277. s = p;
  278. while (p < e && !iswhiteeol(*p))
  279. p++;
  280. len = p - s;
  281. for (i = 0; i < (int)nelem(tokens); i++)
  282. if (len == tokens[i].len && !strncmp((char *) s, tokens[i].str, len))
  283. {
  284. *token = tokens[i].type;
  285. return p;
  286. }
  287. fz_throw(ctx, FZ_ERROR_FORMAT, "unknown header token in pnm image");
  288. }
  289. static int
  290. map_color(fz_context *ctx, int color, int inmax, int outmax)
  291. {
  292. float f = (float) color / inmax;
  293. return f * outmax;
  294. }
  295. static fz_pixmap *
  296. pnm_ascii_read_image(fz_context *ctx, struct info *pnm, const unsigned char *p, const unsigned char *e, int onlymeta, int bitmap, const unsigned char **out)
  297. {
  298. fz_pixmap *img = NULL;
  299. pnm->width = 0;
  300. p = pnm_read_comments(ctx, p, e, 1);
  301. p = pnm_read_int(ctx, p, e, &pnm->width);
  302. p = pnm_read_whites_and_eols(ctx, p, e, 1);
  303. if (bitmap)
  304. {
  305. pnm->height = 0;
  306. p = pnm_read_int(ctx, p, e, &pnm->height);
  307. p = pnm_read_whites_and_eols(ctx, p, e, 1);
  308. pnm->maxval = 1;
  309. }
  310. else
  311. {
  312. pnm->height = 0;
  313. p = pnm_read_comments(ctx, p, e, 1);
  314. p = pnm_read_int(ctx, p, e, &pnm->height);
  315. p = pnm_read_whites_and_eols(ctx, p, e, 1);
  316. pnm->maxval = 0;
  317. p = pnm_read_comments(ctx, p, e, 1);
  318. p = pnm_read_int(ctx, p, e, &pnm->maxval);
  319. p = pnm_read_white_or_eol(ctx, p, e);
  320. }
  321. if (pnm->maxval <= 0 || pnm->maxval >= 65536)
  322. fz_throw(ctx, FZ_ERROR_FORMAT, "maximum sample value of out range in pnm image: %d", pnm->maxval);
  323. pnm->bitdepth = bitdepth_from_maxval(pnm->maxval);
  324. if (pnm->height <= 0)
  325. fz_throw(ctx, FZ_ERROR_FORMAT, "image height must be > 0");
  326. if (pnm->width <= 0)
  327. fz_throw(ctx, FZ_ERROR_FORMAT, "image width must be > 0");
  328. if ((unsigned int)pnm->height > UINT_MAX / pnm->width / fz_colorspace_n(ctx, pnm->cs) / (pnm->bitdepth / 8 + 1))
  329. fz_throw(ctx, FZ_ERROR_LIMIT, "image too large");
  330. if (onlymeta)
  331. {
  332. int x, y, k;
  333. int w, h, n;
  334. w = pnm->width;
  335. h = pnm->height;
  336. n = fz_colorspace_n(ctx, pnm->cs);
  337. if (bitmap)
  338. {
  339. for (y = 0; y < h; y++)
  340. for (x = 0; x < w; x++)
  341. {
  342. p = pnm_read_whites_and_eols(ctx, p, e, 0);
  343. p = pnm_read_digit(ctx, p, e, NULL);
  344. p = pnm_read_whites_and_eols(ctx, p, e, 0);
  345. }
  346. }
  347. else
  348. {
  349. for (y = 0; y < h; y++)
  350. for (x = 0; x < w; x++)
  351. for (k = 0; k < n; k++)
  352. {
  353. p = pnm_read_whites_and_eols(ctx, p, e, 0);
  354. p = pnm_read_int(ctx, p, e, NULL);
  355. p = pnm_read_whites_and_eols(ctx, p, e, 0);
  356. }
  357. }
  358. }
  359. else
  360. {
  361. unsigned char *dp;
  362. int x, y, k;
  363. int w, h, n;
  364. img = fz_new_pixmap(ctx, pnm->cs, pnm->width, pnm->height, NULL, 0);
  365. dp = img->samples;
  366. w = img->w;
  367. h = img->h;
  368. n = img->n;
  369. if (bitmap)
  370. {
  371. for (y = 0; y < h; y++)
  372. {
  373. for (x = 0; x < w; x++)
  374. {
  375. int v = 0;
  376. p = pnm_read_whites_and_eols(ctx, p, e, 0);
  377. p = pnm_read_digit(ctx, p, e, &v);
  378. p = pnm_read_whites_and_eols(ctx, p, e, 0);
  379. *dp++ = v ? 0x00 : 0xff;
  380. }
  381. }
  382. }
  383. else
  384. {
  385. for (y = 0; y < h; y++)
  386. for (x = 0; x < w; x++)
  387. for (k = 0; k < n; k++)
  388. {
  389. int v = 0;
  390. p = pnm_read_whites_and_eols(ctx, p, e, 0);
  391. p = pnm_read_int(ctx, p, e, &v);
  392. p = pnm_read_whites_and_eols(ctx, p, e, 0);
  393. v = fz_clampi(v, 0, pnm->maxval);
  394. *dp++ = map_color(ctx, v, pnm->maxval, 255);
  395. }
  396. }
  397. }
  398. if (out)
  399. *out = p;
  400. return img;
  401. }
  402. static fz_pixmap *
  403. pnm_binary_read_image(fz_context *ctx, struct info *pnm, const unsigned char *p, const unsigned char *e, int onlymeta, int bitmap, const unsigned char **out)
  404. {
  405. fz_pixmap *img = NULL;
  406. size_t span;
  407. int n;
  408. n = fz_colorspace_n(ctx, pnm->cs);
  409. assert(n >= 1 && n <= 3);
  410. pnm->width = 0;
  411. p = pnm_read_comments(ctx, p, e, 1);
  412. p = pnm_read_int(ctx, p, e, &pnm->width);
  413. p = pnm_read_whites_and_eols(ctx, p, e, 1);
  414. if (bitmap)
  415. {
  416. pnm->height = 0;
  417. p = pnm_read_int(ctx, p, e, &pnm->height);
  418. p = pnm_read_whites_and_eols(ctx, p, e, 1);
  419. pnm->maxval = 1;
  420. }
  421. else
  422. {
  423. pnm->height = 0;
  424. p = pnm_read_comments(ctx, p, e, 1);
  425. p = pnm_read_int(ctx, p, e, &pnm->height);
  426. p = pnm_read_whites_and_eols(ctx, p, e, 1);
  427. pnm->maxval = 0;
  428. p = pnm_read_comments(ctx, p, e, 1);
  429. p = pnm_read_int(ctx, p, e, &pnm->maxval);
  430. p = pnm_read_white_or_eol(ctx, p, e);
  431. }
  432. if (pnm->maxval <= 0 || pnm->maxval >= 65536)
  433. fz_throw(ctx, FZ_ERROR_FORMAT, "maximum sample value of out range in pnm image: %d", pnm->maxval);
  434. pnm->bitdepth = bitdepth_from_maxval(pnm->maxval);
  435. if (pnm->height <= 0)
  436. fz_throw(ctx, FZ_ERROR_FORMAT, "image height must be > 0");
  437. if (pnm->width <= 0)
  438. fz_throw(ctx, FZ_ERROR_FORMAT, "image width must be > 0");
  439. if (pnm->bitdepth == 1)
  440. {
  441. /* Overly sensitive test, but we can live with it. */
  442. if ((size_t)pnm->width > SIZE_MAX / (unsigned int)n)
  443. fz_throw(ctx, FZ_ERROR_LIMIT, "image row too large");
  444. span = ((size_t)n * pnm->width + 7)/8;
  445. }
  446. else
  447. {
  448. size_t bytes_per_sample = (pnm->bitdepth-1)/8 + 1;
  449. span = (size_t)n * bytes_per_sample;
  450. if ((size_t)pnm->width > SIZE_MAX / span)
  451. fz_throw(ctx, FZ_ERROR_LIMIT, "image row too large");
  452. span = (size_t)pnm->width * span;
  453. }
  454. if ((size_t)pnm->height > SIZE_MAX / span)
  455. fz_throw(ctx, FZ_ERROR_LIMIT, "image too large");
  456. if (e - p < 0 || ((size_t)(e - p)) < span * (size_t)pnm->height)
  457. fz_throw(ctx, FZ_ERROR_FORMAT, "insufficient data");
  458. if (onlymeta)
  459. {
  460. p += span * (size_t)pnm->height;
  461. }
  462. else
  463. {
  464. unsigned char *dp;
  465. int x, y, k;
  466. int w, h;
  467. img = fz_new_pixmap(ctx, pnm->cs, pnm->width, pnm->height, NULL, 0);
  468. dp = img->samples;
  469. w = img->w;
  470. h = img->h;
  471. n = img->n;
  472. if (pnm->maxval == 255)
  473. {
  474. memcpy(dp, p, (size_t)w * h * n);
  475. p += n * w * h;
  476. }
  477. else if (bitmap)
  478. {
  479. for (y = 0; y < h; y++)
  480. {
  481. for (x = 0; x < w; x++)
  482. {
  483. *dp++ = (*p & (1 << (7 - (x & 0x7)))) ? 0x00 : 0xff;
  484. if ((x & 0x7) == 7)
  485. p++;
  486. }
  487. if (w & 0x7)
  488. p++;
  489. }
  490. }
  491. else if (pnm->maxval < 255)
  492. {
  493. for (y = 0; y < h; y++)
  494. for (x = 0; x < w; x++)
  495. for (k = 0; k < n; k++)
  496. *dp++ = map_color(ctx, *p++, pnm->maxval, 255);
  497. }
  498. else
  499. {
  500. for (y = 0; y < h; y++)
  501. for (x = 0; x < w; x++)
  502. for (k = 0; k < n; k++)
  503. {
  504. *dp++ = map_color(ctx, (p[0] << 8) | p[1], pnm->maxval, 255);
  505. p += 2;
  506. }
  507. }
  508. }
  509. if (out)
  510. *out = p;
  511. return img;
  512. }
  513. static const unsigned char *
  514. pam_binary_read_header(fz_context *ctx, struct info *pnm, const unsigned char *p, const unsigned char *e)
  515. {
  516. int token = TOKEN_UNKNOWN;
  517. const unsigned char *eol;
  518. int seen[TOKEN_ENDHDR] = { 0 };
  519. pnm->width = 0;
  520. pnm->height = 0;
  521. pnm->depth = 0;
  522. pnm->maxval = 0;
  523. pnm->tupletype = 0;
  524. while (p < e && token != TOKEN_ENDHDR)
  525. {
  526. eol = pnm_read_until_eol(ctx, p, e, 0);
  527. p = pnm_read_whites(ctx, p, eol, 0);
  528. if (p < eol && *p != '#')
  529. {
  530. p = pnm_read_token(ctx, p, eol, &token);
  531. if (seen[token - 1])
  532. fz_throw(ctx, FZ_ERROR_FORMAT, "token occurs multiple times in pnm image");
  533. seen[token - 1] = 1;
  534. if (token != TOKEN_ENDHDR)
  535. {
  536. p = pnm_read_whites(ctx, p, eol, 1);
  537. switch (token)
  538. {
  539. case TOKEN_WIDTH: pnm->width = 0; p = pnm_read_int(ctx, p, eol, &pnm->width); break;
  540. case TOKEN_HEIGHT: pnm->height = 0; p = pnm_read_int(ctx, p, eol, &pnm->height); break;
  541. case TOKEN_DEPTH: pnm->depth = 0; p = pnm_read_int(ctx, p, eol, &pnm->depth); break;
  542. case TOKEN_MAXVAL: pnm->maxval = 0; p = pnm_read_int(ctx, p, eol, &pnm->maxval); break;
  543. case TOKEN_TUPLTYPE: pnm->tupletype = 0; p = pnm_read_tupletype(ctx, p, eol, &pnm->tupletype); break;
  544. }
  545. }
  546. p = pnm_read_whites(ctx, p, eol, 0);
  547. }
  548. if (p < eol && *p == '#')
  549. p = pnm_read_comment(ctx, p, eol, 0);
  550. p = pnm_read_eol(ctx, p, e, 0);
  551. }
  552. return p;
  553. }
  554. static fz_pixmap *
  555. pam_binary_read_image(fz_context *ctx, struct info *pnm, const unsigned char *p, const unsigned char *e, int onlymeta, const unsigned char **out)
  556. {
  557. fz_pixmap *img = NULL;
  558. int bitmap = 0;
  559. int minval = 1;
  560. int maxval = 65535;
  561. fz_var(img);
  562. p = pam_binary_read_header(ctx, pnm, p, e);
  563. if (pnm->tupletype == PAM_UNKNOWN)
  564. switch (pnm->depth)
  565. {
  566. case 1: pnm->tupletype = pnm->maxval == 1 ? PAM_BW : PAM_GRAY; break;
  567. case 2: pnm->tupletype = pnm->maxval == 1 ? PAM_BWA : PAM_GRAYA; break;
  568. case 3: pnm->tupletype = PAM_RGB; break;
  569. case 4: pnm->tupletype = PAM_CMYK; break;
  570. case 5: pnm->tupletype = PAM_CMYKA; break;
  571. default:
  572. fz_throw(ctx, FZ_ERROR_FORMAT, "cannot guess tuple type based on depth in pnm image");
  573. }
  574. if (pnm->tupletype == PAM_BW && pnm->maxval > 1)
  575. pnm->tupletype = PAM_GRAY;
  576. else if (pnm->tupletype == PAM_GRAY && pnm->maxval == 1)
  577. pnm->tupletype = PAM_BW;
  578. else if (pnm->tupletype == PAM_BWA && pnm->maxval > 1)
  579. pnm->tupletype = PAM_GRAYA;
  580. else if (pnm->tupletype == PAM_GRAYA && pnm->maxval == 1)
  581. pnm->tupletype = PAM_BWA;
  582. switch (pnm->tupletype)
  583. {
  584. case PAM_BWA:
  585. pnm->alpha = 1;
  586. /* fallthrough */
  587. case PAM_BW:
  588. pnm->cs = fz_device_gray(ctx);
  589. maxval = 1;
  590. bitmap = 1;
  591. break;
  592. case PAM_GRAYA:
  593. pnm->alpha = 1;
  594. /* fallthrough */
  595. case PAM_GRAY:
  596. pnm->cs = fz_device_gray(ctx);
  597. minval = 2;
  598. break;
  599. case PAM_RGBA:
  600. pnm->alpha = 1;
  601. /* fallthrough */
  602. case PAM_RGB:
  603. pnm->cs = fz_device_rgb(ctx);
  604. break;
  605. case PAM_CMYKA:
  606. pnm->alpha = 1;
  607. /* fallthrough */
  608. case PAM_CMYK:
  609. pnm->cs = fz_device_cmyk(ctx);
  610. break;
  611. default:
  612. fz_throw(ctx, FZ_ERROR_FORMAT, "unsupported tuple type");
  613. }
  614. if (pnm->depth != fz_colorspace_n(ctx, pnm->cs) + pnm->alpha)
  615. fz_throw(ctx, FZ_ERROR_FORMAT, "depth out of tuple type range");
  616. if (pnm->maxval < minval || pnm->maxval > maxval)
  617. fz_throw(ctx, FZ_ERROR_FORMAT, "maxval out of range");
  618. pnm->bitdepth = bitdepth_from_maxval(pnm->maxval);
  619. if (pnm->height <= 0)
  620. fz_throw(ctx, FZ_ERROR_FORMAT, "image height must be > 0");
  621. if (pnm->width <= 0)
  622. fz_throw(ctx, FZ_ERROR_FORMAT, "image width must be > 0");
  623. if ((unsigned int)pnm->height > UINT_MAX / pnm->width / fz_colorspace_n(ctx, pnm->cs) / (pnm->bitdepth / 8 + 1))
  624. fz_throw(ctx, FZ_ERROR_LIMIT, "image too large");
  625. if (onlymeta)
  626. {
  627. int packed;
  628. int w, h, n;
  629. size_t size;
  630. w = pnm->width;
  631. h = pnm->height;
  632. n = fz_colorspace_n(ctx, pnm->cs) + pnm->alpha;
  633. /* some encoders incorrectly pack bits into bytes and invert the image */
  634. packed = 0;
  635. size = (size_t)w * h * n;
  636. if (pnm->maxval == 1)
  637. {
  638. const unsigned char *e_packed = p + size / 8;
  639. if (e_packed < e - 1 && e_packed[0] == 'P' && e_packed[1] >= '0' && e_packed[1] <= '7')
  640. e = e_packed;
  641. if (e < p || (size_t)(e - p) < size)
  642. packed = 1;
  643. }
  644. if (packed && (e < p || (size_t)(e - p) < size / 8))
  645. fz_throw(ctx, FZ_ERROR_FORMAT, "truncated packed image");
  646. if (!packed && (e < p || (size_t)(e - p) < size * (pnm->maxval < 256 ? 1 : 2)))
  647. fz_throw(ctx, FZ_ERROR_FORMAT, "truncated image");
  648. if (pnm->maxval == 255)
  649. p += size;
  650. else if (bitmap && packed)
  651. p += ((w + 7) / 8) * h;
  652. else if (bitmap)
  653. p += size;
  654. else if (pnm->maxval < 255)
  655. p += size;
  656. else
  657. p += 2 * size;
  658. }
  659. else
  660. {
  661. unsigned char *dp;
  662. int x, y, k, packed;
  663. int w, h, n;
  664. size_t size;
  665. img = fz_new_pixmap(ctx, pnm->cs, pnm->width, pnm->height, NULL, pnm->alpha);
  666. fz_try(ctx)
  667. {
  668. dp = img->samples;
  669. w = img->w;
  670. h = img->h;
  671. n = img->n;
  672. /* some encoders incorrectly pack bits into bytes and invert the image */
  673. size = (size_t)w * h * n;
  674. packed = 0;
  675. if (pnm->maxval == 1)
  676. {
  677. const unsigned char *e_packed = p + size / 8;
  678. if (e_packed < e - 1 && e_packed[0] == 'P' && e_packed[1] >= '0' && e_packed[1] <= '7')
  679. e = e_packed;
  680. if (e < p || (size_t)(e - p) < size)
  681. packed = 1;
  682. }
  683. if (packed && (e < p || (size_t)(e - p) < size / 8))
  684. fz_throw(ctx, FZ_ERROR_FORMAT, "truncated packed image");
  685. if (!packed && (e < p || (size_t)(e - p) < size * (pnm->maxval < 256 ? 1 : 2)))
  686. fz_throw(ctx, FZ_ERROR_FORMAT, "truncated image");
  687. if (pnm->maxval == 255)
  688. memcpy(dp, p, size);
  689. else if (bitmap && packed)
  690. {
  691. for (y = 0; y < h; y++)
  692. for (x = 0; x < w; x++)
  693. {
  694. for (k = 0; k < n; k++)
  695. {
  696. *dp++ = (*p & (1 << (7 - (x & 0x7)))) ? 0x00 : 0xff;
  697. if ((x & 0x7) == 7)
  698. p++;
  699. }
  700. if (w & 0x7)
  701. p++;
  702. }
  703. }
  704. else if (bitmap)
  705. {
  706. for (y = 0; y < h; y++)
  707. for (x = 0; x < w; x++)
  708. for (k = 0; k < n; k++)
  709. *dp++ = *p++ ? 0xff : 0x00;
  710. }
  711. else if (pnm->maxval < 255)
  712. {
  713. for (y = 0; y < h; y++)
  714. for (x = 0; x < w; x++)
  715. for (k = 0; k < n; k++)
  716. *dp++ = map_color(ctx, *p++, pnm->maxval, 255);
  717. }
  718. else
  719. {
  720. for (y = 0; y < h; y++)
  721. for (x = 0; x < w; x++)
  722. for (k = 0; k < n; k++)
  723. {
  724. *dp++ = map_color(ctx, (p[0] << 8) | p[1], pnm->maxval, 255);
  725. p += 2;
  726. }
  727. }
  728. if (pnm->alpha)
  729. fz_premultiply_pixmap(ctx, img);
  730. }
  731. fz_catch(ctx)
  732. {
  733. fz_drop_pixmap(ctx, img);
  734. fz_rethrow(ctx);
  735. }
  736. }
  737. if (out)
  738. *out = p;
  739. return img;
  740. }
  741. static const unsigned char *
  742. pfm_binary_read_header(fz_context *ctx, struct info *pnm, const unsigned char *p, const unsigned char *e)
  743. {
  744. pnm->width = 0;
  745. p = pnm_read_int(ctx, p, e, &pnm->width);
  746. p = pnm_read_whites_and_eols(ctx, p, e,1);
  747. pnm->height = 0;
  748. p = pnm_read_int(ctx, p, e, &pnm->height);
  749. p = pnm_read_whites_and_eols(ctx, p, e,1);
  750. p = pnm_read_real(ctx, p, e, &pnm->scale);
  751. p = pnm_read_white_or_eol(ctx, p, e);
  752. if (pnm->scale >= 0)
  753. pnm->endian = ENDIAN_BIG;
  754. else
  755. {
  756. pnm->endian = ENDIAN_LITTLE;
  757. pnm->scale = -pnm->scale;
  758. }
  759. return p;
  760. }
  761. static fz_pixmap *
  762. pfm_binary_read_image(fz_context *ctx, struct info *pnm, const unsigned char *p, const unsigned char *e, int onlymeta, int rgb, const unsigned char **out)
  763. {
  764. fz_pixmap *pix = NULL;
  765. fz_var(pix);
  766. p = pfm_binary_read_header(ctx, pnm, p, e);
  767. pnm->cs = rgb ? fz_device_rgb(ctx) : fz_device_gray(ctx);
  768. if (pnm->height <= 0)
  769. fz_throw(ctx, FZ_ERROR_FORMAT, "image height must be > 0");
  770. if (pnm->width <= 0)
  771. fz_throw(ctx, FZ_ERROR_FORMAT, "image width must be > 0");
  772. if ((unsigned int)pnm->height > UINT_MAX / pnm->width / fz_colorspace_n(ctx, pnm->cs) / (pnm->bitdepth / 8 + 1))
  773. fz_throw(ctx, FZ_ERROR_LIMIT, "image too large");
  774. if (onlymeta)
  775. {
  776. size_t w = pnm->width;
  777. size_t h = pnm->height;
  778. int n = fz_colorspace_n(ctx, pnm->cs);
  779. size_t size = w * h * n * sizeof(float);
  780. if (e < p || (size_t)(e - p) < size)
  781. fz_throw(ctx, FZ_ERROR_FORMAT, "truncated image");
  782. p += size;
  783. }
  784. else
  785. {
  786. float *samples = NULL;
  787. float *sample;
  788. int w = pnm->width;
  789. int h = pnm->height;
  790. int n = fz_colorspace_n(ctx, pnm->cs);
  791. size_t size = (size_t) w * h * n * sizeof(float);
  792. int x, y, k;
  793. if (e < p || (size_t)(e - p) < size)
  794. fz_throw(ctx, FZ_ERROR_FORMAT, "truncated image");
  795. sample = samples = fz_malloc(ctx, size);
  796. fz_try(ctx)
  797. {
  798. for (y = 0; y < h; y++)
  799. for (x = 0; x < w; x++)
  800. for (k = 0; k < n; k++)
  801. {
  802. uint32_t u;
  803. float f;
  804. if (pnm->endian == ENDIAN_LITTLE)
  805. u = p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
  806. else
  807. u = p[3] | (p[2] << 8) | (p[1] << 16) | (p[0] << 24);
  808. memcpy(&f, &u, sizeof(float));
  809. *sample++ = f / pnm->scale;
  810. p += sizeof(float);
  811. }
  812. pix = fz_new_pixmap_from_float_data(ctx, pnm->cs, w, h, samples);
  813. }
  814. fz_always(ctx)
  815. fz_free(ctx, samples);
  816. fz_catch(ctx)
  817. fz_rethrow(ctx);
  818. }
  819. if (out)
  820. *out = p;
  821. return pix;
  822. }
  823. static fz_pixmap *
  824. pnm_read_image(fz_context *ctx, struct info *pnm, const unsigned char *p, size_t total, int onlymeta, int subimage)
  825. {
  826. const unsigned char *e = p + total;
  827. char signature[3] = { 0 };
  828. fz_pixmap *pix = NULL;
  829. while (p < e && ((!onlymeta && subimage >= 0) || onlymeta))
  830. {
  831. int subonlymeta = onlymeta || (subimage > 0);
  832. p = pnm_read_whites_and_eols(ctx, p, e, 0);
  833. p = pnm_read_signature(ctx, p, e, signature);
  834. p = pnm_read_whites_and_eols(ctx, p, e, 1);
  835. if (!strcmp(signature, "P1"))
  836. {
  837. pnm->cs = fz_device_gray(ctx);
  838. pix = pnm_ascii_read_image(ctx, pnm, p, e, subonlymeta, 1, &p);
  839. }
  840. else if (!strcmp(signature, "P2"))
  841. {
  842. pnm->cs = fz_device_gray(ctx);
  843. pix = pnm_ascii_read_image(ctx, pnm, p, e, subonlymeta, 0, &p);
  844. }
  845. else if (!strcmp(signature, "P3"))
  846. {
  847. pnm->cs = fz_device_rgb(ctx);
  848. pix = pnm_ascii_read_image(ctx, pnm, p, e, subonlymeta, 0, &p);
  849. }
  850. else if (!strcmp(signature, "P4"))
  851. {
  852. pnm->cs = fz_device_gray(ctx);
  853. pix = pnm_binary_read_image(ctx, pnm, p, e, subonlymeta, 1, &p);
  854. }
  855. else if (!strcmp(signature, "P5"))
  856. {
  857. pnm->cs = fz_device_gray(ctx);
  858. pix = pnm_binary_read_image(ctx, pnm, p, e, subonlymeta, 0, &p);
  859. }
  860. else if (!strcmp(signature, "P6"))
  861. {
  862. pnm->cs = fz_device_rgb(ctx);
  863. pix = pnm_binary_read_image(ctx, pnm, p, e, subonlymeta, 0, &p);
  864. }
  865. else if (!strcmp(signature, "P7"))
  866. pix = pam_binary_read_image(ctx, pnm, p, e, subonlymeta, &p);
  867. else if (!strcmp(signature, "Pf"))
  868. pix = pfm_binary_read_image(ctx, pnm, p, e, subonlymeta, 0, &p);
  869. else if (!strcmp(signature, "PF"))
  870. pix = pfm_binary_read_image(ctx, pnm, p, e, subonlymeta, 1, &p);
  871. else
  872. fz_throw(ctx, FZ_ERROR_FORMAT, "unsupported portable anymap signature (0x%02x, 0x%02x)", signature[0], signature[1]);
  873. p = pnm_read_whites_and_eols(ctx, p, e, 0);
  874. if (onlymeta)
  875. pnm->subimages++;
  876. if (subimage >= 0)
  877. subimage--;
  878. }
  879. if (p >= e && subimage >= 0)
  880. fz_throw(ctx, FZ_ERROR_ARGUMENT, "subimage count out of range");
  881. return pix;
  882. }
  883. fz_pixmap *
  884. fz_load_pnm(fz_context *ctx, const unsigned char *p, size_t total)
  885. {
  886. struct info pnm = { 0 };
  887. return pnm_read_image(ctx, &pnm, p, total, 0, 0);
  888. }
  889. void
  890. fz_load_pnm_info(fz_context *ctx, const unsigned char *p, size_t total, int *wp, int *hp, int *xresp, int *yresp, fz_colorspace **cspacep)
  891. {
  892. struct info pnm = { 0 };
  893. (void) pnm_read_image(ctx, &pnm, p, total, 1, 0);
  894. *cspacep = fz_keep_colorspace(ctx, pnm.cs); /* pnm.cs is a borrowed device colorspace */
  895. *wp = pnm.width;
  896. *hp = pnm.height;
  897. *xresp = 72;
  898. *yresp = 72;
  899. }
  900. fz_pixmap *
  901. fz_load_pnm_subimage(fz_context *ctx, const unsigned char *p, size_t total, int subimage)
  902. {
  903. struct info pnm = { 0 };
  904. return pnm_read_image(ctx, &pnm, p, total, 0, subimage);
  905. }
  906. int
  907. fz_load_pnm_subimage_count(fz_context *ctx, const unsigned char *p, size_t total)
  908. {
  909. struct info pnm = { 0 };
  910. (void) pnm_read_image(ctx, &pnm, p, total, 1, -1);
  911. return pnm.subimages;
  912. }