Clean up image subsampling code, make it less iOS-specific
[WebKit-https.git] / Source / WebCore / platform / graphics / cg / ImageBufferCG.cpp
1 /*
2  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
3  * Copyright (C) 2008 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 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 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 #include "config.h"
29 #include "ImageBuffer.h"
30
31 #include "BitmapImage.h"
32 #include "GraphicsContext.h"
33 #include "GraphicsContextCG.h"
34 #include "ImageData.h"
35 #include "IntRect.h"
36 #include "MIMETypeRegistry.h"
37 #include <math.h>
38 #include <CoreGraphics/CoreGraphics.h>
39 #include <ImageIO/ImageIO.h>
40 #include <wtf/Assertions.h>
41 #include <wtf/CheckedArithmetic.h>
42 #include <wtf/MainThread.h>
43 #include <wtf/RetainPtr.h>
44 #include <wtf/text/Base64.h>
45 #include <wtf/text/WTFString.h>
46 #if PLATFORM(COCOA)
47 #include "WebCoreSystemInterface.h"
48 #endif
49
50 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
51 #include <IOSurface/IOSurface.h>
52 #endif
53
54 // CA uses ARGB32 for textures and ARGB32 -> ARGB32 resampling is optimized.
55 #define USE_ARGB32 PLATFORM(IOS)
56
57 namespace WebCore {
58
59 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
60 #if PLATFORM(IOS)
61 static const int maxIOSurfaceDimension = 2048;
62 #else
63 static const int maxIOSurfaceDimension = 4096;
64 #endif
65
66 // FIXME: Adopt WebCore::IOSurface.
67 static RetainPtr<IOSurfaceRef> createIOSurface(const IntSize& size)
68 {
69     unsigned pixelFormat = 'BGRA';
70     unsigned bytesPerElement = 4;
71     int width = size.width();
72     int height = size.height();
73
74     unsigned long bytesPerRow = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, size.width() * bytesPerElement);
75     if (!bytesPerRow)
76         return 0;
77
78     unsigned long allocSize = IOSurfaceAlignProperty(kIOSurfaceAllocSize, size.height() * bytesPerRow);
79     if (!allocSize)
80         return 0;
81
82 #if !PLATFORM(IOS)
83     const void* keys[6];
84     const void* values[6];
85 #else
86     const void* keys[7];
87     const void* values[7];
88 #endif
89     keys[0] = kIOSurfaceWidth;
90     values[0] = CFNumberCreate(0, kCFNumberIntType, &width);
91     keys[1] = kIOSurfaceHeight;
92     values[1] = CFNumberCreate(0, kCFNumberIntType, &height);
93     keys[2] = kIOSurfacePixelFormat;
94     values[2] = CFNumberCreate(0, kCFNumberIntType, &pixelFormat);
95     keys[3] = kIOSurfaceBytesPerElement;
96     values[3] = CFNumberCreate(0, kCFNumberIntType, &bytesPerElement);
97     keys[4] = kIOSurfaceBytesPerRow;
98     values[4] = CFNumberCreate(0, kCFNumberLongType, &bytesPerRow);
99     keys[5] = kIOSurfaceAllocSize;
100     values[5] = CFNumberCreate(0, kCFNumberLongType, &allocSize);
101 #if PLATFORM(IOS)
102     keys[6] = kIOSurfaceCacheMode;
103     int cacheMode = kIOMapWriteCombineCache;
104     values[6] = CFNumberCreate(0, kCFNumberIntType, &cacheMode);
105 #endif
106
107     RetainPtr<CFDictionaryRef> dict = adoptCF(CFDictionaryCreate(0, keys, values, WTF_ARRAY_LENGTH(values), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
108     for (unsigned i = 0; i < WTF_ARRAY_LENGTH(values); ++i)
109         CFRelease(values[i]);
110
111     return adoptCF(IOSurfaceCreate(dict.get()));
112 }
113 #endif
114
115 static void releaseImageData(void*, const void* data, size_t)
116 {
117     fastFree(const_cast<void*>(data));
118 }
119
120 static FloatSize scaleSizeToUserSpace(const FloatSize& logicalSize, const IntSize& backingStoreSize, const IntSize& internalSize)
121 {
122     float xMagnification = static_cast<float>(backingStoreSize.width()) / internalSize.width();
123     float yMagnification = static_cast<float>(backingStoreSize.height()) / internalSize.height();
124     return FloatSize(logicalSize.width() * xMagnification, logicalSize.height() * yMagnification);
125 }
126
127 ImageBuffer::ImageBuffer(const FloatSize& size, float resolutionScale, ColorSpace imageColorSpace, RenderingMode renderingMode, bool& success)
128     : m_data(IntSize(size)) // NOTE: The input here isn't important as ImageBufferDataCG's constructor just ignores it.
129     , m_logicalSize(size)
130     , m_resolutionScale(resolutionScale)
131 {
132     float scaledWidth = ceilf(resolutionScale * size.width());
133     float scaledHeight = ceilf(resolutionScale * size.height());
134
135     // FIXME: Should we automatically use a lower resolution?
136     if (!FloatSize(scaledWidth, scaledHeight).isExpressibleAsIntSize())
137         return;
138
139     m_size = IntSize(scaledWidth, scaledHeight);
140     m_data.m_backingStoreSize = m_size;
141
142     success = false;  // Make early return mean failure.
143     bool accelerateRendering = renderingMode == Accelerated;
144     if (m_size.width() <= 0 || m_size.height() <= 0)
145         return;
146
147 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
148     Checked<int, RecordOverflow> width = m_size.width();
149     Checked<int, RecordOverflow> height = m_size.height();
150 #endif
151
152     // Prevent integer overflows
153     m_data.m_bytesPerRow = 4 * Checked<unsigned, RecordOverflow>(m_data.m_backingStoreSize.width());
154     Checked<size_t, RecordOverflow> numBytes = Checked<unsigned, RecordOverflow>(m_data.m_backingStoreSize.height()) * m_data.m_bytesPerRow;
155     if (numBytes.hasOverflowed())
156         return;
157
158 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
159     if (width.unsafeGet() > maxIOSurfaceDimension || height.unsafeGet() > maxIOSurfaceDimension)
160         accelerateRendering = false;
161 #else
162     ASSERT(renderingMode == Unaccelerated);
163 #endif
164
165     switch (imageColorSpace) {
166     case ColorSpaceDeviceRGB:
167         m_data.m_colorSpace = deviceRGBColorSpaceRef();
168         break;
169     case ColorSpaceSRGB:
170         m_data.m_colorSpace = sRGBColorSpaceRef();
171         break;
172     case ColorSpaceLinearRGB:
173         m_data.m_colorSpace = linearRGBColorSpaceRef();
174         break;
175     }
176
177     RetainPtr<CGContextRef> cgContext;
178     if (accelerateRendering) {
179 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
180         m_data.m_surface = createIOSurface(m_data.m_backingStoreSize);
181         FloatSize userBounds = scaleSizeToUserSpace(FloatSize(width.unsafeGet(), height.unsafeGet()), m_data.m_backingStoreSize, m_size);
182         cgContext = adoptCF(wkIOSurfaceContextCreate(m_data.m_surface.get(), userBounds.width(), userBounds.height(), m_data.m_colorSpace));
183 #endif
184         if (!cgContext)
185             accelerateRendering = false; // If allocation fails, fall back to non-accelerated path.
186     }
187
188     if (!accelerateRendering) {
189         if (!tryFastCalloc(m_data.m_backingStoreSize.height(), m_data.m_bytesPerRow.unsafeGet()).getValue(m_data.m_data))
190             return;
191         ASSERT(!(reinterpret_cast<intptr_t>(m_data.m_data) & 3));
192
193 #if USE_ARGB32
194         m_data.m_bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;
195 #else
196         m_data.m_bitmapInfo = kCGImageAlphaPremultipliedLast;
197 #endif
198         cgContext = adoptCF(CGBitmapContextCreate(m_data.m_data, m_data.m_backingStoreSize.width(), m_data.m_backingStoreSize.height(), 8, m_data.m_bytesPerRow.unsafeGet(), m_data.m_colorSpace, m_data.m_bitmapInfo));
199         // Create a live image that wraps the data.
200         m_data.m_dataProvider = adoptCF(CGDataProviderCreateWithData(0, m_data.m_data, numBytes.unsafeGet(), releaseImageData));
201     }
202
203     if (!cgContext)
204         return;
205
206     m_context = adoptPtr(new GraphicsContext(cgContext.get()));
207     m_context->scale(FloatSize(1, -1));
208     m_context->translate(0, -m_data.m_backingStoreSize.height());
209     m_context->applyDeviceScaleFactor(m_resolutionScale);
210     m_context->setIsAcceleratedContext(accelerateRendering);
211     success = true;
212 }
213
214 ImageBuffer::~ImageBuffer()
215 {
216 }
217
218 GraphicsContext* ImageBuffer::context() const
219 {
220     return m_context.get();
221 }
222
223 void ImageBuffer::flushContext() const
224 {
225     CGContextFlush(m_context->platformContext());
226 }
227
228 static RetainPtr<CGImageRef> createCroppedImageIfNecessary(CGImageRef image, const IntSize& bounds)
229 {
230     if (image && (CGImageGetWidth(image) != static_cast<size_t>(bounds.width())
231         || CGImageGetHeight(image) != static_cast<size_t>(bounds.height()))) {
232         return adoptCF(CGImageCreateWithImageInRect(image, CGRectMake(0, 0, bounds.width(), bounds.height())));
233     }
234     return image;
235 }
236
237 PassRefPtr<Image> ImageBuffer::copyImage(BackingStoreCopy copyBehavior, ScaleBehavior scaleBehavior) const
238 {
239     RetainPtr<CGImageRef> image;
240     if (m_resolutionScale == 1 || scaleBehavior == Unscaled) {
241         image = copyNativeImage(copyBehavior);
242         image = createCroppedImageIfNecessary(image.get(), internalSize());
243     } else {
244         image = copyNativeImage(DontCopyBackingStore);
245         RetainPtr<CGContextRef> context = adoptCF(CGBitmapContextCreate(0, logicalSize().width(), logicalSize().height(), 8, 4 * logicalSize().width(), deviceRGBColorSpaceRef(), kCGImageAlphaPremultipliedLast));
246         CGContextSetBlendMode(context.get(), kCGBlendModeCopy);
247         CGContextClipToRect(context.get(), FloatRect(FloatPoint::zero(), logicalSize()));
248         FloatSize imageSizeInUserSpace = scaleSizeToUserSpace(logicalSize(), m_data.m_backingStoreSize, internalSize());
249         CGContextDrawImage(context.get(), FloatRect(FloatPoint::zero(), imageSizeInUserSpace), image.get());
250         image = adoptCF(CGBitmapContextCreateImage(context.get()));
251     }
252
253     if (!image)
254         return nullptr;
255
256     RefPtr<BitmapImage> bitmapImage = BitmapImage::create(image.get());
257     bitmapImage->setSpaceSize(spaceSize());
258
259     return bitmapImage.release();
260 }
261
262 BackingStoreCopy ImageBuffer::fastCopyImageMode()
263 {
264     return DontCopyBackingStore;
265 }
266
267 RetainPtr<CGImageRef> ImageBuffer::copyNativeImage(BackingStoreCopy copyBehavior) const
268 {
269     CGImageRef image = 0;
270     if (!m_context->isAcceleratedContext()) {
271         switch (copyBehavior) {
272         case DontCopyBackingStore:
273             image = CGImageCreate(m_data.m_backingStoreSize.width(), m_data.m_backingStoreSize.height(), 8, 32, m_data.m_bytesPerRow.unsafeGet(), m_data.m_colorSpace, m_data.m_bitmapInfo, m_data.m_dataProvider.get(), 0, true, kCGRenderingIntentDefault);
274             break;
275         case CopyBackingStore:
276             image = CGBitmapContextCreateImage(context()->platformContext());
277             break;
278         default:
279             ASSERT_NOT_REACHED();
280             break;
281         }
282     }
283 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
284     else
285         image = wkIOSurfaceContextCreateImage(context()->platformContext());
286 #endif
287
288     return adoptCF(image);
289 }
290
291 void ImageBuffer::draw(GraphicsContext* destContext, ColorSpace styleColorSpace, const FloatRect& destRect, const FloatRect& srcRect, CompositeOperator op, BlendMode blendMode, bool useLowQualityScale)
292 {
293     UNUSED_PARAM(useLowQualityScale);
294     ColorSpace colorSpace = (destContext == m_context) ? ColorSpaceDeviceRGB : styleColorSpace;
295
296     RetainPtr<CGImageRef> image;
297     if (destContext == m_context || destContext->isAcceleratedContext())
298         image = copyNativeImage(CopyBackingStore); // Drawing into our own buffer, need to deep copy.
299     else
300         image = copyNativeImage(DontCopyBackingStore);
301
302     FloatRect adjustedSrcRect = srcRect;
303     adjustedSrcRect.scale(m_resolutionScale, m_resolutionScale);
304     destContext->drawNativeImage(image.get(), m_data.m_backingStoreSize, colorSpace, destRect, adjustedSrcRect, op, blendMode);
305 }
306
307 void ImageBuffer::drawPattern(GraphicsContext* destContext, const FloatRect& srcRect, const AffineTransform& patternTransform, const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect, BlendMode blendMode)
308 {
309     FloatRect adjustedSrcRect = srcRect;
310     adjustedSrcRect.scale(m_resolutionScale, m_resolutionScale);
311
312     if (!m_context->isAcceleratedContext()) {
313         if (destContext == m_context || destContext->isAcceleratedContext()) {
314             RefPtr<Image> copy = copyImage(CopyBackingStore); // Drawing into our own buffer, need to deep copy.
315             copy->drawPattern(destContext, adjustedSrcRect, patternTransform, phase, styleColorSpace, op, destRect, blendMode);
316         } else {
317             RefPtr<Image> imageForRendering = copyImage(DontCopyBackingStore);
318             imageForRendering->drawPattern(destContext, adjustedSrcRect, patternTransform, phase, styleColorSpace, op, destRect, blendMode);
319         }
320     } else {
321         RefPtr<Image> copy = copyImage(CopyBackingStore);
322         copy->drawPattern(destContext, adjustedSrcRect, patternTransform, phase, styleColorSpace, op, destRect, blendMode);
323     }
324 }
325
326 void ImageBuffer::clip(GraphicsContext* contextToClip, const FloatRect& rect) const
327 {
328     FloatSize backingStoreSizeInUserSpace = scaleSizeToUserSpace(rect.size(), m_data.m_backingStoreSize, internalSize());
329
330     CGContextRef platformContextToClip = contextToClip->platformContext();
331     // FIXME: This image needs to be grayscale to be used as an alpha mask here.
332     RetainPtr<CGImageRef> image = copyNativeImage(DontCopyBackingStore);
333     CGContextTranslateCTM(platformContextToClip, rect.x(), rect.y() + backingStoreSizeInUserSpace.height());
334     CGContextScaleCTM(platformContextToClip, 1, -1);
335     CGContextClipToRect(platformContextToClip, FloatRect(FloatPoint(0, backingStoreSizeInUserSpace.height() - rect.height()), rect.size()));
336     CGContextClipToMask(platformContextToClip, FloatRect(FloatPoint(), backingStoreSizeInUserSpace), image.get());
337     CGContextScaleCTM(platformContextToClip, 1, -1);
338     CGContextTranslateCTM(platformContextToClip, -rect.x(), -rect.y() - rect.height());
339 }
340
341 PassRefPtr<Uint8ClampedArray> ImageBuffer::getUnmultipliedImageData(const IntRect& rect, CoordinateSystem coordinateSystem) const
342 {
343     if (m_context->isAcceleratedContext())
344         flushContext();
345
346     IntRect srcRect = rect;
347     if (coordinateSystem == LogicalCoordinateSystem)
348         srcRect.scale(m_resolutionScale);
349
350     return m_data.getData(srcRect, internalSize(), m_context->isAcceleratedContext(), true, 1);
351 }
352
353 PassRefPtr<Uint8ClampedArray> ImageBuffer::getPremultipliedImageData(const IntRect& rect, CoordinateSystem coordinateSystem) const
354 {
355     if (m_context->isAcceleratedContext())
356         flushContext();
357
358     IntRect srcRect = rect;
359     if (coordinateSystem == LogicalCoordinateSystem)
360         srcRect.scale(m_resolutionScale);
361
362     return m_data.getData(srcRect, internalSize(), m_context->isAcceleratedContext(), false, 1);
363 }
364
365 void ImageBuffer::putByteArray(Multiply multiplied, Uint8ClampedArray* source, const IntSize& sourceSize, const IntRect& sourceRect, const IntPoint& destPoint, CoordinateSystem coordinateSystem)
366 {
367     if (!m_context->isAcceleratedContext()) {
368         IntRect scaledSourceRect = sourceRect;
369         IntSize scaledSourceSize = sourceSize;
370         if (coordinateSystem == LogicalCoordinateSystem) {
371             scaledSourceRect.scale(m_resolutionScale);
372             scaledSourceSize.scale(m_resolutionScale);
373         }
374
375         m_data.putData(source, scaledSourceSize, scaledSourceRect, destPoint, internalSize(), false, multiplied == Unmultiplied, 1);
376         return;
377     }
378
379 #if USE(IOSURFACE_CANVAS_BACKING_STORE)
380     // Make a copy of the source to ensure the bits don't change before being drawn
381     IntSize sourceCopySize(sourceRect.width(), sourceRect.height());
382     std::unique_ptr<ImageBuffer> sourceCopy = ImageBuffer::create(sourceCopySize, 1, ColorSpaceDeviceRGB, Unaccelerated);
383     if (!sourceCopy)
384         return;
385
386     sourceCopy->m_data.putData(source, sourceSize, sourceRect, IntPoint(-sourceRect.x(), -sourceRect.y()), sourceCopy->internalSize(), sourceCopy->context()->isAcceleratedContext(), multiplied == Unmultiplied, 1);
387
388     // Set up context for using drawImage as a direct bit copy
389     CGContextRef destContext = context()->platformContext();
390     CGContextSaveGState(destContext);
391     if (coordinateSystem == LogicalCoordinateSystem)
392         CGContextConcatCTM(destContext, AffineTransform(wkGetUserToBaseCTM(destContext)).inverse());
393     else
394         CGContextConcatCTM(destContext, AffineTransform(CGContextGetCTM(destContext)).inverse());
395     wkCGContextResetClip(destContext);
396     CGContextSetInterpolationQuality(destContext, kCGInterpolationNone);
397     CGContextSetAlpha(destContext, 1.0);
398     CGContextSetBlendMode(destContext, kCGBlendModeCopy);
399     CGContextSetShadowWithColor(destContext, CGSizeZero, 0, 0);
400
401     // Draw the image in CG coordinate space
402     FloatSize scaledDestSize = scaleSizeToUserSpace(coordinateSystem == LogicalCoordinateSystem ? logicalSize() : internalSize(), m_data.m_backingStoreSize, internalSize());
403     IntPoint destPointInCGCoords(destPoint.x() + sourceRect.x(), scaledDestSize.height() - (destPoint.y() + sourceRect.y()) - sourceRect.height());
404     IntRect destRectInCGCoords(destPointInCGCoords, sourceCopySize);
405     CGContextClipToRect(destContext, destRectInCGCoords);
406
407     RetainPtr<CGImageRef> sourceCopyImage = sourceCopy->copyNativeImage();
408     FloatRect backingStoreInDestRect = FloatRect(FloatPoint(destPointInCGCoords.x(), destPointInCGCoords.y() + sourceCopySize.height() - (int)CGImageGetHeight(sourceCopyImage.get())), FloatSize(CGImageGetWidth(sourceCopyImage.get()), CGImageGetHeight(sourceCopyImage.get())));
409     CGContextDrawImage(destContext, backingStoreInDestRect, sourceCopyImage.get());
410     CGContextRestoreGState(destContext);
411 #endif
412 }
413
414 static inline CFStringRef jpegUTI()
415 {
416 #if PLATFORM(IOS) || PLATFORM(WIN)
417     static const CFStringRef kUTTypeJPEG = CFSTR("public.jpeg");
418 #endif
419     return kUTTypeJPEG;
420 }
421     
422 static RetainPtr<CFStringRef> utiFromMIMEType(const String& mimeType)
423 {
424 #if PLATFORM(MAC)
425     return adoptCF(UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType.createCFString().get(), 0));
426 #else
427     ASSERT(isMainThread()); // It is unclear if CFSTR is threadsafe.
428
429     // FIXME: Add Windows support for all the supported UTIs when a way to convert from MIMEType to UTI reliably is found.
430     // For now, only support PNG, JPEG, and GIF. See <rdar://problem/6095286>.
431     static const CFStringRef kUTTypePNG = CFSTR("public.png");
432     static const CFStringRef kUTTypeGIF = CFSTR("com.compuserve.gif");
433
434     if (equalIgnoringCase(mimeType, "image/png"))
435         return kUTTypePNG;
436     if (equalIgnoringCase(mimeType, "image/jpeg"))
437         return jpegUTI();
438     if (equalIgnoringCase(mimeType, "image/gif"))
439         return kUTTypeGIF;
440
441     ASSERT_NOT_REACHED();
442     return kUTTypePNG;
443 #endif
444 }
445
446 static bool CGImageEncodeToData(CGImageRef image, CFStringRef uti, const double* quality, CFMutableDataRef data)
447 {
448     if (!image || !uti || !data)
449         return false;
450
451     RetainPtr<CGImageDestinationRef> destination = adoptCF(CGImageDestinationCreateWithData(data, uti, 1, 0));
452     if (!destination)
453         return false;
454
455     RetainPtr<CFDictionaryRef> imageProperties = 0;
456     if (CFEqual(uti, jpegUTI()) && quality && *quality >= 0.0 && *quality <= 1.0) {
457         // Apply the compression quality to the JPEG image destination.
458         RetainPtr<CFNumberRef> compressionQuality = adoptCF(CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, quality));
459         const void* key = kCGImageDestinationLossyCompressionQuality;
460         const void* value = compressionQuality.get();
461         imageProperties = adoptCF(CFDictionaryCreate(0, &key, &value, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
462     }
463
464     // Setting kCGImageDestinationBackgroundColor to black for JPEG images in imageProperties would save some math
465     // in the calling functions, but it doesn't seem to work.
466
467     CGImageDestinationAddImage(destination.get(), image, imageProperties.get());
468     return CGImageDestinationFinalize(destination.get());
469 }
470
471 static String CGImageToDataURL(CGImageRef image, const String& mimeType, const double* quality)
472 {
473     RetainPtr<CFStringRef> uti = utiFromMIMEType(mimeType);
474     ASSERT(uti);
475
476     RetainPtr<CFMutableDataRef> data = adoptCF(CFDataCreateMutable(kCFAllocatorDefault, 0));
477     if (!CGImageEncodeToData(image, uti.get(), quality, data.get()))
478         return "data:,";
479
480     Vector<char> base64Data;
481     base64Encode(CFDataGetBytePtr(data.get()), CFDataGetLength(data.get()), base64Data);
482
483     return "data:" + mimeType + ";base64," + base64Data;
484 }
485
486 String ImageBuffer::toDataURL(const String& mimeType, const double* quality, CoordinateSystem) const
487 {
488     ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
489
490     if (m_context->isAcceleratedContext())
491         flushContext();
492
493     RetainPtr<CFStringRef> uti = utiFromMIMEType(mimeType);
494     ASSERT(uti);
495
496     RefPtr<Uint8ClampedArray> premultipliedData;
497     RetainPtr<CGImageRef> image;
498
499     if (CFEqual(uti.get(), jpegUTI())) {
500         // JPEGs don't have an alpha channel, so we have to manually composite on top of black.
501         premultipliedData = getPremultipliedImageData(IntRect(IntPoint(0, 0), logicalSize()));
502         if (!premultipliedData)
503             return "data:,";
504
505         RetainPtr<CGDataProviderRef> dataProvider;
506         dataProvider = adoptCF(CGDataProviderCreateWithData(0, premultipliedData->data(), 4 * logicalSize().width() * logicalSize().height(), 0));
507         if (!dataProvider)
508             return "data:,";
509
510         image = adoptCF(CGImageCreate(logicalSize().width(), logicalSize().height(), 8, 32, 4 * logicalSize().width(),
511                                     deviceRGBColorSpaceRef(), kCGBitmapByteOrderDefault | kCGImageAlphaNoneSkipLast,
512                                     dataProvider.get(), 0, false, kCGRenderingIntentDefault));
513     } else if (m_resolutionScale == 1) {
514         image = copyNativeImage(CopyBackingStore);
515         image = createCroppedImageIfNecessary(image.get(), internalSize());
516     } else {
517         image = copyNativeImage(DontCopyBackingStore);
518         RetainPtr<CGContextRef> context = adoptCF(CGBitmapContextCreate(0, logicalSize().width(), logicalSize().height(), 8, 4 * logicalSize().width(), deviceRGBColorSpaceRef(), kCGImageAlphaPremultipliedLast));
519         CGContextSetBlendMode(context.get(), kCGBlendModeCopy);
520         CGContextClipToRect(context.get(), CGRectMake(0, 0, logicalSize().width(), logicalSize().height()));
521         FloatSize imageSizeInUserSpace = scaleSizeToUserSpace(logicalSize(), m_data.m_backingStoreSize, internalSize());
522         CGContextDrawImage(context.get(), CGRectMake(0, 0, imageSizeInUserSpace.width(), imageSizeInUserSpace.height()), image.get());
523         image = adoptCF(CGBitmapContextCreateImage(context.get()));
524     }
525
526     return CGImageToDataURL(image.get(), mimeType, quality);
527 }
528
529 String ImageDataToDataURL(const ImageData& source, const String& mimeType, const double* quality)
530 {
531     ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
532
533     RetainPtr<CFStringRef> uti = utiFromMIMEType(mimeType);
534     ASSERT(uti);
535
536     CGImageAlphaInfo dataAlphaInfo = kCGImageAlphaLast;
537     unsigned char* data = source.data()->data();
538     Vector<uint8_t> premultipliedData;
539
540     if (CFEqual(uti.get(), jpegUTI())) {
541         // JPEGs don't have an alpha channel, so we have to manually composite on top of black.
542         size_t size = 4 * source.width() * source.height();
543         if (!premultipliedData.tryReserveCapacity(size))
544             return "data:,";
545
546         unsigned char *buffer = premultipliedData.data();
547         for (size_t i = 0; i < size; i += 4) {
548             unsigned alpha = data[i + 3];
549             if (alpha != 255) {
550                 buffer[i + 0] = data[i + 0] * alpha / 255;
551                 buffer[i + 1] = data[i + 1] * alpha / 255;
552                 buffer[i + 2] = data[i + 2] * alpha / 255;
553             } else {
554                 buffer[i + 0] = data[i + 0];
555                 buffer[i + 1] = data[i + 1];
556                 buffer[i + 2] = data[i + 2];
557             }
558         }
559
560         dataAlphaInfo = kCGImageAlphaNoneSkipLast; // Ignore the alpha channel.
561         data = premultipliedData.data();
562     }
563
564     RetainPtr<CGDataProviderRef> dataProvider;
565     dataProvider = adoptCF(CGDataProviderCreateWithData(0, data, 4 * source.width() * source.height(), 0));
566     if (!dataProvider)
567         return "data:,";
568
569     RetainPtr<CGImageRef> image;
570     image = adoptCF(CGImageCreate(source.width(), source.height(), 8, 32, 4 * source.width(),
571                                 deviceRGBColorSpaceRef(), kCGBitmapByteOrderDefault | dataAlphaInfo,
572                                 dataProvider.get(), 0, false, kCGRenderingIntentDefault));
573
574     return CGImageToDataURL(image.get(), mimeType, quality);
575 }
576
577 } // namespace WebCore