mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-15 18:35:54 -07:00
fix(#43)
This commit is contained in:
parent
827eb82ea3
commit
5d3a306adc
3 changed files with 34 additions and 16 deletions
|
|
@ -55,6 +55,8 @@ const (
|
||||||
VERTICAL Direction = C.VIPS_DIRECTION_VERTICAL
|
VERTICAL Direction = C.VIPS_DIRECTION_VERTICAL
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const WATERMARK_FONT = "sans 10"
|
||||||
|
|
||||||
// Color represents a traditional RGB color scheme
|
// Color represents a traditional RGB color scheme
|
||||||
type Color struct {
|
type Color struct {
|
||||||
R, G, B uint8
|
R, G, B uint8
|
||||||
|
|
|
||||||
47
resize.go
47
resize.go
|
|
@ -72,23 +72,22 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
residual = float64(shrink) / factor
|
residual = float64(shrink) / factor
|
||||||
}
|
}
|
||||||
|
|
||||||
debug("Test %s, %s, %s", shrink, residual, factor)
|
// Zoom image, if necessary
|
||||||
|
|
||||||
// Zoom image if necessary
|
|
||||||
image, err = zoomImage(image, o.Zoom)
|
image, err = zoomImage(image, o.Zoom)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rotate / flip image if necessary
|
// Rotate / flip image, if necessary
|
||||||
image, err = rotateAndFlipImage(image, o)
|
image, err = rotateAndFlipImage(image, o)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transform image if necessary
|
// Transform image, if necessary
|
||||||
shouldTransform := o.Width != inWidth || o.Height != inHeight || o.AreaWidth > 0 || o.AreaHeight > 0
|
transformImage := o.Width != inWidth || o.Height != inHeight || o.AreaWidth > 0 || o.AreaHeight > 0
|
||||||
if shouldTransform {
|
|
||||||
|
if transformImage {
|
||||||
|
|
||||||
// Use vips_shrink with the integral reduction
|
// Use vips_shrink with the integral reduction
|
||||||
if shrink > 1 {
|
if shrink > 1 {
|
||||||
|
|
@ -98,9 +97,12 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
debug("Transform: factor=%v, shrink=%v, residual=%v, interpolator=%v",
|
||||||
|
factor, shrink, residual, o.Interpolator.String())
|
||||||
|
|
||||||
// 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 = affineImage(image, o, residual)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -111,12 +113,9 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
debug("Transform: factor=%v, shrink=%v, residual=%v, interpolator=%v",
|
|
||||||
factor, shrink, residual, o.Interpolator.String())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add watermark if necessary
|
// Add watermark, if necessary
|
||||||
image, err = watermakImage(image, o.Watermark)
|
image, err = watermakImage(image, o.Watermark)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
@ -148,6 +147,7 @@ func extractImage(image *C.struct__VipsImage, o Options) (*C.struct__VipsImage,
|
||||||
width := int(math.Min(float64(inWidth), float64(o.Width)))
|
width := int(math.Min(float64(inWidth), float64(o.Width)))
|
||||||
height := int(math.Min(float64(inHeight), float64(o.Height)))
|
height := int(math.Min(float64(inHeight), float64(o.Height)))
|
||||||
left, top := calculateCrop(inWidth, inHeight, o.Width, o.Height, o.Gravity)
|
left, top := calculateCrop(inWidth, inHeight, o.Width, o.Height, o.Gravity)
|
||||||
|
left, top = int(math.Max(float64(left), 0)), int(math.Max(float64(top), 0))
|
||||||
image, err = vipsExtract(image, left, top, width, height)
|
image, err = vipsExtract(image, left, top, width, height)
|
||||||
break
|
break
|
||||||
case o.Embed:
|
case o.Embed:
|
||||||
|
|
@ -162,7 +162,7 @@ func extractImage(image *C.struct__VipsImage, o Options) (*C.struct__VipsImage,
|
||||||
o.AreaHeight = o.Height
|
o.AreaHeight = o.Height
|
||||||
}
|
}
|
||||||
if o.AreaWidth == 0 || o.AreaHeight == 0 {
|
if o.AreaWidth == 0 || o.AreaHeight == 0 {
|
||||||
return nil, errors.New("Extract area width/height is required")
|
return nil, errors.New("Extract area width/height params are required")
|
||||||
}
|
}
|
||||||
image, err = vipsExtract(image, o.Left, o.Top, o.AreaWidth, o.AreaHeight)
|
image, err = vipsExtract(image, o.Left, o.Top, o.AreaWidth, o.AreaHeight)
|
||||||
break
|
break
|
||||||
|
|
@ -209,7 +209,7 @@ func watermakImage(image *C.struct__VipsImage, w Watermark) (*C.struct__VipsImag
|
||||||
|
|
||||||
// Defaults
|
// Defaults
|
||||||
if w.Font == "" {
|
if w.Font == "" {
|
||||||
w.Font = "sans 10"
|
w.Font = WATERMARK_FONT
|
||||||
}
|
}
|
||||||
if w.Width == 0 {
|
if w.Width == 0 {
|
||||||
w.Width = int(math.Floor(float64(image.Xsize / 6)))
|
w.Width = int(math.Floor(float64(image.Xsize / 6)))
|
||||||
|
|
@ -242,6 +242,23 @@ func zoomImage(image *C.struct__VipsImage, zoom int) (*C.struct__VipsImage, erro
|
||||||
return vipsZoom(image, zoom+1)
|
return vipsZoom(image, zoom+1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func affineImage(image *C.struct__VipsImage, o Options, residual float64) (*C.struct__VipsImage, error) {
|
||||||
|
newImage, err := vipsAffine(image, residual, o.Interpolator)
|
||||||
|
if err != nil {
|
||||||
|
C.g_object_unref(C.gpointer(image))
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if o.Enlarge || (o.Width > int(newImage.Xsize) && o.Height > int(newImage.Ysize)) {
|
||||||
|
C.g_object_unref(C.gpointer(image))
|
||||||
|
image = newImage
|
||||||
|
} else {
|
||||||
|
C.g_object_unref(C.gpointer(newImage))
|
||||||
|
}
|
||||||
|
|
||||||
|
return image, nil
|
||||||
|
}
|
||||||
|
|
||||||
func shrinkImage(image *C.struct__VipsImage, o Options, residual 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
|
// Use vips_shrink with the integral reduction
|
||||||
image, err := vipsShrink(image, shrink)
|
image, err := vipsShrink(image, shrink)
|
||||||
|
|
@ -309,8 +326,8 @@ func imageCalculations(o *Options, inWidth, inHeight int) float64 {
|
||||||
case o.Height > 0:
|
case o.Height > 0:
|
||||||
factor = yfactor
|
factor = yfactor
|
||||||
o.Width = int(math.Floor(float64(inWidth) / factor))
|
o.Width = int(math.Floor(float64(inWidth) / factor))
|
||||||
|
// Identity transform
|
||||||
default:
|
default:
|
||||||
// Identity transform
|
|
||||||
o.Width = inWidth
|
o.Width = inWidth
|
||||||
o.Height = inHeight
|
o.Height = inHeight
|
||||||
break
|
break
|
||||||
|
|
|
||||||
1
vips.go
1
vips.go
|
|
@ -311,7 +311,6 @@ func vipsAffine(input *C.struct__VipsImage, residual float64, i Interpolator) (*
|
||||||
interpolator := C.vips_interpolate_new(cstring)
|
interpolator := C.vips_interpolate_new(cstring)
|
||||||
|
|
||||||
defer C.free(unsafe.Pointer(cstring))
|
defer C.free(unsafe.Pointer(cstring))
|
||||||
defer C.g_object_unref(C.gpointer(input))
|
|
||||||
defer C.g_object_unref(C.gpointer(interpolator))
|
defer C.g_object_unref(C.gpointer(interpolator))
|
||||||
|
|
||||||
err := C.vips_affine_interpolator(input, &image, C.double(residual), 0, 0, C.double(residual), interpolator)
|
err := C.vips_affine_interpolator(input, &image, C.double(residual), 0, 0, C.double(residual), interpolator)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue