Skip to content

Commit

Permalink
Add hasalpha and addalpha methods
Browse files Browse the repository at this point in the history
  • Loading branch information
RiskoZoSlovenska committed Mar 5, 2024
1 parent 9c75467 commit 751c04e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
13 changes: 12 additions & 1 deletion spec/convenience_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,16 @@ describe("test convenience functions", function()
assert.are.equal(result:bands(), 1)
assert.are.equal(result:avg(), 17 / 4)
end)
end)

it("can call hasalpha", function()
local im1 = vips.Image.new_from_file("./spec/images/Gugg_coloured.jpg")
local im2 = vips.Image.new_from_file("./spec/images/watermark.png")

assert.is_false(im1:hasalpha())
assert.is_true(im2:hasalpha())
end)

it("can call addalpha", function ()
assert.are.equal(im:addalpha():avg(), 128.75)
end)
end)
20 changes: 20 additions & 0 deletions src/vips/Image_methods.lua
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,26 @@ end

-- convenience functions

function Image_method:hasalpha()
local result = vips_lib.vips_image_hasalpha(self.vimage)
if result == nil then
error(verror.get())
end

return result ~= 0
end

function Image_method:addalpha()
local max_alpha
if self:interpretation() == "rgb16" or self:interpretation() == "grey16" then
max_alpha = 65535
else
max_alpha = 255
end

return self:bandjoin(max_alpha)
end

function Image_method:bandsplit()
local result

Expand Down
2 changes: 2 additions & 0 deletions src/vips/cdefs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ ffi.cdef [[
void vips_image_set (VipsImage *image, const char *name, GValue *value);
int vips_image_remove (VipsImage *image, const char *name);

int vips_image_hasalpha(VipsImage *image);

char *vips_filename_get_filename (const char *vips_filename);
char *vips_filename_get_options (const char *vips_filename);

Expand Down

0 comments on commit 751c04e

Please sign in to comment.