Add alpha blening support to draw image.

This commit is contained in:
Kwabena W. Agyeman 2019-01-02 12:33:15 -05:00
parent 4ba25713b7
commit e7f7a732eb
4 changed files with 13 additions and 7 deletions

View File

@ -401,9 +401,9 @@ static int safe_map_pixel(image_t *dst, image_t *src, int pixel)
}
}
void imlib_draw_image(image_t *img, image_t *other, int x_off, int y_off, float x_scale, float y_scale, image_t *mask)
void imlib_draw_image(image_t *img, image_t *other, int x_off, int y_off, float x_scale, float y_scale, float alpha, image_t *mask)
{
float over_xscale = IM_DIV(1.0, x_scale), over_yscale = IM_DIV(1.0f, y_scale);
float over_xscale = IM_DIV(1.0, x_scale), over_yscale = IM_DIV(1.0f, y_scale), beta = 1 - alpha;
for (int y = 0, yy = fast_roundf(other->h * y_scale); y < yy; y++) {
int other_y = fast_roundf(y * over_yscale);
@ -412,7 +412,9 @@ void imlib_draw_image(image_t *img, image_t *other, int x_off, int y_off, float
int other_x = fast_roundf(x * over_xscale);
if ((!mask) || image_get_mask_pixel(mask, other_x, other_y)) {
imlib_set_pixel(img, x_off + x, y_off + y, safe_map_pixel(img, other, imlib_get_pixel(other, other_x, other_y)));
int pixel = safe_map_pixel(img, other, imlib_get_pixel(other, other_x, other_y));
imlib_set_pixel(img, x_off + x, y_off + y, (alpha == 1) ? pixel :
(pixel * alpha) + (imlib_get_pixel(img, x_off + x, y_off + y) * beta));
}
}
}

View File

@ -1276,7 +1276,7 @@ void imlib_draw_rectangle(image_t *img, int rx, int ry, int rw, int rh, int c, i
void imlib_draw_circle(image_t *img, int cx, int cy, int r, int c, int thickness, bool fill);
void imlib_draw_ellipse(image_t *img, int cx, int cy, int rx, int ry, int rotation, int c, int thickness, bool fill);
void imlib_draw_string(image_t *img, int x_off, int y_off, const char *str, int c, float scale, int x_spacing, int y_spacing, bool mono_space);
void imlib_draw_image(image_t *img, image_t *other, int x_off, int y_off, float x_scale, float y_scale, image_t *mask);
void imlib_draw_image(image_t *img, image_t *other, int x_off, int y_off, float x_scale, float y_scale, float alpha, image_t *mask);
void imlib_flood_fill(image_t *img, int x, int y,
float seed_threshold, float floating_threshold,
int c, bool invert, bool clear_background, image_t *mask);

View File

@ -1442,10 +1442,13 @@ STATIC mp_obj_t py_image_draw_image(uint n_args, const mp_obj_t *args, mp_map_t
float arg_y_scale =
py_helper_keyword_float(n_args, args, offset + 1, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_y_scale), 1.0f);
PY_ASSERT_TRUE_MSG((0.0f <= arg_y_scale), "Error: 0.0 <= y_scale!");
float arg_alpha =
py_helper_keyword_int(n_args, args, offset + 2, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_alpha), 256) / 256.0f;
PY_ASSERT_TRUE_MSG((0 <= arg_alpha) && (arg_alpha <= 1), "Error: 0 <= alpha <= 256!");
image_t *arg_msk =
py_helper_keyword_to_image_mutable_mask(n_args, args, offset + 2, kw_args);
py_helper_keyword_to_image_mutable_mask(n_args, args, offset + 3, kw_args);
imlib_draw_image(arg_img, arg_other, arg_cx, arg_cy, arg_x_scale, arg_y_scale, arg_msk);
imlib_draw_image(arg_img, arg_other, arg_cx, arg_cy, arg_x_scale, arg_y_scale, arg_alpha, arg_msk);
return args[0];
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_draw_image_obj, 3, py_image_draw_image);

View File

@ -429,6 +429,7 @@ Q(draw_arrow)
Q(draw_image)
Q(x_scale)
Q(y_scale)
Q(alpha)
Q(mask)
// Draw Keypoints
@ -570,7 +571,7 @@ Q(difference)
// Blend
Q(blend)
Q(alpha)
// duplicate Q(alpha)
// duplicate Q(mask)
// Histogram Equalization