Image save and load

Slow, primitive, and wasteful but it works



--image save and load
function setup()
    --make image
    myimage = image(10,10)
    for i = 5, 10 do
        for j = 1, 10 do
            myimage:set(i,j,math.random(256),math.random(256),math.random(256),255)
        end
    end   
    --saveimage
    saveimage = myimage.width .. "," .. myimage.height .. ","
    for i = 1, myimage.width do
        for j = 1, myimage.height do
            a,b,c,d = myimage:get(i,j)
            saveimage = saveimage .. a .. "," .. b .. "," .. c .. "," .. d .. ","
        end
    end    
    saveLocalData("x",saveimage)
    --loadimage
    loadimage = readLocalData("x")
    k = 1
    l = string.find(loadimage,",",k)
    lw = string.sub(loadimage,k,l-1)
    lw = lw + 0
    k = l + 1
    l = string.find(loadimage,",",k)
    lh = string.sub(loadimage,k,l-1)
    k = l + 1
    loadedimage = image(lw,lh)
    for i = 1, lw do
        for j = 1, lh do
            l = string.find(loadimage,",",k)
            a = string.sub(loadimage,k,l-1)
            k = l + 1
            l = string.find(loadimage,",",k)
            b = string.sub(loadimage,k,l-1)
            k = l + 1
            l = string.find(loadimage,",",k)
            c = string.sub(loadimage,k,l-1)
            k = l + 1
            l = string.find(loadimage,",",k)
            d = string.sub(loadimage,k,l-1)
            k = l + 1
            loadedimage:set(i,j,a,b,c,d)
        end
    end    
end

function draw()
    rect(0,0,10,10)
    background(0, 0, "095")
    scale(10)
    sprite(myimage,10,10)
    sprite(loadedimage,30,30)
end


Edit: that’s not a comment on the image command, it’s a comment on this method I wrote.

This is a bridge until a real format can be used.

Nice work, we’ll look at adding special overloads to saveLocal/ProjectData to automatically serialize images.

“just” make them 8-bit clean - images aren’t the only binary data people will want to save.

Or - sharing: https://gist.github.com/1436940

I can’t take credit for that one - just found it and put it up.

Class created

http://ipad41001.posterous.com/temporary-image-store-class

FWIW - the base64 class I linked to up there doesn’t seem to work. Drat.