Skip to content

Allow RenderTexture to save out non-premultiplied alpha images #19781

@rh101

Description

@rh101
  • cocos2d-x version: 3.17.1
  • devices test on: Win32
  • developing environments
    • NDK version: N/A
    • Xcode version: N/A
    • VS version: VS2019

Steps to Reproduce:

The current implementation of RenderTexture has several issues.

Firstly, it does not set the premultiplied alpha flag on the image created by this method:

Image* RenderTexture::newImage(bool flipImage)
{
...
            image->initWithRawData(buffer, savedBufferWidth * savedBufferHeight * 4, savedBufferWidth, savedBufferHeight, 8);
        }
        else
        {
            image->initWithRawData(tempData, savedBufferWidth * savedBufferHeight * 4, savedBufferWidth, savedBufferHeight, 8);
        }
...
}

The correct code would be to change the initWithRawData calls to this:

            image->initWithRawData(buffer, savedBufferWidth * savedBufferHeight * 4, savedBufferWidth, savedBufferHeight, 8, true);
        }
        else
        {
            image->initWithRawData(tempData, savedBufferWidth * savedBufferHeight * 4, savedBufferWidth, savedBufferHeight, 8, true);

Secondly, there is no way to choose if you want to save the file as a non-premultiplied alpha PNG.

Reversing the PMA before saving the image is possible, and a new method can be added to do this, such as: RenderTexture::saveToFileAsNonPMA().

I'll create a pull request with the changes, along with modification of the tests to cater for this new method.

This fix may work for the following reported issues:
#18980
#18193

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions