Diff for /scaleomat/scaleomat.pl between versions 1.2 and 1.7

version 1.2, 2004/07/23 18:00:33 version 1.7, 2005/01/05 17:55:34
Line 1 Line 1
 #!/usr/bin/perl  #!/usr/bin/perl
   
   #  Copyright (C) 2003,2004 Robert Casties, IT-Group MPIWG
   # 
   #  This program is free software; you can redistribute it and/or modify it
   #  under the terms of the GNU General Public License as published by the Free
   #  Software Foundation; either version 2 of the License, or (at your option)
   #  any later version.
   # 
   #  Please read license.txt for the full details. A copy of the GPL may be found
   #  at http://www.gnu.org/copyleft/lgpl.html
   # 
   #  You should have received a copy of the GNU General Public License along with
   #  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
   #  Place, Suite 330, Boston, MA 02111-1307 USA
   
 use strict;  use strict;
   use sigtrap qw(die normal-signals);
   
 # MPIWG libraries  # MPIWG libraries
 use lib '/usr/local/mpiwg/scripts';  use lib '/usr/local/mpiwg/scripts';
Line 9  use MPIWGlib; Line 24  use MPIWGlib;
   
 $| = 1; # unblock IO  $| = 1; # unblock IO
   
 my $version = "V0.9.2 (ROC 22.4.2004)";  my $version = "V0.9.5 (ROC 5.1.2005)";
   
 $debug = 0;  $debug = 0;
   
Line 26  my $overwrite = 0; # overwrite already c Line 41  my $overwrite = 0; # overwrite already c
   
 # image file extensions and formats  # image file extensions and formats
 my %img_type_ext = ("tif" => "TIFF", "tiff" => "TIFF", "gif" => "GIF",   my %img_type_ext = ("tif" => "TIFF", "tiff" => "TIFF", "gif" => "GIF", 
         "jpg" => "JPEG", "png" => "PNG");          "jpg" => "JPEG", "png" => "PNG", "dcr" => "RAW");
 # destination image file extensions  # destination image file extensions
 my %target_ext_type = ("TIFF" => "jpg", "JPEG" => "jpg");  my %target_ext_type = ("TIFF" => "jpg", "JPEG" => "jpg");
   
 # default scale settings  # default scale settings
 my $scalesize = 2048; # pixel of longest side  my $scale_w = 2048; # width in pixel
   my $scale_h = 2048; # height in pixel
 my $scale_relative = 0; # scale by relative factor instead of destination size  my $scale_relative = 0; # scale by relative factor instead of destination size
 my $jpeg_quality = 75; # default JPEG compression quality  my $jpeg_quality = 75; # default JPEG compression quality
 my $use_encoder = 0; # autodetect encoder  my $use_encoder = 0; # autodetect encoder
