#!/usr/bin/perl # Generate image gallery # (c) 2004--2007 Martin Mares # Syntax of input file: # # where is either ".", "l", "r" or "u" # contains: # d touch output file to # D include in output file names # n normalize contrast # s sharpen # h equalize histogram use Image::Magick; use IO::Handle; use strict; use warnings; my $maxw = 1024; my $maxh = 768; my $recompress = 1; STDERR->autoflush(1); print STDERR "Searching for template file... "; my $tdir = ""; my $templ; my $maxdepth = 10; while ($maxdepth--) { my $t = "${tdir}gallery.cf"; if (-f $t) { $templ = $t; last; } $tdir .= "../"; last if ! -d $tdir; } if ($templ) { print STDERR "$templ\n"; } else { print STDERR "NONE\n"; } print "#!/usr/bin/perl\n\n"; if ($templ) { print "require \"$templ\";\n\n"; print "SetOptions(\n"; } else { print < "/~mj/gal", "GalDir" => "/home/mj/WWW/gal", "Theme" => "nrt", EOF ; } print < "Untitled" ); Start(); EOF ; #`rm -f [0-9]*.{jpg,png} *.tmp`; $? && die; my $idx = 0; while () { chomp; my ($src, $date, $rotate, $xform) = split (/\t+/, $_); $idx++; my $id = sprintf("%03d", $idx); if ($xform =~ /D/) { $id = "$date-$id"; $id =~ s/ /-/g; } my $dest; my $is_jpeg = 0; if ($src =~ /\.(jpg|JPG|jpeg)$/) { $dest = "$id.jpg"; $is_jpeg = 1; } elsif ($src =~ /\.png$/) { $dest = "$id.png"; } else { die "$src: Unknown image type"; } my $tmp = "$dest.tmp"; print STDERR "$dest: $src "; my $p = new Image::Magick; my $e; $e = $p->Read($src) and die "Error reading $tmp: $e"; $p->Strip; $p->SetAttribute(quality=>90); my ($w, $h) = $p->Get('width', 'height'); print STDERR "-> ${w}x${h} "; my ($w0, $h0) = ($rotate eq "l" || $rotate eq "r") ? ($h, $w) : ($w, $h); my ($ww, $hh) = ($w0, $h0); if ($ww > $maxw) { my $s = $maxw / $ww; $ww = $ww * $s; $hh = $hh * $s; } if ($hh > $maxh) { my $s = $maxh / $hh; $ww = $ww * $s; $hh = $hh * $s; } $ww = int($ww); $hh = int($hh); if ($recompress || !$is_jpeg || $xform ne "" || $ww != $w0 || $hh != $h0) { my $rot = 0; if ($rotate eq "l") { $rot = 270; } elsif ($rotate eq "r") { $rot = 90; } elsif ($rotate eq "u") { $rot = 180; } if ($xform =~ /s/) { print STDERR "-> sharpen "; $p->Sharpen(1); } if ($xform =~ /h/) { print STDERR "-> equalize "; $p->Equalize(); } if ($xform =~ /n/) { print STDERR "-> normalize "; $p->Normalize(); } if ($rot) { print STDERR "-> rotate $rot "; $p->Rotate(degrees=>$rot); $rotate = "."; } if ($ww != $w0 || $hh != $h0) { print STDERR "-> ${ww}x${hh} "; $p->Resize(width=>$ww, height=>$hh); } $e = $p->Write($tmp) and die "Unable to write $tmp: $e"; } else { `cp $src $tmp`; $? && die; } if ($is_jpeg) { my $tran = "-optimize -copy none"; if ($rotate eq ".") { } elsif ($rotate eq "l") { $tran .= " -rotate 270 -trim"; } elsif ($rotate eq "r") { $tran .= " -rotate 90 -trim"; } elsif ($rotate eq "u") { $tran .= " -rotate 180 -trim"; } else { die "Unknown rotation type $rotate"; } print STDERR "-> $tran "; `jpegtran $tran <$tmp >$dest`; $? && die; } else { rename $tmp, $dest or die; } if ($xform =~ /d/) { `touch -d "$date" $dest`; die if $?; } unlink $tmp; print STDERR "... OK\n"; print "img(\"$dest\", \"\");\t\t# $src (${w0}x${h0})\n"; } print "Finish();\n"; `rm -f *.tmp`; $? && die;