From 48171c23cad1402192b22c094e3612188ed4edae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20L=C3=B6tscher?= Date: Tue, 4 Feb 2025 06:26:52 +0100 Subject: [PATCH] Add test from memory source --- spec/connection_spec.lua | 31 +++++++++++++++++++++++++------ src/vips/Source.lua | 2 +- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/spec/connection_spec.lua b/spec/connection_spec.lua index be00703..44c65be 100644 --- a/spec/connection_spec.lua +++ b/spec/connection_spec.lua @@ -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) diff --git a/src/vips/Source.lua b/src/vips/Source.lua index f6ddfe1..a642f65 100644 --- a/src/vips/Source.lua +++ b/src/vips/Source.lua @@ -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())