From 70b59f339a683c0fdfe2e4b6c2d36c98e6d91339 Mon Sep 17 00:00:00 2001 From: Nils Philippsen <nils@redhat.com> Date: Thu, 5 Jul 2012 13:58:33 +0200 Subject: [PATCH] patch: postscript-crash Squashed commit of the following: commit f437eec885546d58c33c2a4427bd63e244e66e07 Author: Sven Neumann <sven@gimp.org> Date: Tue Aug 29 13:26:53 2006 +0000 Merged from HEAD branch: 2006-08-29 Sven Neumann <sven@gimp.org> Merged from HEAD branch: * plug-ins/common/postscript.c (dither_grey): code cleanup and fix for bug #353381 (crash when Preview option is selected). Conflicts: ChangeLog --- plug-ins/common/postscript.c | 106 ++++++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 45 deletions(-) diff --git a/plug-ins/common/postscript.c b/plug-ins/common/postscript.c index c9edce3..8cd7358 100644 --- a/plug-ins/common/postscript.c +++ b/plug-ins/common/postscript.c @@ -250,7 +250,7 @@ static void save_ps_setup (FILE *ofp, static void save_ps_trailer (FILE *ofp); static void save_ps_preview (FILE *ofp, gint32 drawable_ID); -static void dither_grey (guchar *grey, +static void dither_grey (const guchar *grey, guchar *bw, gint npix, gint linecount); @@ -497,7 +497,7 @@ ps_end_data (FILE *ofp) end_data = ftell (ofp); if (end_data > 0) { - sprintf (s, "%ld ASCII Bytes", end_data-ps_data_pos.begin_data); + sprintf (s, "%ld ASCII Bytes", end_data - ps_data_pos.begin_data); if (fseek (ofp, ps_data_pos.eol - strlen (s), SEEK_SET) == 0) { fprintf (ofp, "%s", s); @@ -2189,59 +2189,74 @@ save_ps_trailer (FILE *ofp) /* If linecount is less than zero, all used memory is freed. */ static void -dither_grey (guchar *grey, - guchar *bw, - gint npix, - gint linecount) +dither_grey (const guchar *grey, + guchar *bw, + gint npix, + gint linecount) { - register guchar *greyptr, *bwptr, mask; - register int *fse; - int x, greyval, fse_inline; - static int *fs_error = NULL; - static int do_init_arrays = 1; - static int limit_array[1278]; - static int east_error[256],seast_error[256],south_error[256],swest_error[256]; - int *limit = &(limit_array[512]); + static gboolean do_init_arrays = TRUE; + static gint *fs_error = NULL; + static gint limit[1278]; + static gint east_error[256]; + static gint seast_error[256]; + static gint south_error[256]; + static gint swest_error[256]; + + register const guchar *greyptr; + register guchar *bwptr, mask; + register gint *fse; + gint x, greyval, fse_inline; if (linecount <= 0) { - if (fs_error) g_free (fs_error-1); - if (linecount < 0) return; - fs_error = g_new (int, npix+2); - memset ((char *)fs_error, 0, (npix+2)*sizeof (int)); - fs_error++; + g_free (fs_error); + + if (linecount < 0) + return; + + fs_error = g_new0 (gint, npix + 2); /* Initialize some arrays that speed up dithering */ if (do_init_arrays) - { - do_init_arrays = 0; - for (x = -511; x <= 766; x++) - limit[x] = (x < 0) ? 0 : ((x > 255) ? 255 : x); - for (greyval = 0; greyval < 256; greyval++) - { - east_error[greyval] = (greyval < 128) ? ((greyval * 79) >> 8) - : (((greyval-255)*79) >> 8); - seast_error[greyval] = (greyval < 128) ? ((greyval * 34) >> 8) - : (((greyval-255)*34) >> 8); - south_error[greyval] = (greyval < 128) ? ((greyval * 56) >> 8) - : (((greyval-255)*56) >> 8); - swest_error[greyval] = (greyval < 128) ? ((greyval * 12) >> 8) - : (((greyval-255)*12) >> 8); - } - } + { + gint i; + + do_init_arrays = FALSE; + + for (i = 0, x = -511; x <= 766; i++, x++) + limit[i] = (x < 0) ? 0 : ((x > 255) ? 255 : x); + + for (greyval = 0; greyval < 256; greyval++) + { + east_error[greyval] = (greyval < 128) ? + ((greyval * 79) >> 8) : (((greyval - 255) * 79) >> 8); + seast_error[greyval] = (greyval < 128) ? + ((greyval * 34) >> 8) : (((greyval - 255) * 34) >> 8); + south_error[greyval] = (greyval < 128) ? + ((greyval * 56) >> 8) : (((greyval - 255) * 56) >> 8); + swest_error[greyval] = (greyval < 128) ? + ((greyval * 12) >> 8) : (((greyval - 255) * 12) >> 8); + } + } } - if (fs_error == NULL) return; - memset (bw, 0, (npix+7)/8); /* Initialize with white */ + g_return_if_fail (fs_error != NULL); + + memset (bw, 0, (npix + 7) / 8); /* Initialize with white */ greyptr = grey; bwptr = bw; mask = 0x80; - fse_inline = fs_error[0]; - for (x = 0, fse = fs_error; x < npix; x++, fse++) + + fse_inline = fs_error[1]; + + for (x = 0, fse = fs_error + 1; x < npix; x++, fse++) { - greyval = limit[*(greyptr++) + fse_inline]; /* 0 <= greyval <= 255 */ - if (greyval < 128) *bwptr |= mask; /* Set a black pixel */ + greyval = + limit[*(greyptr++) + fse_inline + 512]; /* 0 <= greyval <= 255 */ + + if (greyval < 128) + *bwptr |= mask; /* Set a black pixel */ /* Error distribution */ fse_inline = east_error[greyval] + fse[1]; @@ -2250,11 +2265,12 @@ dither_grey (guchar *grey, fse[-1] += swest_error[greyval]; mask >>= 1; /* Get mask for next b/w-pixel */ + if (!mask) - { - mask = 0x80; - bwptr++; - } + { + mask = 0x80; + bwptr++; + } } } -- 1.7.10.4