Commit | Line | Data |
---|---|---|
02d1d628 AMH |
1 | package Imager; |
2 | ||
02d1d628 | 3 | use strict; |
50c75381 | 4 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS %formats $DEBUG %filters %DSOs $ERRSTR %OPCODES $I2P $FORMATGUESS $warn_obsolete); |
02d1d628 AMH |
5 | use IO::File; |
6 | ||
7 | use Imager::Color; | |
8 | use Imager::Font; | |
9 | ||
10 | @EXPORT_OK = qw( | |
11 | init | |
12 | init_log | |
13 | DSO_open | |
14 | DSO_close | |
15 | DSO_funclist | |
16 | DSO_call | |
dd55acc8 | 17 | |
02d1d628 AMH |
18 | load_plugin |
19 | unload_plugin | |
dd55acc8 | 20 | |
02d1d628 | 21 | i_list_formats |
dd55acc8 | 22 | |
02d1d628 AMH |
23 | i_color_new |
24 | i_color_set | |
25 | i_color_info | |
dd55acc8 | 26 | |
02d1d628 AMH |
27 | i_img_empty |
28 | i_img_empty_ch | |
29 | i_img_exorcise | |
30 | i_img_destroy | |
31 | ||
32 | i_img_info | |
33 | ||
34 | i_img_setmask | |
35 | i_img_getmask | |
36 | ||
aa833c97 | 37 | i_line |
02d1d628 AMH |
38 | i_line_aa |
39 | i_box | |
40 | i_box_filled | |
41 | i_arc | |
18063344 | 42 | i_circle_aa |
dd55acc8 | 43 | |
02d1d628 AMH |
44 | i_bezier_multi |
45 | i_poly_aa | |
43c5dacb | 46 | i_poly_aa_cfill |
02d1d628 AMH |
47 | |
48 | i_copyto | |
49 | i_rubthru | |
50 | i_scaleaxis | |
51 | i_scale_nn | |
52 | i_haar | |
53 | i_count_colors | |
dd55acc8 | 54 | |
02d1d628 AMH |
55 | i_gaussian |
56 | i_conv | |
dd55acc8 | 57 | |
f5991c03 | 58 | i_convert |
40eba1ea | 59 | i_map |
dd55acc8 | 60 | |
02d1d628 AMH |
61 | i_img_diff |
62 | ||
02d1d628 AMH |
63 | i_tt_set_aa |
64 | i_tt_cp | |
65 | i_tt_text | |
66 | i_tt_bbox | |
67 | ||
02d1d628 | 68 | i_readpnm_wiol |
067d6bdc | 69 | i_writeppm_wiol |
02d1d628 | 70 | |
895dbd34 AMH |
71 | i_readraw_wiol |
72 | i_writeraw_wiol | |
02d1d628 AMH |
73 | |
74 | i_contrast | |
75 | i_hardinvert | |
76 | i_noise | |
77 | i_bumpmap | |
78 | i_postlevels | |
79 | i_mosaic | |
80 | i_watermark | |
dd55acc8 | 81 | |
02d1d628 AMH |
82 | malloc_state |
83 | ||
84 | list_formats | |
dd55acc8 | 85 | |
02d1d628 AMH |
86 | i_gifquant |
87 | ||
88 | newfont | |
89 | newcolor | |
90 | newcolour | |
91 | NC | |
92 | NF | |
bd8052a6 | 93 | NCF |
02d1d628 AMH |
94 | ); |
95 | ||
9982a307 | 96 | @EXPORT=qw( |
02d1d628 AMH |
97 | ); |
98 | ||
99 | %EXPORT_TAGS= | |
100 | (handy => [qw( | |
101 | newfont | |
102 | newcolor | |
103 | NF | |
104 | NC | |
bd8052a6 | 105 | NCF |
02d1d628 AMH |
106 | )], |
107 | all => [@EXPORT_OK], | |
108 | default => [qw( | |
109 | load_plugin | |
110 | unload_plugin | |
111 | )]); | |
112 | ||
53a6bbd4 TC |
113 | # registered file readers |
114 | my %readers; | |
115 | ||
2b405c9e TC |
116 | # registered file writers |
117 | my %writers; | |
118 | ||
53a6bbd4 TC |
119 | # modules we attempted to autoload |
120 | my %attempted_to_load; | |
121 | ||
5970bd39 TC |
122 | # errors from loading files |
123 | my %file_load_errors; | |
124 | ||
125 | # what happened when we tried to load | |
126 | my %reader_load_errors; | |
127 | my %writer_load_errors; | |
128 | ||
f245645a TC |
129 | # library keys that are image file formats |
130 | my %file_formats = map { $_ => 1 } qw/tiff pnm gif png jpeg raw bmp tga/; | |
131 | ||
9b1ec2b8 TC |
132 | # image pixel combine types |
133 | my @combine_types = | |
134 | qw/none normal multiply dissolve add subtract diff lighten darken | |
135 | hue saturation value color/; | |
136 | my %combine_types; | |
137 | @combine_types{@combine_types} = 0 .. $#combine_types; | |
138 | $combine_types{mult} = $combine_types{multiply}; | |
139 | $combine_types{'sub'} = $combine_types{subtract}; | |
140 | $combine_types{sat} = $combine_types{saturation}; | |
141 | ||
142 | # this will be used to store global defaults at some point | |
143 | my %defaults; | |
144 | ||
02d1d628 AMH |
145 | BEGIN { |
146 | require Exporter; | |
f10bedbb TC |
147 | my $ex_version = eval $Exporter::VERSION; |
148 | if ($ex_version < 5.57) { | |
b1e66a82 TC |
149 | @ISA = qw(Exporter); |
150 | } | |
4b2e59d3 | 151 | $VERSION = '0.86'; |
92bda632 TC |
152 | eval { |
153 | require XSLoader; | |
154 | XSLoader::load(Imager => $VERSION); | |
155 | 1; | |
156 | } or do { | |
157 | require DynaLoader; | |
158 | push @ISA, 'DynaLoader'; | |
159 | bootstrap Imager $VERSION; | |
160 | } | |
02d1d628 AMH |
161 | } |
162 | ||
1d7e3124 TC |
163 | my %formats_low; |
164 | my %format_classes = | |
165 | ( | |
166 | png => "Imager::File::PNG", | |
167 | gif => "Imager::File::GIF", | |
168 | tiff => "Imager::File::TIFF", | |
169 | jpeg => "Imager::File::JPEG", | |
718b8c97 | 170 | w32 => "Imager::Font::W32", |
50c75381 | 171 | ft2 => "Imager::Font::FT2", |
a556912d | 172 | t1 => "Imager::Font::T1", |
1d7e3124 TC |
173 | ); |
174 | ||
175 | tie %formats, "Imager::FORMATS", \%formats_low, \%format_classes; | |
176 | ||
02d1d628 | 177 | BEGIN { |
1d7e3124 | 178 | for(i_list_formats()) { $formats_low{$_}++; } |
02d1d628 | 179 | |
02d1d628 AMH |
180 | %OPCODES=(Add=>[0],Sub=>[1],Mult=>[2],Div=>[3],Parm=>[4],'sin'=>[5],'cos'=>[6],'x'=>[4,0],'y'=>[4,1]); |
181 | ||
182 | $DEBUG=0; | |
183 | ||
6607600c TC |
184 | # the members of the subhashes under %filters are: |
185 | # callseq - a list of the parameters to the underlying filter in the | |
186 | # order they are passed | |
187 | # callsub - a code ref that takes a named parameter list and calls the | |
188 | # underlying filter | |
189 | # defaults - a hash of default values | |
190 | # names - defines names for value of given parameters so if the names | |
191 | # field is foo=> { bar=>1 }, and the user supplies "bar" as the | |
192 | # foo parameter, the filter will receive 1 for the foo | |
193 | # parameter | |
02d1d628 AMH |
194 | $filters{contrast}={ |
195 | callseq => ['image','intensity'], | |
196 | callsub => sub { my %hsh=@_; i_contrast($hsh{image},$hsh{intensity}); } | |
197 | }; | |
198 | ||
199 | $filters{noise} ={ | |
200 | callseq => ['image', 'amount', 'subtype'], | |
201 | defaults => { amount=>3,subtype=>0 }, | |
202 | callsub => sub { my %hsh=@_; i_noise($hsh{image},$hsh{amount},$hsh{subtype}); } | |
203 | }; | |
204 | ||
205 | $filters{hardinvert} ={ | |
206 | callseq => ['image'], | |
207 | defaults => { }, | |
208 | callsub => sub { my %hsh=@_; i_hardinvert($hsh{image}); } | |
209 | }; | |
210 | ||
5558f899 TC |
211 | $filters{hardinvertall} = |
212 | { | |
213 | callseq => ['image'], | |
214 | defaults => { }, | |
215 | callsub => sub { my %hsh=@_; i_hardinvertall($hsh{image}); } | |
216 | }; | |
217 | ||
02d1d628 AMH |
218 | $filters{autolevels} ={ |
219 | callseq => ['image','lsat','usat','skew'], | |
220 | defaults => { lsat=>0.1,usat=>0.1,skew=>0.0 }, | |
221 | callsub => sub { my %hsh=@_; i_autolevels($hsh{image},$hsh{lsat},$hsh{usat},$hsh{skew}); } | |
222 | }; | |
223 | ||
224 | $filters{turbnoise} ={ | |
225 | callseq => ['image'], | |
226 | defaults => { xo=>0.0,yo=>0.0,scale=>10.0 }, | |
227 | callsub => sub { my %hsh=@_; i_turbnoise($hsh{image},$hsh{xo},$hsh{yo},$hsh{scale}); } | |
228 | }; | |
229 | ||
230 | $filters{radnoise} ={ | |
231 | callseq => ['image'], | |
232 | defaults => { xo=>100,yo=>100,ascale=>17.0,rscale=>0.02 }, | |
233 | callsub => sub { my %hsh=@_; i_radnoise($hsh{image},$hsh{xo},$hsh{yo},$hsh{rscale},$hsh{ascale}); } | |
234 | }; | |
235 | ||
6a3cbaef TC |
236 | $filters{conv} = |
237 | { | |
238 | callseq => ['image', 'coef'], | |
239 | defaults => { }, | |
240 | callsub => | |
241 | sub { | |
242 | my %hsh=@_; | |
243 | i_conv($hsh{image},$hsh{coef}) | |
244 | or die Imager->_error_as_msg() . "\n"; | |
245 | } | |
246 | }; | |
02d1d628 | 247 | |
f0ddaffd TC |
248 | $filters{gradgen} = |
249 | { | |
250 | callseq => ['image', 'xo', 'yo', 'colors', 'dist'], | |
251 | defaults => { dist => 0 }, | |
252 | callsub => | |
253 | sub { | |
254 | my %hsh=@_; | |
255 | my @colors = @{$hsh{colors}}; | |
256 | $_ = _color($_) | |
257 | for @colors; | |
258 | i_gradgen($hsh{image}, $hsh{xo}, $hsh{yo}, \@colors, $hsh{dist}); | |
259 | } | |
260 | }; | |
02d1d628 | 261 | |
e310e5f9 TC |
262 | $filters{nearest_color} = |
263 | { | |
264 | callseq => ['image', 'xo', 'yo', 'colors', 'dist'], | |
265 | defaults => { }, | |
266 | callsub => | |
267 | sub { | |
268 | my %hsh=@_; | |
269 | # make sure the segments are specified with colors | |
270 | my @colors; | |
271 | for my $color (@{$hsh{colors}}) { | |
272 | my $new_color = _color($color) | |
273 | or die $Imager::ERRSTR."\n"; | |
274 | push @colors, $new_color; | |
275 | } | |
276 | ||
277 | i_nearest_color($hsh{image}, $hsh{xo}, $hsh{yo}, \@colors, | |
278 | $hsh{dist}) | |
279 | or die Imager->_error_as_msg() . "\n"; | |
280 | }, | |
281 | }; | |
faa9b3e7 TC |
282 | $filters{gaussian} = { |
283 | callseq => [ 'image', 'stddev' ], | |
284 | defaults => { }, | |
285 | callsub => sub { my %hsh = @_; i_gaussian($hsh{image}, $hsh{stddev}); }, | |
286 | }; | |
d08b8f85 TC |
287 | $filters{mosaic} = |
288 | { | |
289 | callseq => [ qw(image size) ], | |
290 | defaults => { size => 20 }, | |
291 | callsub => sub { my %hsh = @_; i_mosaic($hsh{image}, $hsh{size}) }, | |
292 | }; | |
293 | $filters{bumpmap} = | |
294 | { | |
295 | callseq => [ qw(image bump elevation lightx lighty st) ], | |
296 | defaults => { elevation=>0, st=> 2 }, | |
b2778574 | 297 | callsub => sub { |
d08b8f85 TC |
298 | my %hsh = @_; |
299 | i_bumpmap($hsh{image}, $hsh{bump}{IMG}, $hsh{elevation}, | |
300 | $hsh{lightx}, $hsh{lighty}, $hsh{st}); | |
301 | }, | |
302 | }; | |
b2778574 AMH |
303 | $filters{bumpmap_complex} = |
304 | { | |
305 | callseq => [ qw(image bump channel tx ty Lx Ly Lz cd cs n Ia Il Is) ], | |
306 | defaults => { | |
307 | channel => 0, | |
308 | tx => 0, | |
309 | ty => 0, | |
310 | Lx => 0.2, | |
311 | Ly => 0.4, | |
312 | Lz => -1.0, | |
313 | cd => 1.0, | |
314 | cs => 40, | |
315 | n => 1.3, | |
ffddd407 TC |
316 | Ia => [0,0,0], |
317 | Il => [255,255,255], | |
318 | Is => [255,255,255], | |
b2778574 AMH |
319 | }, |
320 | callsub => sub { | |
321 | my %hsh = @_; | |
ffddd407 TC |
322 | for my $cname (qw/Ia Il Is/) { |
323 | my $old = $hsh{$cname}; | |
324 | my $new_color = _color($old) | |
325 | or die $Imager::ERRSTR, "\n"; | |
326 | $hsh{$cname} = $new_color; | |
327 | } | |
b2778574 AMH |
328 | i_bumpmap_complex($hsh{image}, $hsh{bump}{IMG}, $hsh{channel}, |
329 | $hsh{tx}, $hsh{ty}, $hsh{Lx}, $hsh{Ly}, $hsh{Lz}, | |
330 | $hsh{cd}, $hsh{cs}, $hsh{n}, $hsh{Ia}, $hsh{Il}, | |
331 | $hsh{Is}); | |
332 | }, | |
333 | }; | |
d08b8f85 TC |
334 | $filters{postlevels} = |
335 | { | |
336 | callseq => [ qw(image levels) ], | |
337 | defaults => { levels => 10 }, | |
338 | callsub => sub { my %hsh = @_; i_postlevels($hsh{image}, $hsh{levels}); }, | |
339 | }; | |
340 | $filters{watermark} = | |
341 | { | |
342 | callseq => [ qw(image wmark tx ty pixdiff) ], | |
343 | defaults => { pixdiff=>10, tx=>0, ty=>0 }, | |
344 | callsub => | |
345 | sub { | |
346 | my %hsh = @_; | |
347 | i_watermark($hsh{image}, $hsh{wmark}{IMG}, $hsh{tx}, $hsh{ty}, | |
348 | $hsh{pixdiff}); | |
349 | }, | |
350 | }; | |
6607600c TC |
351 | $filters{fountain} = |
352 | { | |
353 | callseq => [ qw(image xa ya xb yb ftype repeat combine super_sample ssample_param segments) ], | |
354 | names => { | |
355 | ftype => { linear => 0, | |
356 | bilinear => 1, | |
357 | radial => 2, | |
358 | radial_square => 3, | |
359 | revolution => 4, | |
360 | conical => 5 }, | |
361 | repeat => { none => 0, | |
362 | sawtooth => 1, | |
363 | triangle => 2, | |
364 | saw_both => 3, | |
365 | tri_both => 4, | |
366 | }, | |
367 | super_sample => { | |
368 | none => 0, | |
369 | grid => 1, | |
370 | random => 2, | |
371 | circle => 3, | |
372 | }, | |
efdc2568 TC |
373 | combine => { |
374 | none => 0, | |
375 | normal => 1, | |
376 | multiply => 2, mult => 2, | |
377 | dissolve => 3, | |
378 | add => 4, | |
9d540150 | 379 | subtract => 5, 'sub' => 5, |
efdc2568 TC |
380 | diff => 6, |
381 | lighten => 7, | |
382 | darken => 8, | |
383 | hue => 9, | |
384 | sat => 10, | |
385 | value => 11, | |
386 | color => 12, | |
387 | }, | |
6607600c TC |
388 | }, |
389 | defaults => { ftype => 0, repeat => 0, combine => 0, | |
390 | super_sample => 0, ssample_param => 4, | |
391 | segments=>[ | |
392 | [ 0, 0.5, 1, | |
ffddd407 TC |
393 | [0,0,0], |
394 | [255, 255, 255], | |
6607600c TC |
395 | 0, 0, |
396 | ], | |
397 | ], | |
398 | }, | |
399 | callsub => | |
400 | sub { | |
401 | my %hsh = @_; | |
109bec2d TC |
402 | |
403 | # make sure the segments are specified with colors | |
404 | my @segments; | |
405 | for my $segment (@{$hsh{segments}}) { | |
406 | my @new_segment = @$segment; | |
407 | ||
408 | $_ = _color($_) or die $Imager::ERRSTR."\n" for @new_segment[3,4]; | |
409 | push @segments, \@new_segment; | |
410 | } | |
411 | ||
6607600c TC |
412 | i_fountain($hsh{image}, $hsh{xa}, $hsh{ya}, $hsh{xb}, $hsh{yb}, |
413 | $hsh{ftype}, $hsh{repeat}, $hsh{combine}, $hsh{super_sample}, | |
e310e5f9 TC |
414 | $hsh{ssample_param}, \@segments) |
415 | or die Imager->_error_as_msg() . "\n"; | |
6607600c TC |
416 | }, |
417 | }; | |
b6381851 TC |
418 | $filters{unsharpmask} = |
419 | { | |
420 | callseq => [ qw(image stddev scale) ], | |
421 | defaults => { stddev=>2.0, scale=>1.0 }, | |
422 | callsub => | |
423 | sub { | |
424 | my %hsh = @_; | |
425 | i_unsharp_mask($hsh{image}, $hsh{stddev}, $hsh{scale}); | |
426 | }, | |
427 | }; | |
02d1d628 AMH |
428 | |
429 | $FORMATGUESS=\&def_guess_type; | |
97c4effc TC |
430 | |
431 | $warn_obsolete = 1; | |
02d1d628 AMH |
432 | } |
433 | ||
434 | # | |
435 | # Non methods | |
436 | # | |
437 | ||
438 | # initlize Imager | |
439 | # NOTE: this might be moved to an import override later on | |
440 | ||
bd8052a6 TC |
441 | sub import { |
442 | my $i = 1; | |
443 | while ($i < @_) { | |
444 | if ($_[$i] eq '-log-stderr') { | |
445 | init_log(undef, 4); | |
446 | splice(@_, $i, 1); | |
447 | } | |
448 | else { | |
449 | ++$i; | |
450 | } | |
451 | } | |
452 | goto &Exporter::import; | |
453 | } | |
02d1d628 | 454 | |
f83bf98a | 455 | sub init_log { |
10ea52a3 | 456 | Imager->open_log(log => $_[0], level => $_[1]); |
f83bf98a AMH |
457 | } |
458 | ||
459 | ||
02d1d628 AMH |
460 | sub init { |
461 | my %parms=(loglevel=>1,@_); | |
f83bf98a | 462 | |
97c4effc TC |
463 | if (exists $parms{'warn_obsolete'}) { |
464 | $warn_obsolete = $parms{'warn_obsolete'}; | |
465 | } | |
02d1d628 | 466 | |
a3fd7df7 TC |
467 | if ($parms{'log'}) { |
468 | Imager->open_log(log => $parms{log}, level => $parms{loglevel}) | |
469 | or return; | |
470 | } | |
471 | ||
4cb58f1b | 472 | if (exists $parms{'t1log'}) { |
a556912d | 473 | if ($formats{t1}) { |
a3fd7df7 TC |
474 | if (Imager::Font::T1::i_init_t1($parms{'t1log'})) { |
475 | Imager->_set_error(Imager->_error_as_msg); | |
476 | return; | |
477 | } | |
a556912d | 478 | } |
4cb58f1b | 479 | } |
a3fd7df7 TC |
480 | |
481 | return 1; | |
02d1d628 AMH |
482 | } |
483 | ||
10ea52a3 TC |
484 | { |
485 | my $is_logging = 0; | |
486 | ||
487 | sub open_log { | |
488 | my $class = shift; | |
489 | my (%opts) = ( loglevel => 1, @_ ); | |
490 | ||
491 | $is_logging = i_init_log($opts{log}, $opts{loglevel}); | |
492 | unless ($is_logging) { | |
493 | Imager->_set_error(Imager->_error_as_msg()); | |
494 | return; | |
495 | } | |
496 | ||
497 | Imager->log("Imager $VERSION starting\n", 1); | |
498 | ||
499 | return $is_logging; | |
500 | } | |
501 | ||
502 | sub close_log { | |
503 | i_init_log(undef, -1); | |
504 | $is_logging = 0; | |
505 | } | |
506 | ||
507 | sub log { | |
508 | my ($class, $message, $level) = @_; | |
509 | ||
510 | defined $level or $level = 1; | |
511 | ||
512 | i_log_entry($message, $level); | |
513 | } | |
514 | ||
515 | sub is_logging { | |
516 | return $is_logging; | |
517 | } | |
518 | } | |
519 | ||
02d1d628 AMH |
520 | END { |
521 | if ($DEBUG) { | |
522 | print "shutdown code\n"; | |
523 | # for(keys %instances) { $instances{$_}->DESTROY(); } | |
524 | malloc_state(); # how do decide if this should be used? -- store something from the import | |
525 | print "Imager exiting\n"; | |
526 | } | |
527 | } | |
528 | ||
529 | # Load a filter plugin | |
530 | ||
531 | sub load_plugin { | |
532 | my ($filename)=@_; | |
533 | my $i; | |
534 | my ($DSO_handle,$str)=DSO_open($filename); | |
535 | if (!defined($DSO_handle)) { $Imager::ERRSTR="Couldn't load plugin '$filename'\n"; return undef; } | |
536 | my %funcs=DSO_funclist($DSO_handle); | |
537 | if ($DEBUG) { print "loading module $filename\n"; $i=0; for(keys %funcs) { printf(" %2d: %s\n",$i++,$_); } } | |
538 | $i=0; | |
539 | for(keys %funcs) { if ($filters{$_}) { $ERRSTR="filter '$_' already exists\n"; DSO_close($DSO_handle); return undef; } } | |
540 | ||
541 | $DSOs{$filename}=[$DSO_handle,\%funcs]; | |
542 | ||
543 | for(keys %funcs) { | |
544 | my $evstr="\$filters{'".$_."'}={".$funcs{$_}.'};'; | |
545 | $DEBUG && print "eval string:\n",$evstr,"\n"; | |
546 | eval $evstr; | |
547 | print $@ if $@; | |
548 | } | |
549 | return 1; | |
550 | } | |
551 | ||
552 | # Unload a plugin | |
553 | ||
554 | sub unload_plugin { | |
555 | my ($filename)=@_; | |
556 | ||
557 | if (!$DSOs{$filename}) { $ERRSTR="plugin '$filename' not loaded."; return undef; } | |
558 | my ($DSO_handle,$funcref)=@{$DSOs{$filename}}; | |
559 | for(keys %{$funcref}) { | |
560 | delete $filters{$_}; | |
561 | $DEBUG && print "unloading: $_\n"; | |
562 | } | |
563 | my $rc=DSO_close($DSO_handle); | |
564 | if (!defined($rc)) { $ERRSTR="unable to unload plugin '$filename'."; return undef; } | |
565 | return 1; | |
566 | } | |
567 | ||
64606cc7 TC |
568 | # take the results of i_error() and make a message out of it |
569 | sub _error_as_msg { | |
570 | return join(": ", map $_->[0], i_errors()); | |
571 | } | |
572 | ||
3a9a4241 TC |
573 | # this function tries to DWIM for color parameters |
574 | # color objects are used as is | |
575 | # simple scalars are simply treated as single parameters to Imager::Color->new | |
576 | # hashrefs are treated as named argument lists to Imager::Color->new | |
577 | # arrayrefs are treated as list arguments to Imager::Color->new iff any | |
578 | # parameter is > 1 | |
579 | # other arrayrefs are treated as list arguments to Imager::Color::Float | |
580 | ||
581 | sub _color { | |
582 | my $arg = shift; | |
b6cfd214 TC |
583 | # perl 5.6.0 seems to do weird things to $arg if we don't make an |
584 | # explicitly stringified copy | |
585 | # I vaguely remember a bug on this on p5p, but couldn't find it | |
586 | # through bugs.perl.org (I had trouble getting it to find any bugs) | |
587 | my $copy = $arg . ""; | |
3a9a4241 TC |
588 | my $result; |
589 | ||
590 | if (ref $arg) { | |
591 | if (UNIVERSAL::isa($arg, "Imager::Color") | |
592 | || UNIVERSAL::isa($arg, "Imager::Color::Float")) { | |
593 | $result = $arg; | |
594 | } | |
595 | else { | |
b6cfd214 | 596 | if ($copy =~ /^HASH\(/) { |
3a9a4241 TC |
597 | $result = Imager::Color->new(%$arg); |
598 | } | |
b6cfd214 | 599 | elsif ($copy =~ /^ARRAY\(/) { |
5daa8f70 | 600 | $result = Imager::Color->new(@$arg); |
3a9a4241 TC |
601 | } |
602 | else { | |
603 | $Imager::ERRSTR = "Not a color"; | |
604 | } | |
605 | } | |
606 | } | |
607 | else { | |
608 | # assume Imager::Color::new knows how to handle it | |
609 | $result = Imager::Color->new($arg); | |
610 | } | |
611 | ||
612 | return $result; | |
613 | } | |
614 | ||
9b1ec2b8 TC |
615 | sub _combine { |
616 | my ($self, $combine, $default) = @_; | |
617 | ||
618 | if (!defined $combine && ref $self) { | |
619 | $combine = $self->{combine}; | |
620 | } | |
621 | defined $combine or $combine = $defaults{combine}; | |
622 | defined $combine or $combine = $default; | |
623 | ||
624 | if (exists $combine_types{$combine}) { | |
625 | $combine = $combine_types{$combine}; | |
626 | } | |
627 | ||
628 | return $combine; | |
629 | } | |
630 | ||
4cda4e76 TC |
631 | sub _valid_image { |
632 | my ($self) = @_; | |
633 | ||
634 | $self->{IMG} and return 1; | |
635 | ||
636 | $self->_set_error('empty input image'); | |
637 | ||
638 | return; | |
639 | } | |
3a9a4241 | 640 | |
500888da TC |
641 | # returns first defined parameter |
642 | sub _first { | |
643 | for (@_) { | |
644 | return $_ if defined $_; | |
645 | } | |
646 | return undef; | |
647 | } | |
648 | ||
02d1d628 AMH |
649 | # |
650 | # Methods to be called on objects. | |
651 | # | |
652 | ||
653 | # Create a new Imager object takes very few parameters. | |
654 | # usually you call this method and then call open from | |
655 | # the resulting object | |
656 | ||
657 | sub new { | |
658 | my $class = shift; | |
659 | my $self ={}; | |
660 | my %hsh=@_; | |
661 | bless $self,$class; | |
662 | $self->{IMG}=undef; # Just to indicate what exists | |
663 | $self->{ERRSTR}=undef; # | |
664 | $self->{DEBUG}=$DEBUG; | |
3c252111 TC |
665 | $self->{DEBUG} and print "Initialized Imager\n"; |
666 | if (defined $hsh{xsize} || defined $hsh{ysize}) { | |
1501d9b3 TC |
667 | unless ($self->img_set(%hsh)) { |
668 | $Imager::ERRSTR = $self->{ERRSTR}; | |
669 | return; | |
670 | } | |
671 | } | |
3c252111 TC |
672 | elsif (defined $hsh{file} || |
673 | defined $hsh{fh} || | |
674 | defined $hsh{fd} || | |
675 | defined $hsh{callback} || | |
75812841 TC |
676 | defined $hsh{readcb} || |
677 | defined $hsh{data}) { | |
3c252111 TC |
678 | # allow $img = Imager->new(file => $filename) |
679 | my %extras; | |
680 | ||
681 | # type is already used as a parameter to new(), rename it for the | |
682 | # call to read() | |
683 | if ($hsh{filetype}) { | |
684 | $extras{type} = $hsh{filetype}; | |
685 | } | |
686 | unless ($self->read(%hsh, %extras)) { | |
687 | $Imager::ERRSTR = $self->{ERRSTR}; | |
688 | return; | |
689 | } | |
690 | } | |
691 | ||
02d1d628 AMH |
692 | return $self; |
693 | } | |
694 | ||
02d1d628 AMH |
695 | # Copy an entire image with no changes |
696 | # - if an image has magic the copy of it will not be magical | |
697 | ||
698 | sub copy { | |
699 | my $self = shift; | |
700 | unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } | |
701 | ||
34b3f7e6 TC |
702 | unless (defined wantarray) { |
703 | my @caller = caller; | |
704 | warn "copy() called in void context - copy() returns the copied image at $caller[1] line $caller[2]\n"; | |
705 | return; | |
706 | } | |
707 | ||
02d1d628 | 708 | my $newcopy=Imager->new(); |
92bda632 | 709 | $newcopy->{IMG} = i_copy($self->{IMG}); |
02d1d628 AMH |
710 | return $newcopy; |
711 | } | |
712 | ||
713 | # Paste a region | |
714 | ||
715 | sub paste { | |
716 | my $self = shift; | |
92bda632 TC |
717 | |
718 | unless ($self->{IMG}) { | |
719 | $self->_set_error('empty input image'); | |
720 | return; | |
721 | } | |
722 | my %input=(left=>0, top=>0, src_minx => 0, src_miny => 0, @_); | |
723 | my $src = $input{img} || $input{src}; | |
724 | unless($src) { | |
725 | $self->_set_error("no source image"); | |
02d1d628 AMH |
726 | return; |
727 | } | |
728 | $input{left}=0 if $input{left} <= 0; | |
729 | $input{top}=0 if $input{top} <= 0; | |
92bda632 | 730 | |
02d1d628 | 731 | my($r,$b)=i_img_info($src->{IMG}); |
92bda632 TC |
732 | my ($src_left, $src_top) = @input{qw/src_minx src_miny/}; |
733 | my ($src_right, $src_bottom); | |
734 | if ($input{src_coords}) { | |
735 | ($src_left, $src_top, $src_right, $src_bottom) = @{$input{src_coords}} | |
736 | } | |
737 | else { | |
738 | if (defined $input{src_maxx}) { | |
739 | $src_right = $input{src_maxx}; | |
740 | } | |
741 | elsif (defined $input{width}) { | |
742 | if ($input{width} <= 0) { | |
743 | $self->_set_error("paste: width must me positive"); | |
744 | return; | |
745 | } | |
746 | $src_right = $src_left + $input{width}; | |
747 | } | |
748 | else { | |
749 | $src_right = $r; | |
750 | } | |
35029411 | 751 | if (defined $input{src_maxy}) { |
92bda632 TC |
752 | $src_bottom = $input{src_maxy}; |
753 | } | |
754 | elsif (defined $input{height}) { | |
755 | if ($input{height} < 0) { | |
756 | $self->_set_error("paste: height must be positive"); | |
757 | return; | |
758 | } | |
759 | $src_bottom = $src_top + $input{height}; | |
760 | } | |
761 | else { | |
762 | $src_bottom = $b; | |
763 | } | |
764 | } | |
765 | ||
766 | $src_right > $r and $src_right = $r; | |
35029411 | 767 | $src_bottom > $b and $src_bottom = $b; |
92bda632 TC |
768 | |
769 | if ($src_right <= $src_left | |
770 | || $src_bottom < $src_top) { | |
771 | $self->_set_error("nothing to paste"); | |
772 | return; | |
773 | } | |
02d1d628 AMH |
774 | |
775 | i_copyto($self->{IMG}, $src->{IMG}, | |
92bda632 TC |
776 | $src_left, $src_top, $src_right, $src_bottom, |
777 | $input{left}, $input{top}); | |
778 | ||
02d1d628 AMH |
779 | return $self; # What should go here?? |
780 | } | |
781 | ||
782 | # Crop an image - i.e. return a new image that is smaller | |
783 | ||
784 | sub crop { | |
785 | my $self=shift; | |
786 | unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } | |
676d5bb5 | 787 | |
34b3f7e6 TC |
788 | unless (defined wantarray) { |
789 | my @caller = caller; | |
790 | warn "crop() called in void context - crop() returns the cropped image at $caller[1] line $caller[2]\n"; | |
791 | return; | |
792 | } | |
793 | ||
676d5bb5 | 794 | my %hsh=@_; |
299a3866 | 795 | |
676d5bb5 TC |
796 | my ($w, $h, $l, $r, $b, $t) = |
797 | @hsh{qw(width height left right bottom top)}; | |
299a3866 | 798 | |
676d5bb5 TC |
799 | # work through the various possibilities |
800 | if (defined $l) { | |
801 | if (defined $w) { | |
802 | $r = $l + $w; | |
803 | } | |
804 | elsif (!defined $r) { | |
805 | $r = $self->getwidth; | |
806 | } | |
807 | } | |
808 | elsif (defined $r) { | |
809 | if (defined $w) { | |
810 | $l = $r - $w; | |
811 | } | |
812 | else { | |
813 | $l = 0; | |
814 | } | |
815 | } | |
816 | elsif (defined $w) { | |
817 | $l = int(0.5+($self->getwidth()-$w)/2); | |
818 | $r = $l + $w; | |
819 | } | |
820 | else { | |
821 | $l = 0; | |
822 | $r = $self->getwidth; | |
823 | } | |
824 | if (defined $t) { | |
825 | if (defined $h) { | |
826 | $b = $t + $h; | |
827 | } | |
828 | elsif (!defined $b) { | |
829 | $b = $self->getheight; | |
830 | } | |
831 | } | |
832 | elsif (defined $b) { | |
833 | if (defined $h) { | |
834 | $t = $b - $h; | |
835 | } | |
836 | else { | |
837 | $t = 0; | |
838 | } | |
839 | } | |
840 | elsif (defined $h) { | |
841 | $t=int(0.5+($self->getheight()-$h)/2); | |
842 | $b=$t+$h; | |
843 | } | |
844 | else { | |
845 | $t = 0; | |
846 | $b = $self->getheight; | |
847 | } | |
02d1d628 AMH |
848 | |
849 | ($l,$r)=($r,$l) if $l>$r; | |
850 | ($t,$b)=($b,$t) if $t>$b; | |
851 | ||
676d5bb5 TC |
852 | $l < 0 and $l = 0; |
853 | $r > $self->getwidth and $r = $self->getwidth; | |
854 | $t < 0 and $t = 0; | |
855 | $b > $self->getheight and $b = $self->getheight; | |
02d1d628 | 856 | |
676d5bb5 TC |
857 | if ($l == $r || $t == $b) { |
858 | $self->_set_error("resulting image would have no content"); | |
859 | return; | |
860 | } | |
9fc9d0ca TC |
861 | if( $r < $l or $b < $t ) { |
862 | $self->_set_error("attempting to crop outside of the image"); | |
863 | return; | |
864 | } | |
676d5bb5 | 865 | my $dst = $self->_sametype(xsize=>$r-$l, ysize=>$b-$t); |
02d1d628 AMH |
866 | |
867 | i_copyto($dst->{IMG},$self->{IMG},$l,$t,$r,$b,0,0); | |
868 | return $dst; | |
869 | } | |
870 | ||
ec76939c TC |
871 | sub _sametype { |
872 | my ($self, %opts) = @_; | |
873 | ||
874 | $self->{IMG} or return $self->_set_error("Not a valid image"); | |
875 | ||
876 | my $x = $opts{xsize} || $self->getwidth; | |
877 | my $y = $opts{ysize} || $self->getheight; | |
878 | my $channels = $opts{channels} || $self->getchannels; | |
879 | ||
880 | my $out = Imager->new; | |
881 | if ($channels == $self->getchannels) { | |
882 | $out->{IMG} = i_sametype($self->{IMG}, $x, $y); | |
883 | } | |
884 | else { | |
885 | $out->{IMG} = i_sametype_chans($self->{IMG}, $x, $y, $channels); | |
886 | } | |
887 | unless ($out->{IMG}) { | |
888 | $self->{ERRSTR} = $self->_error_as_msg; | |
889 | return; | |
890 | } | |
891 | ||
892 | return $out; | |
893 | } | |
894 | ||
02d1d628 AMH |
895 | # Sets an image to a certain size and channel number |
896 | # if there was previously data in the image it is discarded | |
897 | ||
898 | sub img_set { | |
899 | my $self=shift; | |
900 | ||
faa9b3e7 | 901 | my %hsh=(xsize=>100, ysize=>100, channels=>3, bits=>8, type=>'direct', @_); |
02d1d628 AMH |
902 | |
903 | if (defined($self->{IMG})) { | |
faa9b3e7 TC |
904 | # let IIM_DESTROY destroy it, it's possible this image is |
905 | # referenced from a virtual image (like masked) | |
906 | #i_img_destroy($self->{IMG}); | |
02d1d628 AMH |
907 | undef($self->{IMG}); |
908 | } | |
909 | ||
faa9b3e7 TC |
910 | if ($hsh{type} eq 'paletted' || $hsh{type} eq 'pseudo') { |
911 | $self->{IMG} = i_img_pal_new($hsh{xsize}, $hsh{ysize}, $hsh{channels}, | |
912 | $hsh{maxcolors} || 256); | |
913 | } | |
365ea842 TC |
914 | elsif ($hsh{bits} eq 'double') { |
915 | $self->{IMG} = i_img_double_new($hsh{xsize}, $hsh{ysize}, $hsh{channels}); | |
916 | } | |
faa9b3e7 TC |
917 | elsif ($hsh{bits} == 16) { |
918 | $self->{IMG} = i_img_16_new($hsh{xsize}, $hsh{ysize}, $hsh{channels}); | |
919 | } | |
920 | else { | |
921 | $self->{IMG}=Imager::ImgRaw::new($hsh{'xsize'}, $hsh{'ysize'}, | |
922 | $hsh{'channels'}); | |
923 | } | |
1501d9b3 TC |
924 | |
925 | unless ($self->{IMG}) { | |
926 | $self->{ERRSTR} = Imager->_error_as_msg(); | |
927 | return; | |
928 | } | |
929 | ||
930 | $self; | |
faa9b3e7 TC |
931 | } |
932 | ||
933 | # created a masked version of the current image | |
934 | sub masked { | |
935 | my $self = shift; | |
936 | ||
937 | $self or return undef; | |
938 | my %opts = (left => 0, | |
939 | top => 0, | |
940 | right => $self->getwidth, | |
941 | bottom => $self->getheight, | |
942 | @_); | |
943 | my $mask = $opts{mask} ? $opts{mask}{IMG} : undef; | |
944 | ||
945 | my $result = Imager->new; | |
946 | $result->{IMG} = i_img_masked_new($self->{IMG}, $mask, $opts{left}, | |
947 | $opts{top}, $opts{right} - $opts{left}, | |
948 | $opts{bottom} - $opts{top}); | |
416e9814 TC |
949 | unless ($result->{IMG}) { |
950 | $self->_set_error(Imager->_error_as_msg); | |
951 | return; | |
952 | } | |
953 | ||
faa9b3e7 TC |
954 | # keep references to the mask and base images so they don't |
955 | # disappear on us | |
956 | $result->{DEPENDS} = [ $self->{IMG}, $mask ]; | |
957 | ||
416e9814 | 958 | return $result; |
faa9b3e7 TC |
959 | } |
960 | ||
961 | # convert an RGB image into a paletted image | |
962 | sub to_paletted { | |
963 | my $self = shift; | |
964 | my $opts; | |
965 | if (@_ != 1 && !ref $_[0]) { | |
966 | $opts = { @_ }; | |
967 | } | |
968 | else { | |
969 | $opts = shift; | |
970 | } | |
971 | ||
34b3f7e6 TC |
972 | unless (defined wantarray) { |
973 | my @caller = caller; | |
974 | warn "to_paletted() called in void context - to_paletted() returns the converted image at $caller[1] line $caller[2]\n"; | |
975 | return; | |
976 | } | |
977 | ||
3bcba6df TC |
978 | $self->_valid_image |
979 | or return; | |
faa9b3e7 | 980 | |
3bcba6df TC |
981 | my $result = Imager->new; |
982 | unless ($result->{IMG} = i_img_to_pal($self->{IMG}, $opts)) { | |
983 | $self->_set_error(Imager->_error_as_msg); | |
1501d9b3 TC |
984 | return; |
985 | } | |
3bcba6df TC |
986 | |
987 | return $result; | |
faa9b3e7 TC |
988 | } |
989 | ||
3bcba6df | 990 | # convert a paletted (or any image) to an 8-bit/channel RGB image |
faa9b3e7 TC |
991 | sub to_rgb8 { |
992 | my $self = shift; | |
faa9b3e7 | 993 | |
34b3f7e6 TC |
994 | unless (defined wantarray) { |
995 | my @caller = caller; | |
b13bf7e8 | 996 | warn "to_rgb8() called in void context - to_rgb8() returns the converted image at $caller[1] line $caller[2]\n"; |
34b3f7e6 TC |
997 | return; |
998 | } | |
999 | ||
3bcba6df TC |
1000 | $self->_valid_image |
1001 | or return; | |
1002 | ||
1003 | my $result = Imager->new; | |
1004 | unless ($result->{IMG} = i_img_to_rgb($self->{IMG})) { | |
1005 | $self->_set_error(Imager->_error_as_msg()); | |
1006 | return; | |
faa9b3e7 TC |
1007 | } |
1008 | ||
1009 | return $result; | |
1010 | } | |
1011 | ||
3bcba6df | 1012 | # convert a paletted (or any image) to a 16-bit/channel RGB image |
837a4b43 TC |
1013 | sub to_rgb16 { |
1014 | my $self = shift; | |
837a4b43 TC |
1015 | |
1016 | unless (defined wantarray) { | |
1017 | my @caller = caller; | |
3bcba6df | 1018 | warn "to_rgb16() called in void context - to_rgb16() returns the converted image at $caller[1] line $caller[2]\n"; |
837a4b43 TC |
1019 | return; |
1020 | } | |
1021 | ||
3bcba6df TC |
1022 | $self->_valid_image |
1023 | or return; | |
1024 | ||
1025 | my $result = Imager->new; | |
1026 | unless ($result->{IMG} = i_img_to_rgb16($self->{IMG})) { | |
1027 | $self->_set_error(Imager->_error_as_msg()); | |
1028 | return; | |
837a4b43 TC |
1029 | } |
1030 | ||
1031 | return $result; | |
1032 | } | |
1033 | ||
bfe6ba3f TC |
1034 | # convert a paletted (or any image) to an double/channel RGB image |
1035 | sub to_rgb_double { | |
1036 | my $self = shift; | |
1037 | ||
1038 | unless (defined wantarray) { | |
1039 | my @caller = caller; | |
1040 | warn "to_rgb16() called in void context - to_rgb_double() returns the converted image at $caller[1] line $caller[2]\n"; | |
1041 | return; | |
1042 | } | |
1043 | ||
1044 | $self->_valid_image | |
1045 | or return; | |
1046 | ||
1047 | my $result = Imager->new; | |
1048 | unless ($result->{IMG} = i_img_to_drgb($self->{IMG})) { | |
1049 | $self->_set_error(Imager->_error_as_msg()); | |
1050 | return; | |
1051 | } | |
1052 | ||
1053 | return $result; | |
1054 | } | |
1055 | ||
faa9b3e7 TC |
1056 | sub addcolors { |
1057 | my $self = shift; | |
1058 | my %opts = (colors=>[], @_); | |
1059 | ||
32b97571 TC |
1060 | unless ($self->{IMG}) { |
1061 | $self->_set_error("empty input image"); | |
1062 | return; | |
1063 | } | |
1064 | ||
1065 | my @colors = @{$opts{colors}} | |
1066 | or return undef; | |
faa9b3e7 | 1067 | |
32b97571 TC |
1068 | for my $color (@colors) { |
1069 | $color = _color($color); | |
1070 | unless ($color) { | |
1071 | $self->_set_error($Imager::ERRSTR); | |
1072 | return; | |
1073 | } | |
1074 | } | |
1075 | ||
1076 | return i_addcolors($self->{IMG}, @colors); | |
faa9b3e7 TC |
1077 | } |
1078 | ||
1079 | sub setcolors { | |
1080 | my $self = shift; | |
1081 | my %opts = (start=>0, colors=>[], @_); | |
faa9b3e7 | 1082 | |
32b97571 TC |
1083 | unless ($self->{IMG}) { |
1084 | $self->_set_error("empty input image"); | |
1085 | return; | |
1086 | } | |
1087 | ||
1088 | my @colors = @{$opts{colors}} | |
1089 | or return undef; | |
1090 | ||
1091 | for my $color (@colors) { | |
1092 | $color = _color($color); | |
1093 | unless ($color) { | |
1094 | $self->_set_error($Imager::ERRSTR); | |
1095 | return; | |
1096 | } | |
1097 | } | |
1098 | ||
1099 | return i_setcolors($self->{IMG}, $opts{start}, @colors); | |
faa9b3e7 TC |
1100 | } |
1101 | ||
1102 | sub getcolors { | |
1103 | my $self = shift; | |
1104 | my %opts = @_; | |
1105 | if (!exists $opts{start} && !exists $opts{count}) { | |
1106 | # get them all | |
1107 | $opts{start} = 0; | |
1108 | $opts{count} = $self->colorcount; | |
1109 | } | |
1110 | elsif (!exists $opts{count}) { | |
1111 | $opts{count} = 1; | |
1112 | } | |
1113 | elsif (!exists $opts{start}) { | |
1114 | $opts{start} = 0; | |
1115 | } | |
1116 | ||
1117 | $self->{IMG} and | |
1118 | return i_getcolors($self->{IMG}, $opts{start}, $opts{count}); | |
1119 | } | |
1120 | ||
1121 | sub colorcount { | |
1122 | i_colorcount($_[0]{IMG}); | |
1123 | } | |
1124 | ||
1125 | sub maxcolors { | |
1126 | i_maxcolors($_[0]{IMG}); | |
1127 | } | |
1128 | ||
1129 | sub findcolor { | |
1130 | my $self = shift; | |
1131 | my %opts = @_; | |
1132 | $opts{color} or return undef; | |
1133 | ||
1134 | $self->{IMG} and i_findcolor($self->{IMG}, $opts{color}); | |
1135 | } | |
1136 | ||
1137 | sub bits { | |
1138 | my $self = shift; | |
af3c2450 TC |
1139 | my $bits = $self->{IMG} && i_img_bits($self->{IMG}); |
1140 | if ($bits && $bits == length(pack("d", 1)) * 8) { | |
1141 | $bits = 'double'; | |
1142 | } | |
1143 | $bits; | |
faa9b3e7 TC |
1144 | } |
1145 | ||
1146 | sub type { | |
1147 | my $self = shift; | |
1148 | if ($self->{IMG}) { | |
1149 | return i_img_type($self->{IMG}) ? "paletted" : "direct"; | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | sub virtual { | |
1154 | my $self = shift; | |
1155 | $self->{IMG} and i_img_virtual($self->{IMG}); | |
1156 | } | |
1157 | ||
bd8052a6 TC |
1158 | sub is_bilevel { |
1159 | my ($self) = @_; | |
1160 | ||
1161 | $self->{IMG} or return; | |
1162 | ||
1163 | return i_img_is_monochrome($self->{IMG}); | |
1164 | } | |
1165 | ||
faa9b3e7 TC |
1166 | sub tags { |
1167 | my ($self, %opts) = @_; | |
1168 | ||
1169 | $self->{IMG} or return; | |
1170 | ||
1171 | if (defined $opts{name}) { | |
1172 | my @result; | |
1173 | my $start = 0; | |
1174 | my $found; | |
1175 | while (defined($found = i_tags_find($self->{IMG}, $opts{name}, $start))) { | |
1176 | push @result, (i_tags_get($self->{IMG}, $found))[1]; | |
1177 | $start = $found+1; | |
1178 | } | |
1179 | return wantarray ? @result : $result[0]; | |
1180 | } | |
1181 | elsif (defined $opts{code}) { | |
1182 | my @result; | |
1183 | my $start = 0; | |
1184 | my $found; | |
1185 | while (defined($found = i_tags_findn($self->{IMG}, $opts{code}, $start))) { | |
1186 | push @result, (i_tags_get($self->{IMG}, $found))[1]; | |
1187 | $start = $found+1; | |
1188 | } | |
1189 | return @result; | |
1190 | } | |
1191 | else { | |
1192 | if (wantarray) { | |
1193 | return map { [ i_tags_get($self->{IMG}, $_) ] } 0.. i_tags_count($self->{IMG})-1; | |
1194 | } | |
1195 | else { | |
1196 | return i_tags_count($self->{IMG}); | |
1197 | } | |
1198 | } | |
1199 | } | |
1200 | ||
1201 | sub addtag { | |
1202 | my $self = shift; | |
1203 | my %opts = @_; | |
1204 | ||
1205 | return -1 unless $self->{IMG}; | |
1206 | if ($opts{name}) { | |
1207 | if (defined $opts{value}) { | |
1208 | if ($opts{value} =~ /^\d+$/) { | |
1209 | # add as a number | |
1210 | return i_tags_addn($self->{IMG}, $opts{name}, 0, $opts{value}); | |
1211 | } | |
1212 | else { | |
1213 | return i_tags_add($self->{IMG}, $opts{name}, 0, $opts{value}, 0); | |
1214 | } | |
1215 | } | |
1216 | elsif (defined $opts{data}) { | |
1217 | # force addition as a string | |
1218 | return i_tags_add($self->{IMG}, $opts{name}, 0, $opts{data}, 0); | |
1219 | } | |
1220 | else { | |
1221 | $self->{ERRSTR} = "No value supplied"; | |
1222 | return undef; | |
1223 | } | |
1224 | } | |
1225 | elsif ($opts{code}) { | |
1226 | if (defined $opts{value}) { | |
1227 | if ($opts{value} =~ /^\d+$/) { | |
1228 | # add as a number | |
1229 | return i_tags_addn($self->{IMG}, $opts{code}, 0, $opts{value}); | |
1230 | } | |
1231 | else { | |
1232 | return i_tags_add($self->{IMG}, $opts{code}, 0, $opts{value}, 0); | |
1233 | } | |
1234 | } | |
1235 | elsif (defined $opts{data}) { | |
1236 | # force addition as a string | |
1237 | return i_tags_add($self->{IMG}, $opts{code}, 0, $opts{data}, 0); | |
1238 | } | |
1239 | else { | |
1240 | $self->{ERRSTR} = "No value supplied"; | |
1241 | return undef; | |
1242 | } | |
1243 | } | |
1244 | else { | |
1245 | return undef; | |
1246 | } | |
1247 | } | |
1248 | ||
1249 | sub deltag { | |
1250 | my $self = shift; | |
1251 | my %opts = @_; | |
1252 | ||
1253 | return 0 unless $self->{IMG}; | |
1254 | ||
9d540150 TC |
1255 | if (defined $opts{'index'}) { |
1256 | return i_tags_delete($self->{IMG}, $opts{'index'}); | |
faa9b3e7 TC |
1257 | } |
1258 | elsif (defined $opts{name}) { | |
1259 | return i_tags_delbyname($self->{IMG}, $opts{name}); | |
1260 | } | |
1261 | elsif (defined $opts{code}) { | |
1262 | return i_tags_delbycode($self->{IMG}, $opts{code}); | |
1263 | } | |
1264 | else { | |
1265 | $self->{ERRSTR} = "Need to supply index, name, or code parameter"; | |
1266 | return 0; | |
1267 | } | |
02d1d628 AMH |
1268 | } |
1269 | ||
97c4effc TC |
1270 | sub settag { |
1271 | my ($self, %opts) = @_; | |
1272 | ||
1273 | if ($opts{name}) { | |
1274 | $self->deltag(name=>$opts{name}); | |
1275 | return $self->addtag(name=>$opts{name}, value=>$opts{value}); | |
1276 | } | |
1277 | elsif (defined $opts{code}) { | |
1278 | $self->deltag(code=>$opts{code}); | |
1279 | return $self->addtag(code=>$opts{code}, value=>$opts{value}); | |
1280 | } | |
1281 | else { | |
1282 | return undef; | |
1283 | } | |
1284 | } | |
1285 | ||
10461f9a TC |
1286 | |
1287 | sub _get_reader_io { | |
84e51293 | 1288 | my ($self, $input) = @_; |
10461f9a | 1289 | |
e7ff1cf7 TC |
1290 | if ($input->{io}) { |
1291 | return $input->{io}, undef; | |
1292 | } | |
84e51293 | 1293 | elsif ($input->{fd}) { |
10461f9a TC |
1294 | return io_new_fd($input->{fd}); |
1295 | } | |
1296 | elsif ($input->{fh}) { | |
1297 | my $fd = fileno($input->{fh}); | |
de470892 | 1298 | unless (defined $fd) { |
10461f9a TC |
1299 | $self->_set_error("Handle in fh option not opened"); |
1300 | return; | |
1301 | } | |
1302 | return io_new_fd($fd); | |
1303 | } | |
1304 | elsif ($input->{file}) { | |
1305 | my $file = IO::File->new($input->{file}, "r"); | |
1306 | unless ($file) { | |
1307 | $self->_set_error("Could not open $input->{file}: $!"); | |
1308 | return; | |
1309 | } | |
1310 | binmode $file; | |
1311 | return (io_new_fd(fileno($file)), $file); | |
1312 | } | |
1313 | elsif ($input->{data}) { | |
1314 | return io_new_buffer($input->{data}); | |
1315 | } | |
1316 | elsif ($input->{callback} || $input->{readcb}) { | |
84e51293 AMH |
1317 | if (!$input->{seekcb}) { |
1318 | $self->_set_error("Need a seekcb parameter"); | |
10461f9a TC |
1319 | } |
1320 | if ($input->{maxbuffer}) { | |
1321 | return io_new_cb($input->{writecb}, | |
1322 | $input->{callback} || $input->{readcb}, | |
1323 | $input->{seekcb}, $input->{closecb}, | |
1324 | $input->{maxbuffer}); | |
1325 | } | |
1326 | else { | |
1327 | return io_new_cb($input->{writecb}, | |
1328 | $input->{callback} || $input->{readcb}, | |
1329 | $input->{seekcb}, $input->{closecb}); | |
1330 | } | |
1331 | } | |
1332 | else { | |
1333 | $self->_set_error("file/fd/fh/data/callback parameter missing"); | |
1334 | return; | |
1335 | } | |
1336 | } | |
1337 | ||
1338 | sub _get_writer_io { | |
5970bd39 | 1339 | my ($self, $input) = @_; |
10461f9a | 1340 | |
6d5c85a2 TC |
1341 | my $buffered = exists $input->{buffered} ? $input->{buffered} : 1; |
1342 | ||
1343 | my $io; | |
1344 | my @extras; | |
e7ff1cf7 | 1345 | if ($input->{io}) { |
6d5c85a2 | 1346 | $io = $input->{io}; |
e7ff1cf7 TC |
1347 | } |
1348 | elsif ($input->{fd}) { | |
6d5c85a2 | 1349 | $io = io_new_fd($input->{fd}); |
10461f9a TC |
1350 | } |
1351 | elsif ($input->{fh}) { | |
1352 | my $fd = fileno($input->{fh}); | |
de470892 | 1353 | unless (defined $fd) { |
10461f9a TC |
1354 | $self->_set_error("Handle in fh option not opened"); |
1355 | return; | |
1356 | } | |
9d1c4956 TC |
1357 | # flush it |
1358 | my $oldfh = select($input->{fh}); | |
1359 | # flush anything that's buffered, and make sure anything else is flushed | |
1360 | $| = 1; | |
1361 | select($oldfh); | |
6d5c85a2 | 1362 | $io = io_new_fd($fd); |
10461f9a TC |
1363 | } |
1364 | elsif ($input->{file}) { | |
1365 | my $fh = new IO::File($input->{file},"w+"); | |
1366 | unless ($fh) { | |
1367 | $self->_set_error("Could not open file $input->{file}: $!"); | |
1368 | return; | |
1369 | } | |
1370 | binmode($fh) or die; | |
6d5c85a2 TC |
1371 | $io = io_new_fd(fileno($fh)); |
1372 | push @extras, $fh; | |
10461f9a TC |
1373 | } |
1374 | elsif ($input->{data}) { | |
6d5c85a2 | 1375 | $io = io_new_bufchain(); |
10461f9a TC |
1376 | } |
1377 | elsif ($input->{callback} || $input->{writecb}) { | |
6d5c85a2 TC |
1378 | if ($input->{maxbuffer} && $input->{maxbuffer} == 1) { |
1379 | $buffered = 0; | |
10461f9a | 1380 | } |
6d5c85a2 TC |
1381 | $io = io_new_cb($input->{callback} || $input->{writecb}, |
1382 | $input->{readcb}, | |
1383 | $input->{seekcb}, $input->{closecb}); | |
10461f9a TC |
1384 | } |
1385 | else { | |
1386 | $self->_set_error("file/fd/fh/data/callback parameter missing"); | |
1387 | return; | |
1388 | } | |
6d5c85a2 TC |
1389 | |
1390 | unless ($buffered) { | |
1391 | $io->set_buffered(0); | |
1392 | } | |
1393 | ||
1394 | return ($io, @extras); | |
10461f9a TC |
1395 | } |
1396 | ||
02d1d628 AMH |
1397 | # Read an image from file |
1398 | ||
1399 | sub read { | |
1400 | my $self = shift; | |
1401 | my %input=@_; | |
02d1d628 AMH |
1402 | |
1403 | if (defined($self->{IMG})) { | |
faa9b3e7 TC |
1404 | # let IIM_DESTROY do the destruction, since the image may be |
1405 | # referenced from elsewhere | |
1406 | #i_img_destroy($self->{IMG}); | |
02d1d628 AMH |
1407 | undef($self->{IMG}); |
1408 | } | |
1409 | ||
84e51293 AMH |
1410 | my ($IO, $fh) = $self->_get_reader_io(\%input) or return; |
1411 | ||
5970bd39 TC |
1412 | my $type = $input{'type'}; |
1413 | unless ($type) { | |
1414 | $type = i_test_format_probe($IO, -1); | |
66614d6e | 1415 | } |
84e51293 | 1416 | |
4f21e06e TC |
1417 | if ($input{file} && !$type) { |
1418 | # guess the type | |
1419 | $type = $FORMATGUESS->($input{file}); | |
1420 | } | |
1421 | ||
5970bd39 | 1422 | unless ($type) { |
4f21e06e TC |
1423 | my $msg = "type parameter missing and it couldn't be determined from the file contents"; |
1424 | $input{file} and $msg .= " or file name"; | |
1425 | $self->_set_error($msg); | |
10461f9a TC |
1426 | return undef; |
1427 | } | |
02d1d628 | 1428 | |
5970bd39 | 1429 | _reader_autoload($type); |
53a6bbd4 | 1430 | |
5970bd39 TC |
1431 | if ($readers{$type} && $readers{$type}{single}) { |
1432 | return $readers{$type}{single}->($self, $IO, %input); | |
53a6bbd4 TC |
1433 | } |
1434 | ||
5970bd39 | 1435 | unless ($formats_low{$type}) { |
f245645a | 1436 | my $read_types = join ', ', sort Imager->read_types(); |
5970bd39 | 1437 | $self->_set_error("format '$type' not supported - formats $read_types available for reading - $reader_load_errors{$type}"); |
66614d6e TC |
1438 | return; |
1439 | } | |
1440 | ||
d87dc9a4 TC |
1441 | my $allow_incomplete = $input{allow_incomplete}; |
1442 | defined $allow_incomplete or $allow_incomplete = 0; | |
9c106321 | 1443 | |
5970bd39 | 1444 | if ( $type eq 'pnm' ) { |
d87dc9a4 | 1445 | $self->{IMG}=i_readpnm_wiol( $IO, $allow_incomplete ); |
2fe0b227 | 1446 | if ( !defined($self->{IMG}) ) { |
2691d220 TC |
1447 | $self->{ERRSTR}='unable to read pnm image: '._error_as_msg(); |
1448 | return undef; | |
790923a4 | 1449 | } |
2fe0b227 AMH |
1450 | $self->{DEBUG} && print "loading a pnm file\n"; |
1451 | return $self; | |
1452 | } | |
790923a4 | 1453 | |
5970bd39 | 1454 | if ( $type eq 'bmp' ) { |
d87dc9a4 | 1455 | $self->{IMG}=i_readbmp_wiol( $IO, $allow_incomplete ); |
2fe0b227 AMH |
1456 | if ( !defined($self->{IMG}) ) { |
1457 | $self->{ERRSTR}=$self->_error_as_msg(); | |
1458 | return undef; | |
10461f9a | 1459 | } |
2fe0b227 AMH |
1460 | $self->{DEBUG} && print "loading a bmp file\n"; |
1461 | } | |
10461f9a | 1462 | |
5970bd39 | 1463 | if ( $type eq 'gif' ) { |
2fe0b227 AMH |
1464 | if ($input{colors} && !ref($input{colors})) { |
1465 | # must be a reference to a scalar that accepts the colour map | |
1466 | $self->{ERRSTR} = "option 'colors' must be a scalar reference"; | |
1467 | return undef; | |
1ec86afa | 1468 | } |
f1adece7 TC |
1469 | if ($input{'gif_consolidate'}) { |
1470 | if ($input{colors}) { | |
1471 | my $colors; | |
1472 | ($self->{IMG}, $colors) =i_readgif_wiol( $IO ); | |
1473 | if ($colors) { | |
1474 | ${ $input{colors} } = [ map { NC(@$_) } @$colors ]; | |
1475 | } | |
1476 | } | |
1477 | else { | |
1478 | $self->{IMG} =i_readgif_wiol( $IO ); | |
737a830c | 1479 | } |
737a830c | 1480 | } |
2fe0b227 | 1481 | else { |
f1adece7 TC |
1482 | my $page = $input{'page'}; |
1483 | defined $page or $page = 0; | |
1484 | $self->{IMG} = i_readgif_single_wiol( $IO, $page ); | |
a5dbf458 | 1485 | if ($self->{IMG} && $input{colors}) { |
f1adece7 TC |
1486 | ${ $input{colors} } = |
1487 | [ i_getcolors($self->{IMG}, 0, i_colorcount($self->{IMG})) ]; | |
1488 | } | |
895dbd34 | 1489 | } |
f1adece7 | 1490 | |
2fe0b227 AMH |
1491 | if ( !defined($self->{IMG}) ) { |
1492 | $self->{ERRSTR}=$self->_error_as_msg(); | |
1493 | return undef; | |
895dbd34 | 1494 | } |
2fe0b227 AMH |
1495 | $self->{DEBUG} && print "loading a gif file\n"; |
1496 | } | |
895dbd34 | 1497 | |
5970bd39 | 1498 | if ( $type eq 'tga' ) { |
2fe0b227 AMH |
1499 | $self->{IMG}=i_readtga_wiol( $IO, -1 ); # Fixme, check if that length parameter is ever needed |
1500 | if ( !defined($self->{IMG}) ) { | |
1501 | $self->{ERRSTR}=$self->_error_as_msg(); | |
1502 | return undef; | |
895dbd34 | 1503 | } |
2fe0b227 AMH |
1504 | $self->{DEBUG} && print "loading a tga file\n"; |
1505 | } | |
02d1d628 | 1506 | |
5970bd39 | 1507 | if ( $type eq 'raw' ) { |
500888da TC |
1508 | unless ( $input{xsize} && $input{ysize} ) { |
1509 | $self->_set_error('missing xsize or ysize parameter for raw'); | |
2fe0b227 | 1510 | return undef; |
895dbd34 AMH |
1511 | } |
1512 | ||
500888da TC |
1513 | my $interleave = _first($input{raw_interleave}, $input{interleave}); |
1514 | unless (defined $interleave) { | |
1515 | my @caller = caller; | |
1516 | warn "read(type => 'raw') $caller[2] line $caller[1]: supply interleave or raw_interleave for future compatibility\n"; | |
1517 | $interleave = 1; | |
1518 | } | |
1519 | my $data_ch = _first($input{raw_datachannels}, $input{datachannels}, 3); | |
1520 | my $store_ch = _first($input{raw_storechannels}, $input{storechannels}, 3); | |
1521 | ||
2fe0b227 | 1522 | $self->{IMG} = i_readraw_wiol( $IO, |
500888da TC |
1523 | $input{xsize}, |
1524 | $input{ysize}, | |
1525 | $data_ch, | |
1526 | $store_ch, | |
1527 | $interleave); | |
2fe0b227 | 1528 | if ( !defined($self->{IMG}) ) { |
5f8f8e17 | 1529 | $self->{ERRSTR}=$self->_error_as_msg(); |
2fe0b227 | 1530 | return undef; |
dd55acc8 | 1531 | } |
2fe0b227 | 1532 | $self->{DEBUG} && print "loading a raw file\n"; |
02d1d628 | 1533 | } |
2fe0b227 | 1534 | |
02d1d628 | 1535 | return $self; |
02d1d628 AMH |
1536 | } |
1537 | ||
53a6bbd4 TC |
1538 | sub register_reader { |
1539 | my ($class, %opts) = @_; | |
1540 | ||
1541 | defined $opts{type} | |
1542 | or die "register_reader called with no type parameter\n"; | |
1543 | ||
1544 | my $type = $opts{type}; | |
1545 | ||
1546 | defined $opts{single} || defined $opts{multiple} | |
1547 | or die "register_reader called with no single or multiple parameter\n"; | |
1548 | ||
1549 | $readers{$type} = { }; | |
1550 | if ($opts{single}) { | |
1551 | $readers{$type}{single} = $opts{single}; | |
1552 | } | |
1553 | if ($opts{multiple}) { | |
1554 | $readers{$type}{multiple} = $opts{multiple}; | |
1555 | } | |
1556 | ||
1557 | return 1; | |
1558 | } | |
1559 | ||
2b405c9e TC |
1560 | sub register_writer { |
1561 | my ($class, %opts) = @_; | |
1562 | ||
1563 | defined $opts{type} | |
1564 | or die "register_writer called with no type parameter\n"; | |
1565 | ||
1566 | my $type = $opts{type}; | |
1567 | ||
1568 | defined $opts{single} || defined $opts{multiple} | |
1569 | or die "register_writer called with no single or multiple parameter\n"; | |
1570 | ||
1571 | $writers{$type} = { }; | |
1572 | if ($opts{single}) { | |
1573 | $writers{$type}{single} = $opts{single}; | |
1574 | } | |
1575 | if ($opts{multiple}) { | |
1576 | $writers{$type}{multiple} = $opts{multiple}; | |
1577 | } | |
1578 | ||
1579 | return 1; | |
1580 | } | |
1581 | ||
f245645a TC |
1582 | sub read_types { |
1583 | my %types = | |
1584 | ( | |
1585 | map { $_ => 1 } | |
1586 | keys %readers, | |
1587 | grep($file_formats{$_}, keys %formats), | |
1588 | qw(ico sgi), # formats not handled directly, but supplied with Imager | |
1589 | ); | |
1590 | ||
1591 | return keys %types; | |
1592 | } | |
1593 | ||
1594 | sub write_types { | |
1595 | my %types = | |
1596 | ( | |
1597 | map { $_ => 1 } | |
1598 | keys %writers, | |
1599 | grep($file_formats{$_}, keys %formats), | |
1600 | qw(ico sgi), # formats not handled directly, but supplied with Imager | |
1601 | ); | |
1602 | ||
1603 | return keys %types; | |
1604 | } | |
1605 | ||
5970bd39 TC |
1606 | sub _load_file { |
1607 | my ($file, $error) = @_; | |
1608 | ||
1609 | if ($attempted_to_load{$file}) { | |
1610 | if ($file_load_errors{$file}) { | |
1611 | $$error = $file_load_errors{$file}; | |
1612 | return 0; | |
1613 | } | |
1614 | else { | |
1615 | return 1; | |
1616 | } | |
1617 | } | |
1618 | else { | |
1619 | local $SIG{__DIE__}; | |
1620 | my $loaded = eval { | |
1621 | ++$attempted_to_load{$file}; | |
1622 | require $file; | |
1623 | return 1; | |
1624 | }; | |
1625 | if ($loaded) { | |
1626 | return 1; | |
1627 | } | |
1628 | else { | |
1629 | my $work = $@ || "Unknown error loading $file"; | |
1630 | chomp $work; | |
1631 | $work =~ s/\n?Compilation failed in require at .*Imager\.pm line .*\z//m; | |
1632 | $work =~ s/\n/\\n/g; | |
1633 | $file_load_errors{$file} = $work; | |
1634 | $$error = $work; | |
1635 | return 0; | |
1636 | } | |
1637 | } | |
1638 | } | |
1639 | ||
53a6bbd4 TC |
1640 | # probes for an Imager::File::whatever module |
1641 | sub _reader_autoload { | |
1642 | my $type = shift; | |
1643 | ||
1d7e3124 | 1644 | return if $formats_low{$type} || $readers{$type}; |
53a6bbd4 TC |
1645 | |
1646 | return unless $type =~ /^\w+$/; | |
1647 | ||
1648 | my $file = "Imager/File/\U$type\E.pm"; | |
1649 | ||
5970bd39 TC |
1650 | my $error; |
1651 | my $loaded = _load_file($file, \$error); | |
1652 | if (!$loaded && $error =~ /^Can't locate /) { | |
1653 | my $filer = "Imager/File/\U$type\EReader.pm"; | |
1654 | $loaded = _load_file($filer, \$error); | |
1655 | if ($error =~ /^Can't locate /) { | |
1656 | $error = "Can't locate $file or $filer"; | |
2b405c9e TC |
1657 | } |
1658 | } | |
5970bd39 TC |
1659 | unless ($loaded) { |
1660 | $reader_load_errors{$type} = $error; | |
1661 | } | |
2b405c9e TC |
1662 | } |
1663 | ||
1664 | # probes for an Imager::File::whatever module | |
1665 | sub _writer_autoload { | |
1666 | my $type = shift; | |
1667 | ||
5970bd39 | 1668 | return if $formats_low{$type} || $writers{$type}; |
2b405c9e TC |
1669 | |
1670 | return unless $type =~ /^\w+$/; | |
1671 | ||
1672 | my $file = "Imager/File/\U$type\E.pm"; | |
1673 | ||
5970bd39 TC |
1674 | my $error; |
1675 | my $loaded = _load_file($file, \$error); | |
1676 | if (!$loaded && $error =~ /^Can't locate /) { | |
1677 | my $filew = "Imager/File/\U$type\EWriter.pm"; | |
1678 | $loaded = _load_file($filew, \$error); | |
1679 | if ($error =~ /^Can't locate /) { | |
1680 | $error = "Can't locate $file or $filew"; | |
2b405c9e | 1681 | } |
53a6bbd4 | 1682 | } |
5970bd39 TC |
1683 | unless ($loaded) { |
1684 | $writer_load_errors{$type} = $error; | |
1685 | } | |
53a6bbd4 TC |
1686 | } |
1687 | ||
97c4effc TC |
1688 | sub _fix_gif_positions { |
1689 | my ($opts, $opt, $msg, @imgs) = @_; | |
2fe0b227 | 1690 | |
97c4effc TC |
1691 | my $positions = $opts->{'gif_positions'}; |
1692 | my $index = 0; | |
1693 | for my $pos (@$positions) { | |
1694 | my ($x, $y) = @$pos; | |
1695 | my $img = $imgs[$index++]; | |
9d1c4956 TC |
1696 | $img->settag(name=>'gif_left', value=>$x); |
1697 | $img->settag(name=>'gif_top', value=>$y) if defined $y; | |
97c4effc TC |
1698 | } |
1699 | $$msg .= "replaced with the gif_left and gif_top tags"; | |
1700 | } | |
1701 | ||
1702 | my %obsolete_opts = | |
1703 | ( | |
1704 | gif_each_palette=>'gif_local_map', | |
1705 | interlace => 'gif_interlace', | |
1706 | gif_delays => 'gif_delay', | |
1707 | gif_positions => \&_fix_gif_positions, | |
1708 | gif_loop_count => 'gif_loop', | |
1709 | ); | |
1710 | ||
6e4af7d4 TC |
1711 | # options that should be converted to colors |
1712 | my %color_opts = map { $_ => 1 } qw/i_background/; | |
1713 | ||
97c4effc TC |
1714 | sub _set_opts { |
1715 | my ($self, $opts, $prefix, @imgs) = @_; | |
1716 | ||
1717 | for my $opt (keys %$opts) { | |
1718 | my $tagname = $opt; | |
1719 | if ($obsolete_opts{$opt}) { | |
1720 | my $new = $obsolete_opts{$opt}; | |
1721 | my $msg = "Obsolete option $opt "; | |
1722 | if (ref $new) { | |
1723 | $new->($opts, $opt, \$msg, @imgs); | |
1724 | } | |
1725 | else { | |
1726 | $msg .= "replaced with the $new tag "; | |
1727 | $tagname = $new; | |
1728 | } | |
1729 | $msg .= "line ".(caller(2))[2]." of file ".(caller(2))[1]; | |
1730 | warn $msg if $warn_obsolete && $^W; | |
1731 | } | |
1732 | next unless $tagname =~ /^\Q$prefix/; | |
1733 | my $value = $opts->{$opt}; | |
6e4af7d4 TC |
1734 | if ($color_opts{$opt}) { |
1735 | $value = _color($value); | |
1736 | unless ($value) { | |
1737 | $self->_set_error($Imager::ERRSTR); | |
1738 | return; | |
1739 | } | |
1740 | } | |
97c4effc TC |
1741 | if (ref $value) { |
1742 | if (UNIVERSAL::isa($value, "Imager::Color")) { | |
1743 | my $tag = sprintf("color(%d,%d,%d,%d)", $value->rgba); | |
1744 | for my $img (@imgs) { | |
1745 | $img->settag(name=>$tagname, value=>$tag); | |
1746 | } | |
1747 | } | |
1748 | elsif (ref($value) eq 'ARRAY') { | |
1749 | for my $i (0..$#$value) { | |
1750 | my $val = $value->[$i]; | |
1751 | if (ref $val) { | |
1752 | if (UNIVERSAL::isa($val, "Imager::Color")) { | |
1753 | my $tag = sprintf("color(%d,%d,%d,%d)", $value->rgba); | |
1754 | $i < @imgs and | |
1755 | $imgs[$i]->settag(name=>$tagname, value=>$tag); | |
1756 | } | |
1757 | else { | |
1758 | $self->_set_error("Unknown reference type " . ref($value) . | |
1759 | " supplied in array for $opt"); | |
1760 | return; | |
1761 | } | |
1762 | } | |
1763 | else { | |
1764 | $i < @imgs | |
1765 | and $imgs[$i]->settag(name=>$tagname, value=>$val); | |
1766 | } | |
1767 | } | |
1768 | } | |
1769 | else { | |
1770 | $self->_set_error("Unknown reference type " . ref($value) . | |
1771 | " supplied for $opt"); | |
1772 | return; | |
1773 | } | |
1774 | } | |
1775 | else { | |
1776 | # set it as a tag for every image | |
1777 | for my $img (@imgs) { | |
1778 | $img->settag(name=>$tagname, value=>$value); | |
1779 | } | |
1780 | } | |
1781 | } | |
1782 | ||
1783 | return 1; | |
1784 | } | |
1785 | ||
02d1d628 | 1786 | # Write an image to file |
02d1d628 AMH |
1787 | sub write { |
1788 | my $self = shift; | |
2fe0b227 AMH |
1789 | my %input=(jpegquality=>75, |
1790 | gifquant=>'mc', | |
1791 | lmdither=>6.0, | |
febba01f AMH |
1792 | lmfixed=>[], |
1793 | idstring=>"", | |
1794 | compress=>1, | |
1795 | wierdpack=>0, | |
4c2d6970 | 1796 | fax_fine=>1, @_); |
10461f9a | 1797 | my $rc; |
02d1d628 | 1798 | |
97c4effc TC |
1799 | $self->_set_opts(\%input, "i_", $self) |
1800 | or return undef; | |
1801 | ||
02d1d628 AMH |
1802 | unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } |
1803 | ||
5970bd39 TC |
1804 | my $type = $input{'type'}; |
1805 | if (!$type and $input{file}) { | |
1806 | $type = $FORMATGUESS->($input{file}); | |
9d540150 | 1807 | } |
5970bd39 | 1808 | unless ($type) { |
9d540150 TC |
1809 | $self->{ERRSTR}='type parameter missing and not possible to guess from extension'; |
1810 | return undef; | |
1811 | } | |
02d1d628 | 1812 | |
5970bd39 | 1813 | _writer_autoload($type); |
02d1d628 | 1814 | |
2b405c9e | 1815 | my ($IO, $fh); |
5970bd39 TC |
1816 | if ($writers{$type} && $writers{$type}{single}) { |
1817 | ($IO, $fh) = $self->_get_writer_io(\%input) | |
2fe0b227 | 1818 | or return undef; |
febba01f | 1819 | |
5970bd39 | 1820 | $writers{$type}{single}->($self, $IO, %input, type => $type) |
2fe0b227 | 1821 | or return undef; |
2b405c9e TC |
1822 | } |
1823 | else { | |
5970bd39 | 1824 | if (!$formats_low{$type}) { |
f245645a | 1825 | my $write_types = join ', ', sort Imager->write_types(); |
5970bd39 | 1826 | $self->_set_error("format '$type' not supported - formats $write_types available for writing - $writer_load_errors{$type}"); |
2fe0b227 | 1827 | return undef; |
930c67c8 | 1828 | } |
2b405c9e | 1829 | |
5970bd39 | 1830 | ($IO, $fh) = $self->_get_writer_io(\%input, $type) |
2fe0b227 | 1831 | or return undef; |
5970bd39 TC |
1832 | |
1833 | if ( $type eq 'pnm' ) { | |
2b405c9e TC |
1834 | $self->_set_opts(\%input, "pnm_", $self) |
1835 | or return undef; | |
1836 | if ( ! i_writeppm_wiol($self->{IMG},$IO) ) { | |
1837 | $self->{ERRSTR} = $self->_error_as_msg(); | |
1838 | return undef; | |
1839 | } | |
1840 | $self->{DEBUG} && print "writing a pnm file\n"; | |
5970bd39 TC |
1841 | } |
1842 | elsif ( $type eq 'raw' ) { | |
2b405c9e TC |
1843 | $self->_set_opts(\%input, "raw_", $self) |
1844 | or return undef; | |
1845 | if ( !i_writeraw_wiol($self->{IMG},$IO) ) { | |
1846 | $self->{ERRSTR} = $self->_error_as_msg(); | |
1847 | return undef; | |
1848 | } | |
1849 | $self->{DEBUG} && print "writing a raw file\n"; | |
5970bd39 TC |
1850 | } |
1851 | elsif ( $type eq 'bmp' ) { | |
2b405c9e TC |
1852 | $self->_set_opts(\%input, "bmp_", $self) |
1853 | or return undef; | |
1854 | if ( !i_writebmp_wiol($self->{IMG}, $IO) ) { | |
ae12796a | 1855 | $self->{ERRSTR} = $self->_error_as_msg; |
2b405c9e TC |
1856 | return undef; |
1857 | } | |
1858 | $self->{DEBUG} && print "writing a bmp file\n"; | |
5970bd39 TC |
1859 | } |
1860 | elsif ( $type eq 'tga' ) { | |
2b405c9e TC |
1861 | $self->_set_opts(\%input, "tga_", $self) |
1862 | or return undef; | |
1863 | ||
1864 | if ( !i_writetga_wiol($self->{IMG}, $IO, $input{wierdpack}, $input{compress}, $input{idstring}) ) { | |
1865 | $self->{ERRSTR}=$self->_error_as_msg(); | |
1866 | return undef; | |
1867 | } | |
1868 | $self->{DEBUG} && print "writing a tga file\n"; | |
1501d9b3 | 1869 | } |
02d1d628 | 1870 | } |
10461f9a | 1871 | |
2fe0b227 AMH |
1872 | if (exists $input{'data'}) { |
1873 | my $data = io_slurp($IO); | |
1874 | if (!$data) { | |
1875 | $self->{ERRSTR}='Could not slurp from buffer'; | |
1876 | return undef; | |
1877 | } | |
1878 | ${$input{data}} = $data; | |
1879 | } | |
02d1d628 AMH |
1880 | return $self; |
1881 | } | |
1882 | ||
1883 | sub write_multi { | |
1884 | my ($class, $opts, @images) = @_; | |
1885 | ||
2b405c9e TC |
1886 | my $type = $opts->{type}; |
1887 | ||
1888 | if (!$type && $opts->{'file'}) { | |
1889 | $type = $FORMATGUESS->($opts->{'file'}); | |
10461f9a | 1890 | } |
2b405c9e | 1891 | unless ($type) { |
10461f9a TC |
1892 | $class->_set_error('type parameter missing and not possible to guess from extension'); |
1893 | return; | |
1894 | } | |
1895 | # translate to ImgRaw | |
1896 | if (grep !UNIVERSAL::isa($_, 'Imager') || !$_->{IMG}, @images) { | |
1897 | $class->_set_error('Usage: Imager->write_multi({ options }, @images)'); | |
1898 | return 0; | |
1899 | } | |
97c4effc TC |
1900 | $class->_set_opts($opts, "i_", @images) |
1901 | or return; | |
10461f9a | 1902 | my @work = map $_->{IMG}, @images; |
2b405c9e TC |
1903 | |
1904 | _writer_autoload($type); | |
1905 | ||
1906 | my ($IO, $file); | |
1907 | if ($writers{$type} && $writers{$type}{multiple}) { | |
1908 | ($IO, $file) = $class->_get_writer_io($opts, $type) | |
1909 | or return undef; | |
1910 | ||
1911 | $writers{$type}{multiple}->($class, $IO, $opts, @images) | |
1912 | or return undef; | |
1913 | } | |
1914 | else { | |
1915 | if (!$formats{$type}) { | |
f245645a TC |
1916 | my $write_types = join ', ', sort Imager->write_types(); |
1917 | $class->_set_error("format '$type' not supported - formats $write_types available for writing"); | |
2b405c9e TC |
1918 | return undef; |
1919 | } | |
1920 | ||
1921 | ($IO, $file) = $class->_get_writer_io($opts, $type) | |
1922 | or return undef; | |
1923 | ||
e5ee047b | 1924 | if (0) { # eventually PNM in here, now that TIFF/GIF are elsewhere |
02d1d628 AMH |
1925 | } |
1926 | else { | |
e7ff1cf7 TC |
1927 | if (@images == 1) { |
1928 | unless ($images[0]->write(%$opts, io => $IO, type => $type)) { | |
1929 | return 1; | |
1930 | } | |
1931 | } | |
1932 | else { | |
1933 | $ERRSTR = "Sorry, write_multi doesn't support $type yet"; | |
1934 | return 0; | |
1935 | } | |
02d1d628 AMH |
1936 | } |
1937 | } | |
2b405c9e TC |
1938 | |
1939 | if (exists $opts->{'data'}) { | |
1940 | my $data = io_slurp($IO); | |
1941 | if (!$data) { | |
1942 | Imager->_set_error('Could not slurp from buffer'); | |
1943 | return undef; | |
1944 | } | |
1945 | ${$opts->{data}} = $data; | |
02d1d628 | 1946 | } |
2b405c9e | 1947 | return 1; |
02d1d628 AMH |
1948 | } |
1949 | ||
faa9b3e7 TC |
1950 | # read multiple images from a file |
1951 | sub read_multi { | |
1952 | my ($class, %opts) = @_; | |
1953 | ||
53a6bbd4 TC |
1954 | my ($IO, $file) = $class->_get_reader_io(\%opts, $opts{'type'}) |
1955 | or return; | |
1956 | ||
1957 | my $type = $opts{'type'}; | |
1958 | unless ($type) { | |
1959 | $type = i_test_format_probe($IO, -1); | |
1960 | } | |
1961 | ||
1962 | if ($opts{file} && !$type) { | |
faa9b3e7 | 1963 | # guess the type |
53a6bbd4 | 1964 | $type = $FORMATGUESS->($opts{file}); |
faa9b3e7 | 1965 | } |
53a6bbd4 TC |
1966 | |
1967 | unless ($type) { | |
4f21e06e TC |
1968 | my $msg = "type parameter missing and it couldn't be determined from the file contents"; |
1969 | $opts{file} and $msg .= " or file name"; | |
1970 | Imager->_set_error($msg); | |
faa9b3e7 TC |
1971 | return; |
1972 | } | |
faa9b3e7 | 1973 | |
53a6bbd4 TC |
1974 | _reader_autoload($type); |
1975 | ||
1976 | if ($readers{$type} && $readers{$type}{multiple}) { | |
1977 | return $readers{$type}{multiple}->($IO, %opts); | |
1978 | } | |
1979 | ||
8d46e5da TC |
1980 | unless ($formats{$type}) { |
1981 | my $read_types = join ', ', sort Imager->read_types(); | |
1982 | Imager->_set_error("format '$type' not supported - formats $read_types available for reading"); | |
1983 | return; | |
1984 | } | |
1985 | ||
e5ee047b TC |
1986 | my @imgs; |
1987 | if ($type eq 'pnm') { | |
2086be61 | 1988 | @imgs = i_readpnm_multi_wiol($IO, $opts{allow_incomplete}||0); |
faa9b3e7 | 1989 | } |
e7ff1cf7 TC |
1990 | else { |
1991 | my $img = Imager->new; | |
1992 | if ($img->read(%opts, io => $IO, type => $type)) { | |
1993 | return ( $img ); | |
1994 | } | |
f245645a | 1995 | Imager->_set_error($img->errstr); |
2086be61 | 1996 | return; |
e7ff1cf7 | 1997 | } |
faa9b3e7 | 1998 | |
2086be61 TC |
1999 | if (!@imgs) { |
2000 | $ERRSTR = _error_as_msg(); | |
faa9b3e7 | 2001 | return; |
2086be61 TC |
2002 | } |
2003 | return map { | |
2004 | bless { IMG=>$_, DEBUG=>$DEBUG, ERRSTR=>undef }, 'Imager' | |
2005 | } @imgs; | |
faa9b3e7 TC |
2006 | } |
2007 | ||
02d1d628 AMH |
2008 | # Destroy an Imager object |
2009 | ||
2010 | sub DESTROY { | |
2011 | my $self=shift; | |
2012 | # delete $instances{$self}; | |
2013 | if (defined($self->{IMG})) { | |
faa9b3e7 TC |
2014 | # the following is now handled by the XS DESTROY method for |
2015 | # Imager::ImgRaw object | |
2016 | # Re-enabling this will break virtual images | |
2017 | # tested for in t/t020masked.t | |
2018 | # i_img_destroy($self->{IMG}); | |
02d1d628 AMH |
2019 | undef($self->{IMG}); |
2020 | } else { | |
2021 | # print "Destroy Called on an empty image!\n"; # why did I put this here?? | |
2022 | } | |
2023 | } | |
2024 | ||
2025 | # Perform an inplace filter of an image | |
2026 | # that is the image will be overwritten with the data | |
2027 | ||
2028 | sub filter { | |
2029 | my $self=shift; | |
2030 | my %input=@_; | |
2031 | my %hsh; | |
2032 | unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } | |
2033 | ||
9d540150 | 2034 | if (!$input{'type'}) { $self->{ERRSTR}='type parameter missing'; return undef; } |
02d1d628 | 2035 | |
9d540150 | 2036 | if ( (grep { $_ eq $input{'type'} } keys %filters) != 1) { |
02d1d628 AMH |
2037 | $self->{ERRSTR}='type parameter not matching any filter'; return undef; |
2038 | } | |
2039 | ||
9d540150 TC |
2040 | if ($filters{$input{'type'}}{names}) { |
2041 | my $names = $filters{$input{'type'}}{names}; | |
6607600c TC |
2042 | for my $name (keys %$names) { |
2043 | if (defined $input{$name} && exists $names->{$name}{$input{$name}}) { | |
2044 | $input{$name} = $names->{$name}{$input{$name}}; | |
2045 | } | |
2046 | } | |
2047 | } | |
9d540150 | 2048 | if (defined($filters{$input{'type'}}{defaults})) { |
7327d4b0 TC |
2049 | %hsh=( image => $self->{IMG}, |
2050 | imager => $self, | |
2051 | %{$filters{$input{'type'}}{defaults}}, | |
2052 | %input ); | |
02d1d628 | 2053 | } else { |
7327d4b0 TC |
2054 | %hsh=( image => $self->{IMG}, |
2055 | imager => $self, | |
2056 | %input ); | |
02d1d628 AMH |
2057 | } |
2058 | ||
9d540150 | 2059 | my @cs=@{$filters{$input{'type'}}{callseq}}; |
02d1d628 AMH |
2060 | |
2061 | for(@cs) { | |
2062 | if (!defined($hsh{$_})) { | |
9d540150 | 2063 | $self->{ERRSTR}="missing parameter '$_' for filter ".$input{'type'}; return undef; |
02d1d628 AMH |
2064 | } |
2065 | } | |
2066 | ||
109bec2d TC |
2067 | eval { |
2068 | local $SIG{__DIE__}; # we don't want this processed by confess, etc | |
2069 | &{$filters{$input{'type'}}{callsub}}(%hsh); | |
2070 | }; | |
2071 | if ($@) { | |
2072 | chomp($self->{ERRSTR} = $@); | |
2073 | return; | |
2074 | } | |
02d1d628 AMH |
2075 | |
2076 | my @b=keys %hsh; | |
2077 | ||
2078 | $self->{DEBUG} && print "callseq is: @cs\n"; | |
2079 | $self->{DEBUG} && print "matching callseq is: @b\n"; | |
2080 | ||
2081 | return $self; | |
2082 | } | |
2083 | ||
92bda632 TC |
2084 | sub register_filter { |
2085 | my $class = shift; | |
2086 | my %hsh = ( defaults => {}, @_ ); | |
2087 | ||
2088 | defined $hsh{type} | |
2089 | or die "register_filter() with no type\n"; | |
2090 | defined $hsh{callsub} | |
2091 | or die "register_filter() with no callsub\n"; | |
2092 | defined $hsh{callseq} | |
2093 | or die "register_filter() with no callseq\n"; | |
2094 | ||
2095 | exists $filters{$hsh{type}} | |
2096 | and return; | |
2097 | ||
2098 | $filters{$hsh{type}} = \%hsh; | |
2099 | ||
2100 | return 1; | |
2101 | } | |
2102 | ||
df9aaafb TC |
2103 | sub scale_calculate { |
2104 | my $self = shift; | |
02d1d628 | 2105 | |
df9aaafb | 2106 | my %opts = ('type'=>'max', @_); |
4f579313 | 2107 | |
de470892 TC |
2108 | # none of these should be references |
2109 | for my $name (qw/xpixels ypixels xscalefactor yscalefactor width height/) { | |
2110 | if (defined $opts{$name} && ref $opts{$name}) { | |
2111 | $self->_set_error("scale_calculate: $name parameter cannot be a reference"); | |
2112 | return; | |
2113 | } | |
2114 | } | |
2115 | ||
df9aaafb TC |
2116 | my ($x_scale, $y_scale); |
2117 | my $width = $opts{width}; | |
2118 | my $height = $opts{height}; | |
2119 | if (ref $self) { | |
2120 | defined $width or $width = $self->getwidth; | |
2121 | defined $height or $height = $self->getheight; | |
ace46df2 | 2122 | } |
df9aaafb TC |
2123 | else { |
2124 | unless (defined $width && defined $height) { | |
2125 | $self->_set_error("scale_calculate: width and height parameters must be supplied when called as a class method"); | |
2126 | return; | |
2127 | } | |
5168ca3a | 2128 | } |
02d1d628 | 2129 | |
658f724e TC |
2130 | if ($opts{'xscalefactor'} && $opts{'yscalefactor'}) { |
2131 | $x_scale = $opts{'xscalefactor'}; | |
2132 | $y_scale = $opts{'yscalefactor'}; | |
2133 | } | |
2134 | elsif ($opts{'xscalefactor'}) { | |
2135 | $x_scale = $opts{'xscalefactor'}; | |
2136 | $y_scale = $opts{'scalefactor'} || $x_scale; | |
2137 | } | |
2138 | elsif ($opts{'yscalefactor'}) { | |
2139 | $y_scale = $opts{'yscalefactor'}; | |
2140 | $x_scale = $opts{'scalefactor'} || $y_scale; | |
2141 | } | |
2142 | else { | |
2143 | $x_scale = $y_scale = $opts{'scalefactor'} || 0.5; | |
2144 | } | |
2145 | ||
5168ca3a | 2146 | # work out the scaling |
9d540150 | 2147 | if ($opts{xpixels} and $opts{ypixels} and $opts{'type'}) { |
df9aaafb TC |
2148 | my ($xpix, $ypix)=( $opts{xpixels} / $width , |
2149 | $opts{ypixels} / $height ); | |
5168ca3a | 2150 | if ($opts{'type'} eq 'min') { |
658f724e | 2151 | $x_scale = $y_scale = _min($xpix,$ypix); |
5168ca3a TC |
2152 | } |
2153 | elsif ($opts{'type'} eq 'max') { | |
658f724e TC |
2154 | $x_scale = $y_scale = _max($xpix,$ypix); |
2155 | } | |
2156 | elsif ($opts{'type'} eq 'nonprop' || $opts{'type'} eq 'non-proportional') { | |
2157 | $x_scale = $xpix; | |
2158 | $y_scale = $ypix; | |
5168ca3a TC |
2159 | } |
2160 | else { | |
2161 | $self->_set_error('invalid value for type parameter'); | |
df9aaafb | 2162 | return; |
5168ca3a TC |
2163 | } |
2164 | } elsif ($opts{xpixels}) { | |
df9aaafb | 2165 | $x_scale = $y_scale = $opts{xpixels} / $width; |
5168ca3a TC |
2166 | } |
2167 | elsif ($opts{ypixels}) { | |
df9aaafb | 2168 | $x_scale = $y_scale = $opts{ypixels}/$height; |
5168ca3a | 2169 | } |
41c7d053 TC |
2170 | elsif ($opts{constrain} && ref $opts{constrain} |
2171 | && $opts{constrain}->can('constrain')) { | |
2172 | # we've been passed an Image::Math::Constrain object or something | |
2173 | # that looks like one | |
658f724e | 2174 | my $scalefactor; |
4f579313 | 2175 | (undef, undef, $scalefactor) |
41c7d053 | 2176 | = $opts{constrain}->constrain($self->getwidth, $self->getheight); |
4f579313 | 2177 | unless ($scalefactor) { |
41c7d053 | 2178 | $self->_set_error('constrain method failed on constrain parameter'); |
df9aaafb | 2179 | return; |
41c7d053 | 2180 | } |
658f724e | 2181 | $x_scale = $y_scale = $scalefactor; |
41c7d053 | 2182 | } |
02d1d628 | 2183 | |
df9aaafb TC |
2184 | my $new_width = int($x_scale * $width + 0.5); |
2185 | $new_width > 0 or $new_width = 1; | |
2186 | my $new_height = int($y_scale * $height + 0.5); | |
2187 | $new_height > 0 or $new_height = 1; | |
2188 | ||
2189 | return ($x_scale, $y_scale, $new_width, $new_height); | |
2190 | ||
2191 | } | |
2192 | ||
2193 | # Scale an image to requested size and return the scaled version | |
2194 | ||
2195 | sub scale { | |
2196 | my $self=shift; | |
2197 | my %opts = (qtype=>'normal' ,@_); | |
2198 | my $img = Imager->new(); | |
2199 | my $tmp = Imager->new(); | |
2200 | ||
2201 | unless (defined wantarray) { | |
2202 | my @caller = caller; | |
2203 | warn "scale() called in void context - scale() returns the scaled image at $caller[1] line $caller[2]\n"; | |
2204 | return; | |
2205 | } | |
2206 | ||
2207 | unless ($self->{IMG}) { | |
2208 | $self->_set_error('empty input image'); | |
2209 | return undef; | |
2210 | } | |
2211 | ||
2212 | my ($x_scale, $y_scale, $new_width, $new_height) = | |
2213 | $self->scale_calculate(%opts) | |
2214 | or return; | |
2215 | ||
02d1d628 | 2216 | if ($opts{qtype} eq 'normal') { |
658f724e | 2217 | $tmp->{IMG} = i_scaleaxis($self->{IMG}, $x_scale, 0); |
5168ca3a | 2218 | if ( !defined($tmp->{IMG}) ) { |
de470892 | 2219 | $self->{ERRSTR} = 'unable to scale image: ' . $self->_error_as_msg; |
5168ca3a TC |
2220 | return undef; |
2221 | } | |
658f724e | 2222 | $img->{IMG}=i_scaleaxis($tmp->{IMG}, $y_scale, 1); |
5168ca3a | 2223 | if ( !defined($img->{IMG}) ) { |
de470892 | 2224 | $self->{ERRSTR}='unable to scale image: ' . $self->_error_as_msg; |
5168ca3a TC |
2225 | return undef; |
2226 | } | |
2227 | ||
02d1d628 AMH |
2228 | return $img; |
2229 | } | |
5168ca3a | 2230 | elsif ($opts{'qtype'} eq 'preview') { |
658f724e | 2231 | $img->{IMG} = i_scale_nn($self->{IMG}, $x_scale, $y_scale); |
5168ca3a TC |
2232 | if ( !defined($img->{IMG}) ) { |
2233 | $self->{ERRSTR}='unable to scale image'; | |
2234 | return undef; | |
2235 | } | |
02d1d628 AMH |
2236 | return $img; |
2237 | } | |
658f724e | 2238 | elsif ($opts{'qtype'} eq 'mixing') { |
658f724e TC |
2239 | $img->{IMG} = i_scale_mixing($self->{IMG}, $new_width, $new_height); |
2240 | unless ($img->{IMG}) { | |
de470892 | 2241 | $self->_set_error(Imager->_error_as_msg); |
658f724e TC |
2242 | return; |
2243 | } | |
2244 | return $img; | |
2245 | } | |
5168ca3a TC |
2246 | else { |
2247 | $self->_set_error('invalid value for qtype parameter'); | |
2248 | return undef; | |
2249 | } | |
02d1d628 AMH |
2250 | } |
2251 | ||
2252 | # Scales only along the X axis | |
2253 | ||
2254 | sub scaleX { | |
15327bf5 TC |
2255 | my $self = shift; |
2256 | my %opts = ( scalefactor=>0.5, @_ ); | |
02d1d628 | 2257 | |
34b3f7e6 TC |
2258 | unless (defined wantarray) { |
2259 | my @caller = caller; | |
2260 | warn "scaleX() called in void context - scaleX() returns the scaled image at $caller[1] line $caller[2]\n"; | |
2261 | return; | |
2262 | } | |
2263 | ||
15327bf5 TC |
2264 | unless ($self->{IMG}) { |
2265 | $self->{ERRSTR} = 'empty input image'; | |
2266 | return undef; | |
2267 | } | |
02d1d628 AMH |
2268 | |
2269 | my $img = Imager->new(); | |
2270 | ||
15327bf5 | 2271 | my $scalefactor = $opts{scalefactor}; |
02d1d628 | 2272 | |
15327bf5 TC |
2273 | if ($opts{pixels}) { |
2274 | $scalefactor = $opts{pixels} / $self->getwidth(); | |
2275 | } | |
2276 | ||
2277 | unless ($self->{IMG}) { | |
2278 | $self->{ERRSTR}='empty input image'; | |
2279 | return undef; | |
2280 | } | |
2281 | ||
2282 | $img->{IMG} = i_scaleaxis($self->{IMG}, $scalefactor, 0); | |
2283 | ||
2284 | if ( !defined($img->{IMG}) ) { | |
2285 | $self->{ERRSTR} = 'unable to scale image'; | |
2286 | return undef; | |
2287 | } | |
02d1d628 | 2288 | |
02d1d628 AMH |
2289 | return $img; |
2290 | } | |
2291 | ||
2292 | # Scales only along the Y axis | |
2293 | ||
2294 | sub scaleY { | |
15327bf5 TC |
2295 | my $self = shift; |
2296 | my %opts = ( scalefactor => 0.5, @_ ); | |
02d1d628 | 2297 | |
34b3f7e6 TC |
2298 | unless (defined wantarray) { |
2299 | my @caller = caller; | |
2300 | warn "scaleY() called in void context - scaleY() returns the scaled image at $caller[1] line $caller[2]\n"; | |
2301 | return; | |
2302 | } | |
2303 | ||
02d1d628 AMH |
2304 | unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } |
2305 | ||
2306 | my $img = Imager->new(); | |
2307 | ||
15327bf5 | 2308 | my $scalefactor = $opts{scalefactor}; |
02d1d628 | 2309 | |
15327bf5 TC |
2310 | if ($opts{pixels}) { |
2311 | $scalefactor = $opts{pixels} / $self->getheight(); | |
2312 | } | |
2313 | ||
2314 | unless ($self->{IMG}) { | |
2315 | $self->{ERRSTR} = 'empty input image'; | |
2316 | return undef; | |
2317 | } | |
2318 | $img->{IMG}=i_scaleaxis($self->{IMG}, $scalefactor, 1); | |
2319 | ||
2320 | if ( !defined($img->{IMG}) ) { | |
2321 | $self->{ERRSTR} = 'unable to scale image'; | |
2322 | return undef; | |
2323 | } | |
02d1d628 | 2324 | |
02d1d628 AMH |
2325 | return $img; |
2326 | } | |
2327 | ||
02d1d628 AMH |
2328 | # Transform returns a spatial transformation of the input image |
2329 | # this moves pixels to a new location in the returned image. | |
2330 | # NOTE - should make a utility function to check transforms for | |
2331 | # stack overruns | |
2332 | ||
2333 | sub transform { | |
2334 | my $self=shift; | |
2335 | unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } | |
2336 | my %opts=@_; | |
2337 | my (@op,@ropx,@ropy,$iop,$or,@parm,$expr,@xt,@yt,@pt,$numre); | |
2338 | ||
2339 | # print Dumper(\%opts); | |
2340 | # xopcopdes | |
2341 | ||
2342 | if ( $opts{'xexpr'} and $opts{'yexpr'} ) { | |
2343 | if (!$I2P) { | |
2344 | eval ("use Affix::Infix2Postfix;"); | |
2345 | print $@; | |
2346 | if ( $@ ) { | |
2347 | $self->{ERRSTR}='transform: expr given and Affix::Infix2Postfix is not avaliable.'; | |
2348 | return undef; | |
2349 | } | |
2350 | $I2P=Affix::Infix2Postfix->new('ops'=>[{op=>'+',trans=>'Add'}, | |
2351 | {op=>'-',trans=>'Sub'}, | |
2352 | {op=>'*',trans=>'Mult'}, | |
2353 | {op=>'/',trans=>'Div'}, | |
9d540150 | 2354 | {op=>'-','type'=>'unary',trans=>'u-'}, |
02d1d628 | 2355 | {op=>'**'}, |
9d540150 | 2356 | {op=>'func','type'=>'unary'}], |
02d1d628 AMH |
2357 | 'grouping'=>[qw( \( \) )], |
2358 | 'func'=>[qw( sin cos )], | |
2359 | 'vars'=>[qw( x y )] | |
2360 | ); | |
2361 | } | |
2362 | ||
2363 | @xt=$I2P->translate($opts{'xexpr'}); | |
2364 | @yt=$I2P->translate($opts{'yexpr'}); | |
2365 | ||
2366 | $numre=$I2P->{'numre'}; | |
2367 | @pt=(0,0); | |
2368 | ||
2369 | for(@xt) { if (/$numre/) { push(@pt,$_); push(@{$opts{'xopcodes'}},'Parm',$#pt); } else { push(@{$opts{'xopcodes'}},$_); } } | |
2370 | for(@yt) { if (/$numre/) { push(@pt,$_); push(@{$opts{'yopcodes'}},'Parm',$#pt); } else { push(@{$opts{'yopcodes'}},$_); } } | |
2371 | @{$opts{'parm'}}=@pt; | |
2372 | } | |
2373 | ||
2374 | # print Dumper(\%opts); | |
2375 | ||
2376 | if ( !exists $opts{'xopcodes'} or @{$opts{'xopcodes'}}==0) { | |
2377 | $self->{ERRSTR}='transform: no xopcodes given.'; | |
2378 | return undef; | |
2379 | } | |
2380 | ||
2381 | @op=@{$opts{'xopcodes'}}; | |
2382 | for $iop (@op) { | |
2383 | if (!defined ($OPCODES{$iop}) and ($iop !~ /^\d+$/) ) { | |
2384 | $self->{ERRSTR}="transform: illegal opcode '$_'."; | |
2385 | return undef; | |
2386 | } | |
2387 | push(@ropx,(exists $OPCODES{$iop}) ? @{$OPCODES{$iop}} : $iop ); | |
2388 | } | |
2389 | ||
2390 | ||
2391 | # yopcopdes | |
2392 | ||
2393 | if ( !exists $opts{'yopcodes'} or @{$opts{'yopcodes'}}==0) { | |
2394 | $self->{ERRSTR}='transform: no yopcodes given.'; | |
2395 | return undef; | |
2396 | } | |
2397 | ||
2398 | @op=@{$opts{'yopcodes'}}; | |
2399 | for $iop (@op) { | |
2400 | if (!defined ($OPCODES{$iop}) and ($iop !~ /^\d+$/) ) { | |
2401 | $self->{ERRSTR}="transform: illegal opcode '$_'."; | |
2402 | return undef; | |
2403 | } | |
2404 | push(@ropy,(exists $OPCODES{$iop}) ? @{$OPCODES{$iop}} : $iop ); | |
2405 | } | |
2406 | ||
2407 | #parameters | |
2408 | ||
2409 | if ( !exists $opts{'parm'}) { | |
2410 | $self->{ERRSTR}='transform: no parameter arg given.'; | |
2411 | return undef; | |
2412 | } | |
2413 | ||
2414 | # print Dumper(\@ropx); | |
2415 | # print Dumper(\@ropy); | |
2416 | # print Dumper(\@ropy); | |
2417 | ||
2418 | my $img = Imager->new(); | |
2419 | $img->{IMG}=i_transform($self->{IMG},\@ropx,\@ropy,$opts{'parm'}); | |
2420 | if ( !defined($img->{IMG}) ) { $self->{ERRSTR}='transform: failed'; return undef; } | |
2421 | return $img; | |
2422 | } | |
2423 | ||
2424 | ||
bf94b653 TC |
2425 | sub transform2 { |
2426 | my ($opts, @imgs) = @_; | |
2427 | ||
2428 | require "Imager/Expr.pm"; | |
2429 | ||
2430 | $opts->{variables} = [ qw(x y) ]; | |
2431 | my ($width, $height) = @{$opts}{qw(width height)}; | |
2432 | if (@imgs) { | |
2433 | $width ||= $imgs[0]->getwidth(); | |
2434 | $height ||= $imgs[0]->getheight(); | |
2435 | my $img_num = 1; | |
2436 | for my $img (@imgs) { | |
2437 | $opts->{constants}{"w$img_num"} = $img->getwidth(); | |
2438 | $opts->{constants}{"h$img_num"} = $img->getheight(); | |
2439 | $opts->{constants}{"cx$img_num"} = $img->getwidth()/2; | |
2440 | $opts->{constants}{"cy$img_num"} = $img->getheight()/2; | |
2441 | ++$img_num; | |
02d1d628 | 2442 | } |
02d1d628 | 2443 | } |
bf94b653 TC |
2444 | if ($width) { |
2445 | $opts->{constants}{w} = $width; | |
2446 | $opts->{constants}{cx} = $width/2; | |
2447 | } | |
2448 | else { | |
2449 | $Imager::ERRSTR = "No width supplied"; | |
2450 | return; | |
2451 | } | |
2452 | if ($height) { | |
2453 | $opts->{constants}{h} = $height; | |
2454 | $opts->{constants}{cy} = $height/2; | |
2455 | } | |
2456 | else { | |
2457 | $Imager::ERRSTR = "No height supplied"; | |
2458 | return; | |
2459 | } | |
2460 | my $code = Imager::Expr->new($opts); | |
2461 | if (!$code) { | |
2462 | $Imager::ERRSTR = Imager::Expr::error(); | |
2463 | return; | |
2464 | } | |
e5744e01 TC |
2465 | my $channels = $opts->{channels} || 3; |
2466 | unless ($channels >= 1 && $channels <= 4) { | |
2467 | return Imager->_set_error("channels must be an integer between 1 and 4"); | |
2468 | } | |
9982a307 | 2469 | |
bf94b653 | 2470 | my $img = Imager->new(); |
e5744e01 TC |
2471 | $img->{IMG} = i_transform2($opts->{width}, $opts->{height}, |
2472 | $channels, $code->code(), | |
bf94b653 TC |
2473 | $code->nregs(), $code->cregs(), |
2474 | [ map { $_->{IMG} } @imgs ]); | |
2475 | if (!defined $img->{IMG}) { | |
2476 | $Imager::ERRSTR = Imager->_error_as_msg(); | |
2477 | return; | |
2478 | } | |
9982a307 | 2479 | |
bf94b653 | 2480 | return $img; |
02d1d628 AMH |
2481 | } |
2482 | ||
02d1d628 AMH |
2483 | sub rubthrough { |
2484 | my $self=shift; | |
9b1ec2b8 | 2485 | my %opts= @_; |
02d1d628 | 2486 | |
e7b95388 TC |
2487 | unless ($self->{IMG}) { |
2488 | $self->{ERRSTR}='empty input image'; | |
2489 | return undef; | |
2490 | } | |
2491 | unless ($opts{src} && $opts{src}->{IMG}) { | |
2492 | $self->{ERRSTR}='empty input image for src'; | |
2493 | return undef; | |
2494 | } | |
02d1d628 | 2495 | |
71dc4a83 AMH |
2496 | %opts = (src_minx => 0, |
2497 | src_miny => 0, | |
2498 | src_maxx => $opts{src}->getwidth(), | |
2499 | src_maxy => $opts{src}->getheight(), | |
2500 | %opts); | |
2501 | ||
9b1ec2b8 TC |
2502 | my $tx = $opts{tx}; |
2503 | defined $tx or $tx = $opts{left}; | |
2504 | defined $tx or $tx = 0; | |
2505 | ||
2506 | my $ty = $opts{ty}; | |
2507 | defined $ty or $ty = $opts{top}; | |
2508 | defined $ty or $ty = 0; | |
2509 | ||
2510 | unless (i_rubthru($self->{IMG}, $opts{src}->{IMG}, $tx, $ty, | |
e7b95388 TC |
2511 | $opts{src_minx}, $opts{src_miny}, |
2512 | $opts{src_maxx}, $opts{src_maxy})) { | |
2513 | $self->_set_error($self->_error_as_msg()); | |
faa9b3e7 TC |
2514 | return undef; |
2515 | } | |
9b1ec2b8 | 2516 | |
02d1d628 AMH |
2517 | return $self; |
2518 | } | |
2519 | ||
9b1ec2b8 TC |
2520 | sub compose { |
2521 | my $self = shift; | |
2522 | my %opts = | |
2523 | ( | |
2524 | opacity => 1.0, | |
2525 | mask_left => 0, | |
2526 | mask_top => 0, | |
2527 | @_ | |
2528 | ); | |
2529 | ||
2530 | unless ($self->{IMG}) { | |
2531 | $self->_set_error("compose: empty input image"); | |
2532 | return; | |
2533 | } | |
2534 | ||
2535 | unless ($opts{src}) { | |
2536 | $self->_set_error("compose: src parameter missing"); | |
2537 | return; | |
2538 | } | |
2539 | ||
2540 | unless ($opts{src}{IMG}) { | |
2541 | $self->_set_error("compose: src parameter empty image"); | |
2542 | return; | |
2543 | } | |
2544 | my $src = $opts{src}; | |
2545 | ||
2546 | my $left = $opts{left}; | |
2547 | defined $left or $left = $opts{tx}; | |
2548 | defined $left or $left = 0; | |
2549 | ||
2550 | my $top = $opts{top}; | |
2551 | defined $top or $top = $opts{ty}; | |
2552 | defined $top or $top = 0; | |
2553 | ||
2554 | my $src_left = $opts{src_left}; | |
2555 | defined $src_left or $src_left = $opts{src_minx}; | |
2556 | defined $src_left or $src_left = 0; | |
2557 | ||
2558 | my $src_top = $opts{src_top}; | |
2559 | defined $src_top or $src_top = $opts{src_miny}; | |
2560 | defined $src_top or $src_top = 0; | |
2561 | ||
2562 | my $width = $opts{width}; | |
2563 | if (!defined $width && defined $opts{src_maxx}) { | |
2564 | $width = $opts{src_maxx} - $src_left; | |
2565 | } | |
2566 | defined $width or $width = $src->getwidth() - $src_left; | |
2567 | ||
2568 | my $height = $opts{height}; | |
2569 | if (!defined $height && defined $opts{src_maxy}) { | |
2570 | $height = $opts{src_maxy} - $src_top; | |
2571 | } | |
2572 | defined $height or $height = $src->getheight() - $src_top; | |
2573 | ||
2574 | my $combine = $self->_combine($opts{combine}, 'normal'); | |
2575 | ||
2576 | if ($opts{mask}) { | |
2577 | unless ($opts{mask}{IMG}) { | |
2578 | $self->_set_error("compose: mask parameter empty image"); | |
2579 | return; | |
2580 | } | |
2581 | ||
2582 | my $mask_left = $opts{mask_left}; | |
2583 | defined $mask_left or $mask_left = $opts{mask_minx}; | |
2584 | defined $mask_left or $mask_left = 0; | |
2585 | ||
2586 | my $mask_top = $opts{mask_top}; | |
2587 | defined $mask_top or $mask_top = $opts{mask_miny}; | |
2588 | defined $mask_top or $mask_top = 0; | |
2589 | ||
618a3282 | 2590 | unless (i_compose_mask($self->{IMG}, $src->{IMG}, $opts{mask}{IMG}, |
9b1ec2b8 TC |
2591 | $left, $top, $src_left, $src_top, |
2592 | $mask_left, $mask_top, $width, $height, | |
618a3282 TC |
2593 | $combine, $opts{opacity})) { |
2594 | $self->_set_error(Imager->_error_as_msg); | |
2595 | return; | |
2596 | } | |
9b1ec2b8 TC |
2597 | } |
2598 | else { | |
618a3282 TC |
2599 | unless (i_compose($self->{IMG}, $src->{IMG}, $left, $top, $src_left, $src_top, |
2600 | $width, $height, $combine, $opts{opacity})) { | |
2601 | $self->_set_error(Imager->_error_as_msg); | |
2602 | return; | |
2603 | } | |
9b1ec2b8 TC |
2604 | } |
2605 | ||
2606 | return $self; | |
2607 | } | |
02d1d628 | 2608 | |
142c26ff AMH |
2609 | sub flip { |
2610 | my $self = shift; | |
2611 | my %opts = @_; | |
9191e525 | 2612 | my %xlate = (h=>0, v=>1, hv=>2, vh=>2); |
142c26ff AMH |
2613 | my $dir; |
2614 | return () unless defined $opts{'dir'} and defined $xlate{$opts{'dir'}}; | |
2615 | $dir = $xlate{$opts{'dir'}}; | |
2616 | return $self if i_flipxy($self->{IMG}, $dir); | |
2617 | return (); | |
2618 | } | |
2619 | ||
faa9b3e7 TC |
2620 | sub rotate { |
2621 | my $self = shift; | |
2622 | my %opts = @_; | |
34b3f7e6 TC |
2623 | |
2624 | unless (defined wantarray) { | |
2625 | my @caller = caller; | |
2626 | warn "rotate() called in void context - rotate() returns the rotated image at $caller[1] line $caller[2]\n"; | |
2627 | return; | |
2628 | } | |
2629 | ||
faa9b3e7 TC |
2630 | if (defined $opts{right}) { |
2631 | my $degrees = $opts{right}; | |
2632 | if ($degrees < 0) { | |
2633 | $degrees += 360 * int(((-$degrees)+360)/360); | |
2634 | } | |
2635 | $degrees = $degrees % 360; | |
2636 | if ($degrees == 0) { | |
2637 | return $self->copy(); | |
2638 | } | |
2639 | elsif ($degrees == 90 || $degrees == 180 || $degrees == 270) { | |
2640 | my $result = Imager->new(); | |
2641 | if ($result->{IMG} = i_rotate90($self->{IMG}, $degrees)) { | |
2642 | return $result; | |
2643 | } | |
2644 | else { | |
2645 | $self->{ERRSTR} = $self->_error_as_msg(); | |
2646 | return undef; | |
2647 | } | |
2648 | } | |
2649 | else { | |
2650 | $self->{ERRSTR} = "Parameter 'right' must be a multiple of 90 degrees"; | |
2651 | return undef; | |
2652 | } | |
2653 | } | |
2654 | elsif (defined $opts{radians} || defined $opts{degrees}) { | |
2655 | my $amount = $opts{radians} || $opts{degrees} * 3.1415926535 / 180; | |
2656 | ||
7f627571 | 2657 | my $back = $opts{back}; |
faa9b3e7 | 2658 | my $result = Imager->new; |
7f627571 TC |
2659 | if ($back) { |
2660 | $back = _color($back); | |
2661 | unless ($back) { | |
2662 | $self->_set_error(Imager->errstr); | |
2663 | return undef; | |
2664 | } | |
2665 | ||
2666 | $result->{IMG} = i_rotate_exact($self->{IMG}, $amount, $back); | |
0d3b936e TC |
2667 | } |
2668 | else { | |
2669 | $result->{IMG} = i_rotate_exact($self->{IMG}, $amount); | |
2670 | } | |
2671 | if ($result->{IMG}) { | |
faa9b3e7 TC |
2672 | return $result; |
2673 | } | |
2674 | else { | |
2675 | $self->{ERRSTR} = $self->_error_as_msg(); | |
2676 | return undef; | |
2677 | } | |
2678 | } | |
2679 | else { | |
0d3b936e | 2680 | $self->{ERRSTR} = "Only the 'right', 'radians' and 'degrees' parameters are available"; |
faa9b3e7 TC |
2681 | return undef; |
2682 | } | |
2683 | } | |
2684 | ||
2685 | sub matrix_transform { | |
2686 | my $self = shift; | |
2687 | my %opts = @_; | |
2688 | ||
34b3f7e6 TC |
2689 | unless (defined wantarray) { |
2690 | my @caller = caller; | |
2691 | warn "copy() called in void context - copy() returns the copied image at $caller[1] line $caller[2]\n"; | |
2692 | return; | |
2693 | } | |
2694 | ||
faa9b3e7 TC |
2695 | if ($opts{matrix}) { |
2696 | my $xsize = $opts{xsize} || $self->getwidth; | |
2697 | my $ysize = $opts{ysize} || $self->getheight; | |
142c26ff | 2698 | |
faa9b3e7 | 2699 | my $result = Imager->new; |
0d3b936e TC |
2700 | if ($opts{back}) { |
2701 | $result->{IMG} = i_matrix_transform($self->{IMG}, $xsize, $ysize, | |
2702 | $opts{matrix}, $opts{back}) | |
2703 | or return undef; | |
2704 | } | |
2705 | else { | |
2706 | $result->{IMG} = i_matrix_transform($self->{IMG}, $xsize, $ysize, | |
2707 | $opts{matrix}) | |
2708 | or return undef; | |
2709 | } | |
faa9b3e7 TC |
2710 | |
2711 | return $result; | |
2712 | } | |
2713 | else { | |
2714 | $self->{ERRSTR} = "matrix parameter required"; | |
2715 | return undef; | |
2716 | } | |
2717 | } | |
2718 | ||
2719 | # blame Leolo :) | |
2720 | *yatf = \&matrix_transform; | |
02d1d628 AMH |
2721 | |
2722 | # These two are supported for legacy code only | |
2723 | ||
2724 | sub i_color_new { | |
faa9b3e7 | 2725 | return Imager::Color->new(@_); |
02d1d628 AMH |
2726 | } |
2727 | ||
2728 | sub i_color_set { | |
faa9b3e7 | 2729 | return Imager::Color::set(@_); |
02d1d628 AMH |
2730 | } |
2731 | ||
02d1d628 | 2732 | # Draws a box between the specified corner points. |
02d1d628 AMH |
2733 | sub box { |
2734 | my $self=shift; | |
3b000586 TC |
2735 | my $raw = $self->{IMG}; |
2736 | ||
2737 | unless ($raw) { | |
2738 | $self->{ERRSTR}='empty input image'; | |
2739 | return undef; | |
2740 | } | |
2741 | ||
2742 | my %opts = @_; | |
02d1d628 | 2743 | |
3b000586 | 2744 | my ($xmin, $ymin, $xmax, $ymax); |
02d1d628 | 2745 | if (exists $opts{'box'}) { |
3b000586 TC |
2746 | $xmin = _min($opts{'box'}->[0],$opts{'box'}->[2]); |
2747 | $xmax = _max($opts{'box'}->[0],$opts{'box'}->[2]); | |
2748 | $ymin = _min($opts{'box'}->[1],$opts{'box'}->[3]); | |
2749 | $ymax = _max($opts{'box'}->[1],$opts{'box'}->[3]); | |
2750 | } | |
2751 | else { | |
2752 | defined($xmin = $opts{xmin}) or $xmin = 0; | |
2753 | defined($xmax = $opts{xmax}) or $xmax = $self->getwidth()-1; | |
2754 | defined($ymin = $opts{ymin}) or $ymin = 0; | |
2755 | defined($ymax = $opts{ymax}) or $ymax = $self->getheight()-1; | |
02d1d628 AMH |
2756 | } |
2757 | ||
f1ac5027 | 2758 | if ($opts{filled}) { |
4dd88895 TC |
2759 | my $color = $opts{'color'}; |
2760 | ||
2761 | if (defined $color) { | |
813d4d0a | 2762 | unless (_is_color_object($color)) { |
4dd88895 TC |
2763 | $color = _color($color); |
2764 | unless ($color) { | |
2765 | $self->{ERRSTR} = $Imager::ERRSTR; | |
2766 | return; | |
2767 | } | |
2768 | } | |
3a9a4241 | 2769 | } |
4dd88895 TC |
2770 | else { |
2771 | $color = i_color_new(255,255,255,255); | |
2772 | } | |
2773 | ||
7477ff14 TC |
2774 | if ($color->isa("Imager::Color")) { |
2775 | i_box_filled($raw, $xmin, $ymin,$xmax, $ymax, $color); | |
2776 | } | |
2777 | else { | |
2778 | i_box_filledf($raw, $xmin, $ymin,$xmax, $ymax, $color); | |
2779 | } | |
f1ac5027 TC |
2780 | } |
2781 | elsif ($opts{fill}) { | |
2782 | unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) { | |
2783 | # assume it's a hash ref | |
2784 | require 'Imager/Fill.pm'; | |
141a6114 TC |
2785 | unless ($opts{fill} = Imager::Fill->new(%{$opts{fill}})) { |
2786 | $self->{ERRSTR} = $Imager::ERRSTR; | |
2787 | return undef; | |
2788 | } | |
f1ac5027 | 2789 | } |
3b000586 | 2790 | i_box_cfill($raw, $xmin, $ymin, $xmax, $ymax, $opts{fill}{fill}); |
f1ac5027 | 2791 | } |
cdd23610 | 2792 | else { |
4dd88895 TC |
2793 | my $color = $opts{'color'}; |
2794 | if (defined $color) { | |
813d4d0a | 2795 | unless (_is_color_object($color)) { |
4dd88895 TC |
2796 | $color = _color($color); |
2797 | unless ($color) { | |
2798 | $self->{ERRSTR} = $Imager::ERRSTR; | |
2799 | return; | |
2800 | } | |
2801 | } | |
2802 | } | |
2803 | else { | |
2804 | $color = i_color_new(255, 255, 255, 255); | |
2805 | } | |
3a9a4241 | 2806 | unless ($color) { |
cdd23610 AMH |
2807 | $self->{ERRSTR} = $Imager::ERRSTR; |
2808 | return; | |
3a9a4241 | 2809 | } |
3b000586 | 2810 | i_box($raw, $xmin, $ymin, $xmax, $ymax, $color); |
f1ac5027 | 2811 | } |
3b000586 | 2812 | |
02d1d628 AMH |
2813 | return $self; |
2814 | } | |
2815 | ||
02d1d628 AMH |
2816 | sub arc { |
2817 | my $self=shift; | |
2818 | unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } | |
40068b33 TC |
2819 | my $dflcl= [ 255, 255, 255, 255]; |
2820 | my $good = 1; | |
2821 | my %opts= | |
2822 | ( | |
2823 | color=>$dflcl, | |
2824 | 'r'=>_min($self->getwidth(),$self->getheight())/3, | |
2825 | 'x'=>$self->getwidth()/2, | |
2826 | 'y'=>$self->getheight()/2, | |
2827 | 'd1'=>0, 'd2'=>361, | |
2828 | filled => 1, | |
2829 | @_, | |
2830 | ); | |
a8652edf TC |
2831 | if ($opts{aa}) { |
2832 | if ($opts{fill}) { | |
2833 | unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) { | |
2834 | # assume it's a hash ref | |
2835 | require 'Imager/Fill.pm'; | |
2836 | unless ($opts{fill} = Imager::Fill->new(%{$opts{fill}})) { | |
2837 | $self->{ERRSTR} = $Imager::ERRSTR; | |
2838 | return; | |
2839 | } | |
2840 | } | |
2841 | i_arc_aa_cfill($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'},$opts{'d1'}, | |
2842 | $opts{'d2'}, $opts{fill}{fill}); | |
2843 | } | |
40068b33 | 2844 | elsif ($opts{filled}) { |
a8652edf TC |
2845 | my $color = _color($opts{'color'}); |
2846 | unless ($color) { | |
2847 | $self->{ERRSTR} = $Imager::ERRSTR; | |
2848 | return; | |
2849 | } | |
2850 | if ($opts{d1} == 0 && $opts{d2} == 361 && $opts{aa}) { | |
2851 | i_circle_aa($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{'r'}, | |
2852 | $color); | |
2853 | } | |
2854 | else { | |
2855 | i_arc_aa($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'}, | |
2856 | $opts{'d1'}, $opts{'d2'}, $color); | |
569795e8 | 2857 | } |
f1ac5027 | 2858 | } |
40068b33 TC |
2859 | else { |
2860 | my $color = _color($opts{'color'}); | |
2861 | if ($opts{d2} - $opts{d1} >= 360) { | |
2862 | $good = i_circle_out_aa($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{'r'}, $color); | |
2863 | } | |
2864 | else { | |
2865 | $good = i_arc_out_aa($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{'r'}, $opts{'d1'}, $opts{'d2'}, $color); | |
2866 | } | |
2867 | } | |
f1ac5027 TC |
2868 | } |
2869 | else { | |
a8652edf TC |
2870 | if ($opts{fill}) { |
2871 | unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) { | |
2872 | # assume it's a hash ref | |
2873 | require 'Imager/Fill.pm'; | |
2874 | unless ($opts{fill} = Imager::Fill->new(%{$opts{fill}})) { | |
2875 | $self->{ERRSTR} = $Imager::ERRSTR; | |
2876 | return; | |
2877 | } | |
2878 | } | |
2879 | i_arc_cfill($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'},$opts{'d1'}, | |
2880 | $opts{'d2'}, $opts{fill}{fill}); | |
0d321238 TC |
2881 | } |
2882 | else { | |
a8652edf TC |
2883 | my $color = _color($opts{'color'}); |
2884 | unless ($color) { | |
2885 | $self->{ERRSTR} = $Imager::ERRSTR; | |
40068b33 TC |
2886 | return; |
2887 | } | |
2888 | if ($opts{filled}) { | |
2889 | i_arc($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'}, | |
2890 | $opts{'d1'}, $opts{'d2'}, $color); | |
2891 | } | |
2892 | else { | |
2893 | if ($opts{d1} == 0 && $opts{d2} == 361) { | |
2894 | $good = i_circle_out($self->{IMG}, $opts{x}, $opts{y}, $opts{r}, $color); | |
2895 | } | |
2896 | else { | |
2897 | $good = i_arc_out($self->{IMG}, $opts{x}, $opts{y}, $opts{r}, $opts{d1}, $opts{d2}, $color); | |
2898 | } | |
a8652edf | 2899 | } |
0d321238 | 2900 | } |
f1ac5027 | 2901 | } |
40068b33 TC |
2902 | unless ($good) { |
2903 | $self->_set_error($self->_error_as_msg); | |
2904 | return; | |
2905 | } | |
f1ac5027 | 2906 | |
02d1d628 AMH |
2907 | return $self; |
2908 | } | |
2909 | ||
aa833c97 AMH |
2910 | # Draws a line from one point to the other |
2911 | # the endpoint is set if the endp parameter is set which it is by default. | |
2912 | # to turn of the endpoint being set use endp=>0 when calling line. | |
02d1d628 AMH |
2913 | |
2914 | sub line { | |
2915 | my $self=shift; | |
2916 | my $dflcl=i_color_new(0,0,0,0); | |
aa833c97 AMH |
2917 | my %opts=(color=>$dflcl, |
2918 | endp => 1, | |
2919 | @_); | |
02d1d628 AMH |
2920 | unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } |
2921 | ||
2922 | unless (exists $opts{x1} and exists $opts{y1}) { $self->{ERRSTR}='missing begining coord'; return undef; } | |
2923 | unless (exists $opts{x2} and exists $opts{y2}) { $self->{ERRSTR}='missing ending coord'; return undef; } | |
2924 | ||
3a9a4241 | 2925 | my $color = _color($opts{'color'}); |
aa833c97 AMH |
2926 | unless ($color) { |
2927 | $self->{ERRSTR} = $Imager::ERRSTR; | |
2928 | return; | |
3a9a4241 | 2929 | } |
aa833c97 | 2930 | |
3a9a4241 | 2931 | $opts{antialias} = $opts{aa} if defined $opts{aa}; |
02d1d628 | 2932 | if ($opts{antialias}) { |
aa833c97 | 2933 | i_line_aa($self->{IMG},$opts{x1}, $opts{y1}, $opts{x2}, $opts{y2}, |
b437ce0a | 2934 | $color, $opts{endp}); |
02d1d628 | 2935 | } else { |
aa833c97 AMH |
2936 | i_line($self->{IMG},$opts{x1}, $opts{y1}, $opts{x2}, $opts{y2}, |
2937 | $color, $opts{endp}); | |
02d1d628 AMH |
2938 | } |
2939 | return $self; | |
2940 | } | |
2941 | ||
2942 | # Draws a line between an ordered set of points - It more or less just transforms this | |
2943 | # into a list of lines. | |
2944 | ||
2945 | sub polyline { | |
2946 | my $self=shift; | |
2947 | my ($pt,$ls,@points); | |
2948 | my $dflcl=i_color_new(0,0,0,0); | |
2949 | my %opts=(color=>$dflcl,@_); | |
2950 | ||
2951 | unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } | |
2952 | ||
2953 | if (exists($opts{points})) { @points=@{$opts{points}}; } | |
2954 | if (!exists($opts{points}) and exists($opts{'x'}) and exists($opts{'y'}) ) { | |
2955 | @points=map { [ $opts{'x'}->[$_],$opts{'y'}->[$_] ] } (0..(scalar @{$opts{'x'}}-1)); | |
2956 | } | |
2957 | ||
2958 | # print Dumper(\@points); | |
2959 | ||
3a9a4241 TC |
2960 | my $color = _color($opts{'color'}); |
2961 | unless ($color) { | |
2962 | $self->{ERRSTR} = $Imager::ERRSTR; | |
2963 | return; | |
2964 | } | |
2965 | $opts{antialias} = $opts{aa} if defined $opts{aa}; | |
02d1d628 AMH |
2966 | if ($opts{antialias}) { |
2967 | for $pt(@points) { | |
3a9a4241 | 2968 | if (defined($ls)) { |
b437ce0a | 2969 | i_line_aa($self->{IMG},$ls->[0],$ls->[1],$pt->[0],$pt->[1],$color, 1); |
3a9a4241 | 2970 | } |
02d1d628 AMH |
2971 | $ls=$pt; |
2972 | } | |
2973 | } else { | |
2974 | for $pt(@points) { | |
3a9a4241 | 2975 | if (defined($ls)) { |
aa833c97 | 2976 | i_line($self->{IMG},$ls->[0],$ls->[1],$pt->[0],$pt->[1],$color,1); |
3a9a4241 | 2977 | } |
02d1d628 AMH |
2978 | $ls=$pt; |
2979 | } | |
2980 | } | |
2981 | return $self; | |
2982 | } | |
2983 | ||
d0e7bfee AMH |
2984 | sub polygon { |
2985 | my $self = shift; | |
2986 | my ($pt,$ls,@points); | |
2987 | my $dflcl = i_color_new(0,0,0,0); | |
2988 | my %opts = (color=>$dflcl, @_); | |
2989 | ||
2990 | unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } | |
2991 | ||
2992 | if (exists($opts{points})) { | |
2993 | $opts{'x'} = [ map { $_->[0] } @{$opts{points}} ]; | |
2994 | $opts{'y'} = [ map { $_->[1] } @{$opts{points}} ]; | |
2995 | } | |
2996 | ||
2997 | if (!exists $opts{'x'} or !exists $opts{'y'}) { | |
2998 | $self->{ERRSTR} = 'no points array, or x and y arrays.'; return undef; | |
2999 | } | |
3000 | ||
43c5dacb TC |
3001 | if ($opts{'fill'}) { |
3002 | unless (UNIVERSAL::isa($opts{'fill'}, 'Imager::Fill')) { | |
3003 | # assume it's a hash ref | |
3004 | require 'Imager/Fill.pm'; | |
3005 | unless ($opts{'fill'} = Imager::Fill->new(%{$opts{'fill'}})) { | |
3006 | $self->{ERRSTR} = $Imager::ERRSTR; | |
3007 | return undef; | |
3008 | } | |
3009 | } | |
3010 | i_poly_aa_cfill($self->{IMG}, $opts{'x'}, $opts{'y'}, | |
3011 | $opts{'fill'}{'fill'}); | |
3012 | } | |
3013 | else { | |
3a9a4241 TC |
3014 | my $color = _color($opts{'color'}); |
3015 | unless ($color) { | |
3016 | $self->{ERRSTR} = $Imager::ERRSTR; | |
3017 | return; | |
3018 | } | |
3019 | i_poly_aa($self->{IMG}, $opts{'x'}, $opts{'y'}, $color); | |
43c5dacb TC |
3020 | } |
3021 | ||
d0e7bfee AMH |
3022 | return $self; |
3023 | } | |
3024 | ||
3025 | ||
3026 | # this the multipoint bezier curve | |
02d1d628 AMH |
3027 | # this is here more for testing that actual usage since |
3028 | # this is not a good algorithm. Usually the curve would be | |
3029 | # broken into smaller segments and each done individually. | |
3030 | ||
3031 | sub polybezier { | |
3032 | my $self=shift; | |
3033 | my ($pt,$ls,@points); | |
3034 | my $dflcl=i_color_new(0,0,0,0); | |
3035 | my %opts=(color=>$dflcl,@_); | |
3036 | ||
3037 | unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } | |
3038 | ||
3039 | if (exists $opts{points}) { | |
3040 | $opts{'x'}=map { $_->[0]; } @{$opts{'points'}}; | |
3041 | $opts{'y'}=map { $_->[1]; } @{$opts{'points'}}; | |
3042 | } | |
3043 | ||
3044 | unless ( @{$opts{'x'}} and @{$opts{'x'}} == @{$opts{'y'}} ) { | |
3045 | $self->{ERRSTR}='Missing or invalid points.'; | |
3046 | return; | |
3047 | } | |
3048 | ||
3a9a4241 TC |
3049 | my $color = _color($opts{'color'}); |
3050 | unless ($color) { | |
3051 | $self->{ERRSTR} = $Imager::ERRSTR; | |
3052 | return; | |
3053 | } | |
3054 | i_bezier_multi($self->{IMG},$opts{'x'},$opts{'y'},$color); | |
02d1d628 AMH |
3055 | return $self; |
3056 | } | |
3057 | ||
cc6483e0 TC |
3058 | sub flood_fill { |
3059 | my $self = shift; | |
3060 | my %opts = ( color=>Imager::Color->new(255, 255, 255), @_ ); | |
aa833c97 AMH |
3061 | my $rc; |
3062 | ||
9d540150 | 3063 | unless (exists $opts{'x'} && exists $opts{'y'}) { |
cc6483e0 TC |
3064 | $self->{ERRSTR} = "missing seed x and y parameters"; |
3065 | return undef; | |
3066 | } | |
07d70837 | 3067 | |
3efb0915 TC |
3068 | if ($opts{border}) { |
3069 | my $border = _color($opts{border}); | |
3070 | unless ($border) { | |
3071 | $self->_set_error($Imager::ERRSTR); | |
3072 | return; | |
3073 | } | |
3074 | if ($opts{fill}) { | |
3075 | unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) { | |
3076 | # assume it's a hash ref | |
3077 | require Imager::Fill; | |
3078 | unless ($opts{fill} = Imager::Fill->new(%{$opts{fill}})) { | |
3079 | $self->{ERRSTR} = $Imager::ERRSTR; | |
3080 | return; | |
3081 | } | |
569795e8 | 3082 | } |
3efb0915 TC |
3083 | $rc = i_flood_cfill_border($self->{IMG}, $opts{'x'}, $opts{'y'}, |
3084 | $opts{fill}{fill}, $border); | |
3085 | } | |
3086 | else { | |
3087 | my $color = _color($opts{'color'}); | |
3088 | unless ($color) { | |
3089 | $self->{ERRSTR} = $Imager::ERRSTR; | |
3090 | return; | |
3091 | } | |
3092 | $rc = i_flood_fill_border($self->{IMG}, $opts{'x'}, $opts{'y'}, | |
3093 | $color, $border); | |
3094 | } | |
3095 | if ($rc) { | |
3096 | return $self; | |
3097 | } | |
3098 | else { | |
3099 | $self->{ERRSTR} = $self->_error_as_msg(); | |
3100 | return; | |
cc6483e0 | 3101 | } |
cc6483e0 TC |
3102 | } |
3103 | else { | |
3efb0915 TC |
3104 | if ($opts{fill}) { |
3105 | unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) { | |
3106 | # assume it's a hash ref | |
3107 | require 'Imager/Fill.pm'; | |
3108 | unless ($opts{fill} = Imager::Fill->new(%{$opts{fill}})) { | |
3109 | $self->{ERRSTR} = $Imager::ERRSTR; | |
3110 | return; | |
3111 | } | |
3112 | } | |
3113 | $rc = i_flood_cfill($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{fill}{fill}); | |
3114 | } | |
3115 | else { | |
3116 | my $color = _color($opts{'color'}); | |
3117 | unless ($color) { | |
3118 | $self->{ERRSTR} = $Imager::ERRSTR; | |
3119 | return; | |
3120 | } | |
3121 | $rc = i_flood_fill($self->{IMG}, $opts{'x'}, $opts{'y'}, $color); | |
3122 | } | |
3123 | if ($rc) { | |
3124 | return $self; | |
3125 | } | |
3126 | else { | |
3127 | $self->{ERRSTR} = $self->_error_as_msg(); | |
aa833c97 | 3128 | return; |
3a9a4241 | 3129 | } |
3efb0915 | 3130 | } |
cc6483e0 TC |
3131 | } |
3132 | ||
591b5954 | 3133 | sub setpixel { |
b3cdc973 | 3134 | my ($self, %opts) = @_; |
591b5954 | 3135 | |
b3cdc973 TC |
3136 | my $color = $opts{color}; |
3137 | unless (defined $color) { | |
3138 | $color = $self->{fg}; | |
3139 | defined $color or $color = NC(255, 255, 255); | |
3140 | } | |
3141 | ||
3142 | unless (ref $color && UNIVERSAL::isa($color, "Imager::Color")) { | |
3143 | $color = _color($color) | |
3144 | or return undef; | |
3145 | } | |
591b5954 TC |
3146 | |
3147 | unless (exists $opts{'x'} && exists $opts{'y'}) { | |
3148 | $self->{ERRSTR} = 'missing x and y parameters'; | |
3149 | return undef; | |
3150 | } | |
3151 | ||
3152 | my $x = $opts{'x'}; | |
3153 | my $y = $opts{'y'}; | |
591b5954 TC |
3154 | if (ref $x && ref $y) { |
3155 | unless (@$x == @$y) { | |
9650c424 | 3156 | $self->{ERRSTR} = 'length of x and y mismatch'; |
837a4b43 | 3157 | return; |
591b5954 | 3158 | } |
837a4b43 | 3159 | my $set = 0; |
591b5954 TC |
3160 | if ($color->isa('Imager::Color')) { |
3161 | for my $i (0..$#{$opts{'x'}}) { | |
837a4b43 TC |
3162 | i_ppix($self->{IMG}, $x->[$i], $y->[$i], $color) |
3163 | or ++$set; | |
591b5954 TC |
3164 | } |
3165 | } | |
3166 | else { | |
3167 | for my $i (0..$#{$opts{'x'}}) { | |
837a4b43 TC |
3168 | i_ppixf($self->{IMG}, $x->[$i], $y->[$i], $color) |
3169 | or ++$set; | |
591b5954 TC |
3170 | } |
3171 | } | |
837a4b43 TC |
3172 | $set or return; |
3173 | return $set; | |
591b5954 TC |
3174 | } |
3175 | else { | |
3176 | if ($color->isa('Imager::Color')) { | |
837a4b43 TC |
3177 | i_ppix($self->{IMG}, $x, $y, $color) |
3178 | and return; | |
591b5954 TC |
3179 | } |
3180 | else { | |
837a4b43 TC |
3181 | i_ppixf($self->{IMG}, $x, $y, $color) |
3182 | and return; | |
591b5954 TC |
3183 | } |
3184 | } | |
3185 | ||
3186 | $self; | |
3187 | } | |
3188 | ||
3189 | sub getpixel { | |
3190 | my $self = shift; | |
3191 | ||
a9fa203f | 3192 | my %opts = ( "type"=>'8bit', @_); |
591b5954 TC |
3193 | |
3194 | unless (exists $opts{'x'} && exists $opts{'y'}) { | |
3195 | $self->{ERRSTR} = 'missing x and y parameters'; | |
3196 | return undef; | |
3197 | } | |
3198 | ||
3199 | my $x = $opts{'x'}; | |
3200 | my $y = $opts{'y'}; | |
3201 | if (ref $x && ref $y) { | |
3202 | unless (@$x == @$y) { | |
3203 | $self->{ERRSTR} = 'length of x and y mismatch'; | |
3204 | return undef; | |
3205 | } | |
3206 | my @result; | |
a9fa203f | 3207 | if ($opts{"type"} eq '8bit') { |
591b5954 TC |
3208 | for my $i (0..$#{$opts{'x'}}) { |
3209 | push(@result, i_get_pixel($self->{IMG}, $x->[$i], $y->[$i])); | |
3210 | } | |
3211 | } | |
3212 | else { | |
3213 | for my $i (0..$#{$opts{'x'}}) { | |
3214 | push(@result, i_gpixf($self->{IMG}, $x->[$i], $y->[$i])); | |
3215 | } | |
3216 | } | |
3217 | return wantarray ? @result : \@result; | |
3218 | } | |
3219 | else { | |
a9fa203f | 3220 | if ($opts{"type"} eq '8bit') { |
591b5954 TC |
3221 | return i_get_pixel($self->{IMG}, $x, $y); |
3222 | } | |
3223 | else { | |
3224 | return i_gpixf($self->{IMG}, $x, $y); | |
3225 | } | |
3226 | } | |
3227 | ||
3228 | $self; | |
3229 | } | |
3230 | ||
ca4d914e TC |
3231 | sub getscanline { |
3232 | my $self = shift; | |
3233 | my %opts = ( type => '8bit', x=>0, @_); | |
3234 | ||
4cda4e76 TC |
3235 | $self->_valid_image or return; |
3236 | ||
ca4d914e TC |
3237 | defined $opts{width} or $opts{width} = $self->getwidth - $opts{x}; |
3238 | ||
3239 | unless (defined $opts{'y'}) { | |
3240 | $self->_set_error("missing y parameter"); | |
3241 | return; | |
3242 | } | |
3243 | ||
3244 | if ($opts{type} eq '8bit') { | |
3245 | return i_glin($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, | |
4cda4e76 | 3246 | $opts{'y'}); |
ca4d914e TC |
3247 | } |
3248 | elsif ($opts{type} eq 'float') { | |
3249 | return i_glinf($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, | |
4cda4e76 TC |
3250 | $opts{'y'}); |
3251 | } | |
3252 | elsif ($opts{type} eq 'index') { | |
3253 | unless (i_img_type($self->{IMG})) { | |
3254 | $self->_set_error("type => index only valid on paletted images"); | |
3255 | return; | |
3256 | } | |
3257 | return i_gpal($self->{IMG}, $opts{x}, $opts{x} + $opts{width}, | |
3258 | $opts{'y'}); | |
ca4d914e TC |
3259 | } |
3260 | else { | |
3261 | $self->_set_error("invalid type parameter - must be '8bit' or 'float'"); | |
3262 | return; | |
3263 | } | |
3264 | } | |
3265 | ||
3266 | sub setscanline { | |
3267 | my $self = shift; | |
3268 | my %opts = ( x=>0, @_); | |
3269 | ||
4cda4e76 TC |
3270 | $self->_valid_image or return; |
3271 | ||
ca4d914e TC |
3272 | unless (defined $opts{'y'}) { |
3273 | $self->_set_error("missing y parameter"); | |
3274 | return; | |
3275 | } | |
3276 | ||
3277 | if (!$opts{type}) { | |
3278 | if (ref $opts{pixels} && @{$opts{pixels}}) { | |
3279 | # try to guess the type | |
3280 | if ($opts{pixels}[0]->isa('Imager::Color')) { | |
3281 | $opts{type} = '8bit'; | |
3282 | } | |
3283 | elsif ($opts{pixels}[0]->isa('Imager::Color::Float')) { | |
3284 | $opts{type} = 'float'; | |
3285 | } | |
3286 | else { | |
3287 | $self->_set_error("missing type parameter and could not guess from pixels"); | |
3288 | return; | |
3289 | } | |
3290 | } | |
3291 | else { | |
3292 | # default | |
3293 | $opts{type} = '8bit'; | |
3294 | } | |
3295 | } | |
3296 | ||
3297 | if ($opts{type} eq '8bit') { | |
3298 | if (ref $opts{pixels}) { | |
3299 | return i_plin($self->{IMG}, $opts{x}, $opts{'y'}, @{$opts{pixels}}); | |
3300 | } | |
3301 | else { | |
3302 | return i_plin($self->{IMG}, $opts{x}, $opts{'y'}, $opts{pixels}); | |
3303 | } | |
3304 | } | |
3305 | elsif ($opts{type} eq 'float') { | |
3306 | if (ref $opts{pixels}) { | |
3307 | return i_plinf($self->{IMG}, $opts{x}, $opts{'y'}, @{$opts{pixels}}); | |
3308 | } | |
3309 | else { | |
3310 | return i_plinf($self->{IMG}, $opts{x}, $opts{'y'}, $opts{pixels}); | |
3311 | } | |
3312 | } | |
4cda4e76 TC |
3313 | elsif ($opts{type} eq 'index') { |
3314 | if (ref $opts{pixels}) { | |
3315 | return i_ppal($self->{IMG}, $opts{x}, $opts{'y'}, @{$opts{pixels}}); | |
3316 | } | |
3317 | else { | |
3318 | return i_ppal_p($self->{IMG}, $opts{x}, $opts{'y'}, $opts{pixels}); | |
3319 | } | |
3320 | } | |
ca4d914e TC |
3321 | else { |
3322 | $self->_set_error("invalid type parameter - must be '8bit' or 'float'"); | |
3323 | return; | |
3324 | } | |
3325 | } | |
3326 | ||
3327 | sub getsamples { | |
3328 | my $self = shift; | |
bd8052a6 | 3329 | my %opts = ( type => '8bit', x=>0, offset => 0, @_); |
ca4d914e TC |
3330 | |
3331 | defined $opts{width} or $opts{width} = $self->getwidth - $opts{x}; | |
3332 | ||
3333 | unless (defined $opts{'y'}) { | |
3334 | $self->_set_error("missing y parameter"); | |
3335 | return; | |
3336 | } | |
3337 | ||
3338 | unless ($opts{channels}) { | |
3339 | $opts{channels} = [ 0 .. $self->getchannels()-1 ]; | |
3340 | } | |
3341 | ||
bd8052a6 TC |
3342 | if ($opts{target}) { |
3343 | my $target = $opts{target}; | |
3344 | my $offset = $opts{offset}; | |
3345 | if ($opts{type} eq '8bit') { | |
3346 | my @samples = i_gsamp($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, | |
3347 | $opts{y}, @{$opts{channels}}) | |
3348 | or return; | |
3349 | @{$target}{$offset .. $offset + @samples - 1} = @samples; | |
3350 | return scalar(@samples); | |
3351 | } | |
3352 | elsif ($opts{type} eq 'float') { | |
3353 | my @samples = i_gsampf($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, | |
3354 | $opts{y}, @{$opts{channels}}); | |
3355 | @{$target}{$offset .. $offset + @samples - 1} = @samples; | |
3356 | return scalar(@samples); | |
3357 | } | |
3358 | elsif ($opts{type} =~ /^(\d+)bit$/) { | |
3359 | my $bits = $1; | |
3360 | ||
3361 | my @data; | |
3362 | my $count = i_gsamp_bits($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, | |
3363 | $opts{y}, $bits, $target, | |
3364 | $offset, @{$opts{channels}}); | |
3365 | unless (defined $count) { | |
3366 | $self->_set_error(Imager->_error_as_msg); | |
3367 | return; | |
3368 | } | |
3369 | ||
3370 | return $count; | |
3371 | } | |
3372 | else { | |
3373 | $self->_set_error("invalid type parameter - must be '8bit' or 'float'"); | |
3374 | return; | |
3375 | } | |
ca4d914e TC |
3376 | } |
3377 | else { | |
bd8052a6 TC |
3378 | if ($opts{type} eq '8bit') { |
3379 | return i_gsamp($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, | |
3380 | $opts{y}, @{$opts{channels}}); | |
3381 | } | |
3382 | elsif ($opts{type} eq 'float') { | |
3383 | return i_gsampf($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, | |
3384 | $opts{y}, @{$opts{channels}}); | |
3385 | } | |
3386 | elsif ($opts{type} =~ /^(\d+)bit$/) { | |
3387 | my $bits = $1; | |
3388 | ||
3389 | my @data; | |
3390 | i_gsamp_bits($self->{IMG}, $opts{x}, $opts{x}+$opts{width}, | |
3391 | $opts{y}, $bits, \@data, 0, @{$opts{channels}}) | |
3392 | or return; | |
3393 | return @data; | |
3394 | } | |
3395 | else { | |
3396 | $self->_set_error("invalid type parameter - must be '8bit' or 'float'"); | |
3397 | return; | |
3398 | } | |
3399 | } | |
3400 | } | |
3401 | ||
3402 | sub setsamples { | |
3403 | my $self = shift; | |
3404 | my %opts = ( x => 0, offset => 0, @_ ); | |
3405 | ||
3406 | unless ($self->{IMG}) { | |
3407 | $self->_set_error('setsamples: empty input image'); | |
3408 | return; | |
3409 | } | |
3410 | ||
3411 | unless(defined $opts{data} && ref $opts{data}) { | |
3412 | $self->_set_error('setsamples: data parameter missing or invalid'); | |
ca4d914e TC |
3413 | return; |
3414 | } | |
bd8052a6 TC |
3415 | |
3416 | unless ($opts{channels}) { | |
3417 | $opts{channels} = [ 0 .. $self->getchannels()-1 ]; | |
3418 | } | |
3419 | ||
3420 | unless ($opts{type} && $opts{type} =~ /^(\d+)bit$/) { | |
3421 | $self->_set_error('setsamples: type parameter missing or invalid'); | |
3422 | return; | |
3423 | } | |
3424 | my $bits = $1; | |
3425 | ||
3426 | unless (defined $opts{width}) { | |
3427 | $opts{width} = $self->getwidth() - $opts{x}; | |
3428 | } | |
3429 | ||
3430 | my $count = i_psamp_bits($self->{IMG}, $opts{x}, $opts{y}, $bits, | |
3431 | $opts{channels}, $opts{data}, $opts{offset}, | |
3432 | $opts{width}); | |
3433 | unless (defined $count) { | |
3434 | $self->_set_error(Imager->_error_as_msg); | |
3435 | return; | |
3436 | } | |
3437 | ||
3438 | return $count; | |
ca4d914e TC |
3439 | } |
3440 | ||
f5991c03 TC |
3441 | # make an identity matrix of the given size |
3442 | sub _identity { | |
3443 | my ($size) = @_; | |
3444 | ||
3445 | my $matrix = [ map { [ (0) x $size ] } 1..$size ]; | |
3446 | for my $c (0 .. ($size-1)) { | |
3447 | $matrix->[$c][$c] = 1; | |
3448 | } | |
3449 | return $matrix; | |
3450 | } | |
3451 | ||
3452 | # general function to convert an image | |
3453 | sub convert { | |
3454 | my ($self, %opts) = @_; | |
3455 | my $matrix; | |
3456 | ||
34b3f7e6 TC |
3457 | unless (defined wantarray) { |
3458 | my @caller = caller; | |
3459 | warn "convert() called in void context - convert() returns the converted image at $caller[1] line $caller[2]\n"; | |
3460 | return; | |
3461 | } | |
3462 | ||
f5991c03 TC |
3463 | # the user can either specify a matrix or preset |
3464 | # the matrix overrides the preset | |
3465 | if (!exists($opts{matrix})) { | |
3466 | unless (exists($opts{preset})) { | |
3467 | $self->{ERRSTR} = "convert() needs a matrix or preset"; | |
3468 | return; | |
3469 | } | |
3470 | else { | |
3471 | if ($opts{preset} eq 'gray' || $opts{preset} eq 'grey') { | |
3472 | # convert to greyscale, keeping the alpha channel if any | |
3473 | if ($self->getchannels == 3) { | |
3474 | $matrix = [ [ 0.222, 0.707, 0.071 ] ]; | |
3475 | } | |
3476 | elsif ($self->getchannels == 4) { | |
3477 | # preserve the alpha channel | |
3478 | $matrix = [ [ 0.222, 0.707, 0.071, 0 ], | |
3479 | [ 0, 0, 0, 1 ] ]; | |
3480 | } | |
3481 | else { | |
3482 | # an identity | |
3483 | $matrix = _identity($self->getchannels); | |
3484 | } | |
3485 | } | |
3486 | elsif ($opts{preset} eq 'noalpha') { | |
3487 | # strip the alpha channel | |
3488 | if ($self->getchannels == 2 or $self->getchannels == 4) { | |
3489 | $matrix = _identity($self->getchannels); | |
3490 | pop(@$matrix); # lose the alpha entry | |
3491 | } | |
3492 | else { | |
3493 | $matrix = _identity($self->getchannels); | |
3494 | } | |
3495 | } | |
3496 | elsif ($opts{preset} eq 'red' || $opts{preset} eq 'channel0') { | |
3497 | # extract channel 0 | |
3498 | $matrix = [ [ 1 ] ]; | |
3499 | } | |
3500 | elsif ($opts{preset} eq 'green' || $opts{preset} eq 'channel1') { | |
3501 | $matrix = [ [ 0, 1 ] ]; | |
3502 | } | |
3503 | elsif ($opts{preset} eq 'blue' || $opts{preset} eq 'channel2') { | |
3504 | $matrix = [ [ 0, 0, 1 ] ]; | |
3505 | } | |
3506 | elsif ($opts{preset} eq 'alpha') { | |
3507 | if ($self->getchannels == 2 or $self->getchannels == 4) { | |
3508 | $matrix = [ [ (0) x ($self->getchannels-1), 1 ] ]; | |
3509 | } | |
3510 | else { | |
3511 | # the alpha is just 1 <shrug> | |
3512 | $matrix = [ [ (0) x $self->getchannels, 1 ] ]; | |
3513 | } | |
3514 | } | |
3515 | elsif ($opts{preset} eq 'rgb') { | |
3516 | if ($self->getchannels == 1) { | |
3517 | $matrix = [ [ 1 ], [ 1 ], [ 1 ] ]; | |
3518 | } | |
3519 | elsif ($self->getchannels == 2) { | |
3520 | # preserve the alpha channel | |
3521 | $matrix = [ [ 1, 0 ], [ 1, 0 ], [ 1, 0 ], [ 0, 1 ] ]; | |
3522 | } | |
3523 | else { | |
3524 | $matrix = _identity($self->getchannels); | |
3525 | } | |
3526 | } | |
3527 | elsif ($opts{preset} eq 'addalpha') { | |
3528 | if ($self->getchannels == 1) { | |
3529 | $matrix = _identity(2); | |
3530 | } | |
3531 | elsif ($self->getchannels == 3) { | |
3532 | $matrix = _identity(4); | |
3533 | } | |
3534 | else { | |
3535 | $matrix = _identity($self->getchannels); | |
3536 | } | |
3537 | } | |
3538 | else { | |
3539 | $self->{ERRSTR} = "Unknown convert preset $opts{preset}"; | |
3540 | return undef; | |
3541 | } | |
3542 | } | |
3543 | } | |
3544 | else { | |
3545 | $matrix = $opts{matrix}; | |
3546 | } | |
3547 | ||
d5477d3d TC |
3548 | my $new = Imager->new; |
3549 | $new->{IMG} = i_convert($self->{IMG}, $matrix); | |
3550 | unless ($new->{IMG}) { | |
f5991c03 TC |
3551 | # most likely a bad matrix |
3552 | $self->{ERRSTR} = _error_as_msg(); | |
3553 | return undef; | |
3554 | } | |
3555 | return $new; | |
3556 | } | |
40eba1ea | 3557 | |
b47464c1 TC |
3558 | # combine channels from multiple input images, a class method |
3559 | sub combine { | |
3560 | my ($class, %opts) = @_; | |
3561 | ||
3562 | my $src = delete $opts{src}; | |
3563 | unless ($src) { | |
3564 | $class->_set_error("src parameter missing"); | |
3565 | return; | |
3566 | } | |
3567 | my @imgs; | |
3568 | my $index = 0; | |
3569 | for my $img (@$src) { | |
3570 | unless (eval { $img->isa("Imager") }) { | |
3571 | $class->_set_error("src must contain image objects"); | |
3572 | return; | |
3573 | } | |
3574 | unless ($img->{IMG}) { | |
3575 | $class->_set_error("empty input image"); | |
3576 | return; | |
3577 | } | |
3578 | push @imgs, $img->{IMG}; | |
3579 | } | |
3580 | my $result; | |
3581 | if (my $channels = delete $opts{channels}) { | |
3582 | $result = i_combine(\@imgs, $channels); | |
3583 | } | |
3584 | else { | |
3585 | $result = i_combine(\@imgs); | |
3586 | } | |
3587 | unless ($result) { | |
3588 | $class->_set_error($class->_error_as_msg); | |
3589 | return; | |
3590 | } | |
3591 | ||
3592 | my $img = $class->new; | |
3593 | $img->{IMG} = $result; | |
3594 | ||
3595 | return $img; | |
3596 | } | |
3597 | ||
40eba1ea | 3598 | |
40eba1ea | 3599 | # general function to map an image through lookup tables |
9495ee93 | 3600 | |
40eba1ea AMH |
3601 | sub map { |
3602 | my ($self, %opts) = @_; | |
9495ee93 | 3603 | my @chlist = qw( red green blue alpha ); |
40eba1ea AMH |
3604 | |
3605 | if (!exists($opts{'maps'})) { | |
3606 | # make maps from channel maps | |
3607 | my $chnum; | |
3608 | for $chnum (0..$#chlist) { | |
9495ee93 AMH |
3609 | if (exists $opts{$chlist[$chnum]}) { |
3610 | $opts{'maps'}[$chnum] = $opts{$chlist[$chnum]}; | |
3611 | } elsif (exists $opts{'all'}) { | |
3612 | $opts{'maps'}[$chnum] = $opts{'all'}; | |
3613 | } | |
40eba1ea AMH |
3614 | } |
3615 | } | |
3616 | if ($opts{'maps'} and $self->{IMG}) { | |
3617 | i_map($self->{IMG}, $opts{'maps'} ); | |
3618 | } | |
3619 | return $self; | |
3620 | } | |
3621 | ||
dff75dee TC |
3622 | sub difference { |
3623 | my ($self, %opts) = @_; | |
3624 | ||
3625 | defined $opts{mindist} or $opts{mindist} = 0; | |
3626 | ||
3627 | defined $opts{other} | |
3628 | or return $self->_set_error("No 'other' parameter supplied"); | |
3629 | defined $opts{other}{IMG} | |
3630 | or return $self->_set_error("No image data in 'other' image"); | |
3631 | ||
3632 | $self->{IMG} | |
3633 | or return $self->_set_error("No image data"); | |
3634 | ||
3635 | my $result = Imager->new; | |
3636 | $result->{IMG} = i_diff_image($self->{IMG}, $opts{other}{IMG}, | |
3637 | $opts{mindist}) | |
3638 | or return $self->_set_error($self->_error_as_msg()); | |
3639 | ||
3640 | return $result; | |
3641 | } | |
3642 | ||
02d1d628 AMH |
3643 | # destructive border - image is shrunk by one pixel all around |
3644 | ||
3645 | sub border { | |
3646 | my ($self,%opts)=@_; | |
3647 | my($tx,$ty)=($self->getwidth()-1,$self->getheight()-1); | |
3648 | $self->polyline('x'=>[0,$tx,$tx,0,0],'y'=>[0,0,$ty,$ty,0],%opts); | |
3649 | } | |
3650 | ||
3651 | ||
3652 | # Get the width of an image | |
3653 | ||
3654 | sub getwidth { | |
3655 | my $self = shift; | |
3b000586 TC |
3656 | |
3657 | if (my $raw = $self->{IMG}) { | |
3658 | return i_img_get_width($raw); | |
3659 | } | |
3660 | else { | |
3661 | $self->{ERRSTR} = 'image is empty'; return undef; | |
3662 | } | |
02d1d628 AMH |
3663 | } |
3664 | ||
3665 | # Get the height of an image | |
3666 | ||
3667 | sub getheight { | |
3668 | my $self = shift; | |
3b000586 TC |
3669 | |
3670 | if (my $raw = $self->{IMG}) { | |
3671 | return i_img_get_height($raw); | |
3672 | } | |
3673 | else { | |
3674 | $self->{ERRSTR} = 'image is empty'; return undef; | |
3675 | } | |
02d1d628 AMH |
3676 | } |
3677 | ||
3678 | # Get number of channels in an image | |
3679 | ||
3680 | sub getchannels { | |
3681 | my $self = shift; | |
3682 | if (!defined($self->{IMG})) { $self->{ERRSTR} = 'image is empty'; return undef; } | |
3683 | return i_img_getchannels($self->{IMG}); | |
3684 | } | |
3685 | ||
3686 | # Get channel mask | |
3687 | ||
3688 | sub getmask { | |
3689 | my $self = shift; | |
3690 | if (!defined($self->{IMG})) { $self->{ERRSTR} = 'image is empty'; return undef; } | |
3691 | return i_img_getmask($self->{IMG}); | |
3692 | } | |
3693 | ||
3694 | # Set channel mask | |
3695 | ||
3696 | sub setmask { | |
3697 | my $self = shift; | |
3698 | my %opts = @_; | |
35f40526 TC |
3699 | if (!defined($self->{IMG})) { |
3700 | $self->{ERRSTR} = 'image is empty'; | |
3701 | return undef; | |
3702 | } | |
3703 | unless (defined $opts{mask}) { | |
3704 | $self->_set_error("mask parameter required"); | |
3705 | return; | |
3706 | } | |
02d1d628 | 3707 | i_img_setmask( $self->{IMG} , $opts{mask} ); |
35f40526 TC |
3708 | |
3709 | 1; | |
02d1d628 AMH |
3710 | } |
3711 | ||
3712 | # Get number of colors in an image | |
3713 | ||
3714 | sub getcolorcount { | |
3715 | my $self=shift; | |
9d540150 | 3716 | my %opts=('maxcolors'=>2**30,@_); |
02d1d628 AMH |
3717 | if (!defined($self->{IMG})) { $self->{ERRSTR}='image is empty'; return undef; } |
3718 | my $rc=i_count_colors($self->{IMG},$opts{'maxcolors'}); | |
3719 | return ($rc==-1? undef : $rc); | |
3720 | } | |
3721 | ||
fe622da1 TC |
3722 | # Returns a reference to a hash. The keys are colour named (packed) and the |
3723 | # values are the number of pixels in this colour. | |
3724 | sub getcolorusagehash { | |
a60905e4 TC |
3725 | my $self = shift; |
3726 | ||
3727 | my %opts = ( maxcolors => 2**30, @_ ); | |
3728 | my $max_colors = $opts{maxcolors}; | |
3729 | unless (defined $max_colors && $max_colors > 0) { | |
3730 | $self->_set_error('maxcolors must be a positive integer'); | |
3731 | return; | |
3732 | } | |
3733 | ||
3734 | unless (defined $self->{IMG}) { | |
3735 | $self->_set_error('empty input image'); | |
3736 | return; | |
3737 | } | |
3738 | ||
3739 | my $channels= $self->getchannels; | |
3740 | # We don't want to look at the alpha channel, because some gifs using it | |
3741 | # doesn't define it for every colour (but only for some) | |
3742 | $channels -= 1 if $channels == 2 or $channels == 4; | |
3743 | my %color_use; | |
3744 | my $height = $self->getheight; | |
3745 | for my $y (0 .. $height - 1) { | |
3746 | my $colors = $self->getsamples('y' => $y, channels => [ 0 .. $channels - 1 ]); | |
3747 | while (length $colors) { | |
3748 | $color_use{ substr($colors, 0, $channels, '') }++; | |
fe622da1 | 3749 | } |
a60905e4 TC |
3750 | keys %color_use > $max_colors |
3751 | and return; | |
3752 | } | |
3753 | return \%color_use; | |
fe622da1 TC |
3754 | } |
3755 | ||
3756 | # This will return a ordered array of the colour usage. Kind of the sorted | |
3757 | # version of the values of the hash returned by getcolorusagehash. | |
3758 | # You might want to add safety checks and change the names, etc... | |
3759 | sub getcolorusage { | |
a60905e4 TC |
3760 | my $self = shift; |
3761 | ||
3762 | my %opts = ( maxcolors => 2**30, @_ ); | |
3763 | my $max_colors = $opts{maxcolors}; | |
3764 | unless (defined $max_colors && $max_colors > 0) { | |
3765 | $self->_set_error('maxcolors must be a positive integer'); | |
3766 | return; | |
3767 | } | |
3768 | ||
3769 | unless (defined $self->{IMG}) { | |
3770 | $self->_set_error('empty input image'); | |
3771 | return undef; | |
3772 | } | |
3773 | ||
3774 | return i_get_anonymous_color_histo($self->{IMG}, $max_colors); | |
fe622da1 TC |
3775 | } |
3776 | ||
02d1d628 AMH |
3777 | # draw string to an image |
3778 | ||
3779 | sub string { | |
3780 | my $self = shift; | |
3781 | unless ($self->{IMG}) { $self->{ERRSTR}='empty input image'; return undef; } | |
3782 | ||
3783 | my %input=('x'=>0, 'y'=>0, @_); | |
4314a320 | 3784 | defined($input{string}) or $input{string} = $input{text}; |
02d1d628 | 3785 | |
e922ae66 | 3786 | unless(defined $input{string}) { |
02d1d628 AMH |
3787 | $self->{ERRSTR}="missing required parameter 'string'"; |
3788 | return; | |
3789 | } | |
3790 | ||
3791 | unless($input{font}) { | |
3792 | $self->{ERRSTR}="missing required parameter 'font'"; | |
3793 | return; | |
3794 | } | |
3795 | ||
faa9b3e7 | 3796 | unless ($input{font}->draw(image=>$self, %input)) { |
faa9b3e7 TC |
3797 | return; |
3798 | } | |
02d1d628 AMH |
3799 | |
3800 | return $self; | |
3801 | } | |
3802 | ||
a7ccc5e2 TC |
3803 | sub align_string { |
3804 | my $self = shift; | |
e922ae66 TC |
3805 | |
3806 | my $img; | |
3807 | if (ref $self) { | |
3808 | unless ($self->{IMG}) { | |
3809 | $self->{ERRSTR}='empty input image'; | |
3810 | return; | |
3811 | } | |
c9cb3397 | 3812 | $img = $self; |
e922ae66 TC |
3813 | } |
3814 | else { | |
3815 | $img = undef; | |
3816 | } | |
a7ccc5e2 TC |
3817 | |
3818 | my %input=('x'=>0, 'y'=>0, @_); | |
120f4287 TC |
3819 | defined $input{string} |
3820 | or $input{string} = $input{text}; | |
a7ccc5e2 TC |
3821 | |
3822 | unless(exists $input{string}) { | |
e922ae66 | 3823 | $self->_set_error("missing required parameter 'string'"); |
a7ccc5e2 TC |
3824 | return; |
3825 | } | |
3826 | ||
3827 | unless($input{font}) { | |
e922ae66 | 3828 | $self->_set_error("missing required parameter 'font'"); |
a7ccc5e2 TC |
3829 | return; |
3830 | } | |
3831 | ||
3832 | my @result; | |
e922ae66 | 3833 | unless (@result = $input{font}->align(image=>$img, %input)) { |
a7ccc5e2 TC |
3834 | return; |
3835 | } | |
3836 | ||
3837 | return wantarray ? @result : $result[0]; | |
3838 | } | |
3839 | ||
77157728 TC |
3840 | my @file_limit_names = qw/width height bytes/; |
3841 | ||
3842 | sub set_file_limits { | |
3843 | shift; | |
3844 | ||
3845 | my %opts = @_; | |
3846 | my %values; | |
3847 | ||
3848 | if ($opts{reset}) { | |
3849 | @values{@file_limit_names} = (0) x @file_limit_names; | |
3850 | } | |
3851 | else { | |
3852 | @values{@file_limit_names} = i_get_image_file_limits(); | |
3853 | } | |
3854 | ||
3855 | for my $key (keys %values) { | |
3856 | defined $opts{$key} and $values{$key} = $opts{$key}; | |
3857 | } | |
3858 | ||
3859 | i_set_image_file_limits($values{width}, $values{height}, $values{bytes}); | |
3860 | } | |
3861 | ||
3862 | sub get_file_limits { | |
3863 | i_get_image_file_limits(); | |
3864 | } | |
3865 | ||
02d1d628 AMH |
3866 | # Shortcuts that can be exported |
3867 | ||
3868 | sub newcolor { Imager::Color->new(@_); } | |
3869 | sub newfont { Imager::Font->new(@_); } | |
4697b0b9 TC |
3870 | sub NCF { |
3871 | require Imager::Color::Float; | |
3872 | return Imager::Color::Float->new(@_); | |
3873 | } | |
02d1d628 AMH |
3874 | |
3875 | *NC=*newcolour=*newcolor; | |
3876 | *NF=*newfont; | |
3877 | ||
3878 | *open=\&read; | |
3879 | *circle=\&arc; | |
3880 | ||
3881 | ||
3882 | #### Utility routines | |
3883 | ||
faa9b3e7 TC |
3884 | sub errstr { |
3885 | ref $_[0] ? $_[0]->{ERRSTR} : $ERRSTR | |
3886 | } | |
02d1d628 | 3887 | |
10461f9a TC |
3888 | sub _set_error { |
3889 | my ($self, $msg) = @_; | |
3890 | ||
3891 | if (ref $self) { | |
3892 | $self->{ERRSTR} = $msg; | |
3893 | } | |
3894 | else { | |
3895 | $ERRSTR = $msg; | |
3896 | } | |
dff75dee | 3897 | return; |
10461f9a TC |
3898 | } |
3899 | ||
02d1d628 AMH |
3900 | # Default guess for the type of an image from extension |
3901 | ||
4f21e06e TC |
3902 | my @simple_types = qw(png tga gif raw ico cur xpm mng jng ilbm pcx psd eps); |
3903 | ||
3904 | my %ext_types = | |
3905 | ( | |
3906 | ( map { $_ => $_ } @simple_types ), | |
3907 | tiff => "tiff", | |
3908 | tif => "tiff", | |
3909 | pbm => "pnm", | |
3910 | pgm => "pnm", | |
3911 | ppm => "pnm", | |
3912 | pnm => "pnm", # technically wrong, but historically it works in Imager | |
3913 | jpeg => "jpeg", | |
3914 | jpg => "jpeg", | |
3915 | bmp => "bmp", | |
3916 | dib => "bmp", | |
3917 | rgb => "sgi", | |
3918 | bw => "sgi", | |
3919 | sgi => "sgi", | |
3920 | fit => "fits", | |
3921 | fits => "fits", | |
3922 | rle => "utah", | |
3923 | ); | |
3924 | ||
02d1d628 AMH |
3925 | sub def_guess_type { |
3926 | my $name=lc(shift); | |
4f21e06e TC |
3927 | |
3928 | my ($ext) = $name =~ /\.([^.]+)$/ | |
3929 | or return; | |
3930 | ||
3931 | my $type = $ext_types{$ext} | |
3932 | or return; | |
3933 | ||
3934 | return $type; | |
02d1d628 AMH |
3935 | } |
3936 | ||
9b1ec2b8 TC |
3937 | sub combines { |
3938 | return @combine_types; | |
3939 | } | |
3940 | ||
02d1d628 AMH |
3941 | # get the minimum of a list |
3942 | ||
bf1573f9 | 3943 | sub _min { |
02d1d628 AMH |
3944 | my $mx=shift; |
3945 | for(@_) { if ($_<$mx) { $mx=$_; }} | |
3946 | return $mx; | |
3947 | } | |
3948 | ||
3949 | # get the maximum of a list | |
3950 | ||
bf1573f9 | 3951 | sub _max { |
02d1d628 AMH |
3952 | my $mx=shift; |
3953 | for(@_) { if ($_>$mx) { $mx=$_; }} | |
3954 | return $mx; | |
3955 | } | |
3956 | ||
3957 | # string stuff for iptc headers | |
3958 | ||
bf1573f9 | 3959 | sub _clean { |
02d1d628 AMH |
3960 | my($str)=$_[0]; |
3961 | $str = substr($str,3); | |
3962 | $str =~ s/[\n\r]//g; | |
3963 | $str =~ s/\s+/ /g; | |
3964 | $str =~ s/^\s//; | |
3965 | $str =~ s/\s$//; | |
3966 | return $str; | |
3967 | } | |
3968 | ||
3969 | # A little hack to parse iptc headers. | |
3970 | ||
3971 | sub parseiptc { | |
3972 | my $self=shift; | |
3973 | my(@sar,$item,@ar); | |
3974 | my($caption,$photogr,$headln,$credit); | |
3975 | ||
3976 | my $str=$self->{IPTCRAW}; | |
3977 | ||
24ae6325 TC |
3978 | defined $str |
3979 | or return; | |
02d1d628 AMH |
3980 | |
3981 | @ar=split(/8BIM/,$str); | |
3982 | ||
3983 | my $i=0; | |
3984 | foreach (@ar) { | |
3985 | if (/^\004\004/) { | |
3986 | @sar=split(/\034\002/); | |
3987 | foreach $item (@sar) { | |
cdd23610 | 3988 | if ($item =~ m/^x/) { |
bf1573f9 | 3989 | $caption = _clean($item); |
02d1d628 AMH |
3990 | $i++; |
3991 | } | |
cdd23610 | 3992 | if ($item =~ m/^P/) { |
bf1573f9 | 3993 | $photogr = _clean($item); |
02d1d628 AMH |
3994 | $i++; |
3995 | } | |
cdd23610 | 3996 | if ($item =~ m/^i/) { |
bf1573f9 | 3997 | $headln = _clean($item); |
02d1d628 AMH |
3998 | $i++; |
3999 | } | |
cdd23610 | 4000 | if ($item =~ m/^n/) { |
bf1573f9 | 4001 | $credit = _clean($item); |
02d1d628 AMH |
4002 | $i++; |
4003 | } | |
4004 | } | |
4005 | } | |
4006 | } | |
4007 | return (caption=>$caption,photogr=>$photogr,headln=>$headln,credit=>$credit); | |
4008 | } | |
4009 | ||
92bda632 TC |
4010 | sub Inline { |
4011 | my ($lang) = @_; | |
4012 | ||
4013 | $lang eq 'C' | |
4014 | or die "Only C language supported"; | |
4015 | ||
4016 | require Imager::ExtUtils; | |
4017 | return Imager::ExtUtils->inline_config; | |
4018 | } | |
02d1d628 | 4019 | |
ffddd407 TC |
4020 | # threads shouldn't try to close raw Imager objects |
4021 | sub Imager::ImgRaw::CLONE_SKIP { 1 } | |
4022 | ||
2c331f9f TC |
4023 | sub preload { |
4024 | # this serves two purposes: | |
4025 | # - a class method to load the file support modules included with Image | |
4026 | # (or were included, once the library dependent modules are split out) | |
4027 | # - something for Module::ScanDeps to analyze | |
4028 | # https://rt.cpan.org/Ticket/Display.html?id=6566 | |
4029 | local $@; | |
4030 | eval { require Imager::File::GIF }; | |
4031 | eval { require Imager::File::JPEG }; | |
4032 | eval { require Imager::File::PNG }; | |
4033 | eval { require Imager::File::SGI }; | |
4034 | eval { require Imager::File::TIFF }; | |
4035 | eval { require Imager::File::ICO }; | |
4036 | eval { require Imager::Font::W32 }; | |
4037 | eval { require Imager::Font::FT2 }; | |
4038 | eval { require Imager::Font::T1 }; | |
4039 | } | |
4040 | ||
1d7e3124 TC |
4041 | # backward compatibility for %formats |
4042 | package Imager::FORMATS; | |
4043 | use strict; | |
4044 | use constant IX_FORMATS => 0; | |
4045 | use constant IX_LIST => 1; | |
4046 | use constant IX_INDEX => 2; | |
4047 | use constant IX_CLASSES => 3; | |
4048 | ||
4049 | sub TIEHASH { | |
4050 | my ($class, $formats, $classes) = @_; | |
4051 | ||
4052 | return bless [ $formats, [ ], 0, $classes ], $class; | |
4053 | } | |
4054 | ||
4055 | sub _check { | |
4056 | my ($self, $key) = @_; | |
4057 | ||
4058 | (my $file = $self->[IX_CLASSES]{$key} . ".pm") =~ s(::)(/)g; | |
4059 | my $value; | |
5970bd39 TC |
4060 | my $error; |
4061 | my $loaded = Imager::_load_file($file, \$error); | |
4062 | if ($loaded) { | |
1d7e3124 TC |
4063 | $value = 1; |
4064 | } | |
4065 | else { | |
5970bd39 TC |
4066 | if ($error =~ /^Can't locate /) { |
4067 | $error = "Can't locate $file"; | |
4068 | } | |
4069 | $reader_load_errors{$key} = $writer_load_errors{$key} = $error; | |
1d7e3124 TC |
4070 | $value = undef; |
4071 | } | |
4072 | $self->[IX_FORMATS]{$key} = $value; | |
4073 | ||
4074 | return $value; | |
4075 | } | |
4076 | ||
4077 | sub FETCH { | |
4078 | my ($self, $key) = @_; | |
4079 | ||
4080 | exists $self->[IX_FORMATS]{$key} and return $self->[IX_FORMATS]{$key}; | |
4081 | ||
4082 | $self->[IX_CLASSES]{$key} or return undef; | |
4083 | ||
4084 | return $self->_check($key); | |
4085 | } | |