mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-14 18:05:55 -07:00
refactor(resize)
This commit is contained in:
parent
cdade0c27f
commit
40dd19fa6a
8 changed files with 207 additions and 141 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -4,3 +4,4 @@ bin
|
||||||
/*.jpg
|
/*.jpg
|
||||||
/*.png
|
/*.png
|
||||||
/*.webp
|
/*.webp
|
||||||
|
/fixtures/*_out.*
|
||||||
|
|
|
||||||
13
README.md
13
README.md
|
|
@ -15,13 +15,13 @@ bimg was heavily inspired in [sharp](https://github.com/lovell/sharp), a great n
|
||||||
|
|
||||||
- [libvips](https://github.com/jcupitt/libvips) v7.40.0+ (7.42.0+ recommended)
|
- [libvips](https://github.com/jcupitt/libvips) v7.40.0+ (7.42.0+ recommended)
|
||||||
- C compatible compiler such as gcc 4.6+ or clang 3.0+
|
- C compatible compiler such as gcc 4.6+ or clang 3.0+
|
||||||
|
- Go 1.3+
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go get gopkg.in/h2non/bimg.v0
|
go get gopkg.in/h2non/bimg.v0
|
||||||
```
|
```
|
||||||
Requires Go 1.3+
|
|
||||||
|
|
||||||
### libvips
|
### libvips
|
||||||
|
|
||||||
|
|
@ -37,13 +37,22 @@ The [install script](https://github.com/lovell/sharp/blob/master/preinstall.sh)
|
||||||
- Resize
|
- Resize
|
||||||
- Enlarge
|
- Enlarge
|
||||||
- Crop
|
- Crop
|
||||||
- Zoom
|
|
||||||
- Rotate
|
- Rotate
|
||||||
- Flip/Flop
|
- Flip/Flop
|
||||||
- Extract area
|
- Extract area
|
||||||
- Extract image metadata (size, format, profile, orientation...)
|
- Extract image metadata (size, format, profile, orientation...)
|
||||||
- Image conversion to multiple formats
|
- Image conversion to multiple formats
|
||||||
|
|
||||||
|
## Performance
|
||||||
|
|
||||||
|
libvips is probably the faster open source solution for image processing.
|
||||||
|
Here you can see some performance test comparisons for multiple scenarios:
|
||||||
|
|
||||||
|
- [libvips speed and memory usage](http://www.vips.ecs.soton.ac.uk/index.php?title=Speed_and_Memory_Use)
|
||||||
|
- [sharp performance tests](https://github.com/lovell/sharp#the-task)
|
||||||
|
|
||||||
|
bimg performance tests coming soon!
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
|
|
|
||||||
|
|
@ -48,13 +48,13 @@ func Metadata(buf []byte) (ImageMetadata, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
metadata := ImageMetadata{
|
metadata := ImageMetadata{
|
||||||
|
Size: size,
|
||||||
|
Channels: int(image.Bands),
|
||||||
Orientation: vipsExifOrientation(image),
|
Orientation: vipsExifOrientation(image),
|
||||||
Alpha: vipsHasAlpha(image),
|
Alpha: vipsHasAlpha(image),
|
||||||
Profile: vipsHasProfile(image),
|
Profile: vipsHasProfile(image),
|
||||||
Space: vipsSpace(image),
|
Space: vipsSpace(image),
|
||||||
Channels: vipsImageBands(image),
|
|
||||||
Type: getImageTypeName(imageType),
|
Type: getImageTypeName(imageType),
|
||||||
Size: size,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return metadata, nil
|
return metadata, nil
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,14 @@ const QUALITY = 80
|
||||||
|
|
||||||
type Gravity int
|
type Gravity int
|
||||||
|
|
||||||
|
const (
|
||||||
|
CENTRE Gravity = iota
|
||||||
|
NORTH
|
||||||
|
EAST
|
||||||
|
SOUTH
|
||||||
|
WEST
|
||||||
|
)
|
||||||
|
|
||||||
type Interpolator int
|
type Interpolator int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
|
||||||
179
resize.go
179
resize.go
|
|
@ -11,14 +11,6 @@ import (
|
||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
CENTRE Gravity = iota
|
|
||||||
NORTH
|
|
||||||
EAST
|
|
||||||
SOUTH
|
|
||||||
WEST
|
|
||||||
)
|
|
||||||
|
|
||||||
func Resize(buf []byte, o Options) ([]byte, error) {
|
func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
defer C.vips_thread_shutdown()
|
defer C.vips_thread_shutdown()
|
||||||
|
|
||||||
|
|
@ -46,37 +38,8 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
inWidth := int(image.Xsize)
|
inWidth := int(image.Xsize)
|
||||||
inHeight := int(image.Ysize)
|
inHeight := int(image.Ysize)
|
||||||
|
|
||||||
// prepare for factor
|
|
||||||
factor := 0.0
|
|
||||||
|
|
||||||
// image calculations
|
// image calculations
|
||||||
switch {
|
factor := imageCalculations(o, inWidth, inHeight)
|
||||||
// Fixed width and height
|
|
||||||
case o.Width > 0 && o.Height > 0:
|
|
||||||
xf := float64(inWidth) / float64(o.Width)
|
|
||||||
yf := float64(inHeight) / float64(o.Height)
|
|
||||||
if o.Crop {
|
|
||||||
factor = math.Min(xf, yf)
|
|
||||||
} else {
|
|
||||||
factor = math.Max(xf, yf)
|
|
||||||
}
|
|
||||||
// Fixed width, auto height
|
|
||||||
case o.Width > 0:
|
|
||||||
factor = float64(inWidth) / float64(o.Width)
|
|
||||||
o.Height = int(math.Floor(float64(inHeight) / factor))
|
|
||||||
// Fixed height, auto width
|
|
||||||
case o.Height > 0:
|
|
||||||
factor = float64(inHeight) / float64(o.Height)
|
|
||||||
o.Width = int(math.Floor(float64(inWidth) / factor))
|
|
||||||
// Identity transform
|
|
||||||
default:
|
|
||||||
factor = 1
|
|
||||||
o.Width = inWidth
|
|
||||||
o.Height = inHeight
|
|
||||||
}
|
|
||||||
|
|
||||||
debug("transform from %dx%d to %dx%d", inWidth, inHeight, o.Width, o.Height)
|
|
||||||
|
|
||||||
shrink := int(math.Max(math.Floor(factor), 1))
|
shrink := int(math.Max(math.Floor(factor), 1))
|
||||||
residual := float64(shrink) / factor
|
residual := float64(shrink) / factor
|
||||||
|
|
||||||
|
|
@ -91,12 +54,10 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
debug("factor: %v, shrink: %v, residual: %v", factor, shrink, residual)
|
|
||||||
|
|
||||||
// Try to use libjpeg shrink-on-load
|
// Try to use libjpeg shrink-on-load
|
||||||
if imageType == JPEG && shrink >= 2 {
|
if imageType == JPEG && shrink >= 2 {
|
||||||
// Recalculate integral shrink and double residual
|
// Recalculate integral shrink and double residual
|
||||||
tmpImage, factor, err := shrinkJpeg(buf, factor, shrink)
|
tmpImage, factor, err := shrinkJpegImage(buf, factor, shrink)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -108,58 +69,35 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculate integral box shrink
|
||||||
|
windowSize := vipsWindowSize(o.Interpolator.String())
|
||||||
|
if factor >= 2 && windowSize > 3 {
|
||||||
|
// Shrink less, affine more with interpolators that use at least 4x4 pixel window, e.g. bicubic
|
||||||
|
shrink = int(math.Max(float64(math.Floor(factor*3.0/windowSize)), 1))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use vips_shrink with the integral reduction
|
||||||
if shrink > 1 {
|
if shrink > 1 {
|
||||||
debug("shrink %d", shrink)
|
image, residual, err = shrinkImage(image, o, residual, shrink)
|
||||||
// Use vips_shrink with the integral reduction
|
|
||||||
image, err = vipsShrink(image, shrink)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recalculate residual float based on dimensions of required vs shrunk images
|
|
||||||
shrunkWidth := int(image.Xsize)
|
|
||||||
shrunkHeight := int(image.Ysize)
|
|
||||||
|
|
||||||
residualx := float64(o.Width) / float64(shrunkWidth)
|
|
||||||
residualy := float64(o.Height) / float64(shrunkHeight)
|
|
||||||
if o.Crop {
|
|
||||||
residual = math.Max(residualx, residualy)
|
|
||||||
} else {
|
|
||||||
residual = math.Min(residualx, residualy)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use vips_affine with the remaining float part
|
// Use vips_affine with the remaining float part
|
||||||
if residual != 0 {
|
if residual != 0 {
|
||||||
debug("residual %.2f", residual)
|
|
||||||
image, err = vipsAffine(image, residual, o.Interpolator)
|
image, err = vipsAffine(image, residual, o.Interpolator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Crop/embed
|
debug("factor: %v, shrink: %v, residual: %v", factor, shrink, residual)
|
||||||
affinedWidth := int(image.Xsize)
|
|
||||||
affinedHeight := int(image.Ysize)
|
|
||||||
|
|
||||||
if affinedWidth != o.Width || affinedHeight != o.Height {
|
// Extract image
|
||||||
if o.Crop {
|
image, err = extractImage(image, o)
|
||||||
left, top := calculateCrop(affinedWidth, affinedHeight, o.Width, o.Height, o.Gravity)
|
if err != nil {
|
||||||
o.Width = int(math.Min(float64(inWidth), float64(o.Width)))
|
return nil, err
|
||||||
o.Height = int(math.Min(float64(inHeight), float64(o.Height)))
|
|
||||||
debug("crop image to %dx%d to %dx%d", left, top, o.Width, o.Height)
|
|
||||||
image, err = vipsExtract(image, left, top, o.Width, o.Height)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else if o.Embed {
|
|
||||||
left := (o.Width - affinedWidth) / 2
|
|
||||||
top := (o.Height - affinedHeight) / 2
|
|
||||||
image, err = vipsEmbed(image, left, top, o.Width, o.Height, o.Extend)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rotation, flip := calculateRotationAndFlip(image, o.Rotate)
|
rotation, flip := calculateRotationAndFlip(image, o.Rotate)
|
||||||
|
|
@ -171,8 +109,7 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if o.Rotate > 0 {
|
if o.Rotate > 0 {
|
||||||
rotation := calculateRotation(o.Rotate)
|
image, err = vipsRotate(image, getAngle(o.Rotate))
|
||||||
image, err = vipsRotate(image, rotation)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -195,11 +132,67 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
C.vips_error_clear()
|
|
||||||
|
|
||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func extractImage(image *C.struct__VipsImage, o Options) (*C.struct__VipsImage, error) {
|
||||||
|
var err error = nil
|
||||||
|
affinedWidth := int(image.Xsize)
|
||||||
|
affinedHeight := int(image.Ysize)
|
||||||
|
|
||||||
|
if affinedWidth != o.Width || affinedHeight != o.Height {
|
||||||
|
width := int(math.Min(float64(affinedWidth), float64(o.Width)))
|
||||||
|
height := int(math.Min(float64(affinedHeight), float64(o.Height)))
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case o.Crop:
|
||||||
|
left, top := calculateCrop(affinedWidth, affinedHeight, o.Width, o.Height, o.Gravity)
|
||||||
|
image, err = vipsExtract(image, left, top, width, height)
|
||||||
|
break
|
||||||
|
case o.Embed:
|
||||||
|
left, top := (o.Width-affinedWidth)/2, (o.Height-affinedHeight)/2
|
||||||
|
image, err = vipsEmbed(image, left, top, o.Width, o.Height, o.Extend)
|
||||||
|
break
|
||||||
|
case o.Top > 0 && o.Left > 0:
|
||||||
|
image, err = vipsExtract(image, o.Left, o.Top, width, height)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return image, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func imageCalculations(o Options, inWidth, inHeight int) float64 {
|
||||||
|
factor := 1.0
|
||||||
|
|
||||||
|
switch {
|
||||||
|
// Fixed width and height
|
||||||
|
case o.Width > 0 && o.Height > 0:
|
||||||
|
xf := float64(inWidth) / float64(o.Width)
|
||||||
|
yf := float64(inHeight) / float64(o.Height)
|
||||||
|
if o.Crop {
|
||||||
|
factor = math.Min(xf, yf)
|
||||||
|
} else {
|
||||||
|
factor = math.Max(xf, yf)
|
||||||
|
}
|
||||||
|
// Fixed width, auto height
|
||||||
|
case o.Width > 0:
|
||||||
|
factor = float64(inWidth) / float64(o.Width)
|
||||||
|
o.Height = int(math.Floor(float64(inHeight) / factor))
|
||||||
|
// Fixed height, auto width
|
||||||
|
case o.Height > 0:
|
||||||
|
factor = float64(inHeight) / float64(o.Height)
|
||||||
|
o.Width = int(math.Floor(float64(inWidth) / factor))
|
||||||
|
// Identity transform
|
||||||
|
default:
|
||||||
|
o.Width = inWidth
|
||||||
|
o.Height = inHeight
|
||||||
|
}
|
||||||
|
|
||||||
|
return factor
|
||||||
|
}
|
||||||
|
|
||||||
func calculateCrop(inWidth, inHeight, outWidth, outHeight int, gravity Gravity) (int, int) {
|
func calculateCrop(inWidth, inHeight, outWidth, outHeight int, gravity Gravity) (int, int) {
|
||||||
left, top := 0, 0
|
left, top := 0, 0
|
||||||
|
|
||||||
|
|
@ -266,7 +259,27 @@ func calculateRotationAndFlip(image *C.struct__VipsImage, angle Angle) (Angle, b
|
||||||
return rotate, flip
|
return rotate, flip
|
||||||
}
|
}
|
||||||
|
|
||||||
func shrinkJpeg(buf []byte, factor float64, shrink int) (*C.struct__VipsImage, float64, error) {
|
func shrinkImage(image *C.struct__VipsImage, o Options, residual float64, shrink int) (*C.struct__VipsImage, float64, error) {
|
||||||
|
// Use vips_shrink with the integral reduction
|
||||||
|
image, err := vipsShrink(image, shrink)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recalculate residual float based on dimensions of required vs shrunk images
|
||||||
|
residualx := float64(o.Width) / float64(image.Xsize)
|
||||||
|
residualy := float64(o.Height) / float64(image.Ysize)
|
||||||
|
|
||||||
|
if o.Crop {
|
||||||
|
residual = math.Max(residualx, residualy)
|
||||||
|
} else {
|
||||||
|
residual = math.Min(residualx, residualy)
|
||||||
|
}
|
||||||
|
|
||||||
|
return image, residual, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func shrinkJpegImage(buf []byte, factor float64, shrink int) (*C.struct__VipsImage, float64, error) {
|
||||||
shrinkOnLoad := 1
|
shrinkOnLoad := 1
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
|
|
@ -293,7 +306,7 @@ func shrinkJpeg(buf []byte, factor float64, shrink int) (*C.struct__VipsImage, f
|
||||||
return nil, factor, nil
|
return nil, factor, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateRotation(angle Angle) Angle {
|
func getAngle(angle Angle) Angle {
|
||||||
divisor := angle % 90
|
divisor := angle % 90
|
||||||
if divisor != 0 {
|
if divisor != 0 {
|
||||||
angle = angle - divisor
|
angle = angle - divisor
|
||||||
|
|
|
||||||
|
|
@ -28,14 +28,16 @@ func TestResize(t *testing.T) {
|
||||||
t.Fatal("Image is not jpeg")
|
t.Fatal("Image is not jpeg")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ioutil.WriteFile("result.jpg", newImg, 0644)
|
err = ioutil.WriteFile("fixtures/test_out.jpg", newImg, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Cannot save the image")
|
t.Fatal("Cannot save the image")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConvert(t *testing.T) {
|
func TestConvert(t *testing.T) {
|
||||||
options := Options{Width: 640, Height: 480, Crop: true, Type: PNG}
|
width, height := 640, 480
|
||||||
|
|
||||||
|
options := Options{Width: width, Height: height, Crop: true, Type: PNG}
|
||||||
img, err := os.Open("fixtures/test.jpg")
|
img, err := os.Open("fixtures/test.jpg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
@ -56,7 +58,47 @@ func TestConvert(t *testing.T) {
|
||||||
t.Fatal("Image is not png")
|
t.Fatal("Image is not png")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ioutil.WriteFile("result.png", newImg, 0644)
|
size, _ := Size(newImg)
|
||||||
|
if size.Height != height || size.Width != width {
|
||||||
|
t.Fatal("Invalid image size")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ioutil.WriteFile("fixtures/test_out.png", newImg, 0644)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Cannot save the image")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestResizePngWithTransparency(t *testing.T) {
|
||||||
|
width, height := 300, 240
|
||||||
|
|
||||||
|
options := Options{Width: width, Height: height, Crop: true}
|
||||||
|
img, err := os.Open("fixtures/transparent.png")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer img.Close()
|
||||||
|
|
||||||
|
buf, err := ioutil.ReadAll(img)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
newImg, err := Resize(buf, options)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if DetermineImageType(newImg) != PNG {
|
||||||
|
t.Fatal("Image is not png")
|
||||||
|
}
|
||||||
|
|
||||||
|
size, _ := Size(newImg)
|
||||||
|
if size.Height != height || size.Width != width {
|
||||||
|
t.Fatal("Invalid image size")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = ioutil.WriteFile("fixtures/transparent_out.png", newImg, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Cannot save the image")
|
t.Fatal("Cannot save the image")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
16
vips.go
16
vips.go
|
|
@ -209,18 +209,14 @@ func vipsHasProfile(image *C.struct__VipsImage) bool {
|
||||||
return int(C.has_profile_embed(image)) > 0
|
return int(C.has_profile_embed(image)) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func vipsWindowSize(name string) int {
|
func vipsWindowSize(name string) float64 {
|
||||||
return int(C.interpolator_window_size(C.CString(name)))
|
return float64(C.interpolator_window_size(C.CString(name)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func vipsSpace(image *C.struct__VipsImage) string {
|
func vipsSpace(image *C.struct__VipsImage) string {
|
||||||
return C.GoString(C.vips_enum_nick_bridge(image))
|
return C.GoString(C.vips_enum_nick_bridge(image))
|
||||||
}
|
}
|
||||||
|
|
||||||
func vipsImageBands(image *C.struct__VipsImage) int {
|
|
||||||
return int(C.vips_image_bands(image))
|
|
||||||
}
|
|
||||||
|
|
||||||
type vipsSaveOptions struct {
|
type vipsSaveOptions struct {
|
||||||
Quality int
|
Quality int
|
||||||
Compression int
|
Compression int
|
||||||
|
|
@ -232,6 +228,10 @@ func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) {
|
||||||
length := C.size_t(0)
|
length := C.size_t(0)
|
||||||
err := C.int(0)
|
err := C.int(0)
|
||||||
|
|
||||||
|
// cleanup
|
||||||
|
defer C.g_object_unref(C.gpointer(image))
|
||||||
|
defer C.g_free(C.gpointer(ptr))
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case o.Type == PNG:
|
case o.Type == PNG:
|
||||||
err = C.vips_pngsave_bridge(image, &ptr, &length, 1, C.int(o.Compression), C.int(o.Quality), 0)
|
err = C.vips_pngsave_bridge(image, &ptr, &length, 1, C.int(o.Compression), C.int(o.Quality), 0)
|
||||||
|
|
@ -244,14 +244,12 @@ func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
C.g_object_unref(C.gpointer(image))
|
|
||||||
if err != 0 {
|
if err != 0 {
|
||||||
return nil, catchVipsError()
|
return nil, catchVipsError()
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := C.GoBytes(ptr, C.int(length))
|
buf := C.GoBytes(ptr, C.int(length))
|
||||||
// cleanup
|
C.vips_error_clear()
|
||||||
C.g_free(C.gpointer(ptr))
|
|
||||||
|
|
||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
79
vips.h
79
vips.h
|
|
@ -59,15 +59,15 @@ vips_rotate(VipsImage *in, VipsImage **buf, int angle)
|
||||||
|
|
||||||
int
|
int
|
||||||
vips_exif_orientation(VipsImage *image) {
|
vips_exif_orientation(VipsImage *image) {
|
||||||
int orientation = 0;
|
int orientation = 0;
|
||||||
const char **exif;
|
const char **exif;
|
||||||
if (
|
if (
|
||||||
vips_image_get_typeof(image, "exif-ifd0-Orientation") != 0 &&
|
vips_image_get_typeof(image, "exif-ifd0-Orientation") != 0 &&
|
||||||
!vips_image_get_string(image, "exif-ifd0-Orientation", exif)
|
!vips_image_get_string(image, "exif-ifd0-Orientation", exif)
|
||||||
) {
|
) {
|
||||||
orientation = atoi(exif[0]);
|
orientation = atoi(exif[0]);
|
||||||
}
|
}
|
||||||
return orientation;
|
return orientation;
|
||||||
};
|
};
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
@ -78,18 +78,18 @@ has_profile_embed(VipsImage *image) {
|
||||||
int
|
int
|
||||||
has_alpha_channel(VipsImage *image) {
|
has_alpha_channel(VipsImage *image) {
|
||||||
return (
|
return (
|
||||||
(image->Bands == 2 && image->Type == VIPS_INTERPRETATION_B_W) ||
|
(image->Bands == 2 && image->Type == VIPS_INTERPRETATION_B_W) ||
|
||||||
(image->Bands == 4 && image->Type != VIPS_INTERPRETATION_CMYK) ||
|
(image->Bands == 4 && image->Type != VIPS_INTERPRETATION_CMYK) ||
|
||||||
(image->Bands == 5 && image->Type == VIPS_INTERPRETATION_CMYK)
|
(image->Bands == 5 && image->Type == VIPS_INTERPRETATION_CMYK)
|
||||||
) ? 1 : 0;
|
) ? 1 : 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
int
|
int
|
||||||
interpolator_window_size(char const *name) {
|
interpolator_window_size(char const *name) {
|
||||||
VipsInterpolate *interpolator = vips_interpolate_new(name);
|
VipsInterpolate *interpolator = vips_interpolate_new(name);
|
||||||
int window_size = vips_interpolate_get_window_size(interpolator);
|
int window_size = vips_interpolate_get_window_size(interpolator);
|
||||||
g_object_unref(interpolator);
|
g_object_unref(interpolator);
|
||||||
return window_size;
|
return window_size;
|
||||||
};
|
};
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
|
|
@ -97,11 +97,6 @@ vips_enum_nick_bridge(VipsImage *image) {
|
||||||
return vips_enum_nick(VIPS_TYPE_INTERPRETATION, image->Type);
|
return vips_enum_nick(VIPS_TYPE_INTERPRETATION, image->Type);
|
||||||
};
|
};
|
||||||
|
|
||||||
int
|
|
||||||
vips_image_bands(VipsImage *image) {
|
|
||||||
return image->Bands;
|
|
||||||
};
|
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
|
@ -130,10 +125,10 @@ int
|
||||||
vips_pngsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int compression, int quality, int interlace)
|
vips_pngsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int compression, int quality, int interlace)
|
||||||
{
|
{
|
||||||
#if (VIPS_MAJOR_VERSION >= 8 || (VIPS_MAJOR_VERSION >= 7 && VIPS_MINOR_VERSION >= 42))
|
#if (VIPS_MAJOR_VERSION >= 8 || (VIPS_MAJOR_VERSION >= 7 && VIPS_MINOR_VERSION >= 42))
|
||||||
return vips_pngsave_buffer(in, buf, len, "strip", FALSE, "compression", compression,
|
return vips_pngsave_buffer(in, buf, len, "strip", FALSE, "compression", compression,
|
||||||
"interlace", interlace, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL);
|
"interlace", interlace, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL);
|
||||||
#else
|
#else
|
||||||
return vips_pngsave_buffer(image, buf, len, "strip", FALSE, "compression", compression,
|
return vips_pngsave_buffer(image, buf, len, "strip", FALSE, "compression", compression,
|
||||||
"interlace", interlace, NULL);
|
"interlace", interlace, NULL);
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
@ -146,26 +141,26 @@ vips_webpsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int qual
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
if (imageType == JPEG) {
|
if (imageType == JPEG) {
|
||||||
code = vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
code = vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
||||||
} else if (imageType == PNG) {
|
} else if (imageType == PNG) {
|
||||||
code = vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
code = vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
||||||
} else if (imageType == WEBP) {
|
} else if (imageType == WEBP) {
|
||||||
code = vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
code = vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
||||||
} else if (imageType == TIFF) {
|
} else if (imageType == TIFF) {
|
||||||
code = vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
code = vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
||||||
#if (VIPS_MAJOR_VERSION >= 8)
|
#if (VIPS_MAJOR_VERSION >= 8)
|
||||||
} else if (imageType == MAGICK) {
|
} else if (imageType == MAGICK) {
|
||||||
code = vips_magickload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
code = vips_magickload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
if (out != NULL) {
|
|
||||||
// Listen for "postclose" signal to delete input buffer
|
|
||||||
//g_signal_connect(out, "postclose", G_CALLBACK(vips_malloc_cb), buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return code;
|
if (out != NULL) {
|
||||||
|
// Listen for "postclose" signal to delete input buffer
|
||||||
|
//g_signal_connect(out, "postclose", G_CALLBACK(vips_malloc_cb), buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
return code;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue