filters.go 6.9 KB

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