From ad6510b9c78f5c79f7492b608912dc5281e15e47 Mon Sep 17 00:00:00 2001 From: "Dr.Smile" Date: Sun, 17 Sep 2017 03:39:11 +0300 Subject: Consolidate bounding box operations --- libass/ass_bitmap.c | 10 +++++----- libass/ass_drawing.c | 22 ++++------------------ libass/ass_outline.h | 17 +++++++++++++++++ libass/ass_rasterizer.c | 27 +++++++++++++-------------- libass/ass_rasterizer.h | 2 +- libass/ass_render.c | 13 +++---------- 6 files changed, 43 insertions(+), 48 deletions(-) (limited to 'libass') diff --git a/libass/ass_bitmap.c b/libass/ass_bitmap.c index 7d991bf..cc40ed6 100644 --- a/libass/ass_bitmap.c +++ b/libass/ass_bitmap.c @@ -202,13 +202,13 @@ Bitmap *outline_to_bitmap(ASS_Renderer *render_priv, if (bord < 0 || bord > INT_MAX / 2) return NULL; - if (rst->x_max > INT_MAX - 63 || rst->y_max > INT_MAX - 63) + if (rst->bbox.x_max > INT_MAX - 63 || rst->bbox.y_max > INT_MAX - 63) return NULL; - int x_min = rst->x_min >> 6; - int y_min = rst->y_min >> 6; - int x_max = (rst->x_max + 63) >> 6; - int y_max = (rst->y_max + 63) >> 6; + int x_min = rst->bbox.x_min >> 6; + int y_min = rst->bbox.y_min >> 6; + int x_max = (rst->bbox.x_max + 63) >> 6; + int y_max = (rst->bbox.y_max + 63) >> 6; int w = x_max - x_min; int h = y_max - y_min; diff --git a/libass/ass_drawing.c b/libass/ass_drawing.c index 64f7b29..5918469 100644 --- a/libass/ass_drawing.c +++ b/libass/ass_drawing.c @@ -173,29 +173,16 @@ static void drawing_free_tokens(ASS_DrawingToken *token) } } -/* - * \brief Update drawing cbox - */ -static inline void update_cbox(ASS_Drawing *drawing, ASS_Vector *point) -{ - ASS_Rect *box = &drawing->cbox; - - box->x_min = FFMIN(box->x_min, point->x); - box->x_max = FFMAX(box->x_max, point->x); - box->y_min = FFMIN(box->y_min, point->y); - box->y_max = FFMAX(box->y_max, point->y); -} - /* * \brief Translate and scale a point coordinate according to baseline * offset and scale. */ static inline void translate_point(ASS_Drawing *drawing, ASS_Vector *point) { - point->x = drawing->point_scale_x * point->x; - point->y = drawing->point_scale_y * point->y; + point->x = lrint(drawing->point_scale_x * point->x); + point->y = lrint(drawing->point_scale_y * point->y); - update_cbox(drawing, point); + rectangle_update(&drawing->cbox, point->x, point->y, point->x, point->y); } /* @@ -244,8 +231,7 @@ ASS_Drawing *ass_drawing_new(ASS_Library *lib) ASS_Drawing *drawing = calloc(1, sizeof(*drawing)); if (!drawing) return NULL; - drawing->cbox.x_min = drawing->cbox.y_min = INT32_MAX; - drawing->cbox.x_max = drawing->cbox.y_max = INT32_MIN; + rectangle_reset(&drawing->cbox); drawing->library = lib; drawing->scale_x = 1.; drawing->scale_y = 1.; diff --git a/libass/ass_outline.h b/libass/ass_outline.h index c6989f7..6be0211 100644 --- a/libass/ass_outline.h +++ b/libass/ass_outline.h @@ -24,6 +24,8 @@ #include #include +#include "ass_utils.h" + typedef struct { int32_t x, y; @@ -41,6 +43,21 @@ typedef struct { double x_min, y_min, x_max, y_max; } ASS_DRect; +static inline void rectangle_reset(ASS_Rect *rect) +{ + rect->x_min = rect->y_min = INT32_MAX; + rect->x_max = rect->y_max = INT32_MIN; +} + +static inline void rectangle_update(ASS_Rect *rect, + int32_t x_min, int32_t y_min, int32_t x_max, int32_t y_max) +{ + rect->x_min = FFMIN(rect->x_min, x_min); + rect->y_min = FFMIN(rect->y_min, y_min); + rect->x_max = FFMAX(rect->x_max, x_max); + rect->y_max = FFMAX(rect->y_max, y_max); +} + /* * Outline represented with array of points and array of segments. * Segment here is spline of order 1 (line), 2 (quadratic) or 3 (cubic). diff --git a/libass/ass_rasterizer.c b/libass/ass_rasterizer.c index 1af4509..df11753 100644 --- a/libass/ass_rasterizer.c +++ b/libass/ass_rasterizer.c @@ -264,8 +264,7 @@ bool rasterizer_set_outline(RasterizerData *rst, const ASS_Outline *path, bool extra) { if (!extra) { - rst->x_min = rst->y_min = INT32_MAX; - rst->x_max = rst->y_max = INT32_MIN; + rectangle_reset(&rst->bbox); rst->n_first = 0; } rst->size[0] = rst->n_first; @@ -318,10 +317,10 @@ bool rasterizer_set_outline(RasterizerData *rst, assert(start == cur && cur == path->points + path->n_points); for (size_t k = rst->n_first; k < rst->size[0]; k++) { - rst->x_min = FFMIN(rst->x_min, rst->linebuf[0][k].x_min); - rst->x_max = FFMAX(rst->x_max, rst->linebuf[0][k].x_max); - rst->y_min = FFMIN(rst->y_min, rst->linebuf[0][k].y_min); - rst->y_max = FFMAX(rst->y_max, rst->linebuf[0][k].y_max); + struct segment *line = &rst->linebuf[0][k]; + rectangle_update(&rst->bbox, + line->x_min, line->y_min, + line->x_max, line->y_max); } if (!extra) rst->n_first = rst->size[0]; @@ -739,10 +738,10 @@ bool rasterizer_fill(const BitmapEngine *engine, RasterizerData *rst, line->y_max -= y0; line->c -= line->a * (int64_t) x0 + line->b * (int64_t) y0; } - rst->x_min -= x0; - rst->x_max -= x0; - rst->y_min -= y0; - rst->y_max -= y0; + rst->bbox.x_min -= x0; + rst->bbox.x_max -= x0; + rst->bbox.y_min -= y0; + rst->bbox.y_max -= y0; if (!check_capacity(rst, 1, rst->size[0])) return false; @@ -753,27 +752,27 @@ bool rasterizer_fill(const BitmapEngine *engine, RasterizerData *rst, int32_t size_x = (int32_t) width << 6; int32_t size_y = (int32_t) height << 6; - if (rst->x_max >= size_x) { + if (rst->bbox.x_max >= size_x) { polyline_split_horz(rst->linebuf[0], n_lines, rst->linebuf[0], n_lines, rst->linebuf[1], n_unused, winding, size_x); winding[0] = winding[1] = 0; } - if (rst->y_max >= size_y) { + if (rst->bbox.y_max >= size_y) { polyline_split_vert(rst->linebuf[0], n_lines, rst->linebuf[0], n_lines, rst->linebuf[1], n_unused, winding, size_y); winding[0] = winding[1] = 0; } - if (rst->x_min <= 0) { + if (rst->bbox.x_min <= 0) { polyline_split_horz(rst->linebuf[0], n_lines, rst->linebuf[1], n_unused, rst->linebuf[0], n_lines, winding, 0); } - if (rst->y_min <= 0) { + if (rst->bbox.y_min <= 0) { polyline_split_vert(rst->linebuf[0], n_lines, rst->linebuf[1], n_unused, rst->linebuf[0], n_lines, diff --git a/libass/ass_rasterizer.h b/libass/ass_rasterizer.h index 1c2339b..f99205b 100644 --- a/libass/ass_rasterizer.h +++ b/libass/ass_rasterizer.h @@ -46,7 +46,7 @@ typedef struct { int outline_error; // acceptable error (in 1/64 pixel units) // usable after rasterizer_set_outline - int32_t x_min, x_max, y_min, y_max; + ASS_Rect bbox; // internal buffers struct segment *linebuf[2]; diff --git a/libass/ass_render.c b/libass/ass_render.c index d60c93a..be76f76 100644 --- a/libass/ass_render.c +++ b/libass/ass_render.c @@ -2034,18 +2034,11 @@ static void calculate_rotation_params(ASS_Renderer *render_priv, ASS_DRect *bbox } -static inline void rectangle_reset(ASS_Rect *rect) -{ - rect->x_min = rect->y_min = INT32_MAX; - rect->x_max = rect->y_max = INT32_MIN; -} - static inline void rectangle_combine(ASS_Rect *rect, const Bitmap *bm, int x, int y) { - rect->x_min = FFMIN(rect->x_min, x + bm->left); - rect->y_min = FFMIN(rect->y_min, y + bm->top); - rect->x_max = FFMAX(rect->x_max, x + bm->left + bm->w); - rect->y_max = FFMAX(rect->y_max, y + bm->top + bm->h); + x += bm->left; + y += bm->top; + rectangle_update(rect, x, y, x + bm->w, y + bm->h); } // Convert glyphs to bitmaps, combine them, apply blur, generate shadows. -- cgit v1.2.3