Modify Channels v1.0.2 (2017-02-19)

11 posts in this topic

This plugin allows you to modify multiple channels of an image in various ways. Once installed, it can be found in the Adjustments menu, since it is more of an adjustment utility than an effect.


Basics - How the Plugin Works

In the plugin's dialog, you can configure multiple channel modifications. Each modification allows you to manipulate one channel of the image with one of the built in operations. Every operation uses one or more arguments to calculate the new value for the target channel. The number, form and usage of those arguments depends on the chosen operation. The modifications are executed per pixel and in the order you have arranged them. Use the ˄ and ˅ buttons to re-arrange the modifications, the + button to add more, the × button to remove one, and check/uncheck the Enabled flag to switch a modification on and off.


ModifyChannelsScreenshot.png   ModifyChannelsScreenshot2.png


Download and Installation

Download from code.lwchris.de (my webspace)


To install this plugin, please follow the usual steps when installing Paint.NET plugins:

  1. Download the zip file
  2. Extract the content
  3. Move the DLL file into the "Effects" folder in Paint.NET's root directory (for example "C:\Program Files\Paint.NET\Effects")
  4. Restart Paint.NET
  5. The plugin will appear as "Modify Channels..." in the Adjustments, not the Effects menu




Version (2017-02-19)


  • Maximum operation was returning lesser instead of greater value.


Version (2017-01-07)


  • Index offset error that caused "Minimum" to show up as source channel operation for binary operations such as "Minimum" itself, "Maximum", "Add", etc. Selecting this as operation in the dropdown caused the plugin to crash.



Depending on whether you have to select the target channel to write to or the source channel to read from, different channels are available:

  • Red/Green/Blue/Alpha - Gets or sets the value for the respective channel. These values always include the changes from previous modifications.
  • Temp 1/Temp 2 - Gets or sets the value of the respective temporary channel
  • Source Red/Green/Blue/Alpha - Gets the original, unmodified value for the respective channel (read-only)
  • Average - Gets the average of the current Red, Green and Blue channel values (read-only)
  • Grayscale - Sets the Red, Green and Blue channel to the given value (write-only)


Channel value types

The Red, Green, Blue and Alpha channels are int channels (and since Grayscale is just a shortcut for assigning all three at once, it has the same limitations). This means assigned values will be rounded to whole numbers, but are allowed to go way below 0 or above 255. After the last channel modification, their values are clamped to 0-255 and then interpreted as the resulting RGBA color.

Temp 1 and Temp 2 are double channels, therefore they can store floating-point values.

Reading from Average returns a double. Assigning that value to an integer channel will cause the assigned value to be rounded.



Operations can be split into four types: constant, unary, binary or ternary, depending on the number and type of arguments they take.

1. Constant operation
The constant operation takes one constant value as argument.

  • Constant (value) - Returns value

2. Unary operations
Unary operations take one source channel as argument.

  • Identity (channel) - Returns value of the channel
  • Invert (channel) - Returns 255 - value of the channel
  • Absolute (channel) - Returns -1 * value of the channel if value of the channel < 0, or value of the channel otherwise

3. Binary operations
Binary operations take two constant or unary operations as argument. For Divide, the arguments may be augmented with an additional constant.

  • Minimum (arg1, arg2) - Returns arg1 if arg1 < arg2, or arg2 otherwise
  • Maximum (arg1, arg2) - Returns arg1 if arg1 > arg2, or arg2 otherwise
  • Add (arg1, arg2) - Returns arg1 + arg2
  • Subtract (arg1, arg2) - Returns arg1 - arg2
  • Multiply (arg1, arg2) - Returns arg1 * arg2
  • Divide (arg1, arg2, [value]) - Returns arg1 / arg2; if arg2 is a channel, provide value as return value in case the channel value is almost 0 (i. e. < 0.00000001)

