Skip to content

cgifsave: limit max number of colours to 255 #2863

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

Merged
merged 2 commits into from
Jun 19, 2022

Conversation

dloebl
Copy link
Contributor

@dloebl dloebl commented Jun 13, 2022

Limit the maximum number of colours to 255, so we always leave one index free for the transparency.
Avoids the artefact mentioned here: #2852 (comment)

@lovell
Copy link
Member

lovell commented Jun 13, 2022

Thanks for the PR Daniel. A quick thought: could setting a bitdepth of 7 result in 129 palette entries (128+transparency)?

@dloebl
Copy link
Contributor Author

dloebl commented Jun 14, 2022

Hi @lovell. Good question!
If a bitdepth of 7 is specified with this branch we will create a colour palette with a maximum of 128 entries and set the transparency index to the number of colours:

int trans = has_transparency ? 0 : n_colours;

In this case, the valid indices are 0-127 for the image data and 128 is the transparency index.
cgif supports transparency indices that are outside of the active colour palette, but the highest possible index is 255 of course.

As far as I can tell, gifsicle and ImageMagick handle this the same way and allow the transparency index to be exactly one position outside of the active colour palette.

libvips chooses the transparency index and provides the transparent areas (so which areas are identical to the frame before) to cgif as CGIF_FRAME_ATTR_HAS_SET_TRANS is set (CGIF_FRAME_GEN_USE_TRANSPARENCY is ignored by cgif as libvips provides the transparency information). It is necessary that libvips provides the transparency information to support the interframe-maxerror parameter.

With this branch I see:

vips copy earth.gif[n=-1] 7.gif[bitdepth=7,reoptimise=1]

earth.gif

bitdepth 8 bitdepth 7 bitdepth 1
earth.gif 7 1

bitdepth 1 might look unexpected at first because sometimes one can see 3 colours. The colours per frame are limited to 2, but the transparency might cause pixels to shine through.

@jcupitt jcupitt merged commit 905a6fe into libvips:master Jun 19, 2022
@jcupitt
Copy link
Member

jcupitt commented Jun 19, 2022

Thanks @dloebl !

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

Successfully merging this pull request may close these issues.

3 participants