Fix: Ensure non-premultiplied alpha for textures#245
Open
AntonPieper wants to merge 2 commits intomarkusfisch:masterfrom
Open
Fix: Ensure non-premultiplied alpha for textures#245AntonPieper wants to merge 2 commits intomarkusfisch:masterfrom
AntonPieper wants to merge 2 commits intomarkusfisch:masterfrom
Conversation
…d improve texture management
Perform rotation manually on pixel data before cropping to avoid creating premultiplied intermediate bitmaps that could crash when handling rotated textures. Also ensure temporary rotated bitmaps are tracked and recycled, validate crop bounds, log errors with the correct tag, and create the final bitmap with the original config as non-premultiplied before setting pixels to preserve correct pixel format and avoid premultiplication issues.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request resolves a long-standing issue where textures with transparency were incorrectly converted to premultiplied alpha during various bitmap operations. This would cause visual artifacts like dark fringes around transparent edges, as the alpha channel was effectively being applied twice.
A big thank you to @SpartanJoe193 for reporting and providing detailed examples in both issues!
Closes #238, Closes #242.
The Problem
As discussed in the issues, the root cause was more complex than simply setting
inPremultiplied = falseduring the initial texture load. While that was the first step, the core issue was that any subsequent bitmap manipulation—like cropping or rotating—using standard Android methods (Canvas,Matrix) would implicitly re-premultiply the alpha channel, undoing the initial load setting and corrupting the color data in transparent areas.The Solution
To fix this, all standard bitmap operations within the texture processing pipeline have been replaced with manual, pixel-level transformations. These new methods correctly handle and preserve the non-premultiplied (straight alpha) state of the texture data through all steps:
inPremultiplied = false.Canvasand its automatic premultiplication.Note
The new methods are implemented in software and may be slower than the previous hardware-accelerated
Canvasoperations. This is a necessary trade-off for correctness. The performance of these new methods can be a target for future optimization.How to Verify the Fix
You can easily verify the fix using the test texture and shader below.
1. Test Image
Save and import the following test image. It contains three vertical bars and has an alpha gradient.
2. Test Shader
Use this shader. It displays the texture. The left half shows the correct blending for straight alpha, while the right half simulates the old, incorrect behavior.
Click to expand the example shader
3. Steps
sampleTex.Expected Result
The left side of the preview will show the complete, solid color data. The right side will simulate the old, incorrect behavior which gets darker as the alpha drops.