Iterator.pm untested

filter: Statementstmt 0.0% 0 / 46 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 72 CC 6 SCAR 17.2
CC
6
Cov
0%
next
12.0
init
6.0
format
6.0
CRAP 5.6
1 =head1 NAME
2  
3 Image::Magick::Iterator - sequentially read Image::Magick object from
4 a filehandle.
5  
6 =head1 SYNOPSIS
7  
8 use strict;
9 use Image::Magick::Iterator;
10  
11 my $iter = Image::Magick::Iterator->new();
12  
13 #assume PPM stream is coming from STDIN;
14 $iter->handle(\*STDIN);
15  
16 #explicitly set format to PPM, there is no auto-detection built in
17 $iter->format('PPM');
18  
19 while(my $image = $iter->next){
20 print $image->Get('height'),"\n"; #access height attribute of each
21 #Image::Magick object
22 }
23  
24 =head1 DESCRIPTION
25  
26 Image::Magick::Iterator adds iteration support to
27 L<Image::Magick|Image::Magick>. This means that if you have a stream
28 of concatenated images, you can access each image in the stream as an
29 independent L<Image::Magick|Image::Magick> object.
30  
31 Iteration functionality is not present in
32 L<Image::Magick|Image::Magick> itself as of version 5.56. Passing a
33 stream of concatenated images would result in essentially a "stack" of
34 images which would all be manipulated in parallel by any
35 L<Image::Magick|Image::Magick> calls. Calls to Write() either output
36 an animated series of image (a la animated GIFs), or the first image
37 in the series.
38  
39 Image::Magick::Iterator is extensible to support many different image
40 filetypes. Currently only PPM support is implemented. See
41 L</SYNOPSIS|SYOPSIS> for an example.
42  
43 =head1 SUPPORTED FORMATS
44  
45 Currently only PPM images can be iterated. It's not difficult to add
46 new image types though, and I'm receptive to new classes for handling
47 more formats. To add another format:
48  
49 1. Have a look at the source of
50 L<Image::Magick::Iterator::PPM|Image::Magick::Iterator::PPM> to get an
51 idea of how to write a new format handling class. It is basically a
52 class with one method, B<read_image>, that when given a filehandle
53 reference reads an image from it and passes back the raw data.
54  
55 2. add a mapping to L</_delegate()> that maps the desired value of
56 L</format()> to your image reading class.
57  
58 =head1 FEEDBACK
59  
60 Email the author.
61  
62 =head1 AUTHOR
63  
64 Allen Day E<lt>allenday@ucla.eduE<gt>
65  
66 =head1 COPYRIGHT AND LICENSE
67  
68 Copyright (C) 2004 by Allen Day, allenday@ucla.edu
69  
70 This library is released under GPL, the GNU General Public License
71  
72 =head1 APPENDIX
73  
74 The rest of the documentation details each of the object methods.
75 Internal methods are usually preceded with a '_'. Methods are
76 in alphabetical order for the most part.
77  
78 =cut
79  
80  
81 # Let the code begin...
82  
83  
84 package Image::Magick::Iterator;
85 use strict;
86 use base qw();
87 our $VERSION = '0.01';
88  
89 use File::Temp;
90 use Image::Magick;
91  
92 =head2 new()
93  
94 =over
95  
96 =item Usage
97  
98 my $obj = new Image::Magick::Iterator();
99  
100 =item Function
101  
102 Builds a new Image::Magick::Iterator object
103  
104 =item Returns
105  
106 an instance of Image::Magick::Iterator
107  
108 =item Arguments
109  
110 None
111  
112 =back
113  
114 =cut
115  
116 sub new {
117 my($class,%arg) = @_;
118  
119 my $self = bless {}, $class;
120 $self->init(%arg);
121  
122 return $self;
123 }
124  
125 =head2 init()
126  
127 =over
128  
129 =item Usage
130  
131 $obj->init(%arg);
132  
133 =item Function
134  
135 Internal method to initialize a new Image::Magick::Iterator object
136  
137 =item Returns
138  
139 true on success
140  
141 =item Arguments
142  
143 Arguments passed to L</new()>
144  
145 =back
146  
147 =cut
148  
149 sub init {
150 my($self,%arg) = @_;
151  
152 foreach my $arg (keys %arg){
153 $self->$arg($arg{$arg}) if $self->can($arg);
154 }
155  
156 return 1;
157 }
158  
159 =head2 format()
160  
161 =over
162  
163 =item Usage
164  
165 $obj->format(); #get existing value
166  
167 $obj->format($newval); #set new value
168  
169 =item Function
170  
171 stores a scalar value of the fileformat to be read from
172 L</handle()>. currently supported formats are:
173  
174 * PPM
175  
176 =item Returns
177  
178 value of format (a scalar)
179  
180 =item Arguments
181  
182 (optional) on set, a scalar
183  
184 =back
185  
186 =cut
187  
188 sub format {
189 my($self,$val) = @_;
190 $self->{'format'} = $val if defined($val);
191 return $self->{'format'};
192 }
193  
194 =head2 handle()
195  
196 =over
197  
198 =item Usage
199  
200 $obj->handle(); #get existing value
201  
202 $obj->handle($newval); #set new value
203  
204 =item Function
205  
206 stores a filehandle reference (eg \*STDIN, or an
207 L<IO::Handle|IO::Handle>.
208  
209 =item Returns
210  
211 value of handle (a scalar)
212  
213 =item Arguments
214  
215 (optional) on set, a scalar
216  
217 =back
218  
219 =cut
220  
221 sub handle {
222 my($self,$val) = @_;
223 $self->{'handle'} = $val if defined($val);
224 return $self->{'handle'};
225 }
226  
227 =head2 next()
228  
229 =over
230  
231 =item Usage
232  
233 $obj->next(); #get next Image::Magick from stream
234  
235 =item Function
236  
237 reads an L<Image::Magick|Image::Magick> object from a filehandle.
238  
239 =item Returns
240  
241 a L<Image::Magick|Image::Magick> object, or undef if the filehandle is
242 EOF or contains only a partial image.
243  
244 =item Arguments
245  
246 none, read-only
247  
248 =back
249  
250 =cut
251  
252 sub next {
253 my $self = shift;
254  
255 my $image = undef;
256  
257 my $delegate = $self->_delegate($self->format());
258  
259 eval "require $delegate";
260 if($@){ die "couldn't load delegate class '$delegate': $@" }
261  
262 my $raw = $delegate->read_image($self->handle);
263  
264 return undef unless $raw;
265  
266 my $tmp = File::Temp->new(UNLINK => 1);
267 print $tmp $raw;
268  
269 $image = Image::Magick->new();
270  
271 close($tmp);
272  
273 open(IN,"$tmp");
274 $image->Read(file => \*IN);
275 close(IN);
276  
277 return $image;
278 }
279  
280 =head2 _delegate()
281  
282 =over
283  
284 =item Usage
285  
286 $obj->_delegate($format);
287  
288 =item Function
289  
290 internal method, maps format names to class names
291  
292 =item Returns
293  
294 class to be delegated to for reading an image of the specified format
295  
296 =item Arguments
297  
298 name of an image format
299  
300 =back
301  
302 =cut
303  
304 sub _delegate {
305 my ($self,$format) = @_;
306  
307 my %map = (
308 PPM => 'Image::Magick::Iterator::PPM',
309 );
310  
311 return $map{$format};
312 }
313  
314  
315 1;