util.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. // Copyright (C) 2004-2022 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 <float.h>
  24. fz_display_list *
  25. fz_new_display_list_from_page(fz_context *ctx, fz_page *page)
  26. {
  27. fz_display_list *list;
  28. fz_device *dev = NULL;
  29. fz_var(dev);
  30. list = fz_new_display_list(ctx, fz_bound_page(ctx, page));
  31. fz_try(ctx)
  32. {
  33. dev = fz_new_list_device(ctx, list);
  34. fz_run_page(ctx, page, dev, fz_identity, NULL);
  35. fz_close_device(ctx, dev);
  36. }
  37. fz_always(ctx)
  38. {
  39. fz_drop_device(ctx, dev);
  40. }
  41. fz_catch(ctx)
  42. {
  43. fz_drop_display_list(ctx, list);
  44. fz_rethrow(ctx);
  45. }
  46. return list;
  47. }
  48. fz_display_list *
  49. fz_new_display_list_from_page_number(fz_context *ctx, fz_document *doc, int number)
  50. {
  51. fz_page *page;
  52. fz_display_list *list = NULL;
  53. page = fz_load_page(ctx, doc, number);
  54. fz_try(ctx)
  55. list = fz_new_display_list_from_page(ctx, page);
  56. fz_always(ctx)
  57. fz_drop_page(ctx, page);
  58. fz_catch(ctx)
  59. fz_rethrow(ctx);
  60. return list;
  61. }
  62. fz_display_list *
  63. fz_new_display_list_from_page_contents(fz_context *ctx, fz_page *page)
  64. {
  65. fz_display_list *list;
  66. fz_device *dev = NULL;
  67. fz_var(dev);
  68. list = fz_new_display_list(ctx, fz_bound_page(ctx, page));
  69. fz_try(ctx)
  70. {
  71. dev = fz_new_list_device(ctx, list);
  72. fz_run_page_contents(ctx, page, dev, fz_identity, NULL);
  73. fz_close_device(ctx, dev);
  74. }
  75. fz_always(ctx)
  76. {
  77. fz_drop_device(ctx, dev);
  78. }
  79. fz_catch(ctx)
  80. {
  81. fz_drop_display_list(ctx, list);
  82. fz_rethrow(ctx);
  83. }
  84. return list;
  85. }
  86. fz_pixmap *
  87. fz_new_pixmap_from_display_list(fz_context *ctx, fz_display_list *list, fz_matrix ctm, fz_colorspace *cs, int alpha)
  88. {
  89. return fz_new_pixmap_from_display_list_with_separations(ctx, list, ctm, cs, NULL, alpha);
  90. }
  91. fz_pixmap *
  92. fz_new_pixmap_from_display_list_with_separations(fz_context *ctx, fz_display_list *list, fz_matrix ctm, fz_colorspace *cs, fz_separations *seps, int alpha)
  93. {
  94. fz_rect rect;
  95. fz_irect bbox;
  96. fz_pixmap *pix;
  97. rect = fz_bound_display_list(ctx, list);
  98. rect = fz_transform_rect(rect, ctm);
  99. bbox = fz_round_rect(rect);
  100. pix = fz_new_pixmap_with_bbox(ctx, cs, bbox, seps, alpha);
  101. if (alpha)
  102. fz_clear_pixmap(ctx, pix);
  103. else
  104. fz_clear_pixmap_with_value(ctx, pix, 0xFF);
  105. return fz_fill_pixmap_from_display_list(ctx, list, ctm, pix);
  106. }
  107. fz_pixmap *
  108. fz_fill_pixmap_from_display_list(fz_context *ctx, fz_display_list *list, fz_matrix ctm, fz_pixmap *pix)
  109. {
  110. fz_device *dev = NULL;
  111. fz_var(dev);
  112. fz_try(ctx)
  113. {
  114. dev = fz_new_draw_device(ctx, ctm, pix);
  115. fz_run_display_list(ctx, list, dev, fz_identity, fz_infinite_rect, NULL);
  116. fz_close_device(ctx, dev);
  117. }
  118. fz_always(ctx)
  119. {
  120. fz_drop_device(ctx, dev);
  121. }
  122. fz_catch(ctx)
  123. {
  124. fz_drop_pixmap(ctx, pix);
  125. fz_rethrow(ctx);
  126. }
  127. return pix;
  128. }
  129. fz_pixmap *
  130. fz_new_pixmap_from_page_contents(fz_context *ctx, fz_page *page, fz_matrix ctm, fz_colorspace *cs, int alpha)
  131. {
  132. return fz_new_pixmap_from_page_contents_with_separations(ctx, page, ctm, cs, NULL, alpha);
  133. }
  134. fz_pixmap *
  135. fz_new_pixmap_from_page_contents_with_separations(fz_context *ctx, fz_page *page, fz_matrix ctm, fz_colorspace *cs, fz_separations *seps, int alpha)
  136. {
  137. fz_rect rect;
  138. fz_irect bbox;
  139. fz_pixmap *pix;
  140. fz_device *dev = NULL;
  141. fz_var(dev);
  142. rect = fz_bound_page(ctx, page);
  143. rect = fz_transform_rect(rect, ctm);
  144. bbox = fz_round_rect(rect);
  145. pix = fz_new_pixmap_with_bbox(ctx, cs, bbox, seps, alpha);
  146. if (alpha)
  147. fz_clear_pixmap(ctx, pix);
  148. else
  149. fz_clear_pixmap_with_value(ctx, pix, 0xFF);
  150. fz_try(ctx)
  151. {
  152. dev = fz_new_draw_device(ctx, ctm, pix);
  153. fz_run_page_contents(ctx, page, dev, fz_identity, NULL);
  154. fz_close_device(ctx, dev);
  155. }
  156. fz_always(ctx)
  157. {
  158. fz_drop_device(ctx, dev);
  159. }
  160. fz_catch(ctx)
  161. {
  162. fz_drop_pixmap(ctx, pix);
  163. fz_rethrow(ctx);
  164. }
  165. return pix;
  166. }
  167. fz_pixmap *
  168. fz_new_pixmap_from_page(fz_context *ctx, fz_page *page, fz_matrix ctm, fz_colorspace *cs, int alpha)
  169. {
  170. return fz_new_pixmap_from_page_with_separations(ctx, page, ctm, cs, NULL, alpha);
  171. }
  172. fz_pixmap *
  173. fz_new_pixmap_from_page_with_separations(fz_context *ctx, fz_page *page, fz_matrix ctm, fz_colorspace *cs, fz_separations *seps, int alpha)
  174. {
  175. fz_rect rect;
  176. fz_irect bbox;
  177. fz_pixmap *pix;
  178. fz_device *dev = NULL;
  179. fz_var(dev);
  180. rect = fz_bound_page(ctx, page);
  181. rect = fz_transform_rect(rect, ctm);
  182. bbox = fz_round_rect(rect);
  183. pix = fz_new_pixmap_with_bbox(ctx, cs, bbox, seps, alpha);
  184. fz_try(ctx)
  185. {
  186. if (alpha)
  187. fz_clear_pixmap(ctx, pix);
  188. else
  189. fz_clear_pixmap_with_value(ctx, pix, 0xFF);
  190. dev = fz_new_draw_device(ctx, ctm, pix);
  191. fz_run_page(ctx, page, dev, fz_identity, NULL);
  192. fz_close_device(ctx, dev);
  193. }
  194. fz_always(ctx)
  195. {
  196. fz_drop_device(ctx, dev);
  197. }
  198. fz_catch(ctx)
  199. {
  200. fz_drop_pixmap(ctx, pix);
  201. fz_rethrow(ctx);
  202. }
  203. return pix;
  204. }
  205. fz_pixmap *
  206. fz_new_pixmap_from_page_number(fz_context *ctx, fz_document *doc, int number, fz_matrix ctm, fz_colorspace *cs, int alpha)
  207. {
  208. return fz_new_pixmap_from_page_number_with_separations(ctx, doc, number, ctm, cs, NULL, alpha);
  209. }
  210. fz_pixmap *
  211. fz_new_pixmap_from_page_number_with_separations(fz_context *ctx, fz_document *doc, int number, fz_matrix ctm, fz_colorspace *cs, fz_separations *seps, int alpha)
  212. {
  213. fz_page *page;
  214. fz_pixmap *pix = NULL;
  215. page = fz_load_page(ctx, doc, number);
  216. fz_try(ctx)
  217. pix = fz_new_pixmap_from_page_with_separations(ctx, page, ctm, cs, seps, alpha);
  218. fz_always(ctx)
  219. fz_drop_page(ctx, page);
  220. fz_catch(ctx)
  221. fz_rethrow(ctx);
  222. return pix;
  223. }
  224. fz_stext_page *
  225. fz_new_stext_page_from_display_list(fz_context *ctx, fz_display_list *list, const fz_stext_options *options)
  226. {
  227. fz_stext_page *text;
  228. fz_device *dev = NULL;
  229. fz_var(dev);
  230. if (list == NULL)
  231. return NULL;
  232. text = fz_new_stext_page(ctx, fz_bound_display_list(ctx, list));
  233. fz_try(ctx)
  234. {
  235. dev = fz_new_stext_device(ctx, text, options);
  236. fz_run_display_list(ctx, list, dev, fz_identity, fz_infinite_rect, NULL);
  237. fz_close_device(ctx, dev);
  238. }
  239. fz_always(ctx)
  240. {
  241. fz_drop_device(ctx, dev);
  242. }
  243. fz_catch(ctx)
  244. {
  245. fz_drop_stext_page(ctx, text);
  246. fz_rethrow(ctx);
  247. }
  248. return text;
  249. }
  250. fz_stext_page *
  251. fz_new_stext_page_from_page(fz_context *ctx, fz_page *page, const fz_stext_options *options)
  252. {
  253. fz_stext_page *text;
  254. fz_device *dev = NULL;
  255. fz_var(dev);
  256. if (page == NULL)
  257. return NULL;
  258. text = fz_new_stext_page(ctx, fz_bound_page(ctx, page));
  259. fz_try(ctx)
  260. {
  261. dev = fz_new_stext_device(ctx, text, options);
  262. fz_run_page_contents(ctx, page, dev, fz_identity, NULL);
  263. fz_close_device(ctx, dev);
  264. }
  265. fz_always(ctx)
  266. {
  267. fz_drop_device(ctx, dev);
  268. }
  269. fz_catch(ctx)
  270. {
  271. fz_drop_stext_page(ctx, text);
  272. fz_rethrow(ctx);
  273. }
  274. return text;
  275. }
  276. fz_stext_page *
  277. fz_new_stext_page_from_page_number(fz_context *ctx, fz_document *doc, int number, const fz_stext_options *options)
  278. {
  279. fz_page *page;
  280. fz_stext_page *text = NULL;
  281. page = fz_load_page(ctx, doc, number);
  282. fz_try(ctx)
  283. text = fz_new_stext_page_from_page(ctx, page, options);
  284. fz_always(ctx)
  285. fz_drop_page(ctx, page);
  286. fz_catch(ctx)
  287. fz_rethrow(ctx);
  288. return text;
  289. }
  290. fz_stext_page *
  291. fz_new_stext_page_from_chapter_page_number(fz_context *ctx, fz_document *doc, int chapter, int number, const fz_stext_options *options)
  292. {
  293. fz_page *page;
  294. fz_stext_page *text = NULL;
  295. page = fz_load_chapter_page(ctx, doc, chapter, number);
  296. fz_try(ctx)
  297. text = fz_new_stext_page_from_page(ctx, page, options);
  298. fz_always(ctx)
  299. fz_drop_page(ctx, page);
  300. fz_catch(ctx)
  301. fz_rethrow(ctx);
  302. return text;
  303. }
  304. int
  305. fz_search_display_list(fz_context *ctx, fz_display_list *list, const char *needle, int *hit_mark, fz_quad *hit_bbox, int hit_max)
  306. {
  307. fz_stext_page *text;
  308. int count = 0;
  309. text = fz_new_stext_page_from_display_list(ctx, list, NULL);
  310. fz_try(ctx)
  311. count = fz_search_stext_page(ctx, text, needle, hit_mark, hit_bbox, hit_max);
  312. fz_always(ctx)
  313. fz_drop_stext_page(ctx, text);
  314. fz_catch(ctx)
  315. fz_rethrow(ctx);
  316. return count;
  317. }
  318. int
  319. fz_search_display_list_cb(fz_context *ctx, fz_display_list *list, const char *needle, fz_search_callback_fn *cb, void *opaque)
  320. {
  321. fz_stext_page *text;
  322. int count = 0;
  323. text = fz_new_stext_page_from_display_list(ctx, list, NULL);
  324. fz_try(ctx)
  325. count = fz_search_stext_page_cb(ctx, text, needle, cb, opaque);
  326. fz_always(ctx)
  327. fz_drop_stext_page(ctx, text);
  328. fz_catch(ctx)
  329. fz_rethrow(ctx);
  330. return count;
  331. }
  332. int
  333. fz_search_page(fz_context *ctx, fz_page *page, const char *needle, int *hit_mark, fz_quad *hit_bbox, int hit_max)
  334. {
  335. fz_stext_options opts = { FZ_STEXT_DEHYPHENATE };
  336. fz_stext_page *text;
  337. int count = 0;
  338. text = fz_new_stext_page_from_page(ctx, page, &opts);
  339. fz_try(ctx)
  340. count = fz_search_stext_page(ctx, text, needle, hit_mark, hit_bbox, hit_max);
  341. fz_always(ctx)
  342. fz_drop_stext_page(ctx, text);
  343. fz_catch(ctx)
  344. fz_rethrow(ctx);
  345. return count;
  346. }
  347. int
  348. fz_search_page_cb(fz_context *ctx, fz_page *page, const char *needle, fz_search_callback_fn *cb, void *opaque)
  349. {
  350. fz_stext_options opts = { FZ_STEXT_DEHYPHENATE };
  351. fz_stext_page *text;
  352. int count = 0;
  353. text = fz_new_stext_page_from_page(ctx, page, &opts);
  354. fz_try(ctx)
  355. count = fz_search_stext_page_cb(ctx, text, needle, cb, opaque);
  356. fz_always(ctx)
  357. fz_drop_stext_page(ctx, text);
  358. fz_catch(ctx)
  359. fz_rethrow(ctx);
  360. return count;
  361. }
  362. int
  363. fz_search_page_number(fz_context *ctx, fz_document *doc, int number, const char *needle, int *hit_mark, fz_quad *hit_bbox, int hit_max)
  364. {
  365. fz_page *page;
  366. int count = 0;
  367. page = fz_load_page(ctx, doc, number);
  368. fz_try(ctx)
  369. count = fz_search_page(ctx, page, needle, hit_mark, hit_bbox, hit_max);
  370. fz_always(ctx)
  371. fz_drop_page(ctx, page);
  372. fz_catch(ctx)
  373. fz_rethrow(ctx);
  374. return count;
  375. }
  376. int
  377. fz_search_page_number_cb(fz_context *ctx, fz_document *doc, int number, const char *needle, fz_search_callback_fn *cb, void *opaque)
  378. {
  379. fz_page *page;
  380. int count = 0;
  381. page = fz_load_page(ctx, doc, number);
  382. fz_try(ctx)
  383. count = fz_search_page_cb(ctx, page, needle, cb, opaque);
  384. fz_always(ctx)
  385. fz_drop_page(ctx, page);
  386. fz_catch(ctx)
  387. fz_rethrow(ctx);
  388. return count;
  389. }
  390. int
  391. fz_search_chapter_page_number(fz_context *ctx, fz_document *doc, int chapter, int number, const char *needle, int *hit_mark, fz_quad *hit_bbox, int hit_max)
  392. {
  393. fz_page *page;
  394. int count = 0;
  395. page = fz_load_chapter_page(ctx, doc, chapter, number);
  396. fz_try(ctx)
  397. count = fz_search_page(ctx, page, needle, hit_mark, hit_bbox, hit_max);
  398. fz_always(ctx)
  399. fz_drop_page(ctx, page);
  400. fz_catch(ctx)
  401. fz_rethrow(ctx);
  402. return count;
  403. }
  404. int
  405. fz_search_chapter_page_number_cb(fz_context *ctx, fz_document *doc, int chapter, int number, const char *needle, fz_search_callback_fn *cb, void *opaque)
  406. {
  407. fz_page *page;
  408. int count = 0;
  409. page = fz_load_chapter_page(ctx, doc, chapter, number);
  410. fz_try(ctx)
  411. count = fz_search_page_cb(ctx, page, needle, cb, opaque);
  412. fz_always(ctx)
  413. fz_drop_page(ctx, page);
  414. fz_catch(ctx)
  415. fz_rethrow(ctx);
  416. return count;
  417. }
  418. fz_buffer *
  419. fz_new_buffer_from_stext_page(fz_context *ctx, fz_stext_page *page)
  420. {
  421. fz_stext_block *block;
  422. fz_stext_line *line;
  423. fz_stext_char *ch;
  424. fz_buffer *buf;
  425. buf = fz_new_buffer(ctx, 256);
  426. fz_try(ctx)
  427. {
  428. for (block = page->first_block; block; block = block->next)
  429. {
  430. if (block->type == FZ_STEXT_BLOCK_TEXT)
  431. {
  432. for (line = block->u.t.first_line; line; line = line->next)
  433. {
  434. for (ch = line->first_char; ch; ch = ch->next)
  435. fz_append_rune(ctx, buf, ch->c);
  436. fz_append_byte(ctx, buf, '\n');
  437. }
  438. fz_append_byte(ctx, buf, '\n');
  439. }
  440. }
  441. }
  442. fz_catch(ctx)
  443. {
  444. fz_drop_buffer(ctx, buf);
  445. fz_rethrow(ctx);
  446. }
  447. return buf;
  448. }
  449. fz_buffer *
  450. fz_new_buffer_from_display_list(fz_context *ctx, fz_display_list *list, const fz_stext_options *options)
  451. {
  452. fz_stext_page *text;
  453. fz_buffer *buf = NULL;
  454. text = fz_new_stext_page_from_display_list(ctx, list, options);
  455. fz_try(ctx)
  456. buf = fz_new_buffer_from_stext_page(ctx, text);
  457. fz_always(ctx)
  458. fz_drop_stext_page(ctx, text);
  459. fz_catch(ctx)
  460. fz_rethrow(ctx);
  461. return buf;
  462. }
  463. fz_buffer *
  464. fz_new_buffer_from_page(fz_context *ctx, fz_page *page, const fz_stext_options *options)
  465. {
  466. fz_stext_page *text;
  467. fz_buffer *buf = NULL;
  468. text = fz_new_stext_page_from_page(ctx, page, options);
  469. fz_try(ctx)
  470. buf = fz_new_buffer_from_stext_page(ctx, text);
  471. fz_always(ctx)
  472. fz_drop_stext_page(ctx, text);
  473. fz_catch(ctx)
  474. fz_rethrow(ctx);
  475. return buf;
  476. }
  477. fz_buffer *
  478. fz_new_buffer_from_page_number(fz_context *ctx, fz_document *doc, int number, const fz_stext_options *options)
  479. {
  480. fz_page *page;
  481. fz_buffer *buf = NULL;
  482. page = fz_load_page(ctx, doc, number);
  483. fz_try(ctx)
  484. buf = fz_new_buffer_from_page(ctx, page, options);
  485. fz_always(ctx)
  486. fz_drop_page(ctx, page);
  487. fz_catch(ctx)
  488. fz_rethrow(ctx);
  489. return buf;
  490. }
  491. void
  492. fz_write_image_as_data_uri(fz_context *ctx, fz_output *out, fz_image *image)
  493. {
  494. fz_compressed_buffer *cbuf;
  495. fz_buffer *buf;
  496. cbuf = fz_compressed_image_buffer(ctx, image);
  497. if (cbuf && cbuf->params.type == FZ_IMAGE_JPEG)
  498. {
  499. int type = fz_colorspace_type(ctx, image->colorspace);
  500. if (type == FZ_COLORSPACE_GRAY || type == FZ_COLORSPACE_RGB)
  501. {
  502. fz_write_string(ctx, out, "data:image/jpeg;base64,");
  503. fz_write_base64_buffer(ctx, out, cbuf->buffer, 1);
  504. return;
  505. }
  506. }
  507. if (cbuf && cbuf->params.type == FZ_IMAGE_PNG)
  508. {
  509. fz_write_string(ctx, out, "data:image/png;base64,");
  510. fz_write_base64_buffer(ctx, out, cbuf->buffer, 1);
  511. return;
  512. }
  513. buf = fz_new_buffer_from_image_as_png(ctx, image, fz_default_color_params);
  514. fz_try(ctx)
  515. {
  516. fz_write_string(ctx, out, "data:image/png;base64,");
  517. fz_write_base64_buffer(ctx, out, buf, 1);
  518. }
  519. fz_always(ctx)
  520. fz_drop_buffer(ctx, buf);
  521. fz_catch(ctx)
  522. fz_rethrow(ctx);
  523. }
  524. static uint32_t read16(const uint8_t *d, size_t *pos, size_t len, int order)
  525. {
  526. size_t p = *pos;
  527. uint32_t v;
  528. if (p+1 >= len)
  529. {
  530. *pos = len;
  531. return 0;
  532. }
  533. if (order)
  534. {
  535. v = d[p++]<<8; /* BE */
  536. v |= d[p++];
  537. }
  538. else
  539. {
  540. v = d[p++]; /* LE */
  541. v |= d[p++]<<8;
  542. }
  543. *pos = p;
  544. return v;
  545. }
  546. static uint32_t read32(const uint8_t *d, size_t *pos, size_t len, int order)
  547. {
  548. size_t p = *pos;
  549. uint32_t v;
  550. if (p+3 >= len)
  551. {
  552. *pos = len;
  553. return 0;
  554. }
  555. if (order)
  556. {
  557. v = d[p++]<<24; /* BE */
  558. v |= d[p++]<<16;
  559. v |= d[p++]<<8;
  560. v |= d[p++];
  561. }
  562. else
  563. {
  564. v = d[p++];
  565. v |= d[p++]<<8; /* LE */
  566. v |= d[p++]<<16;
  567. v |= d[p++]<<24;
  568. }
  569. *pos = p;
  570. return v;
  571. }
  572. static void write16(uint8_t *d, size_t *pos, size_t len, int order, uint32_t v)
  573. {
  574. size_t p = *pos;
  575. if (p+1 >= len)
  576. {
  577. *pos = len;
  578. return;
  579. }
  580. if (order)
  581. {
  582. d[p++] = (v>>8);
  583. d[p++] = v;
  584. }
  585. else
  586. {
  587. d[p++] = v;
  588. d[p++] = (v>>8);
  589. }
  590. *pos = p;
  591. }
  592. static void write32( uint8_t *d, size_t *pos, size_t len, int order, uint32_t v)
  593. {
  594. size_t p = *pos;
  595. if (p+3 >= len)
  596. {
  597. *pos = len;
  598. return;
  599. }
  600. if (order)
  601. {
  602. d[p++] = (v>>24);
  603. d[p++] = (v>>16);
  604. d[p++] = (v>>8);
  605. d[p++] = v;
  606. }
  607. else
  608. {
  609. d[p++] = v;
  610. d[p++] = (v>>8);
  611. d[p++] = (v>>16);
  612. d[p++] = (v>>24);
  613. }
  614. *pos = p;
  615. }
  616. fz_buffer *
  617. fz_sanitize_jpeg_buffer(fz_context *ctx, fz_buffer *in)
  618. {
  619. fz_buffer *out = fz_clone_buffer(ctx, in);
  620. size_t len = out->len;
  621. size_t pos = 0;
  622. uint8_t *d = out->data;
  623. /* We need at least 4 data bytes. */
  624. while (pos+4 < len)
  625. {
  626. uint8_t m;
  627. /* We should be on a marker. If not, inch forwards until we are. */
  628. if (d[pos++] != 0xff)
  629. continue;
  630. m = d[pos++];
  631. if (m == 0xDA)
  632. break; /* Start Of Scan. All our rewriting happens before this. */
  633. if (m == 0xE1)
  634. {
  635. uint8_t order;
  636. uint32_t tmp;
  637. size_t body_start;
  638. /* APP1 tag. This is where the EXIF data lives. */
  639. /* Read and discard the marker length. We're not continuing after this anyway. */
  640. (void)read16(d, &pos, len, 0);
  641. tmp = read32(d, &pos, len, 0);
  642. if (tmp != 0x66697845) /* Exif */
  643. break; /* Not exif - nothing to rewrite. */
  644. tmp = read16(d, &pos, len, 0);
  645. if (tmp != 0) /* Terminator + Pad */
  646. break; /* Not exif - nothing to rewrite. */
  647. /* Now we're at the APP1 Body. */
  648. body_start = pos;
  649. tmp = read16(d, &pos, len, 0);
  650. if (tmp == 0x4949)
  651. order = 0; /* LE */
  652. else if (tmp == 0x4d4d)
  653. order = 1; /* BE */
  654. else
  655. break; /* Bad TIFF type. Bale. */
  656. tmp = read16(d, &pos, len, order);
  657. if (tmp != 0x002a) /* 42 */
  658. break; /* Bad version field. Bale. */
  659. do
  660. {
  661. uint32_t i, n;
  662. tmp = read32(d, &pos, len, order);
  663. pos = body_start + tmp;
  664. if (tmp == 0 || pos >= len)
  665. break;
  666. n = read16(d, &pos, len, order);
  667. for (i = 0; i < n; i++)
  668. {
  669. if (read16(d, &pos, len, order) == 0x112)
  670. {
  671. /* Orientation tag! */
  672. write16(d, &pos, len, order, 3); /* 3 = short */
  673. write32(d, &pos, len, order, 1); /* Count = 1 */
  674. write16(d, &pos, len, order, 1); /* Value = 1 */
  675. write16(d, &pos, len, order, 0); /* padding */
  676. i = n;
  677. pos = len; /* Done! */
  678. }
  679. else
  680. pos += 10;
  681. }
  682. }
  683. while (pos+4 < len);
  684. break;
  685. }
  686. else if (m >= 0xD0 && m <= 0xD7)
  687. {
  688. /* RSTm - no length code. But we shouldn't hit this! */
  689. }
  690. else if (m == 0x01)
  691. {
  692. /* TEM - temporary private use in arithmetic coding - shouldn't hit this either. */
  693. }
  694. else if (m == 0xD8)
  695. {
  696. /* SOI - start of image. */
  697. }
  698. else if (m == 0x01)
  699. {
  700. /* EOI - end of image. */
  701. }
  702. else
  703. {
  704. /* All other markers have a length. */
  705. size_t marker_len = d[pos]*256 + d[pos+1];
  706. pos += marker_len; /* The 2 length bytes are included in the marker_len */
  707. }
  708. }
  709. return out;
  710. }
  711. void
  712. fz_append_image_as_data_uri(fz_context *ctx, fz_buffer *out, fz_image *image)
  713. {
  714. fz_compressed_buffer *cbuf;
  715. fz_buffer *buf;
  716. cbuf = fz_compressed_image_buffer(ctx, image);
  717. if (cbuf && cbuf->params.type == FZ_IMAGE_JPEG)
  718. {
  719. int type = fz_colorspace_type(ctx, image->colorspace);
  720. if (type == FZ_COLORSPACE_GRAY || type == FZ_COLORSPACE_RGB)
  721. {
  722. fz_buffer *new_buf = fz_sanitize_jpeg_buffer(ctx, cbuf->buffer);
  723. fz_append_string(ctx, out, "data:image/jpeg;base64,");
  724. fz_try(ctx)
  725. fz_append_base64_buffer(ctx, out, new_buf, 1);
  726. fz_always(ctx)
  727. fz_drop_buffer(ctx, new_buf);
  728. fz_catch(ctx)
  729. fz_rethrow(ctx);
  730. return;
  731. }
  732. }
  733. if (cbuf && cbuf->params.type == FZ_IMAGE_PNG)
  734. {
  735. fz_append_string(ctx, out, "data:image/png;base64,");
  736. fz_append_base64_buffer(ctx, out, cbuf->buffer, 1);
  737. return;
  738. }
  739. buf = fz_new_buffer_from_image_as_png(ctx, image, fz_default_color_params);
  740. fz_try(ctx)
  741. {
  742. fz_append_string(ctx, out, "data:image/png;base64,");
  743. fz_append_base64_buffer(ctx, out, buf, 1);
  744. }
  745. fz_always(ctx)
  746. fz_drop_buffer(ctx, buf);
  747. fz_catch(ctx)
  748. fz_rethrow(ctx);
  749. }
  750. void
  751. fz_write_pixmap_as_data_uri(fz_context *ctx, fz_output *out, fz_pixmap *pixmap)
  752. {
  753. fz_buffer *buf = fz_new_buffer_from_pixmap_as_png(ctx, pixmap, fz_default_color_params);
  754. fz_try(ctx)
  755. {
  756. fz_write_string(ctx, out, "data:image/png;base64,");
  757. fz_write_base64_buffer(ctx, out, buf, 1);
  758. }
  759. fz_always(ctx)
  760. fz_drop_buffer(ctx, buf);
  761. fz_catch(ctx)
  762. fz_rethrow(ctx);
  763. }
  764. void
  765. fz_append_pixmap_as_data_uri(fz_context *ctx, fz_buffer *out, fz_pixmap *pixmap)
  766. {
  767. fz_buffer *buf = fz_new_buffer_from_pixmap_as_png(ctx, pixmap, fz_default_color_params);
  768. fz_try(ctx)
  769. {
  770. fz_append_string(ctx, out, "data:image/png;base64,");
  771. fz_append_base64_buffer(ctx, out, buf, 1);
  772. }
  773. fz_always(ctx)
  774. fz_drop_buffer(ctx, buf);
  775. fz_catch(ctx)
  776. fz_rethrow(ctx);
  777. }
  778. fz_document *
  779. fz_new_xhtml_document_from_document(fz_context *ctx, fz_document *old_doc, const fz_stext_options *opts)
  780. {
  781. fz_stext_options default_opts = { FZ_STEXT_PRESERVE_IMAGES | FZ_STEXT_DEHYPHENATE };
  782. fz_document *new_doc;
  783. fz_buffer *buf = NULL;
  784. fz_output *out = NULL;
  785. fz_stream *stm = NULL;
  786. fz_stext_page *text = NULL;
  787. int i;
  788. fz_var(buf);
  789. fz_var(out);
  790. fz_var(stm);
  791. fz_var(text);
  792. if (!opts)
  793. opts = &default_opts;
  794. fz_try(ctx)
  795. {
  796. buf = fz_new_buffer(ctx, 8192);
  797. out = fz_new_output_with_buffer(ctx, buf);
  798. fz_print_stext_header_as_xhtml(ctx, out);
  799. for (i = 0; i < fz_count_pages(ctx, old_doc); ++i)
  800. {
  801. text = fz_new_stext_page_from_page_number(ctx, old_doc, i, opts);
  802. fz_print_stext_page_as_xhtml(ctx, out, text, i+1);
  803. fz_drop_stext_page(ctx, text);
  804. text = NULL;
  805. }
  806. fz_print_stext_trailer_as_xhtml(ctx, out);
  807. fz_close_output(ctx, out);
  808. fz_terminate_buffer(ctx, buf);
  809. stm = fz_open_buffer(ctx, buf);
  810. new_doc = fz_open_document_with_stream(ctx, "application/xhtml+xml", stm);
  811. }
  812. fz_always(ctx)
  813. {
  814. fz_drop_stream(ctx, stm);
  815. fz_drop_buffer(ctx, buf);
  816. fz_drop_output(ctx, out);
  817. fz_drop_stext_page(ctx, text);
  818. }
  819. fz_catch(ctx)
  820. fz_rethrow(ctx);
  821. return new_doc;
  822. }
  823. fz_buffer *
  824. fz_new_buffer_from_page_with_format(fz_context *ctx, fz_page *page, const char *format, const char *options, fz_matrix transform, fz_cookie *cookie)
  825. {
  826. fz_buffer *buf = NULL;
  827. fz_output *out;
  828. fz_document_writer *writer = NULL;
  829. fz_device *dev = NULL;
  830. fz_var(buf);
  831. fz_var(writer);
  832. fz_var(dev);
  833. fz_try(ctx)
  834. {
  835. buf = fz_new_buffer(ctx, 0);
  836. out = fz_new_output_with_buffer(ctx, buf);
  837. writer = fz_new_document_writer_with_output(ctx, out, format, options);
  838. dev = fz_begin_page(ctx, writer, fz_bound_page(ctx, page));
  839. fz_run_page(ctx, page, dev, transform, cookie);
  840. fz_end_page(ctx, writer);
  841. fz_close_document_writer(ctx, writer);
  842. }
  843. fz_always(ctx)
  844. fz_drop_document_writer(ctx, writer);
  845. fz_catch(ctx)
  846. {
  847. fz_drop_buffer(ctx, buf);
  848. fz_rethrow(ctx);
  849. }
  850. return buf;
  851. }