2 * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2008 Apple, Inc
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.
28 #include "ImageBuffer.h"
30 #include "GraphicsContext.h"
31 #include "ImageData.h"
33 #include <ApplicationServices/ApplicationServices.h>
34 #include <wtf/Assertions.h>
40 auto_ptr<ImageBuffer> ImageBuffer::create(const IntSize& size, bool grayScale)
42 if (size.width() < 0 || size.height() < 0)
43 return auto_ptr<ImageBuffer>();
44 unsigned int bytesPerRow = size.width();
46 // Protect against overflow
47 if (bytesPerRow > 0x3FFFFFFF)
48 return auto_ptr<ImageBuffer>();
52 void* imageBuffer = fastCalloc(size.height(), bytesPerRow);
54 return auto_ptr<ImageBuffer>();
56 CGColorSpaceRef colorSpace = grayScale ? CGColorSpaceCreateDeviceGray() : CGColorSpaceCreateDeviceRGB();
57 CGContextRef cgContext = CGBitmapContextCreate(imageBuffer, size.width(), size.height(), 8, bytesPerRow,
58 colorSpace, grayScale ? kCGImageAlphaNone : kCGImageAlphaPremultipliedLast);
59 CGColorSpaceRelease(colorSpace);
61 fastFree(imageBuffer);
62 return auto_ptr<ImageBuffer>();
65 auto_ptr<GraphicsContext> context(new GraphicsContext(cgContext));
66 CGContextRelease(cgContext);
68 return auto_ptr<ImageBuffer>(new ImageBuffer(imageBuffer, size, context));
72 ImageBuffer::ImageBuffer(void* imageData, const IntSize& size, auto_ptr<GraphicsContext> context)
75 , m_context(context.release())
78 ASSERT((reinterpret_cast<size_t>(imageData) & 2) == 0);
81 ImageBuffer::~ImageBuffer()
84 CGImageRelease(m_cgImage);
87 GraphicsContext* ImageBuffer::context() const
89 return m_context.get();
92 CGImageRef ImageBuffer::cgImage() const
94 // It's assumed that if cgImage() is called, the actual rendering to the
95 // contained GraphicsContext must be done, as we create the CGImageRef here.
98 m_cgImage = CGBitmapContextCreateImage(context()->platformContext());
104 PassRefPtr<ImageData> ImageBuffer::getImageData(const IntRect& rect) const
109 PassRefPtr<ImageData> result = ImageData::create(rect.width(), rect.height());
110 unsigned char* data = result->data()->data().data();
112 if (rect.x() < 0 || rect.y() < 0 || (rect.x() + rect.width()) > m_size.width() || (rect.y() + rect.height()) > m_size.height())
113 memset(data, 0, result->data()->length());
115 int originx = rect.x();
121 int endx = rect.x() + rect.width();
122 if (endx > m_size.width())
123 endx = m_size.width();
124 int numColumns = endx - originx;
126 int originy = rect.y();
132 int endy = rect.y() + rect.height();
133 if (endy > m_size.height())
134 endy = m_size.height();
135 int numRows = endy - originy;
137 unsigned srcBytesPerRow = 4 * m_size.width();
138 unsigned destBytesPerRow = 4 * rect.width();
140 // m_size.height() - originy to handle the accursed flipped y axis in CG backing store
141 unsigned char* srcRows = reinterpret_cast<unsigned char*>(m_data) + (m_size.height() - originy - 1) * srcBytesPerRow + originx * 4;
142 unsigned char* destRows = data + desty * destBytesPerRow + destx * 4;
143 for (int y = 0; y < numRows; ++y) {
144 for (int x = 0; x < numColumns; x++) {
145 if (unsigned char alpha = srcRows[3]) {
146 destRows[0] = (srcRows[0] * 255) / alpha;
147 destRows[1] = (srcRows[1] * 255) / alpha;
148 destRows[2] = (srcRows[2] * 255) / alpha;
151 reinterpret_cast<uint32_t*>(destRows)[0] = reinterpret_cast<uint32_t*>(srcRows)[0];
155 srcRows -= srcBytesPerRow;
160 void ImageBuffer::putImageData(ImageData* source, const IntRect& sourceRect, const IntPoint& destPoint)
162 ASSERT(sourceRect.width() > 0);
163 ASSERT(sourceRect.height() > 0);
165 int originx = sourceRect.x();
166 int destx = destPoint.x() + sourceRect.x();
168 ASSERT(destx < m_size.width());
169 ASSERT(originx >= 0);
170 ASSERT(originx <= sourceRect.right());
172 int endx = destPoint.x() + sourceRect.right();
173 ASSERT(endx <= m_size.width());
175 int numColumns = endx - destx;
177 int originy = sourceRect.y();
178 int desty = destPoint.y() + sourceRect.y();
180 ASSERT(desty < m_size.height());
181 ASSERT(originy >= 0);
182 ASSERT(originy <= sourceRect.bottom());
184 int endy = destPoint.y() + sourceRect.bottom();
185 ASSERT(endx <= m_size.height());
186 int numRows = endy - desty;
188 unsigned srcBytesPerRow = 4 * source->width();
189 unsigned destBytesPerRow = 4 * m_size.width();
191 unsigned char* srcRows = source->data()->data().data() + originy * srcBytesPerRow + originx * 4;
193 // -desty to handle the accursed flipped y axis
194 unsigned char* destRows = reinterpret_cast<unsigned char*>(m_data) + (m_size.height() - desty - 1) * destBytesPerRow + destx * 4;
195 for (int y = 0; y < numRows; ++y) {
196 for (int x = 0; x < numColumns; x++) {
197 unsigned char alpha = srcRows[x * 4 + 3];
199 destRows[x * 4 + 0] = (srcRows[0] * alpha) / 255;
200 destRows[x * 4 + 1] = (srcRows[1] * alpha) / 255;
201 destRows[x * 4 + 2] = (srcRows[2] * alpha) / 255;
202 destRows[x * 4 + 3] = alpha;
204 reinterpret_cast<uint32_t*>(destRows + x * 4)[0] = reinterpret_cast<uint32_t*>(srcRows + x * 4)[0];
207 destRows -= destBytesPerRow;
208 srcRows += srcBytesPerRow;