Line 115  sub findfile { Line 131  sub findfile {
     return;      return;
 }  }
   
   #
   # $p = cleanpath($path, $startslash).  
   #
   # returns a pathname with trailing and starting slash removed (if
   # $startslash is true the starting slash is not removed)
   #
   sub cleanpath {
       my ($path, $startslash) = @_;
   
       if ($path =~ /^(\/)*(.*?)\/*$/) {
       if ($startslash) {
           return $1 . $2;
       } else {
           return $2;
       }
       }
       return $path;
   }
   
 #  #
 # ($basename, $extension) = splitfn($filename)  # ($basename, $extension) = splitfn($filename)
Line 142  sub mmkdir { Line 176  sub mmkdir {
     # does the directory already exist?      # does the directory already exist?
     if (-d $dirname) {      if (-d $dirname) {
     chmod $dir_perm, $dirname or do {      chmod $dir_perm, $dirname or do {
         logger('ERROR', "unable to change permission on $dirname!");          logger('WARNING', "unable to change permission on $dirname!");
         return 0;  
     };      };
     return 1;      return 1;
     }      }
Line 175  sub mmkdir { Line 208  sub mmkdir {
 #  #
 sub identify {  sub identify {
     my ($filepath) = @_;      my ($filepath) = @_;
     my $pictype;      my $pictype = "";
     my $picwidth;      my $picwidth = 0;
     my $picheight;      my $picheight = 0;
     my $bitdepth = 0;      my $bitdepth = 0;
     # use quickident first      # use quickident first
     $pictype = quickident($filepath);      $pictype = quickident($filepath);
     # optimize tiff identification      # optimized tiff identification
     if (($pictype)&&($pictype eq 'TIFF')) {      if (($pictype)&&($pictype eq 'TIFF')) {
     logger('DEBUG', "running tiffinfo $tiffinfo");      logger('DEBUG', "running tiffinfo $tiffinfo");
     if (open(IDENT, "nice -10 $tiffinfo '$filepath' 2>/dev/null |")) {      if (open(IDENT, "nice -10 $tiffinfo '$filepath' 2>/dev/null |")) {
         while (<IDENT>) {          while (<IDENT>) {
         chomp;          chomp;
           # we take the biggest values, because embedded thumbnails 
           # may also show up
         if (/Image Width:\s*(\d+)\s*Image Length:\s*(\d+)/) {          if (/Image Width:\s*(\d+)\s*Image Length:\s*(\d+)/) {
             $picwidth = $1;              $picwidth = $1 if ($1 > $picwidth);
             $picheight = $2;              $picheight = $2 if ($2 > $picheight);
             next;              next;
         }          }
         if (/Bits\/Sample:\s*(\d+)/) {          if (/Bits\/Sample:\s*(\d+)/) {
             $bitdepth = $1;              $bitdepth = $1 if ($1 > $bitdepth);
             next;              next;
         }          }
         }          }
         if ($picwidth) {          if ($picwidth) {
         logger('DEBUG', "TIFF $1 x $2");          logger('DEBUG', "TIFF $picwidth x $picheight");
         return ($pictype, $picwidth, $picheight, $bitdepth);          return ($pictype, $picwidth, $picheight, $bitdepth);
         }          }
     }      }
Line 238  sub quickident { Line 273  sub quickident {
   
   
 #  #
 # $error = scale_jpeg(\$args);  # $fact = scalefactor(\$args)
 #  #
 # scale JPEG images to JPEG using netpbm tools  sub scalefactor {
 #  
 # args needed: $srcdir, $filename, $destdir,   
 #              $scalesize, $scale_rel, $picwidth, $picheight  
 #  
 sub scale_jpeg {  
     my ($args) = @_;      my ($args) = @_;
   
     my $srcdir = $$args{'srcdir'};      my $srcdir = $$args{'srcdir'};
     my $filename = $$args{'filename'};      my $filename = $$args{'filename'};
     my $destdir = $$args{'destdir'};      my $scale_w = $$args{'scale_w'};
     my $scalesize = $$args{'scalesize'};      my $scale_h = $$args{'scale_h'};
     my $scale_rel = $$args{'scale_rel'};      my $scale_rel = $$args{'scale_rel'};
     my $scaleopt;      my $scale = 0;
   
     # convert jpg -> jpg  
     my ($basename, $fileext) = splitfn($filename);  
     my $newfn = $basename . ".jpg";  
     logger('INFO', "Convert(jpg): $filename -> $newfn");  
     return 1 if ($simulate);  
   
     if ($scale_rel) {      if ($scale_rel) {
     # scale relative -- no size needed, only scaling factor      # scale relative -- no size needed, only scaling factor
     $scaleopt = $scalesize;      $scale = $scale_w;
     } else {      } else {
     # scale to size -- size needed      # scale to size -- size needed
     my $pictype = $$args{'pictype'};      my $pictype = $$args{'pictype'};
     my $picwidth = $$args{'picwidth'};      my $picwidth = $$args{'picwidth'};
     my $picheight = $$args{'picheight'};      my $picheight = $$args{'picheight'};
     if (! $picwidth) {      if (! $picwidth) {
           # no size yet - identify
         ($pictype, $picwidth, $picheight) = identify("$srcdir/$filename");          ($pictype, $picwidth, $picheight) = identify("$srcdir/$filename");
         if ((! $pictype)||($picwidth == 0)||($picheight == 0)) {          if ((! $pictype)||($picwidth == 0)||($picheight == 0)) {
         logger('ERROR', "unable to identify $srcdir/$filename!");          logger('ERROR', "unable to identify $srcdir/$filename!");
         return 1;          return 0;
         }  
     }      }
     if ($picheight > $picwidth) {          # save values
         $scaleopt = $scalesize / $picheight;          $$args{'pictype'} = $pictype;
         logger('DEBUG', "PIC is portrait");          $$args{'picwidth'} = $picwidth;
           $$args{'picheight'} = $picheight;
       }
       # calculate x and y scaling factors
       my $scale_x = $scale_w / $picwidth;
       my $scale_y = $scale_h / $picheight;
       # take the smallest
       if ($scale_x < $scale_y) {
           $scale = $scale_x;
           logger('DEBUG', "PIC scale to width");
     } else {      } else {
         $scaleopt = $scalesize / $picwidth;          $scale = $scale_y;
         logger('DEBUG', "PIC is landscape");          logger('DEBUG', "PIC scale to height");
     }      }
     if ($scaleopt >= 1) {      if ($scale >= 1) {
         $scaleopt = 1;          $scale = 1;
         logger('DEBUG', "PIC is already smaller");          logger('DEBUG', "PIC is already smaller");
           # continue since we may have to convert
       }
     }      }
       return $scale;
     }      }
   
   
   #
   # $error = scale_jpeg(\$args);
   #
   # scale JPEG images to JPEG using netpbm tools
   #
   # args needed: $srcdir, $filename, $destdir, 
   #              $scale_w, $scale_h, $scale_rel, $picwidth, $picheight
   #
   sub scale_jpeg {
       my ($args) = @_;
   
       my $srcdir = $$args{'srcdir'};
       my $filename = $$args{'filename'};
       my $destdir = $$args{'destdir'};
       my $scale_w = $$args{'scale_w'};
       my $scale_h = $$args{'scale_h'};
       my $scale_rel = $$args{'scale_rel'};
       my $scaleopt;
   
       # convert jpg -> jpg
       my ($basename, $fileext) = splitfn($filename);
       my $newfn = $basename . ".jpg";
       logger('INFO', "Convert(jpg): $filename -> $newfn");
       return 1 if ($simulate);
   
       $scaleopt = scalefactor($args);
   
     if (!$scaleopt) {      if (!$scaleopt) {
     logger('ERROR', "unable to calculate scaling options!");      logger('ERROR', "unable to calculate scaling options!");
     return 1;      return 1;
     }      }
   
       if ($scaleopt = 1) {
       # is already smaller
       return 0;
       }
   
     # convert      # convert
     logger('DEBUG', "nice -10 $jpegloader '$srcdir/$filename' 2>/dev/null | nice -10 $scaler $scaleopt 2>/dev/null | nice -10 $jpegwriter > '$destdir/$newfn' 2>/dev/null");      logger('DEBUG', "nice -10 $jpegloader '$srcdir/$filename' 2>/dev/null | nice -10 $scaler $scaleopt 2>/dev/null | nice -10 $jpegwriter > '$destdir/$newfn' 2>/dev/null");
     return 0 if ($simulate);      return 0 if ($simulate);
     if (system("nice -10 $jpegloader '$srcdir/$filename' 2>/dev/null | nice -10 $scaler $scaleopt 2>/dev/null | nice -10 $jpegwriter > '$destdir/$newfn' 2>/dev/null") != 0) {      if (system("nice -10 $jpegloader '$srcdir/$filename' 2>/dev/null | nice -10 $scaler $scaleopt 2>/dev/null | nice -10 $jpegwriter > '$destdir/$newfn' 2>/dev/null") != 0) {
     logger('ERROR', "error converting '$srcdir/$filename'!");      logger('ERROR', "error converting '$srcdir/$filename'!");
       if (! -s "$destdir/$newfn") {
           # file broken (size 0)
           logger('ERROR', "created file '$destdir/$newfn' broken -- will be removed!");
     unlink "$destdir/$newfn";      unlink "$destdir/$newfn";
       }
     return 1;      return 1;
     }      }
   
Line 323  sub scale_jpeg { Line 396  sub scale_jpeg {
 # scale TIFF images to JPEG using ImageMagick convert  # scale TIFF images to JPEG using ImageMagick convert
 #  #
 # args needed: $srcdir, $filename, $destdir,   # args needed: $srcdir, $filename, $destdir, 
 #              $scalesize, $scale_rel, $picwidth, $picheight  #              $scale_w, $scale_h, $scale_rel, $picwidth, $picheight
 #  #
 sub scale_tiff_jpeg2 {  sub scale_tiff_jpeg2 {
     my ($args) = @_;      my ($args) = @_;
Line 331  sub scale_tiff_jpeg2 { Line 404  sub scale_tiff_jpeg2 {
     my $srcdir = $$args{'srcdir'};      my $srcdir = $$args{'srcdir'};
     my $filename = $$args{'filename'};      my $filename = $$args{'filename'};
     my $destdir = $$args{'destdir'};      my $destdir = $$args{'destdir'};
     my $scalesize = $$args{'scalesize'};      my $scale_w = $$args{'scale_w'};
       my $scale_h = $$args{'scale_h'};
     my $scale_rel = $$args{'scale_rel'};      my $scale_rel = $$args{'scale_rel'};
     my $scaleopt;      my $scaleopt;
   
     my ($basename, $fileext) = splitfn($filename);      my ($basename, $fileext) = splitfn($filename);
     my $newfn = $basename . ".jpg";      my $newfn = $basename . ".jpg";
     logger('INFO', "Convert(tiff2): $filename -> $newfn");      logger('INFO', "Convert(tiff2): $filename -> $newfn");
     return 1 if ($simulate);  
   
     if ($scale_rel) {  
     # scale relative -- no size needed, only scaling factor  
     $scaleopt = $scalesize;  
     } else {  
     # scale to size -- size needed  
     my $pictype = $$args{'pictype'};  
     my $picwidth = $$args{'picwidth'};  
     my $picheight = $$args{'picheight'};  
     if (! $picwidth) {  
         ($pictype, $picwidth, $picheight) = identify("$srcdir/$filename");  
         if ((! $pictype)||($picwidth == 0)||($picheight == 0)) {  
         logger('ERROR', "unable to identify $srcdir/$filename!");  
         return 1;  
         }  
     }  
     if ($picheight > $picwidth) {  
         $scaleopt = $scalesize / $picheight;  
         logger('DEBUG', "PIC is portrait");  
     } else {  
         $scaleopt = $scalesize / $picwidth;  
         logger('DEBUG', "PIC is landscape");  
     }  
     if ($scaleopt >= 1) {  
         $scaleopt = 1;  
         logger('DEBUG', "PIC is already smaller");  
     }  
     }  
   
     if (!$scaleopt) {  
     logger('ERROR', "unable to calculate scaling options!");  
     return 1;  
     }  
   
     if ($scale_rel) {      if ($scale_rel) {
     my $per_scale = 100 * $scaleopt;      my $per_scale = 100 * $scale_w;
     logger('DEBUG', "nice -10 $converter -quality $jpeg_quality -scale $per_scale\% '$srcdir/$filename' '$destdir/$newfn' 2>/dev/null");      logger('DEBUG', "nice -10 $converter -quality $jpeg_quality -scale $per_scale\% '$srcdir/$filename' '$destdir/$newfn' 2>/dev/null");
     return 0 if ($simulate);      return 0 if ($simulate);
     if (system("nice -10 $converter -quality $jpeg_quality -scale $per_scale\% '$srcdir/$filename' '$destdir/$newfn' 2>/dev/null\n") != 0) {      if (system("nice -10 $converter -quality $jpeg_quality -scale $per_scale\% '$srcdir/$filename' '$destdir/$newfn' 2>/dev/null\n") != 0) {
Line 382  sub scale_tiff_jpeg2 { Line 422  sub scale_tiff_jpeg2 {
         return 1;          return 1;
     }      }
     } else {      } else {
     logger('DEBUG', "nice -10 $converter -quality $jpeg_quality -scale ${scalesize}x${scalesize} '$srcdir/$filename' '$destdir/$newfn' 2>/dev/null");      logger('DEBUG', "nice -10 $converter -quality $jpeg_quality -scale ${scale_w}x${scale_h} '$srcdir/$filename' '$destdir/$newfn' 2>/dev/null");
     return 0 if ($simulate);      return 0 if ($simulate);
     if (system("nice -10 $converter -quality $jpeg_quality -scale ${scalesize}x${scalesize} '$srcdir/$filename' '$destdir/$newfn' 2>/dev/null\n") != 0) {      if (system("nice -10 $converter -quality $jpeg_quality -scale ${scale_w}x${scale_h} '$srcdir/$filename' '$destdir/$newfn' 2>/dev/null\n") != 0) {
         logger('ERROR', "error converting '$srcdir/$filename'!");          logger('ERROR', "error converting '$srcdir/$filename'!");
           if (! -s "$destdir/$newfn") {
           # file broken (size 0)
           logger('ERROR', "created file '$destdir/$newfn' broken -- will be removed!");
           unlink "$destdir/$newfn";
           }
         return 1;          return 1;
     }      }
     }      }
Line 394  sub scale_tiff_jpeg2 { Line 439  sub scale_tiff_jpeg2 {
     chmod $file_perm, "$destdir/$newfn" or      chmod $file_perm, "$destdir/$newfn" or
     logger('WARNING', "unable to set permission on '$destdir/$newfn'");      logger('WARNING', "unable to set permission on '$destdir/$newfn'");
   
     if (! -s "$destdir/$newfn") {  
     # file broken (size 0)  
     logger('ERROR', "created file '$destdir/$newfn' broken -- will be removed!");  
     unlink "$destdir/$newfn";  
     return 1;  
     }  
     return 0;      return 0;
 }  }
   
Line 410  sub scale_tiff_jpeg2 { Line 449  sub scale_tiff_jpeg2 {
 # scale TIFF images to JPEG using netpbm tools  # scale TIFF images to JPEG using netpbm tools
 #  #
 # args needed: $srcdir, $filename, $destdir,   # args needed: $srcdir, $filename, $destdir, 
 #              $scalesize, $scale_rel, $picwidth, $picheight  #              $scale_w, $scale_h, $scale_rel, $picwidth, $picheight
 #  #
 sub scale_tiff_jpeg {  sub scale_tiff_jpeg {
     my ($args) = @_;      my ($args) = @_;
Line 418  sub scale_tiff_jpeg { Line 457  sub scale_tiff_jpeg {
     my $srcdir = $$args{'srcdir'};      my $srcdir = $$args{'srcdir'};
     my $filename = $$args{'filename'};      my $filename = $$args{'filename'};
     my $destdir = $$args{'destdir'};      my $destdir = $$args{'destdir'};
     my $scalesize = $$args{'scalesize'};      my $bitdepth = $$args{'bitdepth'};
       my $scale_w = $$args{'scale_w'};
       my $scale_h = $$args{'scale_h'};
     my $scale_rel = $$args{'scale_rel'};      my $scale_rel = $$args{'scale_rel'};
     my $scaleopt;      my $scaleopt;
   
Line 428  sub scale_tiff_jpeg { Line 469  sub scale_tiff_jpeg {
     logger('INFO', "Convert(tiff1): $filename -> $newfn");      logger('INFO', "Convert(tiff1): $filename -> $newfn");
     return 1 if ($simulate);      return 1 if ($simulate);
   
     if ($scale_rel) {      $scaleopt = scalefactor($args);
     # scale relative -- no size needed, only scaling factor  
     $scaleopt = $scalesize;  
     } else {  
     # scale to size -- size needed  
     my $pictype = $$args{'pictype'};  
     my $picwidth = $$args{'picwidth'};  
     my $picheight = $$args{'picheight'};  
     if (! $picwidth) {  
         ($pictype, $picwidth, $picheight) = identify("$srcdir/$filename");  
         if ((! $pictype)||($picwidth == 0)||($picheight == 0)) {  
         logger('ERROR', "unable to identify $srcdir/$filename!");  
         return 1;  
         }  
     }  
     if ($picheight > $picwidth) {  
         $scaleopt = $scalesize / $picheight;  
         logger('DEBUG', "PIC is portrait");  
     } else {  
         $scaleopt = $scalesize / $picwidth;  
         logger('DEBUG', "PIC is landscape");  
     }  
     if ($scaleopt >= 1) {  
         $scaleopt = 1;  
         logger('DEBUG', "PIC is already smaller");  
     }  
     }  
   
     if (!$scaleopt) {      if (!$scaleopt) {
     logger('ERROR', "unable to calculate scaling options!");      logger('ERROR', "unable to calculate scaling options!");
Line 462  sub scale_tiff_jpeg { Line 477  sub scale_tiff_jpeg {
     }      }
   
     # convert      # convert
     logger('DEBUG', "nice -10 $tiffloader '$srcdir/$filename' 2>/dev/null | nice -10 $scaler $scaleopt 2>/dev/null | nice -10 $jpegwriter --quality $jpeg_quality > '$destdir/$newfn' 2>/dev/null");      my $cmd = "nice -10 $tiffloader \'$srcdir/$filename\' 2>/dev/null ";
       if ($bitdepth == 1) {
       # antialiasing bilevel images
       $cmd .= "| nice -10 $quantizer 2 2 2>/dev/null ";
       }
       $cmd .= "| nice -10 $scaler $scaleopt 2>/dev/null | nice -10 $jpegwriter --quality $jpeg_quality > '$destdir/$newfn' 2>/dev/null";
       logger('DEBUG', "$cmd");
     return 0 if ($simulate);      return 0 if ($simulate);
     if (system("nice -10 $tiffloader '$srcdir/$filename' 2>/dev/null | nice -10 $scaler $scaleopt 2>/dev/null | nice -10 $jpegwriter --quality $jpeg_quality > '$destdir/$newfn' 2>/dev/null") != 0) {      if (system($cmd) != 0) {
     logger('ERROR', "error converting '$srcdir/$filename'!");      logger('ERROR', "error converting '$srcdir/$filename'!");
       if (! -s "$destdir/$newfn") {
           # file broken (size 0)
           logger('ERROR', "created file '$destdir/$newfn' broken -- will be removed!");
     unlink "$destdir/$newfn";      unlink "$destdir/$newfn";
       }
     return 1;      return 1;
     }      }
   
Line 474  sub scale_tiff_jpeg { Line 499  sub scale_tiff_jpeg {
     chmod $file_perm, "$destdir/$newfn" or      chmod $file_perm, "$destdir/$newfn" or
     logger('WARNING', "unable to set permission on '$destdir/$newfn'");      logger('WARNING', "unable to set permission on '$destdir/$newfn'");
   
     if (! -s "$destdir/$newfn") {  
     # file broken (size 0)  
     logger('ERROR', "created file '$destdir/$newfn' broken -- will be removed!");  
     unlink "$destdir/$newfn";  
     return 1;  
     }  
     return 0;      return 0;
 }  }
   
Line 491  sub scale_tiff_jpeg { Line 510  sub scale_tiff_jpeg {
 # scale TIFF images to PNG using netpbm tools  # scale TIFF images to PNG using netpbm tools
 #  #
 # args needed: $srcdir, $filename, $destdir,   # args needed: $srcdir, $filename, $destdir, 
 #              $scalesize, $scale_rel, $picwidth, $picheight  #              $scale_w, $scale_h, $scale_rel, $picwidth, $picheight
 #  #
 sub scale_tiff_png {  sub scale_tiff_png {
     my ($args) = @_;      my ($args) = @_;
Line 499  sub scale_tiff_png { Line 518  sub scale_tiff_png {
     my $srcdir = $$args{'srcdir'};      my $srcdir = $$args{'srcdir'};
     my $filename = $$args{'filename'};      my $filename = $$args{'filename'};
     my $destdir = $$args{'destdir'};      my $destdir = $$args{'destdir'};
     my $scalesize = $$args{'scalesize'};  
     my $bitdepth = $$args{'bitdepth'};      my $bitdepth = $$args{'bitdepth'};
       my $scale_w = $$args{'scale_w'};
       my $scale_h = $$args{'scale_h'};
     my $scale_rel = $$args{'scale_rel'};      my $scale_rel = $$args{'scale_rel'};
     my $scaleopt;      my $scaleopt;
   
     # convert jpg -> jpg      # convert tif -> png
     my ($basename, $fileext) = splitfn($filename);      my ($basename, $fileext) = splitfn($filename);
     my $newfn = $basename . ".png";      my $newfn = $basename . ".png";
     logger('INFO', "Convert(tiff3): $filename -> $newfn");      logger('INFO', "Convert(tiff3): $filename -> $newfn");
   
     if ($scale_rel) {      $scaleopt = scalefactor($args);
     # scale relative -- no size needed, only scaling factor  
     $scaleopt = $scalesize;  
     } else {  
     # scale to size -- size needed  
     my $pictype = $$args{'pictype'};  
     my $picwidth = $$args{'picwidth'};  
     my $picheight = $$args{'picheight'};  
     if (! $picwidth) {  
         ($pictype, $picwidth, $picheight, $bitdepth) = identify("$srcdir/$filename");  
         if ((! $pictype)||($picwidth == 0)||($picheight == 0)) {  
         logger('ERROR', "unable to identify $srcdir/$filename!");  
         return 1;  
         }  
     }  
     if ($picheight > $picwidth) {  
         $scaleopt = $scalesize / $picheight;  
         logger('DEBUG', "PIC is portrait");  
     } else {  
         $scaleopt = $scalesize / $picwidth;  
         logger('DEBUG', "PIC is landscape");  
     }  
     if ($scaleopt >= 1) {  
         $scaleopt = 1;  
         logger('DEBUG', "PIC is already smaller");  
     }  
     }  
   
     if (!$scaleopt) {      if (!$scaleopt) {
     logger('ERROR', "unable to calculate scaling options!");      logger('ERROR', "unable to calculate scaling options!");
Line 553  sub scale_tiff_png { Line 547  sub scale_tiff_png {
     return 0 if ($simulate);      return 0 if ($simulate);
     if (system($cmd) != 0) {      if (system($cmd) != 0) {
     logger('ERROR', "error converting '$srcdir/$filename'!");      logger('ERROR', "error converting '$srcdir/$filename'!");
       if (! -s "$destdir/$newfn") {
           # file broken (size 0)
           logger('ERROR', "created file '$destdir/$newfn' broken -- will be removed!");
     unlink "$destdir/$newfn";      unlink "$destdir/$newfn";
       }
     return 1;      return 1;
     }      }
   
Line 561  sub scale_tiff_png { Line 559  sub scale_tiff_png {
     chmod $file_perm, "$destdir/$newfn" or      chmod $file_perm, "$destdir/$newfn" or
     logger('WARNING', "unable to set permission on '$destdir/$newfn'");      logger('WARNING', "unable to set permission on '$destdir/$newfn'");
   
     if (! -s "$destdir/$newfn") {  
     # file broken (size 0)  
     logger('ERROR', "created file '$destdir/$newfn' broken -- will be removed!");  
     unlink "$destdir/$newfn";  
     return 1;  
     }  
     return 0;      return 0;
 }  }
   
Line 605  sub convert_file { Line 597  sub convert_file {
     #      #
     $pictype = quickident("$srcdir/$filename");      $pictype = quickident("$srcdir/$filename");
     if ($pictype) {      if ($pictype) {
       if ($pictype eq "RAW") {
           logger('DEBUG', "skipping raw file '$srcdir/$filename'");
           return 0;
       }
     my $newext = $target_ext_type{$pictype};      my $newext = $target_ext_type{$pictype};
     if ($newext) {      if ($newext) {
         $newfn = $filebase . ".$newext";          $newfn = $filebase . ".$newext";
Line 612  sub convert_file { Line 608  sub convert_file {
         if (-f "$destdir/$newfn") {          if (-f "$destdir/$newfn") {
         logger('DEBUG', "CONV file exists: $newfn");          logger('DEBUG', "CONV file exists: $newfn");
         if (! $overwrite) {          if (! $overwrite) {
               # compare age with source file
               if (-M "$destdir/$newfn" > -M "$srcdir/$filename") {
               logger('DEBUG', "CONV file is older: $newfn");
               } else {
             logger('INFO', "File already converted: $newfn");              logger('INFO', "File already converted: $newfn");
             return 0;              return 0;
         }          }
         }          }
           }
     } else {      } else {
         logger('DEBUG', "target extension for $pictype unknown!");          logger('DEBUG', "target extension for $pictype unknown!");
     }      }
     } else {      } else {
     # quick ident failed -- do it slowly      # quick ident failed -- do it slowly
   
     ($pictype, $picwidth, $picheight, $bitdepth) = identify("$srcdir/$filename");      ($pictype, $picwidth, $picheight, $bitdepth) = identify("$srcdir/$filename");
     if ((! $pictype)||($picwidth == 0)||($picheight == 0)) {      if ((! $pictype)||($picwidth == 0)||($picheight == 0)) {
         logger('WARNING', "unknown file type '$srcdir/$filename'");          logger('WARNING', "unknown file type '$srcdir/$filename'");
Line 639  sub convert_file { Line 639  sub convert_file {
     $args{'picheight'} = $picheight;      $args{'picheight'} = $picheight;
     $args{'bitdepth'} = $bitdepth;      $args{'bitdepth'} = $bitdepth;
     $args{'srcdir'} = $srcdir;      $args{'srcdir'} = $srcdir;
     $args{'scalesize'} = $scalesize;      $args{'scale_w'} = $scale_w;
       $args{'scale_h'} = $scale_h;
     $args{'scale_rel'} = $scale_relative;      $args{'scale_rel'} = $scale_relative;
   
     # decide conversion based on image type and encoding preferences      # decide conversion based on image type and encoding preferences
Line 739  sub convert_dir { Line 740  sub convert_dir {
     my ($srcdir, $workdir, $destdir) = @_;      my ($srcdir, $workdir, $destdir) = @_;
   
     logger('INFO', "** Converting Scans **");      logger('INFO', "** Converting Scans **");
     logger('INFO', "Starting in directory '$srcdir/$workdir'");  
           
       if (-d "$srcdir/$workdir") {
       # it's a dirrectory
       logger('INFO', "Starting in directory '$srcdir/$workdir'");
     walk_convert_dir($srcdir, $workdir, $destdir);      walk_convert_dir($srcdir, $workdir, $destdir);
   
     # touch source directory so digilib rescans the thumbnails      # touch source directory so digilib rescans the thumbnails
     #logger('DEBUG', "/usr/bin/touch $source_base_dirs[0]/$workdir");      #logger('DEBUG', "/usr/bin/touch $source_base_dirs[0]/$workdir");
     system("/usr/bin/touch '$srcdir/$workdir'");      system("/usr/bin/touch '$srcdir/$workdir'");
       } elsif (-f _) {
       # it's a file
       logger('INFO', "Converting file '$srcdir/$workdir'");
       convert_file($srcdir, $workdir, $destdir);
       # touch source parent directory so digilib rescans the thumbnails
       my $pdir = "$srcdir/$workdir";
       # chop off after the last slash
       $pdir =~ s/\/[^\/]+$/\//;
       system("/usr/bin/touch '$pdir'");
       }
   
     logger('DONE', "** Finished converting scans **");      logger('DONE', "** Finished converting scans **");
     return 1;      return 1;
Line 763  if ($#ARGV < 3) { Line 775  if ($#ARGV < 3) {
     print "Scale-O-Mat $version\n";      print "Scale-O-Mat $version\n";
     print "  use: scaleomat.pl -src=src-base -dest=dest-base -dir=workdir [...]\n";      print "  use: scaleomat.pl -src=src-base -dest=dest-base -dir=workdir [...]\n";
     print "    reads from scr-base/workdir and writes to dest-base/workdir\n";      print "    reads from scr-base/workdir and writes to dest-base/workdir\n";
     print "    -scaleto=destination size\n";      print "    -scaleto=destination size (S or WxH)\n";
     print "    -scaleby=magnification factor.\n";      print "    -scaleby=magnification factor.\n";
     print "    -jpegqual=JPEG quality (0-100)\n";      print "    -jpegqual=JPEG quality (0-100)\n";
     print "    -replace=yes replaces existing files (default=skip).\n";      print "    -replace=yes replaces existing files (default=skip).\n";
Line 778  checksoft(); Line 790  checksoft();
 my $args = parseargs();  my $args = parseargs();
   
 # source dir  # source dir
 my $srcdir = $$args{'src'};  my $srcdir = cleanpath($$args{'src'}, 1);
   
 # destination dir  # destination dir
 my $destdir = $$args{'dest'};  my $destdir = cleanpath($$args{'dest'}, 1);
   
 # working dir  # working dir
 my $workdir = $$args{'dir'};  my $workdir = cleanpath($$args{'dir'});
   
 # destination size  # destination size
 if ($$args{'scaleby'}) {  if ($$args{'scaleby'}) {
     $scale_relative = 1;      $scale_relative = 1;
     $scalesize = $$args{'scaleby'};      $scale_w = $$args{'scaleby'};
     logger('INFO', "Scaling relative by factor $scalesize");      logger('INFO', "Scaling relative by factor $scale_w");
 }  }
 if ($$args{'scaleto'}) {  if ($$args{'scaleto'}) {
     $scale_relative = 0;      $scale_relative = 0;
     $scalesize = $$args{'scaleto'};      if ($$args{'scaleto'} =~ /(\d+)x(\d+)/) {
     logger('INFO', "Scaling absolute to size $scalesize");      $scale_w = $1;
       $scale_h = $2;
       } else {
       $scale_w = $$args{'scaleto'};
       $scale_h = $$args{'scaleto'};
       }
       logger('INFO', "Scaling absolute to size $scale_w x $scale_h");
 }  }
   
 # JPEG quality  # JPEG quality

Removed from v.1.2  
changed lines
  Added in v.1.7


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>