output-jpeg.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // Copyright (C) 2004-2023 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 <jpeglib.h>
  24. #ifdef SHARE_JPEG
  25. #define JZ_CTX_FROM_CINFO(c) (fz_context *)((c)->client_data)
  26. static void fz_jpg_mem_init(j_common_ptr cinfo, fz_context *ctx)
  27. {
  28. cinfo->client_data = ctx;
  29. }
  30. #define fz_jpg_mem_term(cinfo)
  31. #else /* SHARE_JPEG */
  32. typedef void * backing_store_ptr;
  33. #include "jmemcust.h"
  34. #define JZ_CTX_FROM_CINFO(c) (fz_context *)(GET_CUST_MEM_DATA(c)->priv)
  35. static void *
  36. fz_jpg_mem_alloc(j_common_ptr cinfo, size_t size)
  37. {
  38. fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo);
  39. return fz_malloc_no_throw(ctx, size);
  40. }
  41. static void
  42. fz_jpg_mem_free(j_common_ptr cinfo, void *object, size_t size)
  43. {
  44. fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo);
  45. fz_free(ctx, object);
  46. }
  47. static void
  48. fz_jpg_mem_init(j_common_ptr cinfo, fz_context *ctx)
  49. {
  50. jpeg_cust_mem_data *custmptr;
  51. custmptr = fz_malloc_struct(ctx, jpeg_cust_mem_data);
  52. if (!jpeg_cust_mem_init(custmptr, (void *) ctx, NULL, NULL, NULL,
  53. fz_jpg_mem_alloc, fz_jpg_mem_free,
  54. fz_jpg_mem_alloc, fz_jpg_mem_free, NULL))
  55. {
  56. fz_free(ctx, custmptr);
  57. fz_throw(ctx, FZ_ERROR_LIBRARY, "cannot initialize custom JPEG memory handler");
  58. }
  59. cinfo->client_data = custmptr;
  60. }
  61. static void
  62. fz_jpg_mem_term(j_common_ptr cinfo)
  63. {
  64. if (cinfo->client_data)
  65. {
  66. fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo);
  67. fz_free(ctx, cinfo->client_data);
  68. cinfo->client_data = NULL;
  69. }
  70. }
  71. #endif
  72. #define OUTPUT_BUF_SIZE (16<<10)
  73. typedef struct {
  74. struct jpeg_destination_mgr pub;
  75. fz_output *out;
  76. JOCTET buffer[OUTPUT_BUF_SIZE];
  77. } my_destination_mgr;
  78. typedef my_destination_mgr * my_dest_ptr;
  79. static void error_exit(j_common_ptr cinfo)
  80. {
  81. char msg[JMSG_LENGTH_MAX];
  82. fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo);
  83. cinfo->err->format_message(cinfo, msg);
  84. fz_throw(ctx, FZ_ERROR_LIBRARY, "jpeg error: %s", msg);
  85. }
  86. static void init_destination(j_compress_ptr cinfo)
  87. {
  88. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  89. dest->pub.next_output_byte = dest->buffer;
  90. dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  91. }
  92. static boolean empty_output_buffer(j_compress_ptr cinfo)
  93. {
  94. fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo);
  95. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  96. fz_output *out = dest->out;
  97. fz_write_data(ctx, out, dest->buffer, OUTPUT_BUF_SIZE);
  98. dest->pub.next_output_byte = dest->buffer;
  99. dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  100. return TRUE;
  101. }
  102. static void term_destination(j_compress_ptr cinfo)
  103. {
  104. fz_context *ctx = JZ_CTX_FROM_CINFO(cinfo);
  105. my_dest_ptr dest = (my_dest_ptr) cinfo->dest;
  106. fz_output *out = dest->out;
  107. size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
  108. fz_write_data(ctx, out, dest->buffer, datacount);
  109. }
  110. void
  111. fz_write_pixmap_as_jpeg(fz_context *ctx, fz_output *out, fz_pixmap *pix, int quality, int invert_cmyk)
  112. {
  113. struct jpeg_compress_struct cinfo;
  114. struct jpeg_error_mgr err;
  115. my_destination_mgr dest;
  116. JSAMPROW row_pointer[1];
  117. unsigned char *outbuffer = NULL;
  118. size_t outsize = 0;
  119. fz_colorspace *cs = pix->colorspace;
  120. int n = pix->n;
  121. int alpha = pix->alpha;
  122. if (pix->s > 0)
  123. fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap may not have separations to save as JPEG");
  124. if (cs && !fz_colorspace_is_gray(ctx, cs) && !fz_colorspace_is_rgb(ctx, cs) && !fz_colorspace_is_cmyk(ctx, cs))
  125. fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap must be Grayscale, RGB, or CMYK to save as JPEG");
  126. /* Treat alpha only as greyscale */
  127. if (n == 1 && alpha)
  128. alpha = 0;
  129. n -= alpha;
  130. if (alpha > 0)
  131. fz_throw(ctx, FZ_ERROR_ARGUMENT, "pixmap may not have alpha to save as JPEG");
  132. cinfo.mem = NULL;
  133. cinfo.global_state = 0;
  134. cinfo.err = jpeg_std_error(&err);
  135. err.error_exit = error_exit;
  136. cinfo.client_data = NULL;
  137. fz_jpg_mem_init((j_common_ptr)&cinfo, ctx);
  138. fz_try(ctx)
  139. {
  140. jpeg_create_compress(&cinfo);
  141. cinfo.dest = (void*) &dest;
  142. dest.pub.init_destination = init_destination;
  143. dest.pub.empty_output_buffer = empty_output_buffer;
  144. dest.pub.term_destination = term_destination;
  145. dest.out = out;
  146. cinfo.image_width = pix->w;
  147. cinfo.image_height = pix->h;
  148. cinfo.input_components = n;
  149. switch (n) {
  150. case 1:
  151. cinfo.in_color_space = JCS_GRAYSCALE;
  152. break;
  153. case 3:
  154. cinfo.in_color_space = JCS_RGB;
  155. break;
  156. case 4:
  157. cinfo.in_color_space = JCS_CMYK;
  158. break;
  159. }
  160. jpeg_set_defaults(&cinfo);
  161. jpeg_set_quality(&cinfo, quality, FALSE);
  162. /* Write image resolution */
  163. cinfo.density_unit = 1; /* dots/inch */
  164. cinfo.X_density = pix->xres;
  165. cinfo.Y_density = pix->yres;
  166. /* Disable chroma subsampling */
  167. cinfo.comp_info[0].h_samp_factor = 1;
  168. cinfo.comp_info[0].v_samp_factor = 1;
  169. /* Progressive JPEGs are smaller */
  170. jpeg_simple_progression(&cinfo);
  171. jpeg_start_compress(&cinfo, TRUE);
  172. if (fz_colorspace_is_cmyk(ctx, pix->colorspace) && invert_cmyk)
  173. fz_invert_pixmap_raw(ctx, pix);
  174. while (cinfo.next_scanline < cinfo.image_height) {
  175. row_pointer[0] = &pix->samples[cinfo.next_scanline * pix->stride];
  176. (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
  177. }
  178. if (fz_colorspace_is_cmyk(ctx, pix->colorspace) && invert_cmyk)
  179. fz_invert_pixmap_raw(ctx, pix);
  180. jpeg_finish_compress(&cinfo);
  181. fz_write_data(ctx, out, outbuffer, outsize);
  182. }
  183. fz_always(ctx)
  184. {
  185. jpeg_destroy_compress(&cinfo);
  186. fz_jpg_mem_term((j_common_ptr)&cinfo);
  187. fz_free(ctx, outbuffer);
  188. }
  189. fz_catch(ctx)
  190. {
  191. fz_rethrow(ctx);
  192. }
  193. }
  194. void
  195. fz_save_pixmap_as_jpeg(fz_context *ctx, fz_pixmap *pixmap, const char *filename, int quality)
  196. {
  197. fz_output *out = fz_new_output_with_path(ctx, filename, 0);
  198. fz_try(ctx)
  199. {
  200. fz_write_pixmap_as_jpeg(ctx, out, pixmap, quality, 1);
  201. fz_close_output(ctx, out);
  202. }
  203. fz_always(ctx)
  204. {
  205. fz_drop_output(ctx, out);
  206. }
  207. fz_catch(ctx)
  208. {
  209. fz_rethrow(ctx);
  210. }
  211. }
  212. static fz_buffer *
  213. jpeg_from_pixmap(fz_context *ctx, fz_pixmap *pix, fz_color_params color_params, int quality, int invert_cmyk, int drop)
  214. {
  215. fz_buffer *buf = NULL;
  216. fz_output *out = NULL;
  217. fz_var(buf);
  218. fz_var(out);
  219. fz_try(ctx)
  220. {
  221. buf = fz_new_buffer(ctx, 1024);
  222. out = fz_new_output_with_buffer(ctx, buf);
  223. fz_write_pixmap_as_jpeg(ctx, out, pix, quality, invert_cmyk);
  224. fz_close_output(ctx, out);
  225. }
  226. fz_always(ctx)
  227. {
  228. if (drop)
  229. fz_drop_pixmap(ctx, pix);
  230. fz_drop_output(ctx, out);
  231. }
  232. fz_catch(ctx)
  233. {
  234. fz_drop_buffer(ctx, buf);
  235. fz_rethrow(ctx);
  236. }
  237. return buf;
  238. }
  239. fz_buffer *
  240. fz_new_buffer_from_image_as_jpeg(fz_context *ctx, fz_image *image, fz_color_params color_params, int quality, int invert_cmyk)
  241. {
  242. fz_pixmap *pix = fz_get_pixmap_from_image(ctx, image, NULL, NULL, NULL, NULL);
  243. return jpeg_from_pixmap(ctx, pix, color_params, quality, 1, invert_cmyk);
  244. }
  245. fz_buffer *
  246. fz_new_buffer_from_pixmap_as_jpeg(fz_context *ctx, fz_pixmap *pix, fz_color_params color_params, int quality, int invert_cmyk)
  247. {
  248. return jpeg_from_pixmap(ctx, pix, color_params, quality, 0, invert_cmyk);
  249. }