-
-
Notifications
You must be signed in to change notification settings - Fork 21.2k
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
Fix grayscale alpha for Image::convert
FORMAT_L8
using REC.709
#77456
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you squash the commits to one?
If you can reference my branch, I put it here. https://github.com/V-Sekai/godot/commits/grayscale-fix-77393 |
I tried and it wouldn't let me do it locally because I had already pushed the first one up to my repo (origin). Geez, I've been using git since 2008 and I still struggle with the basics... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The substantive changes look great to me! I agree with Fire about dropping the extra comment though
d2a9ea2
to
a7caaf5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worked on git on discord with Kory.
a7caaf5
to
00cefa9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Worked with Kory on discord.
wofs[0] = (uint8_t)((luminance * rgba[3] + 255) >> 8U);
is now correct.
Image::convert
FORMAT_L8
using REC.709
Just noticed it should be 255U, not sure if it will really matter but is it worth changing again? @fire also mention about the formatting of the comment after the fact as well... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me as is.
If you want to push one more change to fix the missing "U" on 255 you can. If you are making one more change you might as well also change the comment to // REC.709.
to fit with our comment style guide.
That being said, neither change is really important here, but having perfect style is nice.
9343532
to
3ebebf9
Compare
Comment and unsigned issue fixed. Thanks for the help everyone, this has been a learning experience and I hope to make many more contributions, currently working on a couple more. |
After looking at this again, I am realizing that this is a bigger change in behaviour than I thought. This essentially applies pre-multiplied alpha to any texture when doing a conversion from a color format with alpha to a greyscale format without alpha (e.g RGBA -> L8). The current behaviour averages the color channels and writes them out directly, ignoring alpha. The new behaviour premultiplies the alpha when converting. Seeing as |
So you are implying that the true fix for this is in Also, Is this code actually correct for FORMAT_LA8 (only 1 output byte and it will only store the alpha and no luminance)?
So where does that leave us? What is the proper fix? |
OK, it looks like the Anyhow, the behavior shown in the attached issue is not expected whatsoever and I think we can all agree on that. |
The screenshots you post in the issue look correct for images with If you don't utilize the alpha channel, then it doesn't make sense to apply In your MRP, you can get the correct output by checking "Premultiply Alpha" and unchecking fix alpha border Of course your switch to REC.709 is more accurate as this screenshot is taken with the old grayscale conversion code. In my opinion, the "correct" fix here is to ensure that premultiply_alpha works with LA8 (not just RGBA8) and to remove the premultiplication this PR adds in convert. I don't think that convert should be automatically applying a premultiplication, that should only be done if the user requests it, otherwise you take options away from the user Here is a choice matrix that illustrates my concern
If you automatically premultiply in I dont think we should assume that every user wants a premultiplied image when converting from RGBA8 to L8 or LA8 to L8 |
The MRP was created only to show the issue. I ran into this issue because of the SpriteFramesEditor code when it loads a spritesheet I was working on code to autoslice the spritesheet and needed to generate a binary image from a grayscale. When I used the convert function I was getting that weird output and was unable to generate contours because of the So I think that So, where is the true fix here?
|
I think I am missing something here. Why do you insist on enabling The problem is not with |
So long story short, because I'm making a tool that should work with default settings both |
@clayjohn and I spoke over DM in RocketChat rather than here. clayjohn recommends removing the alpha multiplication for 8-bit grayscale (FORMAT_L8) and instead have users premultiply_alpha first if they want a clean grayscale image. If others agree with him here then I will modify the PR to do that and I will also unlink my issue (because this PR would no longer be a fix for my issue). We both agree that the current grayscale method is wrong. @clayjohn feel free to add anything else you can think of and let me know what you guys would want me to do. |
…T_L8) while using REC.709, with added test case
564f17b
to
c8cac39
Compare
I had to correct the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current set of changes seems great to me! Thanks for bearing with me and being open to feedback.
Image::convert
FORMAT_L8
using REC.709Image::convert
FORMAT_L8
using REC.709
Thanks! And congrats on your first merged Godot PR! |
Thanks everyone and I hope to make many more contributions in the future. I had many growing pains with this first one, and it is good to learn the process. Take care and thanks again! |
This PR fixes the "averaging" method previously and correctly uses REC.709 as indicated in the previous TODO comment.
constexpr
was added to themax_bytes
because this can all be determined at compile time.A new test case was added to convert RGBA values to L8 and then make sure the correct values were received.
Bugsquad edit: