1/* 2 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved. 3 * Copyright (C) 2007-2008 Torch Mobile, Inc. 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#ifndef ImageSource_h 28#define ImageSource_h 29 30#include "ImageOrientation.h" 31#include "NativeImagePtr.h" 32 33#include <wtf/Forward.h> 34#include <wtf/Noncopyable.h> 35#include <wtf/Vector.h> 36 37#if USE(CG) 38typedef struct CGImageSource* CGImageSourceRef; 39typedef const struct __CFData* CFDataRef; 40#endif 41 42namespace WebCore { 43 44class ImageOrientation; 45class IntPoint; 46class IntSize; 47class SharedBuffer; 48 49#if USE(CG) 50typedef CGImageSourceRef NativeImageDecoderPtr; 51#else 52class ImageDecoder; 53typedef ImageDecoder* NativeImageDecoderPtr; 54#endif 55 56#if USE(CG) 57#define NativeImageDecoder ImageDecoder 58#else 59typedef ImageDecoder NativeImageDecoder; 60#endif 61 62// Right now GIFs are the only recognized image format that supports animation. 63// The animation system and the constants below are designed with this in mind. 64// GIFs have an optional 16-bit unsigned loop count that describes how an 65// animated GIF should be cycled. If the loop count is absent, the animation 66// cycles once; if it is 0, the animation cycles infinitely; otherwise the 67// animation plays n + 1 cycles (where n is the specified loop count). If the 68// GIF decoder defaults to cAnimationLoopOnce in the absence of any loop count 69// and translates an explicit "0" loop count to cAnimationLoopInfinite, then we 70// get a couple of nice side effects: 71// * By making cAnimationLoopOnce be 0, we allow the animation cycling code in 72// BitmapImage.cpp to avoid special-casing it, and simply treat all 73// non-negative loop counts identically. 74// * By making the other two constants negative, we avoid conflicts with any 75// real loop count values. 76const int cAnimationLoopOnce = 0; 77const int cAnimationLoopInfinite = -1; 78const int cAnimationNone = -2; 79 80class ImageSource { 81 WTF_MAKE_NONCOPYABLE(ImageSource); 82public: 83 enum AlphaOption { 84 AlphaPremultiplied, 85 AlphaNotPremultiplied 86 }; 87 88 enum GammaAndColorProfileOption { 89 GammaAndColorProfileApplied, 90 GammaAndColorProfileIgnored 91 }; 92 93#if USE(CG) 94 enum ShouldSkipMetadata { 95 DoNotSkipMetadata, 96 SkipMetadata 97 }; 98#endif 99 100 ImageSource(AlphaOption alphaOption = AlphaPremultiplied, GammaAndColorProfileOption gammaAndColorProfileOption = GammaAndColorProfileApplied); 101 ~ImageSource(); 102 103 // Tells the ImageSource that the Image no longer cares about decoded frame 104 // data -- at all (if |destroyAll| is true), or before frame 105 // |clearBeforeFrame| (if |destroyAll| is false). The ImageSource should 106 // delete cached decoded data for these frames where possible to keep memory 107 // usage low. When |destroyAll| is true, the ImageSource should also reset 108 // any local state so that decoding can begin again. 109 // 110 // Implementations that delete less than what's specified above waste 111 // memory. Implementations that delete more may burn CPU re-decoding frames 112 // that could otherwise have been cached, or encounter errors if they're 113 // asked to decode frames they can't decode due to the loss of previous 114 // decoded frames. 115 // 116 // Callers should not call clear(false, n) and subsequently call 117 // createFrameAtIndex(m) with m < n, unless they first call clear(true). 118 // This ensures that stateful ImageSources/decoders will work properly. 119 // 120 // The |data| and |allDataReceived| parameters should be supplied by callers 121 // who set |destroyAll| to true if they wish to be able to continue using 122 // the ImageSource. This way implementations which choose to destroy their 123 // decoders in some cases can reconstruct them correctly. 124 void clear(bool destroyAll, 125 size_t clearBeforeFrame = 0, 126 SharedBuffer* data = NULL, 127 bool allDataReceived = false); 128 129 bool initialized() const; 130 131 void setData(SharedBuffer* data, bool allDataReceived); 132 String filenameExtension() const; 133 134 bool isSizeAvailable(); 135 IntSize size(RespectImageOrientationEnum = DoNotRespectImageOrientation) const; 136 IntSize frameSizeAtIndex(size_t, RespectImageOrientationEnum = DoNotRespectImageOrientation) const; 137 138 bool getHotSpot(IntPoint&) const; 139 140 size_t bytesDecodedToDetermineProperties() const; 141 142 int repetitionCount(); 143 144 size_t frameCount() const; 145 146 // Callers should not call this after calling clear() with a higher index; 147 // see comments on clear() above. 148 PassNativeImagePtr createFrameAtIndex(size_t); 149 150 float frameDurationAtIndex(size_t); 151 bool frameHasAlphaAtIndex(size_t); // Whether or not the frame actually used any alpha. 152 bool frameIsCompleteAtIndex(size_t); // Whether or not the frame is completely decoded. 153 ImageOrientation orientationAtIndex(size_t) const; // EXIF image orientation 154 155 // Return the number of bytes in the decoded frame. If the frame is not yet 156 // decoded then return 0. 157 unsigned frameBytesAtIndex(size_t) const; 158 159#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING) 160 static unsigned maxPixelsPerDecodedImage() { return s_maxPixelsPerDecodedImage; } 161 static void setMaxPixelsPerDecodedImage(unsigned maxPixels) { s_maxPixelsPerDecodedImage = maxPixels; } 162#endif 163 164private: 165 NativeImageDecoderPtr m_decoder; 166 167#if !USE(CG) 168 AlphaOption m_alphaOption; 169 GammaAndColorProfileOption m_gammaAndColorProfileOption; 170#endif 171#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING) 172 static unsigned s_maxPixelsPerDecodedImage; 173#endif 174}; 175 176} 177 178#endif 179