4. Ternary operations
Ternary operations take one source channel and two constant values as arguments.

  • Clamp (channel, min, max) - Returns min if value of the channel < min, max if value of the channel > max, or value of the channel otherwise
  • L-Threshold (channel, min, value) - Returns value if value of the channel < min, or value of the channel otherwise
  • U-Threshold (channel, max, value) - Returns value if value of the channel > max, or value of the channel otherwise


A specification of all possible channel modifications, given in EBNF.


ChannelModification   =   TargetChannel , "=" , ChannelOperation ;
TargetChannel         =   "Red" | "Green" | "Blue" | "Alpha" | "Temp 1" | "Temp 2" | "Grayscale" ;
ChannelOperation      =   ConstantSource | UnarySource | BinarySource | TernarySource ;
ConstantSource        =   "Constant" , Value ;
UnarySource           =   ( "Identity" | "Invert" | "Absolute" ) , SourceChannel ;
BinarySource          =   ( ( "Mininum" | "Maximum" | "Add" | "Subtract" | "Multiply" ) , SourceArg , SourceArg ) |
                            ( "Divide" , SourceArg , "Constant" , ? Number from 1 to 255 ? ) |

                            ( "Divide" , SourceArg , UnarySource , Value ) ;
TernarySource         =   ( "Clamp" | "L-Threshold" | "U-Threshold" ) , SourceChannel , Value , Value ;
Value                 =   ? Number from 0 to 255 ? ;
SourceChannel         =   "Red" | "Green" | "Blue" | "Alpha" | "Temp1" | "Temp2" |
                            "SourceRed" | "SourceGreen" | "SourceBlue" | "SourceAlpha" | "Average" ;
SourceArg             =   ConstantSource | UnarySource ;



Create an opacity map (the darker, the more opaque):

  • Grayscale = Identity Alpha
  • Alpha = Constant 255


Transform into pencil sketch:

  • Alpha = Invert Average
  • Grayscale = Identity Average


Swap the red and blue channel:

  • Red = Identity Blue
  • Blue = Identity Source Red

Note that Blue = Identity Red does not work, because the first channel modification has already changed the value of the Red channel


Legal notes

I hate this part, too, but it is somewhat obligatory for software...

  • The plugin is free to use for anything permitted by applicable law (including commercial purposes).
  • You are however not allowed to modify or sell this plugin.
  • Do not redistribute the plugin except by sharing links to this forum post please. If you want to include it in your plugin pack, please ask for my permission first.
  • The plugin is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall I or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the plugin or the use or other dealings in the plugin.
Edited by LWChris
Version 1.0.2

Share this post

Link to post
Share on other sites

example plugin works



Edited by flaner

Share this post

Link to post
Share on other sites

Hello LWChris, and thanks for this adjustment effect.


I have been trying to use it but everytime I do, it crashes. I am posting the crash message I get : 


This text file was created because paint.net crashed. Please e-mail this to crashlog4@getpaint.net so we can diagnose and fix the problem.

Application version: paint.net 4.0.13 (Final 4.13.6191.1824)
Time of crash: 1/2/2017 12:50:03 PM
Application uptime: 01:14:47.2514519
Application state: Running
Working set: 120,464 KiB
Handles and threads: 1258 handles, 28 threads, 156 gdi, 315 user
Install directory: C:\Program Files\paint.net
Current directory: C:\Program Files\Paint.NET
OS Version: 10.0.14393.0 Workstation x86
.NET version: CLR 4.0.30319.42000 x86, FX 4.6
Processor: "Intel(R) Core(TM)2 Quad  CPU   Q9550  @ 2.83GHz" @ ~2826MHz (4C/4T, DEP, SSE, SSE2, SSE3, SSSE3, SSE4_1)
Physical memory: 3325 MB
Video card: ATI Radeon HD 2400 XT (v:1002, d:94C1, r:0), Microsoft Basic Render Driver (v:1414, d:8C, r:0)
Hardware acceleration: True (default: True)
UI animations: True
UI DPI: 96.00 dpi (1.00x scale)
UI theme: Aero/Aero + DWM (Aero.msstyles)
Updates: False, 6/27/2016
Locale: pdnr.c: en-US, hklm: en-US, hkcu: en-US, cc: en-US, cuic: en-US

