Imageloader.load() and premultiply alpha


I use imageloader extension to load my 1280x800 images dynamically into gui node ,
but the data returned from imageloader.load() function has not “premultiplied alpha” , different as by atlas textures or images loaded with image.load (_, true).

The Question is: how to convert this data to “premultiplied alpha” data like by atlas or image.load (_, true).

Would you like to give me hints or examples to solve this problem ?



To convert an image with alpha, to premultiplied alpha, you need to loop over the texels and multiply the rgb values with the alpha.

Here’s a c++ snippet for comparison.

        for (uint32_t i = 0; i < width*height; ++i)
            uint32_t a = data[3];
            data[0] = (uint8_t)( (data[0] * a) / 255 );
            data[1] = (uint8_t)( (data[1] * a) / 255 );
            data[2] = (uint8_t)( (data[2] * a) / 255 );
            data += 4;

May I ask what you benefit from using imageloader extension over our builtin image.load() ?

In any case, you need to either request a feature from the author (@sergey.lerg) or implement it your self.

Something like this (untested):

local rgba = buffer.get_stream(img_buffer, "pixels")

for y=0,height-1 do
    for x=0,width-1 do
        local index = y * width * 4 + x * 4 + 1
        local a = rgba[index + 3]
        rgba[index + 0] = (self.r * a) / 255
        rgba[index + 1] = (self.g * a) / 255
        rgba[index + 2] = (self.b * a) / 255
1 Like


I recommend saving your images with already premultiplied alpha in them. Saves time on loading them without having to manually premultiplying it in the code. Alternatively you could change the shader of the image to work with non-premultiplied alphas.

It’s also possible to add an extra param in the extension to run the loop and premultiply it, implementing such feature is not too hard, I’ll accept a pull request.



If you’re loading a PNG there’s also this PNG loader extension which can premultiply alpha:



I don’t know anymore, the code with imageloader I have written about half year ago as “study case”.
I have read the forum and decided to use imageloader, because it is a bit faster
and can process both sys.load_resource data and directly PNG file data (without io. before).
I have tested it and found it good. But I have not tested it with transparent images…
Now I have to merge partial “study cases” to whole project
therefore I refactor and modularize the code.
I have replaced imageloader code with image.load at first to continue working.



I think it would really fine , to add “premultiply” functionality to your extension,
because of compatibility to other Defold image handling systematics.

I don’t know how , I use Affinity Photo/Designer to edit images.

Should i do something in this regards?



It is a good tip , I take a look at it