Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(223)

Unified Diff: Source/platform/graphics/ColorSpace.cpp

Issue 134733016: Add support for converting Colors to linear RGB; Fix relevant filters (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rebase; update TestExpectations Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/platform/graphics/ColorSpace.h ('k') | Source/platform/graphics/ImageBuffer.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/platform/graphics/ColorSpace.cpp
diff --git a/Source/platform/graphics/ColorSpace.cpp b/Source/platform/graphics/ColorSpace.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..b33b64db0e2821172026ada2c57f9d7080e96f05
--- /dev/null
+++ b/Source/platform/graphics/ColorSpace.cpp
@@ -0,0 +1,107 @@
+/*
+ * Copyright (c) 2008, Google Inc. All rights reserved.
+ * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
+ * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "platform/graphics/ColorSpace.h"
+
+#include "wtf/MathExtras.h"
+
+namespace WebCore {
+
+namespace ColorSpaceUtilities {
+
+static const uint8_t* getLinearRgbLUT()
+{
+ static uint8_t linearRgbLUT[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);
+ linearRgbLUT[i] = static_cast<uint8_t>(round(color * 255));
+ }
+ initialized = true;
+ }
+ return linearRgbLUT;
+}
+
+static const uint8_t* getDeviceRgbLUT()
+{
+ static uint8_t deviceRgbLUT[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);
+ deviceRgbLUT[i] = static_cast<uint8_t>(round(color * 255));
+ }
+ initialized = true;
+ }
+ return deviceRgbLUT;
+}
+
+const uint8_t* getConversionLUT(ColorSpace dstColorSpace, ColorSpace srcColorSpace)
+{
+ // Identity.
+ if (srcColorSpace == dstColorSpace)
+ return 0;
+
+ // Only sRGB/DeviceRGB <-> linearRGB are supported at the moment.
+ if ((srcColorSpace != ColorSpaceLinearRGB && srcColorSpace != ColorSpaceDeviceRGB)
+ || (dstColorSpace != ColorSpaceLinearRGB && dstColorSpace != ColorSpaceDeviceRGB))
+ return 0;
+
+ if (dstColorSpace == ColorSpaceLinearRGB)
+ return getLinearRgbLUT();
+ if (dstColorSpace == ColorSpaceDeviceRGB)
+ return getDeviceRgbLUT();
+
+ ASSERT_NOT_REACHED();
+ return 0;
+}
+
+Color convertColor(const Color& srcColor, ColorSpace dstColorSpace, ColorSpace srcColorSpace)
+{
+ const uint8_t* lookupTable = getConversionLUT(dstColorSpace, srcColorSpace);
+ if (!lookupTable)
+ return srcColor;
+
+ return Color(lookupTable[srcColor.red()], lookupTable[srcColor.green()], lookupTable[srcColor.blue()], srcColor.alpha());
+}
+
+} // namespace ColorSpaceUtilities
+
+} // namespace WebCore
« no previous file with comments | « Source/platform/graphics/ColorSpace.h ('k') | Source/platform/graphics/ImageBuffer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698