Exception details:
System.ArgumentException: SourceChannel1Operation
   at ModifyChannelsEffect.ChannelModification.get_SourceChannel1OperationType()
   at ModifyChannelsEffect.ChannelModificationControl.UpdateLayout(Boolean updateControlValues)
   at ModifyChannelsEffect.ChannelModificationControl.AfterValueUpdate()
   at ModifyChannelsEffect.ChannelModificationControl.cbx_SourceChannel1Operation_SelectedIndexChanged(Object sender, EventArgs e)
   at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)
   at System.Windows.Forms.ComboBox.WmReflectCommand(Message& m)
   at System.Windows.Forms.ComboBox.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)



Share this post

Link to post
Share on other sites

Hi @LWChris.  I have downloaded this plugin into PDN.  It doesn't crash, but I have no idea of how to use it.  A tutorial would be of great help.  Thank you ;).


Share this post

Link to post
Share on other sites

Dear LWChris! <3


Firstly welcome and have fun here.  :PaintDotNet32: Thank you (Dankeschön ;)) for the plugin. :cake:  :coffee:


Share this post

Link to post
Share on other sites

Posted (edited)

Very similar to my Channel Ops plugin. Mine was missing Absolute and Clamp (though it had the other one-sided versions of clamp). I don't think Absolute does enough to be included in the plugin, if I understand correctly, since it wouldn't be changing anything.


Mine had some other additions (Modulus binary operator and sharpen) as additional features. Your UI is an improvement over mine, though.

Edited by AnthonyScoffler

Share this post

Link to post
Share on other sites
On 2.1.2017 at 1:11 PM, Eli said:

Hello LWChris, and thanks for this adjustment effect.


I have been trying to use it but everytime I do, it crashes.


I am not completely sure, but I think I just ran into the same bug and already found the cause. In the method populating the dropdowns, my maximum index was off by 1, making "Minimum" available as source operation for binary operations such as "Minimum", "Maximum", "Add", "Subtract" etc. Actually only Constant, Identity, Invert or Absolute should be allowed. Fixed in version 1.0.1, available as of now.


Share this post

Link to post
Share on other sites
On 5.1.2017 at 8:13 AM, AnthonyScoffler said:

Very similar to my Channel Ops plugin.

Mine had some other additions (Modulus binary operator and sharpen) as additional features. Your UI is an improvement over mine, though.


Hi Anthony, I actually saw your plugin, but it did not appear to fit my needs. If it had, I hadn't written the plugin.


The main reason I made the plugin is because I work with OpenCV in Visual Studio from time to time. There is a plugin to display an image matrix as actual image again. OpenCV does not use RGB though, but BGR; yet the plugin interprets the channels as RGB, causing the Red and Blue channels to appear swapped. I could make a screenshot of that image and paste it to Paint.NET, but from what I can tell there is no possible operation chain to swap the channels back. Thus I decided to make a plugin for that (originally written in CodeLab and called "Swap Channels"). But then I thought "why not offer more operations", then noticed the CodeLab UI is too limited for this, and things got rolling. In the end, I ended up with a huuuge Visual Studio project and found the plugin to have been too much work to keep it for occasional personal use and not publish it.



Mine was missing Absolute and Clamp (though it had the other one-sided versions of clamp). I don't think Absolute does enough to be included in the plugin, if I understand correctly, since it wouldn't be changing anything.


If I have understood your plugin correctly, it manipulates one channel, then the user clicks "OK", maybe opens the plugin again, manipulates the next channel, etc. In that case, an "Absolute" operation makes no sense since there are no negative input values in your use case. My plugin however allows you to chain multiple modifications (a necessity to perform a channel swap unless you have a dedicated "Swap" operation). Because of that, my plugin deliberately allows negative intermediate values in any channel. For example, you may assign "Red - 200" to the "Red" channel, and then continue to add values to that channel etc. In this case, "Absolute" can make sense, and since it's very cumbersome to simulate that operation with other operations but easy to implement it, I included it.



