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.
This commit is contained in:
Ell 2017-10-04 06:43:56 -04:00
parent 2a0c610e02
commit 0ae7acd594
4 changed files with 110 additions and 16 deletions

View File

@ -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

View File

@ -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,17 +2206,20 @@ 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;
bogus_rle:
@ -2297,17 +2307,20 @@ 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);
return TRUE;

53
app/xcf/xcf-utils.c Normal file
View File

@ -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;
}

26
app/xcf/xcf-utils.h Normal file
View File

@ -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__ */