treeform / pixie

Full-featured 2d graphics library for Nim.
MIT License
743 stars 28 forks source link

Invalid PNG buffer due to CgBI critical chunk #387

Closed chancyk closed 2 years ago

chancyk commented 2 years ago

I'm mostly just documenting a problem I ran into with Xcode when building for iOS. Happy to provide a pull request that generates an error message specific to this issue if you like.

Xcode by default will compress PNGs, I think using a proprietary pngcrush compression variant. The first critical chunk encountered is "CgBI" instead of "IHDR" and the byte length is 4 instead of 13, so the line that raises the error is:

https://github.com/treeform/pixie/blob/master/src/pixie/fileformats/png.nim#L367

The solution is to disable both compression and text metadata removal in Xcode, seen in the image below:

Screen Shot 2022-03-07 at 1 54 17 PM

Here's a reference to the compression I came across:

https://iphonedev.wiki/index.php/CgBI_file_format

And an old issue where I discovered the solution:

https://developer.apple.com/forums/thread/43372

chancyk commented 2 years ago

This might be a project implementing the decompression if someone is inspired to reproduce for pixie:

https://github.com/jakubknejzlik/cgbi-to-png/blob/master/index.js

guzba commented 2 years ago

Sorry, I don't anticipate adding support for this proprietary Apple file format any time soon. PR welcome if you can find some documentation.

chancyk commented 2 years ago

@guzba No worries. I probably won't try to implement it in the near term either but I wanted the workaround to be available if someone else happens to run into the issue. I'll go ahead an close this issue out for now. Thanks!

guzba commented 2 years ago

Makes sense. We ran into I think the HEIF file format too. There's a lot of crazy stuff out there for these tiny gains in compression. It makes sense for the giants, but for a couple programmers, trying to implement every imaginable complex format is just not a winning strategy I think. Who knows though, I certainly do not oppose adding more support if we can be sure it's working and done correctly etc.

chancyk commented 2 years ago

Yeah, I can't imagine they get much. It seems like it may be zlib compression and they might swap around the color bytes also to match what's native on the iPhone. Maybe I'll take a crack at it at some point.