[gimp-perl] Make gallery work with GIF.



commit 91572277c4bfe977723794caa48e5d5bcade68f5
Author: Ed J <edj src gnome org>
Date:   Tue Jun 17 02:30:37 2014 +0100

    Make gallery work with GIF.

 examples/gallery |  192 +++++++++++++++++++++++++++---------------------------
 1 files changed, 97 insertions(+), 95 deletions(-)
---
diff --git a/examples/gallery b/examples/gallery
index 42f8d8c..02b1da5 100755
--- a/examples/gallery
+++ b/examples/gallery
@@ -4,44 +4,50 @@ use Gimp;
 use Gimp::Fu;
 use Gimp::Util;
 use IO::All;
+use File::Basename;
 use strict;
 use warnings;
 
-sub printHeader {
-  my ($rf, $gf, $bf, $r, $g, $b, $htmlFile, $gallerytitle) = @_;
-  printf $htmlFile "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">";
-  printf $htmlFile "<body bgcolor='#%x%x%x'>\n",$r,$g,$b;
-  printf $htmlFile "<center><font face=\"verdana,arial\" size=\"5\" color=\"#%x%x%x\">",$rf,$gf,$bf;
-  printf $htmlFile "$gallerytitle\n";
-  printf $htmlFile "<table><tr>\n";
+sub makeHeader {
+  my ($rf, $gf, $bf, $r, $g, $b, $gallerytitle) = @_;
+  sprintf <<EOF,$r,$g,$b,$rf,$gf,$bf;
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
+<body bgcolor='#%x%x%x'>
+<center><font face="verdana,arial" size="5" color="#%x%x%x">
+$gallerytitle
+<table><tr>
+EOF
 }
 
-sub printFooter {
-  my ($rf, $gf, $bf, $URL, $htmlFile) = @_;
-  printf $htmlFile "<br><br><hr size=\"1\" color=\"white\"><a href=\"$URL\">Back to previous page</a>\n";
-  printf $htmlFile "</body>\n</html>";
+sub makeFooter {
+  my ($URL) = @_;
+  <<EOF;
+<br><br><hr size="1" color="white"><a href="$URL">Back to previous page</a>
+</body>
+</html>
+EOF
 }
 
 podregister {
   Gimp::Context->push();
+  die __"No path supplied\n" if $path eq "";
   $path .= '/';
-  die __"All fields were not completed, sorry.\n" if $path eq "";
   die __"Scale factor must not be 0\n" if $fixed == 0;
   die __"Gallery title must be filled in. Please retry!\n" if $gallerytitle eq "";
   die __"You must also provide a filename (e.g. index) for your gallery generated html file.\n" if 
$galleryfile eq "";
-  my $countTotal = () = glob "$path*.{jpg,JPG}";
+  my @imagefiles = glob "$path*.{jpg,JPG,gif,GIF}";
+  die "Can't adjust brightness or contrast on GIFs\n"
+    if $bright != 0 or $contrast != 0 and grep { /\.gif$/i } @imagefiles;
   $gallerytitle =~ s/ /_/g;
-  opendir(CPATH, $path) or die ("Unable to open destination path");
-  my $sysval = mkdir "$path$gallerytitle", 0755;
-  if ($sysval != 1) {
-    die __"$!\nMake sure you've got write access to selected path ...\nMaybe the target path already exist 
in which case you can simply change the Gallery title and launch again\n";
-  }
-  my $dpath = "$path$gallerytitle/";
-  open (htmlFile, "> $dpath$galleryfile.html") or die ("Unable to process HTML File.");
+  my $dirname = "$path$gallerytitle";
+  mkdir $dirname or die __"$dirname: $!" unless -d $dirname;
+  my $dpath = "$dirname/";
+  my $topfh = io("$dpath$galleryfile.html");
   my ($rf, $gf, $bf) = @$foregroundcolor;
   my ($r, $g, $b) = @$color;
-  printHeader($rf, $gf, $bf, $r, $g, $b, *htmlFile, $gallerytitle);
-  print htmlFile "<table><tr>\n";
+  $topfh->print(makeHeader($rf, $gf, $bf, $r, $g, $b, $gallerytitle))
+    or die "Unable to process HTML File: $!\n";
+  $topfh->append("<table><tr>\n");
   my $totalentries = 0;
   my $entries = 1;
   my $twidthEval = 0;
@@ -50,81 +56,77 @@ podregister {
   my $ycorrection = 0;
   my $xcorrection = 0;
   Gimp::Progress->init("Releasing the gallery");
-  while (defined(my $cfile = readdir(CPATH))) {
-    if (( $cfile =~ /.jpg/ ) || ($cfile =~ /.JPG/)) {
-      my $img = Gimp->file_load("$path$cfile", "$path$cfile");
-      my $clayer = $img->get_active_layer;
-      $clayer->transform_rotate_simple($rotate, TRUE, 0, 0) if $rotate >= 0;
-      my $width = $img->width;
-      my $height = $img->height;
-      $clayer->brightness_contrast($bright, $contrast);
-      if ($label ne "") {
-        my $newlayer = $img->layer_new(200, 100, RGB_IMAGE, "newlayer", 100, NORMAL_MODE);
-        Gimp::Context->set_foreground($labelcolor);
-       my $txtlayer = $img->text_fontname(-1, 1, 1, $label, 0, 0, 16, 0, $font);
-       if ($twidthEval == 0) {
-         $txtwidth = $txtlayer->width;
-         $txtheight = $txtlayer->height;
-         $twidthEval = 1;
-       }
-       $ycorrection = $height - $txtheight if (($labelpos == 2) || ($labelpos == 3));
-       $xcorrection = $width-$txtwidth if (($labelpos == 1) || ($labelpos == 3));
-       $txtlayer->translate($xcorrection, $ycorrection);
-       $img->merge_visible_layers(EXPAND_AS_NECESSARY);
-      }
-      my $cvname="$dpath$cfile";
-      $cvname =~ s/\s/c/g;
-      $cfile =~ s/\s/c/g;
-      $img->get_active_layer->file_save(($cvname) x 2);
-      #Processing Thumbnail
-      my $tbheight = 1;
-      my $tbwidth = 1;
-      if ($scalefix == 1){
-       my $scalefactor = $width / $fixed;
-       $tbheight = $height / $scalefactor;
-       $tbwidth = $fixed;
-      } elsif ($scalefix == 2) {
-        my $scalefactor = $height / $fixed;
-        my $newwidth = $width / $scalefactor;
-        $tbwidth = $newwidth;
-        $tbheight = $fixed;
-      } else {
-         # scalefix 0 => both scaled by factor (value_factor)
-         $tbwidth = $width / $fixed;
-         $tbheight = $height / $fixed;
-      }
-      my $tbfname = "$dpath $cfile";
-      $tbfname =~ s/ /m/g;
-      io($tbfname)->print(io($cvname)->all);
-      $img->delete;
-      $img = Gimp->file_load("$tbfname", "$tbfname");
-      $clayer = $img->get_active_layer;
-      $img->scale($tbwidth, $tbheight);
-      #Processing detail HTML
-      my $htmlDetail = $cfile;
-      $htmlDetail =~ s/.jpg/.html/gi;
-      open (fhtmlDetail, "> $dpath$htmlDetail") or die ("Unable to process HTML File.");
-      printHeader($rf, $gf, $bf, $r, $g, $b, *fhtmlDetail, $gallerytitle);
-      print fhtmlDetail "<img src=\"$cfile\"></img>";
-      printFooter($rf, $gf, $bf, "$galleryfile.html", *fhtmlDetail);
-      close fhtmlDetail;
-      $clayer->file_save(($tbfname) x 2);
-      print htmlFile "         <td><a href=\"$htmlDetail\"><img src=\"m$cfile\" border='0'></a></td>\n";
-      if ($entries >= $columns){
-       print htmlFile "</tr><tr>";
-       $entries = 1;
-      } else {
-       $entries += 1;
+  for my $cfile (@imagefiles) {
+    my $img = Gimp->file_load($cfile, $cfile);
+    $cfile = basename $cfile;
+    my $clayer = $img->get_active_layer;
+    $clayer->transform_rotate_simple($rotate, TRUE, 0, 0) if $rotate >= 0;
+    my $width = $img->width;
+    my $height = $img->height;
+    $clayer->brightness_contrast($bright, $contrast)
+      if $bright != 0 or $contrast != 0;
+    if ($label ne "") {
+      my $newlayer = $img->layer_new(200, 100, RGB_IMAGE, "newlayer", 100, NORMAL_MODE);
+      Gimp::Context->set_foreground($labelcolor);
+      my $txtlayer = $img->text_fontname(-1, 1, 1, $label, 0, 0, 16, 0, $font);
+      if ($twidthEval == 0) {
+       $txtwidth = $txtlayer->width;
+       $txtheight = $txtlayer->height;
+       $twidthEval = 1;
       }
-      $totalentries += 1;
-      Gimp::Progress->update($totalentries / $countTotal);
-      $img->delete;
+      $ycorrection = $height - $txtheight if (($labelpos == 2) || ($labelpos == 3));
+      $xcorrection = $width-$txtwidth if (($labelpos == 1) || ($labelpos == 3));
+      $txtlayer->translate($xcorrection, $ycorrection);
+      $img->merge_visible_layers(EXPAND_AS_NECESSARY);
+    }
+    my $cvname="$dpath$cfile";
+    $cvname =~ s/\s/c/g;
+    $cfile =~ s/\s/c/g;
+    $img->get_active_layer->file_save(($cvname) x 2);
+    #Processing Thumbnail
+    my $tbheight = 1;
+    my $tbwidth = 1;
+    if ($scalefix == 1) {
+      my $scalefactor = $width / $fixed;
+      $tbheight = $height / $scalefactor;
+      $tbwidth = $fixed;
+    } elsif ($scalefix == 2) {
+      my $scalefactor = $height / $fixed;
+      my $newwidth = $width / $scalefactor;
+      $tbwidth = $newwidth;
+      $tbheight = $fixed;
+    } else {
+      # scalefix 0 => both scaled by factor (value_factor)
+      $tbwidth = $width / $fixed;
+      $tbheight = $height / $fixed;
+    }
+    my $tbfname = "$dpath $cfile";
+    $tbfname =~ s/ /m/g;
+    io($tbfname)->print(io($cvname)->all);
+    $img->delete;
+    $img = Gimp->file_load("$tbfname", "$tbfname");
+    $clayer = $img->get_active_layer;
+    $img->scale($tbwidth, $tbheight);
+    (my $htmlDetail = $cfile) =~ s/.(jpg|gif)/.html/gi;
+    io("$dpath$htmlDetail")->print(
+      makeHeader($rf, $gf, $bf, $r, $g, $b, $gallerytitle),
+      "<img src=\"$cfile\"></img>",
+      makeFooter("$galleryfile.html"),
+    ) or die "Unable to process HTML File $dpath$htmlDetail: $!\n";
+    $clayer->file_save(($tbfname) x 2);
+    $topfh->append("           <td><a href=\"$htmlDetail\"><img src=\"m$cfile\" border='0'></a></td>\n");
+    if ($entries >= $columns){
+      $topfh->append("</tr><tr>");
+      $entries = 1;
+    } else {
+      $entries += 1;
     }
+    $totalentries += 1;
+    Gimp::Progress->update($totalentries / @imagefiles);
+    $img->delete;
   }
   Gimp::Progress->update(1);
-  print htmlFile "</table>\n";
-  printFooter($rf, $gf, $bf, $parent_url, *htmlFile);
-  close htmlFile;
+  $topfh->append("</table>\n" . makeFooter($parent_url));
   Gimp->message(__"Your gallery ($dpath) has been generated through Gallery plug-in with $totalentries 
entries");
   Gimp::Context->pop();
   return undef;
@@ -139,7 +141,7 @@ gallerymaker - Generate your gallery in HTML format + some picture improvements
 
 =head1 SYNOPSIS
 
-<Image>/File/Create/HTML Gallery
+<Image>/File/Create/_HTML Gallery
 
 =head1 DESCRIPTION
 
@@ -160,7 +162,7 @@ Note : This plugin only processes JPG pictures.
   [PF_STRING, "label", "Text at top of each picture"],
   [PF_COLOR,  "labelcolor", "Color of label on each picture", [255,255,255]],
   [PF_RADIO,  "labelpos", "Position of text label", 0, [ UpLeft => 0, UpRight => 1, DownLeft => 2, DownRight 
=> 3 ]],
-  [PF_FONT,   "font", "Select font", undef ],
+  [PF_FONT,   "font", "Select font", 'Arial', ],
   [PF_SLIDER, "bright", "Brightness correction", 0, [ -127, 127]],
   [PF_SLIDER, "contrast", "Contrast correction", 0, [-127,127]],
   [PF_RADIO,  "rotate", "Rotation angle", -1, [ None => -1, Rotate90CCW => 2, Rotate90 => 0]],


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