1#!/usr/local/bin/perl
2
3# metapixel-prepare --- prepare images for metapixeling.
4
5# Copyright (C) 1999-2004 Mark Probst
6# Copyright (C) 2004 Jake Di Toro
7# Copyright (C) 2006 Stefan Soeffing
8
9# Maintainer: Mark Probst <schani@complang.tuwien.ac.at>
10
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2, or (at your option)
14# any later version.
15
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19# GNU General Public License for more details.
20
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, you can either send email to this
23# program's maintainer or write to: The Free Software Foundation,
24# Inc.; 675 Massachusetts Avenue; Cambridge, MA 02139, USA.
25
26use strict;
27
28use Getopt::Long;
29use File::Basename;
30use IO::Handle;
31
32sub disambiguate_filename {
33    my $filename = shift;
34    my $suffix = shift;
35
36    return "$filename$suffix" if !-e "$filename$suffix";
37
38    my $ctr = 1;
39
40    while (-e "$filename.$ctr$suffix") {
41	++$ctr;
42    }
43
44    return "$filename.$ctr$suffix";
45}
46
47sub usage {
48    print STDERR "Usage: $0 [OPTION]... <srcdir> <destdir>
49
50Prepares all images in <srcdir> for use as small images in
51photomosaics. The scaled versions and the table file are
52stored in <destdir>.
53
54  --help             display this help and exit
55  --width=WIDTH      specify width of small images
56  --height=HEIGHT    specify height of small images
57  -r, --recurse      recurse through directories
58  --debug            print out debugging info
59";
60    exit(1);
61}
62
63my ($width, $height, $destdir) = split /\s+/, `metapixel --print-prepare-settings`;
64
65my $do_recurse;
66my $DEBUG;
67
68if (!GetOptions("help", \&usage,
69		"width=i", \$width,
70		"height=i", \$height,
71		"recurse|r", \$do_recurse,
72		"debug", \$DEBUG)) {
73    usage();
74}
75
76if (!$width || $width <= 0 || !$height || $height <= 0) {
77    print "$0: Width and height of the prepared images must be specified.\n";
78    exit(1);
79}
80
81my $opts = "--width=$width --height=$height";
82
83if ($#ARGV != 0 && $#ARGV != 1) {
84    usage();
85}
86
87my $srcdir = $ARGV[0];
88
89$destdir = $ARGV[1] if $#ARGV > 0;
90
91if (! -d $srcdir || ! -r $srcdir) {
92    print "$0: Source directory $srcdir does not exist or is unreadable.\n";
93    exit(1);
94}
95
96unless ($destdir) {
97    print "$0: A destination directory must be specified.\n";
98    exit(1);
99}
100if (! -d $destdir) {
101    print "$0: Destination directory $destdir does not exist.\n";
102    exit(1);
103}
104
105STDOUT->autoflush(1);
106
107sub process_dir {
108    my $pdir = shift;
109    my $do_recurse = shift;
110
111    print "Processing dir: $pdir\n" if $DEBUG;
112
113    if (opendir DIR, $pdir)
114    {
115	my @files = grep !/^\.\.?$/, readdir DIR;
116
117	closedir DIR;
118
119	foreach my $filename (@files) {
120	    my $fullname = "$pdir/$filename";
121
122	    print "Testing file: $fullname\n" if $DEBUG;
123
124	    while (-l $fullname) {
125		print "Following symlink: $fullname\n" if $DEBUG;
126		$fullname = readlink($fullname);
127	    }
128
129	    if (-f $fullname && -r $fullname) {
130		my ($name, $path, $suffix) = fileparse($fullname);
131
132		print "Processing: $fullname\n" if $DEBUG;
133
134		my $thumbname = disambiguate_filename("$destdir/$name$suffix", ".png");
135
136		`metapixel $opts --prepare "$fullname" "$thumbname" "$destdir/tables.mxt"`;
137		if ($? != 0) {
138		    print "Error running metapixel - skipping file $fullname\n"
139		    }
140		else
141		{
142		    print "." if !$DEBUG;
143		}
144	    }
145	    elsif (-d $fullname && -r $fullname && $do_recurse) {
146		process_dir($fullname, $do_recurse);
147	    }
148	}
149    } else {
150	print "Error: cannot open directory $pdir\n";
151    }
152}
153
154process_dir($srcdir, $do_recurse);
155