mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-15 02:15:54 -07:00
Add the option to use background and threshold options on trim
This commit is contained in:
parent
1a5477aae5
commit
58bc9312b6
5 changed files with 42 additions and 5 deletions
|
|
@ -495,6 +495,31 @@ func TestImageTrim(t *testing.T) {
|
||||||
Write("testdata/transparent_trim.png", buf)
|
Write("testdata/transparent_trim.png", buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestImageTrimParameters(t *testing.T) {
|
||||||
|
|
||||||
|
if !(VipsMajorVersion >= 8 && VipsMinorVersion >= 6) {
|
||||||
|
t.Skipf("Skipping this test, libvips doesn't meet version requirement %s >= 8.6", VipsVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
i := initImage("test.png")
|
||||||
|
options := Options{
|
||||||
|
Trim: true,
|
||||||
|
Background: Color{0.0, 0.0, 0.0},
|
||||||
|
Threshold: 10.0,
|
||||||
|
}
|
||||||
|
buf, err := i.Process(options)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Cannot process the image: %#v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = assertSize(buf, 400, 257)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("The image wasn't trimmed.")
|
||||||
|
}
|
||||||
|
|
||||||
|
Write("testdata/transparent_trim.png", buf)
|
||||||
|
}
|
||||||
|
|
||||||
func TestImageLength(t *testing.T) {
|
func TestImageLength(t *testing.T) {
|
||||||
i := initImage("test.jpg")
|
i := initImage("test.jpg")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -217,5 +217,6 @@ type Options struct {
|
||||||
Interpretation Interpretation
|
Interpretation Interpretation
|
||||||
GaussianBlur GaussianBlur
|
GaussianBlur GaussianBlur
|
||||||
Sharpen Sharpen
|
Sharpen Sharpen
|
||||||
|
Threshold float32
|
||||||
OutputICC string
|
OutputICC string
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -273,7 +273,7 @@ func extractOrEmbedImage(image *C.VipsImage, o Options) (*C.VipsImage, error) {
|
||||||
image, err = vipsEmbed(image, left, top, o.Width, o.Height, o.Extend, o.Background)
|
image, err = vipsEmbed(image, left, top, o.Width, o.Height, o.Extend, o.Background)
|
||||||
break
|
break
|
||||||
case o.Trim:
|
case o.Trim:
|
||||||
left, top, width, height, err := vipsTrim(image)
|
left, top, width, height, err := vipsTrim(image, o.Background, o.Threshold)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
image, err = vipsExtract(image, left, top, width, height)
|
image, err = vipsExtract(image, left, top, width, height)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
7
vips.go
7
vips.go
|
|
@ -503,7 +503,7 @@ func vipsSmartCrop(image *C.VipsImage, width, height int) (*C.VipsImage, error)
|
||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func vipsTrim(image *C.VipsImage) (int, int, int, int, error) {
|
func vipsTrim(image *C.VipsImage, background Color, threshold float32) (int, int, int, int, error) {
|
||||||
defer C.g_object_unref(C.gpointer(image))
|
defer C.g_object_unref(C.gpointer(image))
|
||||||
|
|
||||||
top := C.int(0)
|
top := C.int(0)
|
||||||
|
|
@ -511,7 +511,10 @@ func vipsTrim(image *C.VipsImage) (int, int, int, int, error) {
|
||||||
width := C.int(0)
|
width := C.int(0)
|
||||||
height := C.int(0)
|
height := C.int(0)
|
||||||
|
|
||||||
err := C.vips_find_trim_bridge(image, &top, &left, &width, &height)
|
err := C.vips_find_trim_bridge(image,
|
||||||
|
&top, &left, &width, &height,
|
||||||
|
C.double(background.R), C.double(background.G), C.double(background.B),
|
||||||
|
C.double(threshold))
|
||||||
if err != 0 {
|
if err != 0 {
|
||||||
return 0, 0, 0, 0, catchVipsError()
|
return 0, 0, 0, 0, catchVipsError()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
vips.h
12
vips.h
|
|
@ -545,9 +545,17 @@ vips_smartcrop_bridge(VipsImage *in, VipsImage **out, int width, int height) {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int vips_find_trim_bridge(VipsImage *in, int *top, int *left, int *width, int *height) {
|
int vips_find_trim_bridge(VipsImage *in, int *top, int *left, int *width, int *height, double r, double g, double b, double threshold) {
|
||||||
#if (VIPS_MAJOR_VERSION >= 8 && VIPS_MINOR_VERSION >= 6)
|
#if (VIPS_MAJOR_VERSION >= 8 && VIPS_MINOR_VERSION >= 6)
|
||||||
return vips_find_trim(in, top, left, width, height, NULL);
|
if (vips_is_16bit(in->Type)) {
|
||||||
|
r = 65535 * r / 255;
|
||||||
|
g = 65535 * g / 255;
|
||||||
|
b = 65535 * b / 255;
|
||||||
|
}
|
||||||
|
|
||||||
|
double background[3] = {r, g, b};
|
||||||
|
VipsArrayDouble *vipsBackground = vips_array_double_new(background, 3);
|
||||||
|
return vips_find_trim(in, top, left, width, height, "background", vipsBackground, "threshold", threshold, NULL);
|
||||||
#else
|
#else
|
||||||
return 0;
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue