1/* 2 * Copyright (C) 2010 Igalia S.L. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17 */ 18 19 20#include "config.h" 21#include "ImageBuffer.h" 22 23#include "Base64.h" 24#include "GdkCairoUtilities.h" 25#include "GOwnPtr.h" 26#include "GRefPtrGtk.h" 27#include "MIMETypeRegistry.h" 28#include <cairo.h> 29#include <gtk/gtk.h> 30#include <wtf/text/CString.h> 31#include <wtf/text/StringConcatenate.h> 32 33namespace WebCore { 34 35String ImageBuffer::toDataURL(const String& mimeType, const double* quality) const 36{ 37 ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); 38 39 if (!mimeType.startsWith("image/")) 40 return "data:,"; 41 42 // List of supported image types comes from the GdkPixbuf documentation. 43 // http://library.gnome.org/devel/gdk-pixbuf/stable/gdk-pixbuf-file-saving.html#gdk-pixbuf-save-to-bufferv 44 String type = mimeType.substring(sizeof "image"); 45 if (type != "jpeg" && type != "png" && type != "tiff" && type != "ico" && type != "bmp") 46 return "data:,"; 47 48 GRefPtr<GdkPixbuf> pixbuf = cairoImageSurfaceToGdkPixbuf(m_data.m_surface); 49 if (!pixbuf) 50 return "data:,"; 51 52 GOwnPtr<gchar> buffer(0); 53 gsize bufferSize; 54 GError* error = 0; 55 gboolean success = FALSE; 56 if (type == "jpeg" && quality && *quality >= 0.0 && *quality <= 1.0) { 57 String qualityString = String::format("%f", *quality * 100.0); 58 success = gdk_pixbuf_save_to_buffer(pixbuf.get(), &buffer.outPtr(), &bufferSize, 59 type.utf8().data(), &error, "quality", qualityString.utf8().data(), NULL); 60 } else { 61 success = gdk_pixbuf_save_to_buffer(pixbuf.get(), &buffer.outPtr(), &bufferSize, type.utf8().data(), &error, NULL); 62 } 63 64 if (!success) 65 return "data:,"; 66 67 Vector<char> out; 68 base64Encode(reinterpret_cast<const char*>(buffer.get()), bufferSize, out); 69 70 return makeString("data:", mimeType, ";base64,", out); 71} 72 73} 74