Mine had some other additions (Modulus binary operator and sharpen) as additional features.


I consider adding more features soon, but at that point, the functionality was rich enough to have it released. I'm not exactly sure what "Sharpen" is (haven't looked into your source code), but since it is not a specific channel operation, I will most likely not include some "overall visual effects" like sharpen, flatten, increase contrast etc.



Your UI is an improvement over mine, though.


I spent five evenings on the plugin. One and a half on the calculation logic, three and a half on the layout planning and UI logic. ;)


Share this post

Link to post
Share on other sites

Posted (edited)

On 2.1.2017 at 3:54 PM, Pixey said:

Hi @LWChris.  I have downloaded this plugin into PDN.  It doesn't crash, but I have no idea of how to use it.  A tutorial would be of great help.  Thank you ;).


Hi @Pixey, here is an example of a more complex task you can perform with the plugin. Hopefully it helps you to understand what the plugin does, how it works and how to use it.


At first, let me start with a short elaboration on what a "channel" is, in case this is still unclear: you can imagine a channel as a large value table. For each pixel in height, there is a table row, for each pixel in width, there is a column. Each table cell stores one value from 0 to 255 for the corresponding pixel. There are 4 actual and 3 calculated channels for an image (or to be more PDN specific, 7 channels per layer): Red, Green, Blue and Alpha are the actual channels (shortened RGBA). Hue, saturation and luminosity (HSL) are calculated from the RGB values. (As a side note, the transformation from RGB to HSL and vice versa is not completely bijective, as different HSL values can lead to the same RGB color.) My plugin is designed to tinker with the values of the RGBA channels. Maybe it'll learn about the HSL channels later on, too, but right now it's limited to the RGBA channels.


Now for the tutorial part.


In this tutorial, we are going to perform a "downward compression" for one color channel of an image, namely red. This can be done by choosing two values: first, a certain threshold value, second a compression ratio. Below our threshold, channel values will not be changed. Values above the threshold however will have their exceedance reduced by our compression ratio. This means for an increasing source value, above the threshold the result value growth will be slower and the overall maximum value will be lower. Usually this is a technique to compress audio signals to dampen peaks (see "dynamic range compression"), but we can do that for images' color channels, too.


The following image illustrates the compression we are going to perform on the red channel, choosing a threshold value of 100 and a compression ratio of 3:1:


For the above image, I created a 256 pixels wide gradient from blue (RGB = 0, 0, 255) to magenta (255, 0, 255), so that at the "0" mark, the pixel values are (0, 0, 255), at the "100" mark they are (100, 0, 255), and so on.


What we expect to happen when we apply the compression is that the blue value stays 255 for the whole gradient like it is. The red value does not change up to the "100" mark, then the value change "slows down" to +1 red per 3 pixels, resulting in (152, 0, 255) for the pixel at the "255" mark. The "152" is the result of the maximum red value "255" processed with our compression formula: 100+(255-100)/3 = 100+155/3 = 100+51,666... = 152 after rounding.


Now let's actually do this with my plugin. We are going to do it in three logical steps:

  1. Split the original Red value into the unmodified amount (left, 0-100) and the to-be-compressed amount (right, 0-155), in two separate channels
  2. Perform the actual compression on the channel with the to-be-compressed values
  3. Add up both values again and assign the result as new Red value

Step 1 - Split the amounts into two channels

Given that my plugin has no single operation to perform the split operation, this step is probably the trickiest to figure out if you're not used to "chaining multiple simple operations to perform a complex operation". For me, being a programmer, seeing the tiny steps in complex tasks is what I do for a living, so it's obvious for me how to do it. Of course one does not have to be a programmer to see it, but I guess it helps. Anyway, this is how we can do it in three smaller steps using operations my plugin actually offers:


