resize.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. Copyright (c) 2012, Jan Schlicht <jan.schlicht@gmail.com>
  3. Permission to use, copy, modify, and/or distribute this software for any purpose
  4. with or without fee is hereby granted, provided that the above copyright notice
  5. and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  7. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  8. FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  9. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  10. OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  11. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  12. THIS SOFTWARE.
  13. */
  14. // Package resize implements various image resizing methods.
  15. //
  16. // The package works with the Image interface described in the image package.
  17. // Various interpolation methods are provided and multiple processors may be
  18. // utilized in the computations.
  19. //
  20. // Example:
  21. // imgResized := resize.Resize(1000, -1, imgOld, Lanczos3)
  22. package resize
  23. import (
  24. "image"
  25. "image/color"
  26. "runtime"
  27. )
  28. var (
  29. // NCPU holds the number of available CPUs at runtime.
  30. NCPU = runtime.NumCPU()
  31. )
  32. // Trans2 is a 2-dimensional linear transformation.
  33. type Trans2 [6]float32
  34. // Apply the transformation to a point (x,y).
  35. func (t *Trans2) Eval(x, y float32) (u, v float32) {
  36. u = t[0]*x + t[1]*y + t[2]
  37. v = t[3]*x + t[4]*y + t[5]
  38. return
  39. }
  40. // Calculate scaling factors using old and new image dimensions.
  41. func calcFactors(w, h int, wo, ho float32) (sx, sy float32) {
  42. if w == -1 {
  43. if h == -1 {
  44. sx = 1.0
  45. sy = 1.0
  46. } else {
  47. sy = ho / float32(h)
  48. sx = sy
  49. }
  50. } else {
  51. sx = wo / float32(w)
  52. if h == -1 {
  53. sy = sx
  54. } else {
  55. sy = ho / float32(h)
  56. }
  57. }
  58. return
  59. }
  60. // InterpolationFunction return a color for an arbitrary point inside
  61. // an image
  62. type InterpolationFunction func(float32, float32, image.Image) color.RGBA64
  63. // Resize an image to new width w and height h using the interpolation function interp.
  64. // A new image with the given dimensions will be returned.
  65. // If one of the parameters w or h is set to -1, its size will be calculated so that
  66. // the aspect ratio is that of the originating image.
  67. // The resizing algorithm uses slices for parallel computation.
  68. func Resize(w int, h int, img image.Image, interp InterpolationFunction) image.Image {
  69. b_old := img.Bounds()
  70. w_old := float32(b_old.Dx())
  71. h_old := float32(b_old.Dy())
  72. scaleX, scaleY := calcFactors(w, h, w_old, h_old)
  73. t := Trans2{scaleX, 0, float32(b_old.Min.X), 0, scaleY, float32(b_old.Min.Y)}
  74. m := image.NewRGBA64(image.Rect(0, 0, int(w_old/scaleX), int(h_old/scaleY)))
  75. b := m.Bounds()
  76. c := make(chan int, NCPU)
  77. for i := 0; i < NCPU; i++ {
  78. go func(b image.Rectangle, c chan int) {
  79. var u, v float32
  80. for y := b.Min.Y; y < b.Max.Y; y++ {
  81. for x := b.Min.X; x < b.Max.X; x++ {
  82. u, v = t.Eval(float32(x), float32(y))
  83. m.SetRGBA64(x, y, interp(u, v, img))
  84. }
  85. }
  86. c <- 1
  87. }(image.Rect(b.Min.X, b.Min.Y+i*(b.Dy())/4, b.Max.X, b.Min.Y+(i+1)*(b.Dy())/4), c)
  88. }
  89. for i := 0; i < NCPU; i++ {
  90. <-c
  91. }
  92. return m
  93. }