SDL2 texture blend help

Ivan Trombley itrombley at dot-borg.org
Tue Dec 12 23:28:23 UTC 2017


On Tuesday, 12 December 2017 at 07:12:07 UTC, Dmitry wrote:
> On Tuesday, 12 December 2017 at 06:27:30 UTC, Ivan Trombley 
> wrote:
>> This isn't a scaling problem (which is totally solved by
> Scaling is not a prerequisite for this problem.
>
>> pre-multiplying the alpha with the colors BTW). This is a gamma
> How did you this? Using editor or using shader? If shder, show 
> the code.
> Can you share both images? I want to check.
>
>> correction problem which is solved only by converting the 
>> color values to linear color space before compositing and then 
>> converting the final pixel back to sRGB.
>
> Are you sure in that? Because what I see is typical alpha 
> blending problem.

Here's the code that produces the correct results (exactly the 
same as GIMP):


     auto bk =IMG_Load(toStringz(folder ~ "/background.png"));
     auto fg =IMG_Load(toStringz(folder ~ "/image.png"));
     enum c = (1.0f / 255.0f);
     enum g = 2.2f;
     enum ig = (1.0f / 2.2f);
     immutable int x = (bk.w - fg.w) / 2;
     immutable int y = (bk.h - fg.h) / 2;
     auto dst = cast(ubyte*) bk.pixels + y * bk.pitch + x * 3;
     immutable size_t add = bk.pitch - fg.w * 3;
     auto src = cast(ubyte*) fg.pixels;
     const auto max = src + fg.h * fg.pitch;
     while (src < max)
     {
       const auto line = dst + fg.w * 3;
       while (dst < line)
       {
         immutable float a = c * src[3];
         dst[0] = cast(ubyte)(((1 - a) * (dst[0] * c) ^^ g + a * 
(src[0] * c) ^^ g) ^^ ig * 255);
         dst[1] = cast(ubyte)(((1 - a) * (dst[1] * c) ^^ g + a * 
(src[1] * c) ^^ g) ^^ ig * 255);
         dst[2] = cast(ubyte)(((1 - a) * (dst[2] * c) ^^ g + a * 
(src[2] * c) ^^ g) ^^ ig * 255);
         src += 4;
         dst += 3;
       }
       dst += add;
     }



More information about the Digitalmars-d mailing list