[gimp-perl] Make Gimp::Fu handle <Load> and <Save>.



commit 694b63cf08ab45e2a8d6cf6b8671a9b0e4ddf7fc
Author: Ed J <edj src gnome org>
Date:   Thu May 15 00:17:59 2014 +0100

    Make Gimp::Fu handle <Load> and <Save>.

 Gimp/Fu.pm         |   47 +++++++++++++++++++++++++++++++++-----------
 TODO               |    2 -
 examples/colorhtml |   24 +++++++---------------
 examples/dataurl   |   54 ++++++++++++++++++++++++++-------------------------
 4 files changed, 71 insertions(+), 56 deletions(-)
---
diff --git a/Gimp/Fu.pm b/Gimp/Fu.pm
index 9cff2c4..a0dc00d 100644
--- a/Gimp/Fu.pm
+++ b/Gimp/Fu.pm
@@ -298,9 +298,17 @@ EOF
             if ! {$p[9]} or $p[9]->[0]->[0] != PF_IMAGE;
       }
    } elsif ($p[6] =~ /^<Load>\//) {
+      my ($start, $label, $fileext, $prefix) = split '/', $p[6];
+      $prefix = '' unless $prefix;
+      Gimp::on_query { Gimp->register_load_handler($p[0], $fileext, $prefix); };
+      $p[6] = join '/', $start, $label;
       unshift @{$p[8]}, @load_params;
       unshift @{$p[9]}, $image_retval;
-   } elsif ($p[6] =~ /^<Save>\//) {
+   } elsif ($p[6] =~ /^<Save>\/(.*)/) {
+      my ($start, $label, $fileext, $prefix) = split '/', $p[6];
+      $prefix = '' unless $prefix;
+      Gimp::on_query { Gimp->register_save_handler($p[0], $fileext, $prefix); };
+      $p[6] = join '/', $start, $label;
       unshift @{$p[8]}, @save_params;
    } elsif ($p[6] =~ m#^<Toolbox>/Xtns/#) {
       undef $p[7];
@@ -686,7 +694,12 @@ value if the first return value is not a C<PF_IMAGE>.
 In any case, the plugin will be installed in the specified menu location;
 almost always under C<File/Create> or C<Filters>.
 
-=item <Load>/FILETYPE
+=item <Load>/Text describing input/file-extensions[/prefixes]
+
+The file-extensions are comma-separated. The prefixes are optional.
+
+Gimp::Fu will automatically register the plugin as a load-handler using
+C<Gimp-E<gt>register_load_handler>.
 
 L<Gimp::Fu> will B<supply parameters>:
 
@@ -698,15 +711,17 @@ L<Gimp::Fu> will B<supply parameters>:
 
 =back
 
-as the first parameters to the plugin.
+as the first parameters to the plugin. It will also automatically add
+a return-value which is a C<PF_IMAGE>, unless there is already such a
+value as first return value.
 
-If the script is an export-handler. Make sure you also have something like:
+=item <Save>/Text describing output/file-extensions[/prefixes]
 
- Gimp::on_query {
-   Gimp->register_save_handler("file_filetype_save", "filetype", "");
- };
+The file-extensions are comma-separated. The prefixes are optional.
 
-=item <Save>/FILETYPE
+Gimp::Fu will automatically register the plugin as a save-handler using
+C<Gimp-E<gt>register_save_handler>. This is not (in GIMP 2.8 terms)
+a save-handler anymore, but an export-handler.
 
 L<Gimp::Fu> will B<supply parameters>:
 
@@ -724,11 +739,19 @@ L<Gimp::Fu> will B<supply parameters>:
 
 as the first parameters to the plugin.
 
-If the script is an export-handler. Make sure you also have something like:
+Outline:
 
- Gimp::on_query {
-   Gimp->register_save_handler("file_filetype_save", "filetype", "");
- };
+  podregister {
+    my $export = Gimp::UI::export_image(
+      my $new_image=$image,
+      my $new_drawable=$drawable,
+      "COLORHTML",
+      EXPORT_CAN_HANDLE_RGB
+    );
+    return if $export == EXPORT_CANCEL;
+    # ...
+    $new_image->delete if $export == EXPORT_EXPORT;
+  };
 
 =item <Toolbox>/Xtns/Label
 
diff --git a/TODO b/TODO
index 21999e8..e0b83cf 100644
--- a/TODO
+++ b/TODO
@@ -5,8 +5,6 @@ Items as of 2014-04-29 (by Ed J)
   is ./app/plug-in/gimppluginmanager.c:gimp_plug_in_manager_exit
   - issue with removing menus, probably stored in _plug_in data
   PDB call added in tools/pdbgen/pdb/gimp.pdb
-* <Load> and <Save> need any registration as such done in Gimp::Fu - see pod and e/dataurl
-* Test Gimp::Fu menupath <Load>/<Save>/<Image>/<Toolbox>/<None>
 * jpegoptim and pngcrush save-handlers
 * e/fade-alpha should generate buttons, not hardcode
 * Gimp/Lib.xs is huge, and not very XS-y - lots of it is manually
diff --git a/examples/colorhtml b/examples/colorhtml
index 36c569a..0f8ae8d 100755
--- a/examples/colorhtml
+++ b/examples/colorhtml
@@ -11,14 +11,13 @@ my %replace = (
 );
 
 podregister {
-  my($new_image,$new_drawable);
   my $export = Gimp::UI::export_image(
-    $new_image=$image,
-    $new_drawable=$drawable,
+    my $new_image=$image,
+    my $new_drawable=$drawable,
     "COLORHTML",
     EXPORT_CAN_HANDLE_RGB
   );
-  die __"Export cancelled" if $export == EXPORT_CANCEL;
+  return if $export == EXPORT_CANCEL;
   my ($w,$h) = ($new_drawable->width, $new_drawable->height);
   Gimp->tile_cache_ntiles($w / Gimp->tile_width + 1);
   my $io = io($filename) or die __"write '$filename': $!\n";
@@ -101,7 +100,6 @@ HEADER
          "<font color=".unpack("H*",$1).">".shift(@chars).$closetag;
        }ges;
     }
