Reptillian Posted February 2, 2021 Share Posted February 2, 2021 (edited) After reading the channel extraction problem made by @garry15, I decided to make a plugin with the KISS principle (keep it simple, stupid) or Occam's Razor principle (the easiest answer is often the correct one) to resolve the problem. Here's the plugin - Channel Extraction.zip You'll find it under Effect-> Colors with the name Channel Extraction. Sample of GUI Finally, the parameters: Channel Choice - Defines what channel to extract. No need to explain. Direction should be self-explanatory, but I'll explain anway. Channel to Gray : Convert a channel to grayscale image with opaque Alpha. Convert Channel into opaque image with one channel active (if you don't count alpha). Average the RGB Channel and then it'll result in a opaque image with one channel active (if you don't count alpha). Source Code (Free for all, no attribution required since it's too easy to figure out): Spoiler // Name: Channel Extraction // Submenu: Color // Author: Reptorian // Title: Channel Extraction // Version: 1 // Desc: Extract individual channel with a simple GUI. // Keywords: channels // URL: https://forums.getpaint.net/profile/85868-reptillian/ // Help: #region UICode RadioButtonControl channel = 0; // Channel Choice|Red|Green|Blue|Alpha ListBoxControl direction = 0; // Direction|Channel to Gray|Singular|Average to One #endregion int calc_avg(int r, int g, int b){ double d_r=(double)(r); double d_g=(double)(g); double d_b=(double)(b); double sum=d_r+d_g+d_b; return (int)(Math.Round(sum/3)); } void Render(Surface dst, Surface src, Rectangle rect) { for (int y = rect.Top; y < rect.Bottom; y++) { if (IsCancelRequested) return; for (int x = rect.Left; x < rect.Right; x++) { ColorBgra currentPixel = src[x,y]; ColorBgra outPixel = currentPixel; int val; switch(channel){ case 0: switch (direction){ case 0: outPixel.R = currentPixel.R; outPixel.G = currentPixel.R; outPixel.B = currentPixel.R; outPixel.A = 255; break; case 1: outPixel.R = currentPixel.R; outPixel.G = 0; outPixel.B = 0; outPixel.A = 255; break; case 2: val = calc_avg(currentPixel.R,currentPixel.G,currentPixel.B); outPixel.R = (byte)(val); outPixel.G = 0; outPixel.B = 0; outPixel.A = 255; break; } break; case 1: switch (direction){ case 0: outPixel.R = currentPixel.G; outPixel.G = currentPixel.G; outPixel.B = currentPixel.G; outPixel.A = 255; break; case 1: outPixel.R = 0; outPixel.G = currentPixel.G; outPixel.B = 0; outPixel.A = 255; break; case 2: val = calc_avg(currentPixel.R,currentPixel.G,currentPixel.B); outPixel.R = 0; outPixel.G = (byte)(val); outPixel.B = 0; outPixel.A = 255; break; } break; case 2: switch (direction){ case 0: outPixel.R = currentPixel.B; outPixel.G = currentPixel.B; outPixel.B = currentPixel.B; outPixel.A = 255; break; case 1: outPixel.R = 0; outPixel.G = 0; outPixel.B = currentPixel.B; outPixel.A = 255; break; case 2: val = calc_avg(currentPixel.R,currentPixel.G,currentPixel.B); outPixel.R = 0; outPixel.G = 0; outPixel.B = (byte)(val); outPixel.A = 255; break; } break; case 3: switch (direction){ case 0: outPixel.R = currentPixel.A; outPixel.G = currentPixel.A; outPixel.B = currentPixel.A; outPixel.A = 255; break; case 1: outPixel.R = 255; outPixel.G = 255; outPixel.B = 255; outPixel.A = currentPixel.A; break; case 2: val = calc_avg(currentPixel.R,currentPixel.G,currentPixel.B); outPixel.R = 255; outPixel.G = 255; outPixel.B = 255; outPixel.A = (byte)(val); break; } break; default: outPixel = currentPixel; break; } dst[x,y]=outPixel; } } } Edited February 2, 2021 by Reptillian 3 3 Quote G'MIC Filter Developer Link to comment Share on other sites More sharing options...
ɡarry15 Posted February 3, 2021 Share Posted February 3, 2021 Very useful, thank you 👍 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.