/src/mupdf/source/fitz/draw-rasterize.c
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (C) 2004-2021 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 "draw-imp.h" |
26 | | #include "pixmap-imp.h" |
27 | | |
28 | | #include <string.h> |
29 | | |
30 | | void fz_init_aa_context(fz_context *ctx) |
31 | 13.4k | { |
32 | 13.4k | #ifndef AA_BITS |
33 | 13.4k | ctx->aa.hscale = 17; |
34 | 13.4k | ctx->aa.vscale = 15; |
35 | 13.4k | ctx->aa.scale = 256; |
36 | 13.4k | ctx->aa.bits = 8; |
37 | 13.4k | ctx->aa.text_bits = 8; |
38 | 13.4k | #endif |
39 | 13.4k | } |
40 | | |
41 | | int |
42 | | fz_aa_level(fz_context *ctx) |
43 | 0 | { |
44 | 0 | return fz_aa_bits; |
45 | 0 | } |
46 | | |
47 | | int |
48 | | fz_graphics_aa_level(fz_context *ctx) |
49 | 0 | { |
50 | 0 | return fz_aa_bits; |
51 | 0 | } |
52 | | |
53 | | int |
54 | | fz_text_aa_level(fz_context *ctx) |
55 | 0 | { |
56 | 0 | return fz_aa_text_bits; |
57 | 0 | } |
58 | | |
59 | | int |
60 | | fz_rasterizer_graphics_aa_level(fz_rasterizer *ras) |
61 | 270k | { |
62 | 270k | return fz_rasterizer_aa_bits(ras); |
63 | 270k | } |
64 | | |
65 | | int |
66 | | fz_rasterizer_text_aa_level(fz_rasterizer *ras) |
67 | 5.75M | { |
68 | 5.75M | return fz_rasterizer_aa_text_bits(ras); |
69 | 5.75M | } |
70 | | |
71 | | void |
72 | | fz_set_rasterizer_text_aa_level(fz_context *ctx, fz_aa_context *aa, int level) |
73 | 0 | { |
74 | | #ifdef AA_BITS |
75 | | if (level != fz_aa_bits) |
76 | | { |
77 | | if (fz_aa_bits == 10) |
78 | | fz_warn(ctx, "Only the Any-part-of-a-pixel rasterizer was compiled in"); |
79 | | else if (fz_aa_bits == 9) |
80 | | fz_warn(ctx, "Only the Centre-of-a-pixel rasterizer was compiled in"); |
81 | | else |
82 | | fz_warn(ctx, "Only the %d bit anti-aliasing rasterizer was compiled in", fz_aa_bits); |
83 | | } |
84 | | #else |
85 | 0 | if (level > 8) |
86 | 0 | aa->text_bits = 0; |
87 | 0 | else if (level > 6) |
88 | 0 | aa->text_bits = 8; |
89 | 0 | else if (level > 4) |
90 | 0 | aa->text_bits = 6; |
91 | 0 | else if (level > 2) |
92 | 0 | aa->text_bits = 4; |
93 | 0 | else if (level > 0) |
94 | 0 | aa->text_bits = 2; |
95 | 0 | else |
96 | 0 | aa->text_bits = 0; |
97 | 0 | #endif |
98 | 0 | } |
99 | | |
100 | | void |
101 | | fz_set_rasterizer_graphics_aa_level(fz_context *ctx, fz_aa_context *aa, int level) |
102 | 0 | { |
103 | | #ifdef AA_BITS |
104 | | if (level != fz_aa_bits) |
105 | | { |
106 | | if (fz_aa_bits == 10) |
107 | | fz_warn(ctx, "Only the Any-part-of-a-pixel rasterizer was compiled in"); |
108 | | else if (fz_aa_bits == 9) |
109 | | fz_warn(ctx, "Only the Centre-of-a-pixel rasterizer was compiled in"); |
110 | | else |
111 | | fz_warn(ctx, "Only the %d bit anti-aliasing rasterizer was compiled in", fz_aa_bits); |
112 | | } |
113 | | #else |
114 | 0 | if (level == 9 || level == 10) |
115 | 0 | { |
116 | 0 | aa->hscale = 1; |
117 | 0 | aa->vscale = 1; |
118 | 0 | aa->bits = level; |
119 | 0 | } |
120 | 0 | else if (level > 6) |
121 | 0 | { |
122 | 0 | aa->hscale = 17; |
123 | 0 | aa->vscale = 15; |
124 | 0 | aa->bits = 8; |
125 | 0 | } |
126 | 0 | else if (level > 4) |
127 | 0 | { |
128 | 0 | aa->hscale = 8; |
129 | 0 | aa->vscale = 8; |
130 | 0 | aa->bits = 6; |
131 | 0 | } |
132 | 0 | else if (level > 2) |
133 | 0 | { |
134 | 0 | aa->hscale = 5; |
135 | 0 | aa->vscale = 3; |
136 | 0 | aa->bits = 4; |
137 | 0 | } |
138 | 0 | else if (level > 0) |
139 | 0 | { |
140 | 0 | aa->hscale = 2; |
141 | 0 | aa->vscale = 2; |
142 | 0 | aa->bits = 2; |
143 | 0 | } |
144 | 0 | else |
145 | 0 | { |
146 | 0 | aa->hscale = 1; |
147 | 0 | aa->vscale = 1; |
148 | 0 | aa->bits = 0; |
149 | 0 | } |
150 | 0 | aa->scale = 0xFF00 / (aa->hscale * aa->vscale); |
151 | 0 | #endif |
152 | 0 | } |
153 | | |
154 | | void |
155 | | fz_set_aa_level(fz_context *ctx, int level) |
156 | 0 | { |
157 | 0 | fz_set_rasterizer_graphics_aa_level(ctx, &ctx->aa, level); |
158 | 0 | fz_set_rasterizer_text_aa_level(ctx, &ctx->aa, level); |
159 | 0 | } |
160 | | |
161 | | void |
162 | | fz_set_text_aa_level(fz_context *ctx, int level) |
163 | 0 | { |
164 | 0 | fz_set_rasterizer_text_aa_level(ctx, &ctx->aa, level); |
165 | 0 | } |
166 | | |
167 | | void |
168 | | fz_set_graphics_aa_level(fz_context *ctx, int level) |
169 | 0 | { |
170 | 0 | fz_set_rasterizer_graphics_aa_level(ctx, &ctx->aa, level); |
171 | 0 | } |
172 | | |
173 | | void |
174 | | fz_set_graphics_min_line_width(fz_context *ctx, float min_line_width) |
175 | 0 | { |
176 | 0 | ctx->aa.min_line_width = min_line_width; |
177 | 0 | } |
178 | | |
179 | | float |
180 | | fz_graphics_min_line_width(fz_context *ctx) |
181 | 0 | { |
182 | 0 | return ctx->aa.min_line_width; |
183 | 0 | } |
184 | | |
185 | | float |
186 | | fz_rasterizer_graphics_min_line_width(fz_rasterizer *ras) |
187 | 270k | { |
188 | 270k | return ras->aa.min_line_width; |
189 | 270k | } |
190 | | |
191 | | fz_irect |
192 | | fz_bound_rasterizer(fz_context *ctx, const fz_rasterizer *rast) |
193 | 1.47M | { |
194 | 1.47M | fz_irect bbox; |
195 | 1.47M | const int hscale = fz_rasterizer_aa_hscale(rast); |
196 | 1.47M | const int vscale = fz_rasterizer_aa_vscale(rast); |
197 | | |
198 | 1.47M | if (rast->bbox.x1 < rast->bbox.x0 || rast->bbox.y1 < rast->bbox.y0) |
199 | 237k | { |
200 | 237k | bbox = fz_empty_irect; |
201 | 237k | } |
202 | 1.23M | else |
203 | 1.23M | { |
204 | 1.23M | bbox.x0 = fz_idiv(rast->bbox.x0, hscale); |
205 | 1.23M | bbox.y0 = fz_idiv(rast->bbox.y0, vscale); |
206 | 1.23M | bbox.x1 = fz_idiv_up(rast->bbox.x1, hscale); |
207 | 1.23M | bbox.y1 = fz_idiv_up(rast->bbox.y1, vscale); |
208 | 1.23M | } |
209 | 1.47M | return bbox; |
210 | 1.47M | } |
211 | | |
212 | | fz_rect fz_scissor_rasterizer(fz_context *ctx, const fz_rasterizer *rast) |
213 | 1.73k | { |
214 | 1.73k | fz_rect r; |
215 | 1.73k | const int hscale = fz_rasterizer_aa_hscale(rast); |
216 | 1.73k | const int vscale = fz_rasterizer_aa_vscale(rast); |
217 | | |
218 | 1.73k | r.x0 = ((float)rast->clip.x0) / hscale; |
219 | 1.73k | r.y0 = ((float)rast->clip.y0) / vscale; |
220 | 1.73k | r.x1 = ((float)rast->clip.x1) / hscale; |
221 | 1.73k | r.y1 = ((float)rast->clip.y1) / vscale; |
222 | | |
223 | 1.73k | return r; |
224 | 1.73k | } |
225 | | |
226 | | static fz_irect fz_clip_rasterizer(fz_context *ctx, const fz_rasterizer *rast) |
227 | 358k | { |
228 | 358k | fz_irect r; |
229 | 358k | const int hscale = fz_rasterizer_aa_hscale(rast); |
230 | 358k | const int vscale = fz_rasterizer_aa_vscale(rast); |
231 | | |
232 | 358k | r.x0 = fz_idiv(rast->clip.x0, hscale); |
233 | 358k | r.y0 = fz_idiv(rast->clip.y0, vscale); |
234 | 358k | r.x1 = fz_idiv_up(rast->clip.x1, hscale); |
235 | 358k | r.y1 = fz_idiv_up(rast->clip.y1, vscale); |
236 | | |
237 | 358k | return r; |
238 | 358k | } |
239 | | |
240 | | int fz_reset_rasterizer(fz_context *ctx, fz_rasterizer *rast, fz_irect clip) |
241 | 685k | { |
242 | 685k | const int hscale = fz_rasterizer_aa_hscale(rast); |
243 | 685k | const int vscale = fz_rasterizer_aa_vscale(rast); |
244 | | |
245 | 685k | if (fz_is_infinite_irect(clip)) |
246 | 0 | { |
247 | 0 | rast->clip.x0 = rast->clip.y0 = BBOX_MIN; |
248 | 0 | rast->clip.x1 = rast->clip.y1 = BBOX_MAX; |
249 | 0 | } |
250 | 685k | else { |
251 | 685k | rast->clip.x0 = clip.x0 * hscale; |
252 | 685k | rast->clip.x1 = clip.x1 * hscale; |
253 | 685k | rast->clip.y0 = clip.y0 * vscale; |
254 | 685k | rast->clip.y1 = clip.y1 * vscale; |
255 | 685k | } |
256 | | |
257 | 685k | rast->bbox.x0 = rast->bbox.y0 = BBOX_MAX; |
258 | 685k | rast->bbox.x1 = rast->bbox.y1 = BBOX_MIN; |
259 | | |
260 | 685k | if (rast->fns.reset) |
261 | 685k | return rast->fns.reset(ctx, rast); |
262 | 0 | return 0; |
263 | 685k | } |
264 | | |
265 | | void *fz_new_rasterizer_of_size(fz_context *ctx, int size, const fz_rasterizer_fns *fns) |
266 | 16.9k | { |
267 | 16.9k | fz_rasterizer *rast = fz_calloc(ctx, 1, size); |
268 | | |
269 | 16.9k | rast->fns = *fns; |
270 | 16.9k | rast->clip.x0 = rast->clip.y0 = BBOX_MIN; |
271 | 16.9k | rast->clip.x1 = rast->clip.y1 = BBOX_MAX; |
272 | | |
273 | 16.9k | rast->bbox.x0 = rast->bbox.y0 = BBOX_MAX; |
274 | 16.9k | rast->bbox.x1 = rast->bbox.y1 = BBOX_MIN; |
275 | | |
276 | 16.9k | return rast; |
277 | 16.9k | } |
278 | | |
279 | | fz_rasterizer *fz_new_rasterizer(fz_context *ctx, const fz_aa_context *aa) |
280 | 16.9k | { |
281 | 16.9k | fz_rasterizer *r; |
282 | 16.9k | int bits; |
283 | | |
284 | | #ifdef AA_BITS |
285 | | bits = AA_BITS; |
286 | | #else |
287 | 16.9k | if (aa == NULL) |
288 | 16.9k | aa = &ctx->aa; |
289 | 16.9k | bits = aa->bits; |
290 | 16.9k | #endif |
291 | 16.9k | if (bits == 10) |
292 | 0 | r = fz_new_edgebuffer(ctx, FZ_EDGEBUFFER_ANY_PART_OF_PIXEL); |
293 | 16.9k | else if (bits == 9) |
294 | 0 | r = fz_new_edgebuffer(ctx, FZ_EDGEBUFFER_CENTER_OF_PIXEL); |
295 | 16.9k | else |
296 | 16.9k | r = fz_new_gel(ctx); |
297 | 16.9k | #ifndef AA_BITS |
298 | 16.9k | r->aa = *aa; |
299 | 16.9k | #endif |
300 | | |
301 | 16.9k | return r; |
302 | 16.9k | } |
303 | | |
304 | | void fz_convert_rasterizer(fz_context *ctx, fz_rasterizer *r, int eofill, fz_pixmap *pix, unsigned char *colorbv, fz_overprint *eop) |
305 | 358k | { |
306 | 358k | fz_irect clip = fz_bound_rasterizer(ctx, r); |
307 | 358k | clip = fz_intersect_irect(clip, fz_pixmap_bbox_no_ctx(pix)); |
308 | 358k | clip = fz_intersect_irect(clip, fz_clip_rasterizer(ctx, r)); |
309 | 358k | if (!fz_is_empty_irect(clip)) |
310 | 358k | r->fns.convert(ctx, r, eofill, &clip, pix, colorbv, eop); |
311 | 358k | } |