]> git.saurik.com Git - iphone-api.git/blob - WebCore/BitmapImage.h
Add support for new WinterBoard Settings features.
[iphone-api.git] / WebCore / BitmapImage.h
1 /*
2 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
3 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
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 BitmapImage_h
28 #define BitmapImage_h
29
30 #include "Image.h"
31 #include "Color.h"
32 #include "IntSize.h"
33
34 #if PLATFORM(MAC)
35 #include <wtf/RetainPtr.h>
36 #endif
37
38 #if PLATFORM(WIN)
39 typedef struct HBITMAP__ *HBITMAP;
40 #endif
41
42 namespace WebCore {
43 struct FrameData;
44 }
45
46 // This complicated-looking declaration tells the FrameData Vector that it should copy without
47 // invoking our constructor or destructor. This allows us to have a vector even for a struct
48 // that's not copyable.
49 namespace WTF {
50 template<> class VectorTraits<WebCore::FrameData> : public SimpleClassVectorTraits {};
51 }
52
53 namespace WebCore {
54
55 template <typename T> class Timer;
56
57 // ================================================
58 // FrameData Class
59 // ================================================
60
61 struct FrameData : Noncopyable {
62 FrameData()
63 : m_frame(0)
64 , m_haveMetadata(false)
65 , m_isComplete(false)
66 #if ENABLE(RESPECT_EXIF_ORIENTATION)
67 , m_orientation(0)
68 #endif
69 , m_bytes(0)
70 , m_scale(0.0f)
71 , m_haveInfo(false)
72 , m_duration(0)
73 , m_hasAlpha(true)
74 {
75 }
76
77 ~FrameData()
78 {
79 clear(true);
80 }
81
82 // Clear the cached image data on the frame, and (optionally) the metadata.
83 // Returns whether there was cached image data to clear.
84 bool clear(bool clearMetadata);
85
86 NativeImagePtr m_frame;
87 bool m_haveMetadata;
88 bool m_isComplete;
89 #if ENABLE(RESPECT_EXIF_ORIENTATION)
90 int m_orientation;
91 #endif
92 ssize_t m_bytes;
93 float m_scale;
94 bool m_haveInfo;
95 float m_duration;
96 bool m_hasAlpha;
97 };
98
99 // =================================================
100 // BitmapImage Class
101 // =================================================
102
103 class BitmapImage : public Image {
104 friend class GeneratedImage;
105 friend class GraphicsContext;
106 public:
107 static PassRefPtr<BitmapImage> create(NativeImagePtr nativeImage, ImageObserver* observer = 0)
108 {
109 return adoptRef(new BitmapImage(nativeImage, observer));
110 }
111 static PassRefPtr<BitmapImage> create(ImageObserver* observer = 0)
112 {
113 return adoptRef(new BitmapImage(observer));
114 }
115 ~BitmapImage();
116
117 virtual bool isBitmapImage() const { return true; }
118
119 virtual bool hasSingleSecurityOrigin() const { return true; }
120
121 virtual IntSize size() const;
122 IntSize currentFrameSize() const;
123
124 virtual bool dataChanged(bool allDataReceived);
125 virtual String filenameExtension() const;
126
127 // It may look unusual that there is no start animation call as public API. This is because
128 // we start and stop animating lazily. Animation begins whenever someone draws the image. It will
129 // automatically pause once all observers no longer want to render the image anywhere.
130 virtual void stopAnimation();
131 virtual void resetAnimation();
132
133 virtual unsigned decodedSize() const { return m_decodedSize; }
134
135 #if PLATFORM(MAC)
136 // Accessors for native image formats.
137 virtual CFDataRef getTIFFRepresentation();
138 #endif
139
140 #if PLATFORM(CG)
141 virtual CGImageRef getCGImageRef();
142 #endif
143
144 #if PLATFORM(WIN)
145 virtual bool getHBITMAP(HBITMAP);
146 virtual bool getHBITMAPOfSize(HBITMAP, LPSIZE);
147 #endif
148
149 virtual NativeImagePtr nativeImageForCurrentFrame() { return frameAtIndex(currentFrame()); }
150
151 #if ENABLE(RESPECT_EXIF_ORIENTATION)
152 // EXIF orientation specified by EXIF spec
153 static const int ImageEXIFOrientationTopLeft = 1;
154 static const int ImageEXIFOrientationTopRight = 2;
155 static const int ImageEXIFOrientationBottomRight = 3;
156 static const int ImageEXIFOrientationBottomLeft = 4;
157 static const int ImageEXIFOrientationLeftTop = 5;
158 static const int ImageEXIFOrientationRightTop = 6;
159 static const int ImageEXIFOrientationRightBottom = 7;
160 static const int ImageEXIFOrientationLeftBottom = 8;
161 #endif
162
163 protected:
164 enum RepetitionCountStatus {
165 Unknown, // We haven't checked the source's repetition count.
166 Uncertain, // We have a repetition count, but it might be wrong (some GIFs have a count after the image data, and will report "loop once" until all data has been decoded).
167 Certain, // The repetition count is known to be correct.
168 };
169
170 BitmapImage(NativeImagePtr, ImageObserver* = 0);
171 BitmapImage(ImageObserver* = 0);
172
173 #if PLATFORM(WIN)
174 virtual void drawFrameMatchingSourceSize(GraphicsContext*, const FloatRect& dstRect, const IntSize& srcSize, CompositeOperator);
175 #endif
176 virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator);
177 #if PLATFORM(QT) || PLATFORM(WX)
178 virtual void drawPattern(GraphicsContext*, const FloatRect& srcRect, const TransformationMatrix& patternTransform,
179 const FloatPoint& phase, CompositeOperator, const FloatRect& destRect);
180 #endif
181 size_t currentFrame() const { return m_currentFrame; }
182 size_t frameCount();
183 NativeImagePtr frameAtIndex(size_t index, float scaleHint);
184 NativeImagePtr frameAtIndex(size_t);
185 bool frameIsCompleteAtIndex(size_t);
186 float frameDurationAtIndex(size_t);
187 bool frameHasAlphaAtIndex(size_t);
188 int frameOrientationAtIndex(size_t);
189
190 // Decodes and caches a frame. Never accessed except internally.
191 virtual unsigned animatedImageSize();
192 virtual void disableImageAnimation();
193 bool m_imageAnimationDisabled;
194 double m_progressiveLoadChunkTime;
195 unsigned m_progressiveLoadChunkCount;
196
197 void cacheFrame(size_t index, float scaleHint);
198
199 // Cache frame metadata without decoding image.
200 void cacheFrameInfo(size_t index);
201
202 // Called to invalidate cached data. When |destroyAll| is true, we wipe out
203 // the entire frame buffer cache and tell the image source to destroy
204 // everything; this is used when e.g. we want to free some room in the image
205 // cache. If |destroyAll| is false, we only delete frames up to the current
206 // one; this is used while animating large images to keep memory footprint
207 // low without redecoding the whole image on every frame.
208 virtual void destroyDecodedData(bool destroyAll = true);
209
210 // If the image is large enough, calls destroyDecodedData() and passes
211 // |destroyAll| along.
212 void destroyDecodedDataIfNecessary(bool destroyAll);
213
214 // Generally called by destroyDecodedData(), destroys whole-image metadata
215 // and notifies observers that the memory footprint has (hopefully)
216 // decreased by |framesCleared| times the size (in bytes) of a frame.
217 void destroyMetadataAndNotify(int framesCleared);
218
219 // Whether or not size is available yet.
220 bool isSizeAvailable();
221
222 // Animation.
223 int repetitionCount(bool imageKnownToBeComplete); // |imageKnownToBeComplete| should be set if the caller knows the entire image has been decoded.
224 bool shouldAnimate();
225 virtual void startAnimation(bool catchUpIfNecessary = true);
226 void advanceAnimation(Timer<BitmapImage>*);
227
228 // Function that does the real work of advancing the animation. When
229 // skippingFrames is true, we're in the middle of a loop trying to skip over
230 // a bunch of animation frames, so we should not do things like decode each
231 // one or notify our observers.
232 // Returns whether the animation was advanced.
233 bool internalAdvanceAnimation(bool skippingFrames);
234
235 // Handle platform-specific data
236 void initPlatformData();
237 void invalidatePlatformData();
238
239 // Checks to see if the image is a 1x1 solid color. We optimize these images and just do a fill rect instead.
240 void checkForSolidColor();
241
242 virtual bool mayFillWithSolidColor() const { return m_isSolidColor && m_currentFrame == 0; }
243 virtual Color solidColor() const { return m_solidColor; }
244
245 ImageSource m_source;
246 mutable IntSize m_size; // The size to use for the overall image (will just be the size of the first image).
247
248 size_t m_currentFrame; // The index of the current frame of animation.
249 Vector<FrameData> m_frames; // An array of the cached frames of the animation. We have to ref frames to pin them in the cache.
250
251 Timer<BitmapImage>* m_frameTimer;
252 int m_repetitionCount; // How many total animation loops we should do. This will be cAnimationNone if this image type is incapable of animation.
253 RepetitionCountStatus m_repetitionCountStatus;
254 int m_repetitionsComplete; // How many repetitions we've finished.
255 double m_desiredFrameStartTime; // The system time at which we hope to see the next call to startAnimation().
256
257 #if PLATFORM(MAC)
258 mutable RetainPtr<CFDataRef> m_tiffRep; // Cached TIFF rep for frame 0. Only built lazily if someone queries for one.
259 #endif
260
261 Color m_solidColor; // If we're a 1x1 solid color, this is the color to use to fill.
262 bool m_isSolidColor; // Whether or not we are a 1x1 solid image.
263
264 bool m_animationFinished; // Whether or not we've completed the entire animation.
265
266 bool m_allDataReceived; // Whether or not we've received all our data.
267
268 mutable bool m_haveSize; // Whether or not our |m_size| member variable has the final overall image size yet.
269 bool m_sizeAvailable; // Whether or not we can obtain the size of the first image frame yet from ImageIO.
270 mutable bool m_hasUniformFrameSize;
271
272 unsigned m_decodedSize; // The current size of all decoded frames.
273
274 mutable bool m_haveFrameCount;
275 size_t m_frameCount;
276 };
277
278 }
279
280 #endif