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