You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

614 lines
20 KiB

  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. // Trivial case: return input image
  81. if int(width) == img.Bounds().Dx() && int(height) == img.Bounds().Dy() {
  82. return img
  83. }
  84. if interp == NearestNeighbor {
  85. return resizeNearest(width, height, scaleX, scaleY, img, interp)
  86. }
  87. taps, kernel := interp.kernel()
  88. cpus := runtime.GOMAXPROCS(0)
  89. wg := sync.WaitGroup{}
  90. // Generic access to image.Image is slow in tight loops.
  91. // The optimal access has to be determined from the concrete image type.
  92. switch input := img.(type) {
  93. case *image.RGBA:
  94. // 8-bit precision
  95. temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  96. result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
  97. // horizontal filter, results in transposed temporary image
  98. coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
  99. wg.Add(cpus)
  100. for i := 0; i < cpus; i++ {
  101. slice := makeSlice(temp, i, cpus).(*image.RGBA)
  102. go func() {
  103. defer wg.Done()
  104. resizeRGBA(input, slice, scaleX, coeffs, offset, filterLength)
  105. }()
  106. }
  107. wg.Wait()
  108. // horizontal filter on transposed image, result is not transposed
  109. coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), taps, blur, scaleY, kernel)
  110. wg.Add(cpus)
  111. for i := 0; i < cpus; i++ {
  112. slice := makeSlice(result, i, cpus).(*image.RGBA)
  113. go func() {
  114. defer wg.Done()
  115. resizeRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
  116. }()
  117. }
  118. wg.Wait()
  119. return result
  120. case *image.NRGBA:
  121. // 8-bit precision
  122. temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  123. result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
  124. // horizontal filter, results in transposed temporary image
  125. coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
  126. wg.Add(cpus)
  127. for i := 0; i < cpus; i++ {
  128. slice := makeSlice(temp, i, cpus).(*image.RGBA)
  129. go func() {
  130. defer wg.Done()
  131. resizeNRGBA(input, slice, scaleX, coeffs, offset, filterLength)
  132. }()
  133. }
  134. wg.Wait()
  135. // horizontal filter on transposed image, result is not transposed
  136. coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), taps, blur, scaleY, kernel)
  137. wg.Add(cpus)
  138. for i := 0; i < cpus; i++ {
  139. slice := makeSlice(result, i, cpus).(*image.RGBA)
  140. go func() {
  141. defer wg.Done()
  142. resizeRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
  143. }()
  144. }
  145. wg.Wait()
  146. return result
  147. case *image.YCbCr:
  148. // 8-bit precision
  149. // accessing the YCbCr arrays in a tight loop is slow.
  150. // converting the image to ycc increases performance by 2x.
  151. temp := newYCC(image.Rect(0, 0, input.Bounds().Dy(), int(width)), input.SubsampleRatio)
  152. result := newYCC(image.Rect(0, 0, int(width), int(height)), image.YCbCrSubsampleRatio444)
  153. coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
  154. in := imageYCbCrToYCC(input)
  155. wg.Add(cpus)
  156. for i := 0; i < cpus; i++ {
  157. slice := makeSlice(temp, i, cpus).(*ycc)
  158. go func() {
  159. defer wg.Done()
  160. resizeYCbCr(in, slice, scaleX, coeffs, offset, filterLength)
  161. }()
  162. }
  163. wg.Wait()
  164. coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), taps, blur, scaleY, kernel)
  165. wg.Add(cpus)
  166. for i := 0; i < cpus; i++ {
  167. slice := makeSlice(result, i, cpus).(*ycc)
  168. go func() {
  169. defer wg.Done()
  170. resizeYCbCr(temp, slice, scaleY, coeffs, offset, filterLength)
  171. }()
  172. }
  173. wg.Wait()
  174. return result.YCbCr()
  175. case *image.RGBA64:
  176. // 16-bit precision
  177. temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  178. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  179. // horizontal filter, results in transposed temporary image
  180. coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
  181. wg.Add(cpus)
  182. for i := 0; i < cpus; i++ {
  183. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  184. go func() {
  185. defer wg.Done()
  186. resizeRGBA64(input, slice, scaleX, coeffs, offset, filterLength)
  187. }()
  188. }
  189. wg.Wait()
  190. // horizontal filter on transposed image, result is not transposed
  191. coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), taps, blur, scaleY, kernel)
  192. wg.Add(cpus)
  193. for i := 0; i < cpus; i++ {
  194. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  195. go func() {
  196. defer wg.Done()
  197. resizeRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
  198. }()
  199. }
  200. wg.Wait()
  201. return result
  202. case *image.NRGBA64:
  203. // 16-bit precision
  204. temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  205. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  206. // horizontal filter, results in transposed temporary image
  207. coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
  208. wg.Add(cpus)
  209. for i := 0; i < cpus; i++ {
  210. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  211. go func() {
  212. defer wg.Done()
  213. resizeNRGBA64(input, slice, scaleX, coeffs, offset, filterLength)
  214. }()
  215. }
  216. wg.Wait()
  217. // horizontal filter on transposed image, result is not transposed
  218. coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), taps, blur, scaleY, kernel)
  219. wg.Add(cpus)
  220. for i := 0; i < cpus; i++ {
  221. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  222. go func() {
  223. defer wg.Done()
  224. resizeRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
  225. }()
  226. }
  227. wg.Wait()
  228. return result
  229. case *image.Gray:
  230. // 8-bit precision
  231. temp := image.NewGray(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  232. result := image.NewGray(image.Rect(0, 0, int(width), int(height)))
  233. // horizontal filter, results in transposed temporary image
  234. coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
  235. wg.Add(cpus)
  236. for i := 0; i < cpus; i++ {
  237. slice := makeSlice(temp, i, cpus).(*image.Gray)
  238. go func() {
  239. defer wg.Done()
  240. resizeGray(input, slice, scaleX, coeffs, offset, filterLength)
  241. }()
  242. }
  243. wg.Wait()
  244. // horizontal filter on transposed image, result is not transposed
  245. coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), taps, blur, scaleY, kernel)
  246. wg.Add(cpus)
  247. for i := 0; i < cpus; i++ {
  248. slice := makeSlice(result, i, cpus).(*image.Gray)
  249. go func() {
  250. defer wg.Done()
  251. resizeGray(temp, slice, scaleY, coeffs, offset, filterLength)
  252. }()
  253. }
  254. wg.Wait()
  255. return result
  256. case *image.Gray16:
  257. // 16-bit precision
  258. temp := image.NewGray16(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  259. result := image.NewGray16(image.Rect(0, 0, int(width), int(height)))
  260. // horizontal filter, results in transposed temporary image
  261. coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
  262. wg.Add(cpus)
  263. for i := 0; i < cpus; i++ {
  264. slice := makeSlice(temp, i, cpus).(*image.Gray16)
  265. go func() {
  266. defer wg.Done()
  267. resizeGray16(input, slice, scaleX, coeffs, offset, filterLength)
  268. }()
  269. }
  270. wg.Wait()
  271. // horizontal filter on transposed image, result is not transposed
  272. coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), taps, blur, scaleY, kernel)
  273. wg.Add(cpus)
  274. for i := 0; i < cpus; i++ {
  275. slice := makeSlice(result, i, cpus).(*image.Gray16)
  276. go func() {
  277. defer wg.Done()
  278. resizeGray16(temp, slice, scaleY, coeffs, offset, filterLength)
  279. }()
  280. }
  281. wg.Wait()
  282. return result
  283. default:
  284. // 16-bit precision
  285. temp := image.NewRGBA64(image.Rect(0, 0, img.Bounds().Dy(), int(width)))
  286. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  287. // horizontal filter, results in transposed temporary image
  288. coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
  289. wg.Add(cpus)
  290. for i := 0; i < cpus; i++ {
  291. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  292. go func() {
  293. defer wg.Done()
  294. resizeGeneric(img, slice, scaleX, coeffs, offset, filterLength)
  295. }()
  296. }
  297. wg.Wait()
  298. // horizontal filter on transposed image, result is not transposed
  299. coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), taps, blur, scaleY, kernel)
  300. wg.Add(cpus)
  301. for i := 0; i < cpus; i++ {
  302. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  303. go func() {
  304. defer wg.Done()
  305. resizeRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
  306. }()
  307. }
  308. wg.Wait()
  309. return result
  310. }
  311. }
  312. func resizeNearest(width, height uint, scaleX, scaleY float64, img image.Image, interp InterpolationFunction) image.Image {
  313. taps, _ := interp.kernel()
  314. cpus := runtime.GOMAXPROCS(0)
  315. wg := sync.WaitGroup{}
  316. switch input := img.(type) {
  317. case *image.RGBA:
  318. // 8-bit precision
  319. temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  320. result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
  321. // horizontal filter, results in transposed temporary image
  322. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
  323. wg.Add(cpus)
  324. for i := 0; i < cpus; i++ {
  325. slice := makeSlice(temp, i, cpus).(*image.RGBA)
  326. go func() {
  327. defer wg.Done()
  328. nearestRGBA(input, slice, scaleX, coeffs, offset, filterLength)
  329. }()
  330. }
  331. wg.Wait()
  332. // horizontal filter on transposed image, result is not transposed
  333. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
  334. wg.Add(cpus)
  335. for i := 0; i < cpus; i++ {
  336. slice := makeSlice(result, i, cpus).(*image.RGBA)
  337. go func() {
  338. defer wg.Done()
  339. nearestRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
  340. }()
  341. }
  342. wg.Wait()
  343. return result
  344. case *image.NRGBA:
  345. // 8-bit precision
  346. temp := image.NewNRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  347. result := image.NewNRGBA(image.Rect(0, 0, int(width), int(height)))
  348. // horizontal filter, results in transposed temporary image
  349. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
  350. wg.Add(cpus)
  351. for i := 0; i < cpus; i++ {
  352. slice := makeSlice(temp, i, cpus).(*image.NRGBA)
  353. go func() {
  354. defer wg.Done()
  355. nearestNRGBA(input, slice, scaleX, coeffs, offset, filterLength)
  356. }()
  357. }
  358. wg.Wait()
  359. // horizontal filter on transposed image, result is not transposed
  360. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
  361. wg.Add(cpus)
  362. for i := 0; i < cpus; i++ {
  363. slice := makeSlice(result, i, cpus).(*image.NRGBA)
  364. go func() {
  365. defer wg.Done()
  366. nearestNRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
  367. }()
  368. }
  369. wg.Wait()
  370. return result
  371. case *image.YCbCr:
  372. // 8-bit precision
  373. // accessing the YCbCr arrays in a tight loop is slow.
  374. // converting the image to ycc increases performance by 2x.
  375. temp := newYCC(image.Rect(0, 0, input.Bounds().Dy(), int(width)), input.SubsampleRatio)
  376. result := newYCC(image.Rect(0, 0, int(width), int(height)), image.YCbCrSubsampleRatio444)
  377. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
  378. in := imageYCbCrToYCC(input)
  379. wg.Add(cpus)
  380. for i := 0; i < cpus; i++ {
  381. slice := makeSlice(temp, i, cpus).(*ycc)
  382. go func() {
  383. defer wg.Done()
  384. nearestYCbCr(in, slice, scaleX, coeffs, offset, filterLength)
  385. }()
  386. }
  387. wg.Wait()
  388. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
  389. wg.Add(cpus)
  390. for i := 0; i < cpus; i++ {
  391. slice := makeSlice(result, i, cpus).(*ycc)
  392. go func() {
  393. defer wg.Done()
  394. nearestYCbCr(temp, slice, scaleY, coeffs, offset, filterLength)
  395. }()
  396. }
  397. wg.Wait()
  398. return result.YCbCr()
  399. case *image.RGBA64:
  400. // 16-bit precision
  401. temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  402. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  403. // horizontal filter, results in transposed temporary image
  404. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
  405. wg.Add(cpus)
  406. for i := 0; i < cpus; i++ {
  407. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  408. go func() {
  409. defer wg.Done()
  410. nearestRGBA64(input, slice, scaleX, coeffs, offset, filterLength)
  411. }()
  412. }
  413. wg.Wait()
  414. // horizontal filter on transposed image, result is not transposed
  415. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
  416. wg.Add(cpus)
  417. for i := 0; i < cpus; i++ {
  418. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  419. go func() {
  420. defer wg.Done()
  421. nearestRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
  422. }()
  423. }
  424. wg.Wait()
  425. return result
  426. case *image.NRGBA64:
  427. // 16-bit precision
  428. temp := image.NewNRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  429. result := image.NewNRGBA64(image.Rect(0, 0, int(width), int(height)))
  430. // horizontal filter, results in transposed temporary image
  431. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
  432. wg.Add(cpus)
  433. for i := 0; i < cpus; i++ {
  434. slice := makeSlice(temp, i, cpus).(*image.NRGBA64)
  435. go func() {
  436. defer wg.Done()
  437. nearestNRGBA64(input, slice, scaleX, coeffs, offset, filterLength)
  438. }()
  439. }
  440. wg.Wait()
  441. // horizontal filter on transposed image, result is not transposed
  442. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
  443. wg.Add(cpus)
  444. for i := 0; i < cpus; i++ {
  445. slice := makeSlice(result, i, cpus).(*image.NRGBA64)
  446. go func() {
  447. defer wg.Done()
  448. nearestNRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
  449. }()
  450. }
  451. wg.Wait()
  452. return result
  453. case *image.Gray:
  454. // 8-bit precision
  455. temp := image.NewGray(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  456. result := image.NewGray(image.Rect(0, 0, int(width), int(height)))
  457. // horizontal filter, results in transposed temporary image
  458. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
  459. wg.Add(cpus)
  460. for i := 0; i < cpus; i++ {
  461. slice := makeSlice(temp, i, cpus).(*image.Gray)
  462. go func() {
  463. defer wg.Done()
  464. nearestGray(input, slice, scaleX, coeffs, offset, filterLength)
  465. }()
  466. }
  467. wg.Wait()
  468. // horizontal filter on transposed image, result is not transposed
  469. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
  470. wg.Add(cpus)
  471. for i := 0; i < cpus; i++ {
  472. slice := makeSlice(result, i, cpus).(*image.Gray)
  473. go func() {
  474. defer wg.Done()
  475. nearestGray(temp, slice, scaleY, coeffs, offset, filterLength)
  476. }()
  477. }
  478. wg.Wait()
  479. return result
  480. case *image.Gray16:
  481. // 16-bit precision
  482. temp := image.NewGray16(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  483. result := image.NewGray16(image.Rect(0, 0, int(width), int(height)))
  484. // horizontal filter, results in transposed temporary image
  485. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
  486. wg.Add(cpus)
  487. for i := 0; i < cpus; i++ {
  488. slice := makeSlice(temp, i, cpus).(*image.Gray16)
  489. go func() {
  490. defer wg.Done()
  491. nearestGray16(input, slice, scaleX, coeffs, offset, filterLength)
  492. }()
  493. }
  494. wg.Wait()
  495. // horizontal filter on transposed image, result is not transposed
  496. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
  497. wg.Add(cpus)
  498. for i := 0; i < cpus; i++ {
  499. slice := makeSlice(result, i, cpus).(*image.Gray16)
  500. go func() {
  501. defer wg.Done()
  502. nearestGray16(temp, slice, scaleY, coeffs, offset, filterLength)
  503. }()
  504. }
  505. wg.Wait()
  506. return result
  507. default:
  508. // 16-bit precision
  509. temp := image.NewRGBA64(image.Rect(0, 0, img.Bounds().Dy(), int(width)))
  510. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  511. // horizontal filter, results in transposed temporary image
  512. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
  513. wg.Add(cpus)
  514. for i := 0; i < cpus; i++ {
  515. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  516. go func() {
  517. defer wg.Done()
  518. nearestGeneric(img, slice, scaleX, coeffs, offset, filterLength)
  519. }()
  520. }
  521. wg.Wait()
  522. // horizontal filter on transposed image, result is not transposed
  523. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
  524. wg.Add(cpus)
  525. for i := 0; i < cpus; i++ {
  526. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  527. go func() {
  528. defer wg.Done()
  529. nearestRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
  530. }()
  531. }
  532. wg.Wait()
  533. return result
  534. }
  535. }
  536. // Calculates scaling factors using old and new image dimensions.
  537. func calcFactors(width, height uint, oldWidth, oldHeight float64) (scaleX, scaleY float64) {
  538. if width == 0 {
  539. if height == 0 {
  540. scaleX = 1.0
  541. scaleY = 1.0
  542. } else {
  543. scaleY = oldHeight / float64(height)
  544. scaleX = scaleY
  545. }
  546. } else {
  547. scaleX = oldWidth / float64(width)
  548. if height == 0 {
  549. scaleY = scaleX
  550. } else {
  551. scaleY = oldHeight / float64(height)
  552. }
  553. }
  554. return
  555. }
  556. type imageWithSubImage interface {
  557. image.Image
  558. SubImage(image.Rectangle) image.Image
  559. }
  560. func makeSlice(img imageWithSubImage, i, n int) image.Image {
  561. 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))
  562. }