public static Bitmap CombineChannels(Bitmap r, Bitmap g, Bitmap b, Bitmap a) { int width = Helpers.Max(r.Width, g.Width, b.Width, a.Width); int height = Helpers.Max(r.Height, g.Height, b.Height, a.Height); var merged = new Bitmap(width, height, PixelFormat.Format32bppArgb); PopupTextWindow popupText = new PopupTextWindow { PopupText = { Text = "Creating Texture, Please Wait" } }; popupText.Show(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int rIntensity = r.GetPixel(x % r.Width, y % r.Height).R; int gIntensity = g.GetPixel(x % g.Width, y % g.Height).G; int bIntensity = b.GetPixel(x % b.Width, y % b.Height).B; // This uses the generated previews, so the alpha preview has 255 alpha throughout. However, any other channel has the alpha value. int aIntensity = a.GetPixel(x % a.Width, y % a.Height).R; merged.SetPixel(x, y, Color.FromArgb(aIntensity, rIntensity, gIntensity, bIntensity)); } } popupText.Close(); return(merged); }
public static Bitmap ExtractChannel(string imagePath, EChannel channelToExtract, EChannel finalChannel, bool invert, string popupTextExtra = "") { if (!ImageUtility.Validation.IsValidImage(imagePath)) { return(new Bitmap(1, 1)); } var bitmap = new Bitmap(imagePath); var extractedBitmap = new Bitmap(bitmap.Width, bitmap.Height, PixelFormat.Format32bppArgb); PopupTextWindow.OpenWindowAndExecute($"Generating Texture By Extracting Channel, Please Wait {popupTextExtra}", () => { for (int y = 0; y < bitmap.Height; y++) { for (int x = 0; x < bitmap.Width; x++) { Color pixelColor = bitmap.GetPixel(x, y); int intensity; switch (channelToExtract) { case EChannel.R: intensity = pixelColor.R; break; case EChannel.G: intensity = pixelColor.G; break; case EChannel.B: intensity = pixelColor.B; break; case EChannel.A: intensity = ImageUtility.Validation.ImageHasTransparency(imagePath) ? pixelColor.A : 255; break; default: throw new ArgumentOutOfRangeException(nameof(channelToExtract), channelToExtract, null); } if (intensity > 0 & intensity <= 255) { if (invert) { intensity = 255 - intensity; } switch (finalChannel) { case EChannel.R: extractedBitmap.SetPixel(x, y, Color.FromArgb(255, intensity, 0, 0)); break; case EChannel.G: extractedBitmap.SetPixel(x, y, Color.FromArgb(255, 0, intensity, 0)); break; case EChannel.B: extractedBitmap.SetPixel(x, y, Color.FromArgb(255, 0, 0, intensity)); break; case EChannel.A: extractedBitmap.SetPixel(x, y, Color.FromArgb(255, intensity, intensity, intensity)); break; default: throw new ArgumentOutOfRangeException(nameof(finalChannel), finalChannel, null); } } else { extractedBitmap.SetPixel(x, y, Color.FromArgb(0, 0, 0, 0)); } } } }); bitmap.Dispose(); return(extractedBitmap); }