Force to use the scaling option in case of big images

This commit is contained in:
esimov
2019-04-26 16:14:49 +03:00
parent 0a8babe4c2
commit b1ec2aada3
2 changed files with 10 additions and 1 deletions

View File

@@ -38,7 +38,7 @@ var (
percentage = flag.Bool("perc", false, "Reduce image by percentage") percentage = flag.Bool("perc", false, "Reduce image by percentage")
square = flag.Bool("square", false, "Reduce image to square dimensions") square = flag.Bool("square", false, "Reduce image to square dimensions")
debug = flag.Bool("debug", false, "Use debugger") debug = flag.Bool("debug", false, "Use debugger")
scale = flag.Bool("scale", true, "Proportional scaling") scale = flag.Bool("scale", false, "Proportional scaling")
faceDetect = flag.Bool("face", false, "Use face detection") faceDetect = flag.Bool("face", false, "Use face detection")
faceAngle = flag.Float64("angle", 0.0, "Plane rotated faces angle") faceAngle = flag.Float64("angle", 0.0, "Plane rotated faces angle")
cascade = flag.String("cc", "", "Cascade classifier") cascade = flag.String("cc", "", "Cascade classifier")

View File

@@ -15,6 +15,8 @@ import (
"golang.org/x/image/bmp" "golang.org/x/image/bmp"
) )
const MaxResizeWithoutScaling = 2000
// SeamCarver interface defines the Resize method. // SeamCarver interface defines the Resize method.
// This has to be implemented by every struct which declares a Resize method. // This has to be implemented by every struct which declares a Resize method.
type SeamCarver interface { type SeamCarver interface {
@@ -114,6 +116,13 @@ func (p *Processor) Resize(img *image.NRGBA) (image.Image, error) {
// then the seam carving algorithm is applied only to the remaining pixels. // then the seam carving algorithm is applied only to the remaining pixels.
// Ex. : given an image of dimensions 2048x1536 if we want to resize to the 1024x500, // Ex. : given an image of dimensions 2048x1536 if we want to resize to the 1024x500,
// the tool first rescale the image to 1024x768, then it will remove the remaining 268px. // the tool first rescale the image to 1024x768, then it will remove the remaining 268px.
// Prevent memory overflow issue in case of huge images by switching to scaling first option
if img.Bounds().Dx() > MaxResizeWithoutScaling ||
img.Bounds().Dy() > MaxResizeWithoutScaling {
p.Scale = true
}
if p.Scale { if p.Scale {
if p.NewWidth > img.Bounds().Max.X || p.NewHeight > img.Bounds().Max.Y { if p.NewWidth > img.Bounds().Max.X || p.NewHeight > img.Bounds().Max.Y {
return nil, errors.New("scale option can not be used on image enlargement") return nil, errors.New("scale option can not be used on image enlargement")