2 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
3 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "ImageBuffer.h"
30 #include "GraphicsContext.h"
32 #include <wtf/MathExtras.h>
36 static const float MaxClampedLength = 4096;
37 static const float MaxClampedArea = MaxClampedLength * MaxClampedLength;
39 bool ImageBuffer::isSizeClamped(const FloatSize& size)
41 if (size.width() < 0 && size.height() < 0)
47 return floorf(size.height()) * floorf(size.width()) <= MaxClampedArea;
50 bool ImageBuffer::isSizeClamped(const FloatSize& size, FloatSize& scale)
52 FloatSize scaledSize(size);
53 scaledSize.scale(scale.width(), scale.height());
55 if (isSizeClamped(scaledSize))
58 // The area of scaled size is bigger than the upper limit, adjust the scale to fit.
59 scale.scale(sqrtf(MaxClampedArea / (scaledSize.width() * scaledSize.height())));
60 ASSERT(isSizeClamped(size, scale));
64 FloatSize ImageBuffer::clampedSize(const FloatSize& size)
66 return size.shrunkTo(FloatSize(MaxClampedLength, MaxClampedLength));
69 FloatSize ImageBuffer::clampedSize(const FloatSize& size, FloatSize& scale)
74 FloatSize clampedSize = ImageBuffer::clampedSize(size);
75 scale = FloatSize(clampedSize.width() / size.width(), clampedSize.height() / size.height());
76 ASSERT(isSizeClamped(clampedSize));
77 ASSERT(isSizeClamped(size, scale));
81 FloatRect ImageBuffer::clampedRect(const FloatRect& rect)
83 return FloatRect(rect.location(), clampedSize(rect.size()));
87 void ImageBuffer::transformColorSpace(ColorSpace srcColorSpace, ColorSpace dstColorSpace)
89 DEPRECATED_DEFINE_STATIC_LOCAL(Vector<int>, deviceRgbLUT, ());
90 DEPRECATED_DEFINE_STATIC_LOCAL(Vector<int>, linearRgbLUT, ());
92 if (srcColorSpace == dstColorSpace)
95 // only sRGB <-> linearRGB are supported at the moment
96 if ((srcColorSpace != ColorSpaceLinearRGB && srcColorSpace != ColorSpaceDeviceRGB)
97 || (dstColorSpace != ColorSpaceLinearRGB && dstColorSpace != ColorSpaceDeviceRGB))
100 if (dstColorSpace == ColorSpaceLinearRGB) {
101 if (linearRgbLUT.isEmpty()) {
102 for (unsigned i = 0; i < 256; i++) {
103 float color = i / 255.0f;
104 color = (color <= 0.04045f ? color / 12.92f : pow((color + 0.055f) / 1.055f, 2.4f));
105 color = std::max(0.0f, color);
106 color = std::min(1.0f, color);
107 linearRgbLUT.append(static_cast<int>(round(color * 255)));
110 platformTransformColorSpace(linearRgbLUT);
111 } else if (dstColorSpace == ColorSpaceDeviceRGB) {
112 if (deviceRgbLUT.isEmpty()) {
113 for (unsigned i = 0; i < 256; i++) {
114 float color = i / 255.0f;
115 color = (powf(color, 1.0f / 2.4f) * 1.055f) - 0.055f;
116 color = std::max(0.0f, color);
117 color = std::min(1.0f, color);
118 deviceRgbLUT.append(static_cast<int>(round(color * 255)));
121 platformTransformColorSpace(deviceRgbLUT);
126 inline void ImageBuffer::genericConvertToLuminanceMask()
128 IntRect luminanceRect(IntPoint(), internalSize());
129 RefPtr<Uint8ClampedArray> srcPixelArray = getUnmultipliedImageData(luminanceRect);
131 unsigned pixelArrayLength = srcPixelArray->length();
132 for (unsigned pixelOffset = 0; pixelOffset < pixelArrayLength; pixelOffset += 4) {
133 unsigned char a = srcPixelArray->item(pixelOffset + 3);
136 unsigned char r = srcPixelArray->item(pixelOffset);
137 unsigned char g = srcPixelArray->item(pixelOffset + 1);
138 unsigned char b = srcPixelArray->item(pixelOffset + 2);
140 double luma = (r * 0.2125 + g * 0.7154 + b * 0.0721) * ((double)a / 255.0);
141 srcPixelArray->set(pixelOffset + 3, luma);
143 putByteArray(Unmultiplied, srcPixelArray.get(), luminanceRect.size(), luminanceRect, IntPoint());
146 void ImageBuffer::convertToLuminanceMask()
148 // Add platform specific functions with platformConvertToLuminanceMask here later.
149 genericConvertToLuminanceMask();
153 PlatformLayer* ImageBuffer::platformLayer() const
159 bool ImageBuffer::copyToPlatformTexture(GraphicsContext3D&, Platform3DObject, GC3Denum, bool, bool)
164 std::unique_ptr<ImageBuffer> ImageBuffer::createCompatibleBuffer(const FloatSize& size, float resolutionScale, ColorSpace colorSpace, const GraphicsContext* context, bool)
166 return create(size, resolutionScale, colorSpace, context->isAcceleratedContext() ? Accelerated : Unaccelerated);