Image Manipulation: Heres a start...

Ive talked about creating something that can create images and manipulate on a pixel level, with getPixel and setPixel support.
The following is a a bit of a hack… but maybe it will be useful for someone.

Here is a GFXClass … save it as GFXClass.lua

  
module( ..., package.seeall )  
--====================================================================--  
-- GFXCLASS  
--[[ Version: 1.0  
 - Made by JTullin 2012  
 - This class is free to use, feel free to change.  
--]]  
  
local picView = display.newGroup()  
local bitMap = {}  
local \_wid = 100  
local \_hei =100  
local \_backColor = {255,255,255}  
  
local \_maxWid = 1023  
local \_maxHei = 767  
local \_group  
function createImage(caller,w,h,r,g,b)  
 if w and h then  
 if w \>=0 and w \<= 1024 and h \>=0 and h \<= \_maxHei then  
 \_wid = w;  
 \_hei = h;  
 \_backColor.r =r  
 \_backColor.g =g  
 \_backColor.b =b  
 else  
 return false  
 end  
 else  
 return false  
 end  
end  
function setPixel( caller,x,y, r,g,b)  
 if x\>= 0 and x\<=\_maxWid and y\>=0 and y\<=\_maxHei then  
 --range is OK  
 if not(bitMap[y\*\_wid + x]) then bitMap[y\*\_wid + x] ={r=0,g=0,b=0} end  
 local pixel  
 pixel = bitMap[y\*\_wid + x]  
 pixel.r = r  
 pixel.g = g  
 pixel.b = b  
 return true  
 else  
 return false   
 end  
end  
function getPixel( caller,x,y)   
 if x\>= 0 and x\<=\_wid and y\>=0 and y\<=\_hei then  
 --range is OK  
 if bitMap[y\*\_wid + x] then  
 --print("values",bitMap[y\*\_wid + x].r, bitMap[y\*\_wid + x].g, bitMap[y\*\_wid + x].b)  
 return bitMap[y\*\_wid + x].r, bitMap[y\*\_wid + x].g, bitMap[y\*\_wid + x].b  
 else  
 --print("returning background")  
 return \_backColor.r, \_backColor.g, \_backColor.b;  
 end  
 return bitMap[y\*\_wid + x].r, bitMap[y\*\_wid + x].g,bitMap[y\*\_wid + x].b;  
 else  
 --print("returning out of range")  
 return \_backColor.r,\_backColor.g,\_backColor.b;  
 end  
end  
function fillRect( caller,x,y, w,h,r,g,b)  
 if x\>= 0 and x\<=\_maxWid and y\>=0 and y\<=\_maxHei then  
 local xx,yy  
 local extentX,extentY  
 extentX = x + w  
 if extentX \>= \_wid then extentX = \_wid-1 end  
 extentY = y + h  
 if extentY \>= \_hei then extentY = \_hei-1 end  
  
 for xx = x, extentX,1 do  
 for yy = y,extentY,1 do  
 setPixel( caller,xx,yy, r,g,b)  
 end  
 end  
  
 end  
end  
function drawLine(caller,x0,y0,x1,y1,r,g,b )  
  
 local dx = math.abs(x1-x0)  
 local dy = math.abs(y1-y0)   
 if x0 \< x1 then   
 sx = 1   
 else   
 sx = -1  
 end  
  
 if y0 \< y1 then   
 sy = 1   
 else   
 sy = -1  
 end  
  
 local err = dx-dy  
 local z = 0  
 while z == 0 do  
 setPixel( caller,math.floor(x0),y0, r,g,b)  
  
 if ((x0 == x1) and (y0 == y1)) then  
 z = 10000  
 end  
  
  
 e2 = 2\*err  
 if e2 \> -dy then   
 err = err - dy  
 x0 = x0 + sx  
 end  
 if e2 \< dx then   
 err = err + dx  
 y0 = y0 + sy   
 end  
 end   
  
  
end  
function renderdisplayGroup( )   
 --returns a displayGroup  
 local xx,yy  
local extentX,extentY  
local aRect  
local pixel  
 \_group = display.newGroup()  
 for xx = 0,\_wid,1 do  
 for yy = 0,\_hei,1 do  
 aRect = display.newRect(\_group,xx,yy,1,1)  
 if bitMap[yy\*\_wid + xx] then  
 pixel = bitMap[yy\*\_wid + xx]  
 aRect:setFillColor( pixel.r, pixel.g, pixel.b)  
else  
aRect:setFillColor( \_backColor.r,\_backColor.g,\_backColor.b)  
 end  
 end  
 end  
  
 return \_group  
end  

[import]uid: 108660 topic_id: 26731 reply_id: 326731[/import]

And here is some code that makes use of it:

[code]

–include the class
myGFX = require “GFXClass”
–to receive the display group
local myGroup

–define the extents
–width, height, rgb of the background
myGFX:createImage(100,100,140,200,200);

–draw a black line using a loop
for x = 0 , 99,1 do
myGFX:setPixel(x,x,0,0,0)
end

–fill a rectangle
–x,y,x2,y2, red,green,blue
myGFX:fillRect(50,50,80,32,200,20,20)

–draw a green line using a public line draw algorithm
myGFX:drawLine(10,50,83,75,0,255,0)

–retrieve r g b from a point
local r,g,b = myGFX:getPixel(76,32)

–print these
print (r,g,b)

–ask for a display group
myGroup = myGFX:renderdisplayGroup()

–and now you could save this as a picture…
display.save ( myGroup, “mypic.jpg” ,system.TemporaryDirectory )
[/code] [import]uid: 108660 topic_id: 26731 reply_id: 108349[/import]

How about preformance? I have some fun with pseudo image manipulation some time ago ( http://www.youtube.com/watch?v=A0-UMUEymcQ ) but FPS for small objects was pathetic… :slight_smile: [import]uid: 12704 topic_id: 26731 reply_id: 108374[/import]

This isn’t a performance class.
I doubt FPS is a concept you could apply here.
As it stands, it creates a bitmap.
After that statement, all bets are off. [import]uid: 108660 topic_id: 26731 reply_id: 108430[/import]