From ded190db41af0886b9cf1a4a86597e8d8dd06d5d Mon Sep 17 00:00:00 2001 From: Cary Clark Date: Mon, 24 May 2010 10:44:10 -0400 Subject: implement Canvas.toDataURL The Chrome implementation serves as the base for this code. The test is at http://philip.html5.org/tests/canvas/suite/tests/index.toDataURL.html Change-Id: I873518362793fbe1c99686fbe240cfe7c27d1493 http://b/2620678 --- .../graphics/android/ImageBufferAndroid.cpp | 28 ++++++++++++++++------ 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'WebCore') diff --git a/WebCore/platform/graphics/android/ImageBufferAndroid.cpp b/WebCore/platform/graphics/android/ImageBufferAndroid.cpp index 6efccfe..92c585f 100644 --- a/WebCore/platform/graphics/android/ImageBufferAndroid.cpp +++ b/WebCore/platform/graphics/android/ImageBufferAndroid.cpp @@ -24,11 +24,11 @@ */ #include "config.h" -#include "BitmapImage.h" #include "ImageBuffer.h" -#include "ImageData.h" -#include "NotImplemented.h" +#include "Base64.h" +#include "BitmapImage.h" +#include "ImageData.h" #include "android_graphics.h" #include "GraphicsContext.h" #include "PlatformGraphicsContext.h" @@ -36,6 +36,8 @@ #include "SkCanvas.h" #include "SkColorPriv.h" #include "SkDevice.h" +#include "SkImageEncoder.h" +#include "SkStream.h" #include "SkUnPreMultiply.h" using namespace std; @@ -208,10 +210,22 @@ void ImageBuffer::putUnmultipliedImageData(ImageData* source, const IntRect& sou String ImageBuffer::toDataURL(const String&) const -{ - // leaving this unimplemented, until I understand what its for (and what it - // really is). - return "data:,"; // I think this means we couldn't make the data url +{ + // Encode the image into a vector. + SkDynamicMemoryWStream pngStream; + const SkBitmap& dst = android_gc2canvas(context())->getDevice()->accessBitmap(true); + SkImageEncoder::EncodeStream(&pngStream, dst, SkImageEncoder::kPNG_Type, 100); + + // Convert it into base64. + Vector pngEncodedData; + pngEncodedData.append(pngStream.getStream(), pngStream.getOffset()); + Vector base64EncodedData; + base64Encode(pngEncodedData, base64EncodedData); + // Append with a \0 so that it's a valid string. + base64EncodedData.append('\0'); + + // And the resulting string. + return String::format("data:image/png;base64,%s", base64EncodedData.data()); } } -- cgit v1.1