Skip to content

Commit 1c2a51d

Browse files
committed
Add test from memory source
1 parent 5bebaf3 commit 1c2a51d

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

spec/connection_spec.lua

+25-6
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,42 @@ local vips = require "vips"
22
local ffi = require "ffi"
33

44
local JPEG_FILE = "./spec/images/Gugg_coloured.jpg"
5-
-- test gvalue
6-
describe("test connection", function()
5+
local TMP_FILE = ffi.os == "Windows" and os.getenv("TMP") .. "\\x.png" or "/tmp/x.png"
76

7+
describe("test connection", function()
88
setup(function()
99
-- vips.log.enable(true)
1010
end)
1111

1212
describe("to file target", function()
13-
it("can create image from file source and write to file target", function()
13+
local target
14+
15+
setup(function()
16+
target = vips.Target.new_to_file(TMP_FILE)
17+
end)
18+
19+
it("can create image from file source", function()
1420
local source = vips.Source.new_from_file(JPEG_FILE)
1521
local image = vips.Image.new_from_source(source, '', { access = 'sequential' })
16-
local filename = ffi.os == "Windows" and os.getenv("TMP") .. "\\x.png" or "/tmp/x.png"
17-
local target = vips.Target.new_to_file(filename)
1822
image:write_to_target(target, '.png')
1923

2024
local image1 = vips.Image.new_from_file(JPEG_FILE, { access = 'sequential' })
21-
local image2 = vips.Image.new_from_file(filename, { access = 'sequential' })
25+
local image2 = vips.Image.new_from_file(TMP_FILE, { access = 'sequential' })
26+
assert.is_true((image1 - image2):abs():max() < 10)
27+
end)
28+
29+
it("can create image from memory source", function()
30+
local file = assert(io.open(JPEG_FILE, "rb"))
31+
local content = file:read("*a")
32+
file:close()
33+
local mem = ffi.new("unsigned char[?]", #content)
34+
ffi.copy(mem, content, #content)
35+
local source = vips.Source.new_from_memory(mem)
36+
local image = vips.Image.new_from_source(source, '', { access = 'sequential' })
37+
image:write_to_target(target, '.png')
38+
39+
local image1 = vips.Image.new_from_file(JPEG_FILE, { access = 'sequential' })
40+
local image2 = vips.Image.new_from_file(TMP_FILE, { access = 'sequential' })
2241
assert.is_true((image1 - image2):abs():max() < 10)
2342
end)
2443
end)

src/vips/Source.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Source.new_from_file = function(filename)
2727
return Connection.new(source)
2828
end
2929

30-
Source.new_from_memory = function(data) -- data is an FFI memory array formatted as a C-style array
30+
Source.new_from_memory = function(data) -- data is an FFI memory array containing the image data
3131
local source = vips_lib.vips_source_new_from_memory(data, ffi.sizeof(data))
3232
if source == ffi.NULL then
3333
error("Can't create input source from memory \n" .. verror.get())

0 commit comments

Comments
 (0)