resize.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. "runtime"
  26. "sync"
  27. )
  28. // An InterpolationFunction provides the parameters that describe an
  29. // interpolation kernel. It returns the number of samples to take
  30. // and the kernel function to use for sampling.
  31. type InterpolationFunction func() (int, func(float64) float64)
  32. // Nearest-neighbor interpolation
  33. func NearestNeighbor() (int, func(float64) float64) {
  34. return 2, nearest
  35. }
  36. // Bilinear interpolation
  37. func Bilinear() (int, func(float64) float64) {
  38. return 2, linear
  39. }
  40. // Bicubic interpolation (with cubic hermite spline)
  41. func Bicubic() (int, func(float64) float64) {
  42. return 4, cubic
  43. }
  44. // Mitchell-Netravali interpolation
  45. func MitchellNetravali() (int, func(float64) float64) {
  46. return 4, mitchellnetravali
  47. }
  48. // Lanczos interpolation (a=2)
  49. func Lanczos2() (int, func(float64) float64) {
  50. return 4, lanczos2
  51. }
  52. // Lanczos interpolation (a=3)
  53. func Lanczos3() (int, func(float64) float64) {
  54. return 6, lanczos3
  55. }
  56. // values <1 will sharpen the image
  57. var blur = 1.0
  58. // Resize scales an image to new width and height using the interpolation function interp.
  59. // A new image with the given dimensions will be returned.
  60. // If one of the parameters width or height is set to 0, its size will be calculated so that
  61. // the aspect ratio is that of the originating image.
  62. // The resizing algorithm uses channels for parallel computation.
  63. func Resize(width, height uint, img image.Image, interp InterpolationFunction) image.Image {
  64. scaleX, scaleY := calcFactors(width, height, float64(img.Bounds().Dx()), float64(img.Bounds().Dy()))
  65. if width == 0 {
  66. width = uint(0.7 + float64(img.Bounds().Dx())/scaleX)
  67. }
  68. if height == 0 {
  69. height = uint(0.7 + float64(img.Bounds().Dy())/scaleY)
  70. }
  71. taps, kernel := interp()
  72. cpus := runtime.NumCPU()
  73. wg := sync.WaitGroup{}
  74. // Generic access to image.Image is slow in tight loops.
  75. // The optimal access has to be determined from the concrete image type.
  76. switch input := img.(type) {
  77. case *image.RGBA:
  78. // 8-bit precision
  79. temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  80. result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
  81. // horizontal filter, results in transposed temporary image
  82. coeffs, filterLength := createWeights8(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
  83. wg.Add(cpus)
  84. for i := 0; i < cpus; i++ {
  85. slice := makeSlice(temp, i, cpus).(*image.RGBA)
  86. go func() {
  87. defer wg.Done()
  88. resizeRGBA(input, slice, scaleX, coeffs, filterLength)
  89. }()
  90. }
  91. wg.Wait()
  92. // horizontal filter on transposed image, result is not transposed
  93. coeffs, filterLength = createWeights8(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  94. wg.Add(cpus)
  95. for i := 0; i < cpus; i++ {
  96. slice := makeSlice(result, i, cpus).(*image.RGBA)
  97. go func() {
  98. defer wg.Done()
  99. resizeRGBA(temp, slice, scaleY, coeffs, filterLength)
  100. }()
  101. }
  102. wg.Wait()
  103. return result
  104. case *image.YCbCr:
  105. // 8-bit precision
  106. // accessing the YCbCr arrays in a tight loop is slow.
  107. // converting the image before filtering will improve performance.
  108. inputAsRGBA := convertYCbCrToRGBA(input)
  109. temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  110. result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
  111. // horizontal filter, results in transposed temporary image
  112. coeffs, filterLength := createWeights8(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
  113. wg.Add(cpus)
  114. for i := 0; i < cpus; i++ {
  115. slice := makeSlice(temp, i, cpus).(*image.RGBA)
  116. go func() {
  117. defer wg.Done()
  118. resizeRGBA(inputAsRGBA, slice, scaleX, coeffs, filterLength)
  119. }()
  120. }
  121. wg.Wait()
  122. // horizontal filter on transposed image, result is not transposed
  123. coeffs, filterLength = createWeights8(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  124. wg.Add(cpus)
  125. for i := 0; i < cpus; i++ {
  126. slice := makeSlice(result, i, cpus).(*image.RGBA)
  127. go func() {
  128. defer wg.Done()
  129. resizeRGBA(temp, slice, scaleY, coeffs, filterLength)
  130. }()
  131. }
  132. wg.Wait()
  133. return result
  134. case *image.RGBA64:
  135. // 16-bit precision
  136. temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  137. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  138. // horizontal filter, results in transposed temporary image
  139. coeffs, filterLength := createWeights16(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
  140. wg.Add(cpus)
  141. for i := 0; i < cpus; i++ {
  142. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  143. go func() {
  144. defer wg.Done()
  145. resizeRGBA64(input, slice, scaleX, coeffs, filterLength)
  146. }()
  147. }
  148. wg.Wait()
  149. // horizontal filter on transposed image, result is not transposed
  150. coeffs, filterLength = createWeights16(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  151. wg.Add(cpus)
  152. for i := 0; i < cpus; i++ {
  153. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  154. go func() {
  155. defer wg.Done()
  156. resizeGeneric(temp, slice, scaleY, coeffs, filterLength)
  157. }()
  158. }
  159. wg.Wait()
  160. return result
  161. case *image.Gray:
  162. // 8-bit precision
  163. temp := image.NewGray(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  164. result := image.NewGray(image.Rect(0, 0, int(width), int(height)))
  165. // horizontal filter, results in transposed temporary image
  166. coeffs, filterLength := createWeights8(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
  167. wg.Add(cpus)
  168. for i := 0; i < cpus; i++ {
  169. slice := makeSlice(temp, i, cpus).(*image.Gray)
  170. go func() {
  171. defer wg.Done()
  172. resizeGray(input, slice, scaleX, coeffs, filterLength)
  173. }()
  174. }
  175. wg.Wait()
  176. // horizontal filter on transposed image, result is not transposed
  177. coeffs, filterLength = createWeights8(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  178. wg.Add(cpus)
  179. for i := 0; i < cpus; i++ {
  180. slice := makeSlice(result, i, cpus).(*image.Gray)
  181. go func() {
  182. defer wg.Done()
  183. resizeGray(temp, slice, scaleY, coeffs, filterLength)
  184. }()
  185. }
  186. wg.Wait()
  187. return result
  188. case *image.Gray16:
  189. // 16-bit precision
  190. temp := image.NewGray16(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  191. result := image.NewGray16(image.Rect(0, 0, int(width), int(height)))
  192. // horizontal filter, results in transposed temporary image
  193. coeffs, filterLength := createWeights16(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
  194. wg.Add(cpus)
  195. for i := 0; i < cpus; i++ {
  196. slice := makeSlice(temp, i, cpus).(*image.Gray16)
  197. go func() {
  198. defer wg.Done()
  199. resizeGray16(input, slice, scaleX, coeffs, filterLength)
  200. }()
  201. }
  202. wg.Wait()
  203. // horizontal filter on transposed image, result is not transposed
  204. coeffs, filterLength = createWeights16(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  205. wg.Add(cpus)
  206. for i := 0; i < cpus; i++ {
  207. slice := makeSlice(result, i, cpus).(*image.Gray16)
  208. go func() {
  209. defer wg.Done()
  210. resizeGray16(temp, slice, scaleY, coeffs, filterLength)
  211. }()
  212. }
  213. wg.Wait()
  214. return result
  215. default:
  216. // 16-bit precision
  217. temp := image.NewRGBA64(image.Rect(0, 0, img.Bounds().Dy(), int(width)))
  218. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  219. // horizontal filter, results in transposed temporary image
  220. coeffs, filterLength := createWeights16(temp.Bounds().Dy(), img.Bounds().Min.X, taps, blur, scaleX, kernel)
  221. wg.Add(cpus)
  222. for i := 0; i < cpus; i++ {
  223. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  224. go func() {
  225. defer wg.Done()
  226. resizeGeneric(img, slice, scaleX, coeffs, filterLength)
  227. }()
  228. }
  229. wg.Wait()
  230. // horizontal filter on transposed image, result is not transposed
  231. coeffs, filterLength = createWeights16(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  232. wg.Add(cpus)
  233. for i := 0; i < cpus; i++ {
  234. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  235. go func() {
  236. defer wg.Done()
  237. resizeRGBA64(temp, slice, scaleY, coeffs, filterLength)
  238. }()
  239. }
  240. wg.Wait()
  241. return result
  242. }
  243. }
  244. // Calculates scaling factors using old and new image dimensions.
  245. func calcFactors(width, height uint, oldWidth, oldHeight float64) (scaleX, scaleY float64) {
  246. if width == 0 {
  247. if height == 0 {
  248. scaleX = 1.0
  249. scaleY = 1.0
  250. } else {
  251. scaleY = oldHeight / float64(height)
  252. scaleX = scaleY
  253. }
  254. } else {
  255. scaleX = oldWidth / float64(width)
  256. if height == 0 {
  257. scaleY = scaleX
  258. } else {
  259. scaleY = oldHeight / float64(height)
  260. }
  261. }
  262. return
  263. }
  264. type imageWithSubImage interface {
  265. image.Image
  266. SubImage(image.Rectangle) image.Image
  267. }
  268. func makeSlice(img imageWithSubImage, i, n int) image.Image {
  269. return img.SubImage(image.Rect(img.Bounds().Min.X, img.Bounds().Min.Y+i*img.Bounds().Dy()/n, img.Bounds().Max.X, img.Bounds().Min.Y+(i+1)*img.Bounds().Dy()/n))
  270. }