1a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)/* 2a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. 3a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * Copyright (C) Research In Motion Limited 2009-2010. All rights reserved. 4a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * 5a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without 6a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * modification, are permitted provided that the following conditions 7a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * are met: 8a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * 1. Redistributions of source code must retain the above copyright 9a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * notice, this list of conditions and the following disclaimer. 10a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright 11a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * notice, this list of conditions and the following disclaimer in the 12a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * documentation and/or other materials provided with the distribution. 13a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * 14a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 15a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 18a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 19a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 20a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 22a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) */ 26a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 27a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#ifndef ImageDecoder_h 28a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#define ImageDecoder_h 29a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 30a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "SkColorPriv.h" 31a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "platform/PlatformExport.h" 32a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "platform/PlatformScreen.h" 33a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "platform/SharedBuffer.h" 34a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "platform/graphics/ImageSource.h" 35a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "platform/image-decoders/ImageFrame.h" 36a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "public/platform/Platform.h" 37a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "wtf/Assertions.h" 38a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "wtf/RefPtr.h" 39a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "wtf/Vector.h" 409e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)#include "wtf/text/WTFString.h" 41a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 42a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#if USE(QCMSLIB) 43a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "qcms.h" 44a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#endif 45a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 467242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tuccitypedef Vector<char> ColorProfile; 477242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci 48c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink { 49c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) 50c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)// ImagePlanes can be used to decode color components into provided buffers instead of using an ImageFrame. 51c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)class PLATFORM_EXPORT ImagePlanes { 52c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)public: 53c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) ImagePlanes(); 547242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci ImagePlanes(void* planes[3], size_t rowBytes[3]); 55c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) 56c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) void* plane(int); 57c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) size_t rowBytes(int) const; 58c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) 59c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)private: 60c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) void* m_planes[3]; 61c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) size_t m_rowBytes[3]; 62c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)}; 63a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 64a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)// ImageDecoder is a base for all format-specific decoders 65a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)// (e.g. JPEGImageDecoder). This base manages the ImageFrame cache. 66a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)// 67a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)class PLATFORM_EXPORT ImageDecoder { 68a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) WTF_MAKE_NONCOPYABLE(ImageDecoder); WTF_MAKE_FAST_ALLOCATED; 69a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)public: 707242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci enum SizeType { ActualSize, SizeForMemoryAllocation }; 717242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci 72a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) static const size_t noDecodedImageByteLimit = blink::Platform::noDecodedImageByteLimit; 73a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 74a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) ImageDecoder(ImageSource::AlphaOption alphaOption, ImageSource::GammaAndColorProfileOption gammaAndColorProfileOption, size_t maxDecodedBytes) 75a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) : m_premultiplyAlpha(alphaOption == ImageSource::AlphaPremultiplied) 76a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) , m_ignoreGammaAndColorProfile(gammaAndColorProfileOption == ImageSource::GammaAndColorProfileIgnored) 77a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) , m_maxDecodedBytes(maxDecodedBytes) 78a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) , m_sizeAvailable(false) 79a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) , m_isAllDataReceived(false) 80a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) , m_failed(false) { } 81a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 82a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual ~ImageDecoder() { } 83a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 84a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Returns a caller-owned decoder of the appropriate type. Returns 0 if 85a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // we can't sniff a supported type from the provided data (possibly 86a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // because there isn't enough data yet). 87a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Sets m_maxDecodedBytes to Platform::maxImageDecodedBytes(). 88a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) static PassOwnPtr<ImageDecoder> create(const SharedBuffer& data, ImageSource::AlphaOption, ImageSource::GammaAndColorProfileOption); 89a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 90a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Returns a decoder with custom maxDecodedSize. 91a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) static PassOwnPtr<ImageDecoder> create(const SharedBuffer& data, ImageSource::AlphaOption, ImageSource::GammaAndColorProfileOption, size_t maxDecodedSize); 92a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 93a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual String filenameExtension() const = 0; 94a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 95a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) bool isAllDataReceived() const { return m_isAllDataReceived; } 96a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 97a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual void setData(SharedBuffer* data, bool allDataReceived) 98a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) { 99a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) if (m_failed) 100a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) return; 101a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) m_data = data; 102a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) m_isAllDataReceived = allDataReceived; 103a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) } 104a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 105a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual bool isSizeAvailable() 106a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) { 107a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) return !m_failed && m_sizeAvailable; 108a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) } 109a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 1107242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci bool isSizeAvailable() const 1117242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci { 1127242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci return !m_failed && m_sizeAvailable; 1137242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci } 1147242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci 115a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual IntSize size() const { return m_size; } 116a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 117a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Decoders which downsample images should override this method to 118a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // return the actual decoded size. 119a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual IntSize decodedSize() const { return size(); } 120a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 121c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) // Decoders which support YUV decoding can override this to 122c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) // give potentially different sizes per component. 1237242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci virtual IntSize decodedYUVSize(int component, SizeType) const { return decodedSize(); } 124c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) 125a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // This will only differ from size() for ICO (where each frame is a 126a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // different icon) or other formats where different frames are different 127a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // sizes. This does NOT differ from size() for GIF or WebP, since 128a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // decoding GIF or WebP composites any smaller frames against previous 129a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // frames to create full-size frames. 130a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual IntSize frameSizeAtIndex(size_t) const 131a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) { 132a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) return size(); 133a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) } 134a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 135a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Returns whether the size is legal (i.e. not going to result in 136a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // overflow elsewhere). If not, marks decoding as failed. 137a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual bool setSize(unsigned width, unsigned height) 138a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) { 139a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) if (sizeCalculationMayOverflow(width, height)) 140a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) return setFailed(); 141a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) m_size = IntSize(width, height); 142a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) m_sizeAvailable = true; 143a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) return true; 144a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) } 145a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 146a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Lazily-decodes enough of the image to get the frame count (if 147a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // possible), without decoding the individual frames. 148a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // FIXME: Right now that has to be done by each subclass; factor the 149a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // decode call out and use it here. 150a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual size_t frameCount() { return 1; } 151a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 152a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual int repetitionCount() const { return cAnimationNone; } 153a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 154a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Decodes as much of the requested frame as possible, and returns an 155a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // ImageDecoder-owned pointer. 156a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual ImageFrame* frameBufferAtIndex(size_t) = 0; 157a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 158a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Make the best effort guess to check if the requested frame has alpha channel. 159a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual bool frameHasAlphaAtIndex(size_t) const; 160a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 161a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Whether or not the frame is fully received. 162a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual bool frameIsCompleteAtIndex(size_t) const; 163a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 164a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Duration for displaying a frame in seconds. This method is used by animated images only. 165a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual float frameDurationAtIndex(size_t) const { return 0; } 166a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 167a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Number of bytes in the decoded frame requested. Return 0 if not yet decoded. 168a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual unsigned frameBytesAtIndex(size_t) const; 169a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 170323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) ImageOrientation orientation() const { return m_orientation; } 171323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) 1726f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch static bool deferredImageDecodingEnabled(); 1736f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch 174a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) void setIgnoreGammaAndColorProfile(bool flag) { m_ignoreGammaAndColorProfile = flag; } 175a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) bool ignoresGammaAndColorProfile() const { return m_ignoreGammaAndColorProfile; } 176a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 177323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles) virtual bool hasColorProfile() const { return false; } 178a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 179323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)#if USE(QCMSLIB) 180a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) enum { iccColorProfileHeaderLength = 128 }; 181a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 182a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) static bool rgbColorProfile(const char* profileData, unsigned profileLength) 183a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) { 184a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) ASSERT_UNUSED(profileLength, profileLength >= iccColorProfileHeaderLength); 185a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 186a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) return !memcmp(&profileData[16], "RGB ", 4); 187a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) } 188a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 189a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) static bool inputDeviceColorProfile(const char* profileData, unsigned profileLength) 190a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) { 191a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) ASSERT_UNUSED(profileLength, profileLength >= iccColorProfileHeaderLength); 192a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 193a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) return !memcmp(&profileData[12], "mntr", 4) || !memcmp(&profileData[12], "scnr", 4); 194a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) } 195a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 19609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) class OutputDeviceProfile { 19709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) public: 19809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) OutputDeviceProfile() 19909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) : m_outputDeviceProfile(0) 20009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) { 2017242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci ColorProfile profile = screenColorProfile(); 202a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) if (!profile.isEmpty()) 20309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) m_outputDeviceProfile = qcms_profile_from_memory(profile.data(), profile.size()); 2047242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci 20509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) if (m_outputDeviceProfile && qcms_profile_is_bogus(m_outputDeviceProfile)) { 20609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) qcms_profile_release(m_outputDeviceProfile); 20709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) m_outputDeviceProfile = 0; 208a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) } 2097242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci 21009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) if (!m_outputDeviceProfile) 21109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) m_outputDeviceProfile = qcms_profile_sRGB(); 21209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) if (m_outputDeviceProfile) 21309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) qcms_profile_precache_output_transform(m_outputDeviceProfile); 214a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) } 21509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 21609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) qcms_profile* profile() const { return m_outputDeviceProfile; } 21709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 21809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) private: 2197242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci static ColorProfile screenColorProfile() 2207242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci { 2217242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci // FIXME: Add optional ICCv4 support and support for multiple monitors. 2227242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci WebVector<char> profile; 2237242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci Platform::current()->screenColorProfile(&profile); 2247242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci 2257242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci ColorProfile colorProfile; 2267242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci colorProfile.append(profile.data(), profile.size()); 2277242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci return colorProfile; 2287242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci } 2297242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci 23009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) qcms_profile* m_outputDeviceProfile; 23109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) }; 23209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 23309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) static qcms_profile* qcmsOutputDeviceProfile() 23409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) { 23509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) AtomicallyInitializedStatic(OutputDeviceProfile*, outputDeviceProfile = new OutputDeviceProfile()); 23609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 23709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) return outputDeviceProfile->profile(); 238a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) } 239a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#endif 240a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 241a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Sets the "decode failure" flag. For caller convenience (since so 242a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // many callers want to return false after calling this), returns false 243a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // to enable easy tailcalling. Subclasses may override this to also 244a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // clean up any local data. 245a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual bool setFailed() 246a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) { 247a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) m_failed = true; 248a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) return false; 249a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) } 250a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 251a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) bool failed() const { return m_failed; } 252a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 253a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Clears decoded pixel data from all frames except the provided frame. 254a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Callers may pass WTF::kNotFound to clear all frames. 255a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Note: If |m_frameBufferCache| contains only one frame, it won't be cleared. 256a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Returns the number of bytes of frame data actually cleared. 257a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual size_t clearCacheExceptFrame(size_t); 258a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 259a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // If the image has a cursor hot-spot, stores it in the argument 260a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // and returns true. Otherwise returns false. 261a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual bool hotSpot(IntPoint&) const { return false; } 262a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 263a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual void setMemoryAllocator(SkBitmap::Allocator* allocator) 264a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) { 265a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // FIXME: this doesn't work for images with multiple frames. 266a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) if (m_frameBufferCache.isEmpty()) { 267a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) m_frameBufferCache.resize(1); 268a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) m_frameBufferCache[0].setRequiredPreviousFrameIndex( 269a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) findRequiredPreviousFrame(0, false)); 270a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) } 271a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) m_frameBufferCache[0].setMemoryAllocator(allocator); 272a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) } 273a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 2747242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci virtual bool canDecodeToYUV() const { return false; } 275c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) virtual bool decodeToYUV() { return false; } 2767242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci virtual void setImagePlanes(PassOwnPtr<ImagePlanes>) { } 277c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles) 278a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)protected: 279a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Calculates the most recent frame whose image data may be needed in 280a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // order to decode frame |frameIndex|, based on frame disposal methods 281a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // and |frameRectIsOpaque|, where |frameRectIsOpaque| signifies whether 282a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // the rectangle of frame at |frameIndex| is known to be opaque. 283a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // If no previous frame's data is required, returns WTF::kNotFound. 284a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // 285a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // This function requires that the previous frame's 286a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // |m_requiredPreviousFrameIndex| member has been set correctly. The 287a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // easiest way to ensure this is for subclasses to call this method and 288a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // store the result on the frame via setRequiredPreviousFrameIndex() 289a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // as soon as the frame has been created and parsed sufficiently to 290a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // determine the disposal method; assuming this happens for all frames 291a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // in order, the required invariant will hold. 292a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // 293a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Image formats which do not use more than one frame do not need to 294a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // worry about this; see comments on 295a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // ImageFrame::m_requiredPreviousFrameIndex. 296a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) size_t findRequiredPreviousFrame(size_t frameIndex, bool frameRectIsOpaque); 297a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 298a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) virtual void clearFrameBuffer(size_t frameIndex); 299a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 300a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) RefPtr<SharedBuffer> m_data; // The encoded data. 301a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) Vector<ImageFrame, 1> m_frameBufferCache; 302a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) bool m_premultiplyAlpha; 303a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) bool m_ignoreGammaAndColorProfile; 304a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) ImageOrientation m_orientation; 305a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 306a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // The maximum amount of memory a decoded image should require. Ideally, 307a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // image decoders should downsample large images to fit under this limit 308a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // (and then return the downsampled size from decodedSize()). Ignoring 309a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // this limit can cause excessive memory use or even crashes on low- 310a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // memory devices. 311a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) size_t m_maxDecodedBytes; 312a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 313a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)private: 314a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // Some code paths compute the size of the image as "width * height * 4" 315a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) // and return it as a (signed) int. Avoid overflow. 316a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) static bool sizeCalculationMayOverflow(unsigned width, unsigned height) 317a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) { 318a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) unsigned long long total_size = static_cast<unsigned long long>(width) 319a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * static_cast<unsigned long long>(height); 320a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) return total_size > ((1 << 29) - 1); 321a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) } 322a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 323a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) IntSize m_size; 324a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) bool m_sizeAvailable; 325a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) bool m_isAllDataReceived; 326a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) bool m_failed; 327a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}; 328a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 329c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink 330a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 331a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#endif 332