From 172b0d789e4e9cc5b11e20c810bcaaf948846063 Mon Sep 17 00:00:00 2001 From: net2cn Date: Sun, 1 May 2022 21:57:57 +0800 Subject: [PATCH] Add premultiplied alpha before saving. ... Signed-off-by: net2cn --- Real-ESRGAN_GUI/ImageProcess.cs | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/Real-ESRGAN_GUI/ImageProcess.cs b/Real-ESRGAN_GUI/ImageProcess.cs index b75108b..1dbc702 100644 --- a/Real-ESRGAN_GUI/ImageProcess.cs +++ b/Real-ESRGAN_GUI/ImageProcess.cs @@ -12,7 +12,7 @@ class ImageProcess public static Bitmap ResizeBitmap(Bitmap image, int width, int height) { var destRect = new Rectangle(0, 0, width, height); - var destImage = new Bitmap(width, height); + var destImage = new Bitmap(width, height, image.PixelFormat); destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); @@ -94,7 +94,7 @@ public static void SplitChannel(Bitmap input, out Bitmap rgb, out Bitmap alpha) /// Format24bppRgb bitmap containing the RGB channels. /// Format24bppRgb bitmap containng the alpha channels in the B channel. /// - public static Bitmap CombineChannel(Bitmap rgb, Bitmap alpha) + public static Bitmap CombineChannel(Bitmap rgb, Bitmap alpha, bool premutiply=true) { if (rgb.PixelFormat!=PixelFormat.Format24bppRgb || alpha.PixelFormat != PixelFormat.Format24bppRgb) { @@ -111,17 +111,32 @@ public static Bitmap CombineChannel(Bitmap rgb, Bitmap alpha) byte* alphaPtr = (byte*)alphaData.Scan0; int y, x; - for (y = 0; y < output.Height; y++) + if (premutiply) { - for (x = 0; x < output.Width; x++) + for (y = 0; y < output.Height; y++) { - outputPtr[y * outputData.Stride + x * 4 + 0] = rgbPtr[y * rgbData.Stride + x * 3 + 0]; - outputPtr[y * outputData.Stride + x * 4 + 1] = rgbPtr[y * rgbData.Stride + x * 3 + 1]; - outputPtr[y * outputData.Stride + x * 4 + 2] = rgbPtr[y * rgbData.Stride + x * 3 + 2]; - outputPtr[y * outputData.Stride + x * 4 + 3] = alphaPtr[y * alphaData.Stride + x * 4 + 0]; + for (x = 0; x < output.Width; x++) + { + outputPtr[y * outputData.Stride + x * 4 + 0] = Convert.ToByte(rgbPtr[y * rgbData.Stride + x * 3 + 0] * alphaPtr[y * alphaData.Stride + x * 3 + 0] / 255f); + outputPtr[y * outputData.Stride + x * 4 + 1] = Convert.ToByte(rgbPtr[y * rgbData.Stride + x * 3 + 1] * alphaPtr[y * alphaData.Stride + x * 3 + 0] / 255f); + outputPtr[y * outputData.Stride + x * 4 + 2] = Convert.ToByte(rgbPtr[y * rgbData.Stride + x * 3 + 2] * alphaPtr[y * alphaData.Stride + x * 3 + 0] / 255f); + outputPtr[y * outputData.Stride + x * 4 + 3] = alphaPtr[y * alphaData.Stride + x * 3 + 0]; + } + } + } + else + { + for (y = 0; y < output.Height; y++) + { + for (x = 0; x < output.Width; x++) + { + outputPtr[y * outputData.Stride + x * 4 + 0] = rgbPtr[y * rgbData.Stride + x * 3 + 0]; + outputPtr[y * outputData.Stride + x * 4 + 1] = rgbPtr[y * rgbData.Stride + x * 3 + 1]; + outputPtr[y * outputData.Stride + x * 4 + 2] = rgbPtr[y * rgbData.Stride + x * 3 + 2]; + outputPtr[y * outputData.Stride + x * 4 + 3] = alphaPtr[y * alphaData.Stride + x * 3 + 0]; + } } } - output.UnlockBits(outputData); rgb.UnlockBits(rgbData);