PDA

View Full Version : GD Library for Girder



Ron
April 8th, 2005, 10:21 AM
Here is the GD library, it allows you to generate images from Lua.

Docs are here:

http://lua-gd.luaforge.net/#api

Unzip and drop this into the Girder directory.

from the docs:



x=require("gd")


function createClock(size, hours, minutes)
local im = gd.createTrueColor(size, size)
local white = im:colorAllocate(255, 255, 255)
local gray = im:colorAllocate(128, 128, 128)
local black = im:colorAllocate(0, 0, 0)
local blue = im:colorAllocate(0, 0, 128)
local cxy = size/2

im:filledRectangle(0, 0, size, size, white)
im:setThickness(math.max(1, size/100))
im:arc(cxy, cxy, size, size, 0, 360, black)

local ang = 0
local rang, gsize
while ang < 360 do
rang = math.rad&#40;ang&#41;
if math.mod&#40;ang, 90&#41; == 0 then
gsize = 0.75
else
gsize = 0.85
end
im&#58;line&#40;
cxy + gsize * cxy * math.sin&#40;rang&#41;,
size - &#40;cxy + gsize * cxy * math.cos&#40;rang&#41;&#41;,
cxy + cxy * 0.9 * math.sin&#40;rang&#41;,
size - &#40;cxy + cxy * 0.9 * math.cos&#40;rang&#41;&#41;,
gray&#41;
ang = ang + 30
end

im&#58;setThickness&#40;math.max&#40;1, size/50&#41;&#41;
im&#58;line&#40;cxy, cxy,
cxy + 0.45 * size * math.sin&#40;math.rad&#40;6*minutes&#41;&#41;,
size - &#40;cxy + 0.45 * size * math.cos&#40;math.rad&#40;6*minutes&#41;&#41;&#41;,
blue&#41;

im&#58;setThickness&#40;math.max&#40;1, size/25&#41;&#41;
rang = math.rad&#40;30*hours + minutes/2&#41;
im&#58;line&#40;cxy, cxy,
cxy + 0.25 * size * math.sin&#40;rang&#41;,
size - &#40;cxy + 0.25 * size * math.cos&#40;rang&#41;&#41;,
blue&#41;

im&#58;setThickness&#40;1&#41;
local sp = math.max&#40;1, size/20&#41;
im&#58;filledArc&#40;cxy, cxy, sp, sp, 0, 360, black, gd.ARC&#41;

return im
end

dh = os.date&#40;"*t"&#41;
im = createClock&#40;100, dh.hour, dh.min&#41;

im&#58;png&#40;"tmp.png"&#41;


and slightly modified to produce an anti-aliased clock


x=require&#40;"gd"&#41;


function createClock&#40;size, hours, minutes&#41;
size = size * 2
local im = gd.createTrueColor&#40;size, size&#41;
local white = im&#58;colorAllocate&#40;255, 255, 255&#41;
local gray = im&#58;colorAllocate&#40;128, 128, 128&#41;
local black = im&#58;colorAllocate&#40;0, 0, 0&#41;
local blue = im&#58;colorAllocate&#40;0, 0, 128&#41;
local cxy = size/2
im&#58;colorTransparent&#40;white&#41;;
im&#58;filledRectangle&#40;0, 0, size, size,white&#41;
im&#58;setThickness&#40;math.max&#40;1, size/100&#41;&#41;
im&#58;arc&#40;cxy, cxy, size, size, 0, 360, black&#41;

local ang = 0
local rang, gsize
while ang < 360 do
rang = math.rad&#40;ang&#41;
if math.mod&#40;ang, 90&#41; == 0 then
gsize = 0.75
else
gsize = 0.85
end
im&#58;line&#40;
cxy + gsize * cxy * math.sin&#40;rang&#41;,
size - &#40;cxy + gsize * cxy * math.cos&#40;rang&#41;&#41;,
cxy + cxy * 0.9 * math.sin&#40;rang&#41;,
size - &#40;cxy + cxy * 0.9 * math.cos&#40;rang&#41;&#41;,
gray&#41;
ang = ang + 30
end

im&#58;setThickness&#40;math.max&#40;1, size/50&#41;&#41;
im&#58;line&#40;cxy, cxy,
cxy + 0.45 * size * math.sin&#40;math.rad&#40;6*minutes&#41;&#41;,
size - &#40;cxy + 0.45 * size * math.cos&#40;math.rad&#40;6*minutes&#41;&#41;&#41;,
blue&#41;

im&#58;setThickness&#40;math.max&#40;1, size/25&#41;&#41;
rang = math.rad&#40;30*hours + minutes/2&#41;
im&#58;line&#40;cxy, cxy,
cxy + 0.25 * size * math.sin&#40;rang&#41;,
size - &#40;cxy + 0.25 * size * math.cos&#40;rang&#41;&#41;,
blue&#41;

im&#58;setThickness&#40;1&#41;
local sp = math.max&#40;1, size/20&#41;
im&#58;filledArc&#40;cxy, cxy, sp, sp, 0, 360, black, gd.ARC&#41;

local im2 = gd.createTrueColor&#40;size/2, size/2&#41;
im2&#58;copyResampled&#40;im,0,0,0,0,size/2,size/2,size,size&#41;
im=nil
return im2
end

dh = os.date&#40;"*t"&#41;
im = createClock&#40;100, dh.hour, dh.min&#41;

im&#58;png&#40;"tmp.png"&#41;

quixote
April 10th, 2005, 09:53 AM
I've put the 2 .dll files into the main G4 directory, I cut and paste the script, and upon testing it I get the message "LUA success (nothing triggered)". Did I do something wrong, or am I missing something? Thanks.

Ron
April 10th, 2005, 10:05 AM
Nothing triggered only means that there was nothing assigned to the 'true' or 'false' links.

There should have been a popup, displaying the image, using the OSD code in there. Please check if you have a file tmp.png in the girder directory.

quixote
April 10th, 2005, 10:40 AM
there is no tmp.png file there.
I am using Windows XP with service pack 2 (if that's relevant).

Ron
April 10th, 2005, 10:46 AM
That is the same as me. Can you check if the gd library is loaded? (gd table in the inspector).

quixote
April 10th, 2005, 10:57 AM
Yes. It's loaded.

Ron
April 10th, 2005, 02:12 PM
Try this one:


x=require&#40;"gd"&#41;


function createClock&#40;size, hours, minutes&#41;
size = size * 2
local im = gd.createTrueColor&#40;size, size&#41;
local white = im&#58;colorAllocate&#40;255, 255, 255&#41;
local gray = im&#58;colorAllocate&#40;128, 128, 128&#41;
local black = im&#58;colorAllocate&#40;0, 0, 0&#41;
local blue = im&#58;colorAllocate&#40;0, 0, 128&#41;
local cxy = size/2
im&#58;colorTransparent&#40;white&#41;;
im&#58;filledRectangle&#40;0, 0, size, size,white&#41;
im&#58;setThickness&#40;math.max&#40;1, size/100&#41;&#41;
im&#58;arc&#40;cxy, cxy, size, size, 0, 360, black&#41;

local ang = 0
local rang, gsize
while ang < 360 do
rang = math.rad&#40;ang&#41;
if math.mod&#40;ang, 90&#41; == 0 then
gsize = 0.75
else
gsize = 0.85
end
im&#58;line&#40;
cxy + gsize * cxy * math.sin&#40;rang&#41;,
size - &#40;cxy + gsize * cxy * math.cos&#40;rang&#41;&#41;,
cxy + cxy * 0.9 * math.sin&#40;rang&#41;,
size - &#40;cxy + cxy * 0.9 * math.cos&#40;rang&#41;&#41;,
gray&#41;
ang = ang + 30
end

im&#58;setThickness&#40;math.max&#40;1, size/50&#41;&#41;
im&#58;line&#40;cxy, cxy,
cxy + 0.45 * size * math.sin&#40;math.rad&#40;6*minutes&#41;&#41;,
size - &#40;cxy + 0.45 * size * math.cos&#40;math.rad&#40;6*minutes&#41;&#41;&#41;,
blue&#41;

im&#58;setThickness&#40;math.max&#40;1, size/25&#41;&#41;
rang = math.rad&#40;30*hours + minutes/2&#41;
im&#58;line&#40;cxy, cxy,
cxy + 0.25 * size * math.sin&#40;rang&#41;,
size - &#40;cxy + 0.25 * size * math.cos&#40;rang&#41;&#41;,
blue&#41;

im&#58;setThickness&#40;1&#41;
local sp = math.max&#40;1, size/20&#41;
im&#58;filledArc&#40;cxy, cxy, sp, sp, 0, 360, black, gd.ARC&#41;

local im2 = gd.createTrueColor&#40;size/2, size/2&#41;
im2&#58;copyResampled&#40;im,0,0,0,0,size/2,size/2,size,size&#41;
im=nil
return im2
end

dh = os.date&#40;"*t"&#41;
im = createClock&#40;100, dh.hour, dh.min&#41;

im&#58;png&#40;"c&#58;\\tmp.png"&#41;


and check if the file appears in the root of the c drive.

quixote
April 10th, 2005, 06:28 PM
Yes, I can now find the .PNG file.

Ron
April 10th, 2005, 06:31 PM
Great! Then at least the gd library is working. Try modifying the OSD example to include the pathname.

quixote
April 11th, 2005, 09:07 AM
That's quite cool. If I'm feeling brave at some point I may try to give a project that I was thinking of a try. I want to have a graphic indication of movement in my appartment and around it, but I could also do that with regular .bmps that I create in corel paint.