scale.go 780 B

12345678910111213141516171819202122232425262728293031
  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. package graphics
  5. import (
  6. "code.google.com/p/graphics-go/graphics/interp"
  7. "errors"
  8. "image"
  9. "image/draw"
  10. )
  11. // Scale produces a scaled version of the image using bilinear interpolation.
  12. func Scale(dst draw.Image, src image.Image) error {
  13. if dst == nil {
  14. return errors.New("graphics: dst is nil")
  15. }
  16. if src == nil {
  17. return errors.New("graphics: src is nil")
  18. }
  19. b := dst.Bounds()
  20. srcb := src.Bounds()
  21. if b.Empty() || srcb.Empty() {
  22. return nil
  23. }
  24. sx := float64(b.Dx()) / float64(srcb.Dx())
  25. sy := float64(b.Dy()) / float64(srcb.Dy())
  26. return I.Scale(sx, sy).Transform(dst, src, interp.Bilinear)
  27. }