-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetectRoll.pm
More file actions
132 lines (113 loc) · 2.72 KB
/
DetectRoll.pm
File metadata and controls
132 lines (113 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package DetectRoll;
use strict;
use Carp;
use Image::ExifTool ':Public';
our $suffix='.jpg';
our $prefix = 'dsc_';
sub new ( $$ ) {
my ($class, $dir) = @_;
bless {dir=>$dir}, $class;
}
sub shutter_count( $$ ) {
my $self=shift;
my $file=shift;
unless (exists $self->{sc}{$file}) {
$self->{sc}{$file} = ImageInfo($self->{dir}."/".$file)->{ShutterCount};
confess "Can't find shutter count" unless defined $self->{sc}{$file};
}
return $self->{sc}{$file};
}
sub open_dir {
my $self=$_[0];
if (defined $self->{DH}) {
rewinddir $self->{DH};
} else {
opendir $self->{DH}, $self->{dir} or croak "$self->{dir}: $!";
}
}
sub DESTROY {
my $self=$_[0];
if (defined $self->{DH}) {
closedir $self->{DH};
}
}
sub has_roll {
&open_dir;
my $self=shift;
my @suffix_list = split / /, $suffix;
my $suffix_re = '(?:' . join('|', @suffix_list) .')';
my ($min,$max);
my ($minf,$maxf);
while (my $fn = readdir $self->{DH}) {
next unless $fn =~ /([0-9]+).*\Q$suffix_re/i;
if (!defined($min) or $min > $1) {
$minf=$fn;
$min=$1;
}
if (!defined($max) or $max < $1) {
$maxf=$fn;
$max=$1;
}
}
if (defined $min and defined $max) {
return ($self->shutter_count($minf) > $self->shutter_count($maxf));
}
};
our $debug;
sub find_roll {
use integer;
&open_dir;
my $self=shift;
my %filenames;
my ($min,$max, $minf, $maxf);
my @suffix_list = split / /, $suffix;
my $suffix_re = '(?:' . join('|', @suffix_list) .')';
while (my $fn = readdir $self->{DH}) {
#FIXME - prefix unitialised when creating new dir.
next unless $fn =~ /$prefix([0-9]+).*\Q$suffix_re/i; #bogus paths will loop
$filenames{int($1)} = $fn;
if (!defined($min) or $min > $1) {
$min=$1;
$minf=$fn;
}
if (!defined($max) or $max < $1) {
$max=$1;
$maxf=$fn;
}
}
unless (keys %filenames) {
warn "No files found: $prefix$suffix";
return;
}
my $mid=int(($min+$max)/2);
my $left;
my $right;
for (;;){
$mid=int($mid);
$left=$right=$mid;
-- $left while $left>=$min and ! exists $filenames{$left};
++ $right while $right <= $max and ! exists $filenames{$right};
warn "compare $min $left $mid $right $max" if $debug;
warn "$minf $maxf" if $debug;
warn "$filenames{$left} $filenames{$right}" if $debug;
if ($self->shutter_count($filenames{$right}) >
$self->shutter_count($minf)) {
if ($mid == $max) {
last;
} elsif (++$mid == $max) {
next;
} else {
$mid=($right+$max)/2;
}
}
elsif ($self->shutter_count($filenames{$left}) <
$self->shutter_count($maxf)) {
$mid=($left+$min)/2;
last if $mid < $min;
} else {
last;
}
}
return ($left,$right);
}
1;