-
     $io->print($pel,"\n");
     Gimp::Progress->update($y/$h);
   }
@@ -110,9 +108,6 @@ HEADER
   ();
 };
 
-Gimp::on_query {
-   Gimp->register_save_handler("file_colorhtml_save", "colorhtml", "");
-};
 exit main;
 __END__
 
@@ -122,7 +117,7 @@ file_colorhtml_save - Saves the image as coloured html text
 
 =head1 SYNOPSIS
 
-<Save>/COLORHTML
+<Save>/HTML with text coloured to match image/html
 
 =head1 DESCRIPTION
 
@@ -149,13 +144,10 @@ and will disturb the current font colour.
 
 =head1 PARAMETERS
 
-  [PF_RADIO,   "character_source", "Where to take characters from", 0,
-               [sourcecode => 0, textfile => 1, textblock => 2]],
-  [
-    PF_FILE,   "characters",
-    "Filename to read or characters to use", ""
-  ],
-  [PF_STRING, "font_size", "HTML font size (1..7 or -7 .. +7)", 2, ],
+  [PF_RADIO, "character_source", "Where to take characters from", 0,
+    [sourcecode => 0, textfile => 1, textblock => 2]],
+  [PF_FILE, "characters", "Filename to read or characters to use", ""],
+  [PF_STRING, "font_size", "HTML font size (1..7 or -7 .. +7)", 2,],
   [PF_BOOL, "use_css", "Use CSS", 1],
   [PF_BOOL, "compatible", "HTML-4.0 compliance", 1],
   [PF_BOOL, "closetag", "Add closing tag", 1],
diff --git a/examples/dataurl b/examples/dataurl
index 1e26852..4809ff6 100755
--- a/examples/dataurl
+++ b/examples/dataurl
@@ -5,46 +5,54 @@ use Gimp::Fu;
 use Gimp::UI;
 use MIME::Base64;
 use IO::All;
+use List::Util qw(max);
+
+my %filetype2info = (
+  0 => [
+    EXPORT_CAN_HANDLE_INDEXED|EXPORT_CAN_HANDLE_ALPHA,
+    "gif",
+    'file_gif_save', [ 0, 0, 0, 0 ],
+  ],
+  1 => [
+    EXPORT_CAN_HANDLE_RGB|EXPORT_CAN_HANDLE_GRAY,
+    "jpeg",
+    'file_jpeg_save', [ 0.7, 0, 1, 0, "", 0, 1, 0, 0 ],
+  ],
+  2 => [
+    EXPORT_CAN_HANDLE_RGB|EXPORT_CAN_HANDLE_GRAY|EXPORT_CAN_HANDLE_INDEXED,
+    "png",
+    'file_png_save', [ 0, 7, 0, 0, 0, 0, 0 ],
+  ],
+);
 
 podregister {
-  my($new_image,$new_drawable);
-  my $max;
+  my $max = 0;
+  my ($flags, $media, $method, $args) = @{$filetype2info{$filetype}};
   my $export = Gimp::UI::export_image(
-    $new_image=$image, $new_drawable=$drawable,
-    "DATAURL",
-    $filetype==0 ? EXPORT_CAN_HANDLE_INDEXED|EXPORT_CAN_HANDLE_ALPHA
-    : $filetype==1 ? EXPORT_CAN_HANDLE_RGB|EXPORT_CAN_HANDLE_GRAY
-    : $filetype==2 ? EXPORT_CAN_HANDLE_RGB|EXPORT_CAN_HANDLE_GRAY|EXPORT_CAN_HANDLE_INDEXED
-    : 0
+    my $new_image=$image, my $new_drawable=$drawable, "DATAURL", $flags,
   );
-  die __"export failed" if $export == EXPORT_CANCEL;
+  return if $export == EXPORT_CANCEL;
   my ($w,$h) = ($new_drawable->width, $new_drawable->height);
   my $tmp = Gimp->temp_name(".img~");
   my $fh = io($filename);
   die __"Unable to open '$filename' for writing: $!\n"
     unless $fh->print("<html><body>\n");
   Gimp::Progress->init(__"Saving '$filename' as DATAURL...");
-  my $media = $filetype==0 ? "gif"
-           : $filetype==1 ? "jpeg"
-           : $filetype==2 ? "png"
-           :            "";
   append $fh "<table width=$w cellspacing=0 cellpadding=0 border=0>";
   for (my $y=0; $y<$h; $y+=$tile_y) {
     my $wy = $h-$y < $tile_y ? $h-$y : $tile_y;
     append $fh "<tr>";
-    for(my $x=0; $x<$w; $x+=$tile_x) {
+    for (my $x=0; $x<$w; $x+=$tile_x) {
       my $wx = $w-$x < $tile_x ? $w-$x : $tile_x;
       my $image = $new_image->duplicate;
       $image->crop($wx,$wy,$x,$y);
-      ($image->get_layers)[0]->file_gif_save  (($tmp)x2, 0, 0, 0, 0)                   if $filetype==0;
-      ($image->get_layers)[0]->file_jpeg_save (($tmp)x2, 0.7, 0, 1, 0, "", 0, 1, 0, 0) if $filetype==1;
-      ($image->get_layers)[0]->file_png_save  (($tmp)x2, 0, 7, 0, 0, 0, 0, 0)          if $filetype==2;
+      ($image->get_layers)[0]->$method(($tmp)x2, @$args);
       $image->delete;
       my $data = io($tmp)->all
        or die __"Unable to read temporary image tile $tmp: $!";
       unlink $tmp;
       $url = "data:image/$media;base64,".(encode_base64 $data);
-      $max = length($url) if length($url) > $max;
+      $max = max($max, length($url));
       append $fh "<td><img src=\"", $url, "\">";
       Gimp::Progress->update(($y*$w+$x*$tile_y)/($w*$h));
     }
@@ -57,10 +65,6 @@ podregister {
   ();
 };
 
-Gimp::on_query {
-   Gimp->register_save_handler("file_dataurl_save", "dataurl", "");
-};
-
 exit main;
 __END__
 
@@ -70,7 +74,7 @@ file_dataurl_save - Saves the image as many small tiles using data:-urls
 
 =head1 SYNOPSIS
 
-<Save>/DATAURL
+<Save>/HTML with many small tiles using data:-urls/html
 
 =head1 DESCRIPTION
 
@@ -264,7 +268,7 @@ Browser compatibility list:
 
 =head1 AUTHOR
 
-Marc Lehmann
+Marc Lehmann <pcg goof com>
 
 =head1 DATE
 
@@ -272,6 +276,4 @@ Marc Lehmann
 
 =head1 LICENSE
 
-Marc Lehmann <pcg goof com>
-
 Distributed under the same terms as Gimp-Perl.


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