mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-14 09:55:56 -07:00
Cleaner fix
This commit is contained in:
parent
31a54299fb
commit
a1b9efdb97
3 changed files with 21 additions and 16 deletions
17
resize.go
17
resize.go
|
|
@ -112,8 +112,11 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only flatten PNG for now
|
// Flatten image on a background, if necessary
|
||||||
flatten := imageType == PNG && o.Background != ColorBlack
|
image, err = imageFlatten(image, imageType, o)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
saveOptions := vipsSaveOptions{
|
saveOptions := vipsSaveOptions{
|
||||||
Quality: o.Quality,
|
Quality: o.Quality,
|
||||||
|
|
@ -122,8 +125,6 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
Interlace: o.Interlace,
|
Interlace: o.Interlace,
|
||||||
NoProfile: o.NoProfile,
|
NoProfile: o.NoProfile,
|
||||||
Interpretation: o.Interpretation,
|
Interpretation: o.Interpretation,
|
||||||
Flatten: flatten,
|
|
||||||
Background: o.Background,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally get the resultant buffer
|
// Finally get the resultant buffer
|
||||||
|
|
@ -324,6 +325,14 @@ func watermakImage(image *C.VipsImage, w Watermark) (*C.VipsImage, error) {
|
||||||
return image, nil
|
return image, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func imageFlatten(image *C.VipsImage, imageType ImageType, o Options) (*C.VipsImage, error) {
|
||||||
|
// Only PNG images are supported for now
|
||||||
|
if imageType != PNG || o.Background == ColorBlack {
|
||||||
|
return image, nil
|
||||||
|
}
|
||||||
|
return vipsFlattenBackground(image, o.Background)
|
||||||
|
}
|
||||||
|
|
||||||
func zoomImage(image *C.VipsImage, zoom int) (*C.VipsImage, error) {
|
func zoomImage(image *C.VipsImage, zoom int) (*C.VipsImage, error) {
|
||||||
if zoom == 0 {
|
if zoom == 0 {
|
||||||
return image, nil
|
return image, nil
|
||||||
|
|
|
||||||
11
vips.go
11
vips.go
|
|
@ -55,8 +55,6 @@ type vipsSaveOptions struct {
|
||||||
Interlace bool
|
Interlace bool
|
||||||
NoProfile bool
|
NoProfile bool
|
||||||
Interpretation Interpretation
|
Interpretation Interpretation
|
||||||
Flatten bool
|
|
||||||
Background Color
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type vipsWatermarkOptions struct {
|
type vipsWatermarkOptions struct {
|
||||||
|
|
@ -343,15 +341,6 @@ func vipsPreSave(image *C.VipsImage, o *vipsSaveOptions) (*C.VipsImage, error) {
|
||||||
image = outImage
|
image = outImage
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flatten image on a background, if necessary
|
|
||||||
if o.Flatten {
|
|
||||||
var err error
|
|
||||||
if outImage, err = vipsFlattenBackground(image, o.Background); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
image = outImage
|
|
||||||
}
|
|
||||||
|
|
||||||
return image, nil
|
return image, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
9
vips.h
9
vips.h
|
|
@ -283,11 +283,18 @@ vips_is_16bit (VipsInterpretation interpretation) {
|
||||||
|
|
||||||
int
|
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) {
|
||||||
|
if (vips_is_16bit(in->Type)) {
|
||||||
|
r = 65535 * r / 255;
|
||||||
|
g = 65535 * g / 255;
|
||||||
|
b = 65535 * b / 255;
|
||||||
|
}
|
||||||
|
|
||||||
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, "max_alpha", 255.0,
|
"background", vipsBackground,
|
||||||
|
"max_alpha", vips_is_16bit(in->Type) ? 65535.0 : 255.0,
|
||||||
NULL
|
NULL
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue