# Channel Extraction

## Recommended Posts

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:

1. Channel Choice - Defines what channel to extract. No need to explain.
2. Direction should be self-explanatory, but I'll explain anway.
1. Channel to Gray : Convert a channel to grayscale image with opaque Alpha.
2. Convert Channel into opaque image with one channel active (if you don't count alpha).
3. 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
// 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 by Reptillian
• 3
• 3

G'MIC Filter Developer

##### Share on other sites

Very useful, thank you 👍

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×