Skip to main content
Participating Frequently
November 11, 2023
Question

Bug: color data is discarded on PNG roundtrip for some border pixels that have RGB data and Alpha=0

  • November 11, 2023
  • 8 replies
  • 755 views

It is very typical in the computer graphics industry to work with files that use RGBA channels separately ("data channels"). In these files, it can happen quite often that the alpha channel of a file is a mix of pixels that have alpha=0 and alpha != 0.

 

1. Download the attached file

2. Unpack

3. Import the PNG into Photoshop

4. (optional) Use Layer > Layer Mask > From Transparency to see the actual color data in the file (color pixels everywhere)

5. Export as PNG again (either with layer mask or the original, same result)

6. Open the new file in Photoshop

7. Check data with Layer > Layer Mask > From Transparency

8. Note that suddenly there's a white border around the image where the re-export as PNG has thrown away important color data. I think data loss on roundtrip of a file should be considered a bug.

 

Expected: full color data is still there after exporting as PNG again.

Actual: data loss in some regions of the image when exporting as PNG again. The original PNG file demonstrates very well that the data could stay in the file.

 

Version: Adobe Photoshop Version: 25.1.0 20231016.r.120 ca99df2 arm64

Platform: MacOS 14.0 Apple M2 Max, HybridCPU(8:4)

This topic has been closed for replies.

8 replies

Jeff Arola
Community Expert
Community Expert
November 12, 2023

Sorry i didn't see the white border at first.

 

For now the only way seems to be to use SuperPNG to retain all the data.

 

SuperPNG settings

save the original image transparency to an Alpha channel, go to Layer>Layer Mask>From Transparency

delete the layer mask. save with SuperPNG having Alpha 1 checked

 

 

The TransparentCat png saved with SuperPNG reopened in photoshop

 

 

 

TransparentCat saved with SuperPNG

 

 

 

D Fosse
Community Expert
Community Expert
November 12, 2023

Just to clear away all the obvious things here - you are viewing at 100% so that you see the actual pixel data in the file, not resampled?

_herbst_Author
Participating Frequently
November 12, 2023

@D Fosse good idea, I tried Save As as well but it also removes some of the color information on the borders of the image. So unfortunately no difference there. 

 

@Jeff Arola importing the file you attached into photoshop and looking at the transparency data as a mask shows that border as well, parts of the data have been lost on Save As:

 

Also, I'm aware of the SuperPNG plugin, but I think roundtripping PNG data should nonetheless work in Photoshop.

 

Regarding the various notes by Adobe employees, the PNG spec is there for everyone to read, and contains these relevant sections:

https://www.w3.org/TR/2003/REC-PNG-20031110/#6AlphaRepresentation

 

PNG does not use premultiplied alpha. In consequence an image editor can take a PNG image and easily change its transparency.

 

Right now I can't take that resulting PNG image and "easily change its transparency" as parts of the data are lost on export.

 

What's interesting is that the spec leaves some things as open to "use case interpretation", which is pretty annoying for a spec if you ask me, as it either requires options for users or a choice by an implementor that's then wrong for some use cases:

Section 12.4 Alpha Channel Creation https://www.w3.org/TR/2003/REC-PNG-20031110/#12Alpha-channel-creation

The alpha channel can be regarded either as a mask that temporarily hides transparent parts of the image, or as a means for constructing a non-rectangular image. In the first case, the colour values of fully transparent pixels should be preserved for future use. In the second case, the transparent pixels carry no useful data and are simply there to fill out the rectangular image area required by PNG. In this case, fully transparent pixels should all be assigned the same colour value for best compression.

 

Curiously, Photoshop implements neither of these cases here - it neither preserves fully transparent pixels for future use, nor does it assign all fully transparent pixels the same colour.

 

Another way to see that this is a bug:

  1. use Save As, the filename already switches to the newly saved file – Photoshop effectively says "the document you have open is that new one"
  2. now look at the Layer Transparency mask, all pixels are there (no border)
  3. now close the file and reopen it
  4. look at Layer Transparency again – now there's borders.

So either should the borders appear immediately or not at all, two different views on the _same file_ are wrong either way.

Jeff Arola
Community Expert
Community Expert
November 11, 2023

If you want more control when saving pngs with the transparency channel look at SuperPNG

https://www.fnordware.com/superpng/

D Fosse
Community Expert
Community Expert
November 11, 2023

@Stephen Marsh 

 

Yes, I remember that. And it wasn't just this thread; he made this point on many occasions. His point was always that Photoshop followed the correct PNG file format specification, while many other applications (with narrow user bases and their conventions) took liberties and shortcuts.

Jeff Arola
Community Expert
Community Expert
November 11, 2023

Are you using File>Export As?

 

What are the settings?

 

Here i was able to save the TransparentCat.png with and without a Layer Mask to .png and still keep the transparency plus "hidden" pixels in the transparency channel.

 

 

actual png saved with export as

 

Stephen Marsh
Community Expert
Community Expert
November 11, 2023

I remember an old thread with (former) Adobe staff member Chris Cox with detailed info on how/why Photoshop does not retain the alpha channel as a discreet separate channel and instead creates transparency for PNG images rather than writing out an RGBA file. This may be related or insightful. I'll see if I can dig it up.

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/creating-premultiplied-alpha-channels-with-png-and-other-formats/m-p/5398312#M36151

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/so-where-s-the-alpha/m-p/12889299#M637810

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/png-not-opening-with-alpha-channel/m-p/9844587#M167993

 

I couldn't find the topic that I was thinking of, but you should get the idea.

 

 

D Fosse
Community Expert
Community Expert
November 11, 2023

I don't have an answer to this specific issue - but note that Export is actually designed to discard data. That is in fact its main purpose!

 

Export is intended for web/screen/mobile devices, and the goal is to produce a file as small as possible. To do that, all non-essential data for the purpose are stripped out.

 

To retain all data you use Save As - or in case the file format specification imposes limitations, Save A Copy which retains as much as possible.