mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-15 02:15:54 -07:00
feat(test): vips
This commit is contained in:
parent
414fe40c91
commit
39931145de
2 changed files with 92 additions and 38 deletions
76
vips.go
76
vips.go
|
|
@ -19,6 +19,12 @@ var (
|
||||||
initialized bool = false
|
initialized bool = false
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type vipsSaveOptions struct {
|
||||||
|
Quality int
|
||||||
|
Compression int
|
||||||
|
Type ImageType
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
if C.VIPS_MAJOR_VERSION <= 7 && C.VIPS_MINOR_VERSION < 40 {
|
if C.VIPS_MAJOR_VERSION <= 7 && C.VIPS_MINOR_VERSION < 40 {
|
||||||
panic("unsupported old vips version!")
|
panic("unsupported old vips version!")
|
||||||
|
|
@ -109,6 +115,38 @@ func vipsRead(buf []byte) (*C.struct__VipsImage, ImageType, error) {
|
||||||
return image, imageType, nil
|
return image, imageType, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) {
|
||||||
|
var ptr unsafe.Pointer
|
||||||
|
length := C.size_t(0)
|
||||||
|
err := C.int(0)
|
||||||
|
|
||||||
|
defer C.g_object_unref(C.gpointer(image))
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case o.Type == PNG:
|
||||||
|
err = C.vips_pngsave_bridge(image, &ptr, &length, 1, C.int(o.Compression), C.int(o.Quality), 0)
|
||||||
|
break
|
||||||
|
case o.Type == WEBP:
|
||||||
|
err = C.vips_webpsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0)
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
err = C.vips_jpegsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if int(err) != 0 {
|
||||||
|
return nil, catchVipsError()
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := C.GoBytes(ptr, C.int(length))
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
C.g_free(C.gpointer(ptr))
|
||||||
|
C.vips_error_clear()
|
||||||
|
|
||||||
|
return buf, nil
|
||||||
|
}
|
||||||
|
|
||||||
func vipsExtract(image *C.struct__VipsImage, left, top, width, height int) (*C.struct__VipsImage, error) {
|
func vipsExtract(image *C.struct__VipsImage, left, top, width, height int) (*C.struct__VipsImage, error) {
|
||||||
var buf *C.struct__VipsImage
|
var buf *C.struct__VipsImage
|
||||||
defer C.g_object_unref(C.gpointer(image))
|
defer C.g_object_unref(C.gpointer(image))
|
||||||
|
|
@ -207,44 +245,6 @@ func vipsImageType(buf []byte) ImageType {
|
||||||
return imageType
|
return imageType
|
||||||
}
|
}
|
||||||
|
|
||||||
type vipsSaveOptions struct {
|
|
||||||
Quality int
|
|
||||||
Compression int
|
|
||||||
Type ImageType
|
|
||||||
}
|
|
||||||
|
|
||||||
func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) {
|
|
||||||
var ptr unsafe.Pointer
|
|
||||||
length := C.size_t(0)
|
|
||||||
err := C.int(0)
|
|
||||||
|
|
||||||
defer C.g_object_unref(C.gpointer(image))
|
|
||||||
|
|
||||||
switch {
|
|
||||||
case o.Type == PNG:
|
|
||||||
err = C.vips_pngsave_bridge(image, &ptr, &length, 1, C.int(o.Compression), C.int(o.Quality), 0)
|
|
||||||
break
|
|
||||||
case o.Type == WEBP:
|
|
||||||
err = C.vips_webpsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0)
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
err = C.vips_jpegsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if int(err) != 0 {
|
|
||||||
return nil, catchVipsError()
|
|
||||||
}
|
|
||||||
|
|
||||||
buf := C.GoBytes(ptr, C.int(length))
|
|
||||||
|
|
||||||
// Cleanup
|
|
||||||
C.g_free(C.gpointer(ptr))
|
|
||||||
C.vips_error_clear()
|
|
||||||
|
|
||||||
return buf, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func vipsExifOrientation(image *C.struct__VipsImage) int {
|
func vipsExifOrientation(image *C.struct__VipsImage) int {
|
||||||
return int(C.vips_exif_orientation(image))
|
return int(C.vips_exif_orientation(image))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
54
vips_test.go
Normal file
54
vips_test.go
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
package bimg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestVipsRead(t *testing.T) {
|
||||||
|
files := []struct {
|
||||||
|
name string
|
||||||
|
expected ImageType
|
||||||
|
}{
|
||||||
|
{"test.jpg", JPEG},
|
||||||
|
{"test.png", PNG},
|
||||||
|
{"test.webp", WEBP},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
img, _ := os.Open(path.Join("fixtures", file.name))
|
||||||
|
buf, _ := ioutil.ReadAll(img)
|
||||||
|
defer img.Close()
|
||||||
|
|
||||||
|
image, imageType, _ := vipsRead(buf)
|
||||||
|
if image == nil {
|
||||||
|
t.Fatal("Empty image")
|
||||||
|
}
|
||||||
|
if imageType != file.expected {
|
||||||
|
t.Fatal("Empty image")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestVipsSave(t *testing.T) {
|
||||||
|
img, _ := os.Open(path.Join("fixtures", "test.jpg"))
|
||||||
|
buf, _ := ioutil.ReadAll(img)
|
||||||
|
defer img.Close()
|
||||||
|
|
||||||
|
image, _, _ := vipsRead(buf)
|
||||||
|
if image == nil {
|
||||||
|
t.Fatal("Empty image")
|
||||||
|
}
|
||||||
|
|
||||||
|
options := vipsSaveOptions{Quality: 95, Type: JPEG}
|
||||||
|
|
||||||
|
buf, err := vipsSave(image, options)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Cannot save the image")
|
||||||
|
}
|
||||||
|
if len(buf) == 0 {
|
||||||
|
t.Fatal("Empty image")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue