Commit 7f8bba77 authored by Florin Malita's avatar Florin Malita Committed by Commit Bot

Use native Skia filters for color space conversion

Should me more correct/efficient.

Change-Id: I7bf98706d592f1d9e992dc0d28deac3a4ed722ec
Reviewed-on: https://chromium-review.googlesource.com/560151
Commit-Queue: Florin Malita <fmalita@chromium.org>
Reviewed-by: default avatarMike Reed <reed@chromium.org>
Reviewed-by: default avatarMike Klein <mtklein@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487659}
parent 2b45276f
...@@ -32,92 +32,48 @@ ...@@ -32,92 +32,48 @@
#include "platform/graphics/InterpolationSpace.h" #include "platform/graphics/InterpolationSpace.h"
#include <algorithm> #include "third_party/skia/include/core/SkColorFilter.h"
#include "platform/graphics/skia/SkiaUtils.h"
#include "platform/wtf/MathExtras.h"
#include "third_party/skia/include/effects/SkTableColorFilter.h"
namespace blink { namespace blink {
namespace InterpolationSpaceUtilities { namespace InterpolationSpaceUtilities {
static const uint8_t* GetLinearRgbLUT() { namespace {
static uint8_t linear_rgb_lut[256];
static bool initialized;
if (!initialized) {
for (unsigned i = 0; i < 256; i++) {
float color = i / 255.0f;
color = (color <= 0.04045f ? color / 12.92f
: pow((color + 0.055f) / 1.055f, 2.4f));
color = std::max(0.0f, color);
color = std::min(1.0f, color);
linear_rgb_lut[i] = static_cast<uint8_t>(round(color * 255));
}
initialized = true;
}
return linear_rgb_lut;
}
static const uint8_t* GetDeviceRgbLUT() {
static uint8_t device_rgb_lut[256];
static bool initialized;
if (!initialized) {
for (unsigned i = 0; i < 256; i++) {
float color = i / 255.0f;
color = (powf(color, 1.0f / 2.4f) * 1.055f) - 0.055f;
color = std::max(0.0f, color);
color = std::min(1.0f, color);
device_rgb_lut[i] = static_cast<uint8_t>(round(color * 255));
}
initialized = true;
}
return device_rgb_lut;
}
const uint8_t* GetConversionLUT(InterpolationSpace dst_interpolation_space, sk_sp<SkColorFilter> GetConversionFilter(
InterpolationSpace src_interpolation_space) { InterpolationSpace dst_interpolation_space,
InterpolationSpace src_interpolation_space) {
// Identity. // Identity.
if (src_interpolation_space == dst_interpolation_space) if (src_interpolation_space == dst_interpolation_space)
return 0; return nullptr;
// Only sRGB/DeviceRGB <-> linearRGB are supported at the moment.
if ((src_interpolation_space != kInterpolationSpaceLinear &&
src_interpolation_space != kInterpolationSpaceSRGB) ||
(dst_interpolation_space != kInterpolationSpaceLinear &&
dst_interpolation_space != kInterpolationSpaceSRGB))
return 0;
if (dst_interpolation_space == kInterpolationSpaceLinear) switch (dst_interpolation_space) {
return GetLinearRgbLUT(); case kInterpolationSpaceLinear:
if (dst_interpolation_space == kInterpolationSpaceSRGB) return SkColorFilter::MakeSRGBToLinearGamma();
return GetDeviceRgbLUT(); case kInterpolationSpaceSRGB:
return SkColorFilter::MakeLinearToSRGBGamma();
}
NOTREACHED(); NOTREACHED();
return 0; return nullptr;
} }
} // namespace
Color ConvertColor(const Color& src_color, Color ConvertColor(const Color& src_color,
InterpolationSpace dst_interpolation_space, InterpolationSpace dst_interpolation_space,
InterpolationSpace src_interpolation_space) { InterpolationSpace src_interpolation_space) {
const uint8_t* lookup_table = sk_sp<SkColorFilter> conversion_filter =
GetConversionLUT(dst_interpolation_space, src_interpolation_space); GetConversionFilter(dst_interpolation_space, src_interpolation_space);
if (!lookup_table) return conversion_filter
return src_color; ? Color(conversion_filter->filterColor(src_color.Rgb()))
: src_color;
return Color(lookup_table[src_color.Red()], lookup_table[src_color.Green()],
lookup_table[src_color.Blue()], src_color.Alpha());
} }
sk_sp<SkColorFilter> CreateInterpolationSpaceFilter( sk_sp<SkColorFilter> CreateInterpolationSpaceFilter(
InterpolationSpace src_interpolation_space, InterpolationSpace src_interpolation_space,
InterpolationSpace dst_interpolation_space) { InterpolationSpace dst_interpolation_space) {
const uint8_t* lookup_table = return GetConversionFilter(dst_interpolation_space, src_interpolation_space);
GetConversionLUT(dst_interpolation_space, src_interpolation_space);
if (!lookup_table)
return nullptr;
return SkTableColorFilter::MakeARGB(0, lookup_table, lookup_table,
lookup_table);
} }
} // namespace InterpolationSpaceUtilities } // namespace InterpolationSpaceUtilities
......
...@@ -44,15 +44,6 @@ enum InterpolationSpace { ...@@ -44,15 +44,6 @@ enum InterpolationSpace {
namespace InterpolationSpaceUtilities { namespace InterpolationSpaceUtilities {
// Get a pointer to a 8-bit lookup table that will convert color components
// in the |src_interpolation_space| to the |dst_interpolation_space|.
// If the conversion cannot be performed, or is a no-op (identity transform),
// then 0 is returned.
// (Note that a round-trip - f(B,A)[f(A,B)[x]] - is not lossless in general.)
const uint8_t* GetConversionLUT(
InterpolationSpace dst_interpolation_space,
InterpolationSpace src_interpolation_space = kInterpolationSpaceSRGB);
// Convert a Color assumed to be in the |src_interpolation_space| into the // Convert a Color assumed to be in the |src_interpolation_space| into the
// |dst_interpolation_space|. // |dst_interpolation_space|.
Color ConvertColor( Color ConvertColor(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment