1/*
2 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
3 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "ImageBuffer.h"
29
30#include "GraphicsContext.h"
31#include "IntRect.h"
32#include <wtf/MathExtras.h>
33
34namespace WebCore {
35
36#if !USE(CG)
37void ImageBuffer::transformColorSpace(ColorSpace srcColorSpace, ColorSpace dstColorSpace)
38{
39    DEPRECATED_DEFINE_STATIC_LOCAL(Vector<int>, deviceRgbLUT, ());
40    DEPRECATED_DEFINE_STATIC_LOCAL(Vector<int>, linearRgbLUT, ());
41
42    if (srcColorSpace == dstColorSpace)
43        return;
44
45    // only sRGB <-> linearRGB are supported at the moment
46    if ((srcColorSpace != ColorSpaceLinearRGB && srcColorSpace != ColorSpaceDeviceRGB)
47        || (dstColorSpace != ColorSpaceLinearRGB && dstColorSpace != ColorSpaceDeviceRGB))
48        return;
49
50    if (dstColorSpace == ColorSpaceLinearRGB) {
51        if (linearRgbLUT.isEmpty()) {
52            for (unsigned i = 0; i < 256; i++) {
53                float color = i  / 255.0f;
54                color = (color <= 0.04045f ? color / 12.92f : pow((color + 0.055f) / 1.055f, 2.4f));
55                color = std::max(0.0f, color);
56                color = std::min(1.0f, color);
57                linearRgbLUT.append(static_cast<int>(round(color * 255)));
58            }
59        }
60        platformTransformColorSpace(linearRgbLUT);
61    } else if (dstColorSpace == ColorSpaceDeviceRGB) {
62        if (deviceRgbLUT.isEmpty()) {
63            for (unsigned i = 0; i < 256; i++) {
64                float color = i / 255.0f;
65                color = (powf(color, 1.0f / 2.4f) * 1.055f) - 0.055f;
66                color = std::max(0.0f, color);
67                color = std::min(1.0f, color);
68                deviceRgbLUT.append(static_cast<int>(round(color * 255)));
69            }
70        }
71        platformTransformColorSpace(deviceRgbLUT);
72    }
73}
74#endif // USE(CG)
75
76inline void ImageBuffer::genericConvertToLuminanceMask()
77{
78    IntRect luminanceRect(IntPoint(), internalSize());
79    RefPtr<Uint8ClampedArray> srcPixelArray = getUnmultipliedImageData(luminanceRect);
80
81    unsigned pixelArrayLength = srcPixelArray->length();
82    for (unsigned pixelOffset = 0; pixelOffset < pixelArrayLength; pixelOffset += 4) {
83        unsigned char a = srcPixelArray->item(pixelOffset + 3);
84        if (!a)
85            continue;
86        unsigned char r = srcPixelArray->item(pixelOffset);
87        unsigned char g = srcPixelArray->item(pixelOffset + 1);
88        unsigned char b = srcPixelArray->item(pixelOffset + 2);
89
90        double luma = (r * 0.2125 + g * 0.7154 + b * 0.0721) * ((double)a / 255.0);
91        srcPixelArray->set(pixelOffset + 3, luma);
92    }
93    putByteArray(Unmultiplied, srcPixelArray.get(), luminanceRect.size(), luminanceRect, IntPoint());
94}
95
96void ImageBuffer::convertToLuminanceMask()
97{
98    // Add platform specific functions with platformConvertToLuminanceMask here later.
99    genericConvertToLuminanceMask();
100}
101
102#if !USE(CAIRO)
103PlatformLayer* ImageBuffer::platformLayer() const
104{
105    return 0;
106}
107#endif
108
109bool ImageBuffer::copyToPlatformTexture(GraphicsContext3D&, Platform3DObject, GC3Denum, bool, bool)
110{
111    return false;
112}
113
114std::unique_ptr<ImageBuffer> ImageBuffer::createCompatibleBuffer(const FloatSize& size, float resolutionScale, ColorSpace colorSpace, const GraphicsContext* context, bool)
115{
116    return create(size, resolutionScale, colorSpace, context->isAcceleratedContext() ? Accelerated : Unaccelerated);
117}
118
119}
120