initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / ImageBuffer.cpp
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 COMPUTER, 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 COMPUTER, 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 "IntRect.h"
31 #include <wtf/MathExtras.h>
32
33 namespace WebCore {
34
35 #if !USE(CG)
36 void ImageBuffer::transformColorSpace(ColorSpace srcColorSpace, ColorSpace dstColorSpace)
37 {
38     if (srcColorSpace == dstColorSpace)
39         return;
40
41     // only sRGB <-> linearRGB are supported at the moment
42     if ((srcColorSpace != ColorSpaceLinearRGB && srcColorSpace != ColorSpaceDeviceRGB)
43         || (dstColorSpace != ColorSpaceLinearRGB && dstColorSpace != ColorSpaceDeviceRGB))
44         return;
45
46     if (dstColorSpace == ColorSpaceLinearRGB) {
47         if (m_linearRgbLUT.isEmpty()) {
48             for (unsigned i = 0; i < 256; i++) {
49                 float color = i  / 255.0f;
50                 color = (color <= 0.04045f ? color / 12.92f : pow((color + 0.055f) / 1.055f, 2.4f));
51                 color = std::max(0.0f, color);
52                 color = std::min(1.0f, color);
53                 m_linearRgbLUT.append(static_cast<int>(color * 255));
54             }
55         }
56         platformTransformColorSpace(m_linearRgbLUT);
57     } else if (dstColorSpace == ColorSpaceDeviceRGB) {
58         if (m_deviceRgbLUT.isEmpty()) {
59             for (unsigned i = 0; i < 256; i++) {
60                 float color = i / 255.0f;
61                 color = (powf(color, 1.0f / 2.4f) * 1.055f) - 0.055f;
62                 color = std::max(0.0f, color);
63                 color = std::min(1.0f, color);
64                 m_deviceRgbLUT.append(static_cast<int>(color * 255));
65             }
66         }
67         platformTransformColorSpace(m_deviceRgbLUT);
68     }
69 }
70 #endif // USE(CG)
71
72 inline void ImageBuffer::genericConvertToLuminanceMask()
73 {
74     IntRect luminanceRect(IntPoint(), size());
75     RefPtr<ByteArray> srcPixelArray = getUnmultipliedImageData(luminanceRect);
76     
77     unsigned pixelArrayLength = srcPixelArray->length();
78     for (unsigned pixelOffset = 0; pixelOffset < pixelArrayLength; pixelOffset += 4) {
79         unsigned char a = srcPixelArray->get(pixelOffset + 3);
80         if (!a)
81             continue;
82         unsigned char r = srcPixelArray->get(pixelOffset);
83         unsigned char g = srcPixelArray->get(pixelOffset + 1);
84         unsigned char b = srcPixelArray->get(pixelOffset + 2);
85         
86         double luma = (r * 0.2125 + g * 0.7154 + b * 0.0721) * ((double)a / 255.0);
87         srcPixelArray->set(pixelOffset + 3, luma);
88     }
89     putUnmultipliedImageData(srcPixelArray.get(), luminanceRect.size(), luminanceRect, IntPoint());
90 }
91
92 void ImageBuffer::convertToLuminanceMask()
93 {
94     // Add platform specific functions with platformConvertToLuminanceMask here later.
95     genericConvertToLuminanceMask();
96 }
97
98 #if USE(ACCELERATED_COMPOSITING) && !USE(SKIA)
99 PlatformLayer* ImageBuffer::platformLayer() const
100 {
101     return 0;
102 }
103 #endif
104
105 }