SrcOverTest.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright 2011 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/private/SkColorData.h"
  8. #include "tests/Test.h"
  9. // our std SkAlpha255To256
  10. static int test_srcover0(unsigned dst, unsigned alpha) {
  11. return alpha + SkAlphaMul(dst, SkAlpha255To256(255 - alpha));
  12. }
  13. // faster hack +1
  14. static int test_srcover1(unsigned dst, unsigned alpha) {
  15. return alpha + SkAlphaMul(dst, 256 - alpha);
  16. }
  17. // slower "correct"
  18. static int test_srcover2(unsigned dst, unsigned alpha) {
  19. return alpha + SkMulDiv255Round(dst, 255 - alpha);
  20. }
  21. DEF_TEST(SrcOver, reporter) {
  22. /* Here's the idea. Can we ensure that when we blend on top of an opaque
  23. dst, that the result always stay's opaque (i.e. exactly 255)?
  24. */
  25. unsigned i;
  26. int opaqueCounter0 = 0;
  27. int opaqueCounter1 = 0;
  28. int opaqueCounter2 = 0;
  29. for (i = 0; i <= 255; i++) {
  30. unsigned result0 = test_srcover0(0xFF, i);
  31. unsigned result1 = test_srcover1(0xFF, i);
  32. unsigned result2 = test_srcover2(0xFF, i);
  33. opaqueCounter0 += (result0 == 0xFF);
  34. opaqueCounter1 += (result1 == 0xFF);
  35. opaqueCounter2 += (result2 == 0xFF);
  36. }
  37. #if 0
  38. INFOF(reporter, "---- opaque test: [%d %d %d]\n",
  39. opaqueCounter0, opaqueCounter1, opaqueCounter2);
  40. #endif
  41. // we acknowledge that technique0 does not always return opaque
  42. REPORTER_ASSERT(reporter, opaqueCounter0 == 256);
  43. REPORTER_ASSERT(reporter, opaqueCounter1 == 256);
  44. REPORTER_ASSERT(reporter, opaqueCounter2 == 256);
  45. // Now ensure that we never over/underflow a byte
  46. for (i = 0; i <= 255; i++) {
  47. for (unsigned dst = 0; dst <= 255; dst++) {
  48. unsigned r0 = test_srcover0(dst, i);
  49. unsigned r1 = test_srcover1(dst, i);
  50. unsigned r2 = test_srcover2(dst, i);
  51. unsigned max = SkMax32(dst, i);
  52. // ignore the known failure
  53. if (dst != 255) {
  54. REPORTER_ASSERT(reporter, r0 <= 255 && r0 >= max);
  55. }
  56. REPORTER_ASSERT(reporter, r1 <= 255 && r1 >= max);
  57. REPORTER_ASSERT(reporter, r2 <= 255 && r2 >= max);
  58. #if 0
  59. // this shows where r1 (faster) differs from r2 (more exact)
  60. if (r1 != r2) {
  61. INFOF(reporter, "--- dst=%d i=%d r1=%d r2=%d exact=%g\n",
  62. dst, i, r1, r2, i + dst - dst*i/255.0f);
  63. }
  64. #endif
  65. }
  66. }
  67. }