Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue] Png encode/decode not working correct #2893

Closed
bobaoapae opened this issue Nov 2, 2023 · 3 comments
Closed

[Issue] Png encode/decode not working correct #2893

bobaoapae opened this issue Nov 2, 2023 · 3 comments
Labels

Comments

@bobaoapae
Copy link

I'm having problem using the bitmapdata encode to png, and the new BitmapData.decode, the bitmapdata currently transparent lose the transparency.

public function addTexture(texture:BitmapData):void
    {
        var bitmapEncoded:ByteArray = texture.encode(texture.rect, new PNGEncoderOptions());
        var randomIdentifierBytes:ByteArray = Digest.hash(Digest.SHA256, bitmapEncoded);
        var randomIdentifier:String = Base64.encodeByteArray(randomIdentifierBytes);
        _textures.push(randomIdentifier);
        var sharedObject:SharedObject = SharedObject.getLocal("redesurftank/textures/" + randomIdentifier);
        sharedObject.data.texture = bitmapEncoded;
        sharedObject.flush();
        sharedObject.close();
    }

    public function getTexture(index:int):BitmapData
    {
        var randomIdentifier:String = _textures[index];
        var sharedObject:SharedObject = SharedObject.getLocal("redesurftank/textures/" + randomIdentifier);
        var textureBytes:ByteArray = sharedObject.data.texture;
        textureBytes.position = 0;
        var texture:BitmapData = BitmapData.decode(textureBytes);
        sharedObject.close();
        return texture;
    }
@bobaoapae bobaoapae added the Bug label Nov 2, 2023
@bobaoapae
Copy link
Author

Using PNGEncoder from as3core don't fix the problem, the problem it's on the BitmapData.decode

@johnou
Copy link

johnou commented Nov 2, 2023

iirc this is a feature

"All pixels in a BitmapData object are stored as premultiplied color values. A premultiplied image pixel has the red, green, and blue color channel values already multiplied by the alpha data. For example, if the alpha value is 0, the values for the RGB channels are also 0, independent of their unmultiplied values. This loss of data can cause some problems when you perform operations. All BitmapData methods take and return unmultiplied values. The internal pixel representation is converted from premultiplied to unmultiplied before it is returned as a value. During a set operation, the pixel value is premultiplied before the raw image pixel is set."

you could try this workaround (remember to lock and unlock the bitmap for extra speed) https://github.com/Tencent/Fanvas/blob/master/exporter_src/src/exporters/JSExporter/ShapeDataExporter.as#L186

@ajwfrost
Copy link
Collaborator

ajwfrost commented Nov 2, 2023

Actually it does look like this is a problem in the line: BitmapData.decode(textureBytes);
The byte array there contains a PNG that includes transparency, so the decoded bitmap data should also contain the same... the issue seems to be caused by the initial generation of the BitmapData object (internally) that's then returned from the static method, after the decoded PNG is drawn onto it.

Should be fixed in our next cross-platform release - within the next week or two - version 50.2.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants