resize.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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, 0, imgOld, Lanczos3)
  22. package resize
  23. import (
  24. "image"
  25. "image/color"
  26. "runtime"
  27. )
  28. // Trans2 is a 2-dimensional linear transformation.
  29. type Trans2 [6]float32
  30. // Apply the transformation to a point (x,y).
  31. func (t *Trans2) Eval(x, y float32) (u, v float32) {
  32. u = t[0]*x + t[1]*y + t[2]
  33. v = t[3]*x + t[4]*y + t[5]
  34. return
  35. }
  36. // Calculate scaling factors using old and new image dimensions.
  37. func calcFactors(width, height uint, oldWidth, oldHeight float32) (scaleX, scaleY float32) {
  38. if width == 0 {
  39. if height == 0 {
  40. scaleX = 1.0
  41. scaleY = 1.0
  42. } else {
  43. scaleY = oldHeight / float32(height)
  44. scaleX = scaleY
  45. }
  46. } else {
  47. scaleX = oldWidth / float32(width)
  48. if height == 0 {
  49. scaleY = scaleX
  50. } else {
  51. scaleY = oldHeight / float32(height)
  52. }
  53. }
  54. return
  55. }
  56. // InterpolationFunction return a color for an arbitrary point inside
  57. // an image
  58. type InterpolationFunction func(float32, float32, image.Image) color.RGBA64
  59. // Resize an image to new width and height using the interpolation function interp.
  60. // A new image with the given dimensions will be returned.
  61. // If one of the parameters width or height is set to 0, its size will be calculated so that
  62. // the aspect ratio is that of the originating image.
  63. // The resizing algorithm uses channels for parallel computation.
  64. func Resize(width, height uint, img image.Image, interp InterpolationFunction) image.Image {
  65. oldBounds := img.Bounds()
  66. oldWidth := float32(oldBounds.Dx())
  67. oldHeight := float32(oldBounds.Dy())
  68. scaleX, scaleY := calcFactors(width, height, oldWidth, oldHeight)
  69. t := Trans2{scaleX, 0, float32(oldBounds.Min.X), 0, scaleY, float32(oldBounds.Min.Y)}
  70. resizedImg := image.NewRGBA64(image.Rect(0, 0, int(oldWidth/scaleX), int(oldHeight/scaleY)))
  71. b := resizedImg.Bounds()
  72. n := numJobs(b.Dy())
  73. c := make(chan int, n)
  74. for i := 0; i < n; i++ {
  75. go func(b image.Rectangle, c chan int) {
  76. var u, v float32
  77. for y := b.Min.Y; y < b.Max.Y; y++ {
  78. for x := b.Min.X; x < b.Max.X; x++ {
  79. u, v = t.Eval(float32(x), float32(y))
  80. resizedImg.SetRGBA64(x, y, interp(u, v, img))
  81. }
  82. }
  83. c <- 1
  84. }(image.Rect(b.Min.X, b.Min.Y+i*(b.Dy())/n, b.Max.X, b.Min.Y+(i+1)*(b.Dy())/n), c)
  85. }
  86. for i := 0; i < n; i++ {
  87. <-c
  88. }
  89. return resizedImg
  90. }
  91. // Set number of parallel jobs
  92. // but prevent resize from doing too much work
  93. // if #CPUs > width
  94. func numJobs(d int) (n int) {
  95. n = runtime.NumCPU()
  96. if n > d {
  97. n = d
  98. }
  99. return
  100. }