[gegl] operation: Added npy save
- From: Dov Grobgeld <dov src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gegl] operation: Added npy save
- Date: Wed, 29 May 2013 19:28:12 +0000 (UTC)
commit af76fe6bb33a3dcd878b538aa974488a390bc90e
Author: Dov Grobgeld <dov grobgeld gmail com>
Date: Wed May 29 22:28:04 2013 +0300
operation: Added npy save
Added save operation for the npy file format. See:
(https://github.com/numpy/numpy/blob/master/doc/neps/npy-format.txt)
The operation operation saves in either "RGB float" or "Y float" depending on the input image format.
operations/external/Makefile.am | 5 ++
operations/external/npy-save.c | 150 +++++++++++++++++++++++++++++++++++++++
2 files changed, 155 insertions(+), 0 deletions(-)
---
diff --git a/operations/external/Makefile.am b/operations/external/Makefile.am
index 2befbb7..b29df12 100644
--- a/operations/external/Makefile.am
+++ b/operations/external/Makefile.am
@@ -125,6 +125,11 @@ ppm_load_la_LIBADD = $(op_libs)
ppm_save_la_SOURCES = ppm-save.c
ppm_save_la_LIBADD = $(op_libs)
+# No dependencies
+ops += npy-save.la
+npy_save_la_SOURCES = npy-save.c
+npy_save_la_LIBADD = $(op_libs)
+
# Dependencies are in our source tree
ops += rgbe-load.la rgbe-save.la
rgbe_load_la_SOURCES = rgbe-load.c
diff --git a/operations/external/npy-save.c b/operations/external/npy-save.c
new file mode 100644
index 0000000..6e9cb5a
--- /dev/null
+++ b/operations/external/npy-save.c
@@ -0,0 +1,150 @@
+/* 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 Dov Grobgeld 2013 <dov.grobgeld (a) gmail.com>
+ *
+ * This operation saves a buffer in the npy file format. It may be
+ * read into python as follows:
+ *
+ * import numpy
+ * img = numpy.load('image.npy')
+ */
+
+#include "config.h"
+#include <glib/gi18n-lib.h>
+
+
+#ifdef GEGL_CHANT_PROPERTIES
+
+gegl_chant_string (path, _("File"), "",
+ _("Target path and filename, use '-' for stdout."))
+
+#else
+
+#define GEGL_CHANT_TYPE_SINK
+#define GEGL_CHANT_C_FILE "npy-save.c"
+
+#include "gegl-chant.h"
+#include <stdio.h>
+
+static int npywrite_header(FILE *fp, int width, int height, int num_channels)
+{
+ const gchar* format;
+ gsize header_len;
+ gchar *header;
+
+ // Write header and version number to file
+ fwrite("\223NUMPY"
+ "\001\000"
+ , 1, 8, fp);
+
+
+ if (num_channels == 3)
+ format = "{'descr': '<f4', 'fortran_order': False, 'shape': (%d, %d, 3), } \n";
+ else
+ format = "{'descr': '<f4', 'fortran_order': False, 'shape': (%d, %d), } \n";
+
+ header = g_strdup_printf(format, height, width);
+ header_len = strlen(header);
+ fwrite(&header_len, 2, 1, fp);
+ fwrite(header, header_len, 1, fp);
+ g_free(header);
+
+ return 0;
+}
+
+static gboolean
+process (GeglOperation *operation,
+ GeglBuffer *input,
+ const GeglRectangle *rect,
+ gint level)
+{
+ GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
+
+ FILE *fp;
+ guchar *data;
+ gsize bpc;
+ gsize numbytes_scanline;
+ gsize numchannels;
+ gboolean ret = FALSE;
+ gint row;
+ gint slice_thickness = 32;
+ const Babl *output_format;
+ const Babl *input_format = gegl_buffer_get_format(input);
+
+ // Get the current format and use it to decide whether to save
+ // the output in color or gray level formats.
+ bpc = sizeof(gfloat);
+ if (babl_format_get_n_components(input_format) >= 3)
+ {
+ numchannels = 3;
+ output_format = babl_format("RGB float");
+ }
+ else
+ {
+ numchannels = 1;
+ output_format = babl_format ("Y float");
+ }
+
+ numbytes_scanline = rect->width * numchannels * bpc;
+
+ fp = (!strcmp (o->path, "-") ? stdout : fopen(o->path, "wb") );
+
+ npywrite_header(fp, rect->width, rect->height, numchannels);
+
+ data = g_malloc (numbytes_scanline * slice_thickness);
+
+ for (row=0; row < rect->height; row+= slice_thickness)
+ {
+ GeglRectangle rect_slice;
+ rect_slice.x = rect->x;
+ rect_slice.width = rect->width;
+ rect_slice.y = rect->y+row;
+ rect_slice.height = MIN(slice_thickness, rect->height-row);
+
+ gegl_buffer_get (input, &rect_slice, 1.0, output_format, data,
+ GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
+
+ fwrite(data, numbytes_scanline, rect_slice.height, fp);
+ }
+
+ g_free (data);
+
+ return ret;
+}
+
+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:npy-save",
+ "categories" , "output",
+ "description" ,
+ _("NPY image saver (Numerical python file saver.)"),
+ NULL);
+
+ gegl_extension_handler_register_saver (".npy", "gegl:npy-save");
+}
+
+#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]