SRGBTest.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2016 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/core/SkTypes.h"
  8. #include "src/core/SkColorSpaceXformSteps.h"
  9. #include "src/core/SkRasterPipeline.h"
  10. #include "tests/Test.h"
  11. #include <math.h>
  12. DEF_TEST(srgb_roundtrip, r) {
  13. for (int normalized = 0; normalized < 2; normalized++) {
  14. uint32_t reds[256];
  15. for (int i = 0; i < 256; i++) {
  16. reds[i] = i;
  17. }
  18. SkRasterPipeline_MemoryCtx ptr = { reds, 0 };
  19. sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeSRGB(),
  20. linear = sRGB->makeLinearGamma();
  21. const SkAlphaType upm = kUnpremul_SkAlphaType;
  22. SkColorSpaceXformSteps linearize{ sRGB.get(),upm, linear.get(),upm},
  23. reencode {linear.get(),upm, sRGB.get(),upm};
  24. SkRasterPipeline_<256> p;
  25. p.append(SkRasterPipeline::load_8888, &ptr);
  26. linearize.apply(&p, !!normalized);
  27. reencode .apply(&p, !!normalized);
  28. p.append(SkRasterPipeline::store_8888, &ptr);
  29. p.run(0,0,256,1);
  30. for (int i = 0; i < 256; i++) {
  31. if (reds[i] != (uint32_t)i) {
  32. ERRORF(r, "%d doesn't round trip, %d", i, reds[i]);
  33. }
  34. }
  35. }
  36. }
  37. DEF_TEST(srgb_edge_cases, r) {
  38. // We need to run at least 4 pixels to make sure we hit all specializations.
  39. float colors[4][4] = { {0,1,1,1}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} };
  40. auto& color = colors[0];
  41. SkRasterPipeline_MemoryCtx dst = { &color, 0 };
  42. sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeSRGB(),
  43. linear = sRGB->makeLinearGamma();
  44. const SkAlphaType upm = kUnpremul_SkAlphaType;
  45. SkColorSpaceXformSteps steps {linear.get(),upm, sRGB.get(),upm};
  46. SkSTArenaAlloc<256> alloc;
  47. SkRasterPipeline p(&alloc);
  48. p.append_constant_color(&alloc, color);
  49. steps.apply(&p, true/*inputs are normalized*/);
  50. p.append(SkRasterPipeline::store_f32, &dst);
  51. p.run(0,0,4,1);
  52. if (color[0] != 0.0f) {
  53. ERRORF(r, "expected to_srgb() to map 0.0f to 0.0f, got %f", color[0]);
  54. }
  55. if (color[1] != 1.0f) {
  56. float f = color[1];
  57. uint32_t x;
  58. memcpy(&x, &f, 4);
  59. ERRORF(r, "expected to_srgb() to map 1.0f to 1.0f, got %f (%08x)", color[1], x);
  60. }
  61. }
  62. // Linearize and then re-encode pixel values, testing that the output is close to the input.
  63. static void test_roundtripping(skiatest::Reporter* r,
  64. sk_sp<SkColorSpace> cs,
  65. float range,
  66. float tolerance,
  67. bool normalized) {
  68. static const int kSteps = 128;
  69. SkColor4f rgba[kSteps];
  70. auto expected = [=](int i) {
  71. float scale = range / (3*kSteps);
  72. return SkColor4f{
  73. (3*i+0) * scale,
  74. (3*i+1) * scale,
  75. (3*i+2) * scale,
  76. 1.0f,
  77. };
  78. };
  79. for (int i = 0; i < kSteps; i++) {
  80. rgba[i] = expected(i);
  81. }
  82. SkRasterPipeline_MemoryCtx ptr = { rgba, 0 };
  83. sk_sp<SkColorSpace> linear = cs->makeLinearGamma();
  84. const SkAlphaType upm = kUnpremul_SkAlphaType;
  85. SkColorSpaceXformSteps linearize{ cs.get(),upm, linear.get(),upm},
  86. reencode {linear.get(),upm, cs.get(),upm};
  87. SkRasterPipeline_<256> p;
  88. p.append(SkRasterPipeline::load_f32, &ptr);
  89. linearize.apply(&p, normalized);
  90. reencode .apply(&p, normalized);
  91. p.append(SkRasterPipeline::store_f32, &ptr);
  92. p.run(0,0,kSteps,1);
  93. auto close = [=](float x, float y) {
  94. return x == y
  95. || (x/y < tolerance && y/x < tolerance);
  96. };
  97. for (int i = 0; i < kSteps; i++) {
  98. SkColor4f want = expected(i);
  99. #if 0
  100. SkDebugf("got %g %g %g, want %g %g %g\n",
  101. rgba[i].fR, rgba[i].fG, rgba[i].fB,
  102. want.fR, want.fG, want.fB);
  103. #endif
  104. REPORTER_ASSERT(r, close(rgba[i].fR, want.fR));
  105. REPORTER_ASSERT(r, close(rgba[i].fG, want.fG));
  106. REPORTER_ASSERT(r, close(rgba[i].fB, want.fB));
  107. }
  108. }
  109. DEF_TEST(srgb_roundtrip_extended, r) {
  110. // We're lying when we set normalized=true, but it allows us to test the to_srgb/from_srgb path.
  111. test_roundtripping(r, SkColorSpace::MakeSRGB(), 2.0f, 1.025f, true);
  112. // This normalized=false path should have much better round-tripping properties.
  113. test_roundtripping(r, SkColorSpace::MakeSRGB(), 10000.0f, 1.001f, false);
  114. }