[gegl/soc-2013-opecl-ops] operations: Add gegl:webp-save



commit 7930ee07aaf4b77aa1f9c709f398f9c8b423f2ea
Author: Michael Henning <drawoc darkrefraction com>
Date:   Thu Jul 25 13:21:02 2013 -0400

    operations: Add gegl:webp-save

 operations/external/Makefile.am |    5 ++
 operations/external/webp-save.c |  123 +++++++++++++++++++++++++++++++++++++++
 po/POTFILES.in                  |    1 +
 3 files changed, 129 insertions(+), 0 deletions(-)
---
diff --git a/operations/external/Makefile.am b/operations/external/Makefile.am
index de8c2ed..b929744 100644
--- a/operations/external/Makefile.am
+++ b/operations/external/Makefile.am
@@ -130,6 +130,11 @@ ops += webp-load.la
 webp_load_la_SOURCES = webp-load.c
 webp_load_la_LIBADD  = $(op_libs) $(WEBP_LIBS)
 webp_load_la_CFLAGS  = $(AM_CFLAGS) $(WEBP_CFLAGS)
+
+ops += webp-save.la
+webp_save_la_SOURCES = webp-save.c
+webp_save_la_LIBADD  = $(op_libs) $(WEBP_LIBS)
+webp_save_la_CFLAGS  = $(AM_CFLAGS) $(WEBP_CFLAGS)
 endif
 
 # No dependencies
diff --git a/operations/external/webp-save.c b/operations/external/webp-save.c
new file mode 100644
index 0000000..7242a9a
--- /dev/null
+++ b/operations/external/webp-save.c
@@ -0,0 +1,123 @@
+/* This file is an image processing operation for GEGL
+ *
+ * GEGL is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * GEGL is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Copyright 2013 Michael Henning <drawoc darkrefraction com>
+ */
+
+#include "config.h"
+#include <glib/gi18n-lib.h>
+
+
+#ifdef GEGL_CHANT_PROPERTIES
+
+gegl_chant_string (path, _("File"), "", _("Target file path."))
+
+gegl_chant_int    (quality, _("Quality"), 1, 100, 90,
+                   _("WebP compression quality"))
+
+#else
+
+#define GEGL_CHANT_TYPE_SINK
+#define GEGL_CHANT_C_FILE       "webp-save.c"
+
+#include "gegl-chant.h"
+#include <webp/encode.h>
+#include <stdio.h>
+
+static int
+write_func (const uint8_t* data, size_t data_size, const WebPPicture* const pic)
+{
+  FILE* const out = (FILE*) pic->custom_ptr;
+
+  if (!data_size) return 1;
+  return fwrite (data, data_size, 1, out) == 1;
+}
+
+static gboolean
+process (GeglOperation       *operation,
+         GeglBuffer          *input,
+         const GeglRectangle *result,
+         gint                 level)
+{
+  GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
+  const GeglRectangle *bounds = gegl_buffer_get_extent (input);
+
+  FILE* file;
+  gboolean alpha;
+  int status;
+
+  WebPConfig config;
+  WebPPicture pic;
+
+  if (!WebPConfigPreset (&config, WEBP_PRESET_DEFAULT, o->quality))
+    return FALSE;
+
+  alpha = babl_format_has_alpha (gegl_buffer_get_format (input));
+
+  if (!WebPValidateConfig (&config))
+    return FALSE;
+
+  if (!WebPPictureInit (&pic))
+    return FALSE;
+
+  pic.width = bounds->width;
+  pic.height = bounds->height;
+
+  file = fopen (o->path, "wb");
+  pic.writer = write_func;
+  pic.custom_ptr = file;
+
+  {
+    gint rowstride = bounds->width * sizeof (char) * 4;
+    gpointer temp = g_malloc (rowstride * bounds->height);
+
+    gegl_buffer_get (input, bounds, 1.0, babl_format ("R'G'B'A u8"), temp,
+                     rowstride, GEGL_ABYSS_NONE);
+
+    WebPPictureImportRGBA (&pic, temp, rowstride);
+
+    g_free (temp);
+  }
+
+  status = WebPEncode (&config, &pic);
+  WebPPictureFree (&pic);
+  fclose (file);
+
+  return status ? TRUE : FALSE;
+}
+
+
+static void
+gegl_chant_class_init (GeglChantClass *klass)
+{
+  GeglOperationClass     *operation_class;
+  GeglOperationSinkClass *sink_class;
+
+  operation_class = GEGL_OPERATION_CLASS (klass);
+  sink_class      = GEGL_OPERATION_SINK_CLASS (klass);
+
+  sink_class->process    = process;
+  sink_class->needs_full = TRUE;
+
+  gegl_operation_class_set_keys (operation_class,
+  "name"       , "gegl:webp-save",
+  "categories" , "output",
+  "description", _("WebP image saver."),
+   NULL);
+
+  gegl_extension_handler_register_saver (".webp", "gegl:webp-save");
+}
+
+#endif
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 6a45ea7..cd110b6 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -129,6 +129,7 @@ operations/external/v4l.c
 operations/external/vector-fill.c
 operations/external/vector-stroke.c
 operations/external/webp-load.c
+operations/external/webp-save.c
 operations/generated/add.c
 operations/generated/clear.c
 operations/generated/color-burn.c


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]