2016-07-08 13:33:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2022-04-05 16:56:01 +00:00
|
|
|
#include "include/core/SkColorSpace.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "src/core/SkColorSpaceXformSteps.h"
|
|
|
|
#include "src/core/SkRasterPipeline.h"
|
|
|
|
#include "tests/Test.h"
|
2016-07-08 13:33:16 +00:00
|
|
|
|
2018-09-19 15:31:27 +00:00
|
|
|
#include <math.h>
|
|
|
|
|
2018-10-26 15:13:04 +00:00
|
|
|
DEF_TEST(srgb_roundtrip, r) {
|
2020-05-22 16:42:38 +00:00
|
|
|
uint32_t reds[256];
|
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
reds[i] = i;
|
|
|
|
}
|
2017-05-15 22:03:52 +00:00
|
|
|
|
2020-05-22 16:42:38 +00:00
|
|
|
SkRasterPipeline_MemoryCtx ptr = { reds, 0 };
|
2017-05-15 22:03:52 +00:00
|
|
|
|
2020-05-22 16:42:38 +00:00
|
|
|
sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeSRGB(),
|
|
|
|
linear = sRGB->makeLinearGamma();
|
|
|
|
const SkAlphaType upm = kUnpremul_SkAlphaType;
|
2018-10-26 15:13:04 +00:00
|
|
|
|
2020-05-22 16:42:38 +00:00
|
|
|
SkColorSpaceXformSteps linearize{ sRGB.get(),upm, linear.get(),upm},
|
|
|
|
reencode {linear.get(),upm, sRGB.get(),upm};
|
2018-10-26 15:13:04 +00:00
|
|
|
|
2020-05-22 16:42:38 +00:00
|
|
|
SkRasterPipeline_<256> p;
|
|
|
|
p.append(SkRasterPipeline::load_8888, &ptr);
|
|
|
|
linearize.apply(&p);
|
|
|
|
reencode .apply(&p);
|
|
|
|
p.append(SkRasterPipeline::store_8888, &ptr);
|
2017-05-15 22:03:52 +00:00
|
|
|
|
2020-05-22 16:42:38 +00:00
|
|
|
p.run(0,0,256,1);
|
2017-05-15 22:03:52 +00:00
|
|
|
|
2020-05-22 16:42:38 +00:00
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
if (reds[i] != (uint32_t)i) {
|
|
|
|
ERRORF(r, "%d doesn't round trip, %d", i, reds[i]);
|
2017-05-15 22:03:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-24 23:00:47 +00:00
|
|
|
|
2018-10-26 15:13:04 +00:00
|
|
|
DEF_TEST(srgb_edge_cases, r) {
|
2017-05-24 23:00:47 +00:00
|
|
|
// We need to run at least 4 pixels to make sure we hit all specializations.
|
2018-09-20 17:10:49 +00:00
|
|
|
float colors[4][4] = { {0,1,1,1}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} };
|
2017-05-24 23:00:47 +00:00
|
|
|
auto& color = colors[0];
|
2017-07-18 22:15:13 +00:00
|
|
|
|
2018-10-24 10:42:14 +00:00
|
|
|
SkRasterPipeline_MemoryCtx dst = { &color, 0 };
|
2017-05-24 23:00:47 +00:00
|
|
|
|
2018-10-26 15:13:04 +00:00
|
|
|
sk_sp<SkColorSpace> sRGB = SkColorSpace::MakeSRGB(),
|
|
|
|
linear = sRGB->makeLinearGamma();
|
|
|
|
const SkAlphaType upm = kUnpremul_SkAlphaType;
|
|
|
|
|
|
|
|
SkColorSpaceXformSteps steps {linear.get(),upm, sRGB.get(),upm};
|
|
|
|
|
2017-08-03 14:22:42 +00:00
|
|
|
SkSTArenaAlloc<256> alloc;
|
|
|
|
SkRasterPipeline p(&alloc);
|
|
|
|
p.append_constant_color(&alloc, color);
|
2020-05-22 16:42:38 +00:00
|
|
|
steps.apply(&p);
|
2017-05-24 23:00:47 +00:00
|
|
|
p.append(SkRasterPipeline::store_f32, &dst);
|
2017-07-18 22:15:13 +00:00
|
|
|
p.run(0,0,4,1);
|
2017-05-24 23:00:47 +00:00
|
|
|
|
2018-09-20 17:10:49 +00:00
|
|
|
if (color[0] != 0.0f) {
|
|
|
|
ERRORF(r, "expected to_srgb() to map 0.0f to 0.0f, got %f", color[0]);
|
2017-05-24 23:00:47 +00:00
|
|
|
}
|
2018-09-20 17:10:49 +00:00
|
|
|
if (color[1] != 1.0f) {
|
|
|
|
float f = color[1];
|
2017-05-24 23:00:47 +00:00
|
|
|
uint32_t x;
|
|
|
|
memcpy(&x, &f, 4);
|
2018-09-20 17:10:49 +00:00
|
|
|
ERRORF(r, "expected to_srgb() to map 1.0f to 1.0f, got %f (%08x)", color[1], x);
|
2017-05-24 23:00:47 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-26 15:13:04 +00:00
|
|
|
|
|
|
|
// Linearize and then re-encode pixel values, testing that the output is close to the input.
|
2020-05-22 16:42:38 +00:00
|
|
|
DEF_TEST(srgb_roundtrip_extended, r) {
|
2018-10-26 15:13:04 +00:00
|
|
|
static const int kSteps = 128;
|
|
|
|
SkColor4f rgba[kSteps];
|
|
|
|
|
|
|
|
auto expected = [=](int i) {
|
2020-05-22 16:42:38 +00:00
|
|
|
float scale = 10000.0f / (3*kSteps);
|
2018-10-26 15:13:04 +00:00
|
|
|
return SkColor4f{
|
|
|
|
(3*i+0) * scale,
|
|
|
|
(3*i+1) * scale,
|
|
|
|
(3*i+2) * scale,
|
|
|
|
1.0f,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < kSteps; i++) {
|
|
|
|
rgba[i] = expected(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkRasterPipeline_MemoryCtx ptr = { rgba, 0 };
|
|
|
|
|
2020-05-22 16:42:38 +00:00
|
|
|
sk_sp<SkColorSpace> cs = SkColorSpace::MakeSRGB();
|
2018-10-26 15:13:04 +00:00
|
|
|
sk_sp<SkColorSpace> linear = cs->makeLinearGamma();
|
|
|
|
const SkAlphaType upm = kUnpremul_SkAlphaType;
|
|
|
|
|
|
|
|
SkColorSpaceXformSteps linearize{ cs.get(),upm, linear.get(),upm},
|
|
|
|
reencode {linear.get(),upm, cs.get(),upm};
|
|
|
|
|
|
|
|
SkRasterPipeline_<256> p;
|
|
|
|
p.append(SkRasterPipeline::load_f32, &ptr);
|
2020-05-22 16:42:38 +00:00
|
|
|
linearize.apply(&p);
|
|
|
|
reencode .apply(&p);
|
2018-10-26 15:13:04 +00:00
|
|
|
p.append(SkRasterPipeline::store_f32, &ptr);
|
|
|
|
p.run(0,0,kSteps,1);
|
|
|
|
|
|
|
|
auto close = [=](float x, float y) {
|
|
|
|
return x == y
|
2020-05-22 16:42:38 +00:00
|
|
|
|| (x/y < 1.001f && y/x < 1.001f);
|
2018-10-26 15:13:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (int i = 0; i < kSteps; i++) {
|
|
|
|
SkColor4f want = expected(i);
|
|
|
|
#if 0
|
|
|
|
SkDebugf("got %g %g %g, want %g %g %g\n",
|
|
|
|
rgba[i].fR, rgba[i].fG, rgba[i].fB,
|
|
|
|
want.fR, want.fG, want.fB);
|
|
|
|
#endif
|
|
|
|
REPORTER_ASSERT(r, close(rgba[i].fR, want.fR));
|
|
|
|
REPORTER_ASSERT(r, close(rgba[i].fG, want.fG));
|
|
|
|
REPORTER_ASSERT(r, close(rgba[i].fB, want.fB));
|
|
|
|
}
|
|
|
|
}
|