pleyo.com

/src/trunk2/BAL/Interfaces/graphics/BitmapImage.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
00003  * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  *
00014  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
00015  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00016  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00017  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
00018  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00019  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00020  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00021  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00022  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00023  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00024  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00025  */
00026 
00027 #ifndef BitmapImage_h
00028 #define BitmapImage_h
00029 
00030 #include "Image.h"
00031 #include "Color.h"
00032 #include "IntSize.h"
00033 
00034 #if PLATFORM(MAC)
00035 #include <wtf/RetainPtr.h>
00036 #ifdef __OBJC__
00037 @class NSImage;
00038 #else
00039 class NSImage;
00040 #endif
00041 #endif
00042 
00043 #if PLATFORM(WIN)
00044 typedef struct HBITMAP__ *HBITMAP;
00045 #endif
00046 
00047 namespace WebCore {
00048     struct FrameData;
00049 }
00050 
00051 // This complicated-looking declaration tells the FrameData Vector that it should copy without
00052 // invoking our constructor or destructor. This allows us to have a vector even for a struct
00053 // that's not copyable.
00054 namespace WTF {
00055     template<> class VectorTraits<WebCore::FrameData> : public SimpleClassVectorTraits {};
00056 }
00057 
00058 namespace WebCore {
00059 
00060 template <typename T> class Timer;
00061 
00062 // ================================================
00063 // FrameData Class
00064 // ================================================
00065 
00066 struct FrameData : Noncopyable {
00067     FrameData()
00068         : m_frame(0)
00069         , m_duration(0)
00070         , m_hasAlpha(true) 
00071     {
00072     }
00073 
00074     ~FrameData()
00075     { 
00076         clear();
00077     }
00078 
00079     void clear();
00080 
00081     NativeImagePtr m_frame;
00082     float m_duration;
00083     bool m_hasAlpha;
00084 };
00085 
00086 // =================================================
00087 // BitmapImage Class
00088 // =================================================
00089 
00090 class BitmapImage : public Image {
00091     friend class GraphicsContext;
00092 public:
00093 #if PLATFORM(QT)
00094     BitmapImage(const QPixmap &pixmap, ImageObserver* = 0);
00095 #endif
00096     BitmapImage(ImageObserver* = 0);
00097     ~BitmapImage();
00098     
00099     virtual IntSize size() const;
00100 
00101     virtual bool dataChanged(bool allDataReceived);
00102 
00103     // It may look unusual that there is no start animation call as public API.  This is because
00104     // we start and stop animating lazily.  Animation begins whenever someone draws the image.  It will
00105     // automatically pause once all observers no longer want to render the image anywhere.
00106     virtual void stopAnimation();
00107     virtual void resetAnimation();
00108     
00109     virtual unsigned decodedSize() const { return m_decodedSize; }
00110 
00111 #if PLATFORM(MAC)
00112     // Accessors for native image formats.
00113     virtual NSImage* getNSImage();
00114     virtual CFDataRef getTIFFRepresentation();
00115 #endif
00116     
00117 #if PLATFORM(CG)
00118     virtual CGImageRef getCGImageRef();
00119 #endif
00120 
00121 #if PLATFORM(QT)
00122     virtual QPixmap* getPixmap() const;
00123 #endif
00124     
00125 #if PLATFORM(WIN)
00126     virtual bool getHBITMAP(HBITMAP);
00127     virtual bool getHBITMAPOfSize(HBITMAP, LPSIZE);
00128 #endif
00129 
00130     virtual NativeImagePtr nativeImageForCurrentFrame() { return frameAtIndex(currentFrame()); }
00131 
00132 private:
00133 #if PLATFORM(WIN)
00134     virtual void drawFrameMatchingSourceSize(GraphicsContext*, const FloatRect& dstRect, const IntSize& srcSize, CompositeOperator);
00135 #endif
00136     virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator);
00137 #if PLATFORM(QT)
00138     virtual void drawPattern(GraphicsContext*, const FloatRect& srcRect, const AffineTransform& patternTransform,
00139                              const FloatPoint& phase, CompositeOperator, const FloatRect& destRect);
00140 #endif    
00141     size_t currentFrame() const { return m_currentFrame; }
00142     size_t frameCount();
00143     NativeImagePtr frameAtIndex(size_t);
00144     float frameDurationAtIndex(size_t);
00145     bool frameHasAlphaAtIndex(size_t); 
00146 
00147     // Decodes and caches a frame. Never accessed except internally.
00148     void cacheFrame(size_t index);
00149 
00150     // Called to invalidate all our cached data.  If an image is loading incrementally, we only
00151     // invalidate the last cached frame.
00152     virtual void destroyDecodedData(bool incremental = false);
00153 
00154     // Whether or not size is available yet.    
00155     bool isSizeAvailable();
00156 
00157     // Animation.
00158     bool shouldAnimate();
00159     virtual void startAnimation();
00160     void advanceAnimation(Timer<BitmapImage>*);
00161     
00162     // Handle platform-specific data
00163     void initPlatformData();
00164     void invalidatePlatformData();
00165     
00166     // Checks to see if the image is a 1x1 solid color.  We optimize these images and just do a fill rect instead.
00167     void checkForSolidColor();
00168     
00169     virtual bool mayFillWithSolidColor() const { return m_isSolidColor && m_currentFrame == 0; }
00170     virtual Color solidColor() const { return m_solidColor; }
00171     
00172     ImageSource m_source;
00173     mutable IntSize m_size; // The size to use for the overall image (will just be the size of the first image).
00174     
00175     size_t m_currentFrame; // The index of the current frame of animation.
00176     Vector<FrameData> m_frames; // An array of the cached frames of the animation. We have to ref frames to pin them in the cache.
00177     
00178     Timer<BitmapImage>* m_frameTimer;
00179     int m_repetitionCount; // How many total animation loops we should do.
00180     int m_repetitionsComplete;  // How many repetitions we've finished.
00181 
00182 #if PLATFORM(MAC)
00183     mutable RetainPtr<NSImage> m_nsImage; // A cached NSImage of frame 0. Only built lazily if someone actually queries for one.
00184     mutable RetainPtr<CFDataRef> m_tiffRep; // Cached TIFF rep for frame 0.  Only built lazily if someone queries for one.
00185 #endif
00186 
00187     Color m_solidColor;  // If we're a 1x1 solid color, this is the color to use to fill.
00188     bool m_isSolidColor;  // Whether or not we are a 1x1 solid image.
00189 
00190     bool m_animatingImageType;  // Whether or not we're an image type that is capable of animating (GIF).
00191     bool m_animationFinished;  // Whether or not we've completed the entire animation.
00192 
00193     bool m_allDataReceived;  // Whether or not we've received all our data.
00194 
00195     mutable bool m_haveSize; // Whether or not our |m_size| member variable has the final overall image size yet.
00196     bool m_sizeAvailable; // Whether or not we can obtain the size of the first image frame yet from ImageIO.
00197     unsigned m_decodedSize; // The current size of all decoded frames.
00198 
00199 #if PLATFORM(QT)
00200     QPixmap *m_pixmap;
00201 #endif
00202 
00203 };
00204 
00205 }
00206 
00207 #endif

Generated on Wed Nov 21 20:04:17 2007 for Origyn Web Browser by Doxygen 1.5.3

pleyo.com
pleyo.com