resize.go 4.3 KB

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