resize.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 before filtering will improve performance.
  120. inputAsRGBA := convertYCbCrToRGBA(input)
  121. temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  122. result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
  123. // horizontal filter, results in transposed temporary image
  124. coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
  125. wg.Add(cpus)
  126. for i := 0; i < cpus; i++ {
  127. slice := makeSlice(temp, i, cpus).(*image.RGBA)
  128. go func() {
  129. defer wg.Done()
  130. resizeRGBA(inputAsRGBA, slice, scaleX, coeffs, offset, filterLength)
  131. }()
  132. }
  133. wg.Wait()
  134. // horizontal filter on transposed image, result is not transposed
  135. coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  136. wg.Add(cpus)
  137. for i := 0; i < cpus; i++ {
  138. slice := makeSlice(result, i, cpus).(*image.RGBA)
  139. go func() {
  140. defer wg.Done()
  141. resizeRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
  142. }()
  143. }
  144. wg.Wait()
  145. return result
  146. case *image.RGBA64:
  147. // 16-bit precision
  148. temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  149. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  150. // horizontal filter, results in transposed temporary image
  151. coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
  152. wg.Add(cpus)
  153. for i := 0; i < cpus; i++ {
  154. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  155. go func() {
  156. defer wg.Done()
  157. resizeRGBA64(input, slice, scaleX, coeffs, offset, filterLength)
  158. }()
  159. }
  160. wg.Wait()
  161. // horizontal filter on transposed image, result is not transposed
  162. coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  163. wg.Add(cpus)
  164. for i := 0; i < cpus; i++ {
  165. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  166. go func() {
  167. defer wg.Done()
  168. resizeGeneric(temp, slice, scaleY, coeffs, offset, filterLength)
  169. }()
  170. }
  171. wg.Wait()
  172. return result
  173. case *image.Gray:
  174. // 8-bit precision
  175. temp := image.NewGray(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  176. result := image.NewGray(image.Rect(0, 0, int(width), int(height)))
  177. // horizontal filter, results in transposed temporary image
  178. coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
  179. wg.Add(cpus)
  180. for i := 0; i < cpus; i++ {
  181. slice := makeSlice(temp, i, cpus).(*image.Gray)
  182. go func() {
  183. defer wg.Done()
  184. resizeGray(input, slice, scaleX, coeffs, offset, filterLength)
  185. }()
  186. }
  187. wg.Wait()
  188. // horizontal filter on transposed image, result is not transposed
  189. coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  190. wg.Add(cpus)
  191. for i := 0; i < cpus; i++ {
  192. slice := makeSlice(result, i, cpus).(*image.Gray)
  193. go func() {
  194. defer wg.Done()
  195. resizeGray(temp, slice, scaleY, coeffs, offset, filterLength)
  196. }()
  197. }
  198. wg.Wait()
  199. return result
  200. case *image.Gray16:
  201. // 16-bit precision
  202. temp := image.NewGray16(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  203. result := image.NewGray16(image.Rect(0, 0, int(width), int(height)))
  204. // horizontal filter, results in transposed temporary image
  205. coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX, kernel)
  206. wg.Add(cpus)
  207. for i := 0; i < cpus; i++ {
  208. slice := makeSlice(temp, i, cpus).(*image.Gray16)
  209. go func() {
  210. defer wg.Done()
  211. resizeGray16(input, slice, scaleX, coeffs, offset, filterLength)
  212. }()
  213. }
  214. wg.Wait()
  215. // horizontal filter on transposed image, result is not transposed
  216. coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  217. wg.Add(cpus)
  218. for i := 0; i < cpus; i++ {
  219. slice := makeSlice(result, i, cpus).(*image.Gray16)
  220. go func() {
  221. defer wg.Done()
  222. resizeGray16(temp, slice, scaleY, coeffs, offset, filterLength)
  223. }()
  224. }
  225. wg.Wait()
  226. return result
  227. default:
  228. // 16-bit precision
  229. temp := image.NewRGBA64(image.Rect(0, 0, img.Bounds().Dy(), int(width)))
  230. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  231. // horizontal filter, results in transposed temporary image
  232. coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), img.Bounds().Min.X, taps, blur, scaleX, kernel)
  233. wg.Add(cpus)
  234. for i := 0; i < cpus; i++ {
  235. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  236. go func() {
  237. defer wg.Done()
  238. resizeGeneric(img, slice, scaleX, coeffs, offset, filterLength)
  239. }()
  240. }
  241. wg.Wait()
  242. // horizontal filter on transposed image, result is not transposed
  243. coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY, kernel)
  244. wg.Add(cpus)
  245. for i := 0; i < cpus; i++ {
  246. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  247. go func() {
  248. defer wg.Done()
  249. resizeRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
  250. }()
  251. }
  252. wg.Wait()
  253. return result
  254. }
  255. }
  256. func resizeNearest(width, height uint, scaleX, scaleY float64, img image.Image, interp InterpolationFunction) image.Image {
  257. taps, _ := interp.kernel()
  258. cpus := runtime.NumCPU()
  259. wg := sync.WaitGroup{}
  260. switch input := img.(type) {
  261. case *image.RGBA:
  262. // 8-bit precision
  263. temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  264. result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
  265. // horizontal filter, results in transposed temporary image
  266. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
  267. wg.Add(cpus)
  268. for i := 0; i < cpus; i++ {
  269. slice := makeSlice(temp, i, cpus).(*image.RGBA)
  270. go func() {
  271. defer wg.Done()
  272. nearestRGBA(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 = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
  278. wg.Add(cpus)
  279. for i := 0; i < cpus; i++ {
  280. slice := makeSlice(result, i, cpus).(*image.RGBA)
  281. go func() {
  282. defer wg.Done()
  283. nearestRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
  284. }()
  285. }
  286. wg.Wait()
  287. return result
  288. case *image.YCbCr:
  289. // 8-bit precision
  290. // accessing the YCbCr arrays in a tight loop is slow.
  291. // converting the image before filtering will improve performance.
  292. inputAsRGBA := convertYCbCrToRGBA(input)
  293. temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  294. result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
  295. // horizontal filter, results in transposed temporary image
  296. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
  297. wg.Add(cpus)
  298. for i := 0; i < cpus; i++ {
  299. slice := makeSlice(temp, i, cpus).(*image.RGBA)
  300. go func() {
  301. defer wg.Done()
  302. nearestRGBA(inputAsRGBA, slice, scaleX, coeffs, offset, filterLength)
  303. }()
  304. }
  305. wg.Wait()
  306. // horizontal filter on transposed image, result is not transposed
  307. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
  308. wg.Add(cpus)
  309. for i := 0; i < cpus; i++ {
  310. slice := makeSlice(result, i, cpus).(*image.RGBA)
  311. go func() {
  312. defer wg.Done()
  313. nearestRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
  314. }()
  315. }
  316. wg.Wait()
  317. return result
  318. case *image.RGBA64:
  319. // 16-bit precision
  320. temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  321. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  322. // horizontal filter, results in transposed temporary image
  323. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
  324. wg.Add(cpus)
  325. for i := 0; i < cpus; i++ {
  326. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  327. go func() {
  328. defer wg.Done()
  329. nearestRGBA64(input, slice, scaleX, coeffs, offset, filterLength)
  330. }()
  331. }
  332. wg.Wait()
  333. // horizontal filter on transposed image, result is not transposed
  334. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
  335. wg.Add(cpus)
  336. for i := 0; i < cpus; i++ {
  337. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  338. go func() {
  339. defer wg.Done()
  340. nearestGeneric(temp, slice, scaleY, coeffs, offset, filterLength)
  341. }()
  342. }
  343. wg.Wait()
  344. return result
  345. case *image.Gray:
  346. // 8-bit precision
  347. temp := image.NewGray(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  348. result := image.NewGray(image.Rect(0, 0, int(width), int(height)))
  349. // horizontal filter, results in transposed temporary image
  350. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
  351. wg.Add(cpus)
  352. for i := 0; i < cpus; i++ {
  353. slice := makeSlice(temp, i, cpus).(*image.Gray)
  354. go func() {
  355. defer wg.Done()
  356. nearestGray(input, slice, scaleX, coeffs, offset, filterLength)
  357. }()
  358. }
  359. wg.Wait()
  360. // horizontal filter on transposed image, result is not transposed
  361. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
  362. wg.Add(cpus)
  363. for i := 0; i < cpus; i++ {
  364. slice := makeSlice(result, i, cpus).(*image.Gray)
  365. go func() {
  366. defer wg.Done()
  367. nearestGray(temp, slice, scaleY, coeffs, offset, filterLength)
  368. }()
  369. }
  370. wg.Wait()
  371. return result
  372. case *image.Gray16:
  373. // 16-bit precision
  374. temp := image.NewGray16(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  375. result := image.NewGray16(image.Rect(0, 0, int(width), int(height)))
  376. // horizontal filter, results in transposed temporary image
  377. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), input.Bounds().Min.X, taps, blur, scaleX)
  378. wg.Add(cpus)
  379. for i := 0; i < cpus; i++ {
  380. slice := makeSlice(temp, i, cpus).(*image.Gray16)
  381. go func() {
  382. defer wg.Done()
  383. nearestGray16(input, slice, scaleX, coeffs, offset, filterLength)
  384. }()
  385. }
  386. wg.Wait()
  387. // horizontal filter on transposed image, result is not transposed
  388. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), temp.Bounds().Min.X, taps, blur, scaleY)
  389. wg.Add(cpus)
  390. for i := 0; i < cpus; i++ {
  391. slice := makeSlice(result, i, cpus).(*image.Gray16)
  392. go func() {
  393. defer wg.Done()
  394. nearestGray16(temp, slice, scaleY, coeffs, offset, filterLength)
  395. }()
  396. }
  397. wg.Wait()
  398. return result
  399. default:
  400. // 16-bit precision
  401. temp := image.NewRGBA64(image.Rect(0, 0, img.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(), img.Bounds().Min.X, 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. nearestGeneric(img, 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(), temp.Bounds().Min.X, 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. }
  427. }
  428. // Calculates scaling factors using old and new image dimensions.
  429. func calcFactors(width, height uint, oldWidth, oldHeight float64) (scaleX, scaleY float64) {
  430. if width == 0 {
  431. if height == 0 {
  432. scaleX = 1.0
  433. scaleY = 1.0
  434. } else {
  435. scaleY = oldHeight / float64(height)
  436. scaleX = scaleY
  437. }
  438. } else {
  439. scaleX = oldWidth / float64(width)
  440. if height == 0 {
  441. scaleY = scaleX
  442. } else {
  443. scaleY = oldHeight / float64(height)
  444. }
  445. }
  446. return
  447. }
  448. type imageWithSubImage interface {
  449. image.Image
  450. SubImage(image.Rectangle) image.Image
  451. }
  452. func makeSlice(img imageWithSubImage, i, n int) image.Image {
  453. 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))
  454. }