diff --git a/libbacteria/lua/libimages.c b/libbacteria/lua/libimages.c index 4a53ec8..8487bcd 100644 --- a/libbacteria/lua/libimages.c +++ b/libbacteria/lua/libimages.c @@ -14,7 +14,7 @@ int luaopen_libimages(lua_State *L) { int gd_##what = lua_tonumber(L, -1); \ lua_pop(L, 1); -#define DEBUG_LIBIMAGES_LUA +//#define DEBUG_LIBIMAGES_LUA INITLUAFUNC(gdInitImage) { int width = (int)luaL_checknumber(L, 1); @@ -241,6 +241,7 @@ INITLUAFUNC(gdAddColor) { GETLUANUM(blue); GETLUANUM(alpha); RGBa color = {gd_red, gd_green, gd_blue, gd_alpha}; + //printf("Color r:%d g:%d b:%d a:%d\n", gd_red, gd_green, gd_blue,gd_alpha); gdAddColor(in, nameColor, &color); return 0; } diff --git a/src/luasubmodules/gd.lua b/src/luasubmodules/gd.lua new file mode 100644 index 0000000..430d2ae --- /dev/null +++ b/src/luasubmodules/gd.lua @@ -0,0 +1,215 @@ +gd={} + + +function gd:new(width, height,quality,color) + local obj = {} + obj.width = width or 120 + obj.height = height or 120 + obj.quality = quality or 50 + obj.gd = nil + obj.defFont = {} + obj.background = color or obj:getColorFromArray(color) +-- + function obj:getSize() + return obj.width,obj.height + end + function obj:getQuality() + return obj.quality + end +-- + function obj:getImageData(q) + q = q or obj.quality + return images.getImageData(obj.gd, q) + end + function obj:getRawImage(imgdata,q) + q = q or obj.quality + imgdata = imgdata or obj:getImageData(q) + c = images.getImageDataRaw(imgdata) + s = images.getImageDataSize(imgdata) + return c,s + end + function obj:saveToFile(path) + images.writeToFile(obj.gd, path, obj.quality) + end +-- + function obj:setQuality(q) + obj.quality = q + end + function obj:setGD(gd) + obj:clear() + obj.gd = gd + end +--constructor + function obj:reopenFromFile(width,height,path) + obj:clear() + --todo: get with and height from file + obj.width=width + obj.height=height + obj.gd = obj:initImageFromFile(path,width,height) + return obj + end +--font + function obj:setDefFont(path) + obj.defFont = path + end +--init functions + function obj:initImage(background) + background = background or obj.background + obj.gd=images.gdInitImage( obj.width, obj.height, background) + return true + end + function obj:initImageFromFile(path, width, height) + if not obj.gd == nil then + print("obj: Firstly free old image") + return nil + end + obj.gd = images.gdInitImageFromFile(path,width,height) + return true + end +--colors + function obj:getColorFromArray(color) + if obj.gd == nil then + print("obj: Firstly init image") + return nil + end + --("Color: ", color['red'], ' ', color['green'], ' ', color['blue'], ' ', color['alpha']) + return images.gdInitColor(obj.gd, color) + end + + function obj:getRandColor() + local color1 = images.gdRandColor() + return obj:getColorFromArray(color1) + end + + function obj:getColorArray(red,green,blue,alpha) + local color = {} + color["red"] = red or 0 + color["green"] = green or 0 + color["blue"] = blue or 0 + color["alpha"] = alpha or 255 + return color + end + + function obj:getColor(red,green,blua,alpha) + if obj.gd == nil then + print("obj: Firstly init image") + return nil + end + color = obj:getColorArray(red,green,blue,alpha) + + return obj:getColorFromArray(color) + end + + function obj:addColor(name,red,green,blue,alpha) + name = name or nil + if name == nil then return nil end + red = red or 0 + green = green or 0 + blue = blue or 0 + alpha = alpha or 255 + --print("obj.getColor") + local c = obj:getColorArray(red,green,blue,alpha) + --print("gdAddColor") + images.gdAddColor(obj.gd,name,c) + return true + end + function obj:foundColor(name) + if obj.gd == nil then + print("obj: Firstly init image") + return nil + end + return images.gdFoundColor(obj.gd,name) + end + -- need color from obj.getColor(...) + function obj:setColor(name, n, color) + if obj.gd == nil then + print("obj: Firstly init image") + return nil + end + if n == nil or name == nil then return nil end + images.gdSetColor( obj.gd, name, n, color ) + end +--draw + function obj:drawPixel(x,y,color) + if obj.gd == nil then + print("obj: Firstly init image") + return nil + end + images.gdSetPixel(obj.gd, x,y, color) + return true + end + function obj:drawRect(x,y,x1,y1,color) + x = x or 0 + y = y or 0 + x1 = x1 or obj.width + y1 = y1 or obj.height + color = color or obj:getColor() + if obj.gd == nil then + print("obj: Firstly init image") + return nil + end + images.gdDrawRect(obj.gd, x,y, x1, y1, color) + return true + end + function obj:drawLine(x,y,x1,y1,color) + x = x or 0 + y = y or 0 + x1 = x1 or obj.width + y1 = y1 or obj.height + color = color or obj:getColor() + if obj.gd == nil then + print("obj: Firstly init image") + return nil + end + images.gdDrawLine(obj.gd, x,y, x1,y1, color) + return true + end + function obj:drawText(text, x,y, color, ptSize, angle, fontPath) + x = x or 0 + y = y or 0 + fontPath = fontPath or obj.defFont + angle = angle or 0.0 + color = color or obj:getColor() + text = text or "" + if obj.gd == nil then + print("obj: Firstly init image") + return nil + end + images.gdWriteText(obj.gd, x,y, fontPath, ptSize, angle, color, text) + end +--random draw + function obj:drawRandomPixels(min,max) + if obj.gd == nil then return nil end + min = min or 100 + max = max or 300 + images.gdDrawRandomPixels(obj.gd, min, max) + end + function obj:drawRandomLines(count) + count = count or {} + if obj.gd == nil then return nil end + images.gdDrawRandomLines(obj.gd, count) + return true + end +--random text + function obj:getRandStr(s) + s = s or 5 + return images.gdGetRandStr(s) + end +--clear + + function obj:clear() + if not obj.gd == nil then + images.gdFree(obj.gd) + obj.gd=nil + return true + end + return nil + end + + + return obj +end + + + +return gd diff --git a/src/luasubmodules/init.lua b/src/luasubmodules/init.lua index 8db8ad0..0cddaa2 100644 --- a/src/luasubmodules/init.lua +++ b/src/luasubmodules/init.lua @@ -4,6 +4,7 @@ sql = require "luasql.postgres" bacteria = require (submoduledir .. "bacteria") bacteria_aes = require (submoduledir .. "bencdec") +gd = require ( submoduledir .. "gd" ) bacteria.init("cryptocoins.ini",{'tgst','tdash'}) bacteria.dumpCryptocoins() @@ -135,48 +136,50 @@ local function EncDecTest() k3.clear() end -local function checkGD(width,height,quality) - width = width or 120 - height = height or 120 - quality = quality or 50 - - print("GD") - - local color = {} - color["red"] = 120 - color["green"] = 320 - color["blue"] = 50 - color["alpha"] = 255 - img=images.gdInitImage(width,height, color) - local color2 = {} - color2["blue"]=255 - color["red"]=100 - color["alpha"]=255 - color["green"]=0 - images.gdAddColor(img,"myColor",color) - - images.gdDrawRandomLines(img, 100) - images.gdDrawRandomPixels(img, 100,300) - local color1 = images.gdRandColor() - print(color1['red'],' ',color1['green'], ' ', color1['blue']) - - myIntColor = images.gdInitColor(img, color1) - myColor = images.gdFoundColor(img,"myColor") - - - images.gdDrawRect(img, 0,0, 30, 30, myIntColor) - images.gdDrawRect(img, 60,60, 30, 30, myColor) - images.gdDrawRect(img, 60,60, 90, 90, myIntColor) - images.gdDrawRect(img, 90,90, 120, 120, myColor) - images.gdWriteText(img, 0,30, "./fonts/dummy.ttf", 15, 0.2, myIntColor, "TestTTF") - images.gdWriteText(img, 0,45, "./fonts/dummy.ttf", 15, -0.2, myIntColor, "TestTTF") - images.writeToFile(img, "Example.jpg", quality) - print(images.gdGetRandStr(30)) - imgData = images.getImageData(img, quality) - c = images.getImageDataRaw(imgData) - s = images.getImageDataSize(imgData) - --print((c), "size: ",s) - images.gdFree(img) +-- new take array +-- local color ={} +-- color['green'] = 0 color['red']=0 +-- color['alpha'] = 0 color['blue']=0 +-- gd:new(120,120,50,color) +-- in another parts color is will be INT color +local function checkGD(width,height,quality,color) + img = gd:new(width,height,quality,color) + print("initImage") + img:initImage() -- can get background color, int not array + print("add red color") + img:addColor("red", 255, 0, 0, 255) + print("initColors") + greenColor = img:getColor(0,255,0,255) + redColor = img:foundColor("red") + randColor = img:getRandColor() + print("Sleep one second for another rand color") + sleep(1) + randColor1 = img:getRandColor() + print("done") + print("draw random") + img:drawRandomLines(1) -- count + img:drawRandomPixels(1,2) -- min count, max count + print("draw rect") + img:drawRect(0,0, 30, 30, redColor) + img:drawRect(30,30, 60,60, randColor1) + img:drawRect(60,60,90,90, greenColor) + img:drawRect(90,90,120,120, randColor) + print("drawLine") + img:drawLine(0,0,width,height, randColor) + img:setDefFont("./fonts/dummy.ttf") + --can take defFont + --12 and 15 is ptSize; 0.5 and -0.2 is angle; width/2 is x;... height/2 is y; + print("drawText") + img:drawText("HelloTTF", width/2,height/2, randColor1, 12, 0.5, "./fonts/dummy.ttf") + img:drawText("hello ttf", width/3, height/3, randColor1, 15, -0.2) + + print("save to file test.jpg") +--function obj:setQuality(q) ; so img:setQuality(100) for 100 quality before save; getQuality for get current quality + img:saveToFile("test.jpg") + rawdata, size_rawdata = img:getRawImage() + print("Size of img: ", size_rawdata) + print("RawData: ", rawdata) + img:clear() end local function GMPTEST() print("GMP") @@ -323,10 +326,10 @@ local function checkEd25519rsa () ed25519rsa.freeaKey(rsa) ed25519rsa.freeaKey(rsa_) end -checkEd25519rsa () -GMPTEST() -CryptocoinsTest() -checkGD(120,120,100) -EncDecTest() +--checkEd25519rsa () +--GMPTEST() +--CryptocoinsTest() +--checkGD(120,120,100,color) +--EncDecTest()