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
38namespace blink {
39
40namespace ColorSpaceUtilities {
41
42static const uint8_t* getLinearRgbLUT()
43{
44    static uint8_t linearRgbLUT[256];
45    static bool initialized;
46    if (!initialized) {
47        for (unsigned i = 0; i < 256; i++) {
48            float color = i  / 255.0f;
49            color = (color <= 0.04045f ? color / 12.92f : pow((color + 0.055f) / 1.055f, 2.4f));
50            color = std::max(0.0f, color);
51            color = std::min(1.0f, color);
52            linearRgbLUT[i] = static_cast<uint8_t>(round(color * 255));
53        }
54        initialized = true;
55    }
56    return linearRgbLUT;
57}
58
59static const uint8_t* getDeviceRgbLUT()
60{
61    static uint8_t deviceRgbLUT[256];
62    static bool initialized;
63    if (!initialized) {
64        for (unsigned i = 0; i < 256; i++) {
65            float color = i / 255.0f;
66            color = (powf(color, 1.0f / 2.4f) * 1.055f) - 0.055f;
67            color = std::max(0.0f, color);
68            color = std::min(1.0f, color);
69            deviceRgbLUT[i] = static_cast<uint8_t>(round(color * 255));
70        }
71        initialized = true;
72    }
73    return deviceRgbLUT;
74}
75
76const uint8_t* getConversionLUT(ColorSpace dstColorSpace, ColorSpace srcColorSpace)
77{
78    // Identity.
79    if (srcColorSpace == dstColorSpace)
80        return 0;
81
82    // Only sRGB/DeviceRGB <-> linearRGB are supported at the moment.
83    if ((srcColorSpace != ColorSpaceLinearRGB && srcColorSpace != ColorSpaceDeviceRGB)
84        || (dstColorSpace != ColorSpaceLinearRGB && dstColorSpace != ColorSpaceDeviceRGB))
85        return 0;
86
87    if (dstColorSpace == ColorSpaceLinearRGB)
88        return getLinearRgbLUT();
89    if (dstColorSpace == ColorSpaceDeviceRGB)
90        return getDeviceRgbLUT();
91
92    ASSERT_NOT_REACHED();
93    return 0;
94}
95
96Color convertColor(const Color& srcColor, ColorSpace dstColorSpace, ColorSpace srcColorSpace)
97{
98    const uint8_t* lookupTable = getConversionLUT(dstColorSpace, srcColorSpace);
99    if (!lookupTable)
100        return srcColor;
101
102    return Color(lookupTable[srcColor.red()], lookupTable[srcColor.green()], lookupTable[srcColor.blue()], srcColor.alpha());
103}
104
105} // namespace ColorSpaceUtilities
106
107} // namespace blink
108