-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit allows creating image sources from descriptor, file or memory and image targets to descriptor, file or memory. Custom classes are not implemented
- Loading branch information
Showing
12 changed files
with
336 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
local vips = require "vips" | ||
|
||
if #arg ~= 2 then | ||
error("Usage: lua target.lua ~/pics/k2.png .avif > x") | ||
end | ||
|
||
local infilename = arg[1] | ||
local fmt = arg[2] | ||
|
||
local descriptor = { | ||
stdin = 0, | ||
stdout = 1, | ||
stderr = 2, | ||
} | ||
|
||
local image = vips.Image.new_from_file(infilename) | ||
local target = vips.Target.new_to_descriptor(descriptor.stdout) | ||
image:write_to_target(target, fmt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
local vips = require "vips" | ||
local ffi = require "ffi" | ||
|
||
local JPEG_FILE = "./spec/images/Gugg_coloured.jpg" | ||
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() | ||
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' }) | ||
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) | ||
|
||
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) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
-- abstract base Connection class | ||
|
||
local ffi = require "ffi" | ||
|
||
local vobject = require "vips.vobject" | ||
|
||
local vips_lib = ffi.load(ffi.os == "Windows" and "libvips-42.dll" or "vips") | ||
|
||
local Connection_method = {} | ||
|
||
local Connection = { | ||
mt = { | ||
__index = Connection_method, | ||
} | ||
} | ||
|
||
function Connection.mt:__tostring() | ||
return self:filename() or self:nick() or "(nil)" | ||
end | ||
|
||
Connection.new = function(vconnection) | ||
local connection = {} | ||
connection.vconnection = vobject.new(vconnection) | ||
return setmetatable(connection, Connection.mt) | ||
end | ||
function Connection_method:vobject() | ||
return ffi.cast(vobject.typeof, self.vconnection) | ||
end | ||
|
||
function Connection_method:filename() | ||
-- Get the filename asscoiated with a connection. Return nil if there is no associated file. | ||
local so = ffi.cast('VipsConnection *', self.vconnection) | ||
local filename = vips_lib.vips_connection_filename(so) | ||
if filename == ffi.NULL then | ||
return nil | ||
else | ||
return ffi.string(filename) | ||
end | ||
end | ||
|
||
function Connection_method:nick() | ||
-- Make a human-readable name for a connection suitable for error messages. | ||
|
||
local so = ffi.cast('VipsConnection *', self.vconnection) | ||
local nick = vips_lib.vips_connection_nick(so) | ||
if nick == ffi.NULL then | ||
return nil | ||
else | ||
return ffi.string(nick) | ||
end | ||
end | ||
|
||
return ffi.metatype("VipsConnection", { | ||
__index = Connection | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
-- An output connection | ||
|
||
local ffi = require "ffi" | ||
|
||
local verror = require "vips.verror" | ||
local Connection = require "vips.Connection" | ||
|
||
local vips_lib = ffi.load(ffi.os == "Windows" and "libvips-42.dll" or "vips") | ||
|
||
local Source = {} | ||
|
||
Source.new_from_descriptor = function(descriptor) | ||
local source = vips_lib.vips_source_new_from_descriptor(descriptor) | ||
if source == ffi.NULL then | ||
error("Can't create source from descriptor " .. descriptor .. "\n" .. verror.get()) | ||
end | ||
|
||
return Connection.new(source) | ||
end | ||
|
||
Source.new_from_file = function(filename) | ||
local source = vips_lib.vips_source_new_from_file(filename) | ||
if source == ffi.NULL then | ||
error("Can't create source from filename " .. filename .. "\n" .. verror.get()) | ||
end | ||
|
||
return Connection.new(source) | ||
end | ||
|
||
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()) | ||
end | ||
|
||
return Connection.new(source) | ||
end | ||
|
||
return ffi.metatype("VipsSource", { | ||
__index = Source | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
-- An input connection | ||
|
||
local ffi = require "ffi" | ||
|
||
local Connection = require "vips.Connection" | ||
|
||
local vips_lib = ffi.load(ffi.os == "Windows" and "libvips-42.dll" or "vips") | ||
|
||
local Target = {} | ||
|
||
Target.new_to_descriptor = function(descriptor) | ||
collectgarbage("stop") | ||
local target = vips_lib.vips_target_new_to_descriptor(descriptor) | ||
collectgarbage("restart") | ||
if target == ffi.NULL then | ||
error("can't create output target from descriptor " .. descriptor) | ||
else | ||
return Connection.new(target) | ||
end | ||
end | ||
|
||
Target.new_to_file = function(filename) | ||
collectgarbage("stop") | ||
local target = vips_lib.vips_target_new_to_file(filename) | ||
collectgarbage("restart") | ||
if target == ffi.NULL then | ||
error("can't create output target from filename " .. filename) | ||
else | ||
return Connection.new(target) | ||
end | ||
end | ||
|
||
Target.new_to_memory = function() | ||
collectgarbage("stop") | ||
local target = vips_lib.vips_target_new_to_memory() | ||
collectgarbage("restart") | ||
if target == ffi.NULL then | ||
error("can't create output target from memory") | ||
else | ||
return Connection.new(target) | ||
end | ||
end | ||
|
||
return ffi.metatype("VipsTarget", { | ||
__index = Target | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.