converter.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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) colorArray
  38. }
  39. type genericConverter struct {
  40. src image.Image
  41. }
  42. func (c *genericConverter) at(x, y int) colorArray {
  43. r, g, b, a := c.src.At(replicateBorder(x, y, c.src.Bounds())).RGBA()
  44. return colorArray{
  45. float32(r),
  46. float32(g),
  47. float32(b),
  48. float32(a),
  49. }
  50. }
  51. type rgbaConverter struct {
  52. src *image.RGBA
  53. }
  54. func (c *rgbaConverter) at(x, y int) colorArray {
  55. i := c.src.PixOffset(replicateBorder(x, y, c.src.Rect))
  56. return colorArray{
  57. float32(uint16(c.src.Pix[i+0])<<8 | uint16(c.src.Pix[i+0])),
  58. float32(uint16(c.src.Pix[i+1])<<8 | uint16(c.src.Pix[i+1])),
  59. float32(uint16(c.src.Pix[i+2])<<8 | uint16(c.src.Pix[i+2])),
  60. float32(uint16(c.src.Pix[i+3])<<8 | uint16(c.src.Pix[i+3])),
  61. }
  62. }
  63. type rgba64Converter struct {
  64. src *image.RGBA64
  65. }
  66. func (c *rgba64Converter) at(x, y int) colorArray {
  67. i := c.src.PixOffset(replicateBorder(x, y, c.src.Rect))
  68. return colorArray{
  69. float32(uint16(c.src.Pix[i+0])<<8 | uint16(c.src.Pix[i+1])),
  70. float32(uint16(c.src.Pix[i+2])<<8 | uint16(c.src.Pix[i+3])),
  71. float32(uint16(c.src.Pix[i+4])<<8 | uint16(c.src.Pix[i+5])),
  72. float32(uint16(c.src.Pix[i+6])<<8 | uint16(c.src.Pix[i+7])),
  73. }
  74. }
  75. type grayConverter struct {
  76. src *image.Gray
  77. }
  78. func (c *grayConverter) at(x, y int) colorArray {
  79. i := c.src.PixOffset(replicateBorder(x, y, c.src.Rect))
  80. g := float32(uint16(c.src.Pix[i])<<8 | uint16(c.src.Pix[i]))
  81. return colorArray{
  82. g,
  83. g,
  84. g,
  85. float32(0xffff),
  86. }
  87. }
  88. type gray16Converter struct {
  89. src *image.Gray16
  90. }
  91. func (c *gray16Converter) at(x, y int) colorArray {
  92. i := c.src.PixOffset(replicateBorder(x, y, c.src.Rect))
  93. g := float32(uint16(c.src.Pix[i+0])<<8 | uint16(c.src.Pix[i+1]))
  94. return colorArray{
  95. g,
  96. g,
  97. g,
  98. float32(0xffff),
  99. }
  100. }
  101. type ycbcrConverter struct {
  102. src *image.YCbCr
  103. }
  104. func (c *ycbcrConverter) at(x, y int) colorArray {
  105. xx, yy := replicateBorder(x, y, c.src.Rect)
  106. yi := c.src.YOffset(xx, yy)
  107. ci := c.src.COffset(xx, yy)
  108. r, g, b := color.YCbCrToRGB(c.src.Y[yi], c.src.Cb[ci], c.src.Cr[ci])
  109. return colorArray{
  110. float32(uint16(r) * 0x101),
  111. float32(uint16(g) * 0x101),
  112. float32(uint16(b) * 0x101),
  113. float32(0xffff),
  114. }
  115. }