resize.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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, resize.MitchellNetravali)
  22. package resize
  23. import (
  24. "image"
  25. "image/color"
  26. "runtime"
  27. )
  28. // Filter can interpolate at points (x,y)
  29. type Filter interface {
  30. Interpolate(u float32, y int) color.RGBA64
  31. }
  32. // InterpolationFunction return a Filter implementation
  33. // that operates on an image. Two factors
  34. // allow to scale the filter kernels in x- and y-direction
  35. // to prevent moire patterns.
  36. type InterpolationFunction func(image.Image, float32) Filter
  37. // Resize an image to new width and height using the interpolation function interp.
  38. // A new image with the given dimensions will be returned.
  39. // If one of the parameters width or height is set to 0, its size will be calculated so that
  40. // the aspect ratio is that of the originating image.
  41. // The resizing algorithm uses channels for parallel computation.
  42. func Resize(width, height uint, img image.Image, interp InterpolationFunction) image.Image {
  43. oldBounds := img.Bounds()
  44. oldWidth := float32(oldBounds.Dx())
  45. oldHeight := float32(oldBounds.Dy())
  46. scaleX, scaleY := calcFactors(width, height, oldWidth, oldHeight)
  47. tempImg := image.NewRGBA64(image.Rect(0, 0, oldBounds.Dy(), int(0.7+oldWidth/scaleX)))
  48. b := tempImg.Bounds()
  49. adjust := 0.5 * ((oldWidth-1.0)/scaleX - float32(b.Dy()-1))
  50. n := numJobs(b.Dy())
  51. c := make(chan int, n)
  52. for i := 0; i < n; i++ {
  53. go func(b image.Rectangle, c chan int) {
  54. filter := interp(img, float32(clampFactor(scaleX)))
  55. var u float32
  56. var color color.RGBA64
  57. for y := b.Min.Y; y < b.Max.Y; y++ {
  58. for x := b.Min.X; x < b.Max.X; x++ {
  59. u = scaleX*(float32(y)+adjust) + float32(oldBounds.Min.X)
  60. color = filter.Interpolate(u, x)
  61. i := tempImg.PixOffset(x, y)
  62. tempImg.Pix[i+0] = uint8(color.R >> 8)
  63. tempImg.Pix[i+1] = uint8(color.R)
  64. tempImg.Pix[i+2] = uint8(color.G >> 8)
  65. tempImg.Pix[i+3] = uint8(color.G)
  66. tempImg.Pix[i+4] = uint8(color.B >> 8)
  67. tempImg.Pix[i+5] = uint8(color.B)
  68. tempImg.Pix[i+6] = uint8(color.A >> 8)
  69. tempImg.Pix[i+7] = uint8(color.A)
  70. }
  71. }
  72. c <- 1
  73. }(image.Rect(b.Min.X, b.Min.Y+i*(b.Dy())/n, b.Max.X, b.Min.Y+(i+1)*(b.Dy())/n), c)
  74. }
  75. for i := 0; i < n; i++ {
  76. <-c
  77. }
  78. resultImg := image.NewRGBA64(image.Rect(0, 0, int(0.7+oldWidth/scaleX), int(0.7+oldHeight/scaleY)))
  79. b = resultImg.Bounds()
  80. adjust = 0.5 * ((oldHeight-1.0)/scaleY - float32(b.Dy()-1))
  81. for i := 0; i < n; i++ {
  82. go func(b image.Rectangle, c chan int) {
  83. filter := interp(tempImg, float32(clampFactor(scaleY)))
  84. var u float32
  85. var color color.RGBA64
  86. for y := b.Min.Y; y < b.Max.Y; y++ {
  87. for x := b.Min.X; x < b.Max.X; x++ {
  88. u = scaleY*(float32(y)+adjust) + float32(oldBounds.Min.Y)
  89. color = filter.Interpolate(u, x)
  90. i := resultImg.PixOffset(x, y)
  91. resultImg.Pix[i+0] = uint8(color.R >> 8)
  92. resultImg.Pix[i+1] = uint8(color.R)
  93. resultImg.Pix[i+2] = uint8(color.G >> 8)
  94. resultImg.Pix[i+3] = uint8(color.G)
  95. resultImg.Pix[i+4] = uint8(color.B >> 8)
  96. resultImg.Pix[i+5] = uint8(color.B)
  97. resultImg.Pix[i+6] = uint8(color.A >> 8)
  98. resultImg.Pix[i+7] = uint8(color.A)
  99. }
  100. }
  101. c <- 1
  102. }(image.Rect(b.Min.X, b.Min.Y+i*(b.Dy())/n, b.Max.X, b.Min.Y+(i+1)*(b.Dy())/n), c)
  103. }
  104. for i := 0; i < n; i++ {
  105. <-c
  106. }
  107. return resultImg
  108. }
  109. // Calculate scaling factors using old and new image dimensions.
  110. func calcFactors(width, height uint, oldWidth, oldHeight float32) (scaleX, scaleY float32) {
  111. if width == 0 {
  112. if height == 0 {
  113. scaleX = 1.0
  114. scaleY = 1.0
  115. } else {
  116. scaleY = oldHeight / float32(height)
  117. scaleX = scaleY
  118. }
  119. } else {
  120. scaleX = oldWidth / float32(width)
  121. if height == 0 {
  122. scaleY = scaleX
  123. } else {
  124. scaleY = oldHeight / float32(height)
  125. }
  126. }
  127. return
  128. }
  129. // Set filter scaling factor to avoid moire patterns.
  130. // This is only useful in case of downscaling (factor>1).
  131. func clampFactor(factor float32) float32 {
  132. if factor < 1 {
  133. factor = 1
  134. }
  135. return factor
  136. }
  137. // Set number of parallel jobs
  138. // but prevent resize from doing too much work
  139. // if #CPUs > width
  140. func numJobs(d int) (n int) {
  141. n = runtime.NumCPU()
  142. if n > d {
  143. n = d
  144. }
  145. return
  146. }