filters.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. // Nearest-neighbor interpolation
  124. func NearestNeighbor(img image.Image, factor [2]float32) Filter {
  125. return createFilter(img, factor, 2, func(x float32) (y float32) {
  126. if x >= -0.5 && x < 0.5 {
  127. y = 1
  128. } else {
  129. y = 0
  130. }
  131. return
  132. })
  133. }
  134. // Bilinear interpolation
  135. func Bilinear(img image.Image, factor [2]float32) Filter {
  136. return createFilter(img, factor, 2, func(x float32) (y float32) {
  137. absX := float32(math.Abs(float64(x)))
  138. if absX <= 1 {
  139. y = 1 - absX
  140. } else {
  141. y = 0
  142. }
  143. return
  144. })
  145. }
  146. // Bicubic interpolation (with cubic hermite spline)
  147. func Bicubic(img image.Image, factor [2]float32) Filter {
  148. return createFilter(img, factor, 4, func(x float32) (y float32) {
  149. absX := float32(math.Abs(float64(x)))
  150. if absX <= 1 {
  151. y = absX*absX*(1.5*absX-2.5) + 1
  152. } else if absX <= 2 {
  153. y = absX*(absX*(2.5-0.5*absX)-4) + 2
  154. } else {
  155. y = 0
  156. }
  157. return
  158. })
  159. }
  160. // Mitchell-Netravali interpolation
  161. func MitchellNetravali(img image.Image, factor [2]float32) Filter {
  162. return createFilter(img, factor, 4, func(x float32) (y float32) {
  163. absX := float32(math.Abs(float64(x)))
  164. if absX <= 1 {
  165. y = absX*absX*(7*absX-12) + 16.0/3
  166. } else if absX <= 2 {
  167. y = -(absX - 2) * (absX - 2) / 3 * (7*absX - 8)
  168. } else {
  169. y = 0
  170. }
  171. return
  172. })
  173. }
  174. func lanczosKernel(a uint) func(float32) float32 {
  175. return func(x float32) (y float32) {
  176. if x > -float32(a) && x < float32(a) {
  177. y = float32(Sinc(float64(x))) * float32(Sinc(float64(x/float32(a))))
  178. } else {
  179. y = 0
  180. }
  181. return
  182. }
  183. }
  184. // Lanczos interpolation (a=2)
  185. func Lanczos2(img image.Image, factor [2]float32) Filter {
  186. return createFilter(img, factor, 4, lanczosKernel(2))
  187. }
  188. // Lanczos interpolation (a=3)
  189. func Lanczos3(img image.Image, factor [2]float32) Filter {
  190. return createFilter(img, factor, 6, lanczosKernel(3))
  191. }