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