resize.go 20 KB

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