Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MedianCutQuantizer: No transparency support; no way to use just the palette calculation #4

Open
ivucica opened this issue Oct 27, 2021 · 0 comments

Comments

@ivucica
Copy link

ivucica commented Oct 27, 2021

As far as I could read, the MedianCutQuantizer's generated palette does not have space for a transparent color.

Given non-paletted img, this is what I do -- tell the quantizer to generate a paletted image, then prepend it with transparent color (I like having transparency as the zeroth color in the palette).

It makes me uneasy that dst.Set() is not skippable, given I have no use for the actual pixels, but I've chosen that's still fine as I still get a palette back:

quantizer := gogif.MedianCutQuantizer{NumColor: 255} // Up to 255 colors plus 1 space for transparency.

pal := image.NewPaletted(img.Bounds(), nil)
quantizer.Quantize(pal, img.Bounds(), img, image.ZP)

// Create a version of paletted image with color.Transparent in it. That's the first
// color so the empty image defaults to it.
palTransparent := image.NewPaletted(img.Bounds(), append(color.Palette([]color.Color{color.Transparent}), pal.Palette...))

// Now use draw.Draw() to create a version of the image which has the
// MedianCutQuantizer's palette plus transparent color.
draw.Draw(palTransparent, img.Bounds(), img, image.ZP, draw.Over)

g.Image = append(g.Image, palTransparent)                                                                                                                                                                                                                   
g.Delay = append(g.Delay, 50)                                                                                                                                                                                                                               
g.Disposal = append(g.Disposal, gif.DisposalBackground)                                                                                                                                                                                                     
g.BackgroundIndex = 0 // image.Transparent                                                                                                                                                                                                                  

draw.Draw is from image/draw, naturally. g is an image/gif.GIF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant