We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3392 - support for bit depths other than 32bit in IMG_SavePNG
Summary: support for bit depths other than 32bit in IMG_SavePNG
Status: RESOLVED FIXED
Alias: None
Product: SDL_image
Classification: Unclassified
Component: misc (show other bugs)
Version: 2.0.1
Hardware: All All
: P2 enhancement
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
: 3595 (view as bug list)
Depends on:
Blocks:
 
Reported: 2016-07-15 14:02 UTC by Jonathan Dowland
Modified: 2017-09-29 17:11 UTC (History)
1 user (show)

See Also:


Attachments
libpng-based implementation of IMG_SavePNG_RW (9.29 KB, patch)
2017-09-11 10:15 UTC, Fabian Greffrath
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Dowland 2016-07-15 14:02:47 UTC
Currently IMG_SavePNG always converts and writes out a 32bit/true colour PNG, regardless of the format of the input surface.

In Chocolate Doom we have 8 bit surfaces and would like to write out 8 bit PNGs. (we currently are using libpng directly to work around this)
Comment 1 Fabian Greffrath 2017-09-06 14:08:38 UTC
Is there a chance this gets addressed in the future, e.g. by actually using libpng for writing out PNG files instead of the embedded miniz stuff?
Comment 2 Sam Lantinga 2017-09-06 17:33:03 UTC
Sure, would you like to contribute a patch?
Comment 3 Fabian Greffrath 2017-09-07 19:18:56 UTC
It's on my TODO list for quite some time now, but will probably havw to wait until after the Chocolate Doom 3.0 release (which will be based on SDL2).
Comment 4 Sam Lantinga 2017-09-07 20:35:21 UTC
Okay, good luck on your release! :)
Comment 5 Fabian Greffrath 2017-09-11 10:15:46 UTC
Created attachment 2926 [details]
libpng-based implementation of IMG_SavePNG_RW

So, I had some free time over the weekend and this is what I have come up with so far. My workign branch can be found here:

https://github.com/fabiangreffrath/SDL_Image/tree/IMG_SavePNG_RW_libpng

Some remarks:

1) I am not sure if libpng requires to adapt the target color space "png_format" based on system endianess, so I have just kept this code for both implementations. Unfortunately, I don't have access to a big-endian system to test this myself.

2) I'd prefer to get rid of the setjmp()-based error handling voodoo (which I frankly speaking don't understand) and instead add two static functions for error handling, i.e. logging with SDL_LogWarn() and SDL_LogError(), respectively. However, I hesitated to do this yet, because it would be orthogonal to the initial purpose of the patch.

3) This is an early implementation and I am open for suggestions. I have not yet commented or restructured the code, so if you feel that some passages need more tuning, please tell me.

 - Fabian
Comment 6 Sam Lantinga 2017-09-12 07:15:36 UTC
This is a great first pass.

I committed it:
https://hg.libsdl.org/SDL_image/rev/4076ac7cf0a1

I'll leave this bug open, feel free to attach additional patches as you iterate and close it when you're done once I've applied your patches.

Thanks!
Comment 7 Sam Lantinga 2017-09-12 07:18:02 UTC
*** Bug 3595 has been marked as a duplicate of this bug. ***
Comment 8 Fabian Greffrath 2017-09-28 20:04:11 UTC
I have just tested on a Debian porter box (pizzetti which is a ppc64) and can confirm that SDL_PIXELFORMAT_RGBA8888 is the correct pixel format for this system.
Comment 9 Sam Lantinga 2017-09-29 14:40:37 UTC
Fabian, I'm going to close out this bug, but feel free to attach more patches as you refine it.

Thanks!
Comment 10 Fabian Greffrath 2017-09-29 17:11:14 UTC
Fine, thanks!