WindowDrawImageAlpha wrong color in 0 alpha regions at certain blend opacities

Posted by Fiendish on Wed 10 Jan 2018 06:07 AM — 6 posts, 21,181 views.

USA Global Moderator #0
At certain blend opacities, WindowDrawImageAlpha (and probably also WindowMergeImageAlpha) is generating the wrong blend color for 0 alpha areas.

I made a 500x500 completely empty (add alpha channel, select all, delete) png in GIMP and saved it as MUSHclient/pure_alpha.png and then used the following.
The lower half of the miniwindow visibly blinks, but the lower half shouldn't be different than the upper half because the image is completely empty.

(You might have to squint or mess with your display brightness)

As you can see in the code below, the color displayed should be 0x1c1c0c, but the bad region is showing 0x1b1b0b.


<aliases>
  <alias
   match="hst"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>
   require "wait"

   bga = "win_"..GetPluginID().."_test"
   WindowDelete(bga)

   local bgcolor = 0x1c1c0c
   WindowCreate (bga, 0, 0, 0, 0, 12, 2, bgcolor)
   WindowShow(bga, true)

   if WindowLoadImage (bga, "pure_alpha", GetInfo(66).."pure_alpha.png") == 0 then
      WindowResize(bga, WindowImageInfo(bga, "pure_alpha", 2), WindowImageInfo(bga, "pure_alpha", 3), bgcolor)
      wait.make(function()
         for i=1,100 do
            WindowRectOp(bga, 2, 0, 0, 0, 0, bgcolor, bgcolor)
            WindowDrawImageAlpha(bga, "pure_alpha", 0, 250, 0, 0, 0.0499) -- good
            Repaint()
            wait.time(0.5)
            WindowRectOp(bga, 2, 0, 0, 0, 0, bgcolor, bgcolor)
            WindowDrawImageAlpha(bga, "pure_alpha", 0, 250, 0, 0, 0.05) -- bad
            Repaint()
            wait.time(0.5)
            WindowRectOp(bga, 2, 0, 0, 0, 0, bgcolor, bgcolor)
            WindowDrawImageAlpha(bga, "pure_alpha", 0, 250, 0, 0, 0.0501) -- good
            Repaint()
            wait.time(0.5)
            WindowRectOp(bga, 2, 0, 0, 0, 0, bgcolor, bgcolor)
            WindowDrawImageAlpha(bga, "pure_alpha", 0, 250, 0, 0, 0.0157) -- bad
            Repaint()
            wait.time(0.5)
            -- WindowRectOp(bga, 2, 0, 0, 0, 0, bgcolor, bgcolor)
            -- WindowDrawImageAlpha(bga, "pure_alpha", 0, 250, 0, 0, 0.0158) -- good
            -- Repaint()
            -- wait.time(0.3)
         end
      end)
   end
</send>
</alias>
</aliases>
Amended on Wed 10 Jan 2018 06:36 AM by Fiendish
USA Global Moderator #1
And, indeed,
    printf("%x", (uint8_t)(0.05 * 0xc + (1 - 0.05) * 0xc));

prints b instead of c


( https://github.com/nickgammon/mushclient/blob/master/blending.h#L45 )

How do you feel about rounding instead of truncating in those blending functions?
https://github.com/nickgammon/mushclient/pull/40
Amended on Wed 10 Jan 2018 07:14 AM by Fiendish
Australia Forum Administrator #2
It looks OK to me. I also added it to Blend_Opacity.

See: https://github.com/nickgammon/mushclient/commit/5411e4895a3b6338
Australia Forum Administrator #3
How did you even discover this, anyway? The difference is so slight it is hard to spot. I mean I think we are talking 1/256 of a colour code difference (as in red/blue/green). If only red changed then the difference is very small.
USA Global Moderator #4
In the process of making a miniwindow color themes framework, I decided that I'd like to let the themes tint the main output background color. But the Aardwolf MUSHclient package already has an image of the Aardwolf logo darkly blended into the background sort of like a watermark, which it places there with a miniwindow flagged as underneath, and I don't want to get rid of that. So to make the main output correspondingly tint with the theme, I slightly adjusted the plugin that embeds the logo to make the background a different color than black just before blending. But the logo image was originally itself very darkly blended with black[0] (I'm not sure why I did that), so blending it with other colors was turning tricky and looked pretty bad on some colors. So I changed the logo image to be normal with a transparent background[1] instead of very black colors on a black background, because then the blending would be easier for me to figure out. But then with one of my color themes the 8% opacity blend that I'd decided on for most of the themes was still visually too bright, so I decided to lower it to 5% opacity, and then at 5% opacity I noticed (just barely) that the image didn't overlay/merge seamlessly.

[0] - https://raw.githubusercontent.com/fiendish/aardwolfclientpackage/2097ebaff6a23df38eb370d0de161c269bfd5485/MUSHclient/worlds/plugins/images/aardbg13.png
[1] - https://i.imgur.com/nzj5cb6.png
Amended on Thu 11 Jan 2018 04:45 AM by Fiendish
Australia Forum Administrator #5
And this, Ladies and Gentlemen, is how a bug report should be submitted.

  • A Minimal, Complete, and Verifiable example.

    That is: code that reproduces the problem and doesn't do anything else. (Although I notice I had to make the pure_alpha.png file myself).
  • A description about what actually happened.
  • A description about what was expected.
  • A speculation about what the problem might be.
  • Suggested code to fix it - if that is something you are comfortable about doing.