From b1ec2aada35271db2e584f96952f83efca5c96de Mon Sep 17 00:00:00 2001 From: esimov Date: Fri, 26 Apr 2019 16:14:49 +0300 Subject: [PATCH] Force to use the scaling option in case of big images --- cmd/caire/main.go | 2 +- process.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cmd/caire/main.go b/cmd/caire/main.go index 7e05b0e..5dac66e 100644 --- a/cmd/caire/main.go +++ b/cmd/caire/main.go @@ -38,7 +38,7 @@ var ( percentage = flag.Bool("perc", false, "Reduce image by percentage") square = flag.Bool("square", false, "Reduce image to square dimensions") 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") faceAngle = flag.Float64("angle", 0.0, "Plane rotated faces angle") cascade = flag.String("cc", "", "Cascade classifier") diff --git a/process.go b/process.go index befb00a..34d9921 100644 --- a/process.go +++ b/process.go @@ -15,6 +15,8 @@ import ( "golang.org/x/image/bmp" ) +const MaxResizeWithoutScaling = 2000 + // SeamCarver interface defines the Resize method. // This has to be implemented by every struct which declares a Resize method. 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. // 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. + + // 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.NewWidth > img.Bounds().Max.X || p.NewHeight > img.Bounds().Max.Y { return nil, errors.New("scale option can not be used on image enlargement")