gimp r28104 - in trunk: . plug-ins/common



Author: neo
Date: Thu Mar  5 14:08:56 2009
New Revision: 28104
URL: http://svn.gnome.org/viewvc/gimp?rev=28104&view=rev

Log:
2009-03-05  Sven Neumann  <sven gimp org>

	Bug 573695 â 1-bit white background saved as PBM becomes all 
black

	* plug-ins/common/file-pnm.c: need to handle the special case 
that
	the image colormap has only one entry.



Modified:
   trunk/ChangeLog
   trunk/plug-ins/common/file-pnm.c

Modified: trunk/plug-ins/common/file-pnm.c
==============================================================================
--- trunk/plug-ins/common/file-pnm.c	(original)
+++ trunk/plug-ins/common/file-pnm.c	Thu Mar  5 14:08:56 2009
@@ -84,7 +84,7 @@
   gint       maxval;            /* For ascii image files, the max value
                                  * which we need to normalize to */
   gint       np;                /* Number of image planes (0 for pbm) */
-  gint       asciibody;         /* 1 if ascii body, 0 if raw body */
+  gboolean   asciibody;         /* 1 if ascii body, 0 if raw body */
   jmp_buf    jmpbuf;            /* Where to jump to on an error loading */
   /* Routine to use to load the pnm body */
   void    (* loader) (PNMScanner *, struct _PNMInfo *, GimpPixelRgn *);
@@ -93,13 +93,14 @@
 /* Contains the information needed to write out PNM rows */
 typedef struct _PNMRowInfo
 {
-  gint    fd;           /* File descriptor */
-  gchar  *rowbuf;       /* Buffer for writing out rows */
-  gint    xres;         /* X resolution */
-  gint    np;           /* Number of planes */
-  guchar *red;          /* Colormap red */
-  guchar *grn;          /* Colormap green */
-  guchar *blu;          /* Colormap blue */
+  gint      fd;           /* File descriptor */
+  gchar    *rowbuf;       /* Buffer for writing out rows */
+  gint      xres;         /* X resolution */
+  gint      np;           /* Number of planes */
+  guchar   *red;          /* Colormap red */
+  guchar   *grn;          /* Colormap green */
+  guchar   *blu;          /* Colormap blue */
+  gboolean  solid_white;  /* image is all white (pbm only) */
 } PNMRowInfo;
 
 /* Save info  */
@@ -807,20 +808,26 @@
 pnmsaverow_raw_pbm (PNMRowInfo   *ri,
                     const guchar *data)
 {
-  gint    b, i, p = 0;
-  gchar  *rbcur   = ri->rowbuf;
-  gint32  len     = (gint) ceil ((gdouble) (ri->xres) / 8.0);
+  gint    b, p  = 0;
+  gchar  *rbcur = ri->rowbuf;
+  gint32  len   = (gint) ceil ((gdouble) (ri->xres) / 8.0);
 
   for (b = 0; b < len; b++) /* each output byte */
     {
-      *(rbcur+b) = 0;
+      gint i;
+
+      rbcur[b] = 0;
+
+      if (ri->solid_white)
+        continue;
+
       for (i = 0; i < 8; i++) /* each bit in this byte */
         {
           if (p >= ri->xres)
-              break;
+            break;
 
-          if (*(data+p) == 0)
-              *(rbcur+b) |= (char) (1 << (7 - i));
+          if (data[p] == 0)
+            rbcur[b] |= (char) (1 << (7 - i));
 
           p++;
         }
@@ -843,16 +850,16 @@
     {
       if (line_len > 69)
         {
-          *(rbcur+i) = '\n';
+          rbcur[i] = '\n';
           line_len = 0;
           len++;
           rbcur++;
         }
 
-      if (*(data+i) == 0)
-        *(rbcur+i) = '1';
+      if (ri->solid_white || data[i] != 0)
+        rbcur[i] = '0';
       else
-        *(rbcur+i) = '0';
+        rbcur[i] = '1';
 
       line_len++;
       len++;
@@ -916,11 +923,11 @@
 
   for (i = 0; i < ri->xres; i++)
     {
-      sprintf ((char *) rbcur,"%d\n", 0xff & ri->red[*(data)]);
+      sprintf (rbcur, "%d\n", 0xff & ri->red[*(data)]);
       rbcur += strlen (rbcur);
-      sprintf ((char *) rbcur,"%d\n", 0xff & ri->grn[*(data)]);
+      sprintf (rbcur, "%d\n", 0xff & ri->grn[*(data)]);
       rbcur += strlen (rbcur);
-      sprintf ((char *) rbcur,"%d\n", 0xff & ri->blu[*(data++)]);
+      sprintf (rbcur, "%d\n", 0xff & ri->blu[*(data++)]);
       rbcur += strlen (rbcur);
     }
 
@@ -1062,24 +1069,43 @@
         }
     }
 
-  if (drawable_type == GIMP_INDEXED_IMAGE && !pbm)
+  rowinfo.solid_white = FALSE;
+
+  if (drawable_type == GIMP_INDEXED_IMAGE)
     {
       guchar *cmap;
       gint    colors;
-      gint    i;
 
       cmap = gimp_image_get_colormap (image_ID, &colors);
 
-      for (i = 0; i < colors; i++)
+      if (pbm)
         {
-          red[i] = *cmap++;
-          grn[i] = *cmap++;
-          blu[i] = *cmap++;
+          /*  If we are dealing with an all-white image, then the colormap
+           *  has one entry only and we can't assume that the first entry
+           *  is black and the second is white.
+           */
+          if (colors == 1 && cmap[0])
+            {
+              rowinfo.solid_white = TRUE;
+            }
+        }
+      else
+        {
+          gint i;
+
+          for (i = 0; i < colors; i++)
+            {
+              red[i] = *cmap++;
+              grn[i] = *cmap++;
+              blu[i] = *cmap++;
+            }
+
+          rowinfo.red = red;
+          rowinfo.grn = grn;
+          rowinfo.blu = blu;
         }
 
-      rowinfo.red = red;
-      rowinfo.grn = grn;
-      rowinfo.blu = blu;
+      g_free (cmap);
     }
 
   /* allocate a buffer for retrieving information from the pixel region  */



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