Coder Social home page Coder Social logo

Comments (11)

RandyGaul avatar RandyGaul commented on May 6, 2024 1

Yep looks like a bug with paletted images. Thank you very much for making the reproduction! I'll have this fixed soon enough and add you as a contributor :)

from cute_headers.

RandyGaul avatar RandyGaul commented on May 6, 2024 1

Looks like the bug wasn't too hard to fix. That depalette code comes from Richard Mittons tigr library, and I don't think he ever bothered to support images saved with a tRNS chunk. But, since tiny images that have a palette can also save some space by placing alpha into a tRNS chunk, I suppose it makes sense to add some support for it.

Okay so here were some of my findings from RFC 2083:

For color type 3 (indexed color), the tRNS chunk contains a
series of one-byte alpha values, corresponding to entries in
the PLTE chunk:

	Alpha for palette index 0:  1 byte
	Alpha for palette index 1:  1 byte
	... etc ...

So I've updated the depalette function to use the tRNS chunk if one exists. Otherwise it the RFC suggests to do the old c ? 255 : 0 trick:

Even if the decoder author does not wish to implement true
compositing logic, it is simple to deal with images that contain
only zero and one alpha values.  (This is implicitly true for
grayscale and truecolor PNG files that use a tRNS chunk; for
indexed-color PNG files, it is easy to check whether tRNS contains
any values other than 0 and 255.)  In this simple case,
transparent pixels are replaced by the background color, while
others are unchanged.  If a decoder contains only this much
transparency capability, it should deal with a full alpha channel
by treating all nonzero alpha values as fully opaque; that is, do
not replace partially transparent pixels by the background.  This
approach will not yield very good results for images converted
from associated-alpha formats, but it's better than doing nothing.

from cute_headers.

zacharycarter avatar zacharycarter commented on May 6, 2024 1

Not at all! The images might be an issue - as they're from http://oryxdesignlab.com/products/16-bit-sci-fi-tileset which is commercially licensed to my knowledge.

I created bindings for Nim to this library. I haven't put them up yet, as they're unfinished at the moment, but I just tried out your fix and everything seems to be working great! Thanks so much for the help!

from cute_headers.

zacharycarter avatar zacharycarter commented on May 6, 2024

I guess it's a little more complicated than that? Seems each image is saved with a white background as well, even if the original image has a transparent BG?

from cute_headers.

RandyGaul avatar RandyGaul commented on May 6, 2024

Here's the white default. And here is the memcpy over the white default with each individual image.

In PNG pixels are stored in DEFLATE format, so there's called to inflate them here. I don't think it's possible for the decoder to go from transparent to white.

The only other place those pixels are modified is in unfilter, depalette, and convert. Scanning over these they are just reading from source directly, not modifying color. If a bug were here in these 3 there would probably be very drastic visual effects.

So looks like the empty atlas portions are defaulted to white, and individual images are not touched as far as I can tell. I can double check at home.

from cute_headers.

RandyGaul avatar RandyGaul commented on May 6, 2024

For now go ahead and try adding in a define for default background color and see if that works. Feel free to re-open the issue if needed! :)

from cute_headers.

RandyGaul avatar RandyGaul commented on May 6, 2024

Oh one more thing, I am doing premultiplied alpha here. Maybe this produced an unexpected result somehow? Perhaps this should be hidden behind a #define option...

from cute_headers.

zacharycarter avatar zacharycarter commented on May 6, 2024

Okay so here are my findings @RandyGaul :

  1. Commenting out the call to premultiply alpha does not fix the problem.
  2. I believe maybe this - https://github.com/RandyGaul/tinyheaders/blob/master/tinydeflate.h#L1834 - assignment to the alpha component might be the issue?

I've attached a small self-contained example you should be able to compile with gcc / clang. In this example I'm trying to pack four images with transparent backgrounds, yet the resulting image has a white background. Even if I set the background color for the image from 0xFFFFFFFF to 0x00000000, the images themselves are still composited with a white background. I'm still not 100% sure why. Hopefully this example helps get to the root of the problem.

example.zip

I'm unable to reopen the issue, I could create a new one if desired.

from cute_headers.

RandyGaul avatar RandyGaul commented on May 6, 2024

Interesting I'll take a look. That line should only be affecting png files saved with a palette... Hmm.

from cute_headers.

zacharycarter avatar zacharycarter commented on May 6, 2024

No problem, glad I could help reproduce it! Thanks for the great set of libraries!

from cute_headers.

RandyGaul avatar RandyGaul commented on May 6, 2024

Also added your nice bind file + test images to examples folder. Hope you don't mind! If it's a problem I can remove it or change the images :)

from cute_headers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.