resize.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 int
  32. // InterpolationFunction constants
  33. const (
  34. // Nearest-neighbor interpolation
  35. NearestNeighbor InterpolationFunction = iota
  36. // Bilinear interpolation
  37. Bilinear
  38. // Bicubic interpolation (with cubic hermite spline)
  39. Bicubic
  40. // Mitchell-Netravali interpolation
  41. MitchellNetravali
  42. // Lanczos interpolation (a=2)
  43. Lanczos2
  44. // Lanczos interpolation (a=3)
  45. Lanczos3
  46. )
  47. // kernal, returns an InterpolationFunctions taps and kernel.
  48. func (i InterpolationFunction) kernel() (int, func(float64) float64) {
  49. switch i {
  50. case Bilinear:
  51. return 2, linear
  52. case Bicubic:
  53. return 4, cubic
  54. case MitchellNetravali:
  55. return 4, mitchellnetravali
  56. case Lanczos2:
  57. return 4, lanczos2
  58. case Lanczos3:
  59. return 6, lanczos3
  60. default:
  61. // Default to NearestNeighbor.
  62. return 2, nearest
  63. }
  64. }
  65. // values <1 will sharpen the image
  66. var blur = 1.0
  67. // Resize scales an image to new width and height using the interpolation function interp.
  68. // A new image with the given dimensions will be returned.
  69. // If one of the parameters width or height is set to 0, its size will be calculated so that
  70. // the aspect ratio is that of the originating image.
  71. // The resizing algorithm uses channels for parallel computation.
  72. func Resize(width, height uint, img image.Image, interp InterpolationFunction) image.Image {
  73. scaleX, scaleY := calcFactors(width, height, float64(img.Bounds().Dx()), float64(img.Bounds().Dy()))
  74. if width == 0 {
  75. width = uint(0.7 + float64(img.Bounds().Dx())/scaleX)
  76. }
  77. if height == 0 {
  78. height = uint(0.7 + float64(img.Bounds().Dy())/scaleY)
  79. }
  80. if interp == NearestNeighbor {
  81. return resizeNearest(width, height, scaleX, scaleY, img, interp)
  82. }
  83. taps, kernel := interp.kernel()
  84. cpus := runtime.NumCPU()
  85. wg := sync.WaitGroup{}
  86. // Generic access to image.Image is slow in tight loops.
  87. // The optimal access has to be determined from the concrete image type.
  88. switch input := img.(type) {
  89. case *image.RGBA:
  90. // 8-bit precision
  91. temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  92. result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
  93. // horizontal filter, results in transposed temporary image
  94. coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
  95. wg.Add(cpus)
  96. for i := 0; i < cpus; i++ {
  97. slice := makeSlice(temp, i, cpus).(*image.RGBA)
  98. go func() {
  99. defer wg.Done()
  100. resizeRGBA(input, slice, scaleX, coeffs, offset, filterLength)
  101. }()
  102. }
  103. wg.Wait()
  104. // horizontal filter on transposed image, result is not transposed
  105. coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  106. wg.Add(cpus)
  107. for i := 0; i < cpus; i++ {
  108. slice := makeSlice(result, i, cpus).(*image.RGBA)
  109. go func() {
  110. defer wg.Done()
  111. resizeRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
  112. }()
  113. }
  114. wg.Wait()
  115. return result
  116. case *image.YCbCr:
  117. // 8-bit precision
  118. // accessing the YCbCr arrays in a tight loop is slow.
  119. // converting the image to ycc increases performance by 2x.
  120. temp := newYCC(image.Rect(0, 0, input.Bounds().Dy(), int(width)), input.SubsampleRatio)
  121. result := newYCC(image.Rect(0, 0, int(width), int(height)), input.SubsampleRatio)
  122. coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
  123. in := imageYCbCrToYCC(input)
  124. wg.Add(cpus)
  125. for i := 0; i < cpus; i++ {
  126. slice := makeSlice(temp, i, cpus).(*ycc)
  127. go func() {
  128. defer wg.Done()
  129. resizeYCbCr(in, slice, scaleX, coeffs, offset, filterLength)
  130. }()
  131. }
  132. wg.Wait()
  133. coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  134. wg.Add(cpus)
  135. for i := 0; i < cpus; i++ {
  136. slice := makeSlice(result, i, cpus).(*ycc)
  137. go func() {
  138. defer wg.Done()
  139. resizeYCbCr(temp, slice, scaleY, coeffs, offset, filterLength)
  140. }()
  141. }
  142. wg.Wait()
  143. return result.YCbCr()
  144. case *image.RGBA64:
  145. // 16-bit precision
  146. temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  147. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  148. // horizontal filter, results in transposed temporary image
  149. coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
  150. wg.Add(cpus)
  151. for i := 0; i < cpus; i++ {
  152. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  153. go func() {
  154. defer wg.Done()
  155. resizeRGBA64(input, slice, scaleX, coeffs, offset, filterLength)
  156. }()
  157. }
  158. wg.Wait()
  159. // horizontal filter on transposed image, result is not transposed
  160. coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  161. wg.Add(cpus)
  162. for i := 0; i < cpus; i++ {
  163. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  164. go func() {
  165. defer wg.Done()
  166. resizeGeneric(temp, slice, scaleY, coeffs, offset, filterLength)
  167. }()
  168. }
  169. wg.Wait()
  170. return result
  171. case *image.Gray:
  172. // 8-bit precision
  173. temp := image.NewGray(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  174. result := image.NewGray(image.Rect(0, 0, int(width), int(height)))
  175. // horizontal filter, results in transposed temporary image
  176. coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
  177. wg.Add(cpus)
  178. for i := 0; i < cpus; i++ {
  179. slice := makeSlice(temp, i, cpus).(*image.Gray)
  180. go func() {
  181. defer wg.Done()
  182. resizeGray(input, slice, scaleX, coeffs, offset, filterLength)
  183. }()
  184. }
  185. wg.Wait()
  186. // horizontal filter on transposed image, result is not transposed
  187. coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  188. wg.Add(cpus)
  189. for i := 0; i < cpus; i++ {
  190. slice := makeSlice(result, i, cpus).(*image.Gray)
  191. go func() {
  192. defer wg.Done()
  193. resizeGray(temp, slice, scaleY, coeffs, offset, filterLength)
  194. }()
  195. }
  196. wg.Wait()
  197. return result
  198. case *image.Gray16:
  199. // 16-bit precision
  200. temp := image.NewGray16(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  201. result := image.NewGray16(image.Rect(0, 0, int(width), int(height)))
  202. // horizontal filter, results in transposed temporary image
  203. coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
  204. wg.Add(cpus)
  205. for i := 0; i < cpus; i++ {
  206. slice := makeSlice(temp, i, cpus).(*image.Gray16)
  207. go func() {
  208. defer wg.Done()
  209. resizeGray16(input, slice, scaleX, coeffs, offset, filterLength)
  210. }()
  211. }
  212. wg.Wait()
  213. // horizontal filter on transposed image, result is not transposed
  214. coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  215. wg.Add(cpus)
  216. for i := 0; i < cpus; i++ {
  217. slice := makeSlice(result, i, cpus).(*image.Gray16)
  218. go func() {
  219. defer wg.Done()
  220. resizeGray16(temp, slice, scaleY, coeffs, offset, filterLength)
  221. }()
  222. }
  223. wg.Wait()
  224. return result
  225. default:
  226. // 16-bit precision
  227. temp := image.NewRGBA64(image.Rect(0, 0, img.Bounds().Dy(), int(width)))
  228. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  229. // horizontal filter, results in transposed temporary image
  230. coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), img.Bounds().Min.X, taps, blur, scaleX, kernel)
  231. wg.Add(cpus)
  232. for i := 0; i < cpus; i++ {
  233. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  234. go func() {
  235. defer wg.Done()
  236. resizeGeneric(img, slice, scaleX, coeffs, offset, filterLength)
  237. }()
  238. }
  239. wg.Wait()
  240. // horizontal filter on transposed image, result is not transposed
  241. coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  242. wg.Add(cpus)
  243. for i := 0; i < cpus; i++ {
  244. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  245. go func() {
  246. defer wg.Done()
  247. resizeRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
  248. }()
  249. }
  250. wg.Wait()
  251. return result
  252. }
  253. }
  254. func resizeNearest(width, height uint, scaleX, scaleY float64, img image.Image, interp InterpolationFunction) image.Image {
  255. taps, _ := interp.kernel()
  256. cpus := runtime.NumCPU()
  257. wg := sync.WaitGroup{}
  258. switch input := img.(type) {
  259. case *image.RGBA:
  260. // 8-bit precision
  261. temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  262. result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
  263. // horizontal filter, results in transposed temporary image
  264. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
  265. wg.Add(cpus)
  266. for i := 0; i < cpus; i++ {
  267. slice := makeSlice(temp, i, cpus).(*image.RGBA)
  268. go func() {
  269. defer wg.Done()
  270. nearestRGBA(input, slice, scaleX, coeffs, offset, filterLength)
  271. }()
  272. }
  273. wg.Wait()
  274. // horizontal filter on transposed image, result is not transposed
  275. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
  276. wg.Add(cpus)
  277. for i := 0; i < cpus; i++ {
  278. slice := makeSlice(result, i, cpus).(*image.RGBA)
  279. go func() {
  280. defer wg.Done()
  281. nearestRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
  282. }()
  283. }
  284. wg.Wait()
  285. return result
  286. case *image.YCbCr:
  287. // 8-bit precision
  288. // accessing the YCbCr arrays in a tight loop is slow.
  289. // converting the image to ycc increases performance by 2x.
  290. temp := newYCC(image.Rect(0, 0, input.Bounds().Dy(), int(width)), input.SubsampleRatio)
  291. result := newYCC(image.Rect(0, 0, int(width), int(height)), input.SubsampleRatio)
  292. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
  293. in := imageYCbCrToYCC(input)
  294. wg.Add(cpus)
  295. for i := 0; i < cpus; i++ {
  296. slice := makeSlice(temp, i, cpus).(*ycc)
  297. go func() {
  298. defer wg.Done()
  299. nearestYCbCr(in, slice, scaleX, coeffs, offset, filterLength)
  300. }()
  301. }
  302. wg.Wait()
  303. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
  304. wg.Add(cpus)
  305. for i := 0; i < cpus; i++ {
  306. slice := makeSlice(result, i, cpus).(*ycc)
  307. go func() {
  308. defer wg.Done()
  309. nearestYCbCr(temp, slice, scaleY, coeffs, offset, filterLength)
  310. }()
  311. }
  312. wg.Wait()
  313. return result.YCbCr()
  314. case *image.RGBA64:
  315. // 16-bit precision
  316. temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  317. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  318. // horizontal filter, results in transposed temporary image
  319. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
  320. wg.Add(cpus)
  321. for i := 0; i < cpus; i++ {
  322. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  323. go func() {
  324. defer wg.Done()
  325. nearestRGBA64(input, slice, scaleX, coeffs, offset, filterLength)
  326. }()
  327. }
  328. wg.Wait()
  329. // horizontal filter on transposed image, result is not transposed
  330. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
  331. wg.Add(cpus)
  332. for i := 0; i < cpus; i++ {
  333. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  334. go func() {
  335. defer wg.Done()
  336. nearestGeneric(temp, slice, scaleY, coeffs, offset, filterLength)
  337. }()
  338. }
  339. wg.Wait()
  340. return result
  341. case *image.Gray:
  342. // 8-bit precision
  343. temp := image.NewGray(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  344. result := image.NewGray(image.Rect(0, 0, int(width), int(height)))
  345. // horizontal filter, results in transposed temporary image
  346. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
  347. wg.Add(cpus)
  348. for i := 0; i < cpus; i++ {
  349. slice := makeSlice(temp, i, cpus).(*image.Gray)
  350. go func() {
  351. defer wg.Done()
  352. nearestGray(input, slice, scaleX, coeffs, offset, filterLength)
  353. }()
  354. }
  355. wg.Wait()
  356. // horizontal filter on transposed image, result is not transposed
  357. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
  358. wg.Add(cpus)
  359. for i := 0; i < cpus; i++ {
  360. slice := makeSlice(result, i, cpus).(*image.Gray)
  361. go func() {
  362. defer wg.Done()
  363. nearestGray(temp, slice, scaleY, coeffs, offset, filterLength)
  364. }()
  365. }
  366. wg.Wait()
  367. return result
  368. case *image.Gray16:
  369. // 16-bit precision
  370. temp := image.NewGray16(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  371. result := image.NewGray16(image.Rect(0, 0, int(width), int(height)))
  372. // horizontal filter, results in transposed temporary image
  373. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
  374. wg.Add(cpus)
  375. for i := 0; i < cpus; i++ {
  376. slice := makeSlice(temp, i, cpus).(*image.Gray16)
  377. go func() {
  378. defer wg.Done()
  379. nearestGray16(input, slice, scaleX, coeffs, offset, filterLength)
  380. }()
  381. }
  382. wg.Wait()
  383. // horizontal filter on transposed image, result is not transposed
  384. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
  385. wg.Add(cpus)
  386. for i := 0; i < cpus; i++ {
  387. slice := makeSlice(result, i, cpus).(*image.Gray16)
  388. go func() {
  389. defer wg.Done()
  390. nearestGray16(temp, slice, scaleY, coeffs, offset, filterLength)
  391. }()
  392. }
  393. wg.Wait()
  394. return result
  395. default:
  396. // 16-bit precision
  397. temp := image.NewRGBA64(image.Rect(0, 0, img.Bounds().Dy(), int(width)))
  398. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  399. // horizontal filter, results in transposed temporary image
  400. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), img.Bounds().Min.X, taps, blur, scaleX)
  401. wg.Add(cpus)
  402. for i := 0; i < cpus; i++ {
  403. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  404. go func() {
  405. defer wg.Done()
  406. nearestGeneric(img, slice, scaleX, coeffs, offset, filterLength)
  407. }()
  408. }
  409. wg.Wait()
  410. // horizontal filter on transposed image, result is not transposed
  411. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
  412. wg.Add(cpus)
  413. for i := 0; i < cpus; i++ {
  414. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  415. go func() {
  416. defer wg.Done()
  417. nearestRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
  418. }()
  419. }
  420. wg.Wait()
  421. return result
  422. }
  423. }
  424. // Calculates scaling factors using old and new image dimensions.
  425. func calcFactors(width, height uint, oldWidth, oldHeight float64) (scaleX, scaleY float64) {
  426. if width == 0 {
  427. if height == 0 {
  428. scaleX = 1.0
  429. scaleY = 1.0
  430. } else {
  431. scaleY = oldHeight / float64(height)
  432. scaleX = scaleY
  433. }
  434. } else {
  435. scaleX = oldWidth / float64(width)
  436. if height == 0 {
  437. scaleY = scaleX
  438. } else {
  439. scaleY = oldHeight / float64(height)
  440. }
  441. }
  442. return
  443. }
  444. type imageWithSubImage interface {
  445. image.Image
  446. SubImage(image.Rectangle) image.Image
  447. }
  448. func makeSlice(img imageWithSubImage, i, n int) image.Image {
  449. 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))
  450. }