1
0

stdlib_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2015 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. // +build go1.9
  5. package draw
  6. // This file contains tests that depend on the exact behavior of the
  7. // image/color package in the standard library. The color conversion formula
  8. // from YCbCr to RGBA changed between Go 1.4 and Go 1.5, and between Go 1.8 and
  9. // Go 1.9, so this file's tests are only enabled for Go 1.9 and above.
  10. import (
  11. "bytes"
  12. "image"
  13. "image/color"
  14. "testing"
  15. )
  16. // TestFastPaths tests that the fast path implementations produce identical
  17. // results to the generic implementation.
  18. func TestFastPaths(t *testing.T) {
  19. drs := []image.Rectangle{
  20. image.Rect(0, 0, 10, 10), // The dst bounds.
  21. image.Rect(3, 4, 8, 6), // A strict subset of the dst bounds.
  22. image.Rect(-3, -5, 2, 4), // Partial out-of-bounds #0.
  23. image.Rect(4, -2, 6, 12), // Partial out-of-bounds #1.
  24. image.Rect(12, 14, 23, 45), // Complete out-of-bounds.
  25. image.Rect(5, 5, 5, 5), // Empty.
  26. }
  27. srs := []image.Rectangle{
  28. image.Rect(0, 0, 12, 9), // The src bounds.
  29. image.Rect(2, 2, 10, 8), // A strict subset of the src bounds.
  30. image.Rect(10, 5, 20, 20), // Partial out-of-bounds #0.
  31. image.Rect(-40, 0, 40, 8), // Partial out-of-bounds #1.
  32. image.Rect(-8, -8, -4, -4), // Complete out-of-bounds.
  33. image.Rect(5, 5, 5, 5), // Empty.
  34. }
  35. srcfs := []func(image.Rectangle) (image.Image, error){
  36. srcGray,
  37. srcNRGBA,
  38. srcRGBA,
  39. srcUnif,
  40. srcYCbCr,
  41. }
  42. var srcs []image.Image
  43. for _, srcf := range srcfs {
  44. src, err := srcf(srs[0])
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. srcs = append(srcs, src)
  49. }
  50. qs := []Interpolator{
  51. NearestNeighbor,
  52. ApproxBiLinear,
  53. CatmullRom,
  54. }
  55. ops := []Op{
  56. Over,
  57. Src,
  58. }
  59. blue := image.NewUniform(color.RGBA{0x11, 0x22, 0x44, 0x7f})
  60. for _, dr := range drs {
  61. for _, src := range srcs {
  62. for _, sr := range srs {
  63. for _, transform := range []bool{false, true} {
  64. for _, q := range qs {
  65. for _, op := range ops {
  66. dst0 := image.NewRGBA(drs[0])
  67. dst1 := image.NewRGBA(drs[0])
  68. Draw(dst0, dst0.Bounds(), blue, image.Point{}, Src)
  69. Draw(dstWrapper{dst1}, dst1.Bounds(), srcWrapper{blue}, image.Point{}, Src)
  70. if transform {
  71. m := transformMatrix(3.75, 2, 1)
  72. q.Transform(dst0, m, src, sr, op, nil)
  73. q.Transform(dstWrapper{dst1}, m, srcWrapper{src}, sr, op, nil)
  74. } else {
  75. q.Scale(dst0, dr, src, sr, op, nil)
  76. q.Scale(dstWrapper{dst1}, dr, srcWrapper{src}, sr, op, nil)
  77. }
  78. if !bytes.Equal(dst0.Pix, dst1.Pix) {
  79. t.Errorf("pix differ for dr=%v, src=%T, sr=%v, transform=%t, q=%T",
  80. dr, src, sr, transform, q)
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }