DEV Community

Cover image for Chasing the GHOST bug

Posted on

Chasing the GHOST bug

One day, I wanted to edit an image, to remove the background and make it transparent using Pinta, which is based on Paint .NET—a great tool for my level of image editing.

Image description

So, I was going through the options, and I came across the Layer Blend Mode "Multiply," which gave me the result that was perfect for my use.

Image description

However, the saved image still had a white background, meaning that the alpha channel was broken. After trying multiple ways to save the correct image, I resorted to downloading the source code and fixing the problem myself.

Firstly, I found the methods that handle image saving and layer flattening. I thought that the code for flattening might be different from the code for displaying. But everything looked as it should be. After some testing, I came to the conclusion that the "Multiply" Blend Mode was broken in a way that when it blends, it does its thing but makes the alpha channel be 1 in the range [0-1].

So, I dug deeper and found out that the base graphics library for Pinta is Cairo. I spent a few hours trying to figure out where the problem was, hoping that I could report a bug or even fix an important bug in such an important library.

I found nothing, and I was a bit disappointed. I went to bed, and then I got it.

Image description

The problem was not in the saved file. "Multiply" means that it multiplies channel by channel [1, 0.5, 1, 1] x [0.5, 0.5, 1, 1] -> [0.5, 0.25, 1, 1]:[R, G, B, A], so it gives a darker image. It has nothing to do with transparency.

So, the Blend Mode worked as intended.

The problem was that this operation is also applied between the canvas (transparent background pattern) and the displayed image, so it gives the illusion that the displayed image is transparent.

The solution would be to separate the layer drawing from the canvas drawing and then draw the layer drawing with the Blend Mode set to "Normal." This way, what we see is what we get in the end.

I may make a proposition to fix this, maybe fix it myself, or if anyone is up to it, they can do it and let me know when to pull the project.

All the best!

Top comments (0)