Skip to content

Commit

Permalink
fix: update blend functions to respect alpha channel (fixes #82) (#83)
Browse files Browse the repository at this point in the history
* fix: update linear_blend to respect alpha channel

* fix: update masked_blend to respect alpha channel

Unclear if this is the right implementation.

* misc: remove unnecessary fixed declaration

* misc: update mask_blend

* update changes.md

* misc: fix typo in changes.md
  • Loading branch information
haganbmj authored Aug 23, 2024
1 parent ddd49b5 commit bd87577
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
8 changes: 5 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@ Magic Set Editor changelog, for the details see `git log`
==============================================================================

------------------------------------------------------------------------------
version 2.5.0 (Unofficial), 2024-04-28
version 2.5.0 (Unofficial), 2024-08-23
------------------------------------------------------------------------------

Features:
* Add Russian and Korean translations. Update locale files from Full-Magic-Pack (haganbmj#69)
* Increase stats column width by 50% (haganbmj#72)

Bug fixes:
* Fixed scrolled widgets not scrolling fully (or at all) on Linux (@llemoi, haganbmj#54)
* Fixed crash when 0 keywords match filter string in keyword panel (@llemoi, haganbmj#57)
* Correct "all images" filter when selecting files. (haganbmj#70)
* Correct "all images" filter when selecting files. (@BaconCatBug, haganbmj#70)
* Correct localization of strings on Print window.
* Fixed issue where "All Images" filter on the art picker was not showing .jpeg files (BaconCatBug)
* Correct how blend functions handle alpha channels. (haganbmj#83)

Internal:
* Add Mac OS build resources (@halian, haganbmj#67, haganbmj#68)
* CMakeLists updates for static compilation. (@BackCatBug, haganbmj#74)

------------------------------------------------------------------------------
version 2.4.0 (Unofficial), 2022-11-19
Expand Down
42 changes: 36 additions & 6 deletions src/gfx/blend_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,53 @@ void linear_blend(Image& img1, const Image& img2, double x1,double y1, double x2
data1 += 3;
data2 += 3;
}
}

// Blend Alpha for the two images.
if (img1.HasAlpha() && img2.HasAlpha()) {
Byte *alpha1 = img1.GetAlpha(), *alpha2 = img2.GetAlpha();
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
int mult = x * xm + y * ym + d;
if (mult < 0) mult = 0;
if (mult > fixed) mult = fixed;
alpha1[0] = alpha1[0] + mult * (alpha2[0] - alpha1[0]) / fixed;
alpha1 += 1;
alpha2 += 1;
}
}
}
}

// ----------------------------------------------------------------------------- : Mask Blend

void mask_blend(Image& img1, const Image& img2, const Image& mask) {
if (img2.GetWidth() != img1.GetWidth() || img2.GetHeight() != img1.GetHeight()
|| mask.GetWidth() != img1.GetWidth() || mask.GetHeight() != img1.GetHeight()) {
throw Error(_("Images used for blending must have the same size"));
int width = img1.GetWidth(), height = img1.GetHeight();
if (img2.GetWidth() != width || img2.GetHeight() != height) {
throw Error(_("Images used for blending in masked_blend function must have the same size"));
}

UInt size = img1.GetWidth() * img1.GetHeight() * 3;
if (mask.GetWidth() != width || mask.GetHeight() != height) {
throw Error(_("Mask used for blending in masked_blend function must have the same size as the images"));
}

UInt size = width * height;
// these have the following structure:
// [pixel1red, pixel1green, pixel1blue, pixel2red, pixel2green, pixel2blue, pixel3red, etc...]
Byte *data1 = img1.GetData(), *data2 = img2.GetData(), *dataM = mask.GetData();
// for each subpixel...
for (UInt i = 0 ; i < size ; ++i) {
for (UInt i = 0; i < (size * 3); ++i) {
data1[i] = (data1[i] * dataM[i] + data2[i] * (255 - dataM[i])) / 255;
}

if (img1.HasAlpha() && img2.HasAlpha()) {
// these have the following structure:
// [pixel1alpha, pixel2alpha, pixel3alpha, etc...]
Byte *alpha1 = img1.GetAlpha(), *alpha2 = img2.GetAlpha();
for (UInt i = 0; i < size; ++i) {
// use mask's red channel to blend alpha (all mask channels should be identical since it's grey scale)
alpha1[i] = (alpha1[i] * dataM[i * 3] + alpha2[i] * (255 - dataM[i * 3])) / 255;
}
}
}

// ----------------------------------------------------------------------------- : Alpha
Expand Down

0 comments on commit bd87577

Please sign in to comment.