Skip to content

Commit

Permalink
vips crop support (#382)
Browse files Browse the repository at this point in the history
Signed-off-by: songjiayang <songjiayang1@gmail.com>
  • Loading branch information
songjiayang authored Nov 10, 2023
1 parent 3e45ca9 commit 1e5b2cf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vips/conversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ int smartcrop(VipsImage *in, VipsImage **out, int width, int height,
NULL);
}

int crop(VipsImage *in, VipsImage **out, int left, int top,
int width, int height) {
return vips_crop(in, out, left, top, width, height,
NULL);
}

int flatten_image(VipsImage *in, VipsImage **out, double r, double g,
double b) {
if (is_16bit(in->Type)) {
Expand Down
12 changes: 12 additions & 0 deletions vips/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,18 @@ func vipsSmartCrop(in *C.VipsImage, width int, height int, interesting Interesti
return out, nil
}

// http://libvips.github.io/libvips/API/current/libvips-conversion.html#vips-crop
func vipsCrop(in *C.VipsImage, left int, top int, width int, height int) (*C.VipsImage, error) {
incOpCounter("crop")
var out *C.VipsImage

if err := C.crop(in, &out, C.int(left), C.int(top), C.int(width), C.int(height)); err != 0 {
return nil, handleImageError(out)
}

return out, nil
}

// https://libvips.github.io/libvips/API/current/libvips-conversion.html#vips-rot
func vipsRotate(in *C.VipsImage, angle Angle) (*C.VipsImage, error) {
incOpCounter("rot")
Expand Down
2 changes: 2 additions & 0 deletions vips/conversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ int autorot_image(VipsImage *in, VipsImage **out);
int zoom_image(VipsImage *in, VipsImage **out, int xfac, int yfac);
int smartcrop(VipsImage *in, VipsImage **out, int width, int height,
int interesting);
int crop(VipsImage *in, VipsImage **out, int left, int top,
int width, int height);

int bandjoin(VipsImage **in, VipsImage **out, int n);
int bandjoin_const(VipsImage *in, VipsImage **out, double constants[], int n);
Expand Down
10 changes: 10 additions & 0 deletions vips/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -1845,6 +1845,16 @@ func (r *ImageRef) SmartCrop(width int, height int, interesting Interesting) err
return nil
}

// Crop will crop the image based on coordinate and box size
func (r *ImageRef) Crop(left int, top int, width int, height int) error {
out, err := vipsCrop(r.image, left, top, width, height)
if err != nil {
return err
}
r.setImage(out)
return nil
}

// Label overlays a label on top of the image
func (r *ImageRef) Label(labelParams *LabelParams) error {
out, err := labelImage(r.image, labelParams)
Expand Down
9 changes: 9 additions & 0 deletions vips/image_golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,15 @@ func TestImage_SmartCrop(t *testing.T) {
}, nil)
}

func TestImage_Crop(t *testing.T) {
goldenTest(t, resources+"jpg-24bit.jpg", func(img *ImageRef) error {
return img.Crop(10, 10, 60, 80)
}, func(result *ImageRef) {
assert.Equal(t, 60, result.Width())
assert.Equal(t, 80, result.Height())
}, nil)
}

func TestImage_Replicate(t *testing.T) {
goldenTest(t, resources+"jpg-24bit.jpg", func(img *ImageRef) error {
return img.Replicate(3, 2)
Expand Down

0 comments on commit 1e5b2cf

Please sign in to comment.