draw_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package draw
  5. import (
  6. "image"
  7. "image/color"
  8. "image/png"
  9. "os"
  10. "testing"
  11. )
  12. func eq(c0, c1 color.Color) bool {
  13. r0, g0, b0, a0 := c0.RGBA()
  14. r1, g1, b1, a1 := c1.RGBA()
  15. return r0 == r1 && g0 == g1 && b0 == b1 && a0 == a1
  16. }
  17. func fillBlue(alpha int) image.Image {
  18. return image.NewUniform(color.RGBA{0, 0, uint8(alpha), uint8(alpha)})
  19. }
  20. func fillAlpha(alpha int) image.Image {
  21. return image.NewUniform(color.Alpha{uint8(alpha)})
  22. }
  23. func vgradGreen(alpha int) image.Image {
  24. m := image.NewRGBA(image.Rect(0, 0, 16, 16))
  25. for y := 0; y < 16; y++ {
  26. for x := 0; x < 16; x++ {
  27. m.Set(x, y, color.RGBA{0, uint8(y * alpha / 15), 0, uint8(alpha)})
  28. }
  29. }
  30. return m
  31. }
  32. func vgradAlpha(alpha int) image.Image {
  33. m := image.NewAlpha(image.Rect(0, 0, 16, 16))
  34. for y := 0; y < 16; y++ {
  35. for x := 0; x < 16; x++ {
  36. m.Set(x, y, color.Alpha{uint8(y * alpha / 15)})
  37. }
  38. }
  39. return m
  40. }
  41. func vgradGreenNRGBA(alpha int) image.Image {
  42. m := image.NewNRGBA(image.Rect(0, 0, 16, 16))
  43. for y := 0; y < 16; y++ {
  44. for x := 0; x < 16; x++ {
  45. m.Set(x, y, color.RGBA{0, uint8(y * 0x11), 0, uint8(alpha)})
  46. }
  47. }
  48. return m
  49. }
  50. func vgradCr() image.Image {
  51. m := &image.YCbCr{
  52. Y: make([]byte, 16*16),
  53. Cb: make([]byte, 16*16),
  54. Cr: make([]byte, 16*16),
  55. YStride: 16,
  56. CStride: 16,
  57. SubsampleRatio: image.YCbCrSubsampleRatio444,
  58. Rect: image.Rect(0, 0, 16, 16),
  59. }
  60. for y := 0; y < 16; y++ {
  61. for x := 0; x < 16; x++ {
  62. m.Cr[y*m.CStride+x] = uint8(y * 0x11)
  63. }
  64. }
  65. return m
  66. }
  67. func hgradRed(alpha int) Image {
  68. m := image.NewRGBA(image.Rect(0, 0, 16, 16))
  69. for y := 0; y < 16; y++ {
  70. for x := 0; x < 16; x++ {
  71. m.Set(x, y, color.RGBA{uint8(x * alpha / 15), 0, 0, uint8(alpha)})
  72. }
  73. }
  74. return m
  75. }
  76. func gradYellow(alpha int) Image {
  77. m := image.NewRGBA(image.Rect(0, 0, 16, 16))
  78. for y := 0; y < 16; y++ {
  79. for x := 0; x < 16; x++ {
  80. m.Set(x, y, color.RGBA{uint8(x * alpha / 15), uint8(y * alpha / 15), 0, uint8(alpha)})
  81. }
  82. }
  83. return m
  84. }
  85. type drawTest struct {
  86. desc string
  87. src image.Image
  88. mask image.Image
  89. op Op
  90. expected color.Color
  91. }
  92. var drawTests = []drawTest{
  93. // Uniform mask (0% opaque).
  94. {"nop", vgradGreen(255), fillAlpha(0), Over, color.RGBA{136, 0, 0, 255}},
  95. {"clear", vgradGreen(255), fillAlpha(0), Src, color.RGBA{0, 0, 0, 0}},
  96. // Uniform mask (100%, 75%, nil) and uniform source.
  97. // At (x, y) == (8, 8):
  98. // The destination pixel is {136, 0, 0, 255}.
  99. // The source pixel is {0, 0, 90, 90}.
  100. {"fill", fillBlue(90), fillAlpha(255), Over, color.RGBA{88, 0, 90, 255}},
  101. {"fillSrc", fillBlue(90), fillAlpha(255), Src, color.RGBA{0, 0, 90, 90}},
  102. {"fillAlpha", fillBlue(90), fillAlpha(192), Over, color.RGBA{100, 0, 68, 255}},
  103. {"fillAlphaSrc", fillBlue(90), fillAlpha(192), Src, color.RGBA{0, 0, 68, 68}},
  104. {"fillNil", fillBlue(90), nil, Over, color.RGBA{88, 0, 90, 255}},
  105. {"fillNilSrc", fillBlue(90), nil, Src, color.RGBA{0, 0, 90, 90}},
  106. // Uniform mask (100%, 75%, nil) and variable source.
  107. // At (x, y) == (8, 8):
  108. // The destination pixel is {136, 0, 0, 255}.
  109. // The source pixel is {0, 48, 0, 90}.
  110. {"copy", vgradGreen(90), fillAlpha(255), Over, color.RGBA{88, 48, 0, 255}},
  111. {"copySrc", vgradGreen(90), fillAlpha(255), Src, color.RGBA{0, 48, 0, 90}},
  112. {"copyAlpha", vgradGreen(90), fillAlpha(192), Over, color.RGBA{100, 36, 0, 255}},
  113. {"copyAlphaSrc", vgradGreen(90), fillAlpha(192), Src, color.RGBA{0, 36, 0, 68}},
  114. {"copyNil", vgradGreen(90), nil, Over, color.RGBA{88, 48, 0, 255}},
  115. {"copyNilSrc", vgradGreen(90), nil, Src, color.RGBA{0, 48, 0, 90}},
  116. // Uniform mask (100%, 75%, nil) and variable NRGBA source.
  117. // At (x, y) == (8, 8):
  118. // The destination pixel is {136, 0, 0, 255}.
  119. // The source pixel is {0, 136, 0, 90} in NRGBA-space, which is {0, 48, 0, 90} in RGBA-space.
  120. // The result pixel is different than in the "copy*" test cases because of rounding errors.
  121. {"nrgba", vgradGreenNRGBA(90), fillAlpha(255), Over, color.RGBA{88, 46, 0, 255}},
  122. {"nrgbaSrc", vgradGreenNRGBA(90), fillAlpha(255), Src, color.RGBA{0, 46, 0, 90}},
  123. {"nrgbaAlpha", vgradGreenNRGBA(90), fillAlpha(192), Over, color.RGBA{100, 34, 0, 255}},
  124. {"nrgbaAlphaSrc", vgradGreenNRGBA(90), fillAlpha(192), Src, color.RGBA{0, 34, 0, 68}},
  125. {"nrgbaNil", vgradGreenNRGBA(90), nil, Over, color.RGBA{88, 46, 0, 255}},
  126. {"nrgbaNilSrc", vgradGreenNRGBA(90), nil, Src, color.RGBA{0, 46, 0, 90}},
  127. // Uniform mask (100%, 75%, nil) and variable YCbCr source.
  128. // At (x, y) == (8, 8):
  129. // The destination pixel is {136, 0, 0, 255}.
  130. // The source pixel is {0, 0, 136} in YCbCr-space, which is {11, 38, 0, 255} in RGB-space.
  131. {"ycbcr", vgradCr(), fillAlpha(255), Over, color.RGBA{11, 38, 0, 255}},
  132. {"ycbcrSrc", vgradCr(), fillAlpha(255), Src, color.RGBA{11, 38, 0, 255}},
  133. {"ycbcrAlpha", vgradCr(), fillAlpha(192), Over, color.RGBA{42, 28, 0, 255}},
  134. {"ycbcrAlphaSrc", vgradCr(), fillAlpha(192), Src, color.RGBA{8, 28, 0, 192}},
  135. {"ycbcrNil", vgradCr(), nil, Over, color.RGBA{11, 38, 0, 255}},
  136. {"ycbcrNilSrc", vgradCr(), nil, Src, color.RGBA{11, 38, 0, 255}},
  137. // Variable mask and variable source.
  138. // At (x, y) == (8, 8):
  139. // The destination pixel is {136, 0, 0, 255}.
  140. // The source pixel is {0, 0, 255, 255}.
  141. // The mask pixel's alpha is 102, or 40%.
  142. {"generic", fillBlue(255), vgradAlpha(192), Over, color.RGBA{81, 0, 102, 255}},
  143. {"genericSrc", fillBlue(255), vgradAlpha(192), Src, color.RGBA{0, 0, 102, 102}},
  144. }
  145. func makeGolden(dst image.Image, r image.Rectangle, src image.Image, sp image.Point, mask image.Image, mp image.Point, op Op) image.Image {
  146. // Since golden is a newly allocated image, we don't have to check if the
  147. // input source and mask images and the output golden image overlap.
  148. b := dst.Bounds()
  149. sb := src.Bounds()
  150. mb := image.Rect(-1e9, -1e9, 1e9, 1e9)
  151. if mask != nil {
  152. mb = mask.Bounds()
  153. }
  154. golden := image.NewRGBA(image.Rect(0, 0, b.Max.X, b.Max.Y))
  155. for y := r.Min.Y; y < r.Max.Y; y++ {
  156. sy := y + sp.Y - r.Min.Y
  157. my := y + mp.Y - r.Min.Y
  158. for x := r.Min.X; x < r.Max.X; x++ {
  159. if !(image.Pt(x, y).In(b)) {
  160. continue
  161. }
  162. sx := x + sp.X - r.Min.X
  163. if !(image.Pt(sx, sy).In(sb)) {
  164. continue
  165. }
  166. mx := x + mp.X - r.Min.X
  167. if !(image.Pt(mx, my).In(mb)) {
  168. continue
  169. }
  170. const M = 1<<16 - 1
  171. var dr, dg, db, da uint32
  172. if op == Over {
  173. dr, dg, db, da = dst.At(x, y).RGBA()
  174. }
  175. sr, sg, sb, sa := src.At(sx, sy).RGBA()
  176. ma := uint32(M)
  177. if mask != nil {
  178. _, _, _, ma = mask.At(mx, my).RGBA()
  179. }
  180. a := M - (sa * ma / M)
  181. golden.Set(x, y, color.RGBA64{
  182. uint16((dr*a + sr*ma) / M),
  183. uint16((dg*a + sg*ma) / M),
  184. uint16((db*a + sb*ma) / M),
  185. uint16((da*a + sa*ma) / M),
  186. })
  187. }
  188. }
  189. return golden.SubImage(b)
  190. }
  191. func TestDraw(t *testing.T) {
  192. rr := []image.Rectangle{
  193. image.Rect(0, 0, 0, 0),
  194. image.Rect(0, 0, 16, 16),
  195. image.Rect(3, 5, 12, 10),
  196. image.Rect(0, 0, 9, 9),
  197. image.Rect(8, 8, 16, 16),
  198. image.Rect(8, 0, 9, 16),
  199. image.Rect(0, 8, 16, 9),
  200. image.Rect(8, 8, 9, 9),
  201. image.Rect(8, 8, 8, 8),
  202. }
  203. for _, r := range rr {
  204. loop:
  205. for _, test := range drawTests {
  206. dst := hgradRed(255).(*image.RGBA).SubImage(r).(Image)
  207. // Draw the (src, mask, op) onto a copy of dst using a slow but obviously correct implementation.
  208. golden := makeGolden(dst, image.Rect(0, 0, 16, 16), test.src, image.ZP, test.mask, image.ZP, test.op)
  209. b := dst.Bounds()
  210. if !b.Eq(golden.Bounds()) {
  211. t.Errorf("draw %v %s: bounds %v versus %v", r, test.desc, dst.Bounds(), golden.Bounds())
  212. continue
  213. }
  214. // Draw the same combination onto the actual dst using the optimized DrawMask implementation.
  215. DrawMask(dst, image.Rect(0, 0, 16, 16), test.src, image.ZP, test.mask, image.ZP, test.op)
  216. if image.Pt(8, 8).In(r) {
  217. // Check that the resultant pixel at (8, 8) matches what we expect
  218. // (the expected value can be verified by hand).
  219. if !eq(dst.At(8, 8), test.expected) {
  220. t.Errorf("draw %v %s: at (8, 8) %v versus %v", r, test.desc, dst.At(8, 8), test.expected)
  221. continue
  222. }
  223. }
  224. // Check that the resultant dst image matches the golden output.
  225. for y := b.Min.Y; y < b.Max.Y; y++ {
  226. for x := b.Min.X; x < b.Max.X; x++ {
  227. if !eq(dst.At(x, y), golden.At(x, y)) {
  228. t.Errorf("draw %v %s: at (%d, %d), %v versus golden %v", r, test.desc, x, y, dst.At(x, y), golden.At(x, y))
  229. continue loop
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. func TestDrawOverlap(t *testing.T) {
  237. for _, op := range []Op{Over, Src} {
  238. for yoff := -2; yoff <= 2; yoff++ {
  239. loop:
  240. for xoff := -2; xoff <= 2; xoff++ {
  241. m := gradYellow(127).(*image.RGBA)
  242. dst := m.SubImage(image.Rect(5, 5, 10, 10)).(*image.RGBA)
  243. src := m.SubImage(image.Rect(5+xoff, 5+yoff, 10+xoff, 10+yoff)).(*image.RGBA)
  244. b := dst.Bounds()
  245. // Draw the (src, mask, op) onto a copy of dst using a slow but obviously correct implementation.
  246. golden := makeGolden(dst, b, src, src.Bounds().Min, nil, image.ZP, op)
  247. if !b.Eq(golden.Bounds()) {
  248. t.Errorf("drawOverlap xoff=%d,yoff=%d: bounds %v versus %v", xoff, yoff, dst.Bounds(), golden.Bounds())
  249. continue
  250. }
  251. // Draw the same combination onto the actual dst using the optimized DrawMask implementation.
  252. DrawMask(dst, b, src, src.Bounds().Min, nil, image.ZP, op)
  253. // Check that the resultant dst image matches the golden output.
  254. for y := b.Min.Y; y < b.Max.Y; y++ {
  255. for x := b.Min.X; x < b.Max.X; x++ {
  256. if !eq(dst.At(x, y), golden.At(x, y)) {
  257. t.Errorf("drawOverlap xoff=%d,yoff=%d: at (%d, %d), %v versus golden %v", xoff, yoff, x, y, dst.At(x, y), golden.At(x, y))
  258. continue loop
  259. }
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266. // TestNonZeroSrcPt checks drawing with a non-zero src point parameter.
  267. func TestNonZeroSrcPt(t *testing.T) {
  268. a := image.NewRGBA(image.Rect(0, 0, 1, 1))
  269. b := image.NewRGBA(image.Rect(0, 0, 2, 2))
  270. b.Set(0, 0, color.RGBA{0, 0, 0, 5})
  271. b.Set(1, 0, color.RGBA{0, 0, 5, 5})
  272. b.Set(0, 1, color.RGBA{0, 5, 0, 5})
  273. b.Set(1, 1, color.RGBA{5, 0, 0, 5})
  274. Draw(a, image.Rect(0, 0, 1, 1), b, image.Pt(1, 1), Over)
  275. if !eq(color.RGBA{5, 0, 0, 5}, a.At(0, 0)) {
  276. t.Errorf("non-zero src pt: want %v got %v", color.RGBA{5, 0, 0, 5}, a.At(0, 0))
  277. }
  278. }
  279. func TestFill(t *testing.T) {
  280. rr := []image.Rectangle{
  281. image.Rect(0, 0, 0, 0),
  282. image.Rect(0, 0, 40, 30),
  283. image.Rect(10, 0, 40, 30),
  284. image.Rect(0, 20, 40, 30),
  285. image.Rect(10, 20, 40, 30),
  286. image.Rect(10, 20, 15, 25),
  287. image.Rect(10, 0, 35, 30),
  288. image.Rect(0, 15, 40, 16),
  289. image.Rect(24, 24, 25, 25),
  290. image.Rect(23, 23, 26, 26),
  291. image.Rect(22, 22, 27, 27),
  292. image.Rect(21, 21, 28, 28),
  293. image.Rect(20, 20, 29, 29),
  294. }
  295. for _, r := range rr {
  296. m := image.NewRGBA(image.Rect(0, 0, 40, 30)).SubImage(r).(*image.RGBA)
  297. b := m.Bounds()
  298. c := color.RGBA{11, 0, 0, 255}
  299. src := &image.Uniform{C: c}
  300. check := func(desc string) {
  301. for y := b.Min.Y; y < b.Max.Y; y++ {
  302. for x := b.Min.X; x < b.Max.X; x++ {
  303. if !eq(c, m.At(x, y)) {
  304. t.Errorf("%s fill: at (%d, %d), sub-image bounds=%v: want %v got %v", desc, x, y, r, c, m.At(x, y))
  305. return
  306. }
  307. }
  308. }
  309. }
  310. // Draw 1 pixel at a time.
  311. for y := b.Min.Y; y < b.Max.Y; y++ {
  312. for x := b.Min.X; x < b.Max.X; x++ {
  313. DrawMask(m, image.Rect(x, y, x+1, y+1), src, image.ZP, nil, image.ZP, Src)
  314. }
  315. }
  316. check("pixel")
  317. // Draw 1 row at a time.
  318. c = color.RGBA{0, 22, 0, 255}
  319. src = &image.Uniform{C: c}
  320. for y := b.Min.Y; y < b.Max.Y; y++ {
  321. DrawMask(m, image.Rect(b.Min.X, y, b.Max.X, y+1), src, image.ZP, nil, image.ZP, Src)
  322. }
  323. check("row")
  324. // Draw 1 column at a time.
  325. c = color.RGBA{0, 0, 33, 255}
  326. src = &image.Uniform{C: c}
  327. for x := b.Min.X; x < b.Max.X; x++ {
  328. DrawMask(m, image.Rect(x, b.Min.Y, x+1, b.Max.Y), src, image.ZP, nil, image.ZP, Src)
  329. }
  330. check("column")
  331. // Draw the whole image at once.
  332. c = color.RGBA{44, 55, 66, 77}
  333. src = &image.Uniform{C: c}
  334. DrawMask(m, b, src, image.ZP, nil, image.ZP, Src)
  335. check("whole")
  336. }
  337. }
  338. // TestFloydSteinbergCheckerboard tests that the result of Floyd-Steinberg
  339. // error diffusion of a uniform 50% gray source image with a black-and-white
  340. // palette is a checkerboard pattern.
  341. func TestFloydSteinbergCheckerboard(t *testing.T) {
  342. b := image.Rect(0, 0, 640, 480)
  343. // We can't represent 50% exactly, but 0x7fff / 0xffff is close enough.
  344. src := &image.Uniform{color.Gray16{0x7fff}}
  345. dst := image.NewPaletted(b, color.Palette{color.Black, color.White})
  346. FloydSteinberg.Draw(dst, b, src, image.Point{})
  347. nErr := 0
  348. for y := b.Min.Y; y < b.Max.Y; y++ {
  349. for x := b.Min.X; x < b.Max.X; x++ {
  350. got := dst.Pix[dst.PixOffset(x, y)]
  351. want := uint8(x+y) % 2
  352. if got != want {
  353. t.Errorf("at (%d, %d): got %d, want %d", x, y, got, want)
  354. if nErr++; nErr == 10 {
  355. t.Fatal("there may be more errors")
  356. }
  357. }
  358. }
  359. }
  360. }
  361. // embeddedPaletted is an Image that behaves like an *image.Paletted but whose
  362. // type is not *image.Paletted.
  363. type embeddedPaletted struct {
  364. *image.Paletted
  365. }
  366. // TestPaletted tests that the drawPaletted function behaves the same
  367. // regardless of whether dst is an *image.Paletted.
  368. func TestPaletted(t *testing.T) {
  369. f, err := os.Open("../testdata/video-001.png")
  370. if err != nil {
  371. t.Fatalf("open: %v", err)
  372. }
  373. defer f.Close()
  374. src, err := png.Decode(f)
  375. if err != nil {
  376. t.Fatalf("decode: %v", err)
  377. }
  378. b := src.Bounds()
  379. cgaPalette := color.Palette{
  380. color.RGBA{0x00, 0x00, 0x00, 0xff},
  381. color.RGBA{0x55, 0xff, 0xff, 0xff},
  382. color.RGBA{0xff, 0x55, 0xff, 0xff},
  383. color.RGBA{0xff, 0xff, 0xff, 0xff},
  384. }
  385. drawers := map[string]Drawer{
  386. "src": Src,
  387. "floyd-steinberg": FloydSteinberg,
  388. }
  389. loop:
  390. for dName, d := range drawers {
  391. dst0 := image.NewPaletted(b, cgaPalette)
  392. dst1 := image.NewPaletted(b, cgaPalette)
  393. d.Draw(dst0, b, src, image.Point{})
  394. d.Draw(embeddedPaletted{dst1}, b, src, image.Point{})
  395. for y := b.Min.Y; y < b.Max.Y; y++ {
  396. for x := b.Min.X; x < b.Max.X; x++ {
  397. if !eq(dst0.At(x, y), dst1.At(x, y)) {
  398. t.Errorf("%s: at (%d, %d), %v versus %v",
  399. dName, x, y, dst0.At(x, y), dst1.At(x, y))
  400. continue loop
  401. }
  402. }
  403. }
  404. }
  405. }