mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-18 03:36:33 -07:00
fix(#31)
This commit is contained in:
parent
eb933a8c38
commit
c2a502a611
3 changed files with 17 additions and 16 deletions
28
resize.go
28
resize.go
|
|
@ -48,6 +48,13 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
shrink := int(math.Max(math.Floor(factor), 1))
|
shrink := int(math.Max(math.Floor(factor), 1))
|
||||||
residual := float64(shrink) / factor
|
residual := float64(shrink) / factor
|
||||||
|
|
||||||
|
// 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))
|
||||||
|
}
|
||||||
|
|
||||||
// Do not enlarge the output if the input width *or* height are already less than the required dimensions
|
// Do not enlarge the output if the input width *or* height are already less than the required dimensions
|
||||||
if o.Enlarge == false {
|
if o.Enlarge == false {
|
||||||
if inWidth < o.Width && inHeight < o.Height {
|
if inWidth < o.Width && inHeight < o.Height {
|
||||||
|
|
@ -65,24 +72,16 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if tmpImage != nil {
|
image = tmpImage
|
||||||
image = tmpImage
|
factor = math.Max(factor, 1.0)
|
||||||
factor = math.Max(factor, 1.0)
|
shrink = int(math.Floor(factor))
|
||||||
shrink = int(math.Floor(factor))
|
residual = float64(shrink) / factor
|
||||||
residual = float64(shrink) / factor
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transform image if necessary
|
// Transform image if necessary
|
||||||
shouldTransform := o.Width != inWidth || o.Height != inHeight || o.AreaWidth > 0 || o.AreaHeight > 0
|
shouldTransform := o.Width != inWidth || o.Height != inHeight || o.AreaWidth > 0 || o.AreaHeight > 0
|
||||||
if shouldTransform {
|
if shouldTransform {
|
||||||
|
|
||||||
// Use vips_shrink with the integral reduction
|
// Use vips_shrink with the integral reduction
|
||||||
if shrink > 1 {
|
if shrink > 1 {
|
||||||
image, residual, err = shrinkImage(image, o, residual, shrink)
|
image, residual, err = shrinkImage(image, o, residual, shrink)
|
||||||
|
|
@ -90,7 +89,8 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Use vips_affine with the remaining float part
|
|
||||||
|
// Affine with the remaining float part
|
||||||
if residual != 0 {
|
if residual != 0 {
|
||||||
image, err = vipsAffine(image, residual, o.Interpolator)
|
image, err = vipsAffine(image, residual, o.Interpolator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import (
|
||||||
|
|
||||||
func TestResize(t *testing.T) {
|
func TestResize(t *testing.T) {
|
||||||
options := Options{Width: 800, Height: 600}
|
options := Options{Width: 800, Height: 600}
|
||||||
buf, _ := Read("fixtures/test.jpg")
|
buf, _ := Read("../vips/fixtures/large.jpg")
|
||||||
|
|
||||||
newImg, err := Resize(buf, options)
|
newImg, err := Resize(buf, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
3
vips.go
3
vips.go
|
|
@ -229,6 +229,7 @@ func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) {
|
||||||
err = C.vips_webpsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0)
|
err = C.vips_webpsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0)
|
||||||
break
|
break
|
||||||
default:
|
default:
|
||||||
|
debug("Save JPEG options: Q: %s", o.Quality)
|
||||||
err = C.vips_jpegsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0)
|
err = C.vips_jpegsave_bridge(image, &ptr, &length, 1, C.int(o.Quality), 0)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
@ -239,7 +240,7 @@ func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) {
|
||||||
|
|
||||||
buf := C.GoBytes(ptr, C.int(length))
|
buf := C.GoBytes(ptr, C.int(length))
|
||||||
|
|
||||||
// Cleanup
|
// Clean up
|
||||||
C.g_free(C.gpointer(ptr))
|
C.g_free(C.gpointer(ptr))
|
||||||
C.vips_error_clear()
|
C.vips_error_clear()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue