Skip to content

Commit

Permalink
Add test from memory source
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandlo committed Feb 5, 2025
1 parent 912a2e7 commit 48171c2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
31 changes: 25 additions & 6 deletions spec/connection_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,42 @@ local vips = require "vips"
local ffi = require "ffi"

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

describe("test connection", function()
setup(function()
-- vips.log.enable(true)
end)

describe("to file target", function()
it("can create image from file source and write to file target", function()
local target

setup(function()
target = vips.Target.new_to_file(TMP_FILE)
end)

it("can create image from file source", function()
local source = vips.Source.new_from_file(JPEG_FILE)
local image = vips.Image.new_from_source(source, '', { access = 'sequential' })
local filename = ffi.os == "Windows" and os.getenv("TMP") .. "\\x.png" or "/tmp/x.png"
local target = vips.Target.new_to_file(filename)
image:write_to_target(target, '.png')

local image1 = vips.Image.new_from_file(JPEG_FILE, { access = 'sequential' })
local image2 = vips.Image.new_from_file(filename, { access = 'sequential' })
local image2 = vips.Image.new_from_file(TMP_FILE, { access = 'sequential' })
assert.is_true((image1 - image2):abs():max() < 10)
end)

it("can create image from memory source", function()
local file = assert(io.open(JPEG_FILE, "rb"))
local content = file:read("*a")
file:close()
local mem = ffi.new("unsigned char[?]", #content)
ffi.copy(mem, content, #content)
local source = vips.Source.new_from_memory(mem)
local image = vips.Image.new_from_source(source, '', { access = 'sequential' })
image:write_to_target(target, '.png')

local image1 = vips.Image.new_from_file(JPEG_FILE, { access = 'sequential' })
local image2 = vips.Image.new_from_file(TMP_FILE, { access = 'sequential' })
assert.is_true((image1 - image2):abs():max() < 10)
end)
end)
Expand Down
2 changes: 1 addition & 1 deletion src/vips/Source.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Source.new_from_file = function(filename)
return Connection.new(source)
end

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

0 comments on commit 48171c2

Please sign in to comment.