/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 | 24 | { |
32 | 24 | #ifndef AA_BITS |
33 | 24 | ctx->aa.hscale = 17; |
34 | 24 | ctx->aa.vscale = 15; |
35 | 24 | ctx->aa.scale = 256; |
36 | 24 | ctx->aa.bits = 8; |
37 | 24 | ctx->aa.text_bits = 8; |
38 | 24 | #endif |
39 | 24 | } |
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 | 0 | { |
62 | 0 | return fz_rasterizer_aa_bits(ras); |
63 | 0 | } |
64 | | |
65 | | int |
66 | | fz_rasterizer_text_aa_level(fz_rasterizer *ras) |
67 | 219 | { |
68 | 219 | return fz_rasterizer_aa_text_bits(ras); |
69 | 219 | } |
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 | 0 | { |
188 | 0 | return ras->aa.min_line_width; |
189 | 0 | } |
190 | | |
191 | | fz_irect |
192 | | fz_bound_rasterizer(fz_context *ctx, const fz_rasterizer *rast) |
193 | 63 | { |
194 | 63 | fz_irect bbox; |
195 | 63 | const int hscale = fz_rasterizer_aa_hscale(rast); |
196 | 63 | const int vscale = fz_rasterizer_aa_vscale(rast); |
197 | | |
198 | 63 | if (rast->bbox.x1 < rast->bbox.x0 || rast->bbox.y1 < rast->bbox.y0) |
199 | 0 | { |
200 | 0 | bbox = fz_empty_irect; |
201 | 0 | } |
202 | 63 | else |
203 | 63 | { |
204 | 63 | bbox.x0 = fz_idiv(rast->bbox.x0, hscale); |
205 | 63 | bbox.y0 = fz_idiv(rast->bbox.y0, vscale); |
206 | 63 | bbox.x1 = fz_idiv_up(rast->bbox.x1, hscale); |
207 | 63 | bbox.y1 = fz_idiv_up(rast->bbox.y1, vscale); |
208 | 63 | } |
209 | 63 | return bbox; |
210 | 63 | } |
211 | | |
212 | | fz_rect fz_scissor_rasterizer(fz_context *ctx, const fz_rasterizer *rast) |
213 | 0 | { |
214 | 0 | fz_rect r; |
215 | 0 | const int hscale = fz_rasterizer_aa_hscale(rast); |
216 | 0 | const int vscale = fz_rasterizer_aa_vscale(rast); |
217 | |
|
218 | 0 | r.x0 = ((float)rast->clip.x0) / hscale; |
219 | 0 | r.y0 = ((float)rast->clip.y0) / vscale; |
220 | 0 | r.x1 = ((float)rast->clip.x1) / hscale; |
221 | 0 | r.y1 = ((float)rast->clip.y1) / vscale; |
222 | |
|
223 | 0 | return r; |
224 | 0 | } |
225 | | |
226 | | static fz_irect fz_clip_rasterizer(fz_context *ctx, const fz_rasterizer *rast) |
227 | 3 | { |
228 | 3 | fz_irect r; |
229 | 3 | const int hscale = fz_rasterizer_aa_hscale(rast); |
230 | 3 | const int vscale = fz_rasterizer_aa_vscale(rast); |
231 | | |
232 | 3 | r.x0 = fz_idiv(rast->clip.x0, hscale); |
233 | 3 | r.y0 = fz_idiv(rast->clip.y0, vscale); |
234 | 3 | r.x1 = fz_idiv_up(rast->clip.x1, hscale); |
235 | 3 | r.y1 = fz_idiv_up(rast->clip.y1, vscale); |
236 | | |
237 | 3 | return r; |
238 | 3 | } |
239 | | |
240 | | int fz_reset_rasterizer(fz_context *ctx, fz_rasterizer *rast, fz_irect clip) |
241 | 30 | { |
242 | 30 | const int hscale = fz_rasterizer_aa_hscale(rast); |
243 | 30 | const int vscale = fz_rasterizer_aa_vscale(rast); |
244 | | |
245 | 30 | 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 | 30 | else { |
251 | 30 | rast->clip.x0 = clip.x0 * hscale; |
252 | 30 | rast->clip.x1 = clip.x1 * hscale; |
253 | 30 | rast->clip.y0 = clip.y0 * vscale; |
254 | 30 | rast->clip.y1 = clip.y1 * vscale; |
255 | 30 | } |
256 | | |
257 | 30 | rast->bbox.x0 = rast->bbox.y0 = BBOX_MAX; |
258 | 30 | rast->bbox.x1 = rast->bbox.y1 = BBOX_MIN; |
259 | | |
260 | 30 | if (rast->fns.reset) |
261 | 30 | return rast->fns.reset(ctx, rast); |
262 | 0 | return 0; |
263 | 30 | } |
264 | | |
265 | | void *fz_new_rasterizer_of_size(fz_context *ctx, int size, const fz_rasterizer_fns *fns) |
266 | 21 | { |
267 | 21 | fz_rasterizer *rast = fz_calloc(ctx, 1, size); |
268 | | |
269 | 21 | rast->fns = *fns; |
270 | 21 | rast->clip.x0 = rast->clip.y0 = BBOX_MIN; |
271 | 21 | rast->clip.x1 = rast->clip.y1 = BBOX_MAX; |
272 | | |
273 | 21 | rast->bbox.x0 = rast->bbox.y0 = BBOX_MAX; |
274 | 21 | rast->bbox.x1 = rast->bbox.y1 = BBOX_MIN; |
275 | | |
276 | 21 | return rast; |
277 | 21 | } |
278 | | |
279 | | fz_rasterizer *fz_new_rasterizer(fz_context *ctx, const fz_aa_context *aa) |
280 | 21 | { |
281 | 21 | fz_rasterizer *r; |
282 | 21 | int bits; |
283 | | |
284 | | #ifdef AA_BITS |
285 | | bits = AA_BITS; |
286 | | #else |
287 | 21 | if (aa == NULL) |
288 | 21 | aa = &ctx->aa; |
289 | 21 | bits = aa->bits; |
290 | 21 | #endif |
291 | 21 | if (bits == 10) |
292 | 0 | r = fz_new_edgebuffer(ctx, FZ_EDGEBUFFER_ANY_PART_OF_PIXEL); |
293 | 21 | else if (bits == 9) |
294 | 0 | r = fz_new_edgebuffer(ctx, FZ_EDGEBUFFER_CENTER_OF_PIXEL); |
295 | 21 | else |
296 | 21 | r = fz_new_gel(ctx); |
297 | 21 | #ifndef AA_BITS |
298 | 21 | r->aa = *aa; |
299 | 21 | #endif |
300 | | |
301 | 21 | return r; |
302 | 21 | } |
303 | | |
304 | | void fz_convert_rasterizer(fz_context *ctx, fz_rasterizer *r, int eofill, fz_pixmap *pix, unsigned char *colorbv, fz_overprint *eop) |
305 | 3 | { |
306 | 3 | fz_irect clip = fz_bound_rasterizer(ctx, r); |
307 | 3 | clip = fz_intersect_irect(clip, fz_pixmap_bbox_no_ctx(pix)); |
308 | 3 | clip = fz_intersect_irect(clip, fz_clip_rasterizer(ctx, r)); |
309 | 3 | if (!fz_is_empty_irect(clip)) |
310 | 3 | r->fns.convert(ctx, r, eofill, &clip, pix, colorbv, eop); |
311 | 3 | } |