[gimp] plug-ins: move around and clean up some code in file-webp



commit 5cccad9867b48f1121b450db2cc6404e7b8be604
Author: Michael Natterer <mitch gimp org>
Date:   Fri Sep 27 12:07:20 2019 +0200

    plug-ins: move around and clean up some code in file-webp
    
    as preparation for GimpProcedureConfig porting.

 plug-ins/file-webp/file-webp-dialog.c |  12 +--
 plug-ins/file-webp/file-webp-save.c   | 150 ++++++++--------------------------
 plug-ins/file-webp/file-webp-save.h   |  26 +++---
 plug-ins/file-webp/file-webp.c        |  96 ++++++++++++++++------
 4 files changed, 125 insertions(+), 159 deletions(-)
---
diff --git a/plug-ins/file-webp/file-webp-dialog.c b/plug-ins/file-webp/file-webp-dialog.c
index cba0ca0def..a34185ff0f 100644
--- a/plug-ins/file-webp/file-webp-dialog.c
+++ b/plug-ins/file-webp/file-webp-dialog.c
@@ -363,33 +363,33 @@ save_dialog (WebPSaveParams *params,
 
   /* Save EXIF data */
   toggle = gtk_check_button_new_with_mnemonic (_("_Save Exif data"));
-  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), params->exif);
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), params->save_exif);
   gtk_box_pack_start (GTK_BOX (vbox), toggle, FALSE, FALSE, 0);
   gtk_widget_show (toggle);
 
   g_signal_connect (toggle, "toggled",
                     G_CALLBACK (gimp_toggle_button_update),
-                    &params->exif);
+                    &params->save_exif);
 
   /* XMP metadata */
   toggle = gtk_check_button_new_with_mnemonic (_("Save _XMP data"));
-  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), params->xmp);
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), params->save_xmp);
   gtk_box_pack_start (GTK_BOX (vbox), toggle, FALSE, FALSE, 0);
   gtk_widget_show (toggle);
 
   g_signal_connect (toggle, "toggled",
                     G_CALLBACK (gimp_toggle_button_update),
-                    &params->xmp);
+                    &params->save_xmp);
 
   /* Color profile */
   toggle = gtk_check_button_new_with_mnemonic (_("Save color _profile"));
-  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), params->profile);
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), params->save_profile);
   gtk_box_pack_start (GTK_BOX (vbox), toggle, FALSE, FALSE, 0);
   gtk_widget_show (toggle);
 
   g_signal_connect (toggle, "toggled",
                     G_CALLBACK (gimp_toggle_button_update),
-                    &params->profile);
+                    &params->save_profile);
 
   gtk_widget_show (dialog);
 
diff --git a/plug-ins/file-webp/file-webp-save.c b/plug-ins/file-webp/file-webp-save.c
index 744b37c9e8..c7172efabe 100644
--- a/plug-ins/file-webp/file-webp-save.c
+++ b/plug-ins/file-webp/file-webp-save.c
@@ -52,21 +52,6 @@ int           webp_file_progress    (int                percent,
                                      const WebPPicture *picture);
 const gchar * webp_error_string     (WebPEncodingError  error_code);
 
