Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
David Avagimyan committed Apr 23, 2024
2 parents c7606a7 + bb678ba commit 715ced8
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 4 deletions.
4 changes: 4 additions & 0 deletions vips/convolution.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ int sharpen_image(VipsImage *in, VipsImage **out, double sigma, double x1,
double m2) {
return vips_sharpen(in, out, "sigma", sigma, "x1", x1, "m2", m2, NULL);
}

int sobel_image(VipsImage *in, VipsImage **out) {
return vips_sobel(in, out, NULL);
}
12 changes: 12 additions & 0 deletions vips/convolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ func vipsSharpen(in *C.VipsImage, sigma float64, x1 float64, m2 float64) (*C.Vip

return out, nil
}

// https://libvips.github.io/libvips/API/current/libvips-convolution.html#vips-sobel
func vipsSobel(in *C.VipsImage) (*C.VipsImage, error) {
incOpCounter("sobel")
var out *C.VipsImage

if err := C.sobel_image(in, &out); err != 0 {
return nil, handleImageError(out)
}

return out, nil
}
1 change: 1 addition & 0 deletions vips/convolution.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
int gaussian_blur_image(VipsImage *in, VipsImage **out, double sigma, double min_ampl);
int sharpen_image(VipsImage *in, VipsImage **out, double sigma, double x1,
double m2);
int sobel_image(VipsImage *in, VipsImage **out);
13 changes: 10 additions & 3 deletions vips/foreign.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ int set_tiffsave_options(VipsOperation *operation, SaveParams *params) {

// https://libvips.github.io/libvips/API/current/VipsForeignSave.html#vips-magicksave-buffer
int set_magicksave_options(VipsOperation *operation, SaveParams *params) {
int ret = vips_object_set(VIPS_OBJECT(operation), "format", "GIF", NULL);
int ret = vips_object_set(VIPS_OBJECT(operation), "format", "GIF", "bitdepth", params->gifBitdepth, NULL);

if (!ret && params->quality) {
ret = vips_object_set(VIPS_OBJECT(operation), "quality", params->quality,
NULL);
Expand All @@ -331,7 +332,7 @@ int set_magicksave_options(VipsOperation *operation, SaveParams *params) {
int set_gifsave_options(VipsOperation *operation, SaveParams *params) {
int ret = 0;
// See for argument values: https://www.libvips.org/API/current/VipsForeignSave.html#vips-gifsave
if (params->gifDither > 0.0 && params->gifDither <= 1.0) {
if (params->gifDither > 0.0 && params->gifDither <= 10) {
ret = vips_object_set(VIPS_OBJECT(operation), "dither", params->gifDither, NULL);
}
if (params->gifEffort >= 1 && params->gifEffort <= 10) {
Expand Down Expand Up @@ -562,7 +563,13 @@ static SaveParams defaultSaveParams = {

.jp2kLossless = FALSE,
.jp2kTileHeight = 512,
.jp2kTileWidth = 512};
.jp2kTileWidth = 512,

.jxlTier = 0,
.jxlDistance = 1.0,
.jxlEffort = 7,
.jxlLossless = FALSE,
};

SaveParams create_save_params(ImageType outputFormat) {
SaveParams params = defaultSaveParams;
Expand Down
17 changes: 16 additions & 1 deletion vips/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ func NewTiffExportParams() *TiffExportParams {
}
}

// GifExportParams are options when exporting a GIF to file or buffer
// Please note that if vips version is above 8.12, then `vips_gifsave_buffer` is used, and only `Dither`, `Effort`, `Bitdepth` is used.
// If vips version is below 8.12, then `vips_magicksave_buffer` is used, and only `Bitdepth`, `Quality` is used.
// StripMetadata does nothing to Gif images.
type GifExportParams struct {
StripMetadata bool
Quality int
Expand Down Expand Up @@ -405,6 +409,7 @@ func NewJxlExportParams() *JxlExportParams {
Quality: 75,
Lossless: false,
Effort: 7,
Distance: 1.0,
}
}

Expand Down Expand Up @@ -761,7 +766,7 @@ func (r *ImageRef) SetPages(pages int) error {
return err
}

vipsSetImageNPages(r.image, pages)
vipsSetImageNPages(out, pages)

r.setImage(out)
return nil
Expand Down Expand Up @@ -1552,6 +1557,16 @@ func (r *ImageRef) Sharpen(sigma float64, x1 float64, m2 float64) error {
return nil
}

// Apply Sobel edge detector to the image.
func (r *ImageRef) Sobel() error {
out, err := vipsSobel(r.image)
if err != nil {
return err
}
r.setImage(out)
return nil
}

// Modulate the colors
func (r *ImageRef) Modulate(brightness, saturation, hue float64) error {
var err error
Expand Down
6 changes: 6 additions & 0 deletions vips/image_golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,12 @@ func TestImage_Sharpen_8bit_Alpha(t *testing.T) {
}, nil, nil)
}

func TestImage_Sobel(t *testing.T) {
goldenTest(t, resources+"png-8bit+alpha.png", func(img *ImageRef) error {
return img.Sobel()
}, nil, nil)
}

func TestImage_Modulate(t *testing.T) {
goldenTest(t, resources+"jpg-24bit-icc-iec.jpg", func(img *ImageRef) error {
return img.Modulate(0.7, 0.5, 180)
Expand Down
12 changes: 12 additions & 0 deletions vips/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,18 @@ func TestImageRef_HistogramEntropy(t *testing.T) {
require.True(t, e > 0)
}

func TestImageRef_SetPages(t *testing.T) {
Startup(nil)

image, err := NewImageFromFile(resources + "gif-animated.gif")
require.NoError(t, err)
require.Equal(t, 8, image.Pages())

err = image.SetPages(3)
require.NoError(t, err)
require.Equal(t, 3, image.Pages())
}

// TODO unit tests to cover:
// NewImageFromReader failing test
// NewImageFromFile failing test
Expand Down

0 comments on commit 715ced8

Please sign in to comment.