[gimp] app: in xcf-load, avoid writing buffer data for empty tiles



commit 0ae7acd594035caa2c20682c101161cbb2a9cd06
Author: Ell <ell_se yahoo com>
Date:   Wed Oct 4 06:43:56 2017 -0400

    app: in xcf-load, avoid writing buffer data for empty tiles
    
    When loading tile data, avoid copying the data into the GEGL
    buffer when the tile is empty (i.e., all its bytes are 0), so that
    GEGL doesn't allocate memory for it unnecessarily.

 app/xcf/Makefile.am |    2 +
 app/xcf/xcf-load.c  |   45 +++++++++++++++++++++++++++---------------
 app/xcf/xcf-utils.c |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++
 app/xcf/xcf-utils.h |   26 +++++++++++++++++++++++++
 4 files changed, 110 insertions(+), 16 deletions(-)
---
diff --git a/app/xcf/Makefile.am b/app/xcf/Makefile.am
index 65a3dc8..a95a87c 100644
--- a/app/xcf/Makefile.am
+++ b/app/xcf/Makefile.am
@@ -25,5 +25,7 @@ libappxcf_a_SOURCES = \
        xcf-save.h      \
        xcf-seek.c      \
        xcf-seek.h      \
+       xcf-utils.c     \
+       xcf-utils.h     \
        xcf-write.c     \
        xcf-write.h
diff --git a/app/xcf/xcf-load.c b/app/xcf/xcf-load.c
index c820c50..a4978af 100644
--- a/app/xcf/xcf-load.c
+++ b/app/xcf/xcf-load.c
@@ -70,6 +70,7 @@
 #include "xcf-load.h"
 #include "xcf-read.h"
 #include "xcf-seek.h"
+#include "xcf-utils.h"
 
 #include "gimp-log.h"
 #include "gimp-intl.h"
@@ -2064,8 +2065,11 @@ xcf_load_tile (XcfInfo       *info,
                           tile_size / bpp * n_components);
     }
 
-  gegl_buffer_set (buffer, tile_rect, 0, format, tile_data,
-                   GEGL_AUTO_ROWSTRIDE);
+  if (! xcf_data_is_zero (tile_data, tile_size))
+    {
+      gegl_buffer_set (buffer, tile_rect, 0, format, tile_data,
+                       GEGL_AUTO_ROWSTRIDE);
+    }
 
   return TRUE;
 }
@@ -2080,6 +2084,7 @@ xcf_load_tile_rle (XcfInfo       *info,
   gint    bpp       = babl_format_get_bytes_per_pixel (format);
   gint    tile_size = bpp * tile_rect->width * tile_rect->height;
   guchar *tile_data = g_alloca (tile_size);
+  guchar  nonzero   = FALSE;
   gsize   bytes_read;
   gint    i;
   guchar *xcfdata;
@@ -2158,6 +2163,7 @@ xcf_load_tile_rle (XcfInfo       *info,
               while (length-- > 0)
                 {
                   *data = *xcfdata++;
+                  nonzero |= *data;
                   data += bpp;
                 }
             }
@@ -2189,6 +2195,7 @@ xcf_load_tile_rle (XcfInfo       *info,
                 }
 
               val = *xcfdata++;
+              nonzero |= val;
 
               for (j = 0; j < length; j++)
                 {
@@ -2199,16 +2206,19 @@ xcf_load_tile_rle (XcfInfo       *info,
         }
     }
 
-  if (info->file_version >= 12)
+  if (nonzero)
     {
-      gint n_components = babl_format_get_n_components (format);
+      if (info->file_version >= 12)
+        {
+          gint n_components = babl_format_get_n_components (format);
 
-      xcf_read_from_be (bpp / n_components, tile_data,
-                        tile_size / bpp * n_components);
-    }
+          xcf_read_from_be (bpp / n_components, tile_data,
+                            tile_size / bpp * n_components);
+        }
 
-  gegl_buffer_set (buffer, tile_rect, 0, format, tile_data,
-                   GEGL_AUTO_ROWSTRIDE);
+      gegl_buffer_set (buffer, tile_rect, 0, format, tile_data,
+                       GEGL_AUTO_ROWSTRIDE);
+    }
 
   return TRUE;
 
@@ -2297,16 +2307,19 @@ xcf_load_tile_zlib (XcfInfo       *info,
         }
     }
 
-  if (info->file_version >= 12)
+  if (! xcf_data_is_zero (tile_data, tile_size))
     {
-      gint n_components = babl_format_get_n_components (format);
+      if (info->file_version >= 12)
+        {
+          gint n_components = babl_format_get_n_components (format);
 
-      xcf_read_from_be (bpp / n_components, tile_data,
-                        tile_size / bpp * n_components);
-    }
+          xcf_read_from_be (bpp / n_components, tile_data,
+                            tile_size / bpp * n_components);
+        }
 
-  gegl_buffer_set (buffer, tile_rect, 0, format, tile_data,
-                   GEGL_AUTO_ROWSTRIDE);
+      gegl_buffer_set (buffer, tile_rect, 0, format, tile_data,
+                       GEGL_AUTO_ROWSTRIDE);
+    }
 
   inflateEnd (&strm);
 
diff --git a/app/xcf/xcf-utils.c b/app/xcf/xcf-utils.c
new file mode 100644
index 0000000..6ef0b72
--- /dev/null
+++ b/app/xcf/xcf-utils.c
@@ -0,0 +1,53 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <cairo.h>
+#include <gegl.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
+
+#include "xcf-utils.h"
+
+
+gboolean
+xcf_data_is_zero (const void *data,
+                  gint        size)
+{
+  const guint8  *data8;
+  const guint64 *data64;
+
+  for (data8 = data; size > 0 && (guintptr) data8 % 8 != 0; data8++, size--)
+    {
+      if (*data8)
+        return FALSE;
+    }
+
+  for (data64 = (gpointer) data8; size >= 8; data64++, size -= 8)
+    {
+      if (*data64)
+        return FALSE;
+    }
+
+  for (data8 = (gpointer) data64; size > 0; data8++, size--)
+    {
+      if (*data8)
+        return FALSE;
+    }
+
+  return TRUE;
+}
diff --git a/app/xcf/xcf-utils.h b/app/xcf/xcf-utils.h
new file mode 100644
index 0000000..07b6856
--- /dev/null
+++ b/app/xcf/xcf-utils.h
@@ -0,0 +1,26 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __XCF_UTILS_H__
+#define __XCF_UTILS_H__
+
+
+gboolean   xcf_data_is_zero (const void *data,
+                             gint        size);
+
+
+#endif  /* __XCF_UTILS_H__ */


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