Brak opisu

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. }