converter.go 3.3 KB

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