to_rgb.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package images
  3. import (
  4. "fmt"
  5. "image"
  6. "image/color"
  7. )
  8. var _ = fmt.Print
  9. type NRGBColor struct {
  10. R, G, B uint8
  11. }
  12. func (c NRGBColor) AsSharp() string {
  13. return fmt.Sprintf("#%02X%02X%02X", c.R, c.G, c.B)
  14. }
  15. func (c NRGBColor) RGBA() (r, g, b, a uint32) {
  16. r = uint32(c.R)
  17. r |= r << 8
  18. g = uint32(c.G)
  19. g |= g << 8
  20. b = uint32(c.B)
  21. b |= b << 8
  22. a = 65280 // ( 255 << 8 )
  23. return
  24. }
  25. // NRGB is an in-memory image whose At method returns NRGBColor values.
  26. type NRGB struct {
  27. // Pix holds the image's pixels, in R, G, B, A order. The pixel at
  28. // (x, y) starts at Pix[(y-Rect.Min.Y)*Stride + (x-Rect.Min.X)*4].
  29. Pix []uint8
  30. // Stride is the Pix stride (in bytes) between vertically adjacent pixels.
  31. Stride int
  32. // Rect is the image's bounds.
  33. Rect image.Rectangle
  34. }
  35. func nrgbModel(c color.Color) color.Color {
  36. if _, ok := c.(NRGBColor); ok {
  37. return c
  38. }
  39. r, g, b, a := c.RGBA()
  40. if a == 0xffff {
  41. return NRGBColor{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)}
  42. }
  43. if a == 0 {
  44. return NRGBColor{0, 0, 0}
  45. }
  46. // Since Color.RGBA returns an alpha-premultiplied color, we should have r <= a && g <= a && b <= a.
  47. r = (r * 0xffff) / a
  48. g = (g * 0xffff) / a
  49. b = (b * 0xffff) / a
  50. return NRGBColor{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8)}
  51. }
  52. var NRGBModel color.Model = color.ModelFunc(nrgbModel)
  53. func (p *NRGB) ColorModel() color.Model { return NRGBModel }
  54. func (p *NRGB) Bounds() image.Rectangle { return p.Rect }
  55. func (p *NRGB) At(x, y int) color.Color {
  56. return p.NRGBAt(x, y)
  57. }
  58. func (p *NRGB) NRGBAt(x, y int) NRGBColor {
  59. if !(image.Point{x, y}.In(p.Rect)) {
  60. return NRGBColor{}
  61. }
  62. i := p.PixOffset(x, y)
  63. s := p.Pix[i : i+4 : i+4] // Small cap improves performance, see https://golang.org/issue/27857
  64. return NRGBColor{s[0], s[1], s[2]}
  65. }
  66. // PixOffset returns the index of the first element of Pix that corresponds to
  67. // the pixel at (x, y).
  68. func (p *NRGB) PixOffset(x, y int) int {
  69. return (y-p.Rect.Min.Y)*p.Stride + (x-p.Rect.Min.X)*4
  70. }
  71. func (p *NRGB) Set(x, y int, c color.Color) {
  72. if !(image.Point{x, y}.In(p.Rect)) {
  73. return
  74. }
  75. i := p.PixOffset(x, y)
  76. c1 := NRGBModel.Convert(c).(NRGBColor)
  77. s := p.Pix[i : i+3 : i+3] // Small cap improves performance, see https://golang.org/issue/27857
  78. s[0] = c1.R
  79. s[1] = c1.G
  80. s[2] = c1.B
  81. }
  82. func (p *NRGB) SetRGBA64(x, y int, c color.RGBA64) {
  83. if !(image.Point{x, y}.In(p.Rect)) {
  84. return
  85. }
  86. r, g, b, a := uint32(c.R), uint32(c.G), uint32(c.B), uint32(c.A)
  87. if (a != 0) && (a != 0xffff) {
  88. r = (r * 0xffff) / a
  89. g = (g * 0xffff) / a
  90. b = (b * 0xffff) / a
  91. }
  92. i := p.PixOffset(x, y)
  93. s := p.Pix[i : i+3 : i+3] // Small cap improves performance, see https://golang.org/issue/27857
  94. s[0] = uint8(r >> 8)
  95. s[1] = uint8(g >> 8)
  96. s[2] = uint8(b >> 8)
  97. }
  98. func (p *NRGB) SetNRGBA(x, y int, c color.NRGBA) {
  99. if !(image.Point{x, y}.In(p.Rect)) {
  100. return
  101. }
  102. i := p.PixOffset(x, y)
  103. s := p.Pix[i : i+3 : i+3] // Small cap improves performance, see https://golang.org/issue/27857
  104. s[0] = c.R
  105. s[1] = c.G
  106. s[2] = c.B
  107. }
  108. // SubImage returns an image representing the portion of the image p visible
  109. // through r. The returned value shares pixels with the original image.
  110. func (p *NRGB) SubImage(r image.Rectangle) image.Image {
  111. r = r.Intersect(p.Rect)
  112. // If r1 and r2 are Rectangles, r1.Intersect(r2) is not guaranteed to be inside
  113. // either r1 or r2 if the intersection is empty. Without explicitly checking for
  114. // this, the Pix[i:] expression below can panic.
  115. if r.Empty() {
  116. return &NRGB{}
  117. }
  118. i := p.PixOffset(r.Min.X, r.Min.Y)
  119. return &NRGB{
  120. Pix: p.Pix[i:],
  121. Stride: p.Stride,
  122. Rect: r,
  123. }
  124. }
  125. // Opaque scans the entire image and reports whether it is fully opaque.
  126. func (p *NRGB) Opaque() bool { return true }
  127. type scanner_rgb struct {
  128. image image.Image
  129. w, h int
  130. palette []NRGBColor
  131. opaque_base []float64
  132. opaque_base_uint []uint8
  133. }
  134. func (s scanner_rgb) bytes_per_pixel() int { return 3 }
  135. func (s scanner_rgb) bounds() image.Rectangle { return s.image.Bounds() }
  136. func blend(dest []uint8, base []float64, r, g, b, a uint8) {
  137. alpha := float64(a) / 255.0
  138. dest[0] = uint8(alpha*float64(r) + (1.0-alpha)*base[0])
  139. dest[1] = uint8(alpha*float64(g) + (1.0-alpha)*base[1])
  140. dest[2] = uint8(alpha*float64(b) + (1.0-alpha)*base[2])
  141. }
  142. func newScannerRGB(img image.Image, opaque_base NRGBColor) *scanner_rgb {
  143. s := &scanner_rgb{
  144. image: img, w: img.Bounds().Dx(), h: img.Bounds().Dy(),
  145. opaque_base: []float64{float64(opaque_base.R), float64(opaque_base.G), float64(opaque_base.B)}[0:3:3],
  146. opaque_base_uint: []uint8{opaque_base.R, opaque_base.G, opaque_base.B}[0:3:3],
  147. }
  148. if img, ok := img.(*image.Paletted); ok {
  149. s.palette = make([]NRGBColor, max(256, len(img.Palette)))
  150. d := make([]uint8, 3)
  151. for i := 0; i < len(img.Palette); i++ {
  152. r, g, b, a := img.Palette[i].RGBA()
  153. switch a {
  154. case 0:
  155. s.palette[i] = opaque_base
  156. default:
  157. blend(d, s.opaque_base, uint8((r*0xffff/a)>>8), uint8((g*0xffff/a)>>8), uint8((b*0xffff/a)>>8), uint8(a>>8))
  158. s.palette[i] = NRGBColor{d[0], d[1], d[2]}
  159. }
  160. }
  161. }
  162. return s
  163. }
  164. // scan scans the given rectangular region of the image into dst.
  165. func (s *scanner_rgb) scan(x1, y1, x2, y2 int, dst []uint8) {
  166. switch img := s.image.(type) {
  167. case *image.NRGBA:
  168. j := 0
  169. for y := y1; y < y2; y++ {
  170. i := y*img.Stride + x1*4
  171. for x := x1; x < x2; x++ {
  172. blend(dst[j:j+3:j+3], s.opaque_base, img.Pix[i], img.Pix[i+1], img.Pix[i+2], img.Pix[i+3])
  173. j += 3
  174. i += 4
  175. }
  176. }
  177. case *image.NRGBA64:
  178. j := 0
  179. for y := y1; y < y2; y++ {
  180. i := y*img.Stride + x1*8
  181. for x := x1; x < x2; x++ {
  182. blend(dst[j:j+3:j+3], s.opaque_base, img.Pix[i], img.Pix[i+2], img.Pix[i+4], img.Pix[i+6])
  183. j += 3
  184. i += 8
  185. }
  186. }
  187. case *image.RGBA:
  188. j := 0
  189. for y := y1; y < y2; y++ {
  190. i := y*img.Stride + x1*4
  191. for x := x1; x < x2; x++ {
  192. d := dst[j : j+3 : j+3]
  193. a := img.Pix[i+3]
  194. switch a {
  195. case 0:
  196. d[0] = s.opaque_base_uint[0]
  197. d[1] = s.opaque_base_uint[1]
  198. d[2] = s.opaque_base_uint[2]
  199. case 0xff:
  200. s := img.Pix[i : i+3 : i+3]
  201. d[0] = s[0]
  202. d[1] = s[1]
  203. d[2] = s[2]
  204. default:
  205. r16 := uint16(img.Pix[i])
  206. g16 := uint16(img.Pix[i+1])
  207. b16 := uint16(img.Pix[i+2])
  208. a16 := uint16(a)
  209. blend(d, s.opaque_base, uint8(r16*0xff/a16), uint8(g16*0xff/a16), uint8(b16*0xff/a16), a)
  210. }
  211. j += 3
  212. i += 4
  213. }
  214. }
  215. case *image.RGBA64:
  216. j := 0
  217. for y := y1; y < y2; y++ {
  218. i := y*img.Stride + x1*8
  219. for x := x1; x < x2; x++ {
  220. src := img.Pix[i : i+8 : i+8]
  221. d := dst[j : j+3 : j+3]
  222. a := src[6]
  223. switch a {
  224. case 0:
  225. d[0] = s.opaque_base_uint[0]
  226. d[1] = s.opaque_base_uint[1]
  227. d[2] = s.opaque_base_uint[2]
  228. case 0xff:
  229. d[0] = src[0]
  230. d[1] = src[2]
  231. d[2] = src[4]
  232. default:
  233. r32 := uint32(src[0])<<8 | uint32(src[1])
  234. g32 := uint32(src[2])<<8 | uint32(src[3])
  235. b32 := uint32(src[4])<<8 | uint32(src[5])
  236. a32 := uint32(src[6])<<8 | uint32(src[7])
  237. blend(d, s.opaque_base, uint8((r32*0xffff/a32)>>8), uint8((g32*0xffff/a32)>>8), uint8((b32*0xffff/a32)>>8), a)
  238. }
  239. j += 3
  240. i += 8
  241. }
  242. }
  243. case *image.Gray:
  244. j := 0
  245. for y := y1; y < y2; y++ {
  246. i := y*img.Stride + x1
  247. for x := x1; x < x2; x++ {
  248. c := img.Pix[i]
  249. d := dst[j : j+3 : j+3]
  250. d[0] = c
  251. d[1] = c
  252. d[2] = c
  253. j += 3
  254. i++
  255. }
  256. }
  257. case *image.Gray16:
  258. j := 0
  259. for y := y1; y < y2; y++ {
  260. i := y*img.Stride + x1*2
  261. for x := x1; x < x2; x++ {
  262. c := img.Pix[i]
  263. d := dst[j : j+3 : j+3]
  264. d[0] = c
  265. d[1] = c
  266. d[2] = c
  267. j += 3
  268. i += 2
  269. }
  270. }
  271. case *image.YCbCr:
  272. j := 0
  273. x1 += img.Rect.Min.X
  274. x2 += img.Rect.Min.X
  275. y1 += img.Rect.Min.Y
  276. y2 += img.Rect.Min.Y
  277. hy := img.Rect.Min.Y / 2
  278. hx := img.Rect.Min.X / 2
  279. for y := y1; y < y2; y++ {
  280. iy := (y-img.Rect.Min.Y)*img.YStride + (x1 - img.Rect.Min.X)
  281. var yBase int
  282. switch img.SubsampleRatio {
  283. case image.YCbCrSubsampleRatio444, image.YCbCrSubsampleRatio422:
  284. yBase = (y - img.Rect.Min.Y) * img.CStride
  285. case image.YCbCrSubsampleRatio420, image.YCbCrSubsampleRatio440:
  286. yBase = (y/2 - hy) * img.CStride
  287. }
  288. for x := x1; x < x2; x++ {
  289. var ic int
  290. switch img.SubsampleRatio {
  291. case image.YCbCrSubsampleRatio444, image.YCbCrSubsampleRatio440:
  292. ic = yBase + (x - img.Rect.Min.X)
  293. case image.YCbCrSubsampleRatio422, image.YCbCrSubsampleRatio420:
  294. ic = yBase + (x/2 - hx)
  295. default:
  296. ic = img.COffset(x, y)
  297. }
  298. yy1 := int32(img.Y[iy]) * 0x10101
  299. cb1 := int32(img.Cb[ic]) - 128
  300. cr1 := int32(img.Cr[ic]) - 128
  301. r := yy1 + 91881*cr1
  302. if uint32(r)&0xff000000 == 0 {
  303. r >>= 16
  304. } else {
  305. r = ^(r >> 31)
  306. }
  307. g := yy1 - 22554*cb1 - 46802*cr1
  308. if uint32(g)&0xff000000 == 0 {
  309. g >>= 16
  310. } else {
  311. g = ^(g >> 31)
  312. }
  313. b := yy1 + 116130*cb1
  314. if uint32(b)&0xff000000 == 0 {
  315. b >>= 16
  316. } else {
  317. b = ^(b >> 31)
  318. }
  319. d := dst[j : j+3 : j+3]
  320. d[0] = uint8(r)
  321. d[1] = uint8(g)
  322. d[2] = uint8(b)
  323. iy++
  324. j += 3
  325. }
  326. }
  327. case *image.Paletted:
  328. j := 0
  329. for y := y1; y < y2; y++ {
  330. i := y*img.Stride + x1
  331. for x := x1; x < x2; x++ {
  332. c := s.palette[img.Pix[i]]
  333. d := dst[j : j+3 : j+3]
  334. d[0] = c.R
  335. d[1] = c.G
  336. d[2] = c.B
  337. j += 3
  338. i++
  339. }
  340. }
  341. default:
  342. j := 0
  343. b := s.image.Bounds()
  344. x1 += b.Min.X
  345. x2 += b.Min.X
  346. y1 += b.Min.Y
  347. y2 += b.Min.Y
  348. for y := y1; y < y2; y++ {
  349. for x := x1; x < x2; x++ {
  350. r16, g16, b16, a16 := s.image.At(x, y).RGBA()
  351. d := dst[j : j+3 : j+3]
  352. switch a16 {
  353. case 0xffff:
  354. d[0] = uint8(r16 >> 8)
  355. d[1] = uint8(g16 >> 8)
  356. d[2] = uint8(b16 >> 8)
  357. case 0:
  358. d[0] = s.opaque_base_uint[0]
  359. d[1] = s.opaque_base_uint[1]
  360. d[2] = s.opaque_base_uint[2]
  361. default:
  362. blend(d, s.opaque_base, uint8(((r16*0xffff)/a16)>>8), uint8(((g16*0xffff)/a16)>>8), uint8(((b16*0xffff)/a16)>>8), uint8(a16>>8))
  363. }
  364. j += 3
  365. }
  366. }
  367. }
  368. }
  369. func (self *Context) paste_nrgb_onto_opaque(background *NRGB, img image.Image, pos image.Point, bgcol *NRGBColor) {
  370. bg := NRGBColor{}
  371. if bgcol != nil {
  372. bg = *bgcol
  373. }
  374. src := newScannerRGB(img, bg)
  375. self.run_paste(src, background, pos, func(dst []byte) {})
  376. }
  377. func NewNRGB(r image.Rectangle) *NRGB {
  378. return &NRGB{
  379. Pix: make([]uint8, 3*r.Dx()*r.Dy()),
  380. Stride: 3 * r.Dx(),
  381. Rect: r,
  382. }
  383. }