Re-saving 16bit PNG image with straight alpha multiple times corrupts the image #99701

Open
opened 2022-07-14 15:07:56 +02:00 by quvr · 14 comments

System Information
Operating system: Windows 10
Graphics card: AMD

Blender Version
Broken: 3.2.1
Worked: ?

Short description of error

Re-saving 16bit PNG image with straight alpha multiple times corrupts the image.
As far as I can tell it's broken only for 16/32bit images and works for 8bit

Exact steps for others to reproduce the error
Texture.zip

  1. Open the file, paint something half-transparent (optionally)
  2. Press "Save image"
  3. Press "Reload image"
  4. Image should look different than before saving
**System Information** Operating system: Windows 10 Graphics card: AMD **Blender Version** Broken: 3.2.1 Worked: ? **Short description of error** Re-saving 16bit PNG image with straight alpha multiple times corrupts the image. As far as I can tell it's broken only for 16/32bit images and works for 8bit **Exact steps for others to reproduce the error** [Texture.zip](https://archive.blender.org/developer/F13292406/Texture.zip) 1) Open the file, paint something half-transparent (optionally) 2) Press "Save image" 3) Press "Reload image" 4) Image should look different than before saving
Author

Added subscriber: @Quver

Added subscriber: @Quver

Added subscriber: @Memento

Added subscriber: @Memento
Member

Added subscriber: @OmarEmaraDev

Added subscriber: @OmarEmaraDev
Member

Changed status from 'Needs Triage' to: 'Needs User Info'

Changed status from 'Needs Triage' to: 'Needs User Info'
Member

Testing a 32-bit OpenEXR image, it seems the difference is just alpha premultiplication, which seems to the expected behavior. Is this the difference you are experiencing or is there something else?

Testing a 32-bit OpenEXR image, it seems the difference is just alpha premultiplication, which seems to the expected behavior. Is this the difference you are experiencing or is there something else?
Author

@OmarEmaraDev you're right the issue is only with alpha channel, colors seems intact. I'm sorry I did not specify a format to export because my blender is configured to save as 16bit png RGBA by default (I think?) so I assumed your export will be in png too. No need to choose any other format to reproduce this. Just open the file and press alt+s, alt+r multiple times. The original file is 16bit png RGBA as well so I expect it to look the same without any changes, but in result the alpha channel seems wrong as you mentioned. Same thing when export to exr.

I think this is still a bug.
If you save the file as 8bit png there are no issues. If I paint something on that texture and want to export it back there are no ways to do this without converting it to 8bit so it looks the same as in blender. Or at least I couldn't find a way to do so. Maybe I'm missing something?

A video just in case
1.mp4

@OmarEmaraDev you're right the issue is only with alpha channel, colors seems intact. I'm sorry I did not specify a format to export because my blender is configured to save as 16bit png RGBA by default (I think?) so I assumed your export will be in png too. No need to choose any other format to reproduce this. Just open the file and press alt+s, alt+r multiple times. The original file is 16bit png RGBA as well so I expect it to look the same without any changes, but in result the alpha channel seems wrong as you mentioned. Same thing when export to exr. I think this is still a bug. If you save the file as 8bit png there are no issues. If I paint something on that texture and want to export it back there are no ways to do this without converting it to 8bit so it looks the same as in blender. Or at least I couldn't find a way to do so. Maybe I'm missing something? A video just in case [1.mp4](https://archive.blender.org/developer/F13298407/1.mp4)
Member

Changed status from 'Needs User Info' to: 'Needs Developer To Reproduce'

Changed status from 'Needs User Info' to: 'Needs Developer To Reproduce'
Member

I see. You mentioned 32bit so I assumed you were talking about EXR images.
I can reproduce the issue, and it doesn't seem to be normal alpha premultiplication and I can see the option being set to Straight Alpha.
But I also don't understand the behavior, so tagging the module for more information.
Might be related to #99186.

I see. You mentioned 32bit so I assumed you were talking about EXR images. I can reproduce the issue, and it doesn't seem to be normal alpha premultiplication and I can see the option being set to Straight Alpha. But I also don't understand the behavior, so tagging the module for more information. Might be related to #99186.
Omar Emara changed title from "Save image" weird color management to Re-saving 16bit PNG image with straight alpha multiple times corrupts the image 2022-07-18 16:55:04 +02:00

Added subscriber: @Vyach

Added subscriber: @Vyach

This comment was removed by @Vyach

*This comment was removed by @Vyach*

This comment was removed by @Vyach

*This comment was removed by @Vyach*

Added subscriber: @crantisz

Added subscriber: @crantisz

This is not a color management issue. This is an Issue related to alpha pre-multiplication.

Here is the case. I saved one render in EXR PNG and PNG16.

EXR is good for image compositing, so that we know that it has pre-multiplied alpha. If we load it into compositor, it shows correct results, and we can note that alpha setting in image node is premultiplied by default.

image.png

But we can say what image is straight, so that Blender multiply RGB channels by alpha:

image.png

PNG16 we know has straight alpha. Blender indicates that and set alpha setting to straight by default. So that we lost glowing effect, as expected.

image.png

We can force image to be pre-multiplied by switching the setting. And in this case we can see the white border around cubes as an artifact of, shall we say, "de-premultiplication" as expected:

image.png

Now load PNG8. It loads as premultiplied even alpha setting is straight by default:

image.png

So we know that PNG8 has straight alpha. And Blender shows it as straight alpha correctly. But what is incorrect, the image itself. In fact, Blender makes "de-premultiplication" under the hood and PNG8 with alpha set to straight displays as pre-multiplied. And we can be convinced of that by changing alpha to premultiplied revealing double "de-premultiplication" (One from loading image and one because of PNG specification) or double dividing original pixels by alpha:

image.png

So in order to get correct representation of PNG8 we need to add additional alpha convert step:

image.png

So looks like this hidden "de-premultiplication" process is made for image texturing. I don't know that's happening under the hood, but it looks like that in texturing PNG's functions as straight, even then the whole "pipeline" uses pre-multiplied. So somewhere in the past someone decided to make PNG8 for texturing and PNG16 for compositing by doing this weird thing under the hood, and making this headache for future us, so that we struggle with different PNG's in one application.

Images:

test.png

test16.png

test.exr

This is not a color management issue. This is an Issue related to alpha pre-multiplication. Here is the case. I saved one render in EXR PNG and PNG16. EXR is good for image compositing, so that we know that it has pre-multiplied alpha. If we load it into compositor, it shows correct results, and we can note that alpha setting in image node is premultiplied by default. ![image.png](https://archive.blender.org/developer/F13859779/image.png) But we can say what image is straight, so that Blender multiply RGB channels by alpha: ![image.png](https://archive.blender.org/developer/F13859783/image.png) PNG16 we know has straight alpha. Blender indicates that and set alpha setting to straight by default. So that we lost glowing effect, as expected. ![image.png](https://archive.blender.org/developer/F13859793/image.png) We can force image to be pre-multiplied by switching the setting. And in this case we can see the white border around cubes as an artifact of, shall we say, "de-premultiplication" as expected: ![image.png](https://archive.blender.org/developer/F13859799/image.png) Now load PNG8. It loads as premultiplied even alpha setting is straight by default: ![image.png](https://archive.blender.org/developer/F13859807/image.png) So we know that PNG8 has straight alpha. And Blender shows it as straight alpha correctly. But what is incorrect, the image itself. In fact, Blender makes "de-premultiplication" under the hood and PNG8 with alpha set to straight displays as pre-multiplied. And we can be convinced of that by changing alpha to premultiplied revealing double "de-premultiplication" (One from loading image and one because of PNG specification) or double dividing original pixels by alpha: ![image.png](https://archive.blender.org/developer/F13859822/image.png) So in order to get correct representation of PNG8 we need to add additional alpha convert step: ![image.png](https://archive.blender.org/developer/F13859826/image.png) So looks like this hidden "de-premultiplication" process is made for image texturing. I don't know that's happening under the hood, but it looks like that in texturing PNG's functions as straight, even then the whole "pipeline" uses pre-multiplied. So somewhere in the past someone decided to make PNG8 for texturing and PNG16 for compositing by doing this weird thing under the hood, and making this headache for future us, so that we struggle with different PNG's in one application. Images: ![test.png](https://archive.blender.org/developer/F13859844/test.png) ![test16.png](https://archive.blender.org/developer/F13859845/test16.png) ![test.exr](https://archive.blender.org/developer/F13859842/test.exr)

image with straight alpha, it have very different alpha and color channel, but it loaded as premultiplied
png8 wrong reading.blend

image with straight alpha, it have very different alpha and color channel, but it loaded as premultiplied [png8 wrong reading.blend](https://archive.blender.org/developer/F13859866/png8_wrong_reading.blend)
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
5 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#99701
No description provided.