No Description

filters.go 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 RGBA [4]uint16
  22. // build RGBA from an arbitrary color
  23. func toRGBA(c color.Color) RGBA {
  24. n := color.RGBA64Model.Convert(c).(color.RGBA64)
  25. return RGBA{n.R, n.G, n.B, n.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(0xffff) {
  32. y = 0xffff
  33. }
  34. return
  35. }
  36. // Nearest-neighbor interpolation.
  37. // Approximates a value by returning the value of the nearest point.
  38. func NearestNeighbor(x, y float32, img image.Image) color.RGBA64 {
  39. xn, yn := int(x), int(y)
  40. c := toRGBA(img.At(xn, yn))
  41. return color.RGBA64{c[0], c[1], c[2], c[3]}
  42. }
  43. // Linear interpolation.
  44. func linearInterp(x float32, p *[2]RGBA) (c RGBA) {
  45. x -= float32(math.Floor(float64(x)))
  46. for i := range c {
  47. c[i] = clampToUint16(float32(p[0][i])*(1.0-x) + x*float32(p[1][i]))
  48. }
  49. return
  50. }
  51. // Bilinear interpolation.
  52. func Bilinear(x, y float32, img image.Image) color.RGBA64 {
  53. xf, yf := int(math.Floor(float64(x))), int(math.Floor(float64(y)))
  54. var row [2]RGBA
  55. var col [2]RGBA
  56. row = [2]RGBA{toRGBA(img.At(xf, yf)), toRGBA(img.At(xf+1, yf))}
  57. col[0] = linearInterp(x, &row)
  58. row = [2]RGBA{toRGBA(img.At(xf, yf+1)), toRGBA(img.At(xf+1, yf+1))}
  59. col[1] = linearInterp(x, &row)
  60. c := linearInterp(y, &col)
  61. return color.RGBA64{c[0], c[1], c[2], c[3]}
  62. }
  63. // cubic interpolation
  64. func cubicInterp(x float32, p *[4]RGBA) (c RGBA) {
  65. x -= float32(math.Floor(float64(x)))
  66. for i := range c {
  67. c[i] = clampToUint16(float32(p[1][i]) + 0.5*x*(float32(p[2][i])-float32(p[0][i])+x*(2.0*float32(p[0][i])-5.0*float32(p[1][i])+4.0*float32(p[2][i])-float32(p[3][i])+x*(3.0*(float32(p[1][i])-float32(p[2][i]))+float32(p[3][i])-float32(p[0][i])))))
  68. }
  69. return
  70. }
  71. // Bicubic interpolation.
  72. func Bicubic(x, y float32, img image.Image) color.RGBA64 {
  73. xf, yf := int(math.Floor(float64(x))), int(math.Floor(float64(y)))
  74. var row [4]RGBA
  75. var col [4]RGBA
  76. row = [4]RGBA{toRGBA(img.At(xf-1, yf-1)), toRGBA(img.At(xf, yf-1)), toRGBA(img.At(xf+1, yf-1)), toRGBA(img.At(xf+2, yf-1))}
  77. col[0] = cubicInterp(x, &row)
  78. row = [4]RGBA{toRGBA(img.At(xf-1, yf)), toRGBA(img.At(xf, yf)), toRGBA(img.At(xf+1, yf)), toRGBA(img.At(xf+2, yf))}
  79. col[1] = cubicInterp(x, &row)
  80. row = [4]RGBA{toRGBA(img.At(xf-1, yf+1)), toRGBA(img.At(xf, yf+1)), toRGBA(img.At(xf+1, yf+1)), toRGBA(img.At(xf+2, yf+1))}
  81. col[2] = cubicInterp(x, &row)
  82. row = [4]RGBA{toRGBA(img.At(xf-1, yf+2)), toRGBA(img.At(xf, yf+2)), toRGBA(img.At(xf+1, yf+2)), toRGBA(img.At(xf+2, yf+2))}
  83. col[3] = cubicInterp(x, &row)
  84. c := cubicInterp(y, &col)
  85. return color.RGBA64{c[0], c[1], c[2], c[3]}
  86. }
  87. // 1-d convolution with windowed sinc for a=3.
  88. func lanczos_x(x float32, p *[6]RGBA) (c RGBA) {
  89. x -= float32(math.Floor(float64(x)))
  90. var v float32
  91. l := [4]float32{0.0, 0.0, 0.0, 0.0}
  92. for j := range p {
  93. v = float32(Sinc(float64(x-float32(j-2)))) * float32(Sinc(float64((x-float32(j-2))/3.0)))
  94. for i := range c {
  95. l[i] += float32(p[j][i]) * v
  96. }
  97. }
  98. for i := range c {
  99. c[i] = clampToUint16(l[i])
  100. }
  101. return
  102. }
  103. // Lanczos interpolation (a=3).
  104. func Lanczos3(x, y float32, img image.Image) color.RGBA64 {
  105. xf, yf := int(math.Floor(float64(x))), int(math.Floor(float64(y)))
  106. var row [6]RGBA
  107. var col [6]RGBA
  108. row = [6]RGBA{toRGBA(img.At(xf-2, yf-2)), toRGBA(img.At(xf-1, yf-2)), toRGBA(img.At(xf, yf-2)), toRGBA(img.At(xf+1, yf-2)), toRGBA(img.At(xf+2, yf-2)), toRGBA(img.At(xf+3, yf-2))}
  109. col[0] = lanczos_x(x, &row)
  110. row = [6]RGBA{toRGBA(img.At(xf-2, yf-1)), toRGBA(img.At(xf-1, yf-1)), toRGBA(img.At(xf, yf-1)), toRGBA(img.At(xf+1, yf-1)), toRGBA(img.At(xf+2, yf-1)), toRGBA(img.At(xf+3, yf-1))}
  111. col[1] = lanczos_x(x, &row)
  112. row = [6]RGBA{toRGBA(img.At(xf-2, yf)), toRGBA(img.At(xf-1, yf)), toRGBA(img.At(xf, yf)), toRGBA(img.At(xf+1, yf)), toRGBA(img.At(xf+2, yf)), toRGBA(img.At(xf+3, yf))}
  113. col[2] = lanczos_x(x, &row)
  114. row = [6]RGBA{toRGBA(img.At(xf-2, yf+1)), toRGBA(img.At(xf-1, yf+1)), toRGBA(img.At(xf, yf+1)), toRGBA(img.At(xf+1, yf+1)), toRGBA(img.At(xf+2, yf+1)), toRGBA(img.At(xf+3, yf+1))}
  115. col[3] = lanczos_x(x, &row)
  116. row = [6]RGBA{toRGBA(img.At(xf-2, yf+2)), toRGBA(img.At(xf-1, yf+2)), toRGBA(img.At(xf, yf+2)), toRGBA(img.At(xf+1, yf+2)), toRGBA(img.At(xf+2, yf+2)), toRGBA(img.At(xf+3, yf+2))}
  117. col[4] = lanczos_x(x, &row)
  118. row = [6]RGBA{toRGBA(img.At(xf-2, yf+3)), toRGBA(img.At(xf-1, yf+3)), toRGBA(img.At(xf, yf+3)), toRGBA(img.At(xf+1, yf+3)), toRGBA(img.At(xf+2, yf+3)), toRGBA(img.At(xf+3, yf+3))}
  119. col[5] = lanczos_x(x, &row)
  120. c := lanczos_x(y, &col)
  121. return color.RGBA64{c[0], c[1], c[2], c[3]}
  122. }