From 3575ad69a946811ee6d210827635b47d7e7a4d9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20L=C3=B6tscher?= Date: Mon, 25 Mar 2024 18:12:30 +0100 Subject: [PATCH] Improve naming for gtype --- src/vips/gvalue.lua | 63 +++++++++++++++++++++-------------------- src/vips/voperation.lua | 10 +++---- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/vips/gvalue.lua b/src/vips/gvalue.lua index 0a748bf..ea8f518 100644 --- a/src/vips/gvalue.lua +++ b/src/vips/gvalue.lua @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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") @@ -182,7 +185,7 @@ 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 @@ -190,13 +193,13 @@ gvalue.get = function(gv) 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) @@ -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) @@ -217,7 +220,7 @@ 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) @@ -225,7 +228,7 @@ gvalue.get = function(gv) 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) @@ -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 diff --git a/src/vips/voperation.lua b/src/vips/voperation.lua index fdba75e..f6e2c35 100644 --- a/src/vips/voperation.lua +++ b/src/vips/voperation.lua @@ -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 @@ -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