Skip to content

Commit 50779d5

Browse files
committed
Bug fixes and general improvements
1 parent 3437523 commit 50779d5

25 files changed

+579
-654
lines changed

.busted

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
return {
2-
_all = {
3-
directory = "spec",
4-
},
2+
default = {
3+
helper = "./spec/helpers.lua",
4+
coverage = os.getenv("TEST_COVERAGE") == "1"
5+
}
56
}

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
.*.swp
2+
3+
# LuaCov
4+
luacov.stats.out
5+
luacov.report.out

.luacheckrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
files["spec/**/*.lua"] = {
2+
std = "+busted",
3+
}

README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,17 @@ You need:
567567

568568
Then to run the test suite:
569569

570-
(cd spec; for i in *_spec.lua; do luajit $i; done)
570+
busted . -o gtest -v
571571

572-
You can't do `busted .`, unfortunately, since busted `fork()`s between files
573-
and this breaks luajit GC with ffi (I think).
572+
### Linting and static analysis
573+
574+
You need:
575+
576+
luarocks --local install luacheck
577+
578+
Then to run the linter:
579+
580+
luacheck .
574581

575582
### Test
576583

example/hello-world.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
vips = require "vips"
1+
local vips = require "vips"
22

33
-- uncomment for very chatty output
44
-- vips.log.enable(true)
55

6-
image1 = vips.Image.text("Hello <i>World!</i>", {dpi = 300})
6+
local image1 = vips.Image.text("Hello <i>World!</i>", { dpi = 300 })
77
print("writing to x.png ...")
88
image1:write_to_file("x.png")
99

example/noise.lua

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
#!/usr/bin/env luajit
1+
local vips = require "vips"
22

3-
vips = require 'vips'
4-
5-
size = 1024
3+
local size = 1024
64

75
-- perlin's "turbulence" image
8-
function turbulence(size)
9-
local image = nil
10-
local iterations = math.log(size, 2) - 2
6+
local function turbulence(turb_size)
7+
local image
8+
local iterations = math.log(turb_size, 2) - 2
119
for i = 0, iterations do
1210
-- make perlin noise at this scale
13-
local layer = vips.Image.perlin(size, size,
14-
{cell_size = size / math.pow(2, i)})
11+
local layer = vips.Image.perlin(turb_size, turb_size, {
12+
cell_size = turb_size / math.pow(2, i)
13+
})
1514
layer = layer:abs() * (1.0 / (i + 1))
1615

1716
-- and sum
@@ -25,23 +24,23 @@ function turbulence(size)
2524
return image
2625
end
2726

28-
-- make a gradient colour map ... a smooth fade from start to stop, with
27+
-- make a gradient colour map ... a smooth fade from start to stop, with
2928
-- start and stop as CIELAB colours, then map as sRGB
30-
function gradient(start, stop)
29+
local function gradient(start, stop)
3130
local lut = vips.Image.identity() / 255
3231
lut = lut * start + (lut * -1 + 1) * stop
33-
return lut:colourspace("srgb", {source_space = "lab"})
32+
return lut:colourspace("srgb", { source_space = "lab" })
3433
end
3534

3635
-- make a turbulent stripe pattern
37-
stripe = vips.Image.xyz(size, size):extract_band(0)
36+
local stripe = vips.Image.xyz(size, size):extract_band(0)
3837
stripe = (stripe * 360 * 4 / size + turbulence(size) * 700):sin()
3938

4039
-- make a colour map ... we want a smooth gradient from white to dark brown
4140
-- colours here in CIELAB
42-
dark_brown = {7.45, 4.3, 8}
43-
white = {100, 0, 0}
44-
lut = gradient(dark_brown, white)
41+
local dark_brown = { 7.45, 4.3, 8 }
42+
local white = { 100, 0, 0 }
43+
local lut = gradient(dark_brown, white)
4544

4645
-- rescale to 0 - 255 and colour with our lut
4746
stripe = ((stripe + 1) * 128):maplut(lut)

spec/cache_spec.lua

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
-- test cache control
2-
3-
require 'busted.runner'()
1+
local vips = require "vips"
42

3+
-- test cache control
54
describe("cache control", function()
6-
vips = require("vips")
7-
--vips.log.enable(true)
5+
6+
setup(function()
7+
-- vips.log.enable(true)
8+
end)
89

910
it("can set number of operations to cache", function()
1011
local max = vips.get_max()
@@ -29,5 +30,4 @@ describe("cache control", function()
2930
assert.are.equal(vips.get_max_mem(), 10)
3031
vips.set_max_mem(max)
3132
end)
32-
3333
end)

spec/convenience_spec.lua

+27-57
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,48 @@
1-
-- test image writers
2-
3-
require 'busted.runner'()
4-
5-
say = require("say")
6-
7-
local function almost_equal(state, arguments)
8-
local has_key = false
9-
local threshold = arguments[3] or 0.001
10-
11-
if type(arguments[1]) ~= "number" or type(arguments[2]) ~= "number" then
12-
return false
13-
end
14-
15-
return math.abs(arguments[1] - arguments[2]) < threshold
16-
end
17-
18-
say:set("assertion.almost_equal.positive",
19-
"Expected %s to almost equal %s")
20-
say:set("assertion.almost_equal.negative",
21-
"Expected %s to not almost equal %s")
22-
assert:register("assertion", "almost_equal", almost_equal,
23-
"assertion.almost_equal.positive",
24-
"assertion.almost_equal.negative")
1+
local vips = require "vips"
252

3+
-- test convenience functions
264
describe("test convenience functions", function()
27-
vips = require("vips")
28-
-- vips.log.enable(true)
5+
local array, im
296

30-
local array = {1, 2, 3, 4}
31-
local im = vips.Image.new_from_array(array)
7+
setup(function()
8+
array = { 1, 2, 3, 4 }
9+
im = vips.Image.new_from_array(array)
10+
-- vips.log.enable(true)
11+
end)
3212

33-
it("can join one image bandwise", function ()
13+
it("can join one image bandwise", function()
3414
local im2 = im:bandjoin(im)
3515

3616
assert.are.equal(im2:width(), 4)
3717
assert.are.equal(im2:height(), 1)
3818
assert.are.equal(im2:bands(), 2)
3919
assert.are.equal(im2:extract_band(0):avg(), 2.5)
4020
assert.are.equal(im2:extract_band(1):avg(), 2.5)
41-
4221
end)
4322

44-
it("can join images bandwise", function ()
45-
local im2 = im:bandjoin{im + 1, im + 2}
23+
it("can join images bandwise", function()
24+
local im2 = im:bandjoin { im + 1, im + 2 }
4625

4726
assert.are.equal(im2:width(), 4)
4827
assert.are.equal(im2:height(), 1)
4928
assert.are.equal(im2:bands(), 3)
5029
assert.are.equal(im2:extract_band(0):avg(), 2.5)
5130
assert.are.equal(im2:extract_band(1):avg(), 3.5)
5231
assert.are.equal(im2:extract_band(2):avg(), 4.5)
53-
5432
end)
5533

56-
it("can join constants to images bandwise", function ()
34+
it("can join constants to images bandwise", function()
5735
local im2 = im:bandjoin(255)
5836

5937
assert.are.equal(im2:width(), 4)
6038
assert.are.equal(im2:height(), 1)
6139
assert.are.equal(im2:bands(), 2)
6240
assert.are.equal(im2:extract_band(0):avg(), 2.5)
6341
assert.are.equal(im2:extract_band(1):avg(), 255)
64-
6542
end)
6643

67-
it("can join images and constants bandwise", function ()
68-
local im2 = im:bandjoin{im + 1, 255, im + 2}
44+
it("can join images and constants bandwise", function()
45+
local im2 = im:bandjoin { im + 1, 255, im + 2 }
6946

7047
assert.are.equal(im2:width(), 4)
7148
assert.are.equal(im2:height(), 1)
@@ -74,11 +51,10 @@ describe("test convenience functions", function()
7451
assert.are.equal(im2:extract_band(1):avg(), 3.5)
7552
assert.are.equal(im2:extract_band(2):avg(), 255)
7653
assert.are.equal(im2:extract_band(3):avg(), 4.5)
77-
7854
end)
7955

80-
it("can join images and array constants bandwise", function ()
81-
local im2 = im:bandjoin{im + 1, {255, 128}}
56+
it("can join images and array constants bandwise", function()
57+
local im2 = im:bandjoin { im + 1, { 255, 128 } }
8258

8359
assert.are.equal(im2:width(), 4)
8460
assert.are.equal(im2:height(), 1)
@@ -87,12 +63,11 @@ describe("test convenience functions", function()
8763
assert.are.equal(im2:extract_band(1):avg(), 3.5)
8864
assert.are.equal(im2:extract_band(2):avg(), 255)
8965
assert.are.equal(im2:extract_band(3):avg(), 128)
90-
9166
end)
9267

93-
if vips.version.at_least(8, 6) then
94-
it("can call composite", function ()
95-
local base = (im + {10, 11, 12}):copy{interpretation = "srgb"}
68+
it("can call composite", function()
69+
if vips.version.at_least(8, 6) then
70+
local base = (im + { 10, 11, 12 }):copy { interpretation = "srgb" }
9671
local overlay = (base + 10):bandjoin(128)
9772
local comp = base:composite(overlay, "over")
9873
local pixel = comp:getpoint(0, 0)
@@ -104,48 +79,43 @@ describe("test convenience functions", function()
10479
assert.is_true(math.abs(pixel[2] - 17) < 0.1)
10580
assert.is_true(math.abs(pixel[3] - 18) < 0.1)
10681
assert.are.equal(pixel[4], 255)
107-
end)
108-
end
82+
end
83+
end)
10984

110-
it("can call bandrank", function ()
111-
local im2 = im:bandrank(im + 1, {index = 0})
85+
it("can call bandrank", function()
86+
local im2 = im:bandrank(im + 1, { index = 0 })
11287

11388
assert.are.equal(im2:width(), 4)
11489
assert.are.equal(im2:height(), 1)
11590
assert.are.equal(im2:bands(), 1)
11691
assert.are.equal(im2:extract_band(0):avg(), 2.5)
117-
11892
end)
11993

120-
it("can call bandsplit", function ()
121-
local bands = im:bandjoin{im + 1, {255, 128}}:bandsplit()
94+
it("can call bandsplit", function()
95+
local bands = im:bandjoin { im + 1, { 255, 128 } }:bandsplit()
12296

12397
assert.are.equal(#bands, 4)
12498
assert.are.equal(bands[1]:width(), 4)
12599
assert.are.equal(bands[1]:height(), 1)
126100
assert.are.equal(bands[1]:bands(), 1)
127-
128101
end)
129102

130-
it("can call ifthenelse with an image and two constants", function ()
103+
it("can call ifthenelse with an image and two constants", function()
131104
local result = im:more(2):ifthenelse(1, 2)
132105

133106
assert.are.equal(result:width(), 4)
134107
assert.are.equal(result:height(), 1)
135108
assert.are.equal(result:bands(), 1)
136109
assert.are.equal(result:avg(), 6 / 4)
137-
138110
end)
139111

140-
it("can call ifthenelse with two images and one constant", function ()
112+
it("can call ifthenelse with two images and one constant", function()
141113
local result = im:more(2):ifthenelse(im + 3, 2)
142114

143115
assert.are.equal(result:width(), 4)
144116
assert.are.equal(result:height(), 1)
145117
assert.are.equal(result:bands(), 1)
146118
assert.are.equal(result:avg(), 17 / 4)
147-
148119
end)
149-
150120
end)
151121

spec/enum_spec.lua

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
-- test metadata read/write
2-
3-
require 'busted.runner'()
1+
local vips = require "vips"
42

3+
-- test metadata read/write
54
describe("enum expansions", function()
6-
vips = require("vips")
7-
--vips.log.enable(true)
5+
local array, im
86

9-
local array = {1, 2, 3, 4}
10-
local im = vips.Image.new_from_array(array)
7+
setup(function()
8+
array = { 1, 2, 3, 4 }
9+
im = vips.Image.new_from_array(array)
10+
-- vips.log.enable(true)
11+
end)
1112

1213
-- there are loads of expansions, just test one of each type
1314

@@ -18,7 +19,6 @@ describe("enum expansions", function()
1819
assert.are.equal(im2:height(), 1)
1920
assert.are.equal(im2:bands(), 1)
2021
assert.are.equal(im2:avg(), (1 + 4 + 9 + 16) / 4)
21-
2222
end)
2323

2424
it("can call pow() with an image arg", function()
@@ -28,7 +28,6 @@ describe("enum expansions", function()
2828
assert.are.equal(im2:height(), 1)
2929
assert.are.equal(im2:bands(), 1)
3030
assert.are.equal(im2:avg(), (1 ^ 1 + 2 ^ 2 + 3 ^ 3 + 4 ^ 4) / 4)
31-
3231
end)
3332

3433
it("can call lshift()", function()
@@ -38,7 +37,6 @@ describe("enum expansions", function()
3837
assert.are.equal(im2:height(), 1)
3938
assert.are.equal(im2:bands(), 1)
4039
assert.are.equal(im2:avg(), (2 + 4 + 6 + 8) / 4)
41-
4240
end)
4341

4442
it("can call less()", function()
@@ -48,7 +46,5 @@ describe("enum expansions", function()
4846
assert.are.equal(im2:height(), 1)
4947
assert.are.equal(im2:bands(), 1)
5048
assert.are.equal(im2:avg(), (255 + 0 + 0 + 0) / 4)
51-
5249
end)
53-
5450
end)

0 commit comments

Comments
 (0)