muimg.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 <string.h>
  24. #define DPI 72.0f
  25. typedef struct
  26. {
  27. fz_page super;
  28. fz_image *image;
  29. } img_page;
  30. typedef struct
  31. {
  32. fz_document super;
  33. fz_buffer *buffer;
  34. const char *format;
  35. int page_count;
  36. fz_pixmap *(*load_subimage)(fz_context *ctx, const unsigned char *p, size_t total, int subimage);
  37. } img_document;
  38. static void
  39. img_drop_document(fz_context *ctx, fz_document *doc_)
  40. {
  41. img_document *doc = (img_document*)doc_;
  42. fz_drop_buffer(ctx, doc->buffer);
  43. }
  44. static int
  45. img_count_pages(fz_context *ctx, fz_document *doc_, int chapter)
  46. {
  47. img_document *doc = (img_document*)doc_;
  48. return doc->page_count;
  49. }
  50. static fz_rect
  51. img_bound_page(fz_context *ctx, fz_page *page_, fz_box_type box)
  52. {
  53. img_page *page = (img_page*)page_;
  54. fz_image *image = page->image;
  55. int xres, yres;
  56. fz_rect bbox;
  57. uint8_t orientation = fz_image_orientation(ctx, page->image);
  58. fz_image_resolution(image, &xres, &yres);
  59. bbox.x0 = bbox.y0 = 0;
  60. if (orientation == 0 || (orientation & 1) == 1)
  61. {
  62. bbox.x1 = image->w * DPI / xres;
  63. bbox.y1 = image->h * DPI / yres;
  64. }
  65. else
  66. {
  67. bbox.y1 = image->w * DPI / xres;
  68. bbox.x1 = image->h * DPI / yres;
  69. }
  70. return bbox;
  71. }
  72. static void
  73. img_run_page(fz_context *ctx, fz_page *page_, fz_device *dev, fz_matrix ctm, fz_cookie *cookie)
  74. {
  75. img_page *page = (img_page*)page_;
  76. fz_image *image = page->image;
  77. int xres, yres;
  78. float w, h;
  79. uint8_t orientation = fz_image_orientation(ctx, page->image);
  80. fz_matrix immat = fz_image_orientation_matrix(ctx, page->image);
  81. fz_image_resolution(image, &xres, &yres);
  82. if (orientation == 0 || (orientation & 1) == 1)
  83. {
  84. w = image->w * DPI / xres;
  85. h = image->h * DPI / yres;
  86. }
  87. else
  88. {
  89. h = image->w * DPI / xres;
  90. w = image->h * DPI / yres;
  91. }
  92. immat = fz_post_scale(immat, w, h);
  93. ctm = fz_concat(immat, ctm);
  94. fz_fill_image(ctx, dev, image, ctm, 1, fz_default_color_params);
  95. }
  96. static void
  97. img_drop_page(fz_context *ctx, fz_page *page_)
  98. {
  99. img_page *page = (img_page*)page_;
  100. fz_drop_image(ctx, page->image);
  101. }
  102. static fz_page *
  103. img_load_page(fz_context *ctx, fz_document *doc_, int chapter, int number)
  104. {
  105. img_document *doc = (img_document*)doc_;
  106. fz_pixmap *pixmap = NULL;
  107. fz_image *image = NULL;
  108. img_page *page = NULL;
  109. if (number < 0 || number >= doc->page_count)
  110. fz_throw(ctx, FZ_ERROR_ARGUMENT, "invalid page number %d", number);
  111. fz_var(pixmap);
  112. fz_var(image);
  113. fz_var(page);
  114. fz_try(ctx)
  115. {
  116. if (doc->load_subimage)
  117. {
  118. size_t len;
  119. unsigned char *data;
  120. len = fz_buffer_storage(ctx, doc->buffer, &data);
  121. pixmap = doc->load_subimage(ctx, data, len, number);
  122. image = fz_new_image_from_pixmap(ctx, pixmap, NULL);
  123. }
  124. else
  125. {
  126. image = fz_new_image_from_buffer(ctx, doc->buffer);
  127. }
  128. page = fz_new_derived_page(ctx, img_page, doc_);
  129. page->super.bound_page = img_bound_page;
  130. page->super.run_page_contents = img_run_page;
  131. page->super.drop_page = img_drop_page;
  132. page->image = fz_keep_image(ctx, image);
  133. }
  134. fz_always(ctx)
  135. {
  136. fz_drop_image(ctx, image);
  137. fz_drop_pixmap(ctx, pixmap);
  138. }
  139. fz_catch(ctx)
  140. {
  141. fz_free(ctx, page);
  142. fz_rethrow(ctx);
  143. }
  144. return (fz_page*)page;
  145. }
  146. static int
  147. img_lookup_metadata(fz_context *ctx, fz_document *doc_, const char *key, char *buf, size_t size)
  148. {
  149. img_document *doc = (img_document*)doc_;
  150. if (!strcmp(key, FZ_META_FORMAT))
  151. return 1 + (int)fz_strlcpy(buf, doc->format, size);
  152. return -1;
  153. }
  154. static fz_document *
  155. img_open_document(fz_context *ctx, const fz_document_handler *handler, fz_stream *file, fz_stream *accel, fz_archive *dir, void *state)
  156. {
  157. img_document *doc = fz_new_derived_document(ctx, img_document);
  158. doc->super.drop_document = img_drop_document;
  159. doc->super.count_pages = img_count_pages;
  160. doc->super.load_page = img_load_page;
  161. doc->super.lookup_metadata = img_lookup_metadata;
  162. fz_try(ctx)
  163. {
  164. int fmt;
  165. size_t len;
  166. unsigned char *data;
  167. doc->buffer = fz_read_all(ctx, file, 0);
  168. len = fz_buffer_storage(ctx, doc->buffer, &data);
  169. fmt = FZ_IMAGE_UNKNOWN;
  170. if (len >= 8)
  171. fmt = fz_recognize_image_format(ctx, data);
  172. if (fmt == FZ_IMAGE_TIFF)
  173. {
  174. doc->page_count = fz_load_tiff_subimage_count(ctx, data, len);
  175. doc->load_subimage = fz_load_tiff_subimage;
  176. doc->format = "TIFF";
  177. }
  178. else if (fmt == FZ_IMAGE_PNM)
  179. {
  180. doc->page_count = fz_load_pnm_subimage_count(ctx, data, len);
  181. doc->load_subimage = fz_load_pnm_subimage;
  182. doc->format = "PNM";
  183. }
  184. else if (fmt == FZ_IMAGE_JBIG2)
  185. {
  186. doc->page_count = fz_load_jbig2_subimage_count(ctx, data, len);
  187. if (doc->page_count > 1)
  188. doc->load_subimage = fz_load_jbig2_subimage;
  189. doc->format = "JBIG2";
  190. }
  191. else if (fmt == FZ_IMAGE_BMP)
  192. {
  193. doc->page_count = fz_load_bmp_subimage_count(ctx, data, len);
  194. doc->load_subimage = fz_load_bmp_subimage;
  195. doc->format = "BMP";
  196. }
  197. else
  198. {
  199. doc->page_count = 1;
  200. doc->format = "Image";
  201. }
  202. }
  203. fz_catch(ctx)
  204. {
  205. fz_drop_document(ctx, (fz_document*)doc);
  206. fz_rethrow(ctx);
  207. }
  208. return (fz_document*)doc;
  209. }
  210. static int
  211. img_recognize_content(fz_context *ctx, const fz_document_handler *handler, fz_stream *stream, fz_archive *dir, void **state, fz_document_recognize_state_free_fn **free_state)
  212. {
  213. unsigned char data[8];
  214. size_t n;
  215. int fmt;
  216. if (stream == NULL)
  217. return 0;
  218. if (state)
  219. *state = NULL;
  220. if (free_state)
  221. *free_state = NULL;
  222. n = fz_read(ctx, stream, data, 8);
  223. if (n != 8)
  224. return 0;
  225. fmt = fz_recognize_image_format(ctx, data);
  226. if (fmt != FZ_IMAGE_UNKNOWN)
  227. return 100;
  228. return 0;
  229. }
  230. static const char *img_extensions[] =
  231. {
  232. "bmp",
  233. "gif",
  234. "hdp",
  235. "j2k",
  236. "jb2",
  237. "jbig2",
  238. "jfif",
  239. "jfif-tbnl",
  240. "jp2",
  241. "jpe",
  242. "jpeg",
  243. "jpg",
  244. "jpx",
  245. "jxr",
  246. "pam",
  247. "pbm",
  248. "pfm",
  249. "pgm",
  250. "pkm",
  251. "png",
  252. "pnm",
  253. "ppm",
  254. "psd",
  255. "tif",
  256. "tiff",
  257. "wdp",
  258. NULL
  259. };
  260. static const char *img_mimetypes[] =
  261. {
  262. "image/bmp",
  263. "image/gif",
  264. "image/jp2",
  265. "image/jpeg",
  266. "image/jpx",
  267. "image/jxr",
  268. "image/pjpeg",
  269. "image/png",
  270. "image/tiff",
  271. "image/vnd.ms-photo",
  272. "image/vnd.adobe.photoshop",
  273. "image/x-jb2",
  274. "image/x-jbig2",
  275. "image/x-portable-anymap",
  276. "image/x-portable-arbitrarymap",
  277. "image/x-portable-bitmap",
  278. "image/x-portable-greymap",
  279. "image/x-portable-pixmap",
  280. "image/x-portable-floatmap",
  281. "image/x-tiff",
  282. NULL
  283. };
  284. fz_document_handler img_document_handler =
  285. {
  286. NULL,
  287. img_open_document,
  288. img_extensions,
  289. img_mimetypes,
  290. img_recognize_content
  291. };