mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-15 02:15:54 -07:00
- Adding a Background option when flattening out a transparent PNG
This commit is contained in:
parent
ebb3688b44
commit
e83c80c93c
5 changed files with 42 additions and 0 deletions
|
|
@ -246,6 +246,19 @@ func TestImageConvert(t *testing.T) {
|
||||||
Write("fixtures/test_image_convert_out.png", buf)
|
Write("fixtures/test_image_convert_out.png", buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTransparentImageConvert(t *testing.T) {
|
||||||
|
image := initImage("transparent.png")
|
||||||
|
options := Options{
|
||||||
|
Type: JPEG,
|
||||||
|
Background: Color{255, 255, 255},
|
||||||
|
}
|
||||||
|
buf, err := image.Process(options)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Cannot process the image: %#v", err)
|
||||||
|
}
|
||||||
|
Write("fixtures/test_transparent_image_convert_out.jpg", buf)
|
||||||
|
}
|
||||||
|
|
||||||
func TestImageMetadata(t *testing.T) {
|
func TestImageMetadata(t *testing.T) {
|
||||||
data, err := initImage("test.png").Metadata()
|
data, err := initImage("test.png").Metadata()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -123,4 +123,5 @@ type Options struct {
|
||||||
Interpolator Interpolator
|
Interpolator Interpolator
|
||||||
Interpretation Interpretation
|
Interpretation Interpretation
|
||||||
GaussianBlur GaussianBlur
|
GaussianBlur GaussianBlur
|
||||||
|
Background Color
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -102,6 +102,15 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flatten image on a background, if necessary
|
||||||
|
black := Color{0, 0, 0}
|
||||||
|
if imageType == PNG && o.Background != black {
|
||||||
|
image, err = vipsFlatten(image, o.Background)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
saveOptions := vipsSaveOptions{
|
saveOptions := vipsSaveOptions{
|
||||||
Quality: o.Quality,
|
Quality: o.Quality,
|
||||||
Type: o.Type,
|
Type: o.Type,
|
||||||
|
|
|
||||||
10
vips.go
10
vips.go
|
|
@ -252,6 +252,16 @@ func vipsInterpretation(image *C.VipsImage) Interpretation {
|
||||||
return Interpretation(C.vips_image_guess_interpretation_bridge(image))
|
return Interpretation(C.vips_image_guess_interpretation_bridge(image))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func vipsFlatten(image *C.VipsImage, background Color) (*C.VipsImage, error) {
|
||||||
|
var outImage *C.VipsImage
|
||||||
|
backgroundC := [3]C.double{C.double(background.R), C.double(background.G), C.double(background.B)}
|
||||||
|
err := int(C.vips_flatten_image(image, &outImage, (*C.double)(&backgroundC[0])))
|
||||||
|
if err != 0 {
|
||||||
|
return nil, catchVipsError()
|
||||||
|
}
|
||||||
|
return outImage, nil
|
||||||
|
}
|
||||||
|
|
||||||
func vipsPreSave(image *C.VipsImage, o *vipsSaveOptions) (*C.VipsImage, error) {
|
func vipsPreSave(image *C.VipsImage, o *vipsSaveOptions) (*C.VipsImage, error) {
|
||||||
// Remove ICC profile metadata
|
// Remove ICC profile metadata
|
||||||
if o.NoProfile {
|
if o.NoProfile {
|
||||||
|
|
|
||||||
9
vips.h
9
vips.h
|
|
@ -226,6 +226,15 @@ vips_webpsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int qual
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
vips_flatten_image(VipsImage *in, VipsImage **out, double background[3]) {
|
||||||
|
VipsArrayDouble *vipsBackground = vips_array_double_new(background, 3);
|
||||||
|
return vips_flatten(in, out,
|
||||||
|
"background", vipsBackground,
|
||||||
|
NULL
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
vips_init_image (void *buf, size_t len, int imageType, VipsImage **out) {
|
vips_init_image (void *buf, size_t len, int imageType, VipsImage **out) {
|
||||||
int code = 1;
|
int code = 1;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue