RSS/Atom feed Twitter
Site is read-only, email is disabled

Layer addition - bug, feature, or user misunderstanding?

This discussion is connected to the gimp-user-list.gnome.org mailing list which is provided by the GIMP developers and not related to gimpusers.com.

This is a read-only list on gimpusers.com so this discussion thread is read-only, too.

8 of 8 messages available
Toggle history

Please log in to manage your subscriptions.

Layer addition - bug, feature, or user misunderstanding? Elle Stone 12 Oct 15:22
  Layer addition - bug, feature, or user misunderstanding? Richard 13 Oct 04:25
   Layer addition - bug, feature, or user misunderstanding? Elle Stone 13 Oct 14:55
    Layer addition - bug, feature, or user misunderstanding? Elle Stone 13 Oct 15:46
     Layer addition - bug, feature, or user misunderstanding? Richard 14 Oct 03:14
     Layer addition - bug, feature, or user misunderstanding? Richard 14 Oct 03:45
      Layer addition - bug, feature, or user misunderstanding? Elle Stone 14 Oct 10:00
       Layer addition - bug, feature, or user misunderstanding? Richard 14 Oct 17:46
Elle Stone
2015-10-12 15:22:46 UTC (over 8 years ago)

Layer addition - bug, feature, or user misunderstanding?

Using GIMP 2.9 updated yesterday, two different ways of adding layers produce different results. But it seems to me that the two ways should produce the same results. Here is a screenshot: http://ninedegreesbelow.com/bug-reports/gimp29/layer-addition/addition-results-vary.png,

Are the different results a bug, a feature, or am I making an obvious mistake or just not understanding something?

Looking at the screenshot, the "channel" layers were produced by making a solid white layer and dragging the Red, Blue, and Green channels over to the layer stack. So of course each channel layer also has R=G=B=1.0.

The channel layers are added using layer percent opacities of 22.2 for the Red channel layer, 71.7% opacity for the Green channel layer, and 6.1% opacity for the Blue channel layer. The percentages are the correct percentages for producing a Luminance conversion to black and white by adding the Red, Green, and Blue channels together as layers.

The result of adding the three layers should be white, R=G=B=1.0, which is what happens with the second way of adding the layers. But the first way, using the more obvious "add each layer to the layers below", produces R=G=B=0.793257.

Here's a download link for the actual XCF file: http://ninedegreesbelow.com/bug-reports/gimp29/layer-addition/white-gimpdefault.xcf

You'll need to reset *all* of the layer opacities to the values given above, because for some reason saving to disk and reopening causes layer opacities to shift slightly (for example, the Blue layer opacities shift to 5.9% instead of staying at 6.1%).

The image is an sRGB image and the precision is 32-bit floating point (linear) in order to get the layers to properly add up to R=G=B=1.0, which they should anyway for solid white, but results would be wrong for colors other than solid white or solid black.

Elle, puzzled

http://ninedegreesbelow.com
Color management and free/libre photography
Richard
2015-10-13 04:25:24 UTC (over 8 years ago)

Layer addition - bug, feature, or user misunderstanding?

The problem is that a layer's opacity doesn't add -- it multiplies, like this:

Result = (opacity) * (this layer) + (100%-opacity) * (result of layers below)

This formula holds true regardless of the layer's assigned blending mode (and it's recursive, with the "result of layers below" defined by inserting the next layer down into the same formula).

So, if you want multiple translucent layers to have specific 'overall' contributions to the final image you need to work out the opacities for each individual layer, namely with lower layers having more opacity to compensate for their lower position in the stack.

Here is what happens with your example on the left -- Red layer has opacity 22.2%, Green has opacity 71.7%, Blue has opacity 6.1% (all this on top of a solid black background), so the overall image is blended like this:

Image = 22.2% * (red) + (100%-22.2%) * (green,blue) = 22.2% * (red) + 77.8% * (71.7% * green + (100%-71.7%) * (6.1% * blue) ) = 22.2% * (red) + 77.8% * (71.7% * green + 28.3% * (6.1% * blue) ) = 22.2% * (red) + 55.8% * (green) + 1.3% * (blue)

And since we know that your red, green, and blue are all white, this means the overall image is:

= (22.2% + 55.8% + 1.3%) * (white) = 79.3% white

Doesn't that 79% look rather familiar? :)

Aside - the left half of your image is totally reproducible on GIMP 2.8 . (I can't seem to reproduce the right half in 2.8, but I haven't examined the actual XCF either, so I don't have all the details.)

Now to fix the values ... first, Red is on top so it can keep the 22.2%; this leaves a translucency of 77.8% for everything below it. For Green, below Red, divide its opacity by Red's translucency (above): Green's opacity should be (71.7% / 77.8%) = 92.1%. This, in turn, leaves 7.9% of translucency for Blue below it. For Blue (which is below both Green and Red), divide its opacity by the overall translucencies of both Red and Green. You can do the math if you want (6.1% / 77.8% / 7.9%), but it conveniently works out to exactly 100% opacity -- i.e. Blue doesn't need any translucency for itself because with both Red and Green on top of it (at the above opacities) only 6.1% of Blue will be visible anyway.

To prove it, just plug the new opacities back into the above formula:

Image = 22.2% * (red) + 77.8% * (92.1% * (green) + (100% - 92.1%) * (100% * blue) ) = 22.2% * (red) + 77.8% * (92.1% * (green) + 7.9% * (blue) ) = 22.2% * red + 71.7% * green + 6.1% * blue

PS - Keep in mind the above math only works when all three of your R/G/B layers are visible; if you toggle even one of them off the opacity of the ones underneath it will look off because they depended on the layer(s) above them to blend correctly.

-- Stratadrake strata_ranger@hotmail.com
--------------------
Numbers may not lie, but neither do they tell the whole truth.

From: ellestone@ninedegreesbelow.com To: gimp-user-list@gnome.org
Date: Mon, 12 Oct 2015 11:22:46 -0400 Subject: [Gimp-user] Layer addition - bug, feature, or user misunderstanding?

Using GIMP 2.9 updated yesterday, two different ways of adding layers produce different results. But it seems to me that the two ways should produce the same results. Here is a screenshot: http://ninedegreesbelow.com/bug-reports/gimp29/layer-addition/addition-results-vary.png,

Are the different results a bug, a feature, or am I making an obvious mistake or just not understanding something?

Looking at the screenshot, the "channel" layers were produced by making a solid white layer and dragging the Red, Blue, and Green channels over to the layer stack. So of course each channel layer also has R=G=B=1.0.

The channel layers are added using layer percent opacities of 22.2 for the Red channel layer, 71.7% opacity for the Green channel layer, and 6.1% opacity for the Blue channel layer. The percentages are the correct percentages for producing a Luminance conversion to black and white by adding the Red, Green, and Blue channels together as layers.

The result of adding the three layers should be white, R=G=B=1.0, which is what happens with the second way of adding the layers. But the first way, using the more obvious "add each layer to the layers below", produces R=G=B=0.793257.

Here's a download link for the actual XCF file: http://ninedegreesbelow.com/bug-reports/gimp29/layer-addition/white-gimpdefault.xcf

You'll need to reset *all* of the layer opacities to the values given above, because for some reason saving to disk and reopening causes layer opacities to shift slightly (for example, the Blue layer opacities shift to 5.9% instead of staying at 6.1%).

The image is an sRGB image and the precision is 32-bit floating point (linear) in order to get the layers to properly add up to R=G=B=1.0, which they should anyway for solid white, but results would be wrong for colors other than solid white or solid black.

Elle, puzzled --
http://ninedegreesbelow.com
Color management and free/libre photography _______________________________________________ gimp-user-list mailing list
List address: gimp-user-list@gnome.org List membership: https://mail.gnome.org/mailman/listinfo/gimp-user-list List archives: https://mail.gnome.org/archives/gimp-user-list

Elle Stone
2015-10-13 14:55:46 UTC (over 8 years ago)

Layer addition - bug, feature, or user misunderstanding?

On 10/13/2015 12:25 AM, Richard wrote:

The problem is that a layer's opacity doesn't add -- it multiplies, like this:

Result = (opacity) * (this layer) + (100%-opacity) * (result of layers below)

This formula holds true regardless of the layer's assigned blending mode (and it's recursive, with the "result of layers below" defined by inserting the next layer down into the same formula).

Hmm, I tried setting the layer blend mode for the three layers to Addition, Normal, Lighten only, and Dodge, and regardless of the chosen blend mode, the result was always 79%. But these identical results are because each layer is white, R=G=B=1.0f or 255 at 8-bit integer. Change to some lower value, such as R=G=B=0.5f or 128 at 8-bit integer, and the result of blending the three layers over black does depend on the chosen blend mode.

= (22.2% + 55.8% + 1.3%) * (white) = 79.3% white

Doesn't that 79% look rather familiar? :)

Many thanks! for explaining this math. The math behind Normal blend mode isn't exactly intuitively obvious, at least not to me.

But as explained below, I think that's not the right math for Addition blend mode. I think something might be wrong in the Addition layer blend mode code.

Aside - the left half of your image is totally reproducible on GIMP 2.8 . (I can't seem to reproduce the right half in 2.8, but I haven't examined the actual XCF either, so I don't have all the details.)

I uploaded a GIMP 2.8 version of the xcf file: http://ninedegreesbelow.com/bug-reports/gimp29/layer-addition/gimp28-addition.xcf

Now to fix the values ... first, Red is on top so it can keep the 22.2%; this leaves a translucency of 77.8% for everything below it. For Green, below Red, divide its opacity by Red's translucency (above): Green's opacity should be (71.7% / 77.8%) = 92.1%. This, in turn, leaves 7.9% of translucency for Blue below it. For Blue (which is below both Green and Red), divide its opacity by the overall translucencies of both Red and Green. You can do the math if you want (6.1% / 77.8% / 7.9%), but it conveniently works out to exactly 100% opacity -- i.e. Blue doesn't need any translucency for itself because with both Red and Green on top of it (at the above opacities) only 6.1% of Blue will be visible anyway.

To prove it, just plug the new opacities back into the above formula:

Image = 22.2% * (red) + 77.8% * (92.1% * (green) + (100% - 92.1%) * (100% * blue) )
= 22.2% * (red) + 77.8% * (92.1% * (green) + 7.9% * (blue) ) = 22.2% * red + 71.7% * green + 6.1% * blue

Many, many thanks! I tried your percentages with a real image rather than a solid white layer, using Normal blend mode, and your percentages produced *exactly* the right black and white image.

Here's why I think there might be a bug in the code for Addition blend mode. But maybe you can explain why it's not a bug:

Working in GIMP 2.9, change the color of the three layers that are being added together to 0.50f (the same as you'd get if you started with a 50% gray layer and dragged the three channels over to the layer stack).

The result of setting the percentages to 22.2% of the Red channel layer, 71.7% of the Green channel layer, and 6.1% of the Blue channel layer, and then setting the layer blend mode for each layer to Addition, is exactly 0.50f, which is intuitively what I would expect, and coheres with the equations:

(0.222 * Red) + (0.717 * Green) + (0.061 * Blue)

If Red=Green=Blue=0.5, the above equation simplifies to

(0.222 + 0.717 + 0.061) * 0.5 = 1.0 * 0.5 = 0.5

The corresponding Normal blend mode math that you provided produces 0.3965.

I tried the same percentages using GIMP 2.8, using R=G=B=128. Well, actually I set the percentages to the "easier to type" 22%, 72%, 6%.

In GIMP 2.8, Addition blend mode for the three layers produces R=G=B=127 or 50% (rounded by the color picker), pretty close to what I expected Addition blend mode to produce. And Normal blend mode produces R=G=B=101, or 40%, exactly as you describe the Normal blend mode math above.

In GIMP 2.9, using Addition blend mode as described produces the intuitively expected results for all shades of gray less than or equal to 55% gray (R=G=B=0.55f, or 140 for 8-bit integer).

But at 56% gray and higher, results are progressively less than I would expect, assuming my equations for Addition blend mode are correct. Instead results progressively converge on the Normal blend mode results as the color of the blended layers approaches solid white.

So does this seem like a bug in the Addition blend mode? Or is there something I'm not still not understanding about how Addition layer blend mode is supposed to work?

Best, Elle

As an aside (in case anyone is interested in using stacked channel layers to produce a black and white image), although the math is the same, the required percentages for converting to Luminance will change for other RGB color spaces. And the image needs to be in a linear gamma color space, or else the addition needs to be done on linearized RGB (as GIMP 2.9 does if you choose linear precision), otherwise you get Luma instead of Luminance.

Elle Stone
2015-10-13 15:46:11 UTC (over 8 years ago)

Layer addition - bug, feature, or user misunderstanding?

On 10/13/2015 10:55 AM, Elle Stone wrote:

So does this seem like a bug in the Addition blend mode? Or is there something I'm not still not understanding about how Addition layer blend mode is supposed to work?

Hmm, Krita produces the results I expected, that is, the three layers add up to R=G=B=1.0f.

So either GIMP intentionally uses a different algorithm for addition blend mode. Or else there's a bug in the code, most likely the same bug reported here: https://bugzilla.gnome.org/show_bug.cgi?id=744265. I'll add a comment to that bug report.

Richard
2015-10-14 03:14:47 UTC (over 8 years ago)

Layer addition - bug, feature, or user misunderstanding?

My explanation ran a bit long, but the key thing is that a layer's opacity setting is generally independent of the layer's blending. AFAIK* "Dissolve" is the only layer blending that actually uses the opacity setting in its blending; for everything else, the layer blending is performed first and the opacity setting just interpolates between that intermediate result and transparency. (* - Let me know if I missed anything.)

I tried taking a look at your XCF directly, however I'm running stable GIMP (2.8) and GIMP 2.9 makes breaking changes to the XCF format, so that's out of my range. And while I was able to reproduce the left half of your screenshot easily in GIMP 2.8, simply stuffing those layers into groups did not change the result any, leaving me unable to reproduce the right half of your screenshot (without further info).

Here's an idea you can try -- move your three layers around so they only partially overlap each other (like a Venn diagram). Then you have the ability to see the final blended result plus each layer individually.

-- Stratadrake strata_ranger@hotmail.com
--------------------
Numbers may not lie, but neither do they tell the whole truth.

Subject: Re: [Gimp-user] Layer addition - bug, feature, or user misunderstanding? To: strata_ranger@hotmail.com; gimp-user-list@gnome.org From: ellestone@ninedegreesbelow.com Date: Tue, 13 Oct 2015 11:46:11 -0400

On 10/13/2015 10:55 AM, Elle Stone wrote:

So does this seem like a bug in the Addition blend mode? Or is there something I'm not still not understanding about how Addition layer blend mode is supposed to work?

Hmm, Krita produces the results I expected, that is, the three layers add up to R=G=B=1.0f.

So either GIMP intentionally uses a different algorithm for addition blend mode. Or else there's a bug in the code, most likely the same bug reported here: https://bugzilla.gnome.org/show_bug.cgi?id=744265. I'll add a comment to that bug report.

Richard
2015-10-14 03:45:41 UTC (over 8 years ago)

Layer addition - bug, feature, or user misunderstanding?

Subject: Re: [Gimp-user] Layer addition - bug, feature, or user misunderstanding? To: strata_ranger@hotmail.com; gimp-user-list@gnome.org From: ellestone@ninedegreesbelow.com Date: Tue, 13 Oct 2015 11:46:11 -0400

So either GIMP intentionally uses a different algorithm for addition blend mode. Or else there's a bug in the code, most likely the same bug reported here: https://bugzilla.gnome.org/show_bug.cgi?id=744265. I'll add a comment to that bug report.

On an aside, I was able to build the testcase in that GTK bug report and those results were ... interesting, to say the least: created a black background layer, then two layers having gradients from black to white (one vertically and one horizontally). Set both of their blend modes to Addition, both at 100% opacity. Result - The lower right triangle of the image has been clamped at white (as expected).

Then I adjusted the opacity of the top layer down to 50%. This caused the "clamp triangle" in the lower right to dim (but still remain a solid color) -- yeah, the clamping action is clearly wrong here since at < 100% opacity there should be fewer pixels running out of gamut. Next I adjusted the middle layer's opacity down to 50% as well, which ... shrunk the aspect ratio of the clamp triangle? At 50% opacity the triangle showed a clear 2:1 aspect ratio. Despite that with both layers at 50% nothing should even be out of gamut here. I also swapped the two layers with each other, and the clamp triangle is now flipped (1:2 aspect ratio instead of 2:1). Which is also wrong because addition is a commutative operation and layer order should make no difference here.

So, yeah, interesting but still wrong results, definitely a bug when using Addition blending (and possibly others) at less than 100% opacity.

Not likely to be something I'd encounter in the wild myself -- but at least good to know it's getting patched.

-- Stratadrake strata_ranger@hotmail.com
--------------------
Numbers may not lie, but neither do they tell the whole truth.

Elle Stone
2015-10-14 10:00:05 UTC (over 8 years ago)

Layer addition - bug, feature, or user misunderstanding?

On 10/13/2015 11:45 PM, Richard wrote:

Subject: Re: [Gimp-user] Layer addition - bug, feature, or user

misunderstanding?

On an aside, I was able to build the testcase in that GTK bug report and those results were ... interesting, to say the least: created a black background layer, then two layers having gradients from black to white (one vertically and one horizontally). Set both of their blend modes to Addition, both at 100% opacity. Result - The lower right triangle of the image has been clamped at white (as expected).

Then I adjusted the opacity of the top layer down to 50%. This caused the "clamp triangle" in the lower right to dim (but still remain a solid color) -- yeah, the clamping action is clearly wrong here since at < 100% opacity there should be fewer pixels running out of gamut. Next I adjusted the middle layer's opacity down to 50% as well, which ... shrunk the aspect ratio of the clamp triangle? At 50% opacity the triangle showed a clear 2:1 aspect ratio. Despite that with both layers at 50% nothing should even be out of gamut here. I also swapped the two layers with each other, and the clamp triangle is now flipped (1:2 aspect ratio instead of 2:1). Which is also wrong because addition is a commutative operation and layer order should make no difference here.

So, yeah, interesting but still wrong results, definitely a bug when using Addition blending (and possibly others) at less than 100% opacity.

It looks like the Addition blend mode issue has been been fixed in GIMP 2.9. The error was subtle enough that it could easily go unnoticed in most use cases. The example images in the bug report are special because they added together to produce obviously wrong results, but now they add together correctly.

I probably never would have noticed anything odd about Addition blend mode, except I was working with a channel layer stack from a color image where I already knew what the result of adding the layers *should* look like. But the highlights were subtly wrong, which is why I asked on the list whether maybe Addition blend mode was supposed to do something different from what I thought it did. And I'm glad I asked - your mathematical explanation of how layer opacities work is excellent, very clear. That information should go in the GIMP documentation somewhere, if it isn't already there.

Best, Elle

Richard
2015-10-14 17:46:28 UTC (over 8 years ago)

Layer addition - bug, feature, or user misunderstanding?

I probably never would have noticed anything odd about Addition blend mode, except I was working with a channel layer stack from a color image where I already knew what the result of adding the layers *should* look like. But the highlights were subtly wrong, which is why I asked on the list whether maybe Addition blend mode was supposed to do something different from what I thought it did. And I'm glad I asked - your mathematical explanation of how layer opacities work is excellent, very clear. That information should go in the GIMP documentation somewhere, if it isn't already there.

Best, Elle

Yeah, I probably would never have encountered this "in the wild" either because layer opacity isn't something I use very often, and the clamping action is moot at 100% opacity.

-- Stratadrake strata_ranger@hotmail.com
--------------------
Numbers may not lie, but neither do they tell the whole truth.