Coverage Report

Created: 2025-01-28 06:17

/src/mupdf/source/fitz/output-cbz.c
Line
Count
Source (jump to first uncovered line)
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
0
{
41
0
  fz_cbz_writer *wri = (fz_cbz_writer*)wri_;
42
0
  return fz_new_draw_device_with_options(ctx, &wri->options, mediabox, &wri->pixmap);
43
0
}
44
45
static void
46
cbz_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
47
0
{
48
0
  fz_cbz_writer *wri = (fz_cbz_writer*)wri_;
49
0
  fz_buffer *buffer = NULL;
50
0
  char name[40];
51
52
0
  fz_var(buffer);
53
54
0
  fz_try(ctx)
55
0
  {
56
0
    fz_close_device(ctx, dev);
57
0
    wri->count += 1;
58
0
    fz_snprintf(name, sizeof name, "p%04d.png", wri->count);
59
0
    buffer = fz_new_buffer_from_pixmap_as_png(ctx, wri->pixmap, fz_default_color_params);
60
0
    fz_write_zip_entry(ctx, wri->zip, name, buffer, 0);
61
0
  }
62
0
  fz_always(ctx)
63
0
  {
64
0
    fz_drop_device(ctx, dev);
65
0
    fz_drop_buffer(ctx, buffer);
66
0
    fz_drop_pixmap(ctx, wri->pixmap);
67
0
    wri->pixmap = NULL;
68
0
  }
69
0
  fz_catch(ctx)
70
0
    fz_rethrow(ctx);
71
0
}
72
73
static void
74
cbz_close_writer(fz_context *ctx, fz_document_writer *wri_)
75
0
{
76
0
  fz_cbz_writer *wri = (fz_cbz_writer*)wri_;
77
0
  fz_close_zip_writer(ctx, wri->zip);
78
0
}
79
80
static void
81
cbz_drop_writer(fz_context *ctx, fz_document_writer *wri_)
82
0
{
83
0
  fz_cbz_writer *wri = (fz_cbz_writer*)wri_;
84
0
  fz_drop_zip_writer(ctx, wri->zip);
85
0
  fz_drop_pixmap(ctx, wri->pixmap);
86
0
}
87
88
fz_document_writer *
89
fz_new_cbz_writer_with_output(fz_context *ctx, fz_output *out, const char *options)
90
0
{
91
0
  fz_cbz_writer *wri = NULL;
92
93
0
  fz_var(wri);
94
0
  fz_var(out);
95
96
0
  fz_try(ctx)
97
0
  {
98
0
    fz_output *out_temp = out;
99
0
    wri = fz_new_derived_document_writer(ctx, fz_cbz_writer, cbz_begin_page, cbz_end_page, cbz_close_writer, cbz_drop_writer);
100
0
    fz_parse_draw_options(ctx, &wri->options, options);
101
0
    out = NULL;
102
0
    wri->zip = fz_new_zip_writer_with_output(ctx, out_temp);
103
0
  }
104
0
  fz_catch(ctx)
105
0
  {
106
0
    fz_drop_output(ctx, out);
107
0
    fz_free(ctx, wri);
108
0
    fz_rethrow(ctx);
109
0
  }
110
0
  return (fz_document_writer*)wri;
111
0
}
112
113
fz_document_writer *
114
fz_new_cbz_writer(fz_context *ctx, const char *path, const char *options)
115
0
{
116
0
  fz_output *out = fz_new_output_with_path(ctx, path ? path : "out.cbz", 0);
117
0
  fz_document_writer *wri = NULL;
118
0
  fz_try(ctx)
119
0
    wri = fz_new_cbz_writer_with_output(ctx, out, options);
120
0
  fz_catch(ctx)
121
0
  {
122
0
    fz_drop_output(ctx, out);
123
0
    fz_rethrow(ctx);
124
0
  }
125
0
  return wri;
126
0
}
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
0
{
143
0
  fz_pixmap_writer *wri = (fz_pixmap_writer*)wri_;
144
0
  return fz_new_draw_device_with_options(ctx, &wri->options, mediabox, &wri->pixmap);
145
0
}
146
147
static void
148
pixmap_end_page(fz_context *ctx, fz_document_writer *wri_, fz_device *dev)
149
0
{
150
0
  fz_pixmap_writer *wri = (fz_pixmap_writer*)wri_;
151
0
  char path[PATH_MAX];
152
153
0
  fz_try(ctx)
154
0
  {
155
0
    fz_close_device(ctx, dev);
156
0
    wri->count += 1;
157
0
    fz_format_output_path(ctx, path, sizeof path, wri->path, wri->count);
158
0
    wri->save(ctx, wri->pixmap, path);
159
0
  }
160
0
  fz_always(ctx)
161
0
  {
162
0
    fz_drop_device(ctx, dev);
163
0
    fz_drop_pixmap(ctx, wri->pixmap);
164
0
    wri->pixmap = NULL;
165
0
  }
166
0
  fz_catch(ctx)
167
0
    fz_rethrow(ctx);
168
0
}
169
170
static void
171
pixmap_drop_writer(fz_context *ctx, fz_document_writer *wri_)
172
0
{
173
0
  fz_pixmap_writer *wri = (fz_pixmap_writer*)wri_;
174
0
  fz_drop_pixmap(ctx, wri->pixmap);
175
0
  fz_free(ctx, wri->path);
176
0
}
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
0
{
183
0
  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
0
  fz_try(ctx)
186
0
  {
187
0
    fz_parse_draw_options(ctx, &wri->options, options);
188
0
    wri->path = fz_strdup(ctx, path ? path : default_path);
189
0
    wri->save = save;
190
0
    switch (n)
191
0
    {
192
0
    case 1: wri->options.colorspace = fz_device_gray(ctx); break;
193
0
    case 3: wri->options.colorspace = fz_device_rgb(ctx); break;
194
0
    case 4: wri->options.colorspace = fz_device_cmyk(ctx); break;
195
0
    }
196
0
  }
197
0
  fz_catch(ctx)
198
0
  {
199
0
    fz_free(ctx, wri);
200
0
    fz_rethrow(ctx);
201
0
  }
202
203
0
  return (fz_document_writer*)wri;
204
0
}