to_rgba.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
  2. package images
  3. import (
  4. "fmt"
  5. "image"
  6. "image/color"
  7. "math"
  8. )
  9. var _ = fmt.Print
  10. type scanner struct {
  11. image image.Image
  12. w, h int
  13. palette []color.NRGBA
  14. }
  15. func (s scanner) bytes_per_pixel() int { return 4 }
  16. func (s scanner) bounds() image.Rectangle { return s.image.Bounds() }
  17. func newScanner(img image.Image) *scanner {
  18. s := &scanner{
  19. image: img,
  20. w: img.Bounds().Dx(),
  21. h: img.Bounds().Dy(),
  22. }
  23. if img, ok := img.(*image.Paletted); ok {
  24. s.palette = make([]color.NRGBA, max(256, len(img.Palette)))
  25. for i := 0; i < len(img.Palette); i++ {
  26. s.palette[i] = color.NRGBAModel.Convert(img.Palette[i]).(color.NRGBA)
  27. }
  28. }
  29. return s
  30. }
  31. // scan scans the given rectangular region of the image into dst.
  32. func (s *scanner) scan(x1, y1, x2, y2 int, dst []uint8) {
  33. switch img := s.image.(type) {
  34. case *image.NRGBA:
  35. size := (x2 - x1) * 4
  36. j := 0
  37. i := y1*img.Stride + x1*4
  38. if size == 4 {
  39. for y := y1; y < y2; y++ {
  40. d := dst[j : j+4 : j+4]
  41. s := img.Pix[i : i+4 : i+4]
  42. d[0] = s[0]
  43. d[1] = s[1]
  44. d[2] = s[2]
  45. d[3] = s[3]
  46. j += size
  47. i += img.Stride
  48. }
  49. } else {
  50. for y := y1; y < y2; y++ {
  51. copy(dst[j:j+size], img.Pix[i:i+size])
  52. j += size
  53. i += img.Stride
  54. }
  55. }
  56. case *image.NRGBA64:
  57. j := 0
  58. for y := y1; y < y2; y++ {
  59. i := y*img.Stride + x1*8
  60. for x := x1; x < x2; x++ {
  61. s := img.Pix[i : i+8 : i+8]
  62. d := dst[j : j+4 : j+4]
  63. d[0] = s[0]
  64. d[1] = s[2]
  65. d[2] = s[4]
  66. d[3] = s[6]
  67. j += 4
  68. i += 8
  69. }
  70. }
  71. case *image.RGBA:
  72. j := 0
  73. for y := y1; y < y2; y++ {
  74. i := y*img.Stride + x1*4
  75. for x := x1; x < x2; x++ {
  76. d := dst[j : j+4 : j+4]
  77. a := img.Pix[i+3]
  78. switch a {
  79. case 0:
  80. d[0] = 0
  81. d[1] = 0
  82. d[2] = 0
  83. d[3] = a
  84. case 0xff:
  85. s := img.Pix[i : i+4 : i+4]
  86. d[0] = s[0]
  87. d[1] = s[1]
  88. d[2] = s[2]
  89. d[3] = a
  90. default:
  91. s := img.Pix[i : i+4 : i+4]
  92. r16 := uint16(s[0])
  93. g16 := uint16(s[1])
  94. b16 := uint16(s[2])
  95. a16 := uint16(a)
  96. d[0] = uint8(r16 * 0xff / a16)
  97. d[1] = uint8(g16 * 0xff / a16)
  98. d[2] = uint8(b16 * 0xff / a16)
  99. d[3] = a
  100. }
  101. j += 4
  102. i += 4
  103. }
  104. }
  105. case *image.RGBA64:
  106. j := 0
  107. for y := y1; y < y2; y++ {
  108. i := y*img.Stride + x1*8
  109. for x := x1; x < x2; x++ {
  110. s := img.Pix[i : i+8 : i+8]
  111. d := dst[j : j+4 : j+4]
  112. a := s[6]
  113. switch a {
  114. case 0:
  115. d[0] = 0
  116. d[1] = 0
  117. d[2] = 0
  118. case 0xff:
  119. d[0] = s[0]
  120. d[1] = s[2]
  121. d[2] = s[4]
  122. default:
  123. r32 := uint32(s[0])<<8 | uint32(s[1])
  124. g32 := uint32(s[2])<<8 | uint32(s[3])
  125. b32 := uint32(s[4])<<8 | uint32(s[5])
  126. a32 := uint32(s[6])<<8 | uint32(s[7])
  127. d[0] = uint8((r32 * 0xffff / a32) >> 8)
  128. d[1] = uint8((g32 * 0xffff / a32) >> 8)
  129. d[2] = uint8((b32 * 0xffff / a32) >> 8)
  130. }
  131. d[3] = a
  132. j += 4
  133. i += 8
  134. }
  135. }
  136. case *image.Gray:
  137. j := 0
  138. for y := y1; y < y2; y++ {
  139. i := y*img.Stride + x1
  140. for x := x1; x < x2; x++ {
  141. c := img.Pix[i]
  142. d := dst[j : j+4 : j+4]
  143. d[0] = c
  144. d[1] = c
  145. d[2] = c
  146. d[3] = 0xff
  147. j += 4
  148. i++
  149. }
  150. }
  151. case *image.Gray16:
  152. j := 0
  153. for y := y1; y < y2; y++ {
  154. i := y*img.Stride + x1*2
  155. for x := x1; x < x2; x++ {
  156. c := img.Pix[i]
  157. d := dst[j : j+4 : j+4]
  158. d[0] = c
  159. d[1] = c
  160. d[2] = c
  161. d[3] = 0xff
  162. j += 4
  163. i += 2
  164. }
  165. }
  166. case *image.YCbCr:
  167. j := 0
  168. x1 += img.Rect.Min.X
  169. x2 += img.Rect.Min.X
  170. y1 += img.Rect.Min.Y
  171. y2 += img.Rect.Min.Y
  172. hy := img.Rect.Min.Y / 2
  173. hx := img.Rect.Min.X / 2
  174. for y := y1; y < y2; y++ {
  175. iy := (y-img.Rect.Min.Y)*img.YStride + (x1 - img.Rect.Min.X)
  176. var yBase int
  177. switch img.SubsampleRatio {
  178. case image.YCbCrSubsampleRatio444, image.YCbCrSubsampleRatio422:
  179. yBase = (y - img.Rect.Min.Y) * img.CStride
  180. case image.YCbCrSubsampleRatio420, image.YCbCrSubsampleRatio440:
  181. yBase = (y/2 - hy) * img.CStride
  182. }
  183. for x := x1; x < x2; x++ {
  184. var ic int
  185. switch img.SubsampleRatio {
  186. case image.YCbCrSubsampleRatio444, image.YCbCrSubsampleRatio440:
  187. ic = yBase + (x - img.Rect.Min.X)
  188. case image.YCbCrSubsampleRatio422, image.YCbCrSubsampleRatio420:
  189. ic = yBase + (x/2 - hx)
  190. default:
  191. ic = img.COffset(x, y)
  192. }
  193. yy1 := int32(img.Y[iy]) * 0x10101
  194. cb1 := int32(img.Cb[ic]) - 128
  195. cr1 := int32(img.Cr[ic]) - 128
  196. r := yy1 + 91881*cr1
  197. if uint32(r)&0xff000000 == 0 {
  198. r >>= 16
  199. } else {
  200. r = ^(r >> 31)
  201. }
  202. g := yy1 - 22554*cb1 - 46802*cr1
  203. if uint32(g)&0xff000000 == 0 {
  204. g >>= 16
  205. } else {
  206. g = ^(g >> 31)
  207. }
  208. b := yy1 + 116130*cb1
  209. if uint32(b)&0xff000000 == 0 {
  210. b >>= 16
  211. } else {
  212. b = ^(b >> 31)
  213. }
  214. d := dst[j : j+4 : j+4]
  215. d[0] = uint8(r)
  216. d[1] = uint8(g)
  217. d[2] = uint8(b)
  218. d[3] = 0xff
  219. iy++
  220. j += 4
  221. }
  222. }
  223. case *image.Paletted:
  224. j := 0
  225. for y := y1; y < y2; y++ {
  226. i := y*img.Stride + x1
  227. for x := x1; x < x2; x++ {
  228. c := s.palette[img.Pix[i]]
  229. d := dst[j : j+4 : j+4]
  230. d[0] = c.R
  231. d[1] = c.G
  232. d[2] = c.B
  233. d[3] = c.A
  234. j += 4
  235. i++
  236. }
  237. }
  238. default:
  239. j := 0
  240. b := s.image.Bounds()
  241. x1 += b.Min.X
  242. x2 += b.Min.X
  243. y1 += b.Min.Y
  244. y2 += b.Min.Y
  245. for y := y1; y < y2; y++ {
  246. for x := x1; x < x2; x++ {
  247. r16, g16, b16, a16 := s.image.At(x, y).RGBA()
  248. d := dst[j : j+4 : j+4]
  249. switch a16 {
  250. case 0xffff:
  251. d[0] = uint8(r16 >> 8)
  252. d[1] = uint8(g16 >> 8)
  253. d[2] = uint8(b16 >> 8)
  254. d[3] = 0xff
  255. case 0:
  256. d[0] = 0
  257. d[1] = 0
  258. d[2] = 0
  259. d[3] = 0
  260. default:
  261. d[0] = uint8(((r16 * 0xffff) / a16) >> 8)
  262. d[1] = uint8(((g16 * 0xffff) / a16) >> 8)
  263. d[2] = uint8(((b16 * 0xffff) / a16) >> 8)
  264. d[3] = uint8(a16 >> 8)
  265. }
  266. j += 4
  267. }
  268. }
  269. }
  270. }
  271. type Scanner interface {
  272. scan(x1, y1, x2, y2 int, dst []uint8)
  273. bytes_per_pixel() int
  274. bounds() image.Rectangle
  275. }
  276. func (self *Context) run_paste(src Scanner, background image.Image, pos image.Point, postprocess func([]byte)) {
  277. pos = pos.Sub(background.Bounds().Min)
  278. pasteRect := image.Rectangle{Min: pos, Max: pos.Add(src.bounds().Size())}
  279. interRect := pasteRect.Intersect(background.Bounds())
  280. if interRect.Empty() {
  281. return
  282. }
  283. bytes_per_pixel := src.bytes_per_pixel()
  284. var stride int
  285. var pix []uint8
  286. switch v := background.(type) {
  287. case *image.NRGBA:
  288. i := background.(*image.NRGBA)
  289. stride = i.Stride
  290. pix = i.Pix
  291. case *NRGB:
  292. i := background.(*NRGB)
  293. stride = i.Stride
  294. pix = i.Pix
  295. default:
  296. panic(fmt.Sprintf("Unsupported image type: %v", v))
  297. }
  298. self.Parallel(interRect.Min.Y, interRect.Max.Y, func(ys <-chan int) {
  299. for y := range ys {
  300. x1 := interRect.Min.X - pasteRect.Min.X
  301. x2 := interRect.Max.X - pasteRect.Min.X
  302. y1 := y - pasteRect.Min.Y
  303. y2 := y1 + 1
  304. i1 := y*stride + interRect.Min.X*bytes_per_pixel
  305. i2 := i1 + interRect.Dx()*bytes_per_pixel
  306. dst := pix[i1:i2]
  307. src.scan(x1, y1, x2, y2, dst)
  308. postprocess(dst)
  309. }
  310. })
  311. }
  312. func (self *Context) paste_nrgba_onto_opaque(background *image.NRGBA, img image.Image, pos image.Point, bgcol *NRGBColor) {
  313. src := newScanner(img)
  314. if bgcol == nil {
  315. self.run_paste(src, background, pos, func([]byte) {})
  316. return
  317. }
  318. bg := [3]float64{float64(bgcol.R), float64(bgcol.G), float64(bgcol.B)}
  319. self.run_paste(src, background, pos, func(dst []byte) {
  320. for len(dst) > 0 {
  321. a := float64(dst[3]) / 255.0
  322. for i := range dst[:3] {
  323. // uint8() automatically converts floats greater than 255 but less than 256 to 255
  324. dst[i] = uint8(float64(dst[i])*a + bg[i]*(1-a))
  325. }
  326. dst[3] = 255
  327. dst = dst[4:]
  328. }
  329. })
  330. }
  331. // Paste pastes the img image to the background image at the specified position. Optionally composing onto the specified opaque color.
  332. func (self *Context) Paste(background image.Image, img image.Image, pos image.Point, opaque_bg *NRGBColor) {
  333. switch b := background.(type) {
  334. case *image.NRGBA:
  335. self.paste_nrgba_onto_opaque(b, img, pos, opaque_bg)
  336. case *NRGB:
  337. self.paste_nrgb_onto_opaque(b, img, pos, opaque_bg)
  338. default:
  339. panic("Unsupported background image type")
  340. }
  341. }
  342. // PasteCenter pastes the img image to the center of the background image. Optionally composing onto the specified opaque color.
  343. func (self *Context) PasteCenter(background image.Image, img image.Image, opaque_bg *NRGBColor) {
  344. bgBounds := background.Bounds()
  345. bgW := bgBounds.Dx()
  346. bgH := bgBounds.Dy()
  347. bgMinX := bgBounds.Min.X
  348. bgMinY := bgBounds.Min.Y
  349. centerX := bgMinX + bgW/2
  350. centerY := bgMinY + bgH/2
  351. x0 := centerX - img.Bounds().Dx()/2
  352. y0 := centerY - img.Bounds().Dy()/2
  353. self.Paste(background, img, image.Pt(x0, y0), opaque_bg)
  354. }
  355. func FitImage(width, height, pwidth, pheight int) (final_width int, final_height int) {
  356. if height > pheight {
  357. corrf := float64(pheight) / float64(height)
  358. width, height = int(math.Floor(corrf*float64(width))), pheight
  359. }
  360. if width > pwidth {
  361. corrf := float64(pwidth) / float64(width)
  362. width, height = pwidth, int(math.Floor(corrf*float64(height)))
  363. }
  364. if height > pheight {
  365. corrf := float64(pheight) / float64(height)
  366. width, height = int(math.Floor(corrf*float64(width))), pheight
  367. }
  368. return width, height
  369. }