[GTK] Separate image encoding from dataURL construction
authornoel.gordon@gmail.com <noel.gordon@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 10 Apr 2012 18:29:09 +0000 (18:29 +0000)
committernoel.gordon@gmail.com <noel.gordon@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 10 Apr 2012 18:29:09 +0000 (18:29 +0000)
https://bugs.webkit.org/show_bug.cgi?id=83152

Reviewed by Martin Robinson.

Remove the implicit assumption that a dataURL is the only desired output format
of the image encoding phase.

No new tests, refactoring only, covered by existing canvas tests.

* platform/graphics/gtk/ImageBufferGtk.cpp:
(WebCore::encodeImage): Output the encoded image to the provided GOwnPtr<gchar>&
buffer. Update GTK document reference to a valid URL. Round the quality argument
to an int like the toDataURL() implementations of other ports. Ditch the success
variable; instead test the GError* error return to indicate success.
(WebCore):
(WebCore::ImageBuffer::toDataURL): Format the dataURL encoding of the mimeType
encoded image data buffer herein.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@113742 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/gtk/ImageBufferGtk.cpp

index 5b38e08..38d2a6a 100644 (file)
@@ -1,3 +1,24 @@
+2012-04-10  Noel Gordon  <noel.gordon@gmail.com>
+
+        [GTK] Separate image encoding from dataURL construction
+        https://bugs.webkit.org/show_bug.cgi?id=83152
+
+        Reviewed by Martin Robinson.
+
+        Remove the implicit assumption that a dataURL is the only desired output format
+        of the image encoding phase.
+
+        No new tests, refactoring only, covered by existing canvas tests.
+
+        * platform/graphics/gtk/ImageBufferGtk.cpp:
+        (WebCore::encodeImage): Output the encoded image to the provided GOwnPtr<gchar>&
+        buffer. Update GTK document reference to a valid URL. Round the quality argument
+        to an int like the toDataURL() implementations of other ports. Ditch the success
+        variable; instead test the GError* error return to indicate success.
+        (WebCore):
+        (WebCore::ImageBuffer::toDataURL): Format the dataURL encoding of the mimeType
+        encoded image data buffer herein.
+
 2012-04-09  James Robinson  <jamesr@chromium.org>
 
         [chromium] Defer texture id allocation for copies until the actual copy executes
index 2083257..617feab 100644 (file)
@@ -16,7 +16,6 @@
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-
 #include "config.h"
 #include "ImageBuffer.h"
 
 
 namespace WebCore {
 
-String ImageBuffer::toDataURL(const String& mimeType, const double* quality) const
+static bool encodeImage(cairo_surface_t* surface, const String& mimeType, const double* quality, GOwnPtr<gchar>& buffer, gsize& bufferSize)
 {
-    ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
-
-    if (!mimeType.startsWith("image/"))
-        return "data:,";
+    // List of supported image encoding types comes from the GdkPixbuf documentation.
+    // http://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-File-saving.html#gdk-pixbuf-save-to-bufferv
 
-    // List of supported image types comes from the GdkPixbuf documentation.
-    // http://library.gnome.org/devel/gdk-pixbuf/stable/gdk-pixbuf-file-saving.html#gdk-pixbuf-save-to-bufferv
     String type = mimeType.substring(sizeof "image");
     if (type != "jpeg" && type != "png" && type != "tiff" && type != "ico" && type != "bmp")
-        return "data:,";
+        return false;
 
-    GRefPtr<GdkPixbuf> pixbuf = cairoImageSurfaceToGdkPixbuf(m_data.m_surface);
+    GRefPtr<GdkPixbuf> pixbuf = cairoImageSurfaceToGdkPixbuf(surface);
     if (!pixbuf)
-        return "data:,";
+        return false;
 
-    GOwnPtr<gchar> buffer(0);
-    gsize bufferSize;
     GError* error = 0;
-    gboolean success = FALSE;
     if (type == "jpeg" && quality && *quality >= 0.0 && *quality <= 1.0) {
-        String qualityString = String::format("%f", *quality * 100.0);
-        success = gdk_pixbuf_save_to_buffer(pixbuf.get(), &buffer.outPtr(), &bufferSize,
-            type.utf8().data(), &error, "quality", qualityString.utf8().data(), NULL);
-    } else {
-        success = gdk_pixbuf_save_to_buffer(pixbuf.get(), &buffer.outPtr(), &bufferSize, type.utf8().data(), &error, NULL);
-    }
+        String qualityString = String::format("%d", static_cast<int>(*quality * 100.0 + 0.5));
+        gdk_pixbuf_save_to_buffer(pixbuf.get(), &buffer.outPtr(), &bufferSize, type.utf8().data(), &error, "quality", qualityString.utf8().data(), NULL);
+    } else
+        gdk_pixbuf_save_to_buffer(pixbuf.get(), &buffer.outPtr(), &bufferSize, type.utf8().data(), &error, NULL);
 
-    if (!success)
+    return !error;
+}
+
+String ImageBuffer::toDataURL(const String& mimeType, const double* quality) const
+{
+    ASSERT(MIMETypeRegistry::isSupportedImageMIMETypeForEncoding(mimeType));
+
+    if (!mimeType.startsWith("image/"))
+        return "data:,";
+
+    GOwnPtr<gchar> buffer(0);
+    gsize bufferSize;
+    if (!encodeImage(m_data.m_surface, mimeType, quality, buffer, bufferSize))
         return "data:,";
 
-    Vector<char> out;
-    base64Encode(reinterpret_cast<const char*>(buffer.get()), bufferSize, out);
+    Vector<char> base64Data;
+    base64Encode(buffer.get(), bufferSize, base64Data);
 
-    return "data:" + mimeType + ";base64," + out;
+    return "data:" + mimeType + ";base64," + base64Data;
 }
 
 }