resize.go 4.7 KB

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