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

Side by Side 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. 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 unified diff | Download patch
« no previous file with comments | « Source/platform/graphics/ColorSpace.h ('k') | Source/platform/graphics/ImageBuffer.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2008, Google Inc. All rights reserved.
3 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
4 * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "config.h"
34 #include "platform/graphics/ColorSpace.h"
35
36 #include "wtf/MathExtras.h"
37 #include "wtf/Vector.h"
38
39 namespace WebCore {
40
41 namespace ColorSpaceUtilities {
42
43 namespace {
44
45 const Vector<uint8_t>& getLinearRgbLUT()
46 {
47 DEFINE_STATIC_LOCAL(Vector<uint8_t>, linearRgbLUT, ());
48 if (linearRgbLUT.isEmpty()) {
49 linearRgbLUT.reserveCapacity(256);
50 for (unsigned i = 0; i < 256; i++) {
51 float color = i / 255.0f;
52 color = (color <= 0.04045f ? color / 12.92f : pow((color + 0.055f) / 1.055f, 2.4f));
53 color = std::max(0.0f, color);
54 color = std::min(1.0f, color);
55 linearRgbLUT.append(static_cast<uint8_t>(round(color * 255)));
56 }
57 }
58 return linearRgbLUT;
59 }
60
61 const Vector<uint8_t>& getDeviceRgbLUT()
62 {
63 DEFINE_STATIC_LOCAL(Vector<uint8_t>, deviceRgbLUT, ());
Stephen Chennney 2014/01/23 16:04:04 Given that the return value gets converted immedia
64 if (deviceRgbLUT.isEmpty()) {
65 deviceRgbLUT.reserveCapacity(256);
66 for (unsigned i = 0; i < 256; i++) {
67 float color = i / 255.0f;
68 color = (powf(color, 1.0f / 2.4f) * 1.055f) - 0.055f;
69 color = std::max(0.0f, color);
70 color = std::min(1.0f, color);
71 deviceRgbLUT.append(static_cast<uint8_t>(round(color * 255)));
72 }
73 }
74 return deviceRgbLUT;
75 }
76
77 } // namespace
78
79 const uint8_t* getConversionLUT(ColorSpace dstColorSpace, ColorSpace srcColorSpa ce)
80 {
81 // Identity.
82 if (srcColorSpace == dstColorSpace)
83 return 0;
84
85 // Only sRGB/DeviceRGB <-> linearRGB are supported at the moment.
86 if ((srcColorSpace != ColorSpaceLinearRGB && srcColorSpace != ColorSpaceDevi ceRGB)
87 || (dstColorSpace != ColorSpaceLinearRGB && dstColorSpace != ColorSpaceD eviceRGB))
88 return 0;
89
90 if (dstColorSpace == ColorSpaceLinearRGB)
91 return getLinearRgbLUT().data();
92 if (dstColorSpace == ColorSpaceDeviceRGB)
93 return getDeviceRgbLUT().data();
94
95 ASSERT_NOT_REACHED();
96 return 0;
97 }
98
99 Color convertColor(const Color& srcColor, ColorSpace dstColorSpace, ColorSpace s rcColorSpace)
100 {
101 const uint8_t* lookupTable = getConversionLUT(dstColorSpace, srcColorSpace);
102 if (!lookupTable)
103 return srcColor;
104
105 return Color(lookupTable[srcColor.red()], lookupTable[srcColor.green()], loo kupTable[srcColor.blue()], srcColor.alpha());
106 }
107
108 } // namespace ColorSpaceUtilities
109
110 } // namespace WebCore
OLDNEW
« 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