Skip to content

Commit 172b0d7

Browse files
committed
Add premultiplied alpha before saving.
... Signed-off-by: net2cn <mcopener@gmail.com>
1 parent cae9345 commit 172b0d7

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

Real-ESRGAN_GUI/ImageProcess.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ImageProcess
1212
public static Bitmap ResizeBitmap(Bitmap image, int width, int height)
1313
{
1414
var destRect = new Rectangle(0, 0, width, height);
15-
var destImage = new Bitmap(width, height);
15+
var destImage = new Bitmap(width, height, image.PixelFormat);
1616

1717
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
1818

@@ -94,7 +94,7 @@ public static void SplitChannel(Bitmap input, out Bitmap rgb, out Bitmap alpha)
9494
/// <param name="rgb">Format24bppRgb bitmap containing the RGB channels.</param>
9595
/// <param name="alpha">Format24bppRgb bitmap containng the alpha channels in the B channel.</param>
9696
/// <returns></returns>
97-
public static Bitmap CombineChannel(Bitmap rgb, Bitmap alpha)
97+
public static Bitmap CombineChannel(Bitmap rgb, Bitmap alpha, bool premutiply=true)
9898
{
9999
if (rgb.PixelFormat!=PixelFormat.Format24bppRgb || alpha.PixelFormat != PixelFormat.Format24bppRgb)
100100
{
@@ -111,17 +111,32 @@ public static Bitmap CombineChannel(Bitmap rgb, Bitmap alpha)
111111
byte* alphaPtr = (byte*)alphaData.Scan0;
112112
int y, x;
113113

114-
for (y = 0; y < output.Height; y++)
114+
if (premutiply)
115115
{
116-
for (x = 0; x < output.Width; x++)
116+
for (y = 0; y < output.Height; y++)
117117
{
118-
outputPtr[y * outputData.Stride + x * 4 + 0] = rgbPtr[y * rgbData.Stride + x * 3 + 0];
119-
outputPtr[y * outputData.Stride + x * 4 + 1] = rgbPtr[y * rgbData.Stride + x * 3 + 1];
120-
outputPtr[y * outputData.Stride + x * 4 + 2] = rgbPtr[y * rgbData.Stride + x * 3 + 2];
121-
outputPtr[y * outputData.Stride + x * 4 + 3] = alphaPtr[y * alphaData.Stride + x * 4 + 0];
118+
for (x = 0; x < output.Width; x++)
119+
{
120+
outputPtr[y * outputData.Stride + x * 4 + 0] = Convert.ToByte(rgbPtr[y * rgbData.Stride + x * 3 + 0] * alphaPtr[y * alphaData.Stride + x * 3 + 0] / 255f);
121+
outputPtr[y * outputData.Stride + x * 4 + 1] = Convert.ToByte(rgbPtr[y * rgbData.Stride + x * 3 + 1] * alphaPtr[y * alphaData.Stride + x * 3 + 0] / 255f);
122+
outputPtr[y * outputData.Stride + x * 4 + 2] = Convert.ToByte(rgbPtr[y * rgbData.Stride + x * 3 + 2] * alphaPtr[y * alphaData.Stride + x * 3 + 0] / 255f);
123+
outputPtr[y * outputData.Stride + x * 4 + 3] = alphaPtr[y * alphaData.Stride + x * 3 + 0];
124+
}
125+
}
126+
}
127+
else
128+
{
129+
for (y = 0; y < output.Height; y++)
130+
{
131+
for (x = 0; x < output.Width; x++)
132+
{
133+
outputPtr[y * outputData.Stride + x * 4 + 0] = rgbPtr[y * rgbData.Stride + x * 3 + 0];
134+
outputPtr[y * outputData.Stride + x * 4 + 1] = rgbPtr[y * rgbData.Stride + x * 3 + 1];
135+
outputPtr[y * outputData.Stride + x * 4 + 2] = rgbPtr[y * rgbData.Stride + x * 3 + 2];
136+
outputPtr[y * outputData.Stride + x * 4 + 3] = alphaPtr[y * alphaData.Stride + x * 3 + 0];
137+
}
122138
}
123139
}
124-
125140

126141
output.UnlockBits(outputData);
127142
rgb.UnlockBits(rgbData);

0 commit comments

Comments
 (0)