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 "ColorSpace.h"
32#include "GraphicsTypes.h"
33#include "ImageOrientation.h"
34#include "IntRect.h"
35#include "NativeImagePtr.h"
36#include <wtf/PassRefPtr.h>
37#include <wtf/RefCounted.h>
38#include <wtf/RefPtr.h>
39#include <wtf/RetainPtr.h>
40#include <wtf/text/WTFString.h>
41
42#if PLATFORM(MAC)
43OBJC_CLASS NSImage;
44#endif
45
46#if USE(CG)
47struct CGContext;
48#endif
49
50#if PLATFORM(WIN)
51typedef struct tagSIZE SIZE;
52typedef SIZE* LPSIZE;
53typedef struct HBITMAP__ *HBITMAP;
54#endif
55
56#if PLATFORM(QT)
57#include <QPixmap>
58#endif
59
60#if PLATFORM(GTK)
61typedef struct _GdkPixbuf GdkPixbuf;
62#endif
63
64#if PLATFORM(EFL)
65#if USE(EO)
66typedef struct _Eo Evas;
67typedef struct _Eo Evas_Object;
68#else
69typedef struct _Evas Evas;
70typedef struct _Evas_Object Evas_Object;
71#endif
72#endif
73
74namespace WebCore {
75
76class AffineTransform;
77class FloatPoint;
78class FloatRect;
79class FloatSize;
80class GraphicsContext;
81class SharedBuffer;
82struct Length;
83
84// This class gets notified when an image creates or destroys decoded frames and when it advances animation frames.
85class ImageObserver;
86
87class Image : public RefCounted<Image> {
88    friend class GeneratedImage;
89    friend class CrossfadeGeneratedImage;
90    friend class GeneratorGeneratedImage;
91    friend class GraphicsContext;
92
93public:
94    virtual ~Image();
95
96    static PassRefPtr<Image> create(ImageObserver* = 0);
97    static PassRefPtr<Image> loadPlatformResource(const char* name);
98    static bool supportsType(const String&);
99
100    virtual bool isSVGImage() const { return false; }
101    virtual bool isBitmapImage() const { return false; }
102    virtual bool isPDFDocumentImage() const { return false; }
103    virtual bool currentFrameKnownToBeOpaque() = 0;
104
105    // Derived classes should override this if they can assure that
106    // the image contains only resources from its own security origin.
107    virtual bool hasSingleSecurityOrigin() const { return false; }
108
109    static Image* nullImage();
110    bool isNull() const { return size().isEmpty(); }
111
112    virtual void setContainerSize(const IntSize&) { }
113    virtual bool usesContainerSize() const { return false; }
114    virtual bool hasRelativeWidth() const { return false; }
115    virtual bool hasRelativeHeight() const { return false; }
116    virtual void computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio);
117
118    virtual IntSize size() const = 0;
119    IntRect rect() const { return IntRect(IntPoint(), size()); }
120    int width() const { return size().width(); }
121    int height() const { return size().height(); }
122    virtual bool getHotSpot(IntPoint&) const { return false; }
123
124    bool setData(PassRefPtr<SharedBuffer> data, bool allDataReceived);
125    virtual bool dataChanged(bool /*allDataReceived*/) { return false; }
126
127    virtual String filenameExtension() const { return String(); } // null string if unknown
128
129    virtual void destroyDecodedData(bool destroyAll = true) = 0;
130    virtual unsigned decodedSize() const = 0;
131
132    SharedBuffer* data() { return m_encodedImageData.get(); }
133
134    // Animation begins whenever someone draws the image, so startAnimation() is not normally called.
135    // It will automatically pause once all observers no longer want to render the image anywhere.
136    virtual void startAnimation(bool /*catchUpIfNecessary*/ = true) { }
137    virtual void stopAnimation() {}
138    virtual void resetAnimation() {}
139
140    // Typically the CachedImage that owns us.
141    ImageObserver* imageObserver() const { return m_imageObserver; }
142    void setImageObserver(ImageObserver* observer) { m_imageObserver = observer; }
143
144    enum TileRule { StretchTile, RoundTile, SpaceTile, RepeatTile };
145
146    virtual PassNativeImagePtr nativeImageForCurrentFrame() { return 0; }
147
148#if PLATFORM(MAC)
149    // Accessors for native image formats.
150    virtual NSImage* getNSImage() { return 0; }
151    virtual CFDataRef getTIFFRepresentation() { return 0; }
152#endif
153
154#if USE(CG)
155    virtual CGImageRef getCGImageRef() { return 0; }
156    virtual CGImageRef getFirstCGImageRefOfSize(const IntSize&) { return 0; }
157    virtual RetainPtr<CFArrayRef> getCGImageArray() { return 0; }
158    static RetainPtr<CGImageRef> imageWithColorSpace(CGImageRef originalImage, ColorSpace);
159#endif
160
161#if PLATFORM(WIN)
162    virtual bool getHBITMAP(HBITMAP) { return false; }
163    virtual bool getHBITMAPOfSize(HBITMAP, LPSIZE) { return false; }
164#endif
165
166#if PLATFORM(GTK)
167    virtual GdkPixbuf* getGdkPixbuf() { return 0; }
168    static PassRefPtr<Image> loadPlatformThemeIcon(const char* name, int size);
169#endif
170
171#if PLATFORM(QT)
172    static void setPlatformResource(const char* name, const QPixmap&);
173#endif
174
175#if PLATFORM(EFL)
176    virtual Evas_Object* getEvasObject(Evas*) { return 0; }
177#endif
178
179    virtual void drawPattern(GraphicsContext*, const FloatRect& srcRect, const AffineTransform& patternTransform,
180        const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator, const FloatRect& destRect, BlendMode = BlendModeNormal);
181
182#if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
183    FloatRect adjustSourceRectForDownSampling(const FloatRect& srcRect, const IntSize& scaledSize) const;
184#endif
185
186#if !ASSERT_DISABLED
187    virtual bool notSolidColor() { return true; }
188#endif
189
190protected:
191    Image(ImageObserver* = 0);
192
193    static void fillWithSolidColor(GraphicsContext*, const FloatRect& dstRect, const Color&, ColorSpace styleColorSpace, CompositeOperator);
194
195    // The ColorSpace parameter will only be used for untagged images.
196#if PLATFORM(WIN)
197    virtual void drawFrameMatchingSourceSize(GraphicsContext*, const FloatRect& dstRect, const IntSize& srcSize, ColorSpace styleColorSpace, CompositeOperator) { }
198#endif
199    virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, ColorSpace styleColorSpace, CompositeOperator, BlendMode) = 0;
200    virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, ColorSpace styleColorSpace, CompositeOperator, BlendMode, RespectImageOrientationEnum);
201    void drawTiled(GraphicsContext*, const FloatRect& dstRect, const FloatPoint& srcPoint, const FloatSize& tileSize, ColorSpace styleColorSpace,
202        CompositeOperator , BlendMode);
203    void drawTiled(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, const FloatSize& tileScaleFactor, TileRule hRule, TileRule vRule, ColorSpace styleColorSpace, CompositeOperator);
204
205    // Supporting tiled drawing
206    virtual bool mayFillWithSolidColor() { return false; }
207    virtual Color solidColor() const { return Color(); }
208
209private:
210    RefPtr<SharedBuffer> m_encodedImageData;
211    ImageObserver* m_imageObserver;
212};
213
214}
215
216#endif
217