comparison mupdf-source/source/fitz/output-cbz.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
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
23 #include "mupdf/fitz.h"
24
25 #include <zlib.h>
26
27 #include <limits.h>
28
29 typedef struct
30 {
31 fz_document_writer super;
32 fz_draw_options options;
33 fz_pixmap *pixmap;
34 int count;
35 fz_zip_writer *zip;
36 } fz_cbz_writer;
37
38 static fz_device *
39 cbz_begin_page(fz_context *ctx, fz_document_writer *wri_, fz_rect mediabox)
40 {
41 fz_cbz_writer *wri = (fz_cbz_writer*)wri_;
42 return fz_new_draw_device_with_options(ctx, &wri->options, mediabox, &wri->pixmap);
43 }
44
45 static void
46 cbz_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
47 {
48 fz_cbz_writer *wri = (fz_cbz_writer*)wri_;
49 fz_buffer *buffer = NULL;
50 char name[40];
51
52 fz_var(buffer);
53
54 fz_try(ctx)
55 {
56 fz_close_device(ctx, dev);
57 wri->count += 1;
58 fz_snprintf(name, sizeof name, "p%04d.png", wri->count);
59 buffer = fz_new_buffer_from_pixmap_as_png(ctx, wri->pixmap, fz_default_color_params);
60 fz_write_zip_entry(ctx, wri->zip, name, buffer, 0);
61 }
62 fz_always(ctx)
63 {
64 fz_drop_device(ctx, dev);
65 fz_drop_buffer(ctx, buffer);
66 fz_drop_pixmap(ctx, wri->pixmap);
67 wri->pixmap = NULL;
68 }
69 fz_catch(ctx)
70 fz_rethrow(ctx);
71 }
72
73 static void
74 cbz_close_writer(fz_context *ctx, fz_document_writer *wri_)
75 {
76 fz_cbz_writer *wri = (fz_cbz_writer*)wri_;
77 fz_close_zip_writer(ctx, wri->zip);
78 }
79
80 static void
81 cbz_drop_writer(fz_context *ctx, fz_document_writer *wri_)
82 {
83 fz_cbz_writer *wri = (fz_cbz_writer*)wri_;
84 fz_drop_zip_writer(ctx, wri->zip);
85 fz_drop_pixmap(ctx, wri->pixmap);
86 }
87
88 fz_document_writer *
89 fz_new_cbz_writer_with_output(fz_context *ctx, fz_output *out, const char *options)
90 {
91 fz_cbz_writer *wri = NULL;
92
93 fz_var(wri);
94 fz_var(out);
95
96 fz_try(ctx)
97 {
98 fz_output *out_temp = out;
99 wri = fz_new_derived_document_writer(ctx, fz_cbz_writer, cbz_begin_page, cbz_end_page, cbz_close_writer, cbz_drop_writer);
100 fz_parse_draw_options(ctx, &wri->options, options);
101 out = NULL;
102 wri->zip = fz_new_zip_writer_with_output(ctx, out_temp);
103 }
104 fz_catch(ctx)
105 {
106 fz_drop_output(ctx, out);
107 fz_free(ctx, wri);
108 fz_rethrow(ctx);
109 }
110 return (fz_document_writer*)wri;
111 }
112
113 fz_document_writer *
114 fz_new_cbz_writer(fz_context *ctx, const char *path, const char *options)
115 {
116 fz_output *out = fz_new_output_with_path(ctx, path ? path : "out.cbz", 0);
117 fz_document_writer *wri = NULL;
118 fz_try(ctx)
119 wri = fz_new_cbz_writer_with_output(ctx, out, options);
120 fz_catch(ctx)
121 {
122 fz_drop_output(ctx, out);
123 fz_rethrow(ctx);
124 }
125 return wri;
126 }
127
128 /* generic image file output writer */
129
130 typedef struct
131 {
132 fz_document_writer super;
133 fz_draw_options options;
134 fz_pixmap *pixmap;
135 void (*save)(fz_context *ctx, fz_pixmap *pix, const char *filename);
136 int count;
137 char *path;
138 } fz_pixmap_writer;
139
140 static fz_device *
141 pixmap_begin_page(fz_context *ctx, fz_document_writer *wri_, fz_rect mediabox)
142 {
143 fz_pixmap_writer *wri = (fz_pixmap_writer*)wri_;
144 return fz_new_draw_device_with_options(ctx, &wri->options, mediabox, &wri->pixmap);
145 }
146
147 static void
148 pixmap_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
149 {
150 fz_pixmap_writer *wri = (fz_pixmap_writer*)wri_;
151 char path[PATH_MAX];
152
153 fz_try(ctx)
154 {
155 fz_close_device(ctx, dev);
156 wri->count += 1;
157 fz_format_output_path(ctx, path, sizeof path, wri->path, wri->count);
158 wri->save(ctx, wri->pixmap, path);
159 }
160 fz_always(ctx)
161 {
162 fz_drop_device(ctx, dev);
163 fz_drop_pixmap(ctx, wri->pixmap);
164 wri->pixmap = NULL;
165 }
166 fz_catch(ctx)
167 fz_rethrow(ctx);
168 }
169
170 static void
171 pixmap_drop_writer(fz_context *ctx, fz_document_writer *wri_)
172 {
173 fz_pixmap_writer *wri = (fz_pixmap_writer*)wri_;
174 fz_drop_pixmap(ctx, wri->pixmap);
175 fz_free(ctx, wri->path);
176 }
177
178 fz_document_writer *
179 fz_new_pixmap_writer(fz_context *ctx, const char *path, const char *options,
180 const char *default_path, int n,
181 void (*save)(fz_context *ctx, fz_pixmap *pix, const char *filename))
182 {
183 fz_pixmap_writer *wri = fz_new_derived_document_writer(ctx, fz_pixmap_writer, pixmap_begin_page, pixmap_end_page, NULL, pixmap_drop_writer);
184
185 fz_try(ctx)
186 {
187 fz_parse_draw_options(ctx, &wri->options, options);
188 wri->path = fz_strdup(ctx, path ? path : default_path);
189 wri->save = save;
190 switch (n)
191 {
192 case 1: wri->options.colorspace = fz_device_gray(ctx); break;
193 case 3: wri->options.colorspace = fz_device_rgb(ctx); break;
194 case 4: wri->options.colorspace = fz_device_cmyk(ctx); break;
195 }
196 }
197 fz_catch(ctx)
198 {
199 fz_free(ctx, wri);
200 fz_rethrow(ctx);
201 }
202
203 return (fz_document_writer*)wri;
204 }