converter.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // converter allows to retrieve
  21. // a colorArray for points of an image
  22. type converter interface {
  23. at(x, y int) colorArray
  24. }
  25. type genericConverter struct {
  26. src image.Image
  27. }
  28. func (c *genericConverter) at(x, y int) colorArray {
  29. r, g, b, a := c.src.At(x, y).RGBA()
  30. return colorArray{
  31. float32(r),
  32. float32(g),
  33. float32(b),
  34. float32(a),
  35. }
  36. }
  37. type rgbaConverter struct {
  38. src *image.RGBA
  39. }
  40. func (c *rgbaConverter) at(x, y int) colorArray {
  41. if !(image.Point{x, y}.In(c.src.Rect)) {
  42. return colorArray{0, 0, 0, 0}
  43. }
  44. i := c.src.PixOffset(x, y)
  45. return colorArray{
  46. float32(uint16(c.src.Pix[i+0])<<8 | uint16(c.src.Pix[i+0])),
  47. float32(uint16(c.src.Pix[i+1])<<8 | uint16(c.src.Pix[i+1])),
  48. float32(uint16(c.src.Pix[i+2])<<8 | uint16(c.src.Pix[i+2])),
  49. float32(uint16(c.src.Pix[i+3])<<8 | uint16(c.src.Pix[i+3])),
  50. }
  51. }
  52. type rgba64Converter struct {
  53. src *image.RGBA64
  54. }
  55. func (c *rgba64Converter) at(x, y int) colorArray {
  56. if !(image.Point{x, y}.In(c.src.Rect)) {
  57. return colorArray{0, 0, 0, 0}
  58. }
  59. i := c.src.PixOffset(x, y)
  60. return colorArray{
  61. float32(uint16(c.src.Pix[i+0])<<8 | uint16(c.src.Pix[i+1])),
  62. float32(uint16(c.src.Pix[i+2])<<8 | uint16(c.src.Pix[i+3])),
  63. float32(uint16(c.src.Pix[i+4])<<8 | uint16(c.src.Pix[i+5])),
  64. float32(uint16(c.src.Pix[i+6])<<8 | uint16(c.src.Pix[i+7])),
  65. }
  66. }
  67. type grayConverter struct {
  68. src *image.Gray
  69. }
  70. func (c *grayConverter) at(x, y int) colorArray {
  71. if !(image.Point{x, y}.In(c.src.Rect)) {
  72. return colorArray{0, 0, 0, 0}
  73. }
  74. i := c.src.PixOffset(x, y)
  75. g := float32(uint16(c.src.Pix[i])<<8 | uint16(c.src.Pix[i]))
  76. return colorArray{
  77. g,
  78. g,
  79. g,
  80. float32(0xffff),
  81. }
  82. }
  83. type gray16Converter struct {
  84. src *image.Gray16
  85. }
  86. func (c *gray16Converter) at(x, y int) colorArray {
  87. if !(image.Point{x, y}.In(c.src.Rect)) {
  88. return colorArray{0, 0, 0, 0}
  89. }
  90. i := c.src.PixOffset(x, y)
  91. g := float32(uint16(c.src.Pix[i+0])<<8 | uint16(c.src.Pix[i+1]))
  92. return colorArray{
  93. g,
  94. g,
  95. g,
  96. float32(0xffff),
  97. }
  98. }
  99. type ycbcrConverter struct {
  100. src *image.YCbCr
  101. }
  102. func (c *ycbcrConverter) at(x, y int) colorArray {
  103. if !(image.Point{x, y}.In(c.src.Rect)) {
  104. return colorArray{0, 0, 0, 0}
  105. }
  106. yi := c.src.YOffset(x, y)
  107. ci := c.src.COffset(x, y)
  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. }