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