[gimp/gimp-2-10] (Backport) Issue #4: Enable loading transparent .EPS files



commit c51e5cbafa7ac073168c52360ca505d93eadc120
Author: Nikc <nikcdc gmail com>
Date:   Tue May 17 22:06:44 2022 +0000

    (Backport) Issue #4: Enable loading transparent .EPS files

 plug-ins/common/file-ps.c | 88 ++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 68 insertions(+), 20 deletions(-)
---
diff --git a/plug-ins/common/file-ps.c b/plug-ins/common/file-ps.c
index 6e4b82ebd6..ee432a8575 100644
--- a/plug-ins/common/file-ps.c
+++ b/plug-ins/common/file-ps.c
@@ -241,15 +241,17 @@ static gint      get_bbox         (const gchar       *filename,
                                    gint              *x1,
                                    gint              *y1);
 
-static FILE    * ps_open         (const gchar       *filename,
-                                  const PSLoadVals  *loadopt,
-                                  gint              *llx,
-                                  gint              *lly,
-                                  gint              *urx,
-                                  gint              *ury,
-                                  gboolean          *is_epsf);
-
-static void      ps_close         (FILE              *ifp);
+static FILE    * ps_open          (const gchar       *filename,
+                                   const PSLoadVals  *loadopt,
+                                   gint              *llx,
+                                   gint              *lly,
+                                   gint              *urx,
+                                   gint              *ury,
+                                   gboolean          *is_epsf,
+                                   gchar            **tmp_filename);
+
+static void      ps_close         (FILE              *ifp,
+                                   gchar             *tmp_filename);
 
 static gboolean  skip_ps          (FILE              *ifp);
 
@@ -1050,6 +1052,8 @@ load_image (const gchar  *filename,
   gint      llx, lly, urx, ury;
   gint      k, n_images, max_images, max_pagenum;
   gboolean  is_epsf;
+  GdkPixbuf    *pixbuf = NULL;
+  gchar        *tmp_filename = NULL;
 
 #ifdef PS_DEBUG
   g_print ("load_image:\n resolution = %d\n", plvals.resolution);
@@ -1074,7 +1078,7 @@ load_image (const gchar  *filename,
     }
   fclose (ifp);
 
-  ifp = ps_open (filename, &plvals, &llx, &lly, &urx, &ury, &is_epsf);
+  ifp = ps_open (filename, &plvals, &llx, &lly, &urx, &ury, &is_epsf, &tmp_filename);
   if (!ifp)
     {
       g_set_error (error, G_FILE_ERROR, G_FILE_ERROR,
@@ -1089,7 +1093,13 @@ load_image (const gchar  *filename,
 
   max_pagenum = 9999;  /* Try to get the maximum pagenumber to read */
   if (is_epsf)
-    max_pagenum = 1;
+    {
+      max_pagenum = 1;
+      /* Use pixbuf to load transparent EPS as PNGs */
+      pixbuf = gdk_pixbuf_new_from_file (tmp_filename, error);
+      if (! pixbuf)
+        return -1;
+    }
 
   if (!page_in_list (plvals.pages, max_pagenum)) /* Is there a limit in list ? */
     {
@@ -1141,7 +1151,38 @@ load_image (const gchar  *filename,
         }
     }
 
-  ps_close (ifp);
+  ps_close (ifp, tmp_filename);
+
+  /* EPS are now imported using pngalpha, so they can be converted
+   * to a layer with gimp_layer_new_from_pixbuf () and exported at
+   * this part of the loading process
+   */
+  if (is_epsf)
+    {
+      gint32 layer;
+
+      image_ID = gimp_image_new (urx, ury, GIMP_RGB);
+
+      gimp_image_undo_disable (image_ID);
+
+      gimp_image_set_filename (image_ID, filename);
+      gimp_image_set_resolution (image_ID,
+                                 plvals.resolution,
+                                 plvals.resolution);
+
+      layer = gimp_layer_new_from_pixbuf (image_ID, _("Rendered EPS"), pixbuf,
+                                          100,
+                                          gimp_image_get_default_new_layer_mode (image_ID),
+                                          0.0, 1.0);
+      gimp_image_insert_layer (image_ID, layer, -1, 0);
+
+      gimp_image_undo_enable (image_ID);
+
+      g_free (image_list);
+      g_object_unref (pixbuf);
+
+      return image_ID;
+    }
 
   if (ps_pagemode == GIMP_PAGE_SELECTOR_TARGET_LAYERS)
     {
@@ -1558,8 +1599,6 @@ get_bbox (const gchar *filename,
   return retval;
 }
 
-static gchar *pnmfile;
-
 /* Open the PostScript file. On failure, NULL is returned. */
 /* The filepointer returned will give a PNM-file generated */
 /* by the PostScript-interpreter. */
@@ -1570,7 +1609,8 @@ ps_open (const gchar      *filename,
          gint             *lly,
          gint             *urx,
          gint             *ury,
-         gboolean         *is_epsf)
+         gboolean         *is_epsf,
+         gchar           **tmp_filename)
 {
   const gchar  *driver;
   GPtrArray    *cmdA;
@@ -1685,7 +1725,15 @@ ps_open (const gchar      *filename,
    * using standard output as output file.
    * Thus, use a real output file.
    */
-  pnmfile = gimp_temp_name ("pnm");
+  if (*is_epsf)
+    {
+      driver = "pngalpha";
+      *tmp_filename = gimp_temp_name ("png");
+    }
+  else
+    {
+      *tmp_filename = gimp_temp_name ("pnm");
+    }
 
   /* Build command array */
   cmdA = g_ptr_array_new ();
@@ -1723,7 +1771,7 @@ ps_open (const gchar      *filename,
     g_ptr_array_add (cmdA, g_strdup ("-dSAFER"));
 
   /* Output file name */
-  g_ptr_array_add (cmdA, g_strdup_printf ("-sOutputFile=%s", pnmfile));
+  g_ptr_array_add (cmdA, g_strdup_printf ("-sOutputFile=%s", *tmp_filename));
 
   /* Offset command for gs to get image part with negative x/y-coord. */
   if ((offx != 0) || (offy != 0))
@@ -1774,7 +1822,7 @@ ps_open (const gchar      *filename,
   /* Don't care about exit status of ghostscript. */
   /* Just try to read what it wrote. */
 
-  fd_popen = g_fopen (pnmfile, "rb");
+  fd_popen = g_fopen (*tmp_filename, "rb");
 
   g_ptr_array_free (cmdA, FALSE);
   g_strfreev (pcmdA);
@@ -1785,11 +1833,11 @@ ps_open (const gchar      *filename,
 
 /* Close the PNM-File of the PostScript interpreter */
 static void
-ps_close (FILE *ifp)
+ps_close (FILE *ifp, gchar *tmp_filename)
 {
  /* If a real outputfile was used, close the file and remove it. */
   fclose (ifp);
-  g_unlink (pnmfile);
+  g_unlink (tmp_filename);
 }
 
 


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