diff --git a/src/gfx/blend_image.cpp b/src/gfx/blend_image.cpp index 3b7623f3..28bc80fa 100644 --- a/src/gfx/blend_image.cpp +++ b/src/gfx/blend_image.cpp @@ -87,6 +87,24 @@ void set_alpha(Image& img, const Image& img_alpha) { } } +void set_alpha(Image& img, Byte* al, const wxSize& alpha_size) { + if (img.GetWidth() != alpha_size.GetWidth() || img.GetHeight() != alpha_size.GetHeight()) { + throw Error(_("Image must have same size as mask")); + } + if (!img.HasAlpha()) { + // copy + img.InitAlpha(); + memcpy(img.GetAlpha(), al, img.GetWidth() * img.GetHeight()); + } else{ + // merge + Byte *im = img.GetAlpha(); + size_t size = img.GetWidth() * img.GetHeight(); + for (size_t i = 0 ; i < size ; ++i) { + im[i] = (im[i] * al[i]) / 255; + } + } +} + void set_alpha(Image& img, double alpha) { Byte b_alpha = alpha * 255; if (!img.HasAlpha()) { diff --git a/src/gfx/gfx.hpp b/src/gfx/gfx.hpp index 9600e951..3da50771 100644 --- a/src/gfx/gfx.hpp +++ b/src/gfx/gfx.hpp @@ -130,6 +130,8 @@ void draw_combine_image(DC& dc, UInt x, UInt y, const Image& img, ImageCombine c /// Use the red channel of img_alpha as alpha channel for img void set_alpha(Image& img, const Image& img_alpha); +/// Use the given bytes as alpha channel for img +void set_alpha(Image& img, Byte* alphas, const wxSize& alphas_size); /// Set the transparency of img void set_alpha(Image& img, double alpha); diff --git a/src/gfx/mask_image.cpp b/src/gfx/mask_image.cpp index ef203305..bfbadac7 100644 --- a/src/gfx/mask_image.cpp +++ b/src/gfx/mask_image.cpp @@ -30,11 +30,7 @@ AlphaMask::~AlphaMask() { } void AlphaMask::setAlpha(Image& img) const { - if (img.GetWidth() != size.GetWidth() || img.GetHeight() != size.GetHeight()) { - throw InternalError(_("Image used with maks must have same size as mask")); - } - if (!img.HasAlpha()) img.InitAlpha(); - memcpy(img.GetAlpha(), alpha, size.GetWidth() * size.GetHeight()); + set_alpha(img, alpha, size); } void AlphaMask::setAlpha(Bitmap& bmp) const { diff --git a/src/gfx/resample_image.cpp b/src/gfx/resample_image.cpp index b4188115..919c9ddf 100644 --- a/src/gfx/resample_image.cpp +++ b/src/gfx/resample_image.cpp @@ -126,6 +126,10 @@ void resample(const Image& img_in, Image& img_out) { } void resample_and_clip(const Image& img_in, Image& img_out, wxRect rect) { + // mask to alpha + if (img_in.HasMask() && !img_in.HasAlpha()) { + const_cast(img_in).InitAlpha(); + } // starting position in data int offset_in = (rect.x + img_in.GetWidth() * rect.y); if (img_out.GetHeight() == rect.height) {