-- custom image object function ccreateImage(...) local image = createImage(...); local mt = getmetatable(image) local destroyed,___tempBitmap = false,nil; if (rawget(mt,"cropImageByValue") and rawget(mt,"cropImageByPercentage") and rawget(mt,"setTransparentSize")) then return image; end local self = image local function cropImage(img,x1,y1,x2,y2,src,x3,y3,x4,y4) if (type(img)=='userdata' and img.copyRect and type(src) == 'userdata' and x1 and x2 and x3 and x4 and y1 and y2 and y3 and y4) then -- print(x1,y1,x2,y2," --> ", x3, y3, x4, y4) img.copyRect(x1,y1,x2,y2,src,x3,y3,x4,y4); end end local function drawImage(obj,x1,y1,x2,y2,source,copyclip) if (obj) then if (not obj.copyRect) then if (obj.Picture and obj.Picture.Bitmap) then obj = obj.Picture.Bitmap end end end local width,height; if (source.ClassName == "TCEImage") then width,height = source.width,source.height; source = source.Picture; end local source = (source.Canvas or source.Bitmap.Canvas) return (obj.copyRect or obj.Canvas.copyRect)(x1,y1,x2,y2,source,0,0,(copyclip and x2 or (width or source.width)), (copyclip and y2 or (height or source.height))) end local function cropImageByValue(img,width,height,case) if (not (img and (width or height))) then return end if (width or height) then width = math.floor(width or height) height = math.floor(height or width) end -- creates new image for us to use; local _useless = img.width -- gotta force the image to update otherwise it gets fucked up local nwidth,nheight = img.width-width,img.height-height; ___tempBitmap = (___tempBitmap or createPicture()) ___tempBitmap.name = "___tempBitmap" ___tempBitmap.Bitmap.Canvas.width = nwidth; ___tempBitmap.Bitmap.Canvas.height = nheight; ___tempBitmap.Bitmap.width = nwidth; ___tempBitmap.Bitmap.height = nheight; ___tempBitmap.Bitmap.canvas.getPixel(0,0); if(case == "TopLeft") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,img.width,img.height,img.Canvas,width,height,img.width,img.height) elseif(case == "TopRight") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Canvas,0,height,nwidth,img.height) elseif(case == "BottomLeft") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Canvas,width,0,img.width,nheight) elseif(case == "BottomRight") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Canvas,0,0,nwidth,nheight) else width1,width2 = math.floor(width/2), math.ceil(width/2); height1,height2 = math.floor(height/2), math.ceil(height/2); cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Canvas,width1,height1,img.width-width2,img.height-height2) end img.Canvas.clear(); -- drawImage(img.picture.bitmap,0,0,img.width,img.height,___tempBitmap) -- copies whole ___tempBitmap and draws it in 1366x768 size (after cropping) img:drawImage(0,0,img.width,img.height,___tempBitmap) -- copies whole ___tempBitmap and draws it in 1366x768 size (after cropping) return end local function trimImageByValue(img,width,height,case) if (not (img and (width or height))) then return end if (width or height) then width = math.floor(width or height) height = math.floor(height or width) end local nwidth,nheight = img.width-width,img.height-height; -- creates new image for us to use; local _useless = img.width -- gotta force the image to update otherwise it gets fucked up ___tempBitmap = (___tempBitmap or createPicture()) ___tempBitmap.name = "___tempBitmap" ___tempBitmap.Bitmap.Canvas.width = nwidth; ___tempBitmap.Bitmap.Canvas.height = nheight; ___tempBitmap.Bitmap.width = nwidth; ___tempBitmap.Bitmap.height = nheight; ___tempBitmap.Bitmap.canvas.getPixel(0,0); if(case == "TopLeft") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Canvas,width,height,img.width,img.height) elseif(case == "TopRight") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Canvas,0,height,nwidth,img.height) elseif(case == "BottomLeft") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Canvas,width,0,img.width,nheight) elseif(case == "BottomRight") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Canvas,0,0,nwidth,nheight) else width1,width2 = math.floor(width/2), math.ceil(width/2); height1,height2 = math.floor(height/2), math.ceil(height/2); cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Canvas,width1,height1,img.width-width2,img.height-height2) end img.Canvas.clear(); img.width = nwidth img.height = nheight img.Canvas.width = nwidth img.Canvas.height = nheight img.canvas.getPixel(0,0); img:drawImage(0,0,img.width,img.height,___tempBitmap) -- copies whole ___tempBitmap and draws it in 1366x768 size (after cropping) return end local function scaleImageByValue(img,width,height) if (not (img and (width or height))) then return end if (width or height) then width = math.floor(width or height) height = math.floor(height or width) end -- creates new image for us to use; local _useless = img.width -- gotta force the image to update otherwise it gets fucked up local nwidth,nheight = img.width-width,img.height-height; ___tempBitmap = (___tempBitmap or createPicture()) ___tempBitmap.name = "___tempBitmap" ___tempBitmap.Bitmap.Canvas.width = img.width; ___tempBitmap.Bitmap.Canvas.height = img.height; ___tempBitmap.Bitmap.width = img.width; ___tempBitmap.Bitmap.height = img.height; ___tempBitmap.Bitmap.canvas.getPixel(0,0); cropImage(___tempBitmap.Bitmap.Canvas,0,0,img.width,img.height,img.Canvas,0,0,img.width,img.height) img.Canvas.clear(); img.width = nwidth img.height = nheight img.Canvas.width = nwidth img.Canvas.height = nheight img.canvas.getPixel(0,0); img:drawImage(0,0,img.width,img.height,___tempBitmap) -- copies whole ___tempBitmap and draws it in 1366x768 size (after cropping) return end local function setTransparentSize(obj,x,y) if (type(obj)~='userdata') then y = x; x = obj; obj = self; end obj.Picture.Bitmap.Width = x; obj.Picture.Bitmap.Height = y; obj.canvas.getPixel(0,0); obj.picture.Bitmap.TransparentColor = 0; obj.Transparent=false; obj.Transparent=true; end local function cropImageByPercentage(img,percentagex,percentagey,case) if (type(img)~='userdata') then case = percentagey; percentagey = percentagex; percentagex = img; img = self; end if (not (percentagex or percentagey)) then percentagex = percentagex or percentagey percentagey = percentagey or percentagex end if (percentagex < 1 and percentagex > 0) then percentagex = percentagex * 100 end if (percentagey < 1 and percentagey > 0) then percentagey = percentagey * 100 end return cropImageByValue(img,img.width*(percentagex/100),img.height*(percentagey/100),case) end local function trimImageByPercentage(img,percentagex,percentagey,case) if (type(img)~='userdata') then case = percentagey; percentagey = percentagex; percentagex = img; img = self; end if (not (percentagex or percentagey)) then percentagey = percentagey or percentagex percentagex = percentagex or percentagey end if (percentagex < 1 and percentagex > 0) then percentagex = percentagex * 100 end if (percentagey < 1 and percentagey > 0) then percentagey = percentagey * 100 end return trimImageByValue(img,img.width*(percentagex/100),img.height*(percentagey/100),case) end local function scaleByPercentage(img,percentagex,percentagey) if (type(img)~='userdata') then percentagey = percentagex; percentagex = img; img = self; end if (not (percentagex or percentagey)) then percentagey = percentagey or percentagex percentagex = percentagex or percentagey end if (percentagex < 1 and percentagex > 0) then percentagex = percentagex * 100 end if (percentagey < 1 and percentagey > 0) then percentagey = percentagey * 100 end return scaleImageByValue(img,img.width*(percentagex/100),img.height*(percentagey/100)) end local function ImportAndLoad(sender,image_path) if (type(sender)~='userdata') then image_path = sender; sender = self; end if (type(image_path)=='string') then local object local file = io.open(image_path,"r") if (not file) then return false; else file:close(); local filename = image_path:match(".+\\(.-)$") object = findTableFile(filename) if (not object) then object = createTableFile(filename,image_path) end object.tag = filename; end sender.autosize = true sender.picture.loadFromStream(object.stream); local _useless = sender.picture.bitmap.width sender.canvas.getPixel(0,0); sender.autosize = false return true,object; end end local _verticalFlipped = false; local _horizontalFlipped = false; local function flipVert(img) img = type(img) =='userdata' and img or self; if (img) then _verticalFlipped = not _verticalFlipped; rawset(mt,"isVertFlip",_verticalFlipped); img.canvas.copyRect(0,0,img.width,img.height,img.canvas,img.width,0,0,img.height) end end local function flipHorz(img) img = type(img) =='userdata' and img or self; if (img) then img.canvas.copyRect(0,0,img.width,img.height,img.canvas,0,img.height,img.width,0) _horizontalFlipped = not _horizontalFlipped; rawset(mt,"isHorzFlip",_horizontalFlipped); end end local ex__index = mt.__index mt.__index = function(tab,index) if (tostring(index):lower() == "destroy") then if (not destroyed and ___tempBitmap) then destroyed = true; ___tempBitmap.destroy(); ___tempBitmap = nil; end end return ex__index(tab,index) end rawset(mt,"cropImageByValue",cropImageByValue) rawset(mt,"cropImageByPercentage",cropImageByPercentage) rawset(mt,"trimImageByValue",trimImageByValue) rawset(mt,"trimImageByPercentage",trimImageByPercentage) rawset(mt,"scaleImageByValue",scaleImageByValue) rawset(mt,"scaleByPercentage",scaleByPercentage) rawset(mt,"setTransparentSize",setTransparentSize) rawset(mt,"importAndLoad",ImportAndLoad) rawset(mt,"isVertFlip",_verticalFlipped) rawset(mt,"isHorzFlip",_horizontalFlipped) rawset(mt,"flipVert",flipVert) rawset(mt,"flipHorz",flipHorz) rawset(mt,"drawImage",drawImage) return image; end -- custom picture object function ccreatePicture() local picture = createPicture(); local mt = getmetatable(picture); local destroyed,___tempBitmap = false,nil; if (rawget(mt,"cropImageByValue") and rawget(mt,"cropImageByPercentage") and rawget(mt,"setTransparentSize")) then return picture; end local self = picture local function cropImage(img,x1,y1,x2,y2,src,x3,y3,x4,y4) if (type(img)=='userdata' and img.copyRect and type(src) == 'userdata' and x1 and x2 and x3 and x4 and y1 and y2 and y3 and y4) then img.copyRect(x1,y1,x2,y2,src,x3,y3,x4,y4); end end local function drawImage(obj,x1,y1,x2,y2,source,copyclip) if (obj) then if (not obj.copyRect) then if (obj.Bitmap) then obj = obj.Bitmap end end end local width,height; if (source.ClassName == "TCEImage") then width,height = source.width,source.height; source = source.Picture; end local source = (source.Canvas or source.Bitmap.Canvas) return (obj.copyRect or obj.Canvas.copyRect)(x1,y1,x2,y2,source,0,0,(copyclip and x2 or (width or source.width)), (copyclip and y2 or (height or source.height))) end local function cropImageByValue(img,width,height,case) if (not (img and (width or height))) then return end if (width or height) then width = math.floor(width or height) height = math.floor(height or width) end -- creates new image for us to use; local _useless = img.Bitmap.width -- gotta force the image to update otherwise it gets fucked up local nwidth,nheight = img.Bitmap.width-width,img.Bitmap.height-height; ___tempBitmap = (___tempBitmap or createPicture()) ___tempBitmap.name = "___tempBitmap" ___tempBitmap.Bitmap.Canvas.width = nwidth; ___tempBitmap.Bitmap.Canvas.height = nheight; ___tempBitmap.Bitmap.width = nwidth; ___tempBitmap.Bitmap.height = nheight; ___tempBitmap.Bitmap.canvas.getPixel(0,0); if(case == "TopLeft") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Bitmap.Canvas,width,height,img.Bitmap.width,img.Bitmap.height) elseif(case == "TopRight") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Bitmap.Canvas,0,height,nwidth,img.Bitmap.height) elseif(case == "BottomLeft") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Bitmap.Canvas,width,0,img.Bitmap.width,nheight) elseif(case == "BottomRight") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Bitmap.Canvas,0,0,nwidth,nheight) else width1,width2 = math.floor(width/2), math.ceil(width/2); height1,height2 = math.floor(height/2), math.ceil(height/2); cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Bitmap.Canvas,width1,height1,img.Bitmap.width-width2,img.Bitmap.height-height2) end img.Bitmap.Canvas.clear(); -- drawImage(img.picture.bitmap,0,0,img.Bitmap.width,img.Bitmap.height,___tempBitmap) -- copies whole ___tempBitmap and draws it in 1366x768 size (after cropping) img:drawImage(0,0,img.Bitmap.width,img.Bitmap.height,___tempBitmap) -- copies whole ___tempBitmap and draws it in 1366x768 size (after cropping) return true end local function trimImageByValue(img,width,height,case) if (not (img and (width or height))) then return end if (width or height) then width = math.floor(width or height) height = math.floor(height or width) end local nwidth,nheight = img.Bitmap.width-width,img.Bitmap.height-height; -- creates new image for us to use; local _useless = img.Bitmap.width -- gotta force the image to update otherwise it gets fucked up ___tempBitmap = (___tempBitmap or createPicture()) ___tempBitmap.name = "___tempBitmap" ___tempBitmap.Bitmap.Canvas.width = nwidth; ___tempBitmap.Bitmap.Canvas.height = nheight; ___tempBitmap.Bitmap.width = nwidth; ___tempBitmap.Bitmap.height = nheight; ___tempBitmap.Bitmap.canvas.getPixel(0,0); if(case == "TopLeft") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Bitmap.canvas,width,height,img.Bitmap.width,img.Bitmap.height) elseif(case == "TopRight") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Bitmap.canvas,0,height,nwidth,img.Bitmap.height) elseif(case == "BottomLeft") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Bitmap.canvas,width,0,img.Bitmap.width,nheight) elseif(case == "BottomRight") then cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Bitmap.canvas,0,0,nwidth,nheight) else width1,width2 = math.floor(width/2), math.ceil(width/2); height1,height2 = math.floor(height/2), math.ceil(height/2); cropImage(___tempBitmap.Bitmap.Canvas,0,0,nwidth,nheight,img.Bitmap.canvas,width1,height1,img.Bitmap.width-width2,img.Bitmap.height-height2) end img.Bitmap.canvas.clear(); img.Bitmap.width = nwidth img.Bitmap.height = nheight img.Bitmap.canvas.width = nwidth img.Bitmap.canvas.height = nheight img.Bitmap.canvas.getPixel(0,0); img:drawImage(0,0,img.Bitmap.width,img.Bitmap.height,___tempBitmap) -- copies whole ___tempBitmap and draws it in 1366x768 size (after cropping) return true end local function scaleImageByValue(img,width,height) if (not (img and (width or height))) then return end if (width or height) then width = math.floor(width or height) height = math.floor(height or width) end -- creates new image for us to use; local _useless = img.Bitmap.width -- gotta force the image to update otherwise it gets fucked up local nwidth,nheight = img.Bitmap.width-width,img.Bitmap.height-height; ___tempBitmap = (___tempBitmap or createPicture()) ___tempBitmap.name = "___tempBitmap" ___tempBitmap.Bitmap.Canvas.width = img.Bitmap.width; ___tempBitmap.Bitmap.Canvas.height = img.Bitmap.height; ___tempBitmap.Bitmap.width = img.Bitmap.width; ___tempBitmap.Bitmap.height = img.Bitmap.height; ___tempBitmap.Bitmap.canvas.getPixel(0,0); cropImage(___tempBitmap.Bitmap.Canvas,0,0,img.Bitmap.width,img.Bitmap.height,img.Bitmap.canvas,0,0,img.Bitmap.width,img.Bitmap.height) img.Bitmap.canvas.clear(); img.Bitmap.width = nwidth img.Bitmap.height = nheight img.Bitmap.canvas.width = nwidth img.Bitmap.canvas.height = nheight img.Bitmap.canvas.getPixel(0,0); img:drawImage(0,0,img.Bitmap.width,img.Bitmap.height,___tempBitmap) -- copies whole ___tempBitmap and draws it in 1366x768 size (after cropping) return true end local function cropImageByPercentage(img,percentagex,percentagey,case) if (type(img)~='userdata') then case = percentagey; percentagey = percentagex; percentagex = img; img = self; end if (not (percentagex and percentagey)) then percentagex = percentagex or percentagey percentagey = percentagey or percentagex end if (percentagex < 1 and percentagex > 0) then percentagex = percentagex * 100 end if (percentagey < 1 and percentagey > 0) then percentagey = percentagey * 100 end return cropImageByValue(img,img.Bitmap.width*(percentagex/100),img.Bitmap.height*(percentagey/100),case) end local function trimImageByPercentage(img,percentagex,percentagey,case) if (type(img)~='userdata') then case = percentagey; percentagey = percentagex; percentagex = img; img = self; end if (not (percentagex or percentagey)) then percentagey = percentagey or percentagex percentagex = percentagex or percentagey end if (percentagex < 1 and percentagex > 0) then percentagex = percentagex * 100 end if (percentagey < 1 and percentagey > 0) then percentagey = percentagey * 100 end return trimImageByValue(img,img.Bitmap.width*(percentagex/100),img.Bitmap.height*(percentagey/100),case) end local function scaleByPercentage(img,percentagex,percentagey) if (type(img)~='userdata') then percentagey = percentagex; percentagex = img; img = self; end if (not (percentagex and percentagey)) then percentagey = percentagey or percentagex percentagex = percentagex or percentagey end if (percentagex < 1 and percentagex > 0) then percentagex = percentagex * 100 end if (percentagey < 1 and percentagey > 0) then percentagey = percentagey * 100 end return scaleImageByValue(img,img.Bitmap.width*(percentagex/100),img.Bitmap.height*(percentagey/100)) end local _verticalFlipped = false; local _horizontalFlipped = false; local function flipVert(img) img = type(img) =='userdata' and img or self; if (img) then _verticalFlipped = not _verticalFlipped; rawset(mt,"isVertFlip",_verticalFlipped); img.Bitmap.canvas.copyRect(0,0,img.Bitmap.width,img.Bitmap.height,img.Bitmap.canvas,img.Bitmap.width,0,0,img.Bitmap.height) end end local function flipHorz(img) img = type(img) =='userdata' and img or self; if (img) then img.Bitmap.canvas.copyRect(0,0,img.Bitmap.width,img.Bitmap.height,img.Bitmap.canvas,0,img.Bitmap.height,img.Bitmap.width,0) _horizontalFlipped = not _horizontalFlipped; rawset(mt,"isHorzFlip",_horizontalFlipped); end end local ex__index = mt.__index mt.__index = function(tab,index) if (tostring(index):lower() == "destroy") then if (not destroyed and ___tempBitmap) then destroyed = true; ___tempBitmap.destroy(); ___tempBitmap = nil; end end return ex__index(tab,index) end rawset(mt,"cropImageByValue",cropImageByValue) rawset(mt,"cropImageByPercentage",cropImageByPercentage) rawset(mt,"trimImageByValue",trimImageByValue) rawset(mt,"trimImageByPercentage",trimImageByPercentage) rawset(mt,"scaleImageByValue",scaleImageByValue) rawset(mt,"scaleByPercentage",scaleByPercentage) rawset(mt,"isVertFlip",_verticalFlipped) rawset(mt,"isHorzFlip",_horizontalFlipped) rawset(mt,"flipVert",flipVert) rawset(mt,"flipHorz",flipHorz) rawset(mt,"drawImage",drawImage) return picture; end