1 /*
2  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
4  * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef ImageBuffer_h
29 #define ImageBuffer_h
30 
31 #include "AffineTransform.h"
32 #include "ColorSpace.h"
33 #include "FloatRect.h"
34 #include "GraphicsTypes.h"
35 #include "IntSize.h"
36 #include "ImageBufferData.h"
37 #include <wtf/ByteArray.h>
38 #include <wtf/Forward.h>
39 #include <wtf/OwnPtr.h>
40 #include <wtf/PassOwnPtr.h>
41 #include <wtf/PassRefPtr.h>
42 #include <wtf/Vector.h>
43 
44 namespace WebCore {
45 
46     class GraphicsContext;
47     class Image;
48     class ImageData;
49     class IntPoint;
50     class IntRect;
51 
52     enum Multiply {
53         Premultiplied,
54         Unmultiplied
55     };
56 
57     enum RenderingMode {
58         Unaccelerated,
59         Accelerated
60     };
61 
62     class ImageBuffer {
63         WTF_MAKE_NONCOPYABLE(ImageBuffer); WTF_MAKE_FAST_ALLOCATED;
64     public:
65         // Will return a null pointer on allocation failure.
66         static PassOwnPtr<ImageBuffer> create(const IntSize& size, ColorSpace colorSpace = ColorSpaceDeviceRGB, RenderingMode renderingMode = Unaccelerated)
67         {
68             bool success = false;
69             OwnPtr<ImageBuffer> buf = adoptPtr(new ImageBuffer(size, colorSpace, renderingMode, success));
70             if (success)
71                 return buf.release();
72             return nullptr;
73         }
74 
75         ~ImageBuffer();
76 
size()77         const IntSize& size() const { return m_size; }
width()78         int width() const { return m_size.width(); }
height()79         int height() const { return m_size.height(); }
80 
81         size_t dataSize() const;
82 
83         GraphicsContext* context() const;
84 
isAccelerated()85         bool isAccelerated() const { return m_accelerateRendering; }
86         bool drawsUsingCopy() const; // If the image buffer has to render using a copied image, it will return true.
87         PassRefPtr<Image> copyImage() const; // Return a new image that is a copy of the buffer.
88 
89         PassRefPtr<ByteArray> getUnmultipliedImageData(const IntRect&) const;
90         PassRefPtr<ByteArray> getPremultipliedImageData(const IntRect&) const;
91 
92         void putUnmultipliedImageData(ByteArray*, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint);
93         void putPremultipliedImageData(ByteArray*, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint);
94 
95         String toDataURL(const String& mimeType, const double* quality = 0) const;
96 #if !USE(CG)
baseTransform()97         AffineTransform baseTransform() const { return AffineTransform(); }
98         void transformColorSpace(ColorSpace srcColorSpace, ColorSpace dstColorSpace);
99         void platformTransformColorSpace(const Vector<int>&);
100 #else
baseTransform()101         AffineTransform baseTransform() const { return AffineTransform(1, 0, 0, -1, 0, m_size.height()); }
102 #endif
103 
104     private:
105         void clip(GraphicsContext*, const FloatRect&) const;
106 
107         // The draw method draws the contents of the buffer without copying it.
108         void draw(GraphicsContext*, ColorSpace styleColorSpace, const FloatRect& destRect, const FloatRect& srcRect = FloatRect(0, 0, -1, -1),
109                              CompositeOperator = CompositeSourceOver, bool useLowQualityScale = false);
110         void drawPattern(GraphicsContext*, const FloatRect& srcRect, const AffineTransform& patternTransform,
111                          const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator, const FloatRect& destRect);
112         friend class GraphicsContext;
113         friend class GeneratedImage;
114 
115     private:
116         ImageBufferData m_data;
117 
118         IntSize m_size;
119         bool m_accelerateRendering;
120         OwnPtr<GraphicsContext> m_context;
121 
122 #if !USE(CG)
123         Vector<int> m_linearRgbLUT;
124         Vector<int> m_deviceRgbLUT;
125 #endif
126 
127         // This constructor will place its success into the given out-variable
128         // so that create() knows when it should return failure.
129         ImageBuffer(const IntSize&, ColorSpace colorSpace, RenderingMode renderingMode, bool& success);
130     };
131 
132 #if USE(CG) || USE(SKIA)
133     String ImageDataToDataURL(const ImageData& input, const String& mimeType, const double* quality);
134 #endif
135 
136 } // namespace WebCore
137 
138 #endif // ImageBuffer_h
139