2 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
3 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
31 #include "ColorSpace.h"
32 #include "GraphicsTypes.h"
33 #include "ImageSource.h"
35 #include <wtf/PassRefPtr.h>
36 #include <wtf/RefCounted.h>
37 #include <wtf/RefPtr.h>
38 #include <wtf/RetainPtr.h>
39 #include <wtf/text/WTFString.h>
50 typedef struct tagSIZE SIZE;
52 typedef struct HBITMAP__ *HBITMAP;
56 typedef struct _GdkPixbuf GdkPixbuf;
61 class AffineTransform;
65 class GraphicsContext;
69 // This class gets notified when an image creates or destroys decoded frames and when it advances animation frames.
72 class Image : public RefCounted<Image> {
73 friend class GeneratedImage;
74 friend class CrossfadeGeneratedImage;
75 friend class GeneratorGeneratedImage;
76 friend class GraphicsContext;
81 static PassRefPtr<Image> create(ImageObserver* = 0);
82 static PassRefPtr<Image> loadPlatformResource(const char* name);
83 static bool supportsType(const String&);
85 virtual bool isSVGImage() const { return false; }
86 virtual bool isBitmapImage() const { return false; }
87 virtual bool currentFrameHasAlpha() { return false; }
89 // Derived classes should override this if they can assure that
90 // the image contains only resources from its own security origin.
91 virtual bool hasSingleSecurityOrigin() const { return false; }
93 static Image* nullImage();
94 bool isNull() const { return size().isEmpty(); }
96 virtual void setContainerSize(const IntSize&) { }
97 virtual bool usesContainerSize() const { return false; }
98 virtual bool hasRelativeWidth() const { return false; }
99 virtual bool hasRelativeHeight() const { return false; }
100 virtual void computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio);
102 virtual IntSize size() const = 0;
103 IntRect rect() const { return IntRect(IntPoint(), size()); }
104 int width() const { return size().width(); }
105 int height() const { return size().height(); }
106 virtual bool getHotSpot(IntPoint&) const { return false; }
108 bool setData(PassRefPtr<SharedBuffer> data, bool allDataReceived);
109 virtual bool dataChanged(bool /*allDataReceived*/) { return false; }
111 virtual String filenameExtension() const { return String(); } // null string if unknown
113 virtual void destroyDecodedData(bool destroyAll = true) = 0;
114 virtual unsigned decodedSize() const = 0;
116 SharedBuffer* data() { return m_data.get(); }
118 // Animation begins whenever someone draws the image, so startAnimation() is not normally called.
119 // It will automatically pause once all observers no longer want to render the image anywhere.
120 virtual void startAnimation(bool /*catchUpIfNecessary*/ = true) { }
121 virtual void stopAnimation() {}
122 virtual void resetAnimation() {}
124 // Typically the CachedImage that owns us.
125 ImageObserver* imageObserver() const { return m_imageObserver; }
126 void setImageObserver(ImageObserver* observer) { m_imageObserver = observer; }
128 enum TileRule { StretchTile, RoundTile, SpaceTile, RepeatTile };
130 virtual NativeImagePtr nativeImageForCurrentFrame() { return 0; }
133 // Accessors for native image formats.
134 virtual NSImage* getNSImage() { return 0; }
135 virtual CFDataRef getTIFFRepresentation() { return 0; }
139 virtual CGImageRef getCGImageRef() { return 0; }
140 virtual CGImageRef getFirstCGImageRefOfSize(const IntSize&) { return 0; }
141 virtual RetainPtr<CFArrayRef> getCGImageArray() { return 0; }
142 static RetainPtr<CGImageRef> imageWithColorSpace(CGImageRef originalImage, ColorSpace);
146 virtual bool getHBITMAP(HBITMAP) { return false; }
147 virtual bool getHBITMAPOfSize(HBITMAP, LPSIZE) { return false; }
151 virtual GdkPixbuf* getGdkPixbuf() { return 0; }
152 static PassRefPtr<Image> loadPlatformThemeIcon(const char* name, int size);
156 static void setPlatformResource(const char* name, const QImage&);
160 #if ENABLE(TIZEN_CLIPBOARD) || ENABLE(TIZEN_PASTEBOARD)
161 virtual NativeImagePtr getCairoSurface() { return 0; }
165 virtual void drawPattern(GraphicsContext*, const FloatRect& srcRect, const AffineTransform& patternTransform,
166 const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator, const FloatRect& destRect);
168 #if ENABLE(IMAGE_DECODER_DOWN_SAMPLING)
169 FloatRect adjustSourceRectForDownSampling(const FloatRect& srcRect, const IntSize& scaledSize) const;
173 virtual bool notSolidColor() { return true; }
176 #if ENABLE(TIZEN_JPEGIMAGE_DECODING_THREAD)
177 virtual void cacheFrame(size_t index) { }
178 virtual ImageSource* getBitmapimageSource() { return 0; }
182 Image(ImageObserver* = 0);
184 static void fillWithSolidColor(GraphicsContext*, const FloatRect& dstRect, const Color&, ColorSpace styleColorSpace, CompositeOperator);
186 // The ColorSpace parameter will only be used for untagged images.
188 virtual void drawFrameMatchingSourceSize(GraphicsContext*, const FloatRect& dstRect, const IntSize& srcSize, ColorSpace styleColorSpace, CompositeOperator) { }
190 virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, ColorSpace styleColorSpace, CompositeOperator) = 0;
191 virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, ColorSpace styleColorSpace, CompositeOperator, RespectImageOrientationEnum);
192 void drawTiled(GraphicsContext*, const FloatRect& dstRect, const FloatPoint& srcPoint, const FloatSize& tileSize, ColorSpace styleColorSpace, CompositeOperator);
193 void drawTiled(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, const FloatSize& tileScaleFactor, TileRule hRule, TileRule vRule, ColorSpace styleColorSpace, CompositeOperator);
195 // Supporting tiled drawing
196 virtual bool mayFillWithSolidColor() { return false; }
197 virtual Color solidColor() const { return Color(); }
200 RefPtr<SharedBuffer> m_data; // The encoded raw data for the image.
201 ImageObserver* m_imageObserver;