Hi,
I am trying to resize a gif with ImageFactory and I get a black background in half of the frames.
I tried setting the background to white as explained on the website, but it does not help.
After analysing the structure of the original image, I notices a few things:
In the modified image
Do you have any idea of the problem?
Original file

Modified Image
After digging deeper into it, I realize that for some reason the white color is not present in the problematic frames' color tables and that the first index has black instead.
Manually adding the white color in first position of the local color table makes the background come back to its white color (there is noise, but it is much better)
Any idea?
I reupload the modified image because it looks like it didn't work in the first post.

Thanks for your amazing work!
Hi @rcharb1 Just a quick note to apologise for not getting back to you on this. I'll have a dig around and try to figure out what has changed as each frame really shouldn't have it's own table.
@rcharb1 I've been having a dig and it looks like an issue with the Octree Quantizer implementation. I decoded and encoded the image using my own encoder/quantizer from within the ImageSharp and it rendered as expected.
I'll have a look and see what is different. (No promises of a fix though as I am hacking around with System.Drawing here)
@rcharb1 I'm not sure I can fix this... I had a good dig around and I cannot figure out why this is happening. I've tested several other images without issue and there's nothing wrong with the image itself. I'll experiment with using a single color table to see whether that makes a difference.
Hi, this is kind of a long shot, since I'n not familiar with the code and the quanization. but I've decided to debug it. I've split the Gif into files were each file contains only 1 frame. I've used http://gifmaker.me/exploder/ for that task.
When I've converted each frame separately, each of them was converted successfully.
Maybe I'm wrong, but each frame has its own palette (at least for specific gif), but still, the Quantizer is not being re-initialized after each frame, so in the end, it keeps collecting "junk", and the leafcount increases, and the octree node is not being cleared from its children.
As a result, the Palette of the Nth frame is a result of the N-1 frames that came before it.
If I create a new quatizer after each frame, the problem is gone.
For example, on ImageExtentions.Copy:
OctreeQuantizer quantizer = new OctreeQuantizer();
for (int i = 0; i < decoder.FrameCount; i++)
{
GifFrame frame = decoder.GetFrame(source, i);
frame.Image = quantizer.Quantize(((Bitmap)frame.Image).Clone(new Rectangle(0, 0, frame.Image.Width, frame.Image.Height), format));
((Bitmap)frame.Image).SetResolution(source.HorizontalResolution, source.VerticalResolution);
encoder.AddFrame(frame);
quantizer = new OctreeQuantizer();
}
In GifFormat.ApplyProcessor:
for (int i = 0; i < decoder.FrameCount; i++)
{
GifFrame frame = decoder.GetFrame(factoryImage, i);
factory.Image = frame.Image;
frame.Image = this.Quantizer.Quantize(processor.Invoke(factory));
encoder.AddFrame(frame);
Quantizer = new OctreeQuantizer();
}
And in GifFormat.Save:
for (int i = 0; i < decoder.FrameCount; i++)
{
GifFrame frame = decoder.GetFrame(image, i);
frame.Image = this.Quantizer.Quantize(frame.Image);
encoder.AddFrame(frame);
this.Quantizer = new OctreeQuantizer();
}
What's your opinion?
10x
Oh well done! @Saragani
Yep, that's the issue. I can fix this really easily now. Great stuff!
W00t :-)
The great thing about your fix @Saragani was that you solved a head scratcher in ImageSharp at the same time. Thanks again! 😄