resize.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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.YCbCr:
  121. // 8-bit precision
  122. // accessing the YCbCr arrays in a tight loop is slow.
  123. // converting the image to ycc increases performance by 2x.
  124. temp := newYCC(image.Rect(0, 0, input.Bounds().Dy(), int(width)), input.SubsampleRatio)
  125. result := newYCC(image.Rect(0, 0, int(width), int(height)), input.SubsampleRatio)
  126. coeffs, offset, filterLength := createWeights8(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
  127. in := imageYCbCrToYCC(input)
  128. wg.Add(cpus)
  129. for i := 0; i < cpus; i++ {
  130. slice := makeSlice(temp, i, cpus).(*ycc)
  131. go func() {
  132. defer wg.Done()
  133. resizeYCbCr(in, slice, scaleX, coeffs, offset, filterLength)
  134. }()
  135. }
  136. wg.Wait()
  137. coeffs, offset, filterLength = createWeights8(result.Bounds().Dy(), taps, blur, scaleY, kernel)
  138. wg.Add(cpus)
  139. for i := 0; i < cpus; i++ {
  140. slice := makeSlice(result, i, cpus).(*ycc)
  141. go func() {
  142. defer wg.Done()
  143. resizeYCbCr(temp, slice, scaleY, coeffs, offset, filterLength)
  144. }()
  145. }
  146. wg.Wait()
  147. return result.YCbCr()
  148. case *image.RGBA64:
  149. // 16-bit precision
  150. temp := image.NewRGBA64(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  151. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  152. // horizontal filter, results in transposed temporary image
  153. coeffs, offset, filterLength := createWeights16(temp.Bounds().Dy(), taps, blur, scaleX, kernel)
  154. wg.Add(cpus)
  155. for i := 0; i < cpus; i++ {
  156. slice := makeSlice(temp, i, cpus).(*image.RGBA64)
  157. go func() {
  158. defer wg.Done()
  159. resizeRGBA64(input, slice, scaleX, coeffs, offset, filterLength)
  160. }()
  161. }
  162. wg.Wait()
  163. // horizontal filter on transposed image, result is not transposed
  164. coeffs, offset, filterLength = createWeights16(result.Bounds().Dy(), taps, blur, scaleY, kernel)
  165. wg.Add(cpus)
  166. for i := 0; i < cpus; i++ {
  167. slice := makeSlice(result, i, cpus).(*image.RGBA64)
  168. go func() {
  169. defer wg.Done()
  170. resizeGeneric(temp, slice, scaleY, coeffs, offset, filterLength)
  171. }()
  172. }
  173. wg.Wait()
  174. return result
  175. case *image.Gray:
  176. // 8-bit precision
  177. temp := image.NewGray(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  178. result := image.NewGray(image.Rect(0, 0, int(width), int(height)))
  179. // horizontal filter, results in transposed temporary image
  180. coeffs, offset, filterLength := createWeights8(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.Gray)
  184. go func() {
  185. defer wg.Done()
  186. resizeGray(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 = createWeights8(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.Gray)
  195. go func() {
  196. defer wg.Done()
  197. resizeGray(temp, slice, scaleY, coeffs, offset, filterLength)
  198. }()
  199. }
  200. wg.Wait()
  201. return result
  202. case *image.Gray16:
  203. // 16-bit precision
  204. temp := image.NewGray16(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  205. result := image.NewGray16(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.Gray16)
  211. go func() {
  212. defer wg.Done()
  213. resizeGray16(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.Gray16)
  222. go func() {
  223. defer wg.Done()
  224. resizeGray16(temp, slice, scaleY, coeffs, offset, filterLength)
  225. }()
  226. }
  227. wg.Wait()
  228. return result
  229. default:
  230. // 16-bit precision
  231. temp := image.NewRGBA64(image.Rect(0, 0, img.Bounds().Dy(), int(width)))
  232. result := image.NewRGBA64(image.Rect(0, 0, int(width), int(height)))
  233. // horizontal filter, results in transposed temporary image
  234. coeffs, offset, filterLength := createWeights16(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.RGBA64)
  238. go func() {
  239. defer wg.Done()
  240. resizeGeneric(img, slice, scaleX, coeffs, offset, filterLength)
  241. }()
  242. }
  243. wg.Wait()
  244. // horizontal filter on transposed image, result is not transposed
  245. coeffs, offset, filterLength = createWeights16(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.RGBA64)
  249. go func() {
  250. defer wg.Done()
  251. resizeRGBA64(temp, slice, scaleY, coeffs, offset, filterLength)
  252. }()
  253. }
  254. wg.Wait()
  255. return result
  256. }
  257. }
  258. func resizeNearest(width, height uint, scaleX, scaleY float64, img image.Image, interp InterpolationFunction) image.Image {
  259. taps, _ := interp.kernel()
  260. cpus := runtime.GOMAXPROCS(0)
  261. wg := sync.WaitGroup{}
  262. switch input := img.(type) {
  263. case *image.RGBA:
  264. // 8-bit precision
  265. temp := image.NewRGBA(image.Rect(0, 0, input.Bounds().Dy(), int(width)))
  266. result := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
  267. // horizontal filter, results in transposed temporary image
  268. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
  269. wg.Add(cpus)
  270. for i := 0; i < cpus; i++ {
  271. slice := makeSlice(temp, i, cpus).(*image.RGBA)
  272. go func() {
  273. defer wg.Done()
  274. nearestRGBA(input, slice, scaleX, coeffs, offset, filterLength)
  275. }()
  276. }
  277. wg.Wait()
  278. // horizontal filter on transposed image, result is not transposed
  279. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
  280. wg.Add(cpus)
  281. for i := 0; i < cpus; i++ {
  282. slice := makeSlice(result, i, cpus).(*image.RGBA)
  283. go func() {
  284. defer wg.Done()
  285. nearestRGBA(temp, slice, scaleY, coeffs, offset, filterLength)
  286. }()
  287. }
  288. wg.Wait()
  289. return result
  290. case *image.YCbCr:
  291. // 8-bit precision
  292. // accessing the YCbCr arrays in a tight loop is slow.
  293. // converting the image to ycc increases performance by 2x.
  294. temp := newYCC(image.Rect(0, 0, input.Bounds().Dy(), int(width)), input.SubsampleRatio)
  295. result := newYCC(image.Rect(0, 0, int(width), int(height)), input.SubsampleRatio)
  296. coeffs, offset, filterLength := createWeightsNearest(temp.Bounds().Dy(), taps, blur, scaleX)
  297. in := imageYCbCrToYCC(input)
  298. wg.Add(cpus)
  299. for i := 0; i < cpus; i++ {
  300. slice := makeSlice(temp, i, cpus).(*ycc)
  301. go func() {
  302. defer wg.Done()
  303. nearestYCbCr(in, slice, scaleX, coeffs, offset, filterLength)
  304. }()
  305. }
  306. wg.Wait()
  307. coeffs, offset, filterLength = createWeightsNearest(result.Bounds().Dy(), taps, blur, scaleY)
  308. wg.Add(cpus)
  309. for i := 0; i < cpus; i++ {
  310. slice := makeSlice(result, i, cpus).(*ycc)
  311. go func() {
  312. defer wg.Done()
  313. nearestYCbCr(temp, slice, scaleY, coeffs, offset, filterLength)
  314. }()
  315. }
  316. wg.Wait()
  317. return result.YCbCr()
  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(), 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(), 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(), 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(), 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(), 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(), 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(), 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(), 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. }