From 85f409a7e7befdc20ca98b4e5cb9c98f1019ec2b Mon Sep 17 00:00:00 2001 From: Jon Gilkison Date: Fri, 7 Jun 2024 09:34:12 +0700 Subject: [PATCH] Add support for the `vips_gamma()` function via a new func `Gamma()` on `ImageRef` (#433) * Add support for vips_gamma() function for adjusting the gamma of the image * Add support for vips_gamma() function for adjusting the gamma of the image * Fix test for gamma --- vips/conversion.c | 6 +++++- vips/conversion.go | 11 +++++++++++ vips/conversion.h | 2 ++ vips/image.go | 11 +++++++++++ vips/image_test.go | 10 ++++++++++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/vips/conversion.c b/vips/conversion.c index 00fc1b08..878e8081 100644 --- a/vips/conversion.c +++ b/vips/conversion.c @@ -235,7 +235,7 @@ int crop(VipsImage *in, VipsImage **out, int left, int top, if (n_pages <= 1) { return vips_crop(in, out, left, top, width, height, NULL); } - + int in_width = in->Xsize; VipsObject *base = VIPS_OBJECT(vips_image_new()); VipsImage **page = (VipsImage **) vips_object_local_array(base, n_pages); @@ -374,3 +374,7 @@ int replicate(VipsImage *in, VipsImage **out, int across, int down) { int grid(VipsImage *in, VipsImage **out, int tileHeight, int across, int down){ return vips_grid(in, out, tileHeight, across, down, NULL); } + +int adjust_gamma(VipsImage *in, VipsImage **out, double g) { + return vips_gamma(in, out, "exponent", g, NULL); +} diff --git a/vips/conversion.go b/vips/conversion.go index 072623c0..0738727d 100644 --- a/vips/conversion.go +++ b/vips/conversion.go @@ -507,3 +507,14 @@ func vipsGrid(in *C.VipsImage, tileHeight, across, down int) (*C.VipsImage, erro } return out, nil } + +func vipsGamma(image *C.VipsImage, gamma float64) (*C.VipsImage, error) { + incOpCounter("gamma") + var out *C.VipsImage + + if err := C.adjust_gamma(image, &out, C.double(gamma)); err != 0 { + return nil, handleImageError(out) + } + + return out, nil +} diff --git a/vips/conversion.h b/vips/conversion.h index 5cb2d361..ac92c596 100644 --- a/vips/conversion.h +++ b/vips/conversion.h @@ -66,3 +66,5 @@ int is_16bit(VipsInterpretation interpretation); int replicate(VipsImage *in, VipsImage **out, int across, int down); int grid(VipsImage *in, VipsImage **out, int tileHeight, int across, int down); + +int adjust_gamma(VipsImage *in, VipsImage **out, double g); diff --git a/vips/image.go b/vips/image.go index e0f5ab0a..82d5fac2 100644 --- a/vips/image.go +++ b/vips/image.go @@ -1304,6 +1304,17 @@ func (r *ImageRef) Linear1(a, b float64) error { return nil } +// Adjusts the image's gamma value. +// See https://www.libvips.org/API/current/libvips-conversion.html#vips-gamma +func (r *ImageRef) Gamma(gamma float64) error { + out, err := vipsGamma(r.image, gamma) + if err != nil { + return err + } + r.setImage(out) + return nil +} + // GetRotationAngleFromExif returns the angle which the image is currently rotated in. // First returned value is the angle and second is a boolean indicating whether image is flipped. // This is based on the EXIF orientation tag standard. diff --git a/vips/image_test.go b/vips/image_test.go index 42a990c2..9c6090bc 100644 --- a/vips/image_test.go +++ b/vips/image_test.go @@ -1180,6 +1180,16 @@ func TestImageRef_SetPages(t *testing.T) { require.Equal(t, 3, image.Pages()) } +func TestImageRef_SetGamma(t *testing.T) { + Startup(nil) + + image, err := NewImageFromFile(resources + "png-24bit.png") + require.NoError(t, err) + + err = image.Gamma(1.0 / 2.4) + require.NoError(t, err) +} + // TODO unit tests to cover: // NewImageFromReader failing test // NewImageFromFile failing test