shared_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2011 The Graphics-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 graphics
  5. import (
  6. "bytes"
  7. "code.google.com/p/graphics-go/graphics/graphicstest"
  8. "image"
  9. "image/color"
  10. "testing"
  11. )
  12. type transformOneColorTest struct {
  13. desc string
  14. dstWidth int
  15. dstHeight int
  16. srcWidth int
  17. srcHeight int
  18. opt interface{}
  19. src []uint8
  20. res []uint8
  21. }
  22. func (oc *transformOneColorTest) newSrc() *image.RGBA {
  23. b := image.Rect(0, 0, oc.srcWidth, oc.srcHeight)
  24. src := image.NewRGBA(b)
  25. i := 0
  26. for y := b.Min.Y; y < b.Max.Y; y++ {
  27. for x := b.Min.X; x < b.Max.X; x++ {
  28. src.SetRGBA(x, y, color.RGBA{
  29. R: oc.src[i],
  30. G: oc.src[i],
  31. B: oc.src[i],
  32. A: oc.src[i],
  33. })
  34. i++
  35. }
  36. }
  37. return src
  38. }
  39. func (oc *transformOneColorTest) newDst() *image.RGBA {
  40. return image.NewRGBA(image.Rect(0, 0, oc.dstWidth, oc.dstHeight))
  41. }
  42. func checkTransformTest(t *testing.T, oc *transformOneColorTest, dst *image.RGBA) bool {
  43. for ch := 0; ch < 4; ch++ {
  44. i := 0
  45. res := make([]byte, len(oc.res))
  46. for y := 0; y < oc.dstHeight; y++ {
  47. for x := 0; x < oc.dstWidth; x++ {
  48. off := (y-dst.Rect.Min.Y)*dst.Stride + (x-dst.Rect.Min.X)*4
  49. res[i] = dst.Pix[off+ch]
  50. i++
  51. }
  52. }
  53. if !bytes.Equal(res, oc.res) {
  54. got := graphicstest.SprintBox(res, oc.dstWidth, oc.dstHeight)
  55. want := graphicstest.SprintBox(oc.res, oc.dstWidth, oc.dstHeight)
  56. t.Errorf("%s: ch=%d\n got\n%s\n want\n%s", oc.desc, ch, got, want)
  57. return false
  58. }
  59. }
  60. return true
  61. }