refactor(#55): minor changes, use proper declarations, unref image

master
Tomas Aparicio 10 years ago
parent e83c80c93c
commit 174de89a40

@ -4,6 +4,11 @@ type Image struct {
buffer []byte buffer []byte
} }
// Creates a new image
func NewImage(buf []byte) *Image {
return &Image{buf}
}
// Resize the image to fixed width and height // Resize the image to fixed width and height
func (i *Image) Resize(width, height int) ([]byte, error) { func (i *Image) Resize(width, height int) ([]byte, error) {
options := Options{ options := Options{
@ -190,8 +195,3 @@ func (i *Image) Size() (ImageSize, error) {
func (i *Image) Image() []byte { func (i *Image) Image() []byte {
return i.buffer 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 R, G, B uint8
} }
// Shortcut to black RGB color representation
var ColorBlack = Color{0, 0, 0}
// Text-based watermark configuration
type Watermark struct { type Watermark struct {
Width int Width int
DPI int DPI int
@ -96,6 +100,7 @@ type GaussianBlur struct {
MinAmpl float64 MinAmpl float64
} }
// Supported image transformation options
type Options struct { type Options struct {
Height int Height int
Width int Width int
@ -117,11 +122,11 @@ type Options struct {
NoProfile bool NoProfile bool
Interlace bool Interlace bool
Rotate Angle Rotate Angle
Background Color
Gravity Gravity Gravity Gravity
Watermark Watermark Watermark Watermark
Type ImageType Type ImageType
Interpolator Interpolator Interpolator Interpolator
Interpretation Interpretation Interpretation Interpretation
GaussianBlur GaussianBlur GaussianBlur GaussianBlur
Background Color
} }

@ -103,12 +103,9 @@ func Resize(buf []byte, o Options) ([]byte, error) {
} }
// Flatten image on a background, if necessary // Flatten image on a background, if necessary
black := Color{0, 0, 0} image, err = imageFlatten(image, imageType, o)
if imageType == PNG && o.Background != black { if err != nil {
image, err = vipsFlatten(image, o.Background) return nil, err
if err != nil {
return nil, err
}
} }
saveOptions := vipsSaveOptions{ saveOptions := vipsSaveOptions{
@ -312,6 +309,15 @@ 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

@ -252,14 +252,24 @@ 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) { func vipsFlattenBackground(image *C.VipsImage, background Color) (*C.VipsImage, error) {
var outImage *C.VipsImage 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]))) backgroundC := [3]C.double{
if err != 0 { 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 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) { 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 // Apply the proper colour space
var outImage *C.VipsImage var outImage *C.VipsImage
if vipsColourspaceIsSupported(image) { if vipsColourspaceIsSupported(image) {
err := int(C.vips_colourspace_bridge(image, &outImage, interpretation)) err := C.vips_colourspace_bridge(image, &outImage, interpretation)
if err != 0 { if int(err) != 0 {
return nil, catchVipsError() return nil, catchVipsError()
} }
C.g_object_unref(C.gpointer(image)) C.g_object_unref(C.gpointer(image))

@ -227,7 +227,7 @@ vips_webpsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int qual
} }
int 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); VipsArrayDouble *vipsBackground = vips_array_double_new(background, 3);
return vips_flatten(in, out, return vips_flatten(in, out,
"background", vipsBackground, "background", vipsBackground,

Loading…
Cancel
Save