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 | #include "config.h" |
20 | #include "ImageBuffer.h" |
21 | |
22 | #include "CairoUtilities.h" |
23 | #include "GRefPtrGtk.h" |
24 | #include "GdkCairoUtilities.h" |
25 | #include "MIMETypeRegistry.h" |
26 | #include <cairo.h> |
27 | #include <gtk/gtk.h> |
28 | #include <wtf/glib/GUniquePtr.h> |
29 | #include <wtf/text/Base64.h> |
30 | #include <wtf/text/CString.h> |
31 | #include <wtf/text/WTFString.h> |
32 | |
33 | namespace WebCore { |
34 | |
35 | static bool encodeImage(cairo_surface_t* surface, const String& mimeType, Optional<double> quality, GUniqueOutPtr<gchar>& buffer, gsize& bufferSize) |
36 | { |
37 | // List of supported image encoding types comes from the GdkPixbuf documentation. |
38 | // http://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-File-saving.html#gdk-pixbuf-save-to-bufferv |
39 | |
40 | String type = mimeType.substring(sizeof "image" ); |
41 | if (type != "jpeg" && type != "png" && type != "tiff" && type != "ico" && type != "bmp" ) |
42 | return false; |
43 | |
44 | GRefPtr<GdkPixbuf> pixbuf; |
45 | if (type == "jpeg" ) { |
46 | // JPEG doesn't support alpha channel. The <canvas> spec states that toDataURL() must encode a Porter-Duff |
47 | // composite source-over black for image types that do not support alpha. |
48 | RefPtr<cairo_surface_t> newSurface; |
49 | if (cairo_surface_get_type(surface) == CAIRO_SURFACE_TYPE_IMAGE) { |
50 | newSurface = adoptRef(cairo_image_surface_create_for_data(cairo_image_surface_get_data(surface), |
51 | CAIRO_FORMAT_RGB24, |
52 | cairo_image_surface_get_width(surface), |
53 | cairo_image_surface_get_height(surface), |
54 | cairo_image_surface_get_stride(surface))); |
55 | } else { |
56 | IntSize size = cairoSurfaceSize(surface); |
57 | newSurface = adoptRef(cairo_image_surface_create(CAIRO_FORMAT_RGB24, size.width(), size.height())); |
58 | RefPtr<cairo_t> cr = adoptRef(cairo_create(newSurface.get())); |
59 | cairo_set_source_surface(cr.get(), surface, 0, 0); |
60 | cairo_paint(cr.get()); |
61 | } |
62 | pixbuf = adoptGRef(cairoSurfaceToGdkPixbuf(newSurface.get())); |
63 | } else |
64 | pixbuf = adoptGRef(cairoSurfaceToGdkPixbuf(surface)); |
65 | if (!pixbuf) |
66 | return false; |
67 | |
68 | GUniqueOutPtr<GError> error; |
69 | if (type == "jpeg" && quality && *quality >= 0.0 && *quality <= 1.0) { |
70 | String qualityString = String::number(static_cast<int>(*quality * 100.0 + 0.5)); |
71 | gdk_pixbuf_save_to_buffer(pixbuf.get(), &buffer.outPtr(), &bufferSize, type.utf8().data(), &error.outPtr(), "quality" , qualityString.utf8().data(), NULL); |
72 | } else |
73 | gdk_pixbuf_save_to_buffer(pixbuf.get(), &buffer.outPtr(), &bufferSize, type.utf8().data(), &error.outPtr(), NULL); |
74 | |
75 | return !error; |
76 | } |
77 | |
78 | String ImageBuffer::toDataURL(const String& mimeType, Optional<double> quality, PreserveResolution) const |
79 | { |
80 | Vector<uint8_t> imageData = toData(mimeType, quality); |
81 | if (imageData.isEmpty()) |
82 | return "data:," ; |
83 | |
84 | Vector<char> base64Data; |
85 | base64Encode(imageData.data(), imageData.size(), base64Data); |
86 | |
87 | return "data:" + mimeType + ";base64," + base64Data; |
88 | } |
89 | |
90 | Vector<uint8_t> ImageBuffer::toData(const String& mimeType, Optional<double> quality) const |
91 | { |
92 | ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType)); |
93 | |
94 | GUniqueOutPtr<gchar> buffer; |
95 | gsize bufferSize; |
96 | if (!encodeImage(m_data.m_surface.get(), mimeType, quality, buffer, bufferSize)) |
97 | return { }; |
98 | |
99 | Vector<uint8_t> imageData; |
100 | imageData.append(buffer.get(), bufferSize); |
101 | return imageData; |
102 | } |
103 | |
104 | } |
105 | |