Skip to content

Commit

Permalink
Improve naming for gtype
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandlo committed Mar 25, 2024
1 parent 22b0b90 commit 3575ad6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
63 changes: 33 additions & 30 deletions src/vips/gvalue.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ gvalue.blob_type = gobject_lib.g_type_from_name("VipsBlob")
gvalue.band_format_type = gobject_lib.g_type_from_name("VipsBandFormat")
gvalue.blend_mode_type = version.at_least(8, 6) and gobject_lib.g_type_from_name("VipsBlendMode") or 0

-- gvalue.*_type can be of type cdata or number depending on the OS and Lua version
-- gtypes as returned by vips_lib can also be of type cdata or number
-- cdata and number are not comparable with Standard Lua (using luaffi-tkl)
gvalue.comparable_type = type(gvalue.gdouble_type) == "number" and
function(gtype) return tonumber(gtype) end or
function(gtype) return gtype end
Expand Down Expand Up @@ -86,27 +89,27 @@ gvalue.init = function(gv, gtype)
end

gvalue.set = function(gv, value)
local gtype_raw = gv.gtype
local gtype = gvalue.comparable_type(gtype_raw)
local fundamental = gobject_lib.g_type_fundamental(gtype_raw)
local gtype = gv.gtype
local gtype_comp = gvalue.comparable_type(gtype)
local fundamental = gobject_lib.g_type_fundamental(gtype)

if gtype == gvalue.gbool_type then
if gtype_comp == gvalue.gbool_type then
gobject_lib.g_value_set_boolean(gv, value)
elseif gtype == gvalue.gint_type then
elseif gtype_comp == gvalue.gint_type then
gobject_lib.g_value_set_int(gv, value)
elseif gtype == gvalue.gdouble_type then
elseif gtype_comp == gvalue.gdouble_type then
gobject_lib.g_value_set_double(gv, value)
elseif fundamental == gvalue.genum_type then
gobject_lib.g_value_set_enum(gv, gvalue.to_enum(gtype_raw, value))
gobject_lib.g_value_set_enum(gv, gvalue.to_enum(gtype, value))
elseif fundamental == gvalue.gflags_type then
gobject_lib.g_value_set_flags(gv, value)
elseif gtype == gvalue.gstr_type then
elseif gtype_comp == gvalue.gstr_type then
gobject_lib.g_value_set_string(gv, value)
elseif gtype == gvalue.refstr_type then
elseif gtype_comp == gvalue.refstr_type then
gobject_lib.vips_value_set_ref_string(gv, value)
elseif gtype == gvalue.image_type then
elseif gtype_comp == gvalue.image_type then
gobject_lib.g_value_set_object(gv, value.vimage)
elseif gtype == gvalue.array_int_type then
elseif gtype_comp == gvalue.array_int_type then
if type(value) == "number" then
value = { value }
end
Expand All @@ -115,7 +118,7 @@ gvalue.set = function(gv, value)
local a = ffi.new(gvalue.int_arr_typeof, n, value)

vips_lib.vips_value_set_array_int(gv, a, n)
elseif gtype == gvalue.array_double_type then
elseif gtype_comp == gvalue.array_double_type then
if type(value) == "number" then
value = { value }
end
Expand All @@ -124,7 +127,7 @@ gvalue.set = function(gv, value)
local a = ffi.new(gvalue.double_arr_typeof, n, value)

vips_lib.vips_value_set_array_double(gv, a, n)
elseif gtype == gvalue.array_image_type then
elseif gtype_comp == gvalue.array_image_type then
if Image.is_Image(value) then
value = { value }
end
Expand All @@ -139,7 +142,7 @@ gvalue.set = function(gv, value)
-- the gvalue needs a set of refs to own
gobject_lib.g_object_ref(a[i])
end
elseif gtype == gvalue.blob_type then
elseif gtype_comp == gvalue.blob_type then
-- we need to set the blob to a copy of the lua string that vips
-- can own
local n = #value
Expand All @@ -153,27 +156,27 @@ gvalue.set = function(gv, value)
vips_lib.vips_value_set_blob(gv, glib_lib.g_free, buf, n)
end
else
error("unsupported gtype for set " .. gvalue.type_name(gtype_raw))
error("unsupported gtype for set " .. gvalue.type_name(gtype))
end
end

gvalue.get = function(gv)
local gtype_raw = gv.gtype
local gtype = gvalue.comparable_type(gtype_raw)
local fundamental = gobject_lib.g_type_fundamental(gtype_raw)
local gtype = gv.gtype
local gtype_comp = gvalue.comparable_type(gtype)
local fundamental = gobject_lib.g_type_fundamental(gtype)

