wxQuantize problem

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Randajad
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon May 03, 2010 7:18 am
Location: Russia
Contact:

wxQuantize problem

Post by Randajad »

Hello for all! I trying to create gif image from images so i use wxQuantize, but have a problem. Simple code:

Code: Select all

wxImage my_image;
...

wxImage image;
wxQuantize::Quantize(my_image, image, 256);

image.SaveFile("res.png"); /// image have no transparent, but it was!
What i'm doing wrong?
Last edited by Randajad on Thu Aug 23, 2012 7:12 am, edited 1 time in total.
Windows Seven x64.
Code::Blocks with TDM MinGW.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxQuantize problem

Post by doublemax »

As i see no mentioning of "alpha" or "transparency" in quantize.cpp, i would guess it's just not implemented.
Use the source, Luke!
Manolo
Can't get richer than this
Can't get richer than this
Posts: 828
Joined: Mon Apr 30, 2012 11:07 pm

Re: wxQuantize problem

Post by Manolo »

Mmmm....
Get the alpha channel with wxImage::GetAlpha()
Do quantization
Set the stored alpha with wxImage::SetAlpha()
Randajad
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon May 03, 2010 7:18 am
Location: Russia
Contact:

Re: wxQuantize problem

Post by Randajad »

Okay, it works and now i can save png after quatize. But wxGIFHandler::SaveAnimation doesn't uses alpha channel, so, i think, it's need to be transparent color in pallette. Or i goes in wrong way?
I'm need to create gif image from others images. :)
Windows Seven x64.
Code::Blocks with TDM MinGW.
Manolo
Can't get richer than this
Can't get richer than this
Posts: 828
Joined: Mon Apr 30, 2012 11:07 pm

Re: wxQuantize problem

Post by Manolo »

GIF images does not use alpha. They use a 256 colours palette.
One of the colours in the palette can be told be transparent. This is not the same as setting for each pixel a different level of transparency.
Each image of the animation has it own palette and trasnparent index.

So, it's not just a matter of adding an alpha channel to a GIF.

Have you read this?
http://docs.wxwidgets.org/trunk/classwx_image.html
Saving GIFs requires images of maximum 8 bpp (see wxQuantize), and the alpha channel converted to a mask (see wxImage::ConvertAlphaToMask). Saving an animated GIF requires images of the same size (see wxGIFHandler::SaveAnimation)
Randajad
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon May 03, 2010 7:18 am
Location: Russia
Contact:

Re: wxQuantize problem

Post by Randajad »

Tkanks!
Okay, i try to do this after quantize. It works, i see. But i have another problem. It seems, that frame doesn't clears after drawing.

Image

It seems bug in wx. When i don't use alpha, all is ok.
Windows Seven x64.
Code::Blocks with TDM MinGW.
Randajad
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon May 03, 2010 7:18 am
Location: Russia
Contact:

Re: wxQuantize problem

Post by Randajad »

Bump. Should i post this to bug trac?
Windows Seven x64.
Code::Blocks with TDM MinGW.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxQuantize problem

Post by doublemax »

It seems, that frame doesn't clears after drawing.
This is hardly a useful error description. If you draw with tranparency, the background must be in a "defined" state, e.g. usually it has to be cleared first. So you if had a blank erase background handler before to avoid flickering, you'll probably get redraw problems now.

But without seeing code what you're actually doing, it's only guessing.
Use the source, Luke!
Randajad
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon May 03, 2010 7:18 am
Location: Russia
Contact:

Re: wxQuantize problem

Post by Randajad »

Okay. I don't draw anything. Do u see the pucture? When character from picture moving, then it's leaves a trail. :(
I only have array of pictures to pass to wxGIFHandler::SaveAnimation. It's need to me to take valid gif animation from picture array.

I mean that if gif is diff-based format, then it's seems to wxwidgets don't correct makes these "diffs" when there is transparent pixels.
Windows Seven x64.
Code::Blocks with TDM MinGW.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxQuantize problem

Post by doublemax »

It's possible that this is bug. You may even be the first one to have tried this ;)
Use the source, Luke!
Randajad
Knows some wx things
Knows some wx things
Posts: 26
Joined: Mon May 03, 2010 7:18 am
Location: Russia
Contact:

Re: wxQuantize problem

Post by Randajad »

Okay. This is bug in wx.
imaggif.cpp:729:
Change

Code: Select all

buf[3] = (maskIndex != wxNOT_FOUND) ? 1 : 0;   // has transparency
To

Code: Select all

buf[3] = (maskIndex != wxNOT_FOUND) ? 1 | 2 << 2 : 0;   // has transparency
It's also need to set proper disposal flag.
http://www.u229.no/stuff/GIFFormat/

Now image goes in right way.
Image
Windows Seven x64.
Code::Blocks with TDM MinGW.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxQuantize problem

Post by doublemax »

Nice :)

If you found a bug in the wx sources (and even a fix for it), please report at http://trac.wxwidgets.org/

Also, could you show some complete, working code for what you did?
Use the source, Luke!
Post Reply