diff options
Diffstat (limited to 'draw/draw_edge.c')
-rw-r--r-- | draw/draw_edge.c | 99 |
1 files changed, 72 insertions, 27 deletions
diff --git a/draw/draw_edge.c b/draw/draw_edge.c index 123c3268..ec1c1ea8 100644 --- a/draw/draw_edge.c +++ b/draw/draw_edge.c @@ -15,15 +15,43 @@ static inline int fz_idiv(int a, int b) * antialiasing is done. If it is undefined to we will leave the antialiasing * accuracy as a run time choice. */ +struct fz_aa_context_s +{ + int hscale; + int vscale; + int scale; + int level; +}; +void fz_new_aa_context(fz_context *ctx) +{ #ifndef AA_BITS + ctx->aa = fz_malloc(ctx, sizeof(*ctx->aa)); + ctx->aa->hscale = 17; + ctx->aa->vscale = 15; + ctx->aa->scale = 256; + ctx->aa->level = 8; + +#define fz_aa_hscale ((ctxaa)->hscale) +#define fz_aa_vscale ((ctxaa)->vscale) +#define fz_aa_scale ((ctxaa)->scale) +#define fz_aa_level ((ctxaa)->level) #define AA_SCALE(x) ((x * fz_aa_scale) >> 8) -static int fz_aa_hscale = 17; -static int fz_aa_vscale = 15; -static int fz_aa_scale = 256; -static int fz_aa_level = 8; -#elif AA_BITS > 6 +#endif +} + +void fz_free_aa_context(fz_context *ctx) +{ +#ifndef AA_BITS + fz_free(ctx, ctx->aa); + ctx->aa = NULL; +#endif +} + +#ifdef AA_BITS + +#if AA_BITS > 6 #define AA_SCALE(x) (x) #define fz_aa_hscale 17 #define fz_aa_vscale 15 @@ -54,18 +82,21 @@ static int fz_aa_level = 8; #define fz_aa_level 0 #endif +#endif int -fz_get_aa_level(void) +fz_get_aa_level(fz_context *ctx) { + fz_aa_context *ctxaa = ctx->aa; return fz_aa_level; } void -fz_set_aa_level(int level) +fz_set_aa_level(fz_context *ctx, int level) { + fz_aa_context *ctxaa = ctx->aa; #ifdef AA_BITS - fz_warn("anti-aliasing was compiled with a fixed precision of %d bits", fz_aa_level); + fz_warn(ctx, "anti-aliasing was compiled with a fixed precision of %d bits", fz_aa_level); #else if (level > 6) { @@ -127,17 +158,19 @@ struct fz_gel_s fz_edge *edges; int acap, alen; fz_edge **active; + fz_context *ctx; }; fz_gel * -fz_new_gel(void) +fz_new_gel(fz_context *ctx) { fz_gel *gel; - gel = fz_malloc(sizeof(fz_gel)); + gel = fz_malloc(ctx, sizeof(fz_gel)); + gel->ctx = ctx; gel->cap = 512; gel->len = 0; - gel->edges = fz_calloc(gel->cap, sizeof(fz_edge)); + gel->edges = fz_malloc_array(ctx, gel->cap, sizeof(fz_edge)); gel->clip.x0 = gel->clip.y0 = BBOX_MAX; gel->clip.x1 = gel->clip.y1 = BBOX_MIN; @@ -147,7 +180,7 @@ fz_new_gel(void) gel->acap = 64; gel->alen = 0; - gel->active = fz_calloc(gel->acap, sizeof(fz_edge*)); + gel->active = fz_malloc_array(ctx, gel->acap, sizeof(fz_edge*)); return gel; } @@ -155,6 +188,8 @@ fz_new_gel(void) void fz_reset_gel(fz_gel *gel, fz_bbox clip) { + fz_aa_context *ctxaa = gel->ctx->aa; + if (fz_is_infinite_rect(clip)) { gel->clip.x0 = gel->clip.y0 = BBOX_MAX; @@ -176,15 +211,16 @@ fz_reset_gel(fz_gel *gel, fz_bbox clip) void fz_free_gel(fz_gel *gel) { - fz_free(gel->active); - fz_free(gel->edges); - fz_free(gel); + fz_free(gel->ctx, gel->active); + fz_free(gel->ctx, gel->edges); + fz_free(gel->ctx, gel); } fz_bbox fz_bound_gel(fz_gel *gel) { fz_bbox bbox; + fz_aa_context *ctxaa = gel->ctx->aa; if (gel->len == 0) return fz_empty_bbox; bbox.x0 = fz_idiv(gel->bbox.x0, fz_aa_hscale); @@ -253,7 +289,7 @@ fz_insert_gel_raw(fz_gel *gel, int x0, int y0, int x1, int y1) if (gel->len + 1 == gel->cap) { gel->cap = gel->cap + 512; - gel->edges = fz_realloc(gel->edges, gel->cap, sizeof(fz_edge)); + gel->edges = fz_resize_array(gel->ctx, gel->edges, gel->cap, sizeof(fz_edge)); } edge = &gel->edges[gel->len++]; @@ -293,6 +329,7 @@ fz_insert_gel(fz_gel *gel, float fx0, float fy0, float fx1, float fy1) { int x0, y0, x1, y1; int d, v; + fz_aa_context *ctxaa = gel->ctx->aa; fx0 = floorf(fx0 * fz_aa_hscale); fx1 = floorf(fx1 * fz_aa_hscale); @@ -443,7 +480,7 @@ insert_active(fz_gel *gel, int y, int *e) while (*e < gel->len && gel->edges[*e].y == y) { if (gel->alen + 1 == gel->acap) { int newcap = gel->acap + 64; - fz_edge **newactive = fz_realloc(gel->active, newcap, sizeof(fz_edge*)); + fz_edge **newactive = fz_resize_array(gel->ctx, gel->active, newcap, sizeof(fz_edge*)); gel->active = newactive; gel->acap = newcap; } @@ -487,7 +524,7 @@ advance_active(fz_gel *gel) * Anti-aliased scan conversion. */ -static inline void add_span_aa(int *list, int x0, int x1, int xofs) +static inline void add_span_aa(fz_aa_context *ctxaa, int *list, int x0, int x1, int xofs) { int x0pix, x0sub; int x1pix, x1sub; @@ -524,12 +561,14 @@ static inline void non_zero_winding_aa(fz_gel *gel, int *list, int xofs) int winding = 0; int x = 0; int i; + fz_aa_context *ctxaa = gel->ctx->aa; + for (i = 0; i < gel->alen; i++) { if (!winding && (winding + gel->active[i]->ydir)) x = gel->active[i]->x; if (winding && !(winding + gel->active[i]->ydir)) - add_span_aa(list, x, gel->active[i]->x, xofs); + add_span_aa(ctxaa, list, x, gel->active[i]->x, xofs); winding += gel->active[i]->ydir; } } @@ -539,17 +578,19 @@ static inline void even_odd_aa(fz_gel *gel, int *list, int xofs) int even = 0; int x = 0; int i; + fz_aa_context *ctxaa = gel->ctx->aa; + for (i = 0; i < gel->alen; i++) { if (!even) x = gel->active[i]->x; else - add_span_aa(list, x, gel->active[i]->x, xofs); + add_span_aa(ctxaa, list, x, gel->active[i]->x, xofs); even = !even; } } -static inline void undelta_aa(unsigned char * restrict out, int * restrict in, int n) +static inline void undelta_aa(fz_aa_context *ctxaa, unsigned char * restrict out, int * restrict in, int n) { int d = 0; while (n--) @@ -578,6 +619,8 @@ fz_scan_convert_aa(fz_gel *gel, int eofill, fz_bbox clip, int *deltas; int y, e; int yd, yc; + fz_context *ctx = gel->ctx; + fz_aa_context *ctxaa = ctx->aa; int xmin = fz_idiv(gel->bbox.x0, fz_aa_hscale); int xmax = fz_idiv(gel->bbox.x1, fz_aa_hscale) + 1; @@ -593,8 +636,8 @@ fz_scan_convert_aa(fz_gel *gel, int eofill, fz_bbox clip, assert(clip.x0 >= xmin); assert(clip.x1 <= xmax); - alphas = fz_malloc(xmax - xmin + 1); - deltas = fz_malloc((xmax - xmin + 1) * sizeof(int)); + alphas = fz_malloc(ctx, xmax - xmin + 1); + deltas = fz_malloc(ctx, (xmax - xmin + 1) * sizeof(int)); memset(deltas, 0, (xmax - xmin + 1) * sizeof(int)); e = 0; @@ -609,7 +652,7 @@ fz_scan_convert_aa(fz_gel *gel, int eofill, fz_bbox clip, { if (yd >= clip.y0 && yd < clip.y1) { - undelta_aa(alphas, deltas, skipx + clipn); + undelta_aa(ctxaa, alphas, deltas, skipx + clipn); blit_aa(dst, xmin + skipx, yd, alphas + skipx, clipn, color); memset(deltas, 0, (skipx + clipn) * sizeof(int)); } @@ -636,12 +679,12 @@ fz_scan_convert_aa(fz_gel *gel, int eofill, fz_bbox clip, if (yd >= clip.y0 && yd < clip.y1) { - undelta_aa(alphas, deltas, skipx + clipn); + undelta_aa(ctxaa, alphas, deltas, skipx + clipn); blit_aa(dst, xmin + skipx, yd, alphas + skipx, clipn, color); } - fz_free(deltas); - fz_free(alphas); + fz_free(ctx, deltas); + fz_free(ctx, alphas); } /* @@ -728,6 +771,8 @@ void fz_scan_convert(fz_gel *gel, int eofill, fz_bbox clip, fz_pixmap *dst, unsigned char *color) { + fz_aa_context *ctxaa = gel->ctx->aa; + if (fz_aa_level > 0) fz_scan_convert_aa(gel, eofill, clip, dst, color); else |