local result

if gtype == gvalue.gbool_type then
if gtype_comp == gvalue.gbool_type then
result = gobject_lib.g_value_get_boolean(gv)
elseif gtype == gvalue.gint_type then
elseif gtype_comp == gvalue.gint_type then
result = gobject_lib.g_value_get_int(gv)
elseif gtype == gvalue.gdouble_type then
elseif gtype_comp == gvalue.gdouble_type then
result = gobject_lib.g_value_get_double(gv)
elseif fundamental == gvalue.genum_type then
local enum_value = gobject_lib.g_value_get_enum(gv)

local cstr = vips_lib.vips_enum_nick(gtype_raw, enum_value)
local cstr = vips_lib.vips_enum_nick(gtype, enum_value)

if cstr == ffi.NULL then
error("value not in enum")
Expand All @@ -182,21 +185,21 @@ gvalue.get = function(gv)
result = ffi.string(cstr)
elseif fundamental == gvalue.gflags_type then
result = gobject_lib.g_value_get_flags(gv)
elseif gtype == gvalue.gstr_type then
elseif gtype_comp == gvalue.gstr_type then
local cstr = gobject_lib.g_value_get_string(gv)

if cstr ~= ffi.NULL then
result = ffi.string(cstr)
else
result = nil
end
elseif gtype == gvalue.refstr_type then
elseif gtype_comp == gvalue.refstr_type then
local psize = ffi.new(gvalue.psize_typeof, 1)

local cstr = vips_lib.vips_value_get_ref_string(gv, psize)

result = ffi.string(cstr, tonumber(psize[0]))
elseif gtype == gvalue.image_type then
elseif gtype_comp == gvalue.image_type then
-- g_value_get_object() will not add a ref ... that is
-- held by the gvalue
local vo = gobject_lib.g_value_get_object(gv)
Expand All @@ -208,7 +211,7 @@ gvalue.get = function(gv)
gobject_lib.g_object_ref(vimage)

result = Image.new(vimage)
elseif gtype == gvalue.array_int_type then
elseif gtype_comp == gvalue.array_int_type then
local pint = ffi.new(gvalue.pint_typeof, 1)

local array = vips_lib.vips_value_get_array_int(gv, pint)
Expand All @@ -217,15 +220,15 @@ gvalue.get = function(gv)
result[i + 1] = array[i]
end

elseif gtype == gvalue.array_double_type then
elseif gtype_comp == gvalue.array_double_type then
local pint = ffi.new(gvalue.pint_typeof, 1)

local array = vips_lib.vips_value_get_array_double(gv, pint)
result = {}
for i = 0, pint[0] - 1 do
result[i + 1] = array[i]
end
elseif gtype == gvalue.array_image_type then
elseif gtype_comp == gvalue.array_image_type then
local pint = ffi.new(gvalue.pint_typeof, 1)

local array = vips_lib.vips_value_get_array_image(gv, pint)
Expand All @@ -240,14 +243,14 @@ gvalue.get = function(gv)

result[i + 1] = Image.new(vimage)
end
elseif gtype == gvalue.blob_type then
elseif gtype_comp == gvalue.blob_type then
local psize = ffi.new(gvalue.psize_typeof, 1)

local array = vips_lib.vips_value_get_blob(gv, psize)

result = ffi.string(array, tonumber(psize[0]))
else
error("unsupported gtype for get " .. gvalue.type_name(gtype_raw))
error("unsupported gtype for get " .. gvalue.type_name(gtype))
end

return result
Expand Down
10 changes: 5 additions & 5 deletions src/vips/voperation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ end

voperation.set = function(self, name, flags, match_image, value)
local vob = self:vobject()
local gtype_raw = vob:get_typeof(name)
local gtype = gvalue.comparable_type(gtype_raw)
local gtype = vob:get_typeof(name)
local gtype_comp = gvalue.comparable_type(gtype)

-- if the object wants an image and we have a constant, imageize it
--
-- if the object wants an image array, imageize any constants in the
-- array
if match_image then
if gtype == gvalue.image_type then
if gtype_comp == gvalue.image_type then
value = match_image:imageize(value)
elseif gtype == gvalue.array_image_type then
elseif gtype_comp == gvalue.array_image_type then
for i = 1, #value do
value[i] = match_image:imageize(value[i])
end
Expand All @@ -96,7 +96,7 @@ voperation.set = function(self, name, flags, match_image, value)
value = value:copy():copy_memory()
end

return vob:set_type(name, value, gtype_raw)
return vob:set_type(name, value, gtype)
end

-- this is slow ... call as little as possible
Expand Down

0 comments on commit 3575ad6

Please sign in to comment.