scanner.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package imaging
  2. import (
  3. "image"
  4. "image/color"
  5. )
  6. type scanner struct {
  7. image image.Image
  8. w, h int
  9. palette []color.NRGBA
  10. }
  11. func newScanner(img image.Image) *scanner {
  12. s := &scanner{
  13. image: img,
  14. w: img.Bounds().Dx(),
  15. h: img.Bounds().Dy(),
  16. }
  17. if img, ok := img.(*image.Paletted); ok {
  18. s.palette = make([]color.NRGBA, len(img.Palette))
  19. for i := 0; i < len(img.Palette); i++ {
  20. s.palette[i] = color.NRGBAModel.Convert(img.Palette[i]).(color.NRGBA)
  21. }
  22. }
  23. return s
  24. }
  25. // scan scans the given rectangular region of the image into dst.
  26. func (s *scanner) scan(x1, y1, x2, y2 int, dst []uint8) {
  27. switch img := s.image.(type) {
  28. case *image.NRGBA:
  29. size := (x2 - x1) * 4
  30. j := 0
  31. i := y1*img.Stride + x1*4
  32. for y := y1; y < y2; y++ {
  33. copy(dst[j:j+size], img.Pix[i:i+size])
  34. j += size
  35. i += img.Stride
  36. }
  37. case *image.NRGBA64:
  38. j := 0
  39. for y := y1; y < y2; y++ {
  40. i := y*img.Stride + x1*8
  41. for x := x1; x < x2; x++ {
  42. s := img.Pix[i : i+8 : i+8]
  43. d := dst[j : j+4 : j+4]
  44. d[0] = s[0]
  45. d[1] = s[2]
  46. d[2] = s[4]
  47. d[3] = s[6]
  48. j += 4
  49. i += 8
  50. }
  51. }
  52. case *image.RGBA:
  53. j := 0
  54. for y := y1; y < y2; y++ {
  55. i := y*img.Stride + x1*4
  56. for x := x1; x < x2; x++ {
  57. s := img.Pix[i : i+4 : i+4]
  58. d := dst[j : j+4 : j+4]
  59. a := s[3]
  60. switch a {
  61. case 0:
  62. d[0] = 0
  63. d[1] = 0
  64. d[2] = 0
  65. case 0xff:
  66. d[0] = s[0]
  67. d[1] = s[1]
  68. d[2] = s[2]
  69. default:
  70. r16 := uint16(s[0])
  71. g16 := uint16(s[1])
  72. b16 := uint16(s[2])
  73. a16 := uint16(a)
  74. d[0] = uint8(r16 * 0xff / a16)
  75. d[1] = uint8(g16 * 0xff / a16)
  76. d[2] = uint8(b16 * 0xff / a16)
  77. }
  78. d[3] = a
  79. j += 4
  80. i += 4
  81. }
  82. }
  83. case *image.RGBA64:
  84. j := 0
  85. for y := y1; y < y2; y++ {
  86. i := y*img.Stride + x1*8
  87. for x := x1; x < x2; x++ {
  88. s := img.Pix[i : i+8 : i+8]
  89. d := dst[j : j+4 : j+4]
  90. a := s[6]
  91. switch a {
  92. case 0:
  93. d[0] = 0
  94. d[1] = 0
  95. d[2] = 0
  96. case 0xff:
  97. d[0] = s[0]
  98. d[1] = s[2]
  99. d[2] = s[4]
  100. default:
  101. r32 := uint32(s[0])<<8 | uint32(s[1])
  102. g32 := uint32(s[2])<<8 | uint32(s[3])
  103. b32 := uint32(s[4])<<8 | uint32(s[5])
  104. a32 := uint32(s[6])<<8 | uint32(s[7])
  105. d[0] = uint8((r32 * 0xffff / a32) >> 8)
  106. d[1] = uint8((g32 * 0xffff / a32) >> 8)
  107. d[2] = uint8((b32 * 0xffff / a32) >> 8)
  108. }
  109. d[3] = a
  110. j += 4
  111. i += 8
  112. }
  113. }
  114. case *image.Gray:
  115. j := 0
  116. for y := y1; y < y2; y++ {
  117. i := y*img.Stride + x1
  118. for x := x1; x < x2; x++ {
  119. c := img.Pix[i]
  120. d := dst[j : j+4 : j+4]
  121. d[0] = c
  122. d[1] = c
  123. d[2] = c
  124. d[3] = 0xff
  125. j += 4
  126. i++
  127. }
  128. }
  129. case *image.Gray16:
  130. j := 0
  131. for y := y1; y < y2; y++ {
  132. i := y*img.Stride + x1*2
  133. for x := x1; x < x2; x++ {
  134. c := img.Pix[i]
  135. d := dst[j : j+4 : j+4]
  136. d[0] = c
  137. d[1] = c
  138. d[2] = c
  139. d[3] = 0xff
  140. j += 4
  141. i += 2
  142. }
  143. }
  144. case *image.YCbCr:
  145. j := 0
  146. x1 += img.Rect.Min.X
  147. x2 += img.Rect.Min.X
  148. y1 += img.Rect.Min.Y
  149. y2 += img.Rect.Min.Y
  150. for y := y1; y < y2; y++ {
  151. iy := (y-img.Rect.Min.Y)*img.YStride + (x1 - img.Rect.Min.X)
  152. for x := x1; x < x2; x++ {
  153. var ic int
  154. switch img.SubsampleRatio {
  155. case image.YCbCrSubsampleRatio444:
  156. ic = (y-img.Rect.Min.Y)*img.CStride + (x - img.Rect.Min.X)
  157. case image.YCbCrSubsampleRatio422:
  158. ic = (y-img.Rect.Min.Y)*img.CStride + (x/2 - img.Rect.Min.X/2)
  159. case image.YCbCrSubsampleRatio420:
  160. ic = (y/2-img.Rect.Min.Y/2)*img.CStride + (x/2 - img.Rect.Min.X/2)
  161. case image.YCbCrSubsampleRatio440:
  162. ic = (y/2-img.Rect.Min.Y/2)*img.CStride + (x - img.Rect.Min.X)
  163. default:
  164. ic = img.COffset(x, y)
  165. }
  166. yy := int(img.Y[iy])
  167. cb := int(img.Cb[ic]) - 128
  168. cr := int(img.Cr[ic]) - 128
  169. r := (yy<<16 + 91881*cr + 1<<15) >> 16
  170. if r > 0xff {
  171. r = 0xff
  172. } else if r < 0 {
  173. r = 0
  174. }
  175. g := (yy<<16 - 22554*cb - 46802*cr + 1<<15) >> 16
  176. if g > 0xff {
  177. g = 0xff
  178. } else if g < 0 {
  179. g = 0
  180. }
  181. b := (yy<<16 + 116130*cb + 1<<15) >> 16
  182. if b > 0xff {
  183. b = 0xff
  184. } else if b < 0 {
  185. b = 0
  186. }
  187. d := dst[j : j+4 : j+4]
  188. d[0] = uint8(r)
  189. d[1] = uint8(g)
  190. d[2] = uint8(b)
  191. d[3] = 0xff
  192. iy++
  193. j += 4
  194. }
  195. }
  196. case *image.Paletted:
  197. j := 0
  198. for y := y1; y < y2; y++ {
  199. i := y*img.Stride + x1
  200. for x := x1; x < x2; x++ {
  201. c := s.palette[img.Pix[i]]
  202. d := dst[j : j+4 : j+4]
  203. d[0] = c.R
  204. d[1] = c.G
  205. d[2] = c.B
  206. d[3] = c.A
  207. j += 4
  208. i++
  209. }
  210. }
  211. default:
  212. j := 0
  213. b := s.image.Bounds()
  214. x1 += b.Min.X
  215. x2 += b.Min.X
  216. y1 += b.Min.Y
  217. y2 += b.Min.Y
  218. for y := y1; y < y2; y++ {
  219. for x := x1; x < x2; x++ {
  220. r16, g16, b16, a16 := s.image.At(x, y).RGBA()
  221. d := dst[j : j+4 : j+4]
  222. switch a16 {
  223. case 0xffff:
  224. d[0] = uint8(r16 >> 8)
  225. d[1] = uint8(g16 >> 8)
  226. d[2] = uint8(b16 >> 8)
  227. d[3] = 0xff
  228. case 0:
  229. d[0] = 0
  230. d[1] = 0
  231. d[2] = 0
  232. d[3] = 0
  233. default:
  234. d[0] = uint8(((r16 * 0xffff) / a16) >> 8)
  235. d[1] = uint8(((g16 * 0xffff) / a16) >> 8)
  236. d[2] = uint8(((b16 * 0xffff) / a16) >> 8)
  237. d[3] = uint8(a16 >> 8)
  238. }
  239. j += 4
  240. }
  241. }
  242. }
  243. }