Skip to content

Handle paletted images with implicit transparency values#47

Merged
Drvi merged 5 commits intomasterfrom
paletted_images_fix
Dec 28, 2021
Merged

Handle paletted images with implicit transparency values#47
Drvi merged 5 commits intomasterfrom
paletted_images_fix

Conversation

@Drvi
Copy link
Copy Markdown
Member

@Drvi Drvi commented Dec 25, 2021

When loading paletted images, the color and transparency components for the pallet are queried separately. Apparently, there can be fewer transparency values than color values, in which case the color values without a transparency values should be opaque. Previously we assumed there is equal count of color and transparency values -- this PR fixes that.

if read_as_paletted
palette_length = Ref{Cint}()
# TODO: Figure out the lenght of paletted before calling png_get_PLTEs
palette_buffer = Vector{RGB{N0f8}}(undef, PNG_MAX_PALETTE_LENGTH)
Copy link
Copy Markdown
Contributor

@t-bltg t-bltg Dec 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use png_get_palette_max(png_ptr, info_ptr) documented in http://www.libpng.org/pub/png/libpng-manual.txt, and thus avoid using PNG_MAX_PALETTE_LENGTH ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that and I can't get any other output than 0 (I did call it after png_read_image). I think I tried it before and gave up:). It is a good point, though, just don't know how to make it work. I'll keep the comment for now.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll create an issue for this. If the approach you discovered (using transformations) is faster, it would be nice to get rid of this hack:)

Copy link
Copy Markdown
Member

@johnnychen94 johnnychen94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me; can't say too much without a test sample to diagnose this.

@Drvi
Copy link
Copy Markdown
Member Author

Drvi commented Dec 26, 2021

Hey, @t-bltg, would you mind if I add https://aws1.discourse-cdn.com/business5/uploads/julialang/original/3X/5/c/5c5cdaa477dcae516c7183c3e710ef65a0bb6ab0.png as a test case? I don't have any other image where palette color count != palette transparency count

@t-bltg
Copy link
Copy Markdown
Contributor

t-bltg commented Dec 27, 2021

Hey, @t-bltg, would you mind if I add https://aws1.discourse-cdn.com/business5/uploads/julialang/original/3X/5/c/5c5cdaa477dcae516c7183c3e710ef65a0bb6ab0.png as a test case?

Of course you can ! Its size makes it ideal for testing ;)

@t-bltg
Copy link
Copy Markdown
Contributor

t-bltg commented Dec 27, 2021

I tried that and I can't get any other output than 0 (I did call it after png_read_image).

So num_palette_max can only be set in png_do_check_palette_indexes (pngtrans.c) private function.
This method is only called in png_do_read_transformations (pngrtran.c)
In turn, png_do_read_transformations is called in png_read_row (pngread.c) only if any png_ptr->transformations is set, which is not the case in our implementation, when read_as_paletted=true.

I can make it work by applying a transformation and if the whole palette computation block is moved after png_read_image in _load!.

@Drvi Drvi force-pushed the paletted_images_fix branch from ed5ba40 to ddd3c65 Compare December 27, 2021 13:02
@Drvi Drvi requested a review from johnnychen94 December 27, 2021 13:07
Co-authored-by: t-bltg <tf.bltg@gmail.com>
@Drvi Drvi merged commit 7ab4d3f into master Dec 28, 2021
@johnnychen94 johnnychen94 deleted the paletted_images_fix branch December 28, 2021 11:23
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