2 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
3 * Copyright (C) 2007-2008 Torch Mobile, Inc.
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 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.
30 #include "ImageOrientation.h"
32 #include <wtf/Forward.h>
33 #include <wtf/Noncopyable.h>
34 #include <wtf/Vector.h>
38 class wxGraphicsBitmap;
40 typedef struct CGImageSource* CGImageSourceRef;
41 typedef struct CGImage* CGImageRef;
42 typedef const struct __CFData* CFDataRef;
49 #include "NativeImageCairo.h"
52 class NativeImageSkia;
55 #include "SharedBitmap.h"
60 class ImageOrientation;
66 typedef CGImageSourceRef NativeImageSourcePtr;
67 typedef CGImageRef NativeImagePtr;
68 #elif PLATFORM(OPENVG)
70 class TiledImageOpenVG;
71 typedef ImageDecoder* NativeImageSourcePtr;
72 typedef TiledImageOpenVG* NativeImagePtr;
75 typedef ImageDecoder* NativeImageSourcePtr;
78 typedef wxGraphicsBitmap* NativeImagePtr;
80 typedef wxBitmap* NativeImagePtr;
83 typedef WebCore::NativeImageCairo* NativeImagePtr;
85 typedef WebCore::NativeImageSkia* NativeImagePtr;
87 typedef RefPtr<SharedBitmap> NativeImagePtr;
88 #elif PLATFORM(BLACKBERRY)
90 typedef ImageDecoder* NativeImageSourcePtr;
91 typedef void* NativeImagePtr;
93 typedef QImage* NativeImagePtr;
97 // Right now GIFs are the only recognized image format that supports animation.
98 // The animation system and the constants below are designed with this in mind.
99 // GIFs have an optional 16-bit unsigned loop count that describes how an
100 // animated GIF should be cycled. If the loop count is absent, the animation
101 // cycles once; if it is 0, the animation cycles infinitely; otherwise the
102 // animation plays n + 1 cycles (where n is the specified loop count). If the
103 // GIF decoder defaults to cAnimationLoopOnce in the absence of any loop count
104 // and translates an explicit "0" loop count to cAnimationLoopInfinite, then we
105 // get a couple of nice side effects:
106 // * By making cAnimationLoopOnce be 0, we allow the animation cycling code in
107 // BitmapImage.cpp to avoid special-casing it, and simply treat all
108 // non-negative loop counts identically.
109 // * By making the other two constants negative, we avoid conflicts with any
110 // real loop count values.
111 const int cAnimationLoopOnce = 0;
112 const int cAnimationLoopInfinite = -1;
113 const int cAnimationNone = -2;
116 WTF_MAKE_NONCOPYABLE(ImageSource);
120 AlphaNotPremultiplied
123 enum GammaAndColorProfileOption {
124 GammaAndColorProfileApplied,
125 GammaAndColorProfileIgnored
129 enum ShouldSkipMetadata {
135 ImageSource(AlphaOption alphaOption = AlphaPremultiplied, GammaAndColorProfileOption gammaAndColorProfileOption = GammaAndColorProfileApplied);
138 // Tells the ImageSource that the Image no longer cares about decoded frame
139 // data -- at all (if |destroyAll| is true), or before frame
140 // |clearBeforeFrame| (if |destroyAll| is false). The ImageSource should
141 // delete cached decoded data for these frames where possible to keep memory
142 // usage low. When |destroyAll| is true, the ImageSource should also reset
143 // any local state so that decoding can begin again.
145 // Implementations that delete less than what's specified above waste
146 // memory. Implementations that delete more may burn CPU re-decoding frames
147 // that could otherwise have been cached, or encounter errors if they're
148 // asked to decode frames they can't decode due to the loss of previous
151 // Callers should not call clear(false, n) and subsequently call
152 // createFrameAtIndex(m) with m < n, unless they first call clear(true).
153 // This ensures that stateful ImageSources/decoders will work properly.
155 // The |data| and |allDataReceived| parameters should be supplied by callers
156 // who set |destroyAll| to true if they wish to be able to continue using
157 // the ImageSource. This way implementations which choose to destroy their
158 // decoders in some cases can reconstruct them correctly.
159 void clear(bool destroyAll,
160 size_t clearBeforeFrame = 0,
161 SharedBuffer* data = NULL,
162 bool allDataReceived = false);
164 bool initialized() const;
166 void setData(SharedBuffer* data, bool allDataReceived);
167 String filenameExtension() const;
169 bool isSizeAvailable();
170 IntSize size(RespectImageOrientationEnum = DoNotRespectImageOrientation) const;
171 IntSize frameSizeAtIndex(size_t, RespectImageOrientationEnum = DoNotRespectImageOrientation) const;
173 bool getHotSpot(IntPoint&) const;
175 size_t bytesDecodedToDetermineProperties() const;
177 int repetitionCount();
179 size_t frameCount() const;
181 // Callers should not call this after calling clear() with a higher index;
182 // see comments on clear() above.
183 NativeImagePtr createFrameAtIndex(size_t);
185 float frameDurationAtIndex(size_t);
186 bool frameHasAlphaAtIndex(size_t); // Whether or not the frame actually used any alpha.
187 bool frameIsCompleteAtIndex(size_t); // Whether or not the frame is completely decoded.
188 ImageOrientation orientationAtIndex(size_t) const; // EXIF image orientation
190 #if ENABLE(IMAGE_DECODER_DOWN_SAMPLING) || ENABLE(TIZEN_JPEG_IMAGE_SCALE_DECODING)
191 static unsigned maxPixelsPerDecodedImage() { return s_maxPixelsPerDecodedImage; }
192 static void setMaxPixelsPerDecodedImage(unsigned maxPixels) { s_maxPixelsPerDecodedImage = maxPixels; }
195 #if ENABLE(TIZEN_JPEGIMAGE_DECODING_THREAD)
196 NativeImageSourcePtr decoder() { return m_decoder; }
200 NativeImageSourcePtr m_decoder;
201 AlphaOption m_alphaOption;
202 GammaAndColorProfileOption m_gammaAndColorProfileOption;
203 #if ENABLE(IMAGE_DECODER_DOWN_SAMPLING) || ENABLE(TIZEN_JPEG_IMAGE_SCALE_DECODING)
204 static unsigned s_maxPixelsPerDecodedImage;