mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-07 21:48:13 -08:00
refactor(#55): minor changes, use proper declarations, unref image
This commit is contained in:
parent
e83c80c93c
commit
174de89a40
5 changed files with 41 additions and 20 deletions
10
image.go
10
image.go
|
|
@ -4,6 +4,11 @@ type Image struct {
|
|||
buffer []byte
|
||||
}
|
||||
|
||||
// Creates a new image
|
||||
func NewImage(buf []byte) *Image {
|
||||
return &Image{buf}
|
||||
}
|
||||
|
||||
// Resize the image to fixed width and height
|
||||
func (i *Image) Resize(width, height int) ([]byte, error) {
|
||||
options := Options{
|
||||
|
|
@ -190,8 +195,3 @@ func (i *Image) Size() (ImageSize, error) {
|
|||
func (i *Image) Image() []byte {
|
||||
return i.buffer
|
||||
}
|
||||
|
||||
// Creates a new image
|
||||
func NewImage(buf []byte) *Image {
|
||||
return &Image{buf}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,6 +80,10 @@ type Color struct {
|
|||
R, G, B uint8
|
||||
}
|
||||
|
||||
// Shortcut to black RGB color representation
|
||||
var ColorBlack = Color{0, 0, 0}
|
||||
|
||||
// Text-based watermark configuration
|
||||
type Watermark struct {
|
||||
Width int
|
||||
DPI int
|
||||
|
|
@ -96,6 +100,7 @@ type GaussianBlur struct {
|
|||
MinAmpl float64
|
||||
}
|
||||
|
||||
// Supported image transformation options
|
||||
type Options struct {
|
||||
Height int
|
||||
Width int
|
||||
|
|
@ -117,11 +122,11 @@ type Options struct {
|
|||
NoProfile bool
|
||||
Interlace bool
|
||||
Rotate Angle
|
||||
Background Color
|
||||
Gravity Gravity
|
||||
Watermark Watermark
|
||||
Type ImageType
|
||||
Interpolator Interpolator
|
||||
Interpretation Interpretation
|
||||
GaussianBlur GaussianBlur
|
||||
Background Color
|
||||
}
|
||||
|
|
|
|||
18
resize.go
18
resize.go
|
|
@ -103,12 +103,9 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
|||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
image, err = imageFlatten(image, imageType, o)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
saveOptions := vipsSaveOptions{
|
||||
|
|
@ -312,6 +309,15 @@ func watermakImage(image *C.VipsImage, w Watermark) (*C.VipsImage, error) {
|
|||
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) {
|
||||
if zoom == 0 {
|
||||
return image, nil
|
||||
|
|
|
|||
24
vips.go
24
vips.go
|
|
@ -252,14 +252,24 @@ func vipsInterpretation(image *C.VipsImage) Interpretation {
|
|||
return Interpretation(C.vips_image_guess_interpretation_bridge(image))
|
||||
}
|
||||
|
||||
func vipsFlatten(image *C.VipsImage, background Color) (*C.VipsImage, error) {
|
||||
func vipsFlattenBackground(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 {
|
||||
|
||||
backgroundC := [3]C.double{
|
||||
C.double(background.R),
|
||||
C.double(background.G),
|
||||
C.double(background.B),
|
||||
}
|
||||
|
||||
err := C.vips_flatten_background_brigde(image, &outImage, (*C.double)(&backgroundC[0]))
|
||||
if int(err) != 0 {
|
||||
return nil, catchVipsError()
|
||||
}
|
||||
return outImage, nil
|
||||
|
||||
C.g_object_unref(C.gpointer(image))
|
||||
image = outImage
|
||||
|
||||
return image, nil
|
||||
}
|
||||
|
||||
func vipsPreSave(image *C.VipsImage, o *vipsSaveOptions) (*C.VipsImage, error) {
|
||||
|
|
@ -277,8 +287,8 @@ func vipsPreSave(image *C.VipsImage, o *vipsSaveOptions) (*C.VipsImage, error) {
|
|||
// Apply the proper colour space
|
||||
var outImage *C.VipsImage
|
||||
if vipsColourspaceIsSupported(image) {
|
||||
err := int(C.vips_colourspace_bridge(image, &outImage, interpretation))
|
||||
if err != 0 {
|
||||
err := C.vips_colourspace_bridge(image, &outImage, interpretation)
|
||||
if int(err) != 0 {
|
||||
return nil, catchVipsError()
|
||||
}
|
||||
C.g_object_unref(C.gpointer(image))
|
||||
|
|
|
|||
2
vips.h
2
vips.h
|
|
@ -227,7 +227,7 @@ 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]) {
|
||||
vips_flatten_background_brigde(VipsImage *in, VipsImage **out, double background[3]) {
|
||||
VipsArrayDouble *vipsBackground = vips_array_double_new(background, 3);
|
||||
return vips_flatten(in, out,
|
||||
"background", vipsBackground,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue