minor cleanup of resizeParams code

This commit is contained in:
Will Norris 2015-11-26 12:05:13 -08:00
parent 8efff4b6a9
commit d9e4cf282a
2 changed files with 12 additions and 8 deletions

View file

@ -79,7 +79,9 @@ func Transform(img []byte, opt Options) ([]byte, error) {
return buf.Bytes(), nil
}
func resizeParams(m image.Image, opt *Options) (w, h int, resize bool) {
// resizeParams determines if the image needs to be resized, and if so, the
// dimensions to resize to.
func resizeParams(m image.Image, opt Options) (w, h int, resize bool) {
// convert percentage width and height values to absolute values
imgW := m.Bounds().Max.X - m.Bounds().Min.X
imgH := m.Bounds().Max.Y - m.Bounds().Min.Y
@ -98,7 +100,7 @@ func resizeParams(m image.Image, opt *Options) (w, h int, resize bool) {
h = int(opt.Height)
}
// never resize larger than the original image
// never resize larger than the original image unless specifically allowed
if !opt.ScaleUp {
if w > imgW {
w = imgW
@ -107,10 +109,12 @@ func resizeParams(m image.Image, opt *Options) (w, h int, resize bool) {
h = imgH
}
}
// if requested width and height match the original, skip resizing
if (w == imgW || w == 0) && (h == imgH || h == 0) {
return 0, 0, false
}
return w, h, true
}
@ -118,7 +122,7 @@ func resizeParams(m image.Image, opt *Options) (w, h int, resize bool) {
// in opt.
func transformImage(m image.Image, opt Options) image.Image {
// resize if needed
if w, h, resize := resizeParams(m, &opt); resize {
if w, h, resize := resizeParams(m, opt); resize {
if opt.Fit {
m = imaging.Fit(m, w, h, resampleFilter)
} else {