output-cbz.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 <zlib.h>
  24. #include <limits.h>
  25. typedef struct
  26. {
  27. fz_document_writer super;
  28. fz_draw_options options;
  29. fz_pixmap *pixmap;
  30. int count;
  31. fz_zip_writer *zip;
  32. } fz_cbz_writer;
  33. static fz_device *
  34. cbz_begin_page(fz_context *ctx, fz_document_writer *wri_, fz_rect mediabox)
  35. {
  36. fz_cbz_writer *wri = (fz_cbz_writer*)wri_;
  37. return fz_new_draw_device_with_options(ctx, &wri->options, mediabox, &wri->pixmap);
  38. }
  39. static void
  40. cbz_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
  41. {
  42. fz_cbz_writer *wri = (fz_cbz_writer*)wri_;
  43. fz_buffer *buffer = NULL;
  44. char name[40];
  45. fz_var(buffer);
  46. fz_try(ctx)
  47. {
  48. fz_close_device(ctx, dev);
  49. wri->count += 1;
  50. fz_snprintf(name, sizeof name, "p%04d.png", wri->count);
  51. buffer = fz_new_buffer_from_pixmap_as_png(ctx, wri->pixmap, fz_default_color_params);
  52. fz_write_zip_entry(ctx, wri->zip, name, buffer, 0);
  53. }
  54. fz_always(ctx)
  55. {
  56. fz_drop_device(ctx, dev);
  57. fz_drop_buffer(ctx, buffer);
  58. fz_drop_pixmap(ctx, wri->pixmap);
  59. wri->pixmap = NULL;
  60. }
  61. fz_catch(ctx)
  62. fz_rethrow(ctx);
  63. }
  64. static void
  65. cbz_close_writer(fz_context *ctx, fz_document_writer *wri_)
  66. {
  67. fz_cbz_writer *wri = (fz_cbz_writer*)wri_;
  68. fz_close_zip_writer(ctx, wri->zip);
  69. }
  70. static void
  71. cbz_drop_writer(fz_context *ctx, fz_document_writer *wri_)
  72. {
  73. fz_cbz_writer *wri = (fz_cbz_writer*)wri_;
  74. fz_drop_zip_writer(ctx, wri->zip);
  75. fz_drop_pixmap(ctx, wri->pixmap);
  76. }
  77. fz_document_writer *
  78. fz_new_cbz_writer_with_output(fz_context *ctx, fz_output *out, const char *options)
  79. {
  80. fz_cbz_writer *wri = NULL;
  81. fz_var(wri);
  82. fz_var(out);
  83. fz_try(ctx)
  84. {
  85. fz_output *out_temp = out;
  86. wri = fz_new_derived_document_writer(ctx, fz_cbz_writer, cbz_begin_page, cbz_end_page, cbz_close_writer, cbz_drop_writer);
  87. fz_parse_draw_options(ctx, &wri->options, options);
  88. out = NULL;
  89. wri->zip = fz_new_zip_writer_with_output(ctx, out_temp);
  90. }
  91. fz_catch(ctx)
  92. {
  93. fz_drop_output(ctx, out);
  94. fz_free(ctx, wri);
  95. fz_rethrow(ctx);
  96. }
  97. return (fz_document_writer*)wri;
  98. }
  99. fz_document_writer *
  100. fz_new_cbz_writer(fz_context *ctx, const char *path, const char *options)
  101. {
  102. fz_output *out = fz_new_output_with_path(ctx, path ? path : "out.cbz", 0);
  103. fz_document_writer *wri = NULL;
  104. fz_try(ctx)
  105. wri = fz_new_cbz_writer_with_output(ctx, out, options);
  106. fz_catch(ctx)
  107. {
  108. fz_drop_output(ctx, out);
  109. fz_rethrow(ctx);
  110. }
  111. return wri;
  112. }
  113. /* generic image file output writer */
  114. typedef struct
  115. {
  116. fz_document_writer super;
  117. fz_draw_options options;
  118. fz_pixmap *pixmap;
  119. void (*save)(fz_context *ctx, fz_pixmap *pix, const char *filename);
  120. int count;
  121. char *path;
  122. } fz_pixmap_writer;
  123. static fz_device *
  124. pixmap_begin_page(fz_context *ctx, fz_document_writer *wri_, fz_rect mediabox)
  125. {
  126. fz_pixmap_writer *wri = (fz_pixmap_writer*)wri_;
  127. return fz_new_draw_device_with_options(ctx, &wri->options, mediabox, &wri->pixmap);
  128. }
  129. static void
  130. pixmap_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
  131. {
  132. fz_pixmap_writer *wri = (fz_pixmap_writer*)wri_;
  133. char path[PATH_MAX];
  134. fz_try(ctx)
  135. {
  136. fz_close_device(ctx, dev);
  137. wri->count += 1;
  138. fz_format_output_path(ctx, path, sizeof path, wri->path, wri->count);
  139. wri->save(ctx, wri->pixmap, path);
  140. }
  141. fz_always(ctx)
  142. {
  143. fz_drop_device(ctx, dev);
  144. fz_drop_pixmap(ctx, wri->pixmap);
  145. wri->pixmap = NULL;
  146. }
  147. fz_catch(ctx)
  148. fz_rethrow(ctx);
  149. }
  150. static void
  151. pixmap_drop_writer(fz_context *ctx, fz_document_writer *wri_)
  152. {
  153. fz_pixmap_writer *wri = (fz_pixmap_writer*)wri_;
  154. fz_drop_pixmap(ctx, wri->pixmap);
  155. fz_free(ctx, wri->path);
  156. }
  157. fz_document_writer *
  158. fz_new_pixmap_writer(fz_context *ctx, const char *path, const char *options,
  159. const char *default_path, int n,
  160. void (*save)(fz_context *ctx, fz_pixmap *pix, const char *filename))
  161. {
  162. fz_pixmap_writer *wri = fz_new_derived_document_writer(ctx, fz_pixmap_writer, pixmap_begin_page, pixmap_end_page, NULL, pixmap_drop_writer);
  163. fz_try(ctx)
  164. {
  165. fz_parse_draw_options(ctx, &wri->options, options);
  166. wri->path = fz_strdup(ctx, path ? path : default_path);
  167. wri->save = save;
  168. switch (n)
  169. {
  170. case 1: wri->options.colorspace = fz_device_gray(ctx); break;
  171. case 3: wri->options.colorspace = fz_device_rgb(ctx); break;
  172. case 4: wri->options.colorspace = fz_device_cmyk(ctx); break;
  173. }
  174. }
  175. fz_catch(ctx)
  176. {
  177. fz_free(ctx, wri);
  178. fz_rethrow(ctx);
  179. }
  180. return (fz_document_writer*)wri;
  181. }