Skip to content

Commit

Permalink
Fix transparency issues for a few effects
Browse files Browse the repository at this point in the history
Premultiplied alpha should be used since the colors are from a Cairo surface

Fixes: #1184
  • Loading branch information
cameronwhite committed Dec 21, 2024
1 parent b23784b commit 7e4bf3d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Thanks to the following contributors who worked on this release:
- Fixed various artifacts in the shape tools, particularly with larger brush widths (#733, #955)
- Fixed an issue where the text tool did not immediately redraw after changes to the font or color of unfinalized text (#952, #975)
- Fixed a bug where cut / paste operations did not behave as expected with complex selections (#951, #978)
- Fixed transparency behavior for several effects (#1184)

## [2.1.2](https://github.com/PintaProject/Pinta/releases/tag/2.1.2) - 2024/04/20

Expand Down
15 changes: 9 additions & 6 deletions Pinta.Effects/Effects/InkSketchEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public override void Render (ImageSurface src, ImageSurface dest, ReadOnlySpan<R
int right = Math.Min (x + Radius + 1, dest.Width);

RectangleI adjustedBounds = RectangleI.FromLTRB (left, top, right, bottom);
ColorBgra baseRGB = CreateBaseRGB (src_data, width, x, y, adjustedBounds);
ColorBgra baseRGB = CreateBaseRGBA (src_data, width, x, y, adjustedBounds);
ColorBgra topLayer = CreateTopLayer (baseRGB);

// Change Blend Mode to Darken
Expand All @@ -104,11 +104,12 @@ public override void Render (ImageSurface src, ImageSurface dest, ReadOnlySpan<R
}
}

private static ColorBgra CreateBaseRGB (ReadOnlySpan<ColorBgra> src_data, int width, int x, int y, RectangleI adjustedBounds)
private static ColorBgra CreateBaseRGBA (ReadOnlySpan<ColorBgra> src_data, int width, int x, int y, RectangleI adjustedBounds)
{
int r = 0;
int g = 0;
int b = 0;
int a = 0;

for (int v = adjustedBounds.Top; v < adjustedBounds.Bottom; v++) {

Expand All @@ -124,13 +125,15 @@ private static ColorBgra CreateBaseRGB (ReadOnlySpan<ColorBgra> src_data, int wi
r += src_pixel.R * w;
g += src_pixel.G * w;
b += src_pixel.B * w;
a += src_pixel.A * w;
}
}

return ColorBgra.FromBgr (
return ColorBgra.FromBgra (
b: Utility.ClampToByte (b),
g: Utility.ClampToByte (g),
r: Utility.ClampToByte (r)
r: Utility.ClampToByte (r),
a: Utility.ClampToByte (a)
);
}

Expand All @@ -141,8 +144,8 @@ private ColorBgra CreateTopLayer (ColorBgra baseRGB)

// Adjust Brightness and Contrast
return
(topLayer.R > (Data.InkOutline * 255 / 100))
? ColorBgra.FromBgra (255, 255, 255, topLayer.A)
(topLayer.ToStraightAlpha ().R > (Data.InkOutline * 255 / 100))
? ColorBgra.FromBgra (topLayer.A, topLayer.A, topLayer.A, topLayer.A)
: ColorBgra.FromBgra (0, 0, 0, topLayer.A);
}
#endregion
Expand Down
24 changes: 12 additions & 12 deletions Pinta.Effects/Effects/RadialBlurEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ private static ColorBgra GetFinalPixelColor (
X: (pixel.coordinates.X << 16) - settings.fcx,
Y: (pixel.coordinates.Y << 16) - settings.fcy);

int sr = sourcePixel.R * sourcePixel.A;
int sg = sourcePixel.G * sourcePixel.A;
int sb = sourcePixel.B * sourcePixel.A;
int sr = sourcePixel.R;
int sg = sourcePixel.G;
int sb = sourcePixel.B;
int sa = sourcePixel.A;
int sc = 1;

Expand All @@ -126,9 +126,9 @@ private static ColorBgra GetFinalPixelColor (

ColorBgra sample = sourceData[p1.Y * settings.canvasSize.Width + p1.X];

sr += sample.R * sample.A;
sg += sample.G * sample.A;
sb += sample.B * sample.A;
sr += sample.R;
sg += sample.G;
sb += sample.B;
sa += sample.A;

++sc;
Expand All @@ -142,9 +142,9 @@ private static ColorBgra GetFinalPixelColor (

ColorBgra sample = sourceData[p2.Y * settings.canvasSize.Width + p2.X];

sr += sample.R * sample.A;
sg += sample.G * sample.A;
sb += sample.B * sample.A;
sr += sample.R;
sg += sample.G;
sb += sample.B;
sa += sample.A;

++sc;
Expand All @@ -154,9 +154,9 @@ private static ColorBgra GetFinalPixelColor (
return
(sa > 0)
? ColorBgra.FromBgra (
b: Utility.ClampToByte (sb / sa),
g: Utility.ClampToByte (sg / sa),
r: Utility.ClampToByte (sr / sa),
b: Utility.ClampToByte (sb / sc),
g: Utility.ClampToByte (sg / sc),
r: Utility.ClampToByte (sr / sc),
a: Utility.ClampToByte (sa / sc))
: ColorBgra.FromUInt32 (0);
}
Expand Down
18 changes: 9 additions & 9 deletions Pinta.Effects/Effects/ZoomBlurEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ private static ColorBgra GetFinalPixelColor (
int sc = 0;

ColorBgra src_pixel = sourceData[pixel.memoryOffset];
sr += src_pixel.R * src_pixel.A;
sg += src_pixel.G * src_pixel.A;
sb += src_pixel.B * src_pixel.A;
sr += src_pixel.R;
sg += src_pixel.G;
sb += src_pixel.B;
sa += src_pixel.A;
++sc;

Expand All @@ -124,9 +124,9 @@ private static ColorBgra GetFinalPixelColor (
settings.size.Width,
transformed);

sr += src_pixel_2.R * src_pixel_2.A;
sg += src_pixel_2.G * src_pixel_2.A;
sb += src_pixel_2.B * src_pixel_2.A;
sr += src_pixel_2.R;
sg += src_pixel_2.G;
sb += src_pixel_2.B;
sa += src_pixel_2.A;
++sc;
}
Expand All @@ -135,9 +135,9 @@ private static ColorBgra GetFinalPixelColor (
return
(sa != 0)
? ColorBgra.FromBgra (
b: Utility.ClampToByte (sb / sa),
g: Utility.ClampToByte (sg / sa),
r: Utility.ClampToByte (sr / sa),
b: Utility.ClampToByte (sb / sc),
g: Utility.ClampToByte (sg / sc),
r: Utility.ClampToByte (sr / sc),
a: Utility.ClampToByte (sa / sc))
: ColorBgra.FromUInt32 (0);
}
Expand Down

0 comments on commit 7e4bf3d

Please sign in to comment.