comparison mupdf-source/source/fitz/compressed-buffer.c @ 3:2c135c81b16c

MERGE: upstream PyMuPDF 1.26.4 with MuPDF 1.26.7
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:44:09 +0200
parents b50eed0cc0ef
children
comparison
equal deleted inserted replaced
0:6015a75abc2d 3:2c135c81b16c
1 // Copyright (C) 2004-2025 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 /* This code needs to be kept out of stm_buffer.c to avoid it being
26 * pulled into cmapdump.c */
27
28 fz_compressed_buffer *
29 fz_keep_compressed_buffer(fz_context *ctx, fz_compressed_buffer *cbuf)
30 {
31 return (fz_compressed_buffer *)fz_keep_imp(ctx, cbuf, &cbuf->refs);
32 }
33
34 void
35 fz_drop_compressed_buffer(fz_context *ctx, fz_compressed_buffer *buf)
36 {
37 if (fz_drop_imp(ctx, buf, &buf->refs))
38 {
39 if (buf->params.type == FZ_IMAGE_JBIG2)
40 fz_drop_jbig2_globals(ctx, buf->params.u.jbig2.globals);
41 fz_drop_buffer(ctx, buf->buffer);
42 fz_free(ctx, buf);
43 }
44 }
45
46 fz_compressed_buffer *
47 fz_new_compressed_buffer(fz_context *ctx)
48 {
49 fz_compressed_buffer *cbuf = fz_malloc_struct(ctx, fz_compressed_buffer);
50
51 cbuf->refs = 1;
52
53 return cbuf;
54 }
55
56 fz_stream *
57 fz_open_image_decomp_stream_from_buffer(fz_context *ctx, fz_compressed_buffer *buffer, int *l2factor)
58 {
59 fz_stream *head, *tail;
60
61 tail = fz_open_buffer(ctx, buffer->buffer);
62 fz_try(ctx)
63 head = fz_open_image_decomp_stream(ctx, tail, &buffer->params, l2factor);
64 fz_always(ctx)
65 fz_drop_stream(ctx, tail);
66 fz_catch(ctx)
67 fz_rethrow(ctx);
68 return head;
69 }
70
71 fz_stream *
72 fz_open_image_decomp_stream(fz_context *ctx, fz_stream *tail, fz_compression_params *params, int *l2factor)
73 {
74 fz_stream *head = NULL, *body = NULL;
75 int our_l2factor = 0;
76
77 fz_var(body);
78
79 fz_try(ctx)
80 {
81 switch (params->type)
82 {
83 default:
84 head = fz_keep_stream(ctx, tail);
85 break;
86
87 case FZ_IMAGE_FAX:
88 head = fz_open_faxd(ctx, tail,
89 params->u.fax.k,
90 params->u.fax.end_of_line,
91 params->u.fax.encoded_byte_align,
92 params->u.fax.columns,
93 params->u.fax.rows,
94 params->u.fax.end_of_block,
95 params->u.fax.black_is_1);
96 break;
97
98 case FZ_IMAGE_JPEG:
99 if (l2factor)
100 {
101 our_l2factor = *l2factor;
102 if (our_l2factor > 3)
103 our_l2factor = 3;
104 *l2factor -= our_l2factor;
105 }
106 head = fz_open_dctd(ctx, tail, params->u.jpeg.color_transform, params->u.jpeg.invert_cmyk, our_l2factor, NULL);
107 break;
108
109 case FZ_IMAGE_JBIG2:
110 head = fz_open_jbig2d(ctx, tail, params->u.jbig2.globals, params->u.jbig2.embedded);
111 break;
112
113 case FZ_IMAGE_RLD:
114 head = fz_open_rld(ctx, tail);
115 break;
116
117 case FZ_IMAGE_FLATE:
118 head = fz_open_flated(ctx, tail, 15);
119 if (params->u.flate.predictor > 1)
120 {
121 body = head;
122 head = fz_open_predict(ctx, body,
123 params->u.flate.predictor,
124 params->u.flate.columns,
125 params->u.flate.colors,
126 params->u.flate.bpc);
127 }
128 break;
129
130 case FZ_IMAGE_BROTLI:
131 head = fz_open_brotlid(ctx, tail);
132 if (params->u.brotli.predictor > 1)
133 {
134 body = head;
135 head = fz_open_predict(ctx, body,
136 params->u.brotli.predictor,
137 params->u.brotli.columns,
138 params->u.brotli.colors,
139 params->u.brotli.bpc);
140 }
141 break;
142
143 case FZ_IMAGE_LZW:
144 head = fz_open_lzwd(ctx, tail, params->u.lzw.early_change, 9, 0, 0);
145 if (params->u.flate.predictor > 1)
146 {
147 body = head;
148 head = fz_open_predict(ctx, body,
149 params->u.lzw.predictor,
150 params->u.lzw.columns,
151 params->u.lzw.colors,
152 params->u.lzw.bpc);
153 }
154 break;
155 }
156 }
157 fz_always(ctx)
158 fz_drop_stream(ctx, body);
159 fz_catch(ctx)
160 fz_rethrow(ctx);
161
162 return head;
163 }
164
165 fz_stream *
166 fz_open_compressed_buffer(fz_context *ctx, fz_compressed_buffer *buffer)
167 {
168 return fz_open_image_decomp_stream_from_buffer(ctx, buffer, NULL);
169 }
170
171 size_t
172 fz_compressed_buffer_size(fz_compressed_buffer *buffer)
173 {
174 if (!buffer)
175 return 0;
176 if (buffer->buffer)
177 return (size_t)buffer->buffer->cap + sizeof(*buffer);
178 return sizeof(*buffer);
179 }