No Description

filters.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "math"
  17. )
  18. func nearest(in float64) float64 {
  19. if in >= -0.5 && in < 0.5 {
  20. return 1
  21. }
  22. return 0
  23. }
  24. func linear(in float64) float64 {
  25. in = math.Abs(in)
  26. if in <= 1 {
  27. return 1 - in
  28. }
  29. return 0
  30. }
  31. func cubic(in float64) float64 {
  32. in = math.Abs(in)
  33. if in <= 1 {
  34. return in*in*(1.5*in-2.5) + 1.0
  35. }
  36. if in <= 2 {
  37. return in*(in*(2.5-0.5*in)-4.0) + 2.0
  38. }
  39. return 0
  40. }
  41. func mitchellnetravali(in float64) float64 {
  42. in = math.Abs(in)
  43. if in <= 1 {
  44. return (7.0*in*in*in - 12.0*in*in + 5.33333333333) * 0.16666666666
  45. }
  46. if in <= 2 {
  47. return (-2.33333333333*in*in*in + 12.0*in*in - 20.0*in + 10.6666666667) * 0.16666666666
  48. }
  49. return 0
  50. }
  51. func sinc(x float64) float64 {
  52. x = math.Abs(x) * math.Pi
  53. if x >= 1.220703e-4 {
  54. return math.Sin(x) / x
  55. }
  56. return 1
  57. }
  58. func lanczos2(in float64) float64 {
  59. if in > -2 && in < 2 {
  60. return sinc(in) * sinc(in*0.5)
  61. }
  62. return 0
  63. }
  64. func lanczos3(in float64) float64 {
  65. if in > -3 && in < 3 {
  66. return sinc(in) * sinc(in*0.3333333333333333)
  67. }
  68. return 0
  69. }
  70. // range [-256,256]
  71. func createWeights8(dy, filterLength int, blur, scale float64, kernel func(float64) float64) ([]int16, []int, int) {
  72. filterLength = filterLength * int(math.Max(math.Ceil(blur*scale), 1))
  73. filterFactor := math.Min(1./(blur*scale), 1)
  74. coeffs := make([]int16, dy*filterLength)
  75. start := make([]int, dy)
  76. for y := 0; y < dy; y++ {
  77. interpX := scale*(float64(y)+0.5) - 0.5
  78. start[y] = int(interpX) - filterLength/2 + 1
  79. interpX -= float64(start[y])
  80. for i := 0; i < filterLength; i++ {
  81. in := (interpX - float64(i)) * filterFactor
  82. coeffs[y*filterLength+i] = int16(kernel(in) * 256)
  83. }
  84. }
  85. return coeffs, start, filterLength
  86. }
  87. // range [-65536,65536]
  88. func createWeights16(dy, filterLength int, blur, scale float64, kernel func(float64) float64) ([]int32, []int, int) {
  89. filterLength = filterLength * int(math.Max(math.Ceil(blur*scale), 1))
  90. filterFactor := math.Min(1./(blur*scale), 1)
  91. coeffs := make([]int32, dy*filterLength)
  92. start := make([]int, dy)
  93. for y := 0; y < dy; y++ {
  94. interpX := scale*(float64(y)+0.5) - 0.5
  95. start[y] = int(interpX) - filterLength/2 + 1
  96. interpX -= float64(start[y])
  97. for i := 0; i < filterLength; i++ {
  98. in := (interpX - float64(i)) * filterFactor
  99. coeffs[y*filterLength+i] = int32(kernel(in) * 65536)
  100. }
  101. }
  102. return coeffs, start, filterLength
  103. }
  104. func createWeightsNearest(dy, filterLength int, blur, scale float64) ([]bool, []int, int) {
  105. filterLength = filterLength * int(math.Max(math.Ceil(blur*scale), 1))
  106. filterFactor := math.Min(1./(blur*scale), 1)
  107. coeffs := make([]bool, dy*filterLength)
  108. start := make([]int, dy)
  109. for y := 0; y < dy; y++ {
  110. interpX := scale*(float64(y)+0.5) - 0.5
  111. start[y] = int(interpX) - filterLength/2 + 1
  112. interpX -= float64(start[y])
  113. for i := 0; i < filterLength; i++ {
  114. in := (interpX - float64(i)) * filterFactor
  115. if in >= -0.5 && in < 0.5 {
  116. coeffs[y*filterLength+i] = true
  117. } else {
  118. coeffs[y*filterLength+i] = false
  119. }
  120. }
  121. }
  122. return coeffs, start, filterLength
  123. }