[gegl] gegl-compression: add zlib algorithms



commit ace1a79e552db86b5cfffd8f0d2cc437057471fe
Author: Ell <ell_se yahoo com>
Date:   Mon Dec 17 05:04:40 2018 -0500

    gegl-compression: add zlib algorithms
    
    Add "zlib", and "zlib1" through "zlib9", compression algorithms,
    which use zlib's DEFLATE algorithm to compress the data.  The
    numeric suffix specifies the compression level, with higher levels
    being better, but slower; "zlib" uses the default compression
    level.
    
    These algorithms are only present when the zlib library is
    available.

 configure.ac                        |  21 ++++
 gegl/Makefile.am                    |   2 +-
 gegl/buffer/Makefile.am             |   4 +-
 gegl/buffer/gegl-compression-zlib.c | 206 ++++++++++++++++++++++++++++++++++++
 gegl/buffer/gegl-compression-zlib.h |  30 ++++++
 gegl/buffer/gegl-compression.c      |   2 +
 6 files changed, 263 insertions(+), 2 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 0bfb83bdc..8afb391a5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -53,6 +53,7 @@ m4_define([lua_required_version], [5.1.0])
 m4_define([openexr_required_version], [1.6.1])
 m4_define([libraw_required_version], [0.15.4])
 m4_define([pango_required_version], [1.38.0])
+m4_define([zlib_required_version], [1.2.0])
 m4_define([png_required_version], [1.6.0])
 m4_define([sdl_required_version], [1.2.0])
 m4_define([libtiff_required_version], [4.0.0])
@@ -824,6 +825,25 @@ if test "$jpeg_ok" != "yes"; then
    AC_MSG_ERROR([Could not find a usable JPEG library with header files])
 fi
 
+################
+# Check for zlib
+################
+
+AC_ARG_WITH(zlib, [  --without-zlib          build without zlib support])
+
+have_zlib="no"
+if test "x$with_zlib" != "xno"; then
+  PKG_CHECK_MODULES(ZLIB, zlib,
+    AC_DEFINE(HAVE_ZLIB, 1,
+                [Define to 1 if the zlib library is available])
+    have_zlib="yes",
+    have_zlib="no  (zlib not found)")
+fi
+
+AM_CONDITIONAL(HAVE_ZLIB, test "$have_zlib" = "yes")
+
+AC_SUBST(ZLIB_CFLAGS)
+AC_SUBST(ZLIB_LIBS)
 
 ##################
 # Check for libpng
@@ -1380,6 +1400,7 @@ Optional dependencies:
   pangocairo:      $have_pangocairo
   GDKPixbuf:       $have_gdk_pixbuf
   JPEG:            $jpeg_ok
+  zlib:            $have_zlib
   PNG:             $have_libpng
   OpenEXR:         $have_openexr
   rsvg:            $have_librsvg
diff --git a/gegl/Makefile.am b/gegl/Makefile.am
index 2f96707bb..b049270be 100644
--- a/gegl/Makefile.am
+++ b/gegl/Makefile.am
@@ -41,7 +41,7 @@ AM_LDFLAGS = \
        $(no_undefined) -export-dynamic -version-info $(GEGL_LIBRARY_VERSION)
 
 LIBS = \
-       $(DEP_LIBS) $(BABL_LIBS) $(MATH_LIB)
+       $(DEP_LIBS) $(BABL_LIBS) $(ZLIB_LIBS) $(MATH_LIB)
 
 GEGL_publicdir = $(includedir)/gegl-$(GEGL_API_VERSION)
 
diff --git a/gegl/buffer/Makefile.am b/gegl/buffer/Makefile.am
index ae95964c7..106f72cb3 100644
--- a/gegl/buffer/Makefile.am
+++ b/gegl/buffer/Makefile.am
@@ -18,7 +18,7 @@ AM_CPPFLAGS = \
 
 EXTRA_DIST = gegl-algorithms-boxfilter.inc gegl-algorithms-2x2-downscale.inc gegl-algorithms-bilinear.inc
 
-AM_CFLAGS = $(DEP_CFLAGS) $(BABL_CFLAGS)
+AM_CFLAGS = $(DEP_CFLAGS) $(BABL_CFLAGS) $(ZLIB_CFLAGS)
 
 noinst_LTLIBRARIES = libbuffer.la
 
@@ -45,6 +45,7 @@ libbuffer_la_SOURCES = \
     gegl-compression.c         \
     gegl-compression-nop.c     \
     gegl-compression-rle.c     \
+    gegl-compression-zlib.c    \
     gegl-sampler.c             \
     gegl-sampler-cubic.c       \
     gegl-sampler-linear.c      \
@@ -79,6 +80,7 @@ libbuffer_la_SOURCES = \
     gegl-compression.h         \
     gegl-compression-nop.h     \
     gegl-compression-rle.h     \
+    gegl-compression-zlib.h    \
     gegl-sampler.h             \
     gegl-sampler-cubic.h       \
     gegl-sampler-linear.h      \
diff --git a/gegl/buffer/gegl-compression-zlib.c b/gegl/buffer/gegl-compression-zlib.c
new file mode 100644
index 000000000..f24d29a92
--- /dev/null
+++ b/gegl/buffer/gegl-compression-zlib.c
@@ -0,0 +1,206 @@
+/* This file is part of 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 <https://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "gegl-compression.h"
+#include "gegl-compression-zlib.h"
+
+
+#ifdef HAVE_ZLIB
+
+
+#include <zlib.h>
+
+typedef struct
+{
+  GeglCompression compression;
+  gint            level;
+} GeglCompressionZlib;
+
+
+/*  local function prototypes  */
+
+static gboolean   gegl_compression_zlib_compress   (const GeglCompression *compression,
+                                                    const Babl            *format,
+                                                    gconstpointer          data,
+                                                    gint                   n,
+                                                    gpointer               compressed,
+                                                    gint                  *compressed_size,
+                                                    gint                   max_compressed_size);
+static gboolean   gegl_compression_zlib_decompress (const GeglCompression *compression,
+                                                    const Babl            *format,
+                                                    gpointer               data,
+                                                    gint                   n,
+                                                    gconstpointer          compressed,
+                                                    gint                   compressed_size);
+
+static voidpf     gegl_compression_zlib_alloc      (voidpf                 opaque,
+                                                    uInt                   items,
+                                                    uInt                   size);
+static void       gegl_compression_zlib_free       (voidpf                 opaque,
+                                                    voidpf                 address);
+
+
+
+/*  private functions  */
+
+static gboolean
+gegl_compression_zlib_compress (const GeglCompression *compression,
+                                const Babl            *format,
+                                gconstpointer          data,
+                                gint                   n,
+                                gpointer               compressed,
+                                gint                  *compressed_size,
+                                gint                   max_compressed_size)
+{
+  const GeglCompressionZlib *compression_zlib;
+  z_stream                   stream;
+  gint                       size;
+
+  compression_zlib = (const GeglCompressionZlib *) compression;
+
+  stream.zalloc = gegl_compression_zlib_alloc;
+  stream.zfree  = gegl_compression_zlib_free;
+  stream.opaque = NULL;
+
+  if (deflateInit (&stream, compression_zlib->level) != Z_OK)
+    return FALSE;
+
+  size = n * babl_format_get_bytes_per_pixel (format);
+
+  stream.next_in   = (gpointer) data;
+  stream.avail_in  = size;
+  stream.next_out  = compressed;
+  stream.avail_out = max_compressed_size;
+
+  if (deflate (&stream, Z_FINISH) != Z_STREAM_END)
+    {
+      deflateEnd (&stream);
+
+      return FALSE;
+    }
+
+  *compressed_size = stream.total_out;
+
+  deflateEnd (&stream);
+
+  return TRUE;
+}
+
+static gboolean
+gegl_compression_zlib_decompress (const GeglCompression *compression,
+                                  const Babl            *format,
+                                  gpointer               data,
+                                  gint                   n,
+                                  gconstpointer          compressed,
+                                  gint                   compressed_size)
+{
+  z_stream stream;
+  gint     size;
+
+  stream.zalloc = gegl_compression_zlib_alloc;
+  stream.zfree  = gegl_compression_zlib_free;
+  stream.opaque = NULL;
+
+  if (inflateInit (&stream) != Z_OK)
+    return FALSE;
+
+  size = n * babl_format_get_bytes_per_pixel (format);
+
+  stream.next_in   = (gpointer) compressed;
+  stream.avail_in  = compressed_size;
+  stream.next_out  = data;
+  stream.avail_out = size;
+
+  if (inflate (&stream, Z_FINISH) != Z_STREAM_END ||
+      stream.total_out != size)
+    {
+      inflateEnd (&stream);
+
+      return FALSE;
+    }
+
+  inflateEnd (&stream);
+
+  return TRUE;
+}
+
+static voidpf
+gegl_compression_zlib_alloc (voidpf opaque,
+                             uInt   items,
+                             uInt   size)
+{
+  return g_try_malloc_n (items, size);
+}
+
+static void
+gegl_compression_zlib_free (voidpf opaque,
+                            voidpf address)
+{
+  g_free (address);
+}
+
+
+/*  public functions  */
+
+void
+gegl_compression_zlib_init (void)
+{
+  #define COMPRESSION_ZLIB(name, zlib_level)                \
+    G_STMT_START                                            \
+      {                                                     \
+        static const GeglCompressionZlib compression_zlib = \
+        {                                                   \
+          .compression =                                    \
+          {                                                 \
+            .compress   = gegl_compression_zlib_compress,   \
+            .decompress = gegl_compression_zlib_decompress  \
+          },                                                \
+          .level = (zlib_level)                             \
+        };                                                  \
+                                                            \
+        gegl_compression_register (                         \
+          name,                                             \
+          (const GeglCompression *) &compression_zlib);     \
+      }                                                     \
+    G_STMT_END
+
+  COMPRESSION_ZLIB ("zlib",  Z_DEFAULT_COMPRESSION);
+  COMPRESSION_ZLIB ("zlib1", 1);
+  COMPRESSION_ZLIB ("zlib2", 2);
+  COMPRESSION_ZLIB ("zlib3", 3);
+  COMPRESSION_ZLIB ("zlib4", 4);
+  COMPRESSION_ZLIB ("zlib5", 5);
+  COMPRESSION_ZLIB ("zlib6", 6);
+  COMPRESSION_ZLIB ("zlib7", 7);
+  COMPRESSION_ZLIB ("zlib8", 8);
+  COMPRESSION_ZLIB ("zlib9", 9);
+}
+
+
+#else /* ! HAVE_ZLIB */
+
+
+/*  public functions  */
+
+void
+gegl_compression_zlib_init (void)
+{
+}
+
+
+#endif /* ! HAVE_ZLIB */
diff --git a/gegl/buffer/gegl-compression-zlib.h b/gegl/buffer/gegl-compression-zlib.h
new file mode 100644
index 000000000..d5b7a8a01
--- /dev/null
+++ b/gegl/buffer/gegl-compression-zlib.h
@@ -0,0 +1,30 @@
+/* This file is part of 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 <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GEGL_COMPRESSION_ZLIB_H__
+#define __GEGL_COMPRESSION_ZLIB_H__
+
+
+#include <glib.h>
+#include <babl/babl.h>
+
+G_BEGIN_DECLS
+
+void   gegl_compression_zlib_init (void);
+
+G_END_DECLS
+
+#endif
diff --git a/gegl/buffer/gegl-compression.c b/gegl/buffer/gegl-compression.c
index 0c2abe2a8..2886aa720 100644
--- a/gegl/buffer/gegl-compression.c
+++ b/gegl/buffer/gegl-compression.c
@@ -22,6 +22,7 @@
 #include "gegl-compression.h"
 #include "gegl-compression-nop.h"
 #include "gegl-compression-rle.h"
+#include "gegl-compression-zlib.h"
 
 
 /*  local variables  */
@@ -40,6 +41,7 @@ gegl_compression_init (void)
 
   gegl_compression_nop_init ();
   gegl_compression_rle_init ();
+  gegl_compression_zlib_init ();
 }
 
 void


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