Step 1.1

At first, we are going to subtract our constant threshold value "100" from the Red channel values. This means, for values less than or equal to the threshold, the result will be less than or equal to 0. For values above the threshold, the result is going to be greater than 0. We can store the result in one of the two temporary channels.


Step 1.2

Next up, we apply an L-Threshold operation to that temporary channel and configure it to set all values less than 0 (first parameter, top row) to 0 (second parameter, second row). After this step, our temporary channel contains the "to-be-compressed" value per pixel. Note that L-Threshold allows us to define the replacement value independently from the threshold value. Since the threshold and the replacement value are identical, we could also use the Clamp operation with "0" and "any number greater than or equal to 155" as arguments, so that the second argument does not apply to any value in the temp channel. In our scenario, it does not make a difference.


Step 1.3

Eventually, we apply the U-Threshold operation to the Red channel, with a threshold and replacement value of 100. This limits all values to a maximum of 100, but keeps lower values unmodified. Now, the Red channel contains the unmodified value amount for every pixel.


Step 2 - Compress

Applying the compression ratio is actually pretty straightforward. You Divide the value in the temporary channel by our constant compression ratio "3" and write the result back to the temporary channel as target channel. Note that both temporary channels actually store floating point values, so for a pixel that originally had "105" as red value, the temporary channel now holds 5 / 3 = "1.666..." as value, not "2" yet. In our example, we are going to add the value to an integer number and then immediately round the result, so we're not going to profit from keeping the precision here. But sometimes you want to multiply or clamp the result later on and it's good to know that for those two channels the values are kept with double precision, which should be "good enough" in all normal cases.


Step 3 - Add up

Adding both values is also simple. As the previous sentence suggests, you chose the Add operation, select the red and the temporary channel as arguments, and apply the result as the new Red value. Again, there is something to "note". As with the temporary channel, the value of the Red channel was changed in step 1.3, and using it as source again get the modified value. This is why you can order the modification steps - because the order actually makes a difference.


This is how the plugin configuration should look like now:


Tip: In case you don't see a difference yet, make sure you enabled the modifications. Newly added modifications are disabled by default, as they are likely not configured correctly yet and IMO it makes no sense to render the whole image with a presumably wrong configuration over and over again. Also, modifications to disabled channels do not trigger a render update, keeping Paint.NET responsive while configuring the values.


And this is how the result looks when the modification is applied with a selection of the gradient area.


You can see how the color on the right end is far from magenta. With the color picker tool I have tested and can assure you, the Red values from 0 to 100 are 0, 1, 2, ..., 98, 99, 100, and the values from 101 to 255 are 100, 101, 101, 101, 102, ..., 150, 151, 151, 151, 152, which are the correct rounded values for 100.3, 100.6, 100.9, 101.3, 101.6, ... etc.


I hope this extensive tutorial/example helps you to warm up to the plugin. Let me know if it helped you, I have an idea for another one, but I'd rather know if I need to change the way I write these tutorials first before I post another one in a second post taking me hours to write. ;)


Thank you for using my plugin, I hope it'll bring great joy and new possibilities to your art.



Edited by LWChris

Share this post

Link to post
Share on other sites

I'm not exactly sure what "Sharpen" is

I scale the current value based on its distance from the extreme values (min/max of a range) or the center. This is my version of sharpen, which shows a moderate and severe sharpen.



Contrast, instead of shrinking the range towards the center, would expand it out from the center (it's the inverse operation). Thus, a max sharpen will shrink it to two values: black and white (which means the min or max values of the range). A max contrast would expand it so that only the center values are possible. That's how I came up with it and designed it; I looked at algorithms for these things, but couldn't find much. I found something about gamma, but it was rather mathematical. I think this method works in practice, but don't quote me on that :D


Share this post

Link to post
Share on other sites

Thank you so much @LWChris for that very detailed description of how your Plugin works. Thank you taking the time to explain it in such detail B).


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now