filters.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // color.RGBA64 as array
  21. type rgba16 [4]uint16
  22. // build rgba16 from an arbitrary color
  23. func toRgba16(c color.Color) rgba16 {
  24. r, g, b, a := c.RGBA()
  25. return rgba16{uint16(r), uint16(g), uint16(b), uint16(a)}
  26. }
  27. func clampToUint16(x float32) (y uint16) {
  28. y = uint16(x)
  29. if x < 0 {
  30. y = 0
  31. } else if x > float32(0xfffe) {
  32. y = 0xffff
  33. }
  34. return
  35. }
  36. type filterModel struct {
  37. src image.Image
  38. factor [2]float32
  39. kernel func(float32) float32
  40. tempRow, tempCol []rgba16
  41. }
  42. func (f *filterModel) convolution1d(x float32, p []rgba16, isRow bool) (c rgba16) {
  43. var k float32
  44. var sum float32 = 0
  45. l := [4]float32{0.0, 0.0, 0.0, 0.0}
  46. var index uint
  47. if isRow {
  48. index = 0
  49. } else {
  50. index = 1
  51. }
  52. for j := range p {
  53. k = f.kernel((x - float32(j)) / f.factor[index])
  54. sum += k
  55. for i := range c {
  56. l[i] += float32(p[j][i]) * k
  57. }
  58. }
  59. for i := range c {
  60. c[i] = clampToUint16(l[i] / sum)
  61. }
  62. return
  63. }
  64. func (f *filterModel) Interpolate(x, y float32) color.RGBA64 {
  65. xf, yf := int(x)-len(f.tempRow)/2+1, int(y)-len(f.tempCol)/2+1
  66. x -= float32(xf)
  67. y -= float32(yf)
  68. for i := 0; i < len(f.tempCol); i++ {
  69. for j := 0; j < len(f.tempRow); j++ {
  70. f.tempRow[j] = toRgba16(f.src.At(xf+j, yf+i))
  71. }
  72. f.tempCol[i] = f.convolution1d(x, f.tempRow, true)
  73. }
  74. c := f.convolution1d(y, f.tempCol, false)
  75. return color.RGBA64{c[0], c[1], c[2], c[3]}
  76. }
  77. func createFilter(img image.Image, factor [2]float32, size int, kernel func(float32) float32) Filter {
  78. sizeX := size * (int(math.Ceil(float64(factor[0]))))
  79. sizeY := size * (int(math.Ceil(float64(factor[1]))))
  80. return &filterModel{img, factor, kernel, make([]rgba16, sizeX), make([]rgba16, sizeY)}
  81. }
  82. // Nearest-neighbor interpolation
  83. func NearestNeighbor(img image.Image, factor [2]float32) Filter {
  84. return createFilter(img, factor, 2, func(x float32) (y float32) {
  85. if x >= -0.5 && x < 0.5 {
  86. y = 1
  87. } else {
  88. y = 0
  89. }
  90. return
  91. })
  92. }
  93. // Bilinear interpolation
  94. func Bilinear(img image.Image, factor [2]float32) Filter {
  95. return createFilter(img, factor, 2, func(x float32) float32 {
  96. return 1 - float32(math.Abs(float64(x)))
  97. })
  98. }
  99. // Bicubic interpolation (with cubic hermite spline)
  100. func Bicubic(img image.Image, factor [2]float32) Filter {
  101. return createFilter(img, factor, 4, func(x float32) (y float32) {
  102. absX := float32(math.Abs(float64(x)))
  103. if absX <= 1 {
  104. y = absX*absX*(1.5*absX-2.5) + 1
  105. } else {
  106. y = absX*(absX*(2.5-0.5*absX)-4) + 2
  107. }
  108. return
  109. })
  110. }
  111. func MitchellNetravali(img image.Image, factor [2]float32) Filter {
  112. return createFilter(img, factor, 4, func(x float32) (y float32) {
  113. absX := float32(math.Abs(float64(x)))
  114. if absX <= 1 {
  115. y = absX*absX*(7*absX-12) + 16.0/3
  116. } else {
  117. y = -(absX - 2) * (absX - 2) / 3 * (7*absX - 8)
  118. }
  119. return
  120. })
  121. }
  122. func lanczosKernel(a uint) func(float32) float32 {
  123. return func(x float32) float32 {
  124. return float32(Sinc(float64(x))) * float32(Sinc(float64(x/float32(a))))
  125. }
  126. }
  127. // Lanczos interpolation (a=2).
  128. func Lanczos2(img image.Image, factor [2]float32) Filter {
  129. return createFilter(img, factor, 4, lanczosKernel(2))
  130. }
  131. // Lanczos interpolation (a=3).
  132. func Lanczos3(img image.Image, factor [2]float32) Filter {
  133. return createFilter(img, factor, 6, lanczosKernel(3))
  134. }