filters.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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
  15. import (
  16. "image"
  17. "image/color"
  18. "math"
  19. )
  20. // restrict an input float32 to the range of uint16 values
  21. func clampToUint16(x float32) (y uint16) {
  22. y = uint16(x)
  23. if x < 0 {
  24. y = 0
  25. } else if x > float32(0xfffe) {
  26. // "else if x > float32(0xffff)" will cause overflows!
  27. y = 0xffff
  28. }
  29. return
  30. }
  31. // describe a resampling filter
  32. type filterModel struct {
  33. // resampling is done by convolution with a (scaled) kernel
  34. kernel func(float32) float32
  35. // instead of blurring an image before downscaling to avoid aliasing,
  36. // the filter is scaled by a factor which leads to a similar effect
  37. factor float32
  38. // for optimized access to image points
  39. converter
  40. // temporary used by Interpolate
  41. tempRow []colorArray
  42. }
  43. func (f *filterModel) convolution1d(x float32, p []colorArray, factor float32) (c colorArray) {
  44. var k float32
  45. var sum float32 = 0
  46. for j := range p {
  47. k = f.kernel((x - float32(j)) / factor)
  48. sum += k
  49. for i := range c {
  50. c[i] += p[j][i] * k
  51. }
  52. }
  53. // normalize values
  54. for i := range c {
  55. c[i] = c[i] / sum
  56. }
  57. return
  58. }
  59. func (f *filterModel) Interpolate(u float32, y int) color.RGBA64 {
  60. uf := int(u) - len(f.tempRow)/2 + 1
  61. u -= float32(uf)
  62. for i := range f.tempRow {
  63. f.at(uf+i, y, &f.tempRow[i])
  64. }
  65. c := f.convolution1d(u, f.tempRow, f.factor)
  66. return color.RGBA64{
  67. clampToUint16(c[0]),
  68. clampToUint16(c[1]),
  69. clampToUint16(c[2]),
  70. clampToUint16(c[3]),
  71. }
  72. }
  73. // createFilter tries to find an optimized converter for the given input image
  74. // and initializes all filterModel members to their defaults
  75. func createFilter(img image.Image, factor float32, size int, kernel func(float32) float32) (f Filter) {
  76. sizeX := size * (int(math.Ceil(float64(factor))))
  77. switch img.(type) {
  78. default:
  79. f = &filterModel{
  80. kernel, factor,
  81. &genericConverter{img},
  82. make([]colorArray, sizeX),
  83. }
  84. case *image.RGBA:
  85. f = &filterModel{
  86. kernel, factor,
  87. &rgbaConverter{img.(*image.RGBA)},
  88. make([]colorArray, sizeX),
  89. }
  90. case *image.RGBA64:
  91. f = &filterModel{
  92. kernel, factor,
  93. &rgba64Converter{img.(*image.RGBA64)},
  94. make([]colorArray, sizeX),
  95. }
  96. case *image.Gray:
  97. f = &filterModel{
  98. kernel, factor,
  99. &grayConverter{img.(*image.Gray)},
  100. make([]colorArray, sizeX),
  101. }
  102. case *image.Gray16:
  103. f = &filterModel{
  104. kernel, factor,
  105. &gray16Converter{img.(*image.Gray16)},
  106. make([]colorArray, sizeX),
  107. }
  108. case *image.YCbCr:
  109. f = &filterModel{
  110. kernel, factor,
  111. &ycbcrConverter{img.(*image.YCbCr)},
  112. make([]colorArray, sizeX),
  113. }
  114. }
  115. return
  116. }
  117. // Return a filter kernel that performs nearly identically to the provided
  118. // kernel, but generates and uses a precomputed table rather than executing
  119. // the kernel for each evaluation. The table is generated with tableSize
  120. // values that cover the kernal domain from -maxX to +maxX. The input kernel
  121. // is assumed to be symmetrical around 0, so the table only includes values
  122. // from 0 to maxX.
  123. func tableKernel(kernel func(float32) float32, tableSize int,
  124. maxX float32) func(float32) float32 {
  125. // precompute an array of filter coefficients
  126. weights := make([]float32, tableSize+1)
  127. for i := range weights {
  128. weights[i] = kernel(maxX * float32(i) / float32(tableSize))
  129. }
  130. weights[tableSize] = 0.0
  131. return func(x float32) float32 {
  132. if x < 0.0 {
  133. x = -x
  134. }
  135. indf := x / maxX * float32(tableSize)
  136. ind := int(indf)
  137. if ind >= tableSize {
  138. return 0.0
  139. }
  140. return weights[ind] + (weights[ind+1]-weights[ind])*(indf-float32(ind))
  141. }
  142. }
  143. // Nearest-neighbor interpolation
  144. func NearestNeighbor(img image.Image, factor float32) Filter {
  145. return createFilter(img, factor, 2, func(x float32) (y float32) {
  146. if x >= -0.5 && x < 0.5 {
  147. y = 1
  148. } else {
  149. y = 0
  150. }
  151. return
  152. })
  153. }
  154. // Bilinear interpolation
  155. func Bilinear(img image.Image, factor float32) Filter {
  156. return createFilter(img, factor, 2, func(x float32) (y float32) {
  157. absX := float32(math.Abs(float64(x)))
  158. if absX <= 1 {
  159. y = 1 - absX
  160. } else {
  161. y = 0
  162. }
  163. return
  164. })
  165. }
  166. // Bicubic interpolation (with cubic hermite spline)
  167. func Bicubic(img image.Image, factor float32) Filter {
  168. return createFilter(img, factor, 4, splineKernel(0, 0.5))
  169. }
  170. // Mitchell-Netravali interpolation
  171. func MitchellNetravali(img image.Image, factor float32) Filter {
  172. return createFilter(img, factor, 4, splineKernel(1.0/3.0, 1.0/3.0))
  173. }
  174. func splineKernel(B, C float32) func(float32) float32 {
  175. factorA := 2.0 - 1.5*B - C
  176. factorB := -3.0 + 2.0*B + C
  177. factorC := 1.0 - 1.0/3.0*B
  178. factorD := -B/6.0 - C
  179. factorE := B + 5.0*C
  180. factorF := -2.0*B - 8.0*C
  181. factorG := 4.0/3.0*B + 4.0*C
  182. return func(x float32) (y float32) {
  183. absX := float32(math.Abs(float64(x)))
  184. if absX <= 1 {
  185. y = absX*absX*(factorA*absX+factorB) + factorC
  186. } else if absX <= 2 {
  187. y = absX*(absX*(absX*factorD+factorE)+factorF) + factorG
  188. } else {
  189. y = 0
  190. }
  191. return
  192. }
  193. }
  194. func lanczosKernel(a uint) func(float32) float32 {
  195. return func(x float32) (y float32) {
  196. if x > -float32(a) && x < float32(a) {
  197. y = float32(Sinc(float64(x))) * float32(Sinc(float64(x/float32(a))))
  198. } else {
  199. y = 0
  200. }
  201. return
  202. }
  203. }
  204. const lanczosTableSize = 300
  205. // Lanczos interpolation (a=2)
  206. func Lanczos2(img image.Image, factor float32) Filter {
  207. return createFilter(img, factor, 4, lanczosKernel(2))
  208. }
  209. // Lanczos interpolation (a=2) using a look-up table
  210. // to speed up computation
  211. func Lanczos2Lut(img image.Image, factor float32) Filter {
  212. return createFilter(img, factor, 4,
  213. tableKernel(lanczosKernel(2), lanczosTableSize, 2.0))
  214. }
  215. // Lanczos interpolation (a=3)
  216. func Lanczos3(img image.Image, factor float32) Filter {
  217. return createFilter(img, factor, 6, lanczosKernel(3))
  218. }
  219. // Lanczos interpolation (a=3) using a look-up table
  220. // to speed up computation
  221. func Lanczos3Lut(img image.Image, factor float32) Filter {
  222. return createFilter(img, factor, 6,
  223. tableKernel(lanczosKernel(3), lanczosTableSize, 3.0))
  224. }