-gboolean      save_layer            (GFile             *file,
-                                     gint32             nLayers,
-                                     GimpImage         *image,
-                                     GimpDrawable      *drawable,
-                                     WebPSaveParams    *params,
-                                     GError           **error);
-
-gboolean      save_animation        (GFile             *file,
-                                     gint32             nLayers,
-                                     GList             *layers,
-                                     GimpImage         *image,
-                                     GimpDrawable      *drawable,
-                                     WebPSaveParams    *params,
-                                     GError           **error);
-
 static void   webp_decide_output    (GimpImage         *image,
                                      WebPSaveParams    *params,
                                      GimpColorProfile **profile,
@@ -141,17 +126,16 @@ webp_error_string (WebPEncodingError error_code)
 
 gboolean
 save_layer (GFile          *file,
-            gint32          nLayers,
             GimpImage      *image,
             GimpDrawable   *drawable,
             WebPSaveParams *params,
             GError        **error)
 {
-  gboolean          status   = FALSE;
-  FILE             *outfile  = NULL;
-  WebPConfig        config   = {0};
-  WebPPicture       picture  = {0};
-  guchar           *buffer   = NULL;
+  gboolean          status      = FALSE;
+  FILE             *outfile     = NULL;
+  WebPConfig        webp_config = { 0, };
+  WebPPicture       picture     = { 0, };
+  guchar           *buffer      = NULL;
   gint              w, h;
   gboolean          has_alpha;
   const gchar      *encoding;
@@ -245,11 +229,11 @@ save_layer (GFile          *file,
 
       /* Initialize the WebP configuration with a preset and fill in the
        * remaining values */
-      WebPConfigPreset (&config, params->preset, params->quality);
+      WebPConfigPreset (&webp_config, params->preset, params->quality);
 
-      config.lossless      = params->lossless;
-      config.method        = 6;  /* better quality */
-      config.alpha_quality = params->alpha_quality;
+      webp_config.lossless      = params->lossless;
+      webp_config.method        = 6;  /* better quality */
+      webp_config.alpha_quality = params->alpha_quality;
 
       /* Prepare the WebP structure */
       WebPPictureInit (&picture);
@@ -288,7 +272,7 @@ save_layer (GFile          *file,
         }
 
       /* Perform the actual encode */
-      if (! WebPEncode (&config, &picture))
+      if (! WebPEncode (&webp_config, &picture))
         {
           g_printerr ("WebP error: '%s'",
                       webp_error_string (picture.error_code));
@@ -451,7 +435,7 @@ parse_combine (const char* str)
   return FALSE;
 }
 
-static gint
+static gboolean
 get_layer_needs_combine (GimpLayer *layer)
 {
   gchar     *layer_name;
@@ -464,7 +448,7 @@ get_layer_needs_combine (GimpLayer *layer)
   return needs_combine;
 }
 
-static GeglBuffer*
+static GeglBuffer *
 combine_buffers (GeglBuffer *layer_buffer,
                  GeglBuffer *prev_frame_buffer)
 {
@@ -506,13 +490,13 @@ combine_buffers (GeglBuffer *layer_buffer,
 
 gboolean
 save_animation (GFile          *file,
-                gint32          nLayers,
-                GList          *layers,
                 GimpImage      *image,
                 GimpDrawable   *drawable,
                 WebPSaveParams *params,
                 GError        **error)
 {
+  GList                 *layers;
+  gint32                 n_layers;
   gboolean               status   = TRUE;
   FILE                  *outfile  = NULL;
   guchar                *buffer   = NULL;
@@ -527,13 +511,18 @@ save_animation (GFile          *file,
   WebPAnimEncoderOptions enc_options;
   WebPData               webp_data;
   int                    frame_timestamp = 0;
-  WebPAnimEncoder       *enc = NULL;
-  GeglBuffer            *prev_frame = NULL;
-  gboolean               out_linear = FALSE;
+  WebPAnimEncoder       *enc             = NULL;
+  GeglBuffer            *prev_frame      = NULL;
+  gboolean               out_linear      = FALSE;
 
-  if (nLayers < 1)
+  layers = gimp_image_list_layers (image);
+
+  if (! layers)
     return FALSE;
 
+  layers   = g_list_reverse (layers);
+  n_layers = g_list_length (layers);
+
   webp_decide_output (image, params, &profile, &out_linear);
   if (profile)
     {
@@ -550,6 +539,7 @@ save_animation (GFile          *file,
         }
 
     }
+
   if (! space)
     space = gimp_drawable_get_format (drawable);
 
@@ -612,7 +602,7 @@ save_animation (GFile          *file,
           GeglBuffer       *geglbuffer;
           GeglBuffer       *current_frame;
           GeglRectangle     extent;
-          WebPConfig        config;
+          WebPConfig        webp_config;
           WebPPicture       picture;
           WebPMemoryWriter  mw       = { 0 };
           GimpDrawable     *drawable = list->data;
@@ -680,12 +670,12 @@ save_animation (GFile          *file,
                 }
             }
 
-          WebPConfigPreset (&config, params->preset, params->quality);
+          WebPConfigPreset (&webp_config, params->preset, params->quality);
 
-          config.lossless      = params->lossless;
-          config.method        = 6;  /* better quality */
-          config.alpha_quality = params->alpha_quality;
-          config.exact         = 1;
+          webp_config.lossless      = params->lossless;
+          webp_config.method        = 6;  /* better quality */
+          webp_config.alpha_quality = params->alpha_quality;
+          webp_config.exact         = 1;
 
           WebPMemoryWriterInit (&mw);
 
@@ -733,7 +723,8 @@ save_animation (GFile          *file,
                           G_STRFUNC);
             }
           /* Perform the actual encode */
-          else if (! WebPAnimEncoderAdd (enc, &picture, frame_timestamp, &config))
+          else if (! WebPAnimEncoderAdd (enc, &picture, frame_timestamp,
+                                         &webp_config))
             {
               g_printerr ("ERROR[%d]: %s\n",
                           picture.error_code,
@@ -747,7 +738,7 @@ save_animation (GFile          *file,
           if (status == FALSE)
             break;
 
-          gimp_progress_update ((loop + 1.0) / nLayers);
+          gimp_progress_update ((loop + 1.0) / n_layers);
           frame_timestamp += (delay <= 0 || force_delay) ? default_delay : delay;
         }
       g_free (buffer);
@@ -813,80 +804,8 @@ save_animation (GFile          *file,
   if (outfile)
     fclose (outfile);
 
-  return status;
-}
-
-
-gboolean
-save_image (GFile                  *file,
-            GimpImage              *image,
-            GimpDrawable           *drawable,
-            GimpMetadata           *metadata,
-            GimpMetadataSaveFlags   metadata_flags,
-            WebPSaveParams         *params,
-            GError                **error)
-{
-  gboolean  status = FALSE;
-  GList    *layers;
-
-  layers = gimp_image_list_layers (image);
-  layers = g_list_reverse (layers);
-
-  if (! layers)
-    return FALSE;
-
-  g_printerr ("Saving WebP file %s\n", gimp_file_get_utf8_name (file));
-
-  if (params->animation)
-    {
-      status = save_animation (file,
-                               g_list_length (layers), layers,
-                               image, drawable, params,
-                               error);
-    }
-  else
-    {
-      status = save_layer (file,
-                           g_list_length (layers),
-                           image, drawable, params, error);
-    }
-
   g_list_free (layers);
 
-  if (metadata)
-    {
-      gimp_metadata_set_bits_per_sample (metadata, 8);
-
-      if (params->exif)
-        metadata_flags |= GIMP_METADATA_SAVE_EXIF;
-      else
-        metadata_flags &= ~GIMP_METADATA_SAVE_EXIF;
-
-      /* WebP doesn't support iptc natively and
-         sets it via xmp */
-      if (params->xmp)
-        {
-          metadata_flags |= GIMP_METADATA_SAVE_XMP;
-          metadata_flags |= GIMP_METADATA_SAVE_IPTC;
-        }
-      else
-        {
-          metadata_flags &= ~GIMP_METADATA_SAVE_XMP;
-          metadata_flags &= ~GIMP_METADATA_SAVE_IPTC;
-        }
-
-      if (params->profile)
-        metadata_flags |= GIMP_METADATA_SAVE_COLOR_PROFILE;
-      else
-        metadata_flags &= ~GIMP_METADATA_SAVE_COLOR_PROFILE;
-
-      gimp_image_metadata_save_finish (image,
-                                       "image/webp",
-                                       metadata, metadata_flags,
-                                       file, NULL);
-    }
-
-  /* Return the status */
   return status;
 }
 
@@ -899,7 +818,8 @@ webp_decide_output (GimpImage         *image,
   g_return_if_fail (profile && *profile == NULL);
 
   *out_linear = FALSE;
-  if (params->profile)
+
+  if (params->save_profile)
     {
       *profile = gimp_image_get_color_profile (image);
 
diff --git a/plug-ins/file-webp/file-webp-save.h b/plug-ins/file-webp/file-webp-save.h
index f61f86613e..2324ad75b0 100644
--- a/plug-ins/file-webp/file-webp-save.h
+++ b/plug-ins/file-webp/file-webp-save.h
@@ -33,22 +33,26 @@ typedef struct
   gint       kf_distance;
   gfloat     quality;
   gfloat     alpha_quality;
-  gboolean   exif;
-  gboolean   iptc;
-  gboolean   xmp;
-  gboolean   profile;
+  gboolean   save_exif;
+  gboolean   save_iptc;
+  gboolean   save_xmp;
+  gboolean   save_profile;
   gint       delay;
   gboolean   force_delay;
 } WebPSaveParams;
 
 
-gboolean   save_image (GFile                  *file,
-                       GimpImage              *image,
-                       GimpDrawable           *drawable,
-                       GimpMetadata           *metadata,
-                       GimpMetadataSaveFlags   metadata_flags,
-                       WebPSaveParams         *params,
-                       GError                **error);
+gboolean   save_layer     (GFile           *file,
+                           GimpImage       *image,
+                           GimpDrawable    *drawable,
+                           WebPSaveParams  *params,
+                           GError         **error);
+
+gboolean   save_animation (GFile           *file,
+                           GimpImage       *image,
+                           GimpDrawable    *drawable,
+                           WebPSaveParams  *params,
+                           GError         **error);
 
 
 #endif /* __WEBP_SAVE_H__ */
diff --git a/plug-ins/file-webp/file-webp.c b/plug-ins/file-webp/file-webp.c
index e358e4bbb5..4342ee8e0c 100644
--- a/plug-ins/file-webp/file-webp.c
+++ b/plug-ins/file-webp/file-webp.c
@@ -208,22 +208,22 @@ webp_create_procedure (GimpPlugIn  *plug_in,
                          0, G_MAXINT, 50,
                          G_PARAM_READWRITE);
 
-      GIMP_PROC_ARG_BOOLEAN (procedure, "exif",
-                             "EXIF",
-                             "Toggle saving exif data",
-                             FALSE,
+      GIMP_PROC_ARG_BOOLEAN (procedure, "save-exif",
+                             "Save Exif",
+                             "Toggle saving Exif data",
+                             gimp_export_exif (),
                              G_PARAM_READWRITE);
 
-      GIMP_PROC_ARG_BOOLEAN (procedure, "iptc",
-                             "IPTC",
-                             "Toggle saving iptc data",
-                             FALSE,
+      GIMP_PROC_ARG_BOOLEAN (procedure, "save-iptc",
+                             "Save IPTC",
+                             "Toggle saving IPTC data",
+                             gimp_export_iptc (),
                              G_PARAM_READWRITE);
 
-      GIMP_PROC_ARG_BOOLEAN (procedure, "xmp",
-                             "XMP",
-                             "Toggle saving xmp data",
-                             FALSE,
+      GIMP_PROC_ARG_BOOLEAN (procedure, "save-xmp",
+                             "Save XMP",
+                             "Toggle saving XMP data",
+                             gimp_export_xmp (),
                              G_PARAM_READWRITE);
 
       GIMP_PROC_ARG_INT (procedure, "delay",
@@ -304,9 +304,9 @@ webp_save (GimpProcedure        *procedure,
   params.kf_distance   = 50;
   params.quality       = 90.0f;
   params.alpha_quality = 100.0f;
-  params.exif          = FALSE;
-  params.iptc          = FALSE;
-  params.xmp           = FALSE;
+  params.save_exif     = FALSE;
+  params.save_iptc     = FALSE;
+  params.save_xmp      = FALSE;
   params.delay         = 200;
   params.force_delay   = FALSE;
 
@@ -314,10 +314,10 @@ webp_save (GimpProcedure        *procedure,
   metadata = gimp_image_metadata_save_prepare (image,
                                                "image/webp",
                                                &metadata_flags);
-  params.exif    = (metadata_flags & GIMP_METADATA_SAVE_EXIF) != 0;
-  params.xmp     = (metadata_flags & GIMP_METADATA_SAVE_XMP) != 0;
-  params.iptc    = (metadata_flags & GIMP_METADATA_SAVE_IPTC) != 0;
-  params.profile = (metadata_flags & GIMP_METADATA_SAVE_COLOR_PROFILE) != 0;
+  params.save_exif    = (metadata_flags & GIMP_METADATA_SAVE_EXIF) != 0;
+  params.save_xmp     = (metadata_flags & GIMP_METADATA_SAVE_XMP) != 0;
+  params.save_iptc    = (metadata_flags & GIMP_METADATA_SAVE_IPTC) != 0;
+  params.save_profile = (metadata_flags & GIMP_METADATA_SAVE_COLOR_PROFILE) != 0;
 
   switch (run_mode)
     {
@@ -345,9 +345,9 @@ webp_save (GimpProcedure        *procedure,
       params.loop          = GIMP_VALUES_GET_BOOLEAN (args, 5);
       params.minimize_size = GIMP_VALUES_GET_BOOLEAN (args, 6);
       params.kf_distance   = GIMP_VALUES_GET_INT     (args, 7);
-      params.exif          = GIMP_VALUES_GET_BOOLEAN (args, 8);
-      params.iptc          = GIMP_VALUES_GET_BOOLEAN (args, 9);
-      params.xmp           = GIMP_VALUES_GET_BOOLEAN (args, 10);
+      params.save_exif     = GIMP_VALUES_GET_BOOLEAN (args, 8);
+      params.save_iptc     = GIMP_VALUES_GET_BOOLEAN (args, 9);
+      params.save_xmp      = GIMP_VALUES_GET_BOOLEAN (args, 10);
       params.delay         = GIMP_VALUES_GET_INT     (args, 11);
       params.force_delay   = GIMP_VALUES_GET_BOOLEAN (args, 12);
       break;
@@ -376,12 +376,54 @@ webp_save (GimpProcedure        *procedure,
                                                  NULL);
     }
 
-  if (! save_image (file, image, drawable,
-                    metadata, metadata_flags,
-                    &params,
-                    &error))
+  if (params.animation)
+    {
+      if (! save_animation (file, image, drawable, &params,
+                            &error))
+        {
+          status = GIMP_PDB_EXECUTION_ERROR;
+        }
+    }
+  else
+    {
+      if (! save_layer (file, image, drawable, &params,
+                        &error))
+        {
+          status = GIMP_PDB_EXECUTION_ERROR;
+        }
+    }
+
+  if (status == GIMP_PDB_SUCCESS && metadata)
     {
-      status = GIMP_PDB_EXECUTION_ERROR;
+      gimp_metadata_set_bits_per_sample (metadata, 8);
+
+      if (params.save_exif)
+        metadata_flags |= GIMP_METADATA_SAVE_EXIF;
+      else
+        metadata_flags &= ~GIMP_METADATA_SAVE_EXIF;
+
+      /* WebP doesn't support iptc natively and sets it via xmp
+       */
+      if (params.save_xmp)
+        {
+          metadata_flags |= GIMP_METADATA_SAVE_XMP;
+          metadata_flags |= GIMP_METADATA_SAVE_IPTC;
+        }
+      else
+        {
+          metadata_flags &= ~GIMP_METADATA_SAVE_XMP;
+          metadata_flags &= ~GIMP_METADATA_SAVE_IPTC;
+        }
+
+      if (params.save_profile)
+        metadata_flags |= GIMP_METADATA_SAVE_COLOR_PROFILE;
+      else
+        metadata_flags &= ~GIMP_METADATA_SAVE_COLOR_PROFILE;
+
+      gimp_image_metadata_save_finish (image,
+                                       "image/webp",
+                                       metadata, metadata_flags,
+                                       file, NULL);
     }
 
   if (export == GIMP_EXPORT_EXPORT)


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