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 Image_h 28 #define Image_h 29 30 #include "Color.h" 31 #include "GraphicsTypes.h" 32 #include "ImageSource.h" 33 #include "IntRect.h" 34 #include "SharedBuffer.h" 35 #include <wtf/PassRefPtr.h> 36 #include <wtf/RefPtr.h> 37 38 #if PLATFORM(MAC) 39 #ifdef __OBJC__ 40 @class NSImage; 41 #else 42 class NSImage; 43 #endif 44 #endif 45 46 #if PLATFORM(CG) 47 struct CGContext; 48 #endif 49 50 #if PLATFORM(WIN) 51 typedef struct tagSIZE SIZE; 52 typedef SIZE* LPSIZE; 53 typedef struct HBITMAP__ *HBITMAP; 54 #endif 55 56 #if PLATFORM(SKIA) 57 class NativeImageSkia; 58 #endif 59 60 #if PLATFORM(QT) 61 #include <QPixmap> 62 #endif 63 64 #if PLATFORM(GTK) 65 typedef struct _GdkPixbuf GdkPixbuf; 66 #endif 67 68 namespace WebCore { 69 70 class FloatPoint; 71 class FloatRect; 72 class FloatSize; 73 class GraphicsContext; 74 class SharedBuffer; 75 class String; 76 class TransformationMatrix; 77 78 // This class gets notified when an image creates or destroys decoded frames and when it advances animation frames. 79 class ImageObserver; 80 81 class Image : public RefCounted<Image> { 82 friend class GeneratedImage; 83 friend class GraphicsContext; 84 85 public: 86 virtual ~Image(); 87 88 static PassRefPtr<Image> create(ImageObserver* = 0); 89 static PassRefPtr<Image> loadPlatformResource(const char* name); 90 static bool supportsType(const String&); 91 isBitmapImage()92 virtual bool isBitmapImage() const { return false; } 93 94 // Derived classes should override this if they can assure that 95 // the image contains only resources from its own security origin. hasSingleSecurityOrigin()96 virtual bool hasSingleSecurityOrigin() const { return false; } 97 98 static Image* nullImage(); isNull()99 bool isNull() const { return size().isEmpty(); } 100 101 // These are only used for SVGImage right now setContainerSize(const IntSize &)102 virtual void setContainerSize(const IntSize&) { } usesContainerSize()103 virtual bool usesContainerSize() const { return false; } hasRelativeWidth()104 virtual bool hasRelativeWidth() const { return false; } hasRelativeHeight()105 virtual bool hasRelativeHeight() const { return false; } 106 107 virtual IntSize size() const = 0; rect()108 IntRect rect() const { return IntRect(IntPoint(), size()); } width()109 int width() const { return size().width(); } height()110 int height() const { return size().height(); } 111 112 bool setData(PassRefPtr<SharedBuffer> data, bool allDataReceived); dataChanged(bool)113 virtual bool dataChanged(bool /*allDataReceived*/) { return false; } 114 filenameExtension()115 virtual String filenameExtension() const { return String(); } // null string if unknown 116 117 virtual void destroyDecodedData(bool destroyAll = true) = 0; 118 virtual unsigned decodedSize() const = 0; 119 data()120 SharedBuffer* data() { return m_data.get(); } 121 122 // Animation begins whenever someone draws the image, so startAnimation() is not normally called. 123 // It will automatically pause once all observers no longer want to render the image anywhere. 124 virtual void startAnimation(bool /*catchUpIfNecessary*/ = true) { } stopAnimation()125 virtual void stopAnimation() {} resetAnimation()126 virtual void resetAnimation() {} 127 128 // Typically the CachedImage that owns us. imageObserver()129 ImageObserver* imageObserver() const { return m_imageObserver; } 130 131 enum TileRule { StretchTile, RoundTile, RepeatTile }; 132 nativeImageForCurrentFrame()133 virtual NativeImagePtr nativeImageForCurrentFrame() { return 0; } 134 135 #if PLATFORM(MAC) 136 // Accessors for native image formats. getNSImage()137 virtual NSImage* getNSImage() { return 0; } getTIFFRepresentation()138 virtual CFDataRef getTIFFRepresentation() { return 0; } 139 #endif 140 141 #if PLATFORM(CG) getCGImageRef()142 virtual CGImageRef getCGImageRef() { return 0; } 143 #endif 144 145 #if PLATFORM(WIN) getHBITMAP(HBITMAP)146 virtual bool getHBITMAP(HBITMAP) { return false; } getHBITMAPOfSize(HBITMAP,LPSIZE)147 virtual bool getHBITMAPOfSize(HBITMAP, LPSIZE) { return false; } 148 #endif 149 150 #if PLATFORM(SGL) setURL(const String & str)151 virtual void setURL(const String& str) {} 152 #endif 153 154 #if PLATFORM(GTK) getGdkPixbuf()155 virtual GdkPixbuf* getGdkPixbuf() { return 0; } 156 #endif 157 158 protected: 159 Image(ImageObserver* = 0); 160 161 static void fillWithSolidColor(GraphicsContext* ctxt, const FloatRect& dstRect, const Color& color, CompositeOperator op); 162 163 #if PLATFORM(WIN) drawFrameMatchingSourceSize(GraphicsContext *,const FloatRect & dstRect,const IntSize & srcSize,CompositeOperator)164 virtual void drawFrameMatchingSourceSize(GraphicsContext*, const FloatRect& dstRect, const IntSize& srcSize, CompositeOperator) { } 165 #endif 166 virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, CompositeOperator) = 0; 167 void drawTiled(GraphicsContext*, const FloatRect& dstRect, const FloatPoint& srcPoint, const FloatSize& tileSize, CompositeOperator); 168 void drawTiled(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, TileRule hRule, TileRule vRule, CompositeOperator); 169 170 // Supporting tiled drawing mayFillWithSolidColor()171 virtual bool mayFillWithSolidColor() { return false; } solidColor()172 virtual Color solidColor() const { return Color(); } 173 174 virtual void drawPattern(GraphicsContext*, const FloatRect& srcRect, const TransformationMatrix& patternTransform, 175 const FloatPoint& phase, CompositeOperator, const FloatRect& destRect); 176 177 private: 178 RefPtr<SharedBuffer> m_data; // The encoded raw data for the image. 179 ImageObserver* m_imageObserver; 180 }; 181 182 } 183 184 #endif 185