filters.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. func boolToUint(b bool) (i uint) {
  32. if b {
  33. i = 1
  34. } else {
  35. i = 0
  36. }
  37. return
  38. }
  39. // describe a resampling filter
  40. type filterModel struct {
  41. // resampling is done by convolution with a (scaled) kernel
  42. kernel func(float32) float32
  43. // instead of blurring an image before downscaling to avoid aliasing,
  44. // to filter is scaled by a factor which leads to a similar effect
  45. factor [2]float32
  46. // for optimized access to image points
  47. converter
  48. // temporaries used by Interpolate
  49. tempRow, tempCol []colorArray
  50. }
  51. func (f *filterModel) convolution1d(x float32, p []colorArray, isCol bool) colorArray {
  52. var k float32
  53. var sum float32 = 0
  54. c := colorArray{0.0, 0.0, 0.0, 0.0}
  55. index := boolToUint(isCol)
  56. for j := range p {
  57. k = f.kernel((x - float32(j)) / f.factor[index])
  58. sum += k
  59. for i := range c {
  60. c[i] += p[j][i] * k
  61. }
  62. }
  63. // normalize values
  64. for i := range c {
  65. c[i] = c[i] / sum
  66. }
  67. return c
  68. }
  69. func (f *filterModel) Interpolate(x, y float32) color.RGBA64 {
  70. xf, yf := int(x)-len(f.tempRow)/2+1, int(y)-len(f.tempCol)/2+1
  71. x -= float32(xf)
  72. y -= float32(yf)
  73. for i := 0; i < len(f.tempCol); i++ {
  74. for j := 0; j < len(f.tempRow); j++ {
  75. f.tempRow[j] = f.at(xf+j, yf+i)
  76. }
  77. f.tempCol[i] = f.convolution1d(x, f.tempRow, false)
  78. }
  79. c := f.convolution1d(y, f.tempCol, true)
  80. return color.RGBA64{
  81. clampToUint16(c[0]),
  82. clampToUint16(c[1]),
  83. clampToUint16(c[2]),
  84. clampToUint16(c[3]),
  85. }
  86. }
  87. // createFilter tries to find an optimized converter for the given input image
  88. // and initializes all filterModel members to their defaults
  89. func createFilter(img image.Image, factor [2]float32, size int, kernel func(float32) float32) (f Filter) {
  90. sizeX := size * (int(math.Ceil(float64(factor[0]))))
  91. sizeY := size * (int(math.Ceil(float64(factor[1]))))
  92. switch img.(type) {
  93. default:
  94. f = &filterModel{
  95. kernel, factor,
  96. &genericConverter{img},
  97. make([]colorArray, sizeX), make([]colorArray, sizeY),
  98. }
  99. case *image.RGBA:
  100. f = &filterModel{
  101. kernel, factor,
  102. &rgbaConverter{img.(*image.RGBA)},
  103. make([]colorArray, sizeX), make([]colorArray, sizeY),
  104. }
  105. case *image.RGBA64:
  106. f = &filterModel{
  107. kernel, factor,
  108. &rgba64Converter{img.(*image.RGBA64)},
  109. make([]colorArray, sizeX), make([]colorArray, sizeY),
  110. }
  111. case *image.Gray:
  112. f = &filterModel{
  113. kernel, factor,
  114. &grayConverter{img.(*image.Gray)},
  115. make([]colorArray, sizeX), make([]colorArray, sizeY),
  116. }
  117. case *image.Gray16:
  118. f = &filterModel{
  119. kernel, factor,
  120. &gray16Converter{img.(*image.Gray16)},
  121. make([]colorArray, sizeX), make([]colorArray, sizeY),
  122. }
  123. case *image.YCbCr:
  124. f = &filterModel{
  125. kernel, factor,
  126. &ycbcrConverter{img.(*image.YCbCr)},
  127. make([]colorArray, sizeX), make([]colorArray, sizeY),
  128. }
  129. }
  130. return
  131. }
  132. // Nearest-neighbor interpolation
  133. func NearestNeighbor(img image.Image, factor [2]float32) Filter {
  134. return createFilter(img, factor, 2, func(x float32) (y float32) {
  135. if x >= -0.5 && x < 0.5 {
  136. y = 1
  137. } else {
  138. y = 0
  139. }
  140. return
  141. })
  142. }
  143. // Bilinear interpolation
  144. func Bilinear(img image.Image, factor [2]float32) Filter {
  145. return createFilter(img, factor, 2, func(x float32) (y float32) {
  146. absX := float32(math.Abs(float64(x)))
  147. if absX <= 1 {
  148. y = 1 - absX
  149. } else {
  150. y = 0
  151. }
  152. return
  153. })
  154. }
  155. // Bicubic interpolation (with cubic hermite spline)
  156. func Bicubic(img image.Image, factor [2]float32) Filter {
  157. return createFilter(img, factor, 4, func(x float32) (y float32) {
  158. absX := float32(math.Abs(float64(x)))
  159. if absX <= 1 {
  160. y = absX*absX*(1.5*absX-2.5) + 1
  161. } else if absX <= 2 {
  162. y = absX*(absX*(2.5-0.5*absX)-4) + 2
  163. } else {
  164. y = 0
  165. }
  166. return
  167. })
  168. }
  169. // Mitchell-Netravali interpolation
  170. func MitchellNetravali(img image.Image, factor [2]float32) Filter {
  171. return createFilter(img, factor, 4, func(x float32) (y float32) {
  172. absX := float32(math.Abs(float64(x)))
  173. if absX <= 1 {
  174. y = absX*absX*(7*absX-12) + 16.0/3
  175. } else if absX <= 2 {
  176. y = -(absX - 2) * (absX - 2) / 3 * (7*absX - 8)
  177. } else {
  178. y = 0
  179. }
  180. return
  181. })
  182. }
  183. func lanczosKernel(a uint) func(float32) float32 {
  184. return func(x float32) (y float32) {
  185. if x > -float32(a) && x < float32(a) {
  186. y = float32(Sinc(float64(x))) * float32(Sinc(float64(x/float32(a))))
  187. } else {
  188. y = 0
  189. }
  190. return
  191. }
  192. }
  193. // Lanczos interpolation (a=2)
  194. func Lanczos2(img image.Image, factor [2]float32) Filter {
  195. return createFilter(img, factor, 4, lanczosKernel(2))
  196. }
  197. // Lanczos interpolation (a=3)
  198. func Lanczos3(img image.Image, factor [2]float32) Filter {
  199. return createFilter(img, factor, 6, lanczosKernel(3))
  200. }