]> mj.ucw.cz Git - gallery.git/blob - gal/gal-gen
Temporary move to gal/, so that we can import history from mj-web
[gallery.git] / gal / gal-gen
1 #!/usr/bin/perl
2 # Generate image gallery
3 # (c) 2004--2007 Martin Mares <mj@ucw.cz>
4
5 # Syntax of input file:
6 # <filename>    <datestamp>     <rotation>      <xform>
7 # where <rotation> is either ".", "l", "r" or "u"
8 #       <xform> contains:
9 #               d       touch output file to <datestamp>
10 #               D       include <datestamp> in output file names
11 #               n       normalize contrast
12 #               s       sharpen
13 #               h       equalize histogram
14
15 use Image::Magick;
16 use IO::Handle;
17
18 use strict;
19 use warnings;
20
21 my $maxw = 1024;
22 my $maxh = 768;
23 my $recompress = 1;
24
25 STDERR->autoflush(1);
26 print STDERR "Searching for template file... ";
27
28 my $tdir = "";
29 my $templ;
30 my $maxdepth = 10;
31 while ($maxdepth--) {
32         my $t = "${tdir}gallery.cf";
33         if (-f $t) {
34                 $templ = $t;
35                 last;
36         }
37         $tdir .= "../";
38         last if ! -d $tdir;
39 }
40 if ($templ) {
41         print STDERR "$templ\n";
42 } else {
43         print STDERR "NONE\n";
44 }
45
46 print "#!/usr/bin/perl\n\n";
47 if ($templ) {
48         print "require \"$templ\";\n\n";
49         print "SetOptions(\n";
50 } else {
51         print <<EOF
52 use lib '/home/mj/WWW/gal';
53 use Gallery;
54
55 SetOptions(
56         "GalURL" => "/~mj/gal",
57         "GalDir" => "/home/mj/WWW/gal",
58         "Theme" => "nrt",
59 EOF
60 ;
61 }
62 print <<EOF
63         "Title" => "Untitled"
64 );
65 Start();
66 EOF
67 ;
68
69 #`rm -f [0-9]*.{jpg,png} *.tmp`;  $? && die;
70
71 my $idx = 0;
72 while (<STDIN>) {
73         chomp;
74         my ($src, $date, $rotate, $xform) = split (/\t+/, $_);
75         $idx++;
76         my $id = sprintf("%03d", $idx);
77         if ($xform =~ /D/) {
78                 $id = "$date-$id";
79                 $id =~ s/ /-/g;
80         }
81         my $dest;
82         my $is_jpeg = 0;
83         if ($src =~ /\.(jpg|JPG|jpeg)$/) {
84                 $dest = "$id.jpg";
85                 $is_jpeg = 1;
86         } elsif ($src =~ /\.png$/) {
87                 $dest = "$id.png";
88         } else {
89                 die "$src: Unknown image type";
90         }
91         my $tmp = "$dest.tmp";
92         print STDERR "$dest: $src ";
93
94         my $p = new Image::Magick;
95         my $e;
96         $e = $p->Read($src) and die "Error reading $tmp: $e";
97         $p->Strip;
98         $p->SetAttribute(quality=>90);
99         my ($w, $h) = $p->Get('width', 'height');
100         print STDERR "-> ${w}x${h} ";
101
102         my ($w0, $h0) = ($rotate eq "l" || $rotate eq "r") ? ($h, $w) : ($w, $h);
103         my ($ww, $hh) = ($w0, $h0);
104         if ($ww > $maxw) {
105                 my $s = $maxw / $ww;
106                 $ww = $ww * $s;
107                 $hh = $hh * $s;
108         }
109         if ($hh > $maxh) {
110                 my $s = $maxh / $hh;
111                 $ww = $ww * $s;
112                 $hh = $hh * $s;
113         }
114         $ww = int($ww);
115         $hh = int($hh);
116
117         if ($recompress ||
118             !$is_jpeg ||
119             $xform ne "" ||
120             $ww != $w0 || $hh != $h0) {
121                 my $rot = 0;
122                 if ($rotate eq "l") { $rot = 270; }
123                 elsif ($rotate eq "r") { $rot = 90; }
124                 elsif ($rotate eq "u") { $rot = 180; }
125                 if ($xform =~ /s/) {
126                         print STDERR "-> sharpen ";
127                         $p->Sharpen(1);
128                 }
129                 if ($xform =~ /h/) {
130                         print STDERR "-> equalize ";
131                         $p->Equalize();
132                 }
133                 if ($xform =~ /n/) {
134                         print STDERR "-> normalize ";
135                         $p->Normalize();
136                 }
137                 if ($rot) {
138                         print STDERR "-> rotate $rot ";
139                         $p->Rotate(degrees=>$rot);
140                         $rotate = ".";
141                 }
142                 if ($ww != $w0 || $hh != $h0) {
143                         print STDERR "-> ${ww}x${hh} ";
144                         $p->Resize(width=>$ww, height=>$hh);
145                 }
146                 $e = $p->Write($tmp) and die "Unable to write $tmp: $e";
147         } else {
148                 `cp $src $tmp`;  $? && die;
149         }
150
151         if ($is_jpeg) {
152                 my $tran = "-optimize -copy none";
153                    if ($rotate eq ".") { }
154                 elsif ($rotate eq "l") { $tran .= " -rotate 270 -trim"; }
155                 elsif ($rotate eq "r") { $tran .= " -rotate 90 -trim"; }
156                 elsif ($rotate eq "u") { $tran .= " -rotate 180 -trim"; }
157                 else { die "Unknown rotation type $rotate"; }
158                 print STDERR "-> $tran ";
159                 `jpegtran $tran <$tmp >$dest`;  $? && die;
160         } else {
161                 rename $tmp, $dest or die;
162         }
163
164         if ($xform =~ /d/) {
165                 `touch -d "$date" $dest`; die if $?;
166         }
167
168         unlink $tmp;
169         print STDERR "... OK\n";
170         print "img(\"$dest\", \"\");\t\t# $src (${w0}x${h0})\n";
171 }
172
173 print "Finish();\n";
174
175 `rm -f *.tmp`;  $? && die;