Preallocating two big images for teselation drawing time was improved.
This is new code:
------------------Begin sample
--package.cpath = package.cpath ..
package.cpath:gsub("%?","?"..table.concat{_VERSION:match("(%d).(%d)")})
require"imlua"
require"cdlua"
require"cdluaim"
require"iuplua"
require"iupluacd"
require"imlua_process"
---[[
iup._ERRORMESSAGE=function(msg,...)
print(msg,...);
error(msg,0)
end
--]]
anchosc = 500 -- window width
filename =
[[C:\LUA\im-3.8.2_Win32_dllw4_lib\im-3.8.2_Examples\im\html\examples\flower.jpg]]
--set your file
heightfac=math.sqrt(3)*0.5 -- ratio from height to side in equilateral
triangle
image=im.FileImageLoad(filename)
cnv = iup.canvas{rastersize = anchosc .."x".. anchosc*heightfac, border =
"NO"}
function cnv:map_cb() -- the CD canvas can only be created when the
IUP canvas is mapped
self.canvas = cd.CreateCanvas(cd.IUP, self)
self.canvas:Background(cd.BLACK)
end
mouseX,mouseY=0,0
nostatus=(" "):rep(10)
function cnv:motion_cb(x,y,status)
if status == nostatus then
mouseX = x
mouseY = y
iup.Update(cnv)
end
end
function EQTriangle(can,len,mode)
mode = mode or cd.FILL
can:Begin(mode)
can:Vertex(0,0)
can:Vertex(len,0)
can:Vertex(len*0.5,0.5*len*math.sqrt(3))
can:End()
end
function clip(val,mini,maxi)
return math.max(mini,math.min(val,maxi))
end
function linearmap(v,s,e,ds,de)
return ((de-ds)*(v-s)/(e-s)) + ds
end
function imResize(im1,w,h)
local im2 = im.ImageCreateBased(im1,w,h)
im.ProcessResize(im1,im2,3)
return im2
end
function MaskTriang(img,size)
local im2 = img:Clone()
im2:AddAlpha()
local imcv = im2:cdCreateCanvas()
EQTriangle(imcv,size,cd.CLIP)
imcv:Clip(cd.CLIPPOLYGON)
img:cdCanvasPutImageRect(imcv, 0, 0, 0, 0, 0, 0, 0, 0)
imcv:Kill()
return im2
end
function Teselate(imA,imB,size)
--imB:Clear()
local imcv = imB:cdCreateCanvas()
--local ident = imcv:GetTransform()
--print("imcv:GetTransformation",imcv.GetTransform,ident)
--imcv:Transform({1,0,0,1,0,0}) --reset identity
imA:cdCanvasPutImageRect(imcv, 0, 0, 0, 0, 0, 0, 0, 0)
imcv:TransformTranslate(1.5*size, size*heightfac)
imcv:TransformRotate(120)
imA:cdCanvasPutImageRect(imcv, 0, 0, 0, 0, 0, 0, 0, 0)
imcv:Transform({1,0,0,1,0,0}) --reset identity
imcv:TransformTranslate(1.5*size, size*heightfac)
imcv:TransformRotate(-120)
imA:cdCanvasPutImageRect(imcv, 0, 0, 0, 0, 0, 0, 0, 0)
imcv:TransformScale(1,-1)
imA:cdCanvasPutImageRect(imcv, 0, 0, 0, 0, 0, 0, 0, 0)
imcv:Kill()
return imB,imA
end
function EndTeselate(imA,imB,size)
--imB:Clear()
local imcv = imB:cdCreateCanvas()
imA:cdCanvasPutImageRect(imcv, 0, 0, 0, 0, 0, 0, 0, 0)
imcv:TransformTranslate(1.5*size, size*heightfac)
imcv:TransformRotate(-120)
imcv:TransformScale(1,-1)
imA:cdCanvasPutImageRect(imcv, 0, 0, 0, 0, 0, 0, 0, 0)
imcv:Transform({1,0,0,1,0,0}) --reset identity
imcv:TransformRotate(120)
imcv:TransformScale(1,-1)
imA:cdCanvasPutImageRect(imcv, 0, 0, 0, 0, 0, 0, 0, 0)
imcv:Kill()
return imB,imA
end
function cnv:action()
local timebegin = os.clock()
self.canvas:Activate()
local wcd,hcd = self.canvas:GetSize()
local width = wcd --+ hcd/heightfac
local size = clip(mouseX,10,width)
--self.canvas:Clear()
local imm = imResize(image,size,size)
local im2 = MaskTriang(imm,size)
local imA =
im.ImageCreateBased(im2,width,math.max(width*heightfac*0.5,hcd))
local imB =
im.ImageCreateBased(im2,width,math.max(width*heightfac*0.5,hcd))
im.ProcessInsert(imA, im2,imA, 0, 0)
while size < width do
imA,imB = Teselate(imA,imB,size)
size = size * 2
end
imA,imB = EndTeselate(imA,imB,size)
imA:cdCanvasPutImageRect(self.canvas, 0, 0, 0, 0, 0, 0, 0, 0)
imm:Destroy()
im2:Destroy()
imA:Destroy()
imB:Destroy()
print("drawtime",os.clock()-timebegin)
end
dlg = iup.dialog{cnv}
function dlg:resize_cb(ww,hh)
cnv.rastersize = ww .. "x" .. hh
end
function dlg:close_cb()
image:Destroy()
cnv.canvas:Kill()
self:destroy()
return iup.IGNORE -- because we destroy the dialog
end
dlg:show()
iup.MainLoop()
-------------End sample
------------------------------------------------------------------------------
CenturyLink Cloud: The Leader in Enterprise Cloud Services.
Learn Why More Businesses Are Choosing CenturyLink Cloud For
Critical Workloads, Development Environments & Everything In Between.
Get a Quote or Start a Free Trial Today.
http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk
_______________________________________________
Iup-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/iup-users