saveImage() readImage()

@Simeon

I was playing around with saving an image (saveImage) and reading it back (readImage) and noticed something strange. Here is a hacked up example so I can show what I’m talking about. It looks like an image saved with an alpha value less than 255, when read back, won’t have ‘rgb’ values greater than the alpha value. Run this example and see that img1 has rgb values of 200, 201, 202 and alpha 255. When it’s read back into img2, img1 and img2 are the same. If you change the alpha value ‘a’ to 201 and run it again, the g value of img2 is now set to 201. If you set the alpha value to 190 then all the values are set to 190. This means that an image read back won’t have the same color values as the original if the alpha value is less than 255. I added the output as comments at the end.


function setup()
    img1=image(3,3)
    
    print(" img1\
\
 r       g       b       a\
--------------------------")
    
    for x=1,3 do 
        for y=1,3 do
            r=200
            g=201
            b=202
            a=255
            img1:set(x,y,r,g,b,a)
            print(r,g,b,a)
        end
    end
    
    saveImage('Documents:test1',img1)
    
    img2=readImage('Documents:test1')
    
    print("\
 img2\
\
 r       g       b       a\
--------------------------")
    
    for x=1,3 do 
        for y=1,3 do
            r,g,b,a=img2:get(x,y)
            print(r,g,b,a)
        end
    end 
end

function draw()
    background(40, 40, 50)
end

--[[

 img1

 r       g       b       a        alpha set to 255
--------------------------
200	201	202	255
200	201	202	255
200	201	202	255
200	201	202	255
200	201	202	255
200	201	202	255
200	201	202	255
200	201	202	255
200	201	202	255

 img2

 r       g       b       a       img2 is the same as img1
--------------------------
200	201	202	255
200	201	202	255
200	201	202	255
200	201	202	255
200	201	202	255
200	201	202	255
200	201	202	255
200	201	202	255
200	201	202	255

 img1

 r       g       b       a       alpha set to 201
--------------------------
200	201	202	201
200	201	202	201
200	201	202	201
200	201	202	201
200	201	202	201
200	201	202	201
200	201	202	201
200	201	202	201
200	201	202	201

 img2

 r       g       b       a       the values are limited to 201
--------------------------
200	201	201	201
200	201	201	201
200	201	201	201
200	201	201	201
200	201	201	201
200	201	201	201
200	201	201	201
200	201	201	201
200	201	201	201


 img1

 r       g       b       a       alpha set to 190
--------------------------
200	201	202	190
200	201	202	190
200	201	202	190
200	201	202	190
200	201	202	190
200	201	202	190
200	201	202	190
200	201	202	190
200	201	202	190

 img2

 r       g       b       a       the values are limited to 190
--------------------------
190	190	190	190
190	190	190	190
190	190	190	190
190	190	190	190
190	190	190	190
190	190	190	190
190	190	190	190
190	190	190	190
190	190	190	190


--]]

This happens because Codea writes out a premultiplied image. That is, the RGB components are pre-multiplied by the alpha value.

I’ll have to look into exactly when this happens, and whether it should happen in some cases (sometimes the image data is stored internally as premultiplied, because that is the preferred way for iOS to load images).