[BUG] Resampling images darkens colors on partially-transparent areas

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
PasswordIsntHunter2
Newbie
Posts: 19
Joined: Sun Jan 07, 2018 6:23 pm
Contact:

[BUG] Resampling images darkens colors on partially-transparent areas

#1 Post by PasswordIsntHunter2 »

Whenever I use Scale or FactorScale on an image (with the default bilinear=True), the resampling algorithm darkens some areas of the image because it's not taking transparency into account correctly when averaging. Using Nearest Neighbor sampling does not have this issue but of course Nearest Neighbor is hot garbage for actual use.

script.rpy:

Code: Select all

image test1 = "test.png"
image test2 = im.FactorScale("test.png", 1.1)

label start:

    show test1 at right
    show test2 at left

    "The gray in the source image is #8C8C8C, exactly the same as the checkered background's light gray."

    return
test.png

result: The image on the right is not resampled. The image on the left, with the outline and darkened areas, is resampled to a scale of 1.1.

Test game to reproduce the bug

...and actually, I just checked, and it looks like the normal flat gray in the resampled image, even where there's no transparency at all, gets darkened to #8A8A8A instead of #8C8C8C. Lol

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: [BUG] Resampling images darkens colors on partially-transparent areas

#2 Post by Imperf3kt »

Have you tried setting developer mode to false?

IIRC, I experienced the same thing a while back.
It was because images were being made partially transparent in developer mode and that was allowing me to see the checkerboard pattern behind the image, appearing to be darkening it.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

PasswordIsntHunter2
Newbie
Posts: 19
Joined: Sun Jan 07, 2018 6:23 pm
Contact:

Re: [BUG] Resampling images darkens colors on partially-transparent areas

#3 Post by PasswordIsntHunter2 »

That's not the issue. I've seen this before in other engines. (also I packaged the game up, and had to custom-make the checkerboard grid since it isn't included in a build, and it still occurs)

The images aren't being made partially transparent - the image on the right, which isn't resampled, displays 100% correctly, and both images are opaque in the areas where the source image is opaque. (the gray is also the exact same color as the background's gray).

The resampling step darkens areas that are partially transparent in the source (such as antialiased borders of any sprite, as seen on the circle).

Specifically, what ren'py is generating here is an image with "premultiplied alpha" - the colors in each pixel are multiplied by the alpha value of the pixel, which results in darkening. This step can be done explicitly or it can be the byproduct of a naive resampling algorithm. Not necessarily a bad algorithm, since premultiplied alpha is useful - the renderer will have to do this multiplication anyway when drawing images to the screen. But the images produced by the resampling function are being treated the same as my other (source) image, which is not premultiplied, and so the multiplication is effectively being done again, which results in darkening.

Post Reply

Who is online

Users browsing this forum: Google [Bot], LuckyT