scanner.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. if size == 4 {
  33. for y := y1; y < y2; y++ {
  34. d := dst[j : j+4 : j+4]
  35. s := img.Pix[i : i+4 : i+4]
  36. d[0] = s[0]
  37. d[1] = s[1]
  38. d[2] = s[2]
  39. d[3] = s[3]
  40. j += size
  41. i += img.Stride
  42. }
  43. } else {
  44. for y := y1; y < y2; y++ {
  45. copy(dst[j:j+size], img.Pix[i:i+size])
  46. j += size
  47. i += img.Stride
  48. }
  49. }
  50. case *image.NRGBA64:
  51. j := 0
  52. for y := y1; y < y2; y++ {
  53. i := y*img.Stride + x1*8
  54. for x := x1; x < x2; x++ {
  55. s := img.Pix[i : i+8 : i+8]
  56. d := dst[j : j+4 : j+4]
  57. d[0] = s[0]
  58. d[1] = s[2]
  59. d[2] = s[4]
  60. d[3] = s[6]
  61. j += 4
  62. i += 8
  63. }
  64. }
  65. case *image.RGBA:
  66. j := 0
  67. for y := y1; y < y2; y++ {
  68. i := y*img.Stride + x1*4
  69. for x := x1; x < x2; x++ {
  70. d := dst[j : j+4 : j+4]
  71. a := img.Pix[i+3]
  72. switch a {
  73. case 0:
  74. d[0] = 0
  75. d[1] = 0
  76. d[2] = 0
  77. d[3] = a
  78. case 0xff:
  79. s := img.Pix[i : i+4 : i+4]
  80. d[0] = s[0]
  81. d[1] = s[1]
  82. d[2] = s[2]
  83. d[3] = a
  84. default:
  85. s := img.Pix[i : i+4 : i+4]
  86. r16 := uint16(s[0])
  87. g16 := uint16(s[1])
  88. b16 := uint16(s[2])
  89. a16 := uint16(a)
  90. d[0] = uint8(r16 * 0xff / a16)
  91. d[1] = uint8(g16 * 0xff / a16)
  92. d[2] = uint8(b16 * 0xff / a16)
  93. d[3] = a
  94. }
  95. j += 4
  96. i += 4
  97. }
  98. }
  99. case *image.RGBA64:
  100. j := 0
  101. for y := y1; y < y2; y++ {
  102. i := y*img.Stride + x1*8
  103. for x := x1; x < x2; x++ {
  104. s := img.Pix[i : i+8 : i+8]
  105. d := dst[j : j+4 : j+4]
  106. a := s[6]
  107. switch a {
  108. case 0:
  109. d[0] = 0
  110. d[1] = 0
  111. d[2] = 0
  112. case 0xff:
  113. d[0] = s[0]
  114. d[1] = s[2]
  115. d[2] = s[4]
  116. default:
  117. r32 := uint32(s[0])<<8 | uint32(s[1])
  118. g32 := uint32(s[2])<<8 | uint32(s[3])
  119. b32 := uint32(s[4])<<8 | uint32(s[5])
  120. a32 := uint32(s[6])<<8 | uint32(s[7])
  121. d[0] = uint8((r32 * 0xffff / a32) >> 8)
  122. d[1] = uint8((g32 * 0xffff / a32) >> 8)
  123. d[2] = uint8((b32 * 0xffff / a32) >> 8)
  124. }
  125. d[3] = a
  126. j += 4
  127. i += 8
  128. }
  129. }
  130. case *image.Gray:
  131. j := 0
  132. for y := y1; y < y2; y++ {
  133. i := y*img.Stride + x1
  134. for x := x1; x < x2; x++ {
  135. c := img.Pix[i]
  136. d := dst[j : j+4 : j+4]
  137. d[0] = c
  138. d[1] = c
  139. d[2] = c
  140. d[3] = 0xff
  141. j += 4
  142. i++
  143. }
  144. }
  145. case *image.Gray16:
  146. j := 0
  147. for y := y1; y < y2; y++ {
  148. i := y*img.Stride + x1*2
  149. for x := x1; x < x2; x++ {
  150. c := img.Pix[i]
  151. d := dst[j : j+4 : j+4]
  152. d[0] = c
  153. d[1] = c
  154. d[2] = c
  155. d[3] = 0xff
  156. j += 4
  157. i += 2
  158. }
  159. }
  160. case *image.YCbCr:
  161. j := 0
  162. x1 += img.Rect.Min.X
  163. x2 += img.Rect.Min.X
  164. y1 += img.Rect.Min.Y
  165. y2 += img.Rect.Min.Y
  166. hy := img.Rect.Min.Y / 2
  167. hx := img.Rect.Min.X / 2
  168. for y := y1; y < y2; y++ {
  169. iy := (y-img.Rect.Min.Y)*img.YStride + (x1 - img.Rect.Min.X)
  170. var yBase int
  171. switch img.SubsampleRatio {
  172. case image.YCbCrSubsampleRatio444, image.YCbCrSubsampleRatio422:
  173. yBase = (y - img.Rect.Min.Y) * img.CStride
  174. case image.YCbCrSubsampleRatio420, image.YCbCrSubsampleRatio440:
  175. yBase = (y/2 - hy) * img.CStride
  176. }
  177. for x := x1; x < x2; x++ {
  178. var ic int
  179. switch img.SubsampleRatio {
  180. case image.YCbCrSubsampleRatio444, image.YCbCrSubsampleRatio440:
  181. ic = yBase + (x - img.Rect.Min.X)
  182. case image.YCbCrSubsampleRatio422, image.YCbCrSubsampleRatio420:
  183. ic = yBase + (x/2 - hx)
  184. default:
  185. ic = img.COffset(x, y)
  186. }
  187. yy1 := int32(img.Y[iy]) * 0x10101
  188. cb1 := int32(img.Cb[ic]) - 128
  189. cr1 := int32(img.Cr[ic]) - 128
  190. r := yy1 + 91881*cr1
  191. if uint32(r)&0xff000000 == 0 {
  192. r >>= 16
  193. } else {
  194. r = ^(r >> 31)
  195. }
  196. g := yy1 - 22554*cb1 - 46802*cr1
  197. if uint32(g)&0xff000000 == 0 {
  198. g >>= 16
  199. } else {
  200. g = ^(g >> 31)
  201. }
  202. b := yy1 + 116130*cb1
  203. if uint32(b)&0xff000000 == 0 {
  204. b >>= 16
  205. } else {
  206. b = ^(b >> 31)
  207. }
  208. d := dst[j : j+4 : j+4]
  209. d[0] = uint8(r)
  210. d[1] = uint8(g)
  211. d[2] = uint8(b)
  212. d[3] = 0xff
  213. iy++
  214. j += 4
  215. }
  216. }
  217. case *image.Paletted:
  218. j := 0
  219. for y := y1; y < y2; y++ {
  220. i := y*img.Stride + x1
  221. for x := x1; x < x2; x++ {
  222. c := s.palette[img.Pix[i]]
  223. d := dst[j : j+4 : j+4]
  224. d[0] = c.R
  225. d[1] = c.G
  226. d[2] = c.B
  227. d[3] = c.A
  228. j += 4
  229. i++
  230. }
  231. }
  232. default:
  233. j := 0
  234. b := s.image.Bounds()
  235. x1 += b.Min.X
  236. x2 += b.Min.X
  237. y1 += b.Min.Y
  238. y2 += b.Min.Y
  239. for y := y1; y < y2; y++ {
  240. for x := x1; x < x2; x++ {
  241. r16, g16, b16, a16 := s.image.At(x, y).RGBA()
  242. d := dst[j : j+4 : j+4]
  243. switch a16 {
  244. case 0xffff:
  245. d[0] = uint8(r16 >> 8)
  246. d[1] = uint8(g16 >> 8)
  247. d[2] = uint8(b16 >> 8)
  248. d[3] = 0xff
  249. case 0:
  250. d[0] = 0
  251. d[1] = 0
  252. d[2] = 0
  253. d[3] = 0
  254. default:
  255. d[0] = uint8(((r16 * 0xffff) / a16) >> 8)
  256. d[1] = uint8(((g16 * 0xffff) / a16) >> 8)
  257. d[2] = uint8(((b16 * 0xffff) / a16) >> 8)
  258. d[3] = uint8(a16 >> 8)
  259. }
  260. j += 4
  261. }
  262. }
  263. }
  264. }