github.com/aloncn/graphics-go@v0.0.1/graphics/scale.go (about)

     1  // Copyright 2011 The Graphics-Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package graphics
     6  
     7  import (
     8  	"code.google.com/p/graphics-go/graphics/interp"
     9  	"errors"
    10  	"image"
    11  	"image/draw"
    12  )
    13  
    14  // Scale produces a scaled version of the image using bilinear interpolation.
    15  func Scale(dst draw.Image, src image.Image) error {
    16  	if dst == nil {
    17  		return errors.New("graphics: dst is nil")
    18  	}
    19  	if src == nil {
    20  		return errors.New("graphics: src is nil")
    21  	}
    22  
    23  	b := dst.Bounds()
    24  	srcb := src.Bounds()
    25  	if b.Empty() || srcb.Empty() {
    26  		return nil
    27  	}
    28  	sx := float64(b.Dx()) / float64(srcb.Dx())
    29  	sy := float64(b.Dy()) / float64(srcb.Dy())
    30  	return I.Scale(sx, sy).Transform(dst, src, interp.Bilinear)
    31  }