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