Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
Update the coding style used in the examples and fixes some undesired
behaviors (eg. overwriting versioned files, polluting the directory,
etc.).
  • Loading branch information
ittner committed Oct 4, 2013
1 parent 04f6deb commit 2ab4a08
Show file tree
Hide file tree
Showing 24 changed files with 102 additions and 97 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
*.so
*.lo
*.tar.gz

demos/out.png
demos/out.gif
demos/counter.txt

8 changes: 4 additions & 4 deletions demos/brush.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local gd = require("gd")

im = gd.createTrueColor(400, 400)
local im = gd.createTrueColor(400, 400)
assert(im)

black = im:colorAllocate(0, 0, 0)
white = im:colorAllocate(255, 255, 255)
local black = im:colorAllocate(0, 0, 0)
local white = im:colorAllocate(255, 255, 255)

brush = gd.createFromPng("paper.png")
local brush = gd.createFromPng("paper.png")
im:setBrush(brush)
im:line(60, 60, 70, 70, gd.BRUSHED)
im:line(120, 120, 130, 130, gd.BRUSHED)
Expand Down
6 changes: 3 additions & 3 deletions demos/circle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

local gd = require("gd")

im = gd.createTrueColor(100, 100)
local im = gd.createTrueColor(100, 100)

white = im:colorAllocate(255, 255, 255)
blue = im:colorAllocate(0, 0, 255)
local white = im:colorAllocate(255, 255, 255)
local blue = im:colorAllocate(0, 0, 255)

im:filledRectangle(0, 0, 100, 100, white)
im:stringFTCircle(50, 50, 40, 10, 0.9, "./Vera.ttf", 24, "Powered", "by Lua", blue)
Expand Down
6 changes: 3 additions & 3 deletions demos/clock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

local gd = require("gd")

function createClock(size, hours, minutes)
local 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)
Expand Down Expand Up @@ -55,8 +55,8 @@ function createClock(size, hours, minutes)
return im
end

dh = os.date("*t")
im = createClock(100, dh.hour, dh.min)
local dh = os.date("*t")
local im = createClock(100, dh.hour, dh.min)

print("Content-type: image/png")
print("Refresh: 60") -- Ask browser to reload the image after 60s
Expand Down
14 changes: 7 additions & 7 deletions demos/counter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@

local gd = require("gd")

datafile = "counter.txt"
fp = io.open(datafile, "r+")
local datafile = "counter.txt"
local fp = io.open(datafile, "r+")
local cnt = 0
if fp then
cnt = tonumber(fp:read("*l")) or 0
fp:seek("set", 0)
else
cnt = 0
fp = io.open(datafile, "w")
assert(fp)
end
cnt = cnt + 1
fp:write(cnt .."\n")
fp:close()

sx = math.max(string.len(tostring(cnt)), 1) * 8
im = gd.create(sx, 15)
local sx = math.max(string.len(tostring(cnt)), 1) * 8
local im = gd.create(sx, 15)
-- first allocated color defines the background.
white = im:colorAllocate(255, 255, 255)
local white = im:colorAllocate(255, 255, 255)
im:colorTransparent(white)
black = im:colorAllocate(0, 0, 0)
local black = im:colorAllocate(0, 0, 0)
im:string(gd.FONT_MEDIUM, 1, 1, cnt, black)

print("Content-type: image/png\n")
Expand Down
1 change: 0 additions & 1 deletion demos/counter.txt

This file was deleted.

6 changes: 3 additions & 3 deletions demos/ellipse.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local gd = require("gd")
im = gd.createTrueColor(80, 80)
local im = gd.createTrueColor(80, 80)
assert(im)

black = im:colorAllocate(0, 0, 0)
white = im:colorAllocate(255, 255, 255)
local black = im:colorAllocate(0, 0, 0)
local white = im:colorAllocate(255, 255, 255)
im:filledEllipse(40, 40, 70, 50, white)

im:png("out.png")
Expand Down
8 changes: 4 additions & 4 deletions demos/fontconfig.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

local gd = require("gd")

im = gd.createTrueColor(220, 190)
white = im:colorAllocate(255, 255, 255)
black = im:colorAllocate(0, 0, 0)
x, y = im:sizeXY()
local im = gd.createTrueColor(220, 190)
local white = im:colorAllocate(255, 255, 255)
local black = im:colorAllocate(0, 0, 0)
local x, y = im:sizeXY()
im:filledRectangle(0, 0, x, y, white)

gd.useFontConfig(true)
Expand Down
6 changes: 3 additions & 3 deletions demos/gifanim.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
local gd = require("gd")

im = gd.createPalette(80, 80)
local im = gd.createPalette(80, 80)
assert(im)

black = im:colorAllocate(0, 0, 0)
white = im:colorAllocate(255, 255, 255)
local black = im:colorAllocate(0, 0, 0)
local white = im:colorAllocate(255, 255, 255)
im:gifAnimBegin("out.gif", true, 0)

for i = 1, 10 do
Expand Down
6 changes: 3 additions & 3 deletions demos/gifanim2.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
local gd = require("gd")

im = gd.createPalette(120, 120)
local im = gd.createPalette(120, 120)
assert(im)

black = im:colorAllocate(0, 0, 0)
blue = {}
local black = im:colorAllocate(0, 0, 0)
local blue = {}
for i = 1, 20 do
blue[i] = im:colorAllocate(0, 0, 120+6*i)
end
Expand Down
8 changes: 4 additions & 4 deletions demos/gifanim3.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
local gd = require("gd")

im = gd.createPalette(120, 120)
local im = gd.createPalette(120, 120)
assert(im)

black = im:colorAllocate(0, 0, 0)
blue = {}
local black = im:colorAllocate(0, 0, 0)
local blue = {}
for i = 1, 20 do
blue[i] = im:colorAllocate(0, 0, 120+6*i)
end

fp = io.open("out.gif", "w")
local fp = io.open("out.gif", "w")
assert(fp, "Failed to open file for writting")

fp:write(im:gifAnimBeginStr(true, 0))
Expand Down
Binary file removed demos/grid.png
Binary file not shown.
5 changes: 2 additions & 3 deletions demos/lualogo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ function makelogo(size)
im:filledArc(cxy, cxy, ediam, ediam, 0, 360, blue, gd.ARC)

im:setThickness(math.max(0.02 * ediam, 1))
local i
for i = 0, 360, 10 do
im:arc(cxy, cxy, odiam, odiam, i, i+5, gray)
end
Expand All @@ -57,5 +56,5 @@ function makelogo(size)
return im2
end

makelogo(140):png("lualogo.png")
os.execute("display lualogo.png")
makelogo(140):png("out.png")
os.execute("display out.png")
Binary file removed demos/lualogo.png
Binary file not shown.
6 changes: 3 additions & 3 deletions demos/poly.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
local gd = require("gd")

im = gd.createTrueColor(80, 80)
local im = gd.createTrueColor(80, 80)
assert(im)

black = im:colorAllocate(0, 0, 0)
white = im:colorAllocate(255, 255, 255)
local black = im:colorAllocate(0, 0, 0)
local white = im:colorAllocate(255, 255, 255)

im:polygon( { { 10, 10 }, { 10, 20 }, { 20, 20 }, { 20, 10 } }, white)
im:filledPolygon( { { 30, 30 }, { 30, 40 }, { 40, 40 }, { 40, 30 } }, white)
Expand Down
3 changes: 2 additions & 1 deletion demos/setstyle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ for i = 0, 100, 2 do
im:line(i, i, i+50, i, gd.STYLED)
end

im:png("styled.png")
im:png("out.png")
os.execute("display out.png")


11 changes: 6 additions & 5 deletions demos/stdfont.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
local gd = require("gd")

x, y = 140, 110
local x, y = 140, 110

im = gd.createPalette(x, y)
white = im:colorAllocate(255, 255, 255)
black = im:colorAllocate(0, 0, 0)
local im = gd.createPalette(x, y)
local white = im:colorAllocate(255, 255, 255)
local black = im:colorAllocate(0, 0, 0)

im:string(gd.FONT_TINY, 10, 10, "gd.FONT_TINY", black)
im:string(gd.FONT_SMALL, 10, 20, "gd.FONT_SMALL", black)
Expand All @@ -15,5 +15,6 @@ im:string(gd.FONT_GIANT, 10, 65, "gd.FONT_GIANT", black)
im:line(60, 93, 70, 93, black)
im:string(gd.FONT_SMALL, 80, 86, "= 10 px", black)

im:png("stdfonts.png")
im:png("out.png")
os.execute("display out.png")

26 changes: 13 additions & 13 deletions demos/steg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ to stop. Of course, this program will also do this boring job for Bob.
local gd = require("gd")


function getLSB(n)
local function getLSB(n)
return math.mod(n, 2) ~= 0
end


-- Bizarre way to do some bit-level operations without bitlib.
function setLSB(n, b)
local function setLSB(n, b)
if type(b) == "number" then
if b == 0 then
b = false
Expand Down Expand Up @@ -124,7 +124,7 @@ function setLSB(n, b)
end


function intToBitArray(n)
local function intToBitArray(n)
local ret = {}
local i = 0
while n ~= 0 do
Expand All @@ -137,7 +137,7 @@ function intToBitArray(n)
end


function printBitArray(a)
local function printBitArray(a)
local i
for i = a.size,0,-1 do
if a[i] then
Expand All @@ -149,7 +149,7 @@ function printBitArray(a)
end


function mergeMessage(im, msg)
local function mergeMessage(im, msg)
local w, h = im:sizeXY()
msg = msg .. string.char(0)
local len = string.len(msg)
Expand Down Expand Up @@ -204,7 +204,7 @@ function mergeMessage(im, msg)
end


function getMessage(im)
local function getMessage(im)
local msg = {}
local w, h = im:sizeXY()
local x, y = 0, 0
Expand Down Expand Up @@ -245,7 +245,7 @@ function getMessage(im)
end


function compare(fimg1, fimg2)
local function compare(fimg1, fimg2)
local im1 = gd.createFromPng(fimg1)
if not im1 then
print("ERROR: " .. fimg1 .. " bad PNG data.")
Expand Down Expand Up @@ -288,7 +288,7 @@ function compare(fimg1, fimg2)
end


function usage()
local function usage()
print("Usage:")
print(" lua steg.lua hide <input file> <output file>")
print(" lua steg.lua show <input file>")
Expand All @@ -308,7 +308,7 @@ if not arg[1] or not arg[2] then
end

if arg[1] == "show" then
im = gd.createFromPng(arg[2])
local im = gd.createFromPng(arg[2])
if not im then
print("ERROR: Bad image data.")
os.exit(1)
Expand All @@ -322,14 +322,14 @@ if arg[1] == "hide" then
usage()
os.exit(1)
end
im = gd.createFromPng(arg[2])
local im = gd.createFromPng(arg[2])
if not im then
print("ERROR: Bad image data.")
os.exit(1)
end
print("Type your message and press CTRL+D to finish.")
msg = io.read("*a")
oim, l, t = mergeMessage(im, msg)
local msg = io.read("*a")
local oim, l, t = mergeMessage(im, msg)
if not oim then
print("ERROR: Image is too small for the message.")
os.exit(1)
Expand All @@ -348,7 +348,7 @@ if arg[1] == "diff" then
usage()
os.exit(1)
end
oim = compare(arg[2], arg[3])
local oim = compare(arg[2], arg[3])
if not oim:png(arg[4]) then
print("ERROR: Failed to write output file.")
os.exit(1)
Expand Down
16 changes: 8 additions & 8 deletions demos/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ local gd = require("gd")

math.randomseed(os.time())

im = gd.createFromJpeg("./bugs.jpg")
local im = gd.createFromJpeg("./bugs.jpg")
assert(im)
sx, sy = im:sizeXY()
local sx, sy = im:sizeXY()

im2 = gd.createTrueColor(2*sx, sy)
black = im2:colorAllocate(0, 0, 0)
white = im2:colorAllocate(255, 255, 255)
local im2 = gd.createTrueColor(2*sx, sy)
local black = im2:colorAllocate(0, 0, 0)
local white = im2:colorAllocate(255, 255, 255)
gd.copy(im2, im, 0, 0, 0, 0, sx, sy, sx, sy)

sx2, sy2 = im2:sizeXY()
local sx2, sy2 = im2:sizeXY()
im2:stringUp(gd.FONT_SMALL, 5, sy2-10, gd.VERSION, white)

for i = 0, 14 do
for j = 0, 24 do
rcl = im2:colorAllocate(math.random(255), math.random(255),
local rcl = im2:colorAllocate(math.random(255), math.random(255),
math.random(255))
im2:filledRectangle(sx+20+j*10, i*20+40, sx+30+j*10, i*20+50, rcl)
end
end

im2:string(gd.FONT_GIANT, sx+80, 10, "Powered by Lua", white)

blackTr = im2:colorAllocateAlpha(0, 0, 0, 80)
local blackTr = im2:colorAllocateAlpha(0, 0, 0, 80)
im2:stringFT(blackTr, "./Vera.ttf", 140, 0, 70, 130, "gd")
im2:stringFT(white, "./Vera.ttf", 45, math.pi/5, 340, 250, "FreeType")

Expand Down
Loading

0 comments on commit 2ab4a08

Please sign in to comment.