Image filtering framework

I got tired of only being able to use tint, so I made this. To see the list of filters I have made and add your own go to imgcomp:filterfunctions().

This should be used sparingly, as it is somewhat inefficient.

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
thingy =  imgcomp:import(readImage("Planet Cute:Character Boy"))
thingy = imgcomp:filter(thingy,{"contrast",2},{"shift"},{"offset",4,55,-128}) -- try
thinger = imgcomp:export(thingy)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    sprite(thinger,333,333,333)

end

--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@--

imgcomp = class()

function imgcomp:export(file)
    local index = image(file.dim.x,file.dim.y)
    local r,g,b,a = 0,0,0,0
    for i = 1,file.dim.x do
        for j = 1,file.dim.y do
            if file.dat[i][j] ~= 0 then
                r,g,b,a = unpack( file.dat[i][j])
            index:set(i,j,r,g,b,a)
            end
        end
    end
return index
end

function imgcomp:import(index)   
    local file = {}
    file.dim = vec2(index.width,index.height)
    file.dat = {}
    local r,g,b,a = 0,0,0,0
    for i = 1,file.dim.x do
        file.dat[i] = {}
        for j = 1,file.dim.y do
            file.dat[i][j] = 0            
            r,g,b,a = index:get(i,j)
                file.dat[i][j] = {r,g,b,a}
        end
    end    
return file
end

function imgcomp:filter(file,data1,data2,data3,data4)    
     local r,g,b,a = 0,0,0,0
    for i = 1,file.dim.x do
        for j = 1,file.dim.y do
            if file.dat[i][j] ~= 0 then
                r,g,b,a = unpack( file.dat[i][j])                               
                if data1 ~= nil then
                    r,g,b,a =  imgcomp:filterfunctions(r,g,b,a,data1)    
                end                           
                if data2 ~= nil then
                    r,g,b,a =  imgcomp:filterfunctions(r,g,b,a,data2)    
                end                          
                if data3 ~= nil then
                    r,g,b,a =  imgcomp:filterfunctions(r,g,b,a,data3)  
                end                           
                if data4 ~= nil then
                    r,g,b,a =  imgcomp:filterfunctions(r,g,b,a,data4)    
                end            
                file.dat[i][j] ={r,g,b,a}
            end
        end
    end
    return file
end



    
    
function imgcomp:filterfunctions(r,g,b,a,filterdata)
  local function rt(input)  return math.min(255,math.max(0,math.floor(input+0.49))) end
local mode = filterdata[1]    
local aa = filterdata[2]
local bb = filterdata[3]
local cc = filterdata[3]
local dd = filterdata[4]
local ee = filterdata[5]

 if mode == "invert" then
    r,g,b = 255-r,255-g,255-b
 end

if mode == "shift" then
    if aa == nil then aa = 1 end
    for i = 1, aa do
        r,g,b = g,b,r 
    end
end

if mode == "offset" then
    if aa == nil then aa = 0 end
    if bb == nil then bb = aa
    cc = aa end
    r,g,b = r+aa, g+bb, b+cc
end

if mode == "contrast" then 
    local function bip(one,two) return 128+(one-128)*two end
    
    r,g,b = bip(r,aa),bip(g,aa),bip(b,aa)
end

if mode == "grayscale" then
    local l = (r+g+b)/3
    r,g,b=l,l,l
end

return rt(r), rt(g), rt(b), rt(a)
end

Did you see the way I did the effects, @KMEB?

http://twolivesleft.com/Codea/Talk/discussion/1048/plasma-generator-some-effects

I used a lookup table for contrast to improve speed. I also applied contrast and brightness in the same operation.

This is designed for one- time use at setup, not on a per frame basis.