blib/lib/Image/Magick/Tiler.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Image::Magick::Tiler;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶use Image::Magick;
Subroutine BEGIN: called CC=1 SCAR=2.6
9  
10 use Moo;
11  
12 use Types::Standard qw/Any ArrayRef Int Str/;
13  
14 has count =>
15 (
16 default => sub {return 0},
17 is => 'rw',
18 isa => Int,
19 required => 0,
20 );
21  
22 has input_file =>
23 (
24 default => sub {return ''},
25 is => 'rw',
26 isa => Str,
27 required => 0,
28 );
29  
30 has geometry =>
31 (
32 default => sub {return '2x2+0+0'},
33 is => 'rw',
34 isa => Str,
35 required => 0,
36 );
37  
38 has geometry_set =>
39 (
40 default => sub {return [2, undef, 2, 0, 0]},
41 is => 'rw',
42 isa => ArrayRef,
43 required => 0,
44 );
45  
46 has output_dir =>
47 (
48 default => sub {return ''},
49 is => 'rw',
50 isa => Any,
51 required => 0,
52 );
53  
54 has output_type =>
55 (
56 default => sub {return 'png'},
57 is => 'rw',
58 isa => Str,
59 required => 0,
60 );
61  
62 has verbose =>
63 (
64 default => sub {return 0},
65 is => 'rw',
66 isa => Int,
67 required => 0,
68 );
69  
70 has write =>
71 (
72 default => sub {return 0},
73 is => 'rw',
74 isa => Int,
75 required => 0,
76 );
77  
78 our $VERSION = '2.00';
79  
80 # -----------------------------------------------
81  
82 sub BUILD
83 {
84 my($self) = @_;
85  
86 die "Error. You must call new as new(input_file => 'path/to/x.suffix')\n" if (! $self -> input_file);
87  
88 my($g) = $self -> geometry;
89 $g = ($g =~ /^\d+x\d+$/) ? "$g+0+0" : $g;
90  
91 my(@g);
92  
93 if ($g =~ /^(\d+)(x)(\d)([+-])(\d+)([+-])(\d+)$/)
94 {
95 @g = ($1, $2, $3, $4, $5, $6, $7);
96  
97 $self -> geometry("$g[0]$g[1]$g[2]$g[3]$g[4]$g[5]$g[6]");
98 $self -> geometry_set([$g[0], $g[1], $g[2], $g[3], $g[4], $g[5], $g[6] ]);
99  
100 if ($self -> verbose)
101 {
102 print "Image::Magick: V @{[$Image::Magick::VERSION || 'undef']}\n";
103 print "Image::Magick::Tiler: V $Image::Magick::Tiler::VERSION\n";
104 print "Geometry: $g parsed as NxM+x+y = " . $self -> geometry . "\n";
105 }
106 }
107 else
108 {
109 die "Error. Geometry (NxM+x+y = $g) is not in the correct format. \n";
110 }
111  
112 } # End of BUILD.
113  
114 # -----------------------------------------------
115  
116 sub tile
117 {
118 my($self) = @_;
119 my($image) = Image::Magick -> new();
120 my($result) = $image -> Read($self -> input_file);
121  
122 die "Error. Unable to read file $self -> input_file. Image::Magick error: $result\n" if ($result);
123  
124 my(@g) = @{$self -> geometry_set};
125 my($param) = {};
126 $$param{image} = {};
127 ($$param{image}{width}, $$param{image}{height}) = $image -> Get('width', 'height');
128 $$param{tile} = {};
129 $$param{tile}{width} = int($$param{image}{width} / $g[0]);
130 $$param{tile}{height} = int($$param{image}{height} / $g[2]);
131  
132 if ($self -> verbose)
133 {
134 print 'Image: ' . $self -> input_file . "\n";
135 print "Image size: ($$param{image}{width}, $$param{image}{height})\n";
136 print "Tile size: ($$param{tile}{width}, $$param{tile}{height}) (before applying x and y)\n";
137 }
138  
139 die "Error. Tile width ($$param{tile}{width}) < input x ($g[4]). \n" if ($$param{tile}{width} < abs($g[4]) );
140 die "Error. Tile height ($$param{tile}{height}) < input y ($g[6]). \n" if ($$param{tile}{height} < abs($g[6]) );
141  
142 $$param{tile}{width} += $g[4];
143 $$param{tile}{height} += $g[6];
144  
145 if ($self -> verbose)
146 {
147 print "Tile size: ($$param{tile}{width}, $$param{tile}{height}) (after applying x and y)\n";
148 }
149  
150 my($count) = 0;
151 my($output) = [];
152 my($x) = 0;
153  
154 my($y, $tile, $output_file_name);
155  
156 for my $xg (1 .. $g[0])
157 {
158 $y = 0;
159  
160 for my $yg (1 .. $g[2])
161 {
162 $count++;
163  
164 $output_file_name = "$yg-$xg." . $self -> output_type;
165 $output_file_name = File::Spec -> catfile($self -> output_dir, $output_file_name) if ($self -> output_dir);
166 $tile = $image -> Clone();
167  
168 die "Error. Unable to clone image $output_file_name\n" if (! ref $tile);
169  
170 $result = $tile -> Crop(x => $x, y => $y, width => $$param{tile}{width}, height => $$param{tile}{height});
171  
172 die "Error. Unable to crop image $output_file_name. Image::Magick error: $result\n" if ($result);
173  
174 push @{$output},
175 {
176 file_name => $output_file_name,
177 image => $tile,
178 };
179  
180 if ($self -> write)
181 {
182 $tile -> Write($output_file_name);
183  
184 if ($self -> verbose > 1)
185 {
186 print 'Wrote tile ' . sprintf('%4d', $count) . " $output_file_name\n";
187 }
188 }
189  
190 $y += $$param{tile}{height};
191 }
192  
193 $x += $$param{tile}{width};
194 }
195  
196 if ($self -> verbose)
197 {
198 print "Tile count: $count\n";
199 }
200  
201 $self -> count($count);
202  
203 return $output;
204  
205 } # End of tile.
206  
207 # -----------------------------------------------
208  
209 1;
210  
211 __END__
↑ Summary