converter.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. )
  19. type colorArray [4]float32
  20. func replicateBorder1d(x, min, max int) int {
  21. if x < min {
  22. x = min
  23. } else if x >= max {
  24. x = max - 1
  25. }
  26. return x
  27. }
  28. func replicateBorder(x, y int, rect image.Rectangle) (xx, yy int) {
  29. xx = replicateBorder1d(x, rect.Min.X, rect.Max.X)
  30. yy = replicateBorder1d(y, rect.Min.Y, rect.Max.Y)
  31. return
  32. }
  33. // converter allows to retrieve a colorArray for points of an image.
  34. // the idea is to speed up computation by providing optimized implementations
  35. // for different image types instead of relying on image.Image.At().
  36. type converter interface {
  37. at(x, y int, color *colorArray)
  38. }
  39. type genericConverter struct {
  40. src image.Image
  41. }
  42. func (c *genericConverter) at(x, y int, result *colorArray) {
  43. r, g, b, a := c.src.At(replicateBorder(x, y, c.src.Bounds())).RGBA()
  44. result[0] = float32(r)
  45. result[1] = float32(g)
  46. result[2] = float32(b)
  47. result[3] = float32(a)
  48. return
  49. }
  50. type rgbaConverter struct {
  51. src *image.RGBA
  52. }
  53. func (c *rgbaConverter) at(x, y int, result *colorArray) {
  54. i := c.src.PixOffset(replicateBorder(x, y, c.src.Rect))
  55. result[0] = float32(uint16(c.src.Pix[i+0])<<8 | uint16(c.src.Pix[i+0]))
  56. result[1] = float32(uint16(c.src.Pix[i+1])<<8 | uint16(c.src.Pix[i+1]))
  57. result[2] = float32(uint16(c.src.Pix[i+2])<<8 | uint16(c.src.Pix[i+2]))
  58. result[3] = float32(uint16(c.src.Pix[i+3])<<8 | uint16(c.src.Pix[i+3]))
  59. return
  60. }
  61. type rgba64Converter struct {
  62. src *image.RGBA64
  63. }
  64. func (c *rgba64Converter) at(x, y int, result *colorArray) {
  65. i := c.src.PixOffset(replicateBorder(x, y, c.src.Rect))
  66. result[0] = float32(uint16(c.src.Pix[i+0])<<8 | uint16(c.src.Pix[i+1]))
  67. result[1] = float32(uint16(c.src.Pix[i+2])<<8 | uint16(c.src.Pix[i+3]))
  68. result[2] = float32(uint16(c.src.Pix[i+4])<<8 | uint16(c.src.Pix[i+5]))
  69. result[3] = float32(uint16(c.src.Pix[i+6])<<8 | uint16(c.src.Pix[i+7]))
  70. return
  71. }
  72. type grayConverter struct {
  73. src *image.Gray
  74. }
  75. func (c *grayConverter) at(x, y int, result *colorArray) {
  76. i := c.src.PixOffset(replicateBorder(x, y, c.src.Rect))
  77. g := float32(uint16(c.src.Pix[i])<<8 | uint16(c.src.Pix[i]))
  78. result[0] = g
  79. result[1] = g
  80. result[2] = g
  81. result[3] = float32(0xffff)
  82. return
  83. }
  84. type gray16Converter struct {
  85. src *image.Gray16
  86. }
  87. func (c *gray16Converter) at(x, y int, result *colorArray) {
  88. i := c.src.PixOffset(replicateBorder(x, y, c.src.Rect))
  89. g := float32(uint16(c.src.Pix[i+0])<<8 | uint16(c.src.Pix[i+1]))
  90. result[0] = g
  91. result[1] = g
  92. result[2] = g
  93. result[3] = float32(0xffff)
  94. return
  95. }
  96. type ycbcrConverter struct {
  97. src *image.YCbCr
  98. }
  99. func (c *ycbcrConverter) at(x, y int, result *colorArray) {
  100. xx, yy := replicateBorder(x, y, c.src.Rect)
  101. yi := c.src.YOffset(xx, yy)
  102. ci := c.src.COffset(xx, yy)
  103. r, g, b := color.YCbCrToRGB(c.src.Y[yi], c.src.Cb[ci], c.src.Cr[ci])
  104. result[0] = float32(uint16(r) * 0x101)
  105. result[1] = float32(uint16(g) * 0x101)
  106. result[2] = float32(uint16(b) * 0x101)
  107. result[3] = float32(0xffff)
  108. return
  109. }