mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-15 02:15:54 -07:00
feat(#92): support Extend param with optional background
This commit is contained in:
parent
44636a88f0
commit
522cf71c73
6 changed files with 73 additions and 7 deletions
BIN
fixtures/test_issue.jpg
Normal file
BIN
fixtures/test_issue.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 44 KiB |
23
options.go
23
options.go
|
|
@ -112,6 +112,27 @@ const (
|
||||||
InterpretationXYZ Interpretation = C.VIPS_INTERPRETATION_XYZ
|
InterpretationXYZ Interpretation = C.VIPS_INTERPRETATION_XYZ
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Extend represents the image extend mode, used when the edges
|
||||||
|
// of an image are extended, you can specify how you want the extension done.
|
||||||
|
type Extend int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// ExtendBlack extend with black (all 0) pixels mode.
|
||||||
|
ExtendBlack Extend = C.VIPS_EXTEND_BLACK
|
||||||
|
// ExtendCopy copy the image edges.
|
||||||
|
ExtendCopy Extend = C.VIPS_EXTEND_COPY
|
||||||
|
// ExtendRepeat repeat the whole image.
|
||||||
|
ExtendRepeat Extend = C.VIPS_EXTEND_REPEAT
|
||||||
|
// ExtendMirro mirror the whole image.
|
||||||
|
ExtendMirror Extend = C.VIPS_EXTEND_MIRROR
|
||||||
|
// ExtendWhite extend with white (all bits set) pixels.
|
||||||
|
ExtendWhite Extend = C.VIPS_EXTEND_WHITE
|
||||||
|
// ExtendBackground with colour from the background property.
|
||||||
|
ExtendBackground Extend = C.VIPS_EXTEND_BACKGROUND
|
||||||
|
// ExtendLast extend with last pixel.
|
||||||
|
ExtendLast Extend = C.VIPS_EXTEND_LAST
|
||||||
|
)
|
||||||
|
|
||||||
// WatermarkFont defines the default watermark font to be used.
|
// WatermarkFont defines the default watermark font to be used.
|
||||||
var WatermarkFont = "sans 10"
|
var WatermarkFont = "sans 10"
|
||||||
|
|
||||||
|
|
@ -159,7 +180,6 @@ type Options struct {
|
||||||
AreaWidth int
|
AreaWidth int
|
||||||
Top int
|
Top int
|
||||||
Left int
|
Left int
|
||||||
Extend int
|
|
||||||
Quality int
|
Quality int
|
||||||
Compression int
|
Compression int
|
||||||
Zoom int
|
Zoom int
|
||||||
|
|
@ -172,6 +192,7 @@ type Options struct {
|
||||||
NoAutoRotate bool
|
NoAutoRotate bool
|
||||||
NoProfile bool
|
NoProfile bool
|
||||||
Interlace bool
|
Interlace bool
|
||||||
|
Extend Extend
|
||||||
Rotate Angle
|
Rotate Angle
|
||||||
Background Color
|
Background Color
|
||||||
Gravity Gravity
|
Gravity Gravity
|
||||||
|
|
|
||||||
|
|
@ -240,7 +240,7 @@ func extractOrEmbedImage(image *C.VipsImage, o Options) (*C.VipsImage, error) {
|
||||||
break
|
break
|
||||||
case o.Embed:
|
case o.Embed:
|
||||||
left, top := (o.Width-inWidth)/2, (o.Height-inHeight)/2
|
left, top := (o.Width-inWidth)/2, (o.Height-inHeight)/2
|
||||||
image, err = vipsEmbed(image, left, top, o.Width, o.Height, o.Extend)
|
image, err = vipsEmbed(image, left, top, o.Width, o.Height, o.Extend, o.Background)
|
||||||
break
|
break
|
||||||
case o.Top != 0 || o.Left != 0:
|
case o.Top != 0 || o.Left != 0:
|
||||||
if o.AreaWidth == 0 {
|
if o.AreaWidth == 0 {
|
||||||
|
|
|
||||||
|
|
@ -216,6 +216,40 @@ func TestNoColorProfile(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEmbedExtendColor(t *testing.T) {
|
||||||
|
options := Options{Width: 400, Height: 600, Crop: false, Embed: true, Extend: ExtendWhite, Background: Color{255, 20, 10}}
|
||||||
|
buf, _ := Read("fixtures/test_issue.jpg")
|
||||||
|
|
||||||
|
newImg, err := Resize(buf, options)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
size, _ := Size(newImg)
|
||||||
|
if size.Height != options.Height || size.Width != options.Width {
|
||||||
|
t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height)
|
||||||
|
}
|
||||||
|
|
||||||
|
Write("fixtures/test_extend_white_out.jpg", newImg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEmbedExtendWithCustomColor(t *testing.T) {
|
||||||
|
options := Options{Width: 400, Height: 600, Crop: false, Embed: true, Extend: 5, Background: Color{255, 20, 10}}
|
||||||
|
buf, _ := Read("fixtures/test_issue.jpg")
|
||||||
|
|
||||||
|
newImg, err := Resize(buf, options)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
size, _ := Size(newImg)
|
||||||
|
if size.Height != options.Height || size.Width != options.Width {
|
||||||
|
t.Fatalf("Invalid image size: %dx%d", size.Width, size.Height)
|
||||||
|
}
|
||||||
|
|
||||||
|
Write("fixtures/test_extend_background_out.jpg", newImg)
|
||||||
|
}
|
||||||
|
|
||||||
func TestGaussianBlur(t *testing.T) {
|
func TestGaussianBlur(t *testing.T) {
|
||||||
options := Options{Width: 800, Height: 600, GaussianBlur: GaussianBlur{Sigma: 5}}
|
options := Options{Width: 800, Height: 600, GaussianBlur: GaussianBlur{Sigma: 5}}
|
||||||
buf, _ := Read("fixtures/test.jpg")
|
buf, _ := Read("fixtures/test.jpg")
|
||||||
|
|
|
||||||
12
vips.go
12
vips.go
|
|
@ -459,11 +459,17 @@ func vipsShrink(input *C.VipsImage, shrink int) (*C.VipsImage, error) {
|
||||||
return image, nil
|
return image, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func vipsEmbed(input *C.VipsImage, left, top, width, height, extend int) (*C.VipsImage, error) {
|
func vipsEmbed(input *C.VipsImage, left, top, width, height int, extend Extend, background Color) (*C.VipsImage, error) {
|
||||||
var image *C.VipsImage
|
var image *C.VipsImage
|
||||||
defer C.g_object_unref(C.gpointer(input))
|
|
||||||
|
|
||||||
err := C.vips_embed_bridge(input, &image, C.int(left), C.int(top), C.int(width), C.int(height), C.int(extend))
|
// Max extend value, see: http://www.vips.ecs.soton.ac.uk/supported/8.4/doc/html/libvips/libvips-conversion.html#VipsExtend
|
||||||
|
if extend > 5 {
|
||||||
|
extend = ExtendBackground
|
||||||
|
}
|
||||||
|
|
||||||
|
defer C.g_object_unref(C.gpointer(input))
|
||||||
|
err := C.vips_embed_bridge(input, &image, C.int(left), C.int(top), C.int(width),
|
||||||
|
C.int(height), C.int(extend), C.double(background.R), C.double(background.G), C.double(background.B))
|
||||||
if err != 0 {
|
if err != 0 {
|
||||||
return nil, catchVipsError()
|
return nil, catchVipsError()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
9
vips.h
9
vips.h
|
|
@ -207,7 +207,12 @@ vips_zoom_bridge(VipsImage *in, VipsImage **out, int xfac, int yfac) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
vips_embed_bridge(VipsImage *in, VipsImage **out, int left, int top, int width, int height, int extend) {
|
vips_embed_bridge(VipsImage *in, VipsImage **out, int left, int top, int width, int height, int extend, double r, double g, double b) {
|
||||||
|
if (extend == VIPS_EXTEND_BACKGROUND) {
|
||||||
|
double background[3] = {r, g, b};
|
||||||
|
VipsArrayDouble *vipsBackground = vips_array_double_new(background, 3);
|
||||||
|
return vips_embed(in, out, left, top, width, height, "extend", extend, "background", vipsBackground, NULL);
|
||||||
|
}
|
||||||
return vips_embed(in, out, left, top, width, height, "extend", extend, NULL);
|
return vips_embed(in, out, left, top, width, height, "extend", extend, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -280,7 +285,7 @@ int
|
||||||
vips_flatten_background_brigde(VipsImage *in, VipsImage **out, double r, double g, double b) {
|
vips_flatten_background_brigde(VipsImage *in, VipsImage **out, double r, double g, double b) {
|
||||||
double background[3] = {r, g, b};
|
double background[3] = {r, g, b};
|
||||||
VipsArrayDouble *vipsBackground = vips_array_double_new(background, 3);
|
VipsArrayDouble *vipsBackground = vips_array_double_new(background, 3);
|
||||||
|
|
||||||
return vips_flatten(in, out,
|
return vips_flatten(in, out,
|
||||||
"background", vipsBackground,
|
"background", vipsBackground,
|
||||||
"max_alpha", vips_is_16bit(in->Type) ? 65535.0 : 255.0,
|
"max_alpha", vips_is_16bit(in->Type) ? 65535.0 : 255.0,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue