Skip to content

Commit

Permalink
Add support for the vips_gamma() function via a new func Gamma()
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
jawngee committed Jun 7, 2024
1 parent 637db4b commit 85f409a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion vips/conversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
11 changes: 11 additions & 0 deletions vips/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions vips/conversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
11 changes: 11 additions & 0 deletions vips/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions vips/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 85f409a

Please sign in to comment.