Debian Codegraph @

Imager.pm

raw
1 package Imager;
2 use 5.006;
3
4 use strict;
5 use Scalar::Util;
6 use Imager::Color;
7 use Imager::Color::Float;
8 use Imager::Font;
9 use Imager::TrimColorList;
10 use POSIX qw(INT_MIN INT_MAX);
11 use if $] >= 5.014, "warnings::register" => qw(tagcodes channelmask);
12
13 our $ERRSTR;
14
15 our @EXPORT_OK = qw(
16 init
17 init_log
18 DSO_open
19 DSO_close
20 DSO_funclist
21 DSO_call
22
23 load_plugin
24 unload_plugin
25
26 i_list_formats
27
28 i_color_new
29 i_color_set
30 i_color_info
31
32 i_img_info
33
34 i_img_setmask
35 i_img_getmask
36
37 i_line
38 i_line_aa
39 i_box
40 i_box_filled
41 i_arc
42 i_circle_aa
43
44 i_bezier_multi
45 i_poly_aa
46 i_poly_aa_cfill
47
48 i_copyto
49 i_rubthru
50 i_scaleaxis
51 i_scale_nn
52 i_haar
53 i_count_colors
54
55 i_gaussian
56 i_conv
57
58 i_convert
59 i_map
60
61 i_img_diff
62
63 i_tt_set_aa
64 i_tt_cp
65 i_tt_text
66 i_tt_bbox
67
68 i_readpnm_wiol
69 i_writeppm_wiol
70
71 i_readraw_wiol
72 i_writeraw_wiol
73
74 i_contrast
75 i_hardinvert
76 i_noise
77 i_bumpmap
78 i_postlevels
79 i_mosaic
80 i_watermark
81
82 malloc_state
83
84 list_formats
85
86 i_gifquant
87
88 newfont
89 newcolor
90 newcolour
91 NC
92 NF
93 NCF
94 );
95
96 our @EXPORT=qw(
97 );
98
99 our %EXPORT_TAGS=
100 (handy => [qw(
101 newfont
102 newcolor
103 NF
104 NC
105 NCF
106 )],
107 all => [@EXPORT_OK],
108 default => [qw(
109 load_plugin
110 unload_plugin
111 )]);
112
113 # registered file readers
114 my %readers;
115
116 # registered file writers
117 my %writers;
118
119 # modules we attempted to autoload
120 my %attempted_to_load;
121
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
129 # library keys that are image file formats
130 my %file_formats = map { $_ => 1 } qw/tiff pnm gif png jpeg raw bmp tga/;
131
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
145 our $VERSION;
146
147 BEGIN {
148 require Exporter;
149 my $ex_version = eval $Exporter::VERSION;
150 if ($ex_version < 5.57) {
151 our @ISA = qw(Exporter);
152 }
153 $VERSION = '1.036';
154 require XSLoader;
155 XSLoader::load(Imager => $VERSION);
156 }
157
158 my %formats_low;
159 my %format_classes =
160 (
161 png => "Imager::File::PNG",
162 gif => "Imager::File::GIF",
163 tiff => "Imager::File::TIFF",
164 jpeg => "Imager::File::JPEG",
165 w32 => "Imager::Font::W32",
166 ft2 => "Imager::Font::FT2",
167 t1 => "Imager::Font::T1",
168 );
169
170 our %formats;
171
172 tie %formats, "Imager::FORMATS", \%formats_low, \%format_classes;
173
174 our %filters;
175
176 our $DEBUG;
177 our %OPCODES;
178 our $FORMATGUESS;
179 our $warn_obsolete;
180
181 BEGIN {
182 for(i_list_formats()) { $formats_low{$_}++; }
183
184 %OPCODES=(Add=>[0],Sub=>[1],Mult=>[2],Div=>[3],Parm=>[4],'sin'=>[5],'cos'=>[6],'x'=>[4,0],'y'=>[4,1]);
185
186 $DEBUG=0;
187
188 # the members of the subhashes under %filters are:
189 # callseq - a list of the parameters to the underlying filter in the
190 # order they are passed
191 # callsub - a code ref that takes a named parameter list and calls the
192 # underlying filter
193 # defaults - a hash of default values
194 # names - defines names for value of given parameters so if the names
195 # field is foo=> { bar=>1 }, and the user supplies "bar" as the
196 # foo parameter, the filter will receive 1 for the foo
197 # parameter
198 $filters{contrast}={
199 callseq => ['image','intensity'],
200 callsub => sub { my %hsh=@_; i_contrast($hsh{image},$hsh{intensity}); }
201 };
202
203 $filters{noise} ={
204 callseq => ['image', 'amount', 'subtype'],
205 defaults => { amount=>3,subtype=>0 },
206 callsub => sub { my %hsh=@_; i_noise($hsh{image},$hsh{amount},$hsh{subtype}); }
207 };
208
209 $filters{hardinvert} ={
210 callseq => ['image'],
211 defaults => { },
212 callsub => sub { my %hsh=@_; i_hardinvert($hsh{image}); }
213 };
214
215 $filters{hardinvertall} =
216 {
217 callseq => ['image'],
218 defaults => { },
219 callsub => sub { my %hsh=@_; i_hardinvertall($hsh{image}); }
220 };
221
222 $filters{autolevels_skew} ={
223 callseq => ['image','lsat','usat','skew'],
224 defaults => { lsat=>0.1,usat=>0.1,skew=>0.0 },
225 callsub => sub { my %hsh=@_; i_autolevels($hsh{image},$hsh{lsat},$hsh{usat},$hsh{skew}); }
226 };
227
228 $filters{autolevels} ={
229 callseq => ['image','lsat','usat'],
230 defaults => { lsat=>0.1,usat=>0.1 },
231 callsub => sub { my %hsh=@_; i_autolevels_mono($hsh{image},$hsh{lsat},$hsh{usat}); }
232 };
233
234 $filters{turbnoise} ={
235 callseq => ['image'],
236 defaults => { xo=>0.0,yo=>0.0,scale=>10.0 },
237 callsub => sub { my %hsh=@_; i_turbnoise($hsh{image},$hsh{xo},$hsh{yo},$hsh{scale}); }
238 };
239
240 $filters{radnoise} ={
241 callseq => ['image'],
242 defaults => { xo=>100,yo=>100,ascale=>17.0,rscale=>0.02 },
243 callsub => sub { my %hsh=@_; i_radnoise($hsh{image},$hsh{xo},$hsh{yo},$hsh{rscale},$hsh{ascale}); }
244 };
245
246 $filters{conv} =
247 {
248 callseq => ['image', 'coef'],
249 defaults => { },
250 callsub =>
251 sub {
252 my %hsh=@_;
253 i_conv($hsh{image},$hsh{coef})
254 or die Imager->_error_as_msg() . "\n";
255 }
256 };
257
258 $filters{gradgen} =
259 {
260 callseq => ['image', 'xo', 'yo', 'colors', 'dist'],
261 defaults => { dist => 0 },
262 callsub =>
263 sub {
264 my %hsh=@_;
265 my @colors = @{$hsh{colors}};
266 $_ = _color($_)
267 for @colors;
268 i_gradgen($hsh{image}, $hsh{xo}, $hsh{yo}, \@colors, $hsh{dist});
269 }
270 };
271
272 $filters{nearest_color} =
273 {
274 callseq => ['image', 'xo', 'yo', 'colors', 'dist'],
275 defaults => { },
276 callsub =>
277 sub {
278 my %hsh=@_;
279 # make sure the segments are specified with colors
280 my @colors;
281 for my $color (@{$hsh{colors}}) {
282 my $new_color = _color($color)
283 or die $Imager::ERRSTR."\n";
284 push @colors, $new_color;
285 }
286
287 i_nearest_color($hsh{image}, $hsh{xo}, $hsh{yo}, \@colors,
288 $hsh{dist})
289 or die Imager->_error_as_msg() . "\n";
290 },
291 };
292 $filters{gaussian} = {
293 callseq => [ 'image', 'stddev' ],
294 defaults => { },
295 callsub => sub { my %hsh = @_; i_gaussian($hsh{image}, $hsh{stddev}); },
296 };
297 $filters{gaussian2} = {
298 callseq => [ 'image', 'stddevX', 'stddevY' ],
299 defaults => { },
300 callsub => sub { my %hsh = @_; i_gaussian2($hsh{image}, $hsh{stddevX}, $hsh{stddevY}); },
301 };
302 $filters{mosaic} =
303 {
304 callseq => [ qw(image size) ],
305 defaults => { size => 20 },
306 callsub => sub { my %hsh = @_; i_mosaic($hsh{image}, $hsh{size}) },
307 };
308 $filters{bumpmap} =
309 {
310 callseq => [ qw(image bump elevation lightx lighty st) ],
311 defaults => { elevation=>0, st=> 2 },
312 callsub => sub {
313 my %hsh = @_;
314 i_bumpmap($hsh{image}, $hsh{bump}{IMG}, $hsh{elevation},
315 $hsh{lightx}, $hsh{lighty}, $hsh{st});
316 },
317 };
318 $filters{bumpmap_complex} =
319 {
320 callseq => [ qw(image bump channel tx ty Lx Ly Lz cd cs n Ia Il Is) ],
321 defaults => {
322 channel => 0,
323 tx => 0,
324 ty => 0,
325 Lx => 0.2,
326 Ly => 0.4,
327 Lz => -1.0,
328 cd => 1.0,
329 cs => 40,
330 n => 1.3,
331 Ia => [0,0,0],
332 Il => [255,255,255],
333 Is => [255,255,255],
334 },
335 callsub => sub {
336 my %hsh = @_;
337 for my $cname (qw/Ia Il Is/) {
338 my $old = $hsh{$cname};
339 my $new_color = _color($old)
340 or die $Imager::ERRSTR, "\n";
341 $hsh{$cname} = $new_color;
342 }
343 i_bumpmap_complex($hsh{image}, $hsh{bump}{IMG}, $hsh{channel},
344 $hsh{tx}, $hsh{ty}, $hsh{Lx}, $hsh{Ly}, $hsh{Lz},
345 $hsh{cd}, $hsh{cs}, $hsh{n}, $hsh{Ia}, $hsh{Il},
346 $hsh{Is});
347 },
348 };
349 $filters{postlevels} =
350 {
351 callseq => [ qw(image levels) ],
352 defaults => { levels => 10 },
353 callsub => sub { my %hsh = @_; i_postlevels($hsh{image}, $hsh{levels}); },
354 };
355 $filters{watermark} =
356 {
357 callseq => [ qw(image wmark tx ty pixdiff) ],
358 defaults => { pixdiff=>10, tx=>0, ty=>0 },
359 callsub =>
360 sub {
361 my %hsh = @_;
362 i_watermark($hsh{image}, $hsh{wmark}{IMG}, $hsh{tx}, $hsh{ty},
363 $hsh{pixdiff});
364 },
365 };
366 $filters{fountain} =
367 {
368 callseq => [ qw(image xa ya xb yb ftype repeat combine super_sample ssample_param segments) ],
369 names => {
370 ftype => { linear => 0,
371 bilinear => 1,
372 radial => 2,
373 radial_square => 3,
374 revolution => 4,
375 conical => 5 },
376 repeat => { none => 0,
377 sawtooth => 1,
378 triangle => 2,
379 saw_both => 3,
380 tri_both => 4,
381 },
382 super_sample => {
383 none => 0,
384 grid => 1,
385 random => 2,
386 circle => 3,
387 },
388 combine => {
389 none => 0,
390 normal => 1,
391 multiply => 2, mult => 2,
392 dissolve => 3,
393 add => 4,
394 subtract => 5, 'sub' => 5,
395 diff => 6,
396 lighten => 7,
397 darken => 8,
398 hue => 9,
399 sat => 10,
400 value => 11,
401 color => 12,
402 },
403 },
404 defaults => { ftype => 0, repeat => 0, combine => 0,
405 super_sample => 0, ssample_param => 4,
406 segments=>[
407 [ 0, 0.5, 1,
408 [0,0,0],
409 [255, 255, 255],
410 0, 0,
411 ],
412 ],
413 },
414 callsub =>
415 sub {
416 my %hsh = @_;
417
418 # make sure the segments are specified with colors
419 my @segments;
420 for my $segment (@{$hsh{segments}}) {
421 my @new_segment = @$segment;
422
423 $_ = _color($_) or die $Imager::ERRSTR."\n" for @new_segment[3,4];
424 push @segments, \@new_segment;
425 }
426
427 i_fountain($hsh{image}, $hsh{xa}, $hsh{ya}, $hsh{xb}, $hsh{yb},
428 $hsh{ftype}, $hsh{repeat}, $hsh{combine}, $hsh{super_sample},
429 $hsh{ssample_param}, \@segments)
430 or die Imager->_error_as_msg() . "\n";
431 },
432 };
433 $filters{unsharpmask} =
434 {
435 callseq => [ qw(image stddev scale) ],
436 defaults => { stddev=>2.0, scale=>1.0 },
437 callsub =>
438 sub {
439 my %hsh = @_;
440 i_unsharp_mask($hsh{image}, $hsh{stddev}, $hsh{scale});
441 },
442 };
443
444 $FORMATGUESS=\&def_guess_type;
445
446 $warn_obsolete = 1;
447 }
448
449 #
450 # Non methods
451 #
452
453 # initialize Imager
454 # NOTE: this might be moved to an import override later on
455
456 sub import {
457 my $i = 1;
458 while ($i < @_) {
459 if ($_[$i] eq '-log-stderr') {
460 init_log(undef, 4);
461 splice(@_, $i, 1);
462 }
463 else {
464 ++$i;
465 }
466 }
467 goto &Exporter::import;
468 }
469
470 sub init_log {
471 Imager->open_log(log => $_[0], level => $_[1]);
472 }
473
474
475 sub init {
476 my %parms=(loglevel=>1,@_);
477
478 if (exists $parms{'warn_obsolete'}) {
479 $warn_obsolete = $parms{'warn_obsolete'};
480 }
481
482 if ($parms{'log'}) {
483 Imager->open_log(log => $parms{log}, level => $parms{loglevel})
484 or return;
485 }
486
487 if (exists $parms{'t1log'}) {
488 if ($formats{t1}) {
489 if (Imager::Font::T1::i_init_t1($parms{'t1log'})) {
490 Imager->_set_error(Imager->_error_as_msg);
491 return;
492 }
493 }
494 }
495
496 return 1;
497 }
498
499 {
500 my $is_logging = 0;
501
502 sub open_log {
503 my $class = shift;
504 my (%opts) = ( loglevel => 1, @_ );
505
506 $is_logging = i_init_log($opts{log}, $opts{loglevel});
507 unless ($is_logging) {
508 Imager->_set_error(Imager->_error_as_msg());
509 return;
510 }
511
512 Imager->log("Imager $VERSION starting\n", 1);
513
514 return $is_logging;
515 }
516
517 sub close_log {
518 i_init_log(undef, -1);
519 $is_logging = 0;
520 }
521
522 sub log {
523 my ($class, $message, $level) = @_;
524
525 defined $level or $level = 1;
526
527 i_log_entry($message, $level);
528 }
529
530 sub is_logging {
531 return $is_logging;
532 }
533 }
534
535 END {
536 if ($DEBUG) {
537 print "shutdown code\n";
538 # for(keys %instances) { $instances{$_}->DESTROY(); }
539 malloc_state(); # how do decide if this should be used? -- store something from the import
540 print "Imager exiting\n";
541 }
542 }
543
544 # Load a filter plugin
545
546 our %DSOs;
547
548 sub load_plugin {
549 my ($filename)=@_;
550 my $i;
551
552 if ($^O eq 'android') {
553 require File::Spec;
554 $filename = File::Spec->rel2abs($filename);
555 }
556
557 my ($DSO_handle,$str)=DSO_open($filename);
558 if (!defined($DSO_handle)) { $Imager::ERRSTR="Couldn't load plugin '$filename'\n"; return undef; }
559 my %funcs=DSO_funclist($DSO_handle);
560 if ($DEBUG) { print "loading module $filename\n"; $i=0; for(keys %funcs) { printf(" %2d: %s\n",$i++,$_); } }
561 $i=0;
562 for(keys %funcs) { if ($filters{$_}) { $ERRSTR="filter '$_' already exists\n"; DSO_close($DSO_handle); return undef; } }
563
564 $DSOs{$filename}=[$DSO_handle,\%funcs];
565
566 for(keys %funcs) {
567 my $evstr="\$filters{'".$_."'}={".$funcs{$_}.'};';
568 $DEBUG && print "eval string:\n",$evstr,"\n";
569 eval $evstr;
570 print $@ if $@;
571 }
572 return 1;
573 }
574
575 # Unload a plugin
576
577 sub unload_plugin {
578 my ($filename)=@_;
579
580 if ($^O eq 'android') {
581 require File::Spec;
582 $filename = File::Spec->rel2abs($filename);
583 }
584
585 if (!$DSOs{$filename}) { $ERRSTR="plugin '$filename' not loaded."; return undef; }
586 my ($DSO_handle,$funcref)=@{$DSOs{$filename}};
587 for(keys %{$funcref}) {
588 delete $filters{$_};
589 $DEBUG && print "unloading: $_\n";
590 }
591 my $rc=DSO_close($DSO_handle);
592 if (!defined($rc)) { $ERRSTR="unable to unload plugin '$filename'."; return undef; }
593 return 1;
594 }
595
596 # take the results of i_error() and make a message out of it
597 sub _error_as_msg {
598 return join(": ", map $_->[0], i_errors());
599 }
600
601 # this function tries to DWIM for color parameters
602 # color objects are used as is
603 # simple scalars are simply treated as single parameters to Imager::Color->new
604 # hashrefs are treated as named argument lists to Imager::Color->new
605 # arrayrefs are treated as list arguments to Imager::Color->new iff any
606 # parameter is > 1
607 # other arrayrefs are treated as list arguments to Imager::Color::Float
608
609 sub _color {
610 my $arg = shift;
611 # perl 5.6.0 seems to do weird things to $arg if we don't make an
612 # explicitly stringified copy
613 # I vaguely remember a bug on this on p5p, but couldn't find it
614 # through bugs.perl.org (I had trouble getting it to find any bugs)
615 my $copy = $arg . "";
616 my $result;
617
618 if (ref $arg) {
619 if (UNIVERSAL::isa($arg, "Imager::Color")
620 || UNIVERSAL::isa($arg, "Imager::Color::Float")) {
621 $result = $arg;
622 }
623 else {
624 if ($copy =~ /^HASH\(/) {
625 $result = Imager::Color->new(%$arg);
626 }
627 elsif ($copy =~ /^ARRAY\(/) {
628 $result = Imager::Color->new(@$arg);
629 }
630 else {
631 $Imager::ERRSTR = "Not a color";
632 }
633 }
634 }
635 else {
636 # assume Imager::Color::new knows how to handle it
637 $result = Imager::Color->new($arg);
638 }
639
640 return $result;
641 }
642
643 sub _combine {
644 my ($self, $combine, $default) = @_;
645
646 if (!defined $combine && ref $self) {
647 $combine = $self->{combine};
648 }
649 defined $combine or $combine = $defaults{combine};
650 defined $combine or $combine = $default;
651
652 if (exists $combine_types{$combine}) {
653 $combine = $combine_types{$combine};
654 }
655
656 return $combine;
657 }
658
659 sub _valid_image {
660 my ($self, $method) = @_;
661
662 ref $self
663 or return Imager->_set_error("$method needs an image object");
664
665 $self->{IMG} && Scalar::Util::blessed($self->{IMG}) and return 1;
666
667 my $msg = $self->{IMG} ? "images do not cross threads" : "empty input image";
668 $msg = "$method: $msg" if $method;
669 $self->_set_error($msg);
670
671 return;
672 }
673
674 # returns first defined parameter
675 sub _first {
676 for (@_) {
677 return $_ if defined $_;
678 }
679 return undef;
680 }
681
682 #
683 # Methods to be called on objects.
684 #
685
686 # Create a new Imager object takes very few parameters.
687 # usually you call this method and then call open from
688 # the resulting object
689
690 sub new {
691 my $class = shift;
692 my $self ={};
693 my %hsh=@_;
694 bless $self,$class;
695 $self->{IMG}=undef; # Just to indicate what exists
696 $self->{ERRSTR}=undef; #
697 $self->{DEBUG}=$DEBUG;
698 $self->{DEBUG} and print "Initialized Imager\n";
699 if (defined $hsh{file} ||
700 defined $hsh{fh} ||
701 defined $hsh{fd} ||
702 defined $hsh{callback} ||
703 defined $hsh{readcb} ||
704 defined $hsh{data} ||
705 defined $hsh{io}) {
706 # allow $img = Imager->new(file => $filename)
707 my %extras;
708
709 # type is already used as a parameter to new(), rename it for the
710 # call to read()
711 if ($hsh{filetype}) {
712 $extras{type} = $hsh{filetype};
713 }
714 unless ($self->read(%hsh, %extras)) {
715 $Imager::ERRSTR = $self->{ERRSTR};
716 return;
717 }
718 }
719 elsif (defined $hsh{xsize} || defined $hsh{ysize}) {
720 unless ($self->img_set(%hsh)) {
721 $Imager::ERRSTR = $self->{ERRSTR};
722 return;
723 }
724 }
725 elsif (%hsh) {
726 Imager->_set_error("new: supply xsize and ysize or a file access parameter or no parameters");
727 return;
728 }
729
730 return $self;
731 }
732
733 # Copy an entire image with no changes
734 # - if an image has magic the copy of it will not be magical
735
736 sub copy {
737 my $self = shift;
738
739 $self->_valid_image("copy")
740 or return;
741
742 unless (defined wantarray) {
743 my @caller = caller;
744 warn "copy() called in void context - copy() returns the copied image at $caller[1] line $caller[2]\n";
745 return;
746 }
747
748 my $newcopy=Imager->new();
749 $newcopy->{IMG} = i_copy($self->{IMG});
750 return $newcopy;
751 }
752
753 # Paste a region
754
755 sub paste {
756 my $self = shift;
757
758 $self->_valid_image("paste")
759 or return;
760
761 my %input=(left=>0, top=>0, src_minx => 0, src_miny => 0, @_);
762 my $src = $input{img} || $input{src};
763 unless($src) {
764 $self->_set_error("no source image");
765 return;
766 }
767 unless ($src->_valid_image("paste")) {
768 $self->{ERRSTR} = $src->{ERRSTR} . " (for src)";
769 return;
770 }
771 $input{left}=0 if $input{left} <= 0;
772 $input{top}=0 if $input{top} <= 0;
773
774 my($r,$b)=i_img_info($src->{IMG});
775 my ($src_left, $src_top) = @input{qw/src_minx src_miny/};
776 my ($src_right, $src_bottom);
777 if ($input{src_coords}) {
778 ($src_left, $src_top, $src_right, $src_bottom) = @{$input{src_coords}}
779 }
780 else {
781 if (defined $input{src_maxx}) {
782 $src_right = $input{src_maxx};
783 }
784 elsif (defined $input{width}) {
785 if ($input{width} <= 0) {
786 $self->_set_error("paste: width must me positive");
787 return;
788 }
789 $src_right = $src_left + $input{width};
790 }
791 else {
792 $src_right = $r;
793 }
794 if (defined $input{src_maxy}) {
795 $src_bottom = $input{src_maxy};
796 }
797 elsif (defined $input{height}) {
798 if ($input{height} < 0) {
799 $self->_set_error("paste: height must be positive");
800 return;
801 }
802 $src_bottom = $src_top + $input{height};
803 }
804 else {
805 $src_bottom = $b;
806 }
807 }
808
809 $src_right > $r and $src_right = $r;
810 $src_bottom > $b and $src_bottom = $b;
811
812 if ($src_right <= $src_left
813 || $src_bottom < $src_top) {
814 $self->_set_error("nothing to paste");
815 return;
816 }
817
818 i_copyto($self->{IMG}, $src->{IMG},
819 $src_left, $src_top, $src_right, $src_bottom,
820 $input{left}, $input{top});
821
822 return $self; # What should go here??
823 }
824
825 # Crop an image - i.e. return a new image that is smaller
826
827 sub crop {
828 my $self=shift;
829
830 $self->_valid_image("crop")
831 or return;
832
833 unless (defined wantarray) {
834 my @caller = caller;
835 warn "crop() called in void context - crop() returns the cropped image at $caller[1] line $caller[2]\n";
836 return;
837 }
838
839 my %hsh=@_;
840
841 my ($w, $h, $l, $r, $b, $t) =
842 @hsh{qw(width height left right bottom top)};
843
844 # work through the various possibilities
845 if (defined $l) {
846 if (defined $w) {
847 $r = $l + $w;
848 }
849 elsif (!defined $r) {
850 $r = $self->getwidth;
851 }
852 }
853 elsif (defined $r) {
854 if (defined $w) {
855 $l = $r - $w;
856 }
857 else {
858 $l = 0;
859 }
860 }
861 elsif (defined $w) {
862 $l = int(0.5+($self->getwidth()-$w)/2);
863 $r = $l + $w;
864 }
865 else {
866 $l = 0;
867 $r = $self->getwidth;
868 }
869 if (defined $t) {
870 if (defined $h) {
871 $b = $t + $h;
872 }
873 elsif (!defined $b) {
874 $b = $self->getheight;
875 }
876 }
877 elsif (defined $b) {
878 if (defined $h) {
879 $t = $b - $h;
880 }
881 else {
882 $t = 0;
883 }
884 }
885 elsif (defined $h) {
886 $t=int(0.5+($self->getheight()-$h)/2);
887 $b=$t+$h;
888 }
889 else {
890 $t = 0;
891 $b = $self->getheight;
892 }
893
894 ($l,$r)=($r,$l) if $l>$r;
895 ($t,$b)=($b,$t) if $t>$b;
896
897 $l < 0 and $l = 0;
898 $r > $self->getwidth and $r = $self->getwidth;
899 $t < 0 and $t = 0;
900 $b > $self->getheight and $b = $self->getheight;
901
902 if ($l == $r || $t == $b) {
903 $self->_set_error("resulting image would have no content");
904 return;
905 }
906 if( $r < $l or $b < $t ) {
907 $self->_set_error("attempting to crop outside of the image");
908 return;
909 }
910 my $dst = $self->_sametype(xsize=>$r-$l, ysize=>$b-$t);
911
912 i_copyto($dst->{IMG},$self->{IMG},$l,$t,$r,$b,0,0);
913 return $dst;
914 }
915
916 my $empty_trim_colors = Imager::TrimColorList->new();
917
918 sub _trim_rect {
919 my ($self, $name, %hsh) = @_;
920
921 $self->_valid_image($name)
922 or return;
923
924 my $auto = delete $hsh{auto};
925 my $colors = delete $hsh{colors} || $empty_trim_colors;
926 my $alpha = delete $hsh{alpha} || 0;
927 my $tolerance = delete $hsh{tolerance};
928 defined $tolerance or $tolerance = 0.01;
929
930 if (keys %hsh) {
931 $self->_set_error("$name: unexpected arguments:".join(", ", sort keys %hsh));
932 return;
933 }
934
935 if ($auto) {
936 if ($colors != $empty_trim_colors) {
937 $self->_set_error("$name: only one of auto and colors can be supplied");
938 return;
939 }
940 if ($tolerance < 0) {
941 $self->_set_error("$name: tolerance must be non-negative");
942 return;
943 }
944
945 $colors = Imager::TrimColorList->auto
946 (
947 auto => $auto,
948 tolerance => $tolerance,
949 name => $name,
950 image => $self,
951 );
952 unless ($colors) {
953 $self->_set_error(Imager->errstr);
954 return;
955 }
956 }
957
958 unless (ref $colors) {
959 $self->_set_error("$name: colors must be an arrayref or an Imager::TrimColorList object");
960 return;
961 }
962 unless (UNIVERSAL::isa($colors, "Imager::TrimColorList")) {
963 unless (Scalar::Util::reftype($colors) eq "ARRAY") {
964 $self->_set_error("$name: colors must be an arrayref or an Imager::TrimColorList object");
965 return;
966 }
967 $colors = Imager::TrimColorList->new(@$colors);
968 }
969
970 return i_trim_rect($self->{IMG}, $alpha, $colors);
971 }
972
973 sub trim_rect {
974 my ($self, %hsh) = @_;
975
976 return $self->_trim_rect("trim_rect", %hsh);
977 }
978
979 sub trim {
980 my ($self, %hsh) = @_;
981
982 my ($left, $top, $right, $bottom) = $self->_trim_rect("trim", %hsh)
983 or return;
984
985 if ($left == $self->getwidth) {
986 # the whole image would be trimmed, but we don't support zero
987 # width or height images.
988 return $self->crop(width => 1, height => 1);
989 }
990 else {
991 my ($w, $h) = i_img_info($self->{IMG});
992 return $self->crop(left => $left, right => $w - $right,
993 top => $top, bottom => $h - $bottom);
994 }
995 }
996
997 sub _sametype {
998 my ($self, %opts) = @_;
999
1000 $self->_valid_image
1001 or return;
1002
1003 my $x = $opts{xsize} || $self->getwidth;
1004 my $y = $opts{ysize} || $self->getheight;
1005 my $channels = $opts{channels} || $self->getchannels;
1006
1007 my $out = Imager->new;
1008 if ($channels == $self->getchannels) {
1009 $out->{IMG} = i_sametype($self->{IMG}, $x, $y);
1010 }
1011 else {
1012 $out->{IMG} = i_sametype_chans($self->{IMG}, $x, $y, $channels);
1013 }
1014 unless ($out->{IMG}) {
1015 $self->{ERRSTR} = $self->_error_as_msg;
1016 return;
1017 }
1018
1019 return $out;
1020 }
1021
1022 # Sets an image to a certain size and channel number
1023 # if there was previously data in the image it is discarded
1024
1025 my %model_channels =
1026 (
1027 gray => 1,
1028 graya => 2,
1029 rgb => 3,
1030 rgba => 4,
1031 );
1032
1033 sub img_set {
1034 my $self=shift;
1035
1036 my %hsh=(xsize=>100, ysize=>100, channels=>3, bits=>8, type=>'direct', @_);
1037
1038 undef($self->{IMG});
1039
1040 if ($hsh{model}) {
1041 if (my $channels = $model_channels{$hsh{model}}) {
1042 $hsh{channels} = $channels;
1043 }
1044 else {
1045 $self->_set_error("new: unknown value for model '$hsh{model}'");
1046 return;
1047 }
1048 }
1049
1050 if ($hsh{type} eq 'paletted' || $hsh{type} eq 'pseudo') {
1051 $self->{IMG} = i_img_pal_new($hsh{xsize}, $hsh{ysize}, $hsh{channels},
1052 256);
1053 }
1054 elsif ($hsh{bits} eq 'double') {
1055 $self->{IMG} = i_img_double_new($hsh{xsize}, $hsh{ysize}, $hsh{channels});
1056 }
1057 elsif ($hsh{bits} == 16) {
1058 $self->{IMG} = i_img_16_new($hsh{xsize}, $hsh{ysize}, $hsh{channels});
1059 }
1060 else {
1061 $self->{IMG}= i_img_8_new($hsh{'xsize'}, $hsh{'ysize'},
1062 $hsh{'channels'});
1063 }
1064
1065 unless ($self->{IMG}) {
1066 $self->_set_error(Imager->_error_as_msg());
1067 return;
1068 }
1069
1070 $self;
1071 }
1072
1073 # created a masked version of the current image
1074 sub masked {
1075 my $self = shift;
1076
1077 $self->_valid_image("masked")
1078 or return;
1079
1080 my %opts = (left => 0,
1081 top => 0,
1082 right => $self->getwidth,
1083 bottom => $self->getheight,
1084 @_);
1085 my $mask = $opts{mask} ? $opts{mask}{IMG} : undef;
1086
1087 my ($left, $top, $right, $bottom) = @opts{qw(left top right bottom)};
1088 for my $val ($left, $right) {
1089 if ($val < 0) {
1090 $val = $self->getwidth() + $val;
1091 }
1092 }
1093 for my $val ($top, $bottom) {
1094 if ($val < 0) {
1095 $val = $self->getheight() + $val;
1096 }
1097 }
1098
1099 my $result = Imager->new;
1100 $result->{IMG} = i_img_masked_new($self->{IMG}, $mask, $left,
1101 $top, $right - $left,
1102 $bottom - $top);
1103 unless ($result->{IMG}) {
1104 $self->_set_error(Imager->_error_as_msg);
1105 return;
1106 }
1107
1108 # keep references to the mask and base images so they don't
1109 # disappear on us
1110 $result->{DEPENDS} = [ $self->{IMG}, $mask ];
1111
1112 return $result;
1113 }
1114
1115 # convert an RGB image into a paletted image
1116 sub to_paletted {
1117 my $self = shift;
1118 my $opts;
1119 if (@_ != 1 && !ref $_[0]) {
1120 $opts = { @_ };
1121 }
1122 else {
1123 $opts = shift;
1124 }
1125
1126 unless (defined wantarray) {
1127 my @caller = caller;
1128 warn "to_paletted() called in void context - to_paletted() returns the converted image at $caller[1] line $caller[2]\n";
1129 return;
1130 }
1131
1132 $self->_valid_image("to_paletted")
1133 or return;
1134
1135 my $result = Imager->new;
1136 unless ($result->{IMG} = i_img_to_pal($self->{IMG}, $opts)) {
1137 $self->_set_error(Imager->_error_as_msg);
1138 return;
1139 }
1140
1141 return $result;
1142 }
1143
1144 sub make_palette {
1145 my ($class, $quant, @images) = @_;
1146
1147 unless (@images) {
1148 Imager->_set_error("make_palette: supply at least one image");
1149 return;
1150 }
1151 my $index = 1;
1152 for my $img (@images) {
1153 unless ($img->{IMG}) {
1154 Imager->_set_error("make_palette: image $index is empty");
1155 return;
1156 }
1157 ++$index;
1158 }
1159
1160 my @cols = i_img_make_palette($quant, map $_->{IMG}, @images);
1161 unless (@cols) {
1162 Imager->_set_error(Imager->_error_as_msg);
1163 return;
1164 }
1165 return @cols;
1166 }
1167
1168 # convert a paletted (or any image) to an 8-bit/channel RGB image
1169 sub to_rgb8 {
1170 my $self = shift;
1171
1172 unless (defined wantarray) {
1173 my @caller = caller;
1174 warn "to_rgb8() called in void context - to_rgb8() returns the converted image at $caller[1] line $caller[2]\n";
1175 return;
1176 }
1177
1178 $self->_valid_image("to_rgb8")
1179 or return;
1180
1181 my $result = Imager->new;
1182 unless ($result->{IMG} = i_img_to_rgb($self->{IMG})) {
1183 $self->_set_error(Imager->_error_as_msg());
1184 return;
1185 }
1186
1187 return $result;
1188 }
1189
1190 # convert a paletted (or any image) to a 16-bit/channel RGB image
1191 sub to_rgb16 {
1192 my $self = shift;
1193
1194 unless (defined wantarray) {
1195 my @caller = caller;
1196 warn "to_rgb16() called in void context - to_rgb16() returns the converted image at $caller[1] line $caller[2]\n";
1197 return;
1198 }
1199
1200 $self->_valid_image("to_rgb16")
1201 or return;
1202
1203 my $result = Imager->new;
1204 unless ($result->{IMG} = i_img_to_rgb16($self->{IMG})) {
1205 $self->_set_error(Imager->_error_as_msg());
1206 return;
1207 }
1208
1209 return $result;
1210 }
1211
1212 # convert a paletted (or any image) to an double/channel RGB image
1213 sub to_rgb_double {
1214 my $self = shift;
1215
1216 unless (defined wantarray) {
1217 my @caller = caller;
1218 warn "to_rgb16() called in void context - to_rgb_double() returns the converted image at $caller[1] line $caller[2]\n";
1219 return;
1220 }
1221
1222 $self->_valid_image("to_rgb_double")
1223 or return;
1224
1225 my $result = Imager->new;
1226 unless ($result->{IMG} = i_img_to_drgb($self->{IMG})) {
1227 $self->_set_error(Imager->_error_as_msg());
1228 return;
1229 }
1230
1231 return $result;
1232 }
1233
1234 sub addcolors {
1235 my $self = shift;
1236 my %opts = (colors=>[], @_);
1237
1238 $self->_valid_image("addcolors")
1239 or return -1;
1240
1241 my @colors = @{$opts{colors}}
1242 or return undef;
1243
1244 for my $color (@colors) {
1245 $color = _color($color);
1246 unless ($color) {
1247 $self->_set_error($Imager::ERRSTR);
1248 return;
1249 }
1250 }
1251
1252 return i_addcolors($self->{IMG}, @colors);
1253 }
1254
1255 sub setcolors {
1256 my $self = shift;
1257 my %opts = (start=>0, colors=>[], @_);
1258
1259 $self->_valid_image("setcolors")
1260 or return;
1261
1262 my @colors = @{$opts{colors}}
1263 or return undef;
1264
1265 for my $color (@colors) {
1266 $color = _color($color);
1267 unless ($color) {
1268 $self->_set_error($Imager::ERRSTR);
1269 return;
1270 }
1271 }
1272
1273 return i_setcolors($self->{IMG}, $opts{start}, @colors);
1274 }
1275
1276 sub getcolors {
1277 my $self = shift;
1278 my %opts = @_;
1279
1280 $self->_valid_image("getcolors")
1281 or return;
1282
1283 if (!exists $opts{start} && !exists $opts{count}) {
1284 # get them all
1285 $opts{start} = 0;
1286 $opts{count} = $self->colorcount;
1287 }
1288 elsif (!exists $opts{count}) {
1289 $opts{count} = 1;
1290 }
1291 elsif (!exists $opts{start}) {
1292 $opts{start} = 0;
1293 }
1294
1295 return i_getcolors($self->{IMG}, $opts{start}, $opts{count});
1296 }
1297
1298 sub colorcount {
1299 my ($self) = @_;
1300
1301 $self->_valid_image("colorcount")
1302 or return -1;
1303
1304 return i_colorcount($self->{IMG});
1305 }
1306
1307 sub maxcolors {
1308 my $self = shift;
1309
1310 $self->_valid_image("maxcolors")
1311 or return -1;
1312
1313 i_maxcolors($self->{IMG});
1314 }
1315
1316 sub findcolor {
1317 my $self = shift;
1318 my %opts = @_;
1319
1320 $self->_valid_image("findcolor")
1321 or return;
1322
1323 unless ($opts{color}) {
1324 $self->_set_error("findcolor: no color parameter");
1325 return;
1326 }
1327
1328 my $color = _color($opts{color})
1329 or return;
1330
1331 return i_findcolor($self->{IMG}, $color);
1332 }
1333
1334 sub bits {
1335 my $self = shift;
1336
1337 $self->_valid_image("bits")
1338 or return;
1339
1340 my $bits = i_img_bits($self->{IMG});
1341 if ($bits && $bits == length(pack("d", 1)) * 8) {
1342 $bits = 'double';
1343 }
1344 return $bits;
1345 }
1346
1347 sub type {
1348 my $self = shift;
1349
1350 $self->_valid_image("type")
1351 or return;
1352
1353 return i_img_type($self->{IMG}) ? "paletted" : "direct";
1354 }
1355
1356 sub virtual {
1357 my $self = shift;
1358
1359 $self->_valid_image("virtual")
1360 or return;
1361
1362 return i_img_virtual($self->{IMG});
1363 }
1364
1365 sub is_bilevel {
1366 my ($self) = @_;
1367
1368 $self->_valid_image("is_bilevel")
1369 or return;
1370
1371 return i_img_is_monochrome($self->{IMG});
1372 }
1373
1374 sub tags {
1375 my ($self, %opts) = @_;
1376
1377 $self->_valid_image("tags")
1378 or return;
1379
1380 if (defined $opts{name}) {
1381 my @result;
1382 my $start = 0;
1383 my $found;
1384 while (defined($found = i_tags_find($self->{IMG}, $opts{name}, $start))) {
1385 push @result, (i_tags_get($self->{IMG}, $found))[1];
1386 $start = $found+1;
1387 }
1388 return wantarray ? @result : $result[0];
1389 }
1390 elsif (defined $opts{code}) {
1391 my @result;
1392 my $start = 0;
1393 my $found;
1394 while (defined($found = i_tags_findn($self->{IMG}, $opts{code}, $start))) {
1395 push @result, (i_tags_get($self->{IMG}, $found))[1];
1396 $start = $found+1;
1397 }
1398 return @result;
1399 }
1400 else {
1401 if (wantarray) {
1402 return map { [ i_tags_get($self->{IMG}, $_) ] } 0.. i_tags_count($self->{IMG})-1;
1403 }
1404 else {
1405 return i_tags_count($self->{IMG});
1406 }
1407 }
1408 }
1409
1410 my $int_re = qr/^(?:0|-[1-9][0-9]*|[1-9][0-9]*)$/;
1411
1412 sub addtag {
1413 my $self = shift;
1414 my %opts = @_;
1415
1416 $self->_valid_image("addtag")
1417 or return;
1418
1419 my $value = $opts{value};
1420 if ($opts{name}) {
1421 if (defined $value) {
1422 if ($value =~ $int_re && $value >= INT_MIN && $value <= INT_MAX) {
1423 # add as an int
1424 return i_tags_addn($self->{IMG}, $opts{name}, 0, $value);
1425 }
1426 else {
1427 return i_tags_add($self->{IMG}, $opts{name}, 0, $value, 0);
1428 }
1429 }
1430 elsif (defined $opts{data}) {
1431 # force addition as a string
1432 return i_tags_add($self->{IMG}, $opts{name}, 0, $opts{data}, 0);
1433 }
1434 else {
1435 $self->{ERRSTR} = "No value supplied";
1436 return undef;
1437 }
1438 }
1439 elsif ($opts{code}) {
1440 warnings::warnif("Imager::tagcodes", "addtag: code parameter is deprecated")
1441 if $] >= 5.014;
1442 if (defined $value) {
1443 if ($value =~ $int_re && $value >= INT_MIN && $value <= INT_MAX) {
1444 # add as a number
1445 return i_tags_addn($self->{IMG}, $opts{code}, 0, $opts{value});
1446 }
1447 else {
1448 return i_tags_add($self->{IMG}, $opts{code}, 0, $opts{value}, 0);
1449 }
1450 }
1451 elsif (defined $opts{data}) {
1452 # force addition as a string
1453 return i_tags_add($self->{IMG}, $opts{code}, 0, $opts{data}, 0);
1454 }
1455 else {
1456 $self->{ERRSTR} = "No value supplied";
1457 return undef;
1458 }
1459 }
1460 else {
1461 return undef;
1462 }
1463 }
1464
1465 sub deltag {
1466 my $self = shift;
1467 my %opts = @_;
1468
1469 $self->_valid_image("deltag")
1470 or return 0;
1471
1472 if (defined $opts{'index'}) {
1473 return i_tags_delete($self->{IMG}, $opts{'index'});
1474 }
1475 elsif (defined $opts{name}) {
1476 return i_tags_delbyname($self->{IMG}, $opts{name});
1477 }
1478 elsif (defined $opts{code}) {
1479 warnings::warnif("Imager::tagcodes", "deltag: code parameter is deprecated")
1480 if $] >= 5.014;
1481 return i_tags_delbycode($self->{IMG}, $opts{code});
1482 }
1483 else {
1484 $self->{ERRSTR} = "Need to supply index, name, or code parameter";
1485 return 0;
1486 }
1487 }
1488
1489 sub settag {
1490 my ($self, %opts) = @_;
1491
1492 $self->_valid_image("settag")
1493 or return;
1494
1495 if ($opts{name}) {
1496 $self->deltag(name=>$opts{name});
1497 return $self->addtag(name=>$opts{name}, value=>$opts{value});
1498 }
1499 elsif (defined $opts{code}) {
1500 warnings::warnif("Imager::tagcodes", "settag: code parameter is deprecated")
1501 if $] >= 5.014;
1502 i_tags_delbycode($self->{IMG}, $opts{code});
1503 if (defined $opts{value}) {
1504 if ($opts{value} =~ /^\d+$/) {
1505 # add as a number
1506 return i_tags_addn($self->{IMG}, $opts{code}, 0, $opts{value});
1507 }
1508 else {
1509 return i_tags_add($self->{IMG}, $opts{code}, 0, $opts{value}, 0);
1510 }
1511 }
1512 elsif (defined $opts{data}) {
1513 # force addition as a string
1514 return i_tags_add($self->{IMG}, $opts{code}, 0, $opts{data}, 0);
1515 }
1516 else {
1517 $self->{ERRSTR} = "No value supplied";
1518 return undef;
1519 }
1520 }
1521 else {
1522 return undef;
1523 }
1524 }
1525
1526
1527 sub _get_reader_io {
1528 my ($self, $input) = @_;
1529
1530 if ($input->{io}) {
1531 return $input->{io}, undef;
1532 }
1533 elsif ($input->{fd}) {
1534 return io_new_fd($input->{fd});
1535 }
1536 elsif ($input->{fh}) {
1537 unless (Scalar::Util::openhandle($input->{fh})) {
1538 $self->_set_error("Handle in fh option not opened");
1539 return;
1540 }
1541 return Imager::IO->new_fh($input->{fh});
1542 }
1543 elsif ($input->{file}) {
1544 my $file;
1545 unless (open $file, "<", $input->{file}) {
1546 $self->_set_error("Could not open $input->{file}: $!");
1547 return;
1548 }
1549 binmode $file;
1550 return (io_new_fd(fileno($file)), $file);
1551 }
1552 elsif ($input->{data}) {
1553 return io_new_buffer($input->{data});
1554 }
1555 elsif ($input->{callback} || $input->{readcb}) {
1556 if (!$input->{seekcb}) {
1557 $self->_set_error("Need a seekcb parameter");
1558 }
1559 if ($input->{maxbuffer}) {
1560 return io_new_cb($input->{writecb},
1561 $input->{callback} || $input->{readcb},
1562 $input->{seekcb}, $input->{closecb},
1563 $input->{maxbuffer});
1564 }
1565 else {
1566 return io_new_cb($input->{writecb},
1567 $input->{callback} || $input->{readcb},
1568 $input->{seekcb}, $input->{closecb});
1569 }
1570 }
1571 else {
1572 $self->_set_error("file/fd/fh/data/callback parameter missing");
1573 return;
1574 }
1575 }
1576
1577 sub _get_writer_io {
1578 my ($self, $input) = @_;
1579
1580 my $buffered = exists $input->{buffered} ? $input->{buffered} : 1;
1581
1582 my $io;
1583 my @extras;
1584 if ($input->{io}) {
1585 $io = $input->{io};
1586 }
1587 elsif ($input->{fd}) {
1588 $io = io_new_fd($input->{fd});
1589 }
1590 elsif ($input->{fh}) {
1591 unless (Scalar::Util::openhandle($input->{fh})) {
1592 $self->_set_error("Handle in fh option not opened");
1593 return;
1594 }
1595 $io = Imager::IO->new_fh($input->{fh});
1596 }
1597 elsif ($input->{file}) {
1598 my $fh;
1599 unless (open $fh, "+>", $input->{file}) {
1600 $self->_set_error("Could not open file $input->{file}: $!");
1601 return;
1602 }
1603 binmode($fh) or die;
1604 $io = io_new_fd(fileno($fh));
1605 push @extras, $fh;
1606 }
1607 elsif ($input->{data}) {
1608 $io = io_new_bufchain();
1609 }
1610 elsif ($input->{callback} || $input->{writecb}) {
1611 if ($input->{maxbuffer} && $input->{maxbuffer} == 1) {
1612 $buffered = 0;
1613 }
1614 $io = io_new_cb($input->{callback} || $input->{writecb},
1615 $input->{readcb},
1616 $input->{seekcb}, $input->{closecb});
1617 }
1618 else {
1619 $self->_set_error("file/fd/fh/data/callback parameter missing");
1620 return;
1621 }
1622
1623 unless ($buffered) {
1624 $io->set_buffered(0);
1625 }
1626
1627 return ($io, @extras);
1628 }
1629
1630 sub _test_format {
1631 my ($io) = @_;
1632
1633 return i_test_format_probe($io, -1);
1634 }
1635
1636 sub add_file_magic {
1637 my ($class, %opts) = @_;
1638
1639 my $name = delete $opts{name};
1640 my $bits = delete $opts{bits};
1641 my $mask = delete $opts{mask};
1642
1643 unless (i_add_file_magic($name, $bits, $mask)) {
1644 Imager->_set_error(Imager->_error_as_msg);
1645 return;
1646 }
1647
1648 1;
1649 }
1650
1651 # Read an image from file
1652
1653 sub read {
1654 my $self = shift;
1655 my %input=@_;
1656
1657 if (defined($self->{IMG})) {
1658 # let IIM_DESTROY do the destruction, since the image may be
1659 # referenced from elsewhere
1660 #i_img_destroy($self->{IMG});
1661 undef($self->{IMG});
1662 }
1663
1664 my ($IO, $fh) = $self->_get_reader_io(\%input) or return;
1665
1666 my $type = $input{'type'};
1667 unless ($type) {
1668 $type = _test_format($IO);
1669 }
1670
1671 if ($input{file} && !$type) {
1672 # guess the type
1673 $type = $FORMATGUESS->($input{file});
1674 }
1675
1676 unless ($type) {
1677 my $msg = "type parameter missing and it couldn't be determined from the file contents";
1678 $input{file} and $msg .= " or file name";
1679 $self->_set_error($msg);
1680 return undef;
1681 }
1682
1683 _reader_autoload($type);
1684
1685 if ($readers{$type} && $readers{$type}{single}) {
1686 return $readers{$type}{single}->($self, $IO, %input);
1687 }
1688
1689 unless ($formats_low{$type}) {
1690 my $read_types = join ', ', sort Imager->read_types();
1691 $self->_set_error("format '$type' not supported - formats $read_types available for reading - $reader_load_errors{$type}");
1692 return;
1693 }
1694
1695 my $allow_incomplete = $input{allow_incomplete};
1696 defined $allow_incomplete or $allow_incomplete = 0;
1697
1698 if ( $type eq 'pnm' ) {
1699 $self->{IMG}=i_readpnm_wiol( $IO, $allow_incomplete );
1700 if ( !defined($self->{IMG}) ) {
1701 $self->{ERRSTR}='unable to read pnm image: '._error_as_msg();
1702 return undef;
1703 }
1704 $self->{DEBUG} && print "loading a pnm file\n";
1705 return $self;
1706 }
1707
1708 if ( $type eq 'bmp' ) {
1709 $self->{IMG}=i_readbmp_wiol( $IO, $allow_incomplete );
1710 if ( !defined($self->{IMG}) ) {
1711 $self->{ERRSTR}=$self->_error_as_msg();
1712 return undef;
1713 }
1714 $self->{DEBUG} && print "loading a bmp file\n";
1715 }
1716
1717 if ( $type eq 'tga' ) {
1718 $self->{IMG}=i_readtga_wiol( $IO, -1 ); # Fixme, check if that length parameter is ever needed
1719 if ( !defined($self->{IMG}) ) {
1720 $self->{ERRSTR}=$self->_error_as_msg();
1721 return undef;
1722 }
1723 $self->{DEBUG} && print "loading a tga file\n";
1724 }
1725
1726 if ( $type eq 'raw' ) {
1727 unless ( $input{xsize} && $input{ysize} ) {
1728 $self->_set_error('missing xsize or ysize parameter for raw');
1729 return undef;
1730 }
1731
1732 my $interleave = _first($input{raw_interleave}, $input{interleave});
1733 unless (defined $interleave) {
1734 my @caller = caller;
1735 warn "read(type => 'raw') $caller[2] line $caller[1]: supply interleave or raw_interleave for future compatibility\n";
1736 $interleave = 1;
1737 }
1738 my $data_ch = _first($input{raw_datachannels}, $input{datachannels}, 3);
1739 my $store_ch = _first($input{raw_storechannels}, $input{storechannels}, 3);
1740
1741 $self->{IMG} = i_readraw_wiol( $IO,
1742 $input{xsize},
1743 $input{ysize},
1744 $data_ch,
1745 $store_ch,
1746 $interleave);
1747 if ( !defined($self->{IMG}) ) {
1748 $self->{ERRSTR}=$self->_error_as_msg();
1749 return undef;
1750 }
1751 $self->{DEBUG} && print "loading a raw file\n";
1752 }
1753
1754 return $self;
1755 }
1756
1757 sub register_reader {
1758 my ($class, %opts) = @_;
1759
1760 defined $opts{type}
1761 or die "register_reader called with no type parameter\n";
1762
1763 my $type = $opts{type};
1764
1765 defined $opts{single} || defined $opts{multiple}
1766 or die "register_reader called with no single or multiple parameter\n";
1767
1768 $readers{$type} = { };
1769 if ($opts{single}) {
1770 $readers{$type}{single} = $opts{single};
1771 }
1772 if ($opts{multiple}) {
1773 $readers{$type}{multiple} = $opts{multiple};
1774 }
1775
1776 return 1;
1777 }
1778
1779 sub register_writer {
1780 my ($class, %opts) = @_;
1781
1782 defined $opts{type}
1783 or die "register_writer called with no type parameter\n";
1784
1785 my $type = $opts{type};
1786
1787 defined $opts{single} || defined $opts{multiple}
1788 or die "register_writer called with no single or multiple parameter\n";
1789
1790 $writers{$type} = { };
1791 if ($opts{single}) {
1792 $writers{$type}{single} = $opts{single};
1793 }
1794 if ($opts{multiple}) {
1795 $writers{$type}{multiple} = $opts{multiple};
1796 }
1797
1798 return 1;
1799 }
1800
1801 sub read_types {
1802 my %types =
1803 (
1804 map { $_ => 1 }
1805 keys %readers,
1806 grep($file_formats{$_}, keys %formats),
1807 qw(ico sgi), # formats not handled directly, but supplied with Imager
1808 );
1809
1810 return keys %types;
1811 }
1812
1813 sub write_types {
1814 my %types =
1815 (
1816 map { $_ => 1 }
1817 keys %writers,
1818 grep($file_formats{$_}, keys %formats),
1819 qw(ico sgi), # formats not handled directly, but supplied with Imager
1820 );
1821
1822 return keys %types;
1823 }
1824
1825 sub _load_file {
1826 my ($file, $error) = @_;
1827
1828 if ($attempted_to_load{$file}) {
1829 if ($file_load_errors{$file}) {
1830 $$error = $file_load_errors{$file};
1831 return 0;
1832 }
1833 else {
1834 return 1;
1835 }
1836 }
1837 else {
1838 local $SIG{__DIE__};
1839 my $loaded = eval {
1840 local @INC = @INC;
1841 pop @INC if $INC[-1] eq '.';
1842 ++$attempted_to_load{$file};
1843 require $file;
1844 return 1;
1845 };
1846 if ($loaded) {
1847 return 1;
1848 }
1849 else {
1850 my $work = $@ || "Unknown error";
1851 chomp $work;
1852 $work =~ s/\n?Compilation failed in require at .*Imager\.pm line .*\z//m;
1853 $work =~ s/\n/\\n/g;
1854 $work =~ s/\s*\.?\z/ loading $file/;
1855 $file_load_errors{$file} = $work;
1856 $$error = $work;
1857 return 0;
1858 }
1859 }
1860 }
1861
1862 # probes for an Imager::File::whatever module
1863 sub _reader_autoload {
1864 my $type = shift;
1865
1866 return if $formats_low{$type} || $readers{$type};
1867
1868 return unless $type =~ /^\w+$/;
1869
1870 my $file = "Imager/File/\U$type\E.pm";
1871
1872 my $error;
1873 my $loaded = _load_file($file, \$error);
1874 if (!$loaded && $error =~ /^Can't locate /) {
1875 my $filer = "Imager/File/\U$type\EReader.pm";
1876 $loaded = _load_file($filer, \$error);
1877 if ($error =~ /^Can't locate /) {
1878 $error = "Can't locate $file or $filer";
1879 }
1880 }
1881 unless ($loaded) {
1882 $reader_load_errors{$type} = $error;
1883 }
1884 }
1885
1886 # probes for an Imager::File::whatever module
1887 sub _writer_autoload {
1888 my $type = shift;
1889
1890 return if $formats_low{$type} || $writers{$type};
1891
1892 return unless $type =~ /^\w+$/;
1893
1894 my $file = "Imager/File/\U$type\E.pm";
1895
1896 my $error;
1897 my $loaded = _load_file($file, \$error);
1898 if (!$loaded && $error =~ /^Can't locate /) {
1899 my $filew = "Imager/File/\U$type\EWriter.pm";
1900 $loaded = _load_file($filew, \$error);
1901 if ($error =~ /^Can't locate /) {
1902 $error = "Can't locate $file or $filew";
1903 }
1904 }
1905 unless ($loaded) {
1906 $writer_load_errors{$type} = $error;
1907 }
1908 }
1909
1910 sub _fix_gif_positions {
1911 my ($opts, $opt, $msg, @imgs) = @_;
1912
1913 my $positions = $opts->{'gif_positions'};
1914 my $index = 0;
1915 for my $pos (@$positions) {
1916 my ($x, $y) = @$pos;
1917 my $img = $imgs[$index++];
1918 $img->settag(name=>'gif_left', value=>$x);
1919 $img->settag(name=>'gif_top', value=>$y) if defined $y;
1920 }
1921 $$msg .= "replaced with the gif_left and gif_top tags";
1922 }
1923
1924 my %obsolete_opts =
1925 (
1926 gif_each_palette=>'gif_local_map',
1927 interlace => 'gif_interlace',
1928 gif_delays => 'gif_delay',
1929 gif_positions => \&_fix_gif_positions,
1930 gif_loop_count => 'gif_loop',
1931 );
1932
1933 # options that should be converted to colors
1934 my %color_opts = map { $_ => 1 } qw/i_background/;
1935
1936 sub _set_opts {
1937 my ($self, $opts, $prefix, @imgs) = @_;
1938
1939 for my $opt (keys %$opts) {
1940 my $tagname = $opt;
1941 if ($obsolete_opts{$opt}) {
1942 my $new = $obsolete_opts{$opt};
1943 my $msg = "Obsolete option $opt ";
1944 if (ref $new) {
1945 $new->($opts, $opt, \$msg, @imgs);
1946 }
1947 else {
1948 $msg .= "replaced with the $new tag ";
1949 $tagname = $new;
1950 }
1951 $msg .= "line ".(caller(2))[2]." of file ".(caller(2))[1];
1952 warn $msg if $warn_obsolete && $^W;
1953 }
1954 next unless $tagname =~ /^\Q$prefix/;
1955 my $value = $opts->{$opt};
1956 if ($color_opts{$opt}) {
1957 $value = _color($value);
1958 unless ($value) {
1959 $self->_set_error($Imager::ERRSTR);
1960 return;
1961 }
1962 }
1963 if (ref $value) {
1964 if (UNIVERSAL::isa($value, "Imager::Color")) {
1965 my $tag = sprintf("color(%d,%d,%d,%d)", $value->rgba);
1966 for my $img (@imgs) {
1967 $img->settag(name=>$tagname, value=>$tag);
1968 }
1969 }
1970 elsif (ref($value) eq 'ARRAY') {
1971 for my $i (0..$#$value) {
1972 my $val = $value->[$i];
1973 if (ref $val) {
1974 if (UNIVERSAL::isa($val, "Imager::Color")) {
1975 my $tag = sprintf("color(%d,%d,%d,%d)", $value->rgba);
1976 $i < @imgs and
1977 $imgs[$i]->settag(name=>$tagname, value=>$tag);
1978 }
1979 else {
1980 $self->_set_error("Unknown reference type " . ref($value) .
1981 " supplied in array for $opt");
1982 return;
1983 }
1984 }
1985 else {
1986 $i < @imgs
1987 and $imgs[$i]->settag(name=>$tagname, value=>$val);
1988 }
1989 }
1990 }
1991 else {
1992 $self->_set_error("Unknown reference type " . ref($value) .
1993 " supplied for $opt");
1994 return;
1995 }
1996 }
1997 else {
1998 # set it as a tag for every image
1999 for my $img (@imgs) {
2000 $img->settag(name=>$tagname, value=>$value);
2001 }
2002 }
2003 }
2004
2005 return 1;
2006 }
2007
2008 # Write an image to file
2009 sub write {
2010 my $self = shift;
2011 my %input=(jpegquality=>75,
2012 gifquant=>'mc',
2013 lmdither=>6.0,
2014 lmfixed=>[],
2015 idstring=>"",
2016 compress=>1,
2017 wierdpack=>0,
2018 fax_fine=>1, @_);
2019 my $rc;
2020
2021 $self->_valid_image("write")
2022 or return;
2023
2024 $self->_set_opts(\%input, "i_", $self)
2025 or return undef;
2026
2027 my $type = $input{'type'};
2028 if (!$type and $input{file}) {
2029 $type = $FORMATGUESS->($input{file});
2030 }
2031 unless ($type) {
2032 $self->{ERRSTR}='type parameter missing and not possible to guess from extension';
2033 return undef;
2034 }
2035
2036 _writer_autoload($type);
2037
2038 my ($IO, $fh);
2039 if ($writers{$type} && $writers{$type}{single}) {
2040 ($IO, $fh) = $self->_get_writer_io(\%input)
2041 or return undef;
2042
2043 $writers{$type}{single}->($self, $IO, %input, type => $type)
2044 or return undef;
2045 }
2046 else {
2047 if (!$formats_low{$type}) {
2048 my $write_types = join ', ', sort Imager->write_types();
2049 $self->_set_error("format '$type' not supported - formats $write_types available for writing - $writer_load_errors{$type}");
2050 return undef;
2051 }
2052
2053 ($IO, $fh) = $self->_get_writer_io(\%input, $type)
2054 or return undef;
2055
2056 if ( $type eq 'pnm' ) {
2057 $self->_set_opts(\%input, "pnm_", $self)
2058 or return undef;
2059 if ( ! i_writeppm_wiol($self->{IMG},$IO) ) {
2060 $self->{ERRSTR} = $self->_error_as_msg();
2061 return undef;
2062 }
2063 $self->{DEBUG} && print "writing a pnm file\n";
2064 }
2065 elsif ( $type eq 'raw' ) {
2066 $self->_set_opts(\%input, "raw_", $self)
2067 or return undef;
2068 if ( !i_writeraw_wiol($self->{IMG},$IO) ) {
2069 $self->{ERRSTR} = $self->_error_as_msg();
2070 return undef;
2071 }
2072 $self->{DEBUG} && print "writing a raw file\n";
2073 }
2074 elsif ( $type eq 'bmp' ) {
2075 $self->_set_opts(\%input, "bmp_", $self)
2076 or return undef;
2077 if ( !i_writebmp_wiol($self->{IMG}, $IO) ) {
2078 $self->{ERRSTR} = $self->_error_as_msg;
2079 return undef;
2080 }
2081 $self->{DEBUG} && print "writing a bmp file\n";
2082 }
2083 elsif ( $type eq 'tga' ) {
2084 $self->_set_opts(\%input, "tga_", $self)
2085 or return undef;
2086
2087 if ( !i_writetga_wiol($self->{IMG}, $IO, $input{wierdpack}, $input{compress}, $input{idstring}) ) {
2088 $self->{ERRSTR}=$self->_error_as_msg();
2089 return undef;
2090 }
2091 $self->{DEBUG} && print "writing a tga file\n";
2092 }
2093 }
2094
2095 if (exists $input{'data'}) {
2096 my $data = io_slurp($IO);
2097 if (!$data) {
2098 $self->{ERRSTR}='Could not slurp from buffer';
2099 return undef;
2100 }
2101 ${$input{data}} = $data;
2102 }
2103 return $self;
2104 }
2105
2106 sub write_multi {
2107 my ($class, $opts, @images) = @_;
2108
2109 my $type = $opts->{type};
2110
2111 if (!$type && $opts->{'file'}) {
2112 $type = $FORMATGUESS->($opts->{'file'});
2113 }
2114 unless ($type) {
2115 $class->_set_error('type parameter missing and not possible to guess from extension');
2116 return;
2117 }
2118 # translate to ImgRaw
2119 my $index = 1;
2120 for my $img (@images) {
2121 unless (ref $img && Scalar::Util::blessed($img) && $img->isa("Imager")) {
2122 $class->_set_error("write_multi: image $index is not an Imager image object");
2123 return;
2124 }
2125 unless ($img->_valid_image("write_multi")) {
2126 $class->_set_error($img->errstr . " (image $index)");
2127 return;
2128 }
2129 ++$index;
2130 }
2131 $class->_set_opts($opts, "i_", @images)
2132 or return;
2133 my @work = map $_->{IMG}, @images;
2134
2135 _writer_autoload($type);
2136
2137 my ($IO, $file);
2138 if ($writers{$type} && $writers{$type}{multiple}) {
2139 ($IO, $file) = $class->_get_writer_io($opts, $type)
2140 or return undef;
2141
2142 $writers{$type}{multiple}->($class, $IO, $opts, @images)
2143 or return undef;
2144 }
2145 else {
2146 if (!$formats{$type}) {
2147 my $write_types = join ', ', sort Imager->write_types();
2148 $class->_set_error("format '$type' not supported - formats $write_types available for writing");
2149 return undef;
2150 }
2151
2152 ($IO, $file) = $class->_get_writer_io($opts, $type)
2153 or return undef;
2154
2155 if (0) { # eventually PNM in here, now that TIFF/GIF are elsewhere
2156 }
2157 else {
2158 if (@images == 1) {
2159 unless ($images[0]->write(%$opts, io => $IO, type => $type)) {
2160 return 1;
2161 }
2162 }
2163 else {
2164 $ERRSTR = "Sorry, write_multi doesn't support $type yet";
2165 return 0;
2166 }
2167 }
2168 }
2169
2170 if (exists $opts->{'data'}) {
2171 my $data = io_slurp($IO);
2172 if (!$data) {
2173 Imager->_set_error('Could not slurp from buffer');
2174 return undef;
2175 }
2176 ${$opts->{data}} = $data;
2177 }
2178 return 1;
2179 }
2180
2181 # read multiple images from a file
2182 sub read_multi {
2183 my ($class, %opts) = @_;
2184
2185 my ($IO, $file) = $class->_get_reader_io(\%opts, $opts{'type'})
2186 or return;
2187
2188 my $type = $opts{'type'};
2189 unless ($type) {
2190 $type = _test_format($IO);
2191 }
2192
2193 if ($opts{file} && !$type) {
2194 # guess the type
2195 $type = $FORMATGUESS->($opts{file});
2196 }
2197
2198 unless ($type) {
2199 my $msg = "type parameter missing and it couldn't be determined from the file contents";
2200 $opts{file} and $msg .= " or file name";
2201 Imager->_set_error($msg);
2202 return;
2203 }
2204
2205 _reader_autoload($type);
2206
2207 if ($readers{$type} && $readers{$type}{multiple}) {
2208 return $readers{$type}{multiple}->($IO, %opts);
2209 }
2210
2211 unless ($formats{$type}) {
2212 my $read_types = join ', ', sort Imager->read_types();
2213 Imager->_set_error("format '$type' not supported - formats $read_types available for reading");
2214 return;
2215 }
2216
2217 my @imgs;
2218 if ($type eq 'pnm') {
2219 @imgs = i_readpnm_multi_wiol($IO, $opts{allow_incomplete}||0);
2220 }
2221 else {
2222 my $img = Imager->new;
2223 if ($img->read(%opts, io => $IO, type => $type)) {
2224 return ( $img );
2225 }
2226 Imager->_set_error($img->errstr);
2227 return;
2228 }
2229
2230 if (!@imgs) {
2231 $ERRSTR = _error_as_msg();
2232 return;
2233 }
2234 return map {
2235 bless { IMG=>$_, DEBUG=>$DEBUG, ERRSTR=>undef }, 'Imager'
2236 } @imgs;
2237 }
2238
2239 # Destroy an Imager object
2240
2241 sub DESTROY {
2242 my $self=shift;
2243 # delete $instances{$self};
2244 if (defined($self->{IMG})) {
2245 # the following is now handled by the XS DESTROY method for
2246 # Imager::ImgRaw object
2247 # Re-enabling this will break virtual images
2248 # tested for in t/t020masked.t
2249 # i_img_destroy($self->{IMG});
2250 undef($self->{IMG});
2251 } else {
2252 # print "Destroy Called on an empty image!\n"; # why did I put this here??
2253 }
2254 }
2255
2256 # Perform an inplace filter of an image
2257 # that is the image will be overwritten with the data
2258
2259 sub filter {
2260 my $self=shift;
2261 my %input=@_;
2262 my %hsh;
2263
2264 $self->_valid_image("filter")
2265 or return;
2266
2267 if (!$input{'type'}) { $self->{ERRSTR}='type parameter missing'; return undef; }
2268
2269 if (!exists $filters{$input{'type'}}) {
2270 $self->{ERRSTR}='type parameter not matching any filter'; return undef;
2271 }
2272
2273 if ($filters{$input{'type'}}{names}) {
2274 my $names = $filters{$input{'type'}}{names};
2275 for my $name (keys %$names) {
2276 if (defined $input{$name} && exists $names->{$name}{$input{$name}}) {
2277 $input{$name} = $names->{$name}{$input{$name}};
2278 }
2279 }
2280 }
2281 if (defined($filters{$input{'type'}}{defaults})) {
2282 %hsh=( image => $self->{IMG},
2283 imager => $self,
2284 %{$filters{$input{'type'}}{defaults}},
2285 %input );
2286 } else {
2287 %hsh=( image => $self->{IMG},
2288 imager => $self,
2289 %input );
2290 }
2291
2292 my @cs=@{$filters{$input{'type'}}{callseq}};
2293
2294 for(@cs) {
2295 if (!defined($hsh{$_})) {
2296 $self->{ERRSTR}="missing parameter '$_' for filter ".$input{'type'}; return undef;
2297 }
2298 }
2299
2300 eval {
2301 local $SIG{__DIE__}; # we don't want this processed by confess, etc
2302 &{$filters{$input{'type'}}{callsub}}(%hsh);
2303 };
2304 if ($@) {
2305 chomp($self->{ERRSTR} = $@);
2306 return;
2307 }
2308
2309 my @b=keys %hsh;
2310
2311 $self->{DEBUG} && print "callseq is: @cs\n";
2312 $self->{DEBUG} && print "matching callseq is: @b\n";
2313
2314 return $self;
2315 }
2316
2317 sub register_filter {
2318 my $class = shift;
2319 my %hsh = ( defaults => {}, @_ );
2320
2321 defined $hsh{type}
2322 or die "register_filter() with no type\n";
2323 defined $hsh{callsub}
2324 or die "register_filter() with no callsub\n";
2325 defined $hsh{callseq}
2326 or die "register_filter() with no callseq\n";
2327
2328 exists $filters{$hsh{type}}
2329 and return;
2330
2331 $filters{$hsh{type}} = \%hsh;
2332
2333 return 1;
2334 }
2335
2336 sub scale_calculate {
2337 my $self = shift;
2338
2339 my %opts = ('type'=>'max', @_);
2340
2341 # none of these should be references
2342 for my $name (qw/xpixels ypixels xscalefactor yscalefactor width height/) {
2343 if (defined $opts{$name} && ref $opts{$name}) {
2344 $self->_set_error("scale_calculate: $name parameter cannot be a reference");
2345 return;
2346 }
2347 }
2348
2349 my ($x_scale, $y_scale);
2350 my $width = $opts{width};
2351 my $height = $opts{height};
2352 if (ref $self) {
2353 defined $width or $width = $self->getwidth;
2354 defined $height or $height = $self->getheight;
2355 }
2356 else {
2357 unless (defined $width && defined $height) {
2358 $self->_set_error("scale_calculate: width and height parameters must be supplied when called as a class method");
2359 return;
2360 }
2361 }
2362
2363 if ($opts{'xscalefactor'} && $opts{'yscalefactor'}) {
2364 $x_scale = $opts{'xscalefactor'};
2365 $y_scale = $opts{'yscalefactor'};
2366 }
2367 elsif ($opts{'xscalefactor'}) {
2368 $x_scale = $opts{'xscalefactor'};
2369 $y_scale = $opts{'scalefactor'} || $x_scale;
2370 }
2371 elsif ($opts{'yscalefactor'}) {
2372 $y_scale = $opts{'yscalefactor'};
2373 $x_scale = $opts{'scalefactor'} || $y_scale;
2374 }
2375 else {
2376 $x_scale = $y_scale = $opts{'scalefactor'} || 0.5;
2377 }
2378
2379 # work out the scaling
2380 if ($opts{xpixels} and $opts{ypixels} and $opts{'type'}) {
2381 my ($xpix, $ypix)=( $opts{xpixels} / $width ,
2382 $opts{ypixels} / $height );
2383 if ($opts{'type'} eq 'min') {
2384 $x_scale = $y_scale = _min($xpix,$ypix);
2385 }
2386 elsif ($opts{'type'} eq 'max') {
2387 $x_scale = $y_scale = _max($xpix,$ypix);
2388 }
2389 elsif ($opts{'type'} eq 'nonprop' || $opts{'type'} eq 'non-proportional') {
2390 $x_scale = $xpix;
2391 $y_scale = $ypix;
2392 }
2393 else {
2394 $self->_set_error('invalid value for type parameter');
2395 return;
2396 }
2397 } elsif ($opts{xpixels}) {
2398 $x_scale = $y_scale = $opts{xpixels} / $width;
2399 }
2400 elsif ($opts{ypixels}) {
2401 $x_scale = $y_scale = $opts{ypixels}/$height;
2402 }
2403 elsif ($opts{constrain} && ref $opts{constrain}
2404 && $opts{constrain}->can('constrain')) {
2405 # we've been passed an Image::Math::Constrain object or something
2406 # that looks like one
2407 my $scalefactor;
2408 (undef, undef, $scalefactor)
2409 = $opts{constrain}->constrain($self->getwidth, $self->getheight);
2410 unless ($scalefactor) {
2411 $self->_set_error('constrain method failed on constrain parameter');
2412 return;
2413 }
2414 $x_scale = $y_scale = $scalefactor;
2415 }
2416
2417 my $new_width = int($x_scale * $width + 0.5);
2418 $new_width > 0 or $new_width = 1;
2419 my $new_height = int($y_scale * $height + 0.5);
2420 $new_height > 0 or $new_height = 1;
2421
2422 return ($x_scale, $y_scale, $new_width, $new_height);
2423
2424 }
2425
2426 # Scale an image to requested size and return the scaled version
2427
2428 sub scale {
2429 my $self=shift;
2430 my %opts = (qtype=>'normal' ,@_);
2431 my $img = Imager->new();
2432 my $tmp = Imager->new();
2433
2434 unless (defined wantarray) {
2435 my @caller = caller;
2436 warn "scale() called in void context - scale() returns the scaled image at $caller[1] line $caller[2]\n";
2437 return;
2438 }
2439
2440 $self->_valid_image("scale")
2441 or return;
2442
2443 my ($x_scale, $y_scale, $new_width, $new_height) =
2444 $self->scale_calculate(%opts)
2445 or return;
2446
2447 if ($opts{qtype} eq 'normal') {
2448 $tmp->{IMG} = i_scaleaxis($self->{IMG}, $x_scale, 0);
2449 if ( !defined($tmp->{IMG}) ) {
2450 $self->{ERRSTR} = 'unable to scale image: ' . $self->_error_as_msg;
2451 return undef;
2452 }
2453 $img->{IMG}=i_scaleaxis($tmp->{IMG}, $y_scale, 1);
2454 if ( !defined($img->{IMG}) ) {
2455 $self->{ERRSTR}='unable to scale image: ' . $self->_error_as_msg;
2456 return undef;
2457 }
2458
2459 return $img;
2460 }
2461 elsif ($opts{'qtype'} eq 'preview') {
2462 $img->{IMG} = i_scale_nn($self->{IMG}, $x_scale, $y_scale);
2463 if ( !defined($img->{IMG}) ) {
2464 $self->{ERRSTR}='unable to scale image';
2465 return undef;
2466 }
2467 return $img;
2468 }
2469 elsif ($opts{'qtype'} eq 'mixing') {
2470 $img->{IMG} = i_scale_mixing($self->{IMG}, $new_width, $new_height);
2471 unless ($img->{IMG}) {
2472 $self->_set_error(Imager->_error_as_msg);
2473 return;
2474 }
2475 return $img;
2476 }
2477 else {
2478 $self->_set_error('invalid value for qtype parameter');
2479 return undef;
2480 }
2481 }
2482
2483 # Scales only along the X axis
2484
2485 sub scaleX {
2486 my $self = shift;
2487 my %opts = ( scalefactor=>0.5, @_ );
2488
2489 unless (defined wantarray) {
2490 my @caller = caller;
2491 warn "scaleX() called in void context - scaleX() returns the scaled image at $caller[1] line $caller[2]\n";
2492 return;
2493 }
2494
2495 $self->_valid_image("scaleX")
2496 or return;
2497
2498 my $img = Imager->new();
2499
2500 my $scalefactor = $opts{scalefactor};
2501
2502 if ($opts{pixels}) {
2503 $scalefactor = $opts{pixels} / $self->getwidth();
2504 }
2505
2506 unless ($self->{IMG}) {
2507 $self->{ERRSTR}='empty input image';
2508 return undef;
2509 }
2510
2511 $img->{IMG} = i_scaleaxis($self->{IMG}, $scalefactor, 0);
2512
2513 if ( !defined($img->{IMG}) ) {
2514 $self->{ERRSTR} = 'unable to scale image';
2515 return undef;
2516 }
2517
2518 return $img;
2519 }
2520
2521 # Scales only along the Y axis
2522
2523 sub scaleY {
2524 my $self = shift;
2525 my %opts = ( scalefactor => 0.5, @_ );
2526
2527 unless (defined wantarray) {
2528 my @caller = caller;
2529 warn "scaleY() called in void context - scaleY() returns the scaled image at $caller[1] line $caller[2]\n";
2530 return;
2531 }
2532
2533 $self->_valid_image("scaleY")
2534 or return;
2535
2536 my $img = Imager->new();
2537
2538 my $scalefactor = $opts{scalefactor};
2539
2540 if ($opts{pixels}) {
2541 $scalefactor = $opts{pixels} / $self->getheight();
2542 }
2543
2544 unless ($self->{IMG}) {
2545 $self->{ERRSTR} = 'empty input image';
2546 return undef;
2547 }
2548 $img->{IMG}=i_scaleaxis($self->{IMG}, $scalefactor, 1);
2549
2550 if ( !defined($img->{IMG}) ) {
2551 $self->{ERRSTR} = 'unable to scale image';
2552 return undef;
2553 }
2554
2555 return $img;
2556 }
2557
2558 # Transform returns a spatial transformation of the input image
2559 # this moves pixels to a new location in the returned image.
2560 # NOTE - should make a utility function to check transforms for
2561 # stack overruns
2562
2563 our $I2P;
2564
2565 sub transform {
2566 my $self=shift;
2567 my %opts=@_;
2568 my (@op,@ropx,@ropy,$iop,$or,@parm,$expr,@xt,@yt,@pt,$numre);
2569
2570 # print Dumper(\%opts);
2571 # xopcopdes
2572
2573 $self->_valid_image("transform")
2574 or return;
2575
2576 if ( $opts{'xexpr'} and $opts{'yexpr'} ) {
2577 if (!$I2P) {
2578 {
2579 local @INC = @INC;
2580 pop @INC if $INC[-1] eq '.';
2581 eval ("use Affix::Infix2Postfix;");
2582 }
2583
2584 if ( $@ ) {
2585 $self->{ERRSTR}='transform: expr given and Affix::Infix2Postfix is not avaliable.';
2586 return undef;
2587 }
2588 $I2P=Affix::Infix2Postfix->new('ops'=>[{op=>'+',trans=>'Add'},
2589 {op=>'-',trans=>'Sub'},
2590 {op=>'*',trans=>'Mult'},
2591 {op=>'/',trans=>'Div'},
2592 {op=>'-','type'=>'unary',trans=>'u-'},
2593 {op=>'**'},
2594 {op=>'func','type'=>'unary'}],
2595 'grouping'=>[qw( \( \) )],
2596 'func'=>[qw( sin cos )],
2597 'vars'=>[qw( x y )]
2598 );
2599 }
2600
2601 @xt=$I2P->translate($opts{'xexpr'});
2602 @yt=$I2P->translate($opts{'yexpr'});
2603
2604 $numre=$I2P->{'numre'};
2605 @pt=(0,0);
2606
2607 for(@xt) { if (/$numre/) { push(@pt,$_); push(@{$opts{'xopcodes'}},'Parm',$#pt); } else { push(@{$opts{'xopcodes'}},$_); } }
2608 for(@yt) { if (/$numre/) { push(@pt,$_); push(@{$opts{'yopcodes'}},'Parm',$#pt); } else { push(@{$opts{'yopcodes'}},$_); } }
2609 @{$opts{'parm'}}=@pt;
2610 }
2611
2612 # print Dumper(\%opts);
2613
2614 if ( !exists $opts{'xopcodes'} or @{$opts{'xopcodes'}}==0) {
2615 $self->{ERRSTR}='transform: no xopcodes given.';
2616 return undef;
2617 }
2618
2619 @op=@{$opts{'xopcodes'}};
2620 for $iop (@op) {
2621 if (!defined ($OPCODES{$iop}) and ($iop !~ /^\d+$/) ) {
2622 $self->{ERRSTR}="transform: illegal opcode '$_'.";
2623 return undef;
2624 }
2625 push(@ropx,(exists $OPCODES{$iop}) ? @{$OPCODES{$iop}} : $iop );
2626 }
2627
2628
2629 # yopcopdes
2630
2631 if ( !exists $opts{'yopcodes'} or @{$opts{'yopcodes'}}==0) {
2632 $self->{ERRSTR}='transform: no yopcodes given.';
2633 return undef;
2634 }
2635
2636 @op=@{$opts{'yopcodes'}};
2637 for $iop (@op) {
2638 if (!defined ($OPCODES{$iop}) and ($iop !~ /^\d+$/) ) {
2639 $self->{ERRSTR}="transform: illegal opcode '$_'.";
2640 return undef;
2641 }
2642 push(@ropy,(exists $OPCODES{$iop}) ? @{$OPCODES{$iop}} : $iop );
2643 }
2644
2645 #parameters
2646
2647 if ( !exists $opts{'parm'}) {
2648 $self->{ERRSTR}='transform: no parameter arg given.';
2649 return undef;
2650 }
2651
2652 # print Dumper(\@ropx);
2653 # print Dumper(\@ropy);
2654 # print Dumper(\@ropy);
2655
2656 my $img = Imager->new();
2657 $img->{IMG}=i_transform($self->{IMG},\@ropx,\@ropy,$opts{'parm'});
2658 if ( !defined($img->{IMG}) ) { $self->{ERRSTR}='transform: failed'; return undef; }
2659 return $img;
2660 }
2661
2662
2663 sub transform2 {
2664 my ($opts, @imgs) = @_;
2665
2666 require "Imager/Expr.pm";
2667
2668 $opts->{variables} = [ qw(x y) ];
2669 my ($width, $height) = @{$opts}{qw(width height)};
2670 if (@imgs) {
2671 my $index = 1;
2672 for my $img (@imgs) {
2673 unless ($img->_valid_image("transform2")) {
2674 Imager->_set_error($img->errstr . " (input image $index)");
2675 return;
2676 }
2677 ++$index;
2678 }
2679
2680 $width ||= $imgs[0]->getwidth();
2681 $height ||= $imgs[0]->getheight();
2682 my $img_num = 1;
2683 for my $img (@imgs) {
2684 $opts->{constants}{"w$img_num"} = $img->getwidth();
2685 $opts->{constants}{"h$img_num"} = $img->getheight();
2686 $opts->{constants}{"cx$img_num"} = $img->getwidth()/2;
2687 $opts->{constants}{"cy$img_num"} = $img->getheight()/2;
2688 ++$img_num;
2689 }
2690 }
2691 if ($width) {
2692 $opts->{constants}{w} = $width;
2693 $opts->{constants}{cx} = $width/2;
2694 }
2695 else {
2696 $Imager::ERRSTR = "No width supplied";
2697 return;
2698 }
2699 if ($height) {
2700 $opts->{constants}{h} = $height;
2701 $opts->{constants}{cy} = $height/2;
2702 }
2703 else {
2704 $Imager::ERRSTR = "No height supplied";
2705 return;
2706 }
2707 my $code = Imager::Expr->new($opts);
2708 if (!$code) {
2709 $Imager::ERRSTR = Imager::Expr::error();
2710 return;
2711 }
2712 my $channels = $opts->{channels} || 3;
2713 unless ($channels >= 1 && $channels <= 4) {
2714 return Imager->_set_error("channels must be an integer between 1 and 4");
2715 }
2716
2717 my $img = Imager->new();
2718 $img->{IMG} = i_transform2($opts->{width}, $opts->{height},
2719 $channels, $code->code(),
2720 $code->nregs(), $code->cregs(),
2721 [ map { $_->{IMG} } @imgs ]);
2722 if (!defined $img->{IMG}) {
2723 $Imager::ERRSTR = Imager->_error_as_msg();
2724 return;
2725 }
2726
2727 return $img;
2728 }
2729
2730 sub rubthrough {
2731 my $self=shift;
2732 my %opts= @_;
2733
2734 $self->_valid_image("rubthrough")
2735 or return;
2736
2737 unless ($opts{src} && $opts{src}->_valid_image("rubthrough")) {
2738 $self->{ERRSTR} = $opts{src}{ERRSTR} . ' (for src)';
2739 return;
2740 }
2741
2742 %opts = (src_minx => 0,
2743 src_miny => 0,
2744 src_maxx => $opts{src}->getwidth(),
2745 src_maxy => $opts{src}->getheight(),
2746 %opts);
2747
2748 my $tx = $opts{tx};
2749 defined $tx or $tx = $opts{left};
2750 defined $tx or $tx = 0;
2751
2752 my $ty = $opts{ty};
2753 defined $ty or $ty = $opts{top};
2754 defined $ty or $ty = 0;
2755
2756 unless (i_rubthru($self->{IMG}, $opts{src}->{IMG}, $tx, $ty,
2757 $opts{src_minx}, $opts{src_miny},
2758 $opts{src_maxx}, $opts{src_maxy})) {
2759 $self->_set_error($self->_error_as_msg());
2760 return undef;
2761 }
2762
2763 return $self;
2764 }
2765
2766 sub compose {
2767 my $self = shift;
2768 my %opts =
2769 (
2770 opacity => 1.0,
2771 mask_left => 0,
2772 mask_top => 0,
2773 @_
2774 );
2775
2776 $self->_valid_image("compose")
2777 or return;
2778
2779 unless ($opts{src}) {
2780 $self->_set_error("compose: src parameter missing");
2781 return;
2782 }
2783
2784 unless ($opts{src}->_valid_image("compose")) {
2785 $self->_set_error($opts{src}->errstr . " (for src)");
2786 return;
2787 }
2788 my $src = $opts{src};
2789
2790 my $left = $opts{left};
2791 defined $left or $left = $opts{tx};
2792 defined $left or $left = 0;
2793
2794 my $top = $opts{top};
2795 defined $top or $top = $opts{ty};
2796 defined $top or $top = 0;
2797
2798 my $src_left = $opts{src_left};
2799 defined $src_left or $src_left = $opts{src_minx};
2800 defined $src_left or $src_left = 0;
2801
2802 my $src_top = $opts{src_top};
2803 defined $src_top or $src_top = $opts{src_miny};
2804 defined $src_top or $src_top = 0;
2805
2806 my $width = $opts{width};
2807 if (!defined $width && defined $opts{src_maxx}) {
2808 $width = $opts{src_maxx} - $src_left;
2809 }
2810 defined $width or $width = $src->getwidth() - $src_left;
2811
2812 my $height = $opts{height};
2813 if (!defined $height && defined $opts{src_maxy}) {
2814 $height = $opts{src_maxy} - $src_top;
2815 }
2816 defined $height or $height = $src->getheight() - $src_top;
2817
2818 my $combine = $self->_combine($opts{combine}, 'normal');
2819
2820 if ($opts{mask}) {
2821 unless ($opts{mask}->_valid_image("compose")) {
2822 $self->_set_error($opts{mask}->errstr . " (for mask)");
2823 return;
2824 }
2825
2826 my $mask_left = $opts{mask_left};
2827 defined $mask_left or $mask_left = $opts{mask_minx};
2828 defined $mask_left or $mask_left = 0;
2829
2830 my $mask_top = $opts{mask_top};
2831 defined $mask_top or $mask_top = $opts{mask_miny};
2832 defined $mask_top or $mask_top = 0;
2833
2834 unless (i_compose_mask($self->{IMG}, $src->{IMG}, $opts{mask}{IMG},
2835 $left, $top, $src_left, $src_top,
2836 $mask_left, $mask_top, $width, $height,
2837 $combine, $opts{opacity})) {
2838 $self->_set_error(Imager->_error_as_msg);
2839 return;
2840 }
2841 }
2842 else {
2843 unless (i_compose($self->{IMG}, $src->{IMG}, $left, $top, $src_left, $src_top,
2844 $width, $height, $combine, $opts{opacity})) {
2845 $self->_set_error(Imager->_error_as_msg);
2846 return;
2847 }
2848 }
2849
2850 return $self;
2851 }
2852
2853 sub flip {
2854 my $self = shift;
2855 my %opts = @_;
2856
2857 $self->_valid_image("flip")
2858 or return;
2859
2860 my %xlate = (h=>0, v=>1, hv=>2, vh=>2);
2861 my $dir;
2862 return () unless defined $opts{'dir'} and defined $xlate{$opts{'dir'}};
2863 $dir = $xlate{$opts{'dir'}};
2864 return $self if i_flipxy($self->{IMG}, $dir);
2865 return ();
2866 }
2867
2868 sub rotate {
2869 my $self = shift;
2870 my %opts = @_;
2871
2872 unless (defined wantarray) {
2873 my @caller = caller;
2874 warn "rotate() called in void context - rotate() returns the rotated image at $caller[1] line $caller[2]\n";
2875 return;
2876 }
2877
2878 $self->_valid_image("rotate")
2879 or return;
2880
2881 if (defined $opts{right}) {
2882 my $degrees = $opts{right};
2883 if ($degrees < 0) {
2884 $degrees += 360 * int(((-$degrees)+360)/360);
2885 }
2886 $degrees = $degrees % 360;
2887 if ($degrees == 0) {
2888 return $self->copy();
2889 }
2890 elsif ($degrees == 90 || $degrees == 180 || $degrees == 270) {
2891 my $result = Imager->new();
2892 if ($result->{IMG} = i_rotate90($self->{IMG}, $degrees)) {
2893 return $result;
2894 }
2895 else {
2896 $self->{ERRSTR} = $self->_error_as_msg();
2897 return undef;
2898 }
2899 }
2900 else {
2901 $self->{ERRSTR} = "Parameter 'right' must be a multiple of 90 degrees";
2902 return undef;
2903 }
2904 }
2905 elsif (defined $opts{radians} || defined $opts{degrees}) {
2906 my $amount = $opts{radians} || $opts{degrees} * 3.14159265358979 / 180;
2907
2908 my $back = $opts{back};
2909 my $result = Imager->new;
2910 if ($back) {
2911 $back = _color($back);
2912 unless ($back) {
2913 $self->_set_error(Imager->errstr);
2914 return undef;
2915 }
2916
2917 $result->{IMG} = i_rotate_exact($self->{IMG}, $amount, $back);
2918 }
2919 else {
2920 $result->{IMG} = i_rotate_exact($self->{IMG}, $amount);
2921 }
2922 if ($result->{IMG}) {
2923 return $result;
2924 }
2925 else {
2926 $self->{ERRSTR} = $self->_error_as_msg();
2927 return undef;
2928 }
2929 }
2930 else {
2931 $self->{ERRSTR} = "Only the 'right', 'radians' and 'degrees' parameters are available";
2932 return undef;
2933 }
2934 }
2935
2936 sub matrix_transform {
2937 my $self = shift;
2938 my %opts = @_;
2939
2940 $self->_valid_image("matrix_transform")
2941 or return;
2942
2943 unless (defined wantarray) {
2944 my @caller = caller;
2945 warn "copy() called in void context - copy() returns the copied image at $caller[1] line $caller[2]\n";
2946 return;
2947 }
2948
2949 if ($opts{matrix}) {
2950 my $xsize = $opts{xsize} || $self->getwidth;
2951 my $ysize = $opts{ysize} || $self->getheight;
2952
2953 my $result = Imager->new;
2954 if ($opts{back}) {
2955 $result->{IMG} = i_matrix_transform($self->{IMG}, $xsize, $ysize,
2956 $opts{matrix}, $opts{back})
2957 or return undef;
2958 }
2959 else {
2960 $result->{IMG} = i_matrix_transform($self->{IMG}, $xsize, $ysize,
2961 $opts{matrix})
2962 or return undef;
2963 }
2964
2965 return $result;
2966 }
2967 else {
2968 $self->{ERRSTR} = "matrix parameter required";
2969 return undef;
2970 }
2971 }
2972
2973 # blame Leolo :)
2974 *yatf = \&matrix_transform;
2975
2976 # These two are supported for legacy code only
2977
2978 sub i_color_new {
2979 return Imager::Color->new(@_);
2980 }
2981
2982 sub i_color_set {
2983 return Imager::Color::set(@_);
2984 }
2985
2986 # Draws a box between the specified corner points.
2987 sub box {
2988 my $self=shift;
2989 my $raw = $self->{IMG};
2990
2991 $self->_valid_image("box")
2992 or return;
2993
2994 my %opts = @_;
2995
2996 my ($xmin, $ymin, $xmax, $ymax);
2997 if (exists $opts{'box'}) {
2998 $xmin = _min($opts{'box'}->[0],$opts{'box'}->[2]);
2999 $xmax = _max($opts{'box'}->[0],$opts{'box'}->[2]);
3000 $ymin = _min($opts{'box'}->[1],$opts{'box'}->[3]);
3001 $ymax = _max($opts{'box'}->[1],$opts{'box'}->[3]);
3002 }
3003 else {
3004 defined($xmin = $opts{xmin}) or $xmin = 0;
3005 defined($xmax = $opts{xmax}) or $xmax = $self->getwidth()-1;
3006 defined($ymin = $opts{ymin}) or $ymin = 0;
3007 defined($ymax = $opts{ymax}) or $ymax = $self->getheight()-1;
3008 }
3009
3010 if ($opts{filled}) {
3011 my $color = $opts{'color'};
3012
3013 if (defined $color) {
3014 unless (_is_color_object($color)) {
3015 $color = _color($color);
3016 unless ($color) {
3017 $self->{ERRSTR} = $Imager::ERRSTR;
3018 return;
3019 }
3020 }
3021 }
3022 else {
3023 $color = i_color_new(255,255,255,255);
3024 }
3025
3026 if ($color->isa("Imager::Color")) {
3027 i_box_filled($raw, $xmin, $ymin,$xmax, $ymax, $color);
3028 }
3029 else {
3030 i_box_filledf($raw, $xmin, $ymin,$xmax, $ymax, $color);
3031 }
3032 }
3033 elsif ($opts{fill}) {
3034 unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) {
3035 # assume it's a hash ref
3036 require 'Imager/Fill.pm';
3037 unless ($opts{fill} = Imager::Fill->new(%{$opts{fill}})) {
3038 $self->{ERRSTR} = $Imager::ERRSTR;
3039 return undef;
3040 }
3041 }
3042 i_box_cfill($raw, $xmin, $ymin, $xmax, $ymax, $opts{fill}{fill});
3043 }
3044 else {
3045 my $color = $opts{'color'};
3046 if (defined $color) {
3047 unless (_is_color_object($color)) {
3048 $color = _color($color);
3049 unless ($color) {
3050 $self->{ERRSTR} = $Imager::ERRSTR;
3051 return;
3052 }
3053 }
3054 }
3055 else {
3056 $color = i_color_new(255, 255, 255, 255);
3057 }
3058 unless ($color) {
3059 $self->{ERRSTR} = $Imager::ERRSTR;
3060 return;
3061 }
3062 i_box($raw, $xmin, $ymin, $xmax, $ymax, $color);
3063 }
3064
3065 return $self;
3066 }
3067
3068 sub arc {
3069 my $self=shift;
3070
3071 $self->_valid_image("arc")
3072 or return;
3073
3074 my $dflcl= [ 255, 255, 255, 255];
3075 my $good = 1;
3076 my %opts=
3077 (
3078 color=>$dflcl,
3079 'r'=>_min($self->getwidth(),$self->getheight())/3,
3080 'x'=>$self->getwidth()/2,
3081 'y'=>$self->getheight()/2,
3082 'd1'=>0, 'd2'=>361,
3083 filled => 1,
3084 @_,
3085 );
3086 if ($opts{aa}) {
3087 if ($opts{fill}) {
3088 unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) {
3089 # assume it's a hash ref
3090 require 'Imager/Fill.pm';
3091 unless ($opts{fill} = Imager::Fill->new(%{$opts{fill}})) {
3092 $self->{ERRSTR} = $Imager::ERRSTR;
3093 return;
3094 }
3095 }
3096 if ($opts{d1} == 0 && $opts{d2} == 361) {
3097 i_circle_aa_fill($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{'r'},
3098 $opts{fill}{fill});
3099 }
3100 else {
3101 i_arc_aa_cfill($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'},$opts{'d1'},
3102 $opts{'d2'}, $opts{fill}{fill});
3103 }
3104 }
3105 elsif ($opts{filled}) {
3106 my $color = _color($opts{'color'});
3107 unless ($color) {
3108 $self->{ERRSTR} = $Imager::ERRSTR;
3109 return;
3110 }
3111 if ($opts{d1} == 0 && $opts{d2} == 361 && $opts{aa}) {
3112 i_circle_aa($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{'r'},
3113 $color);
3114 }
3115 else {
3116 i_arc_aa($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'},
3117 $opts{'d1'}, $opts{'d2'}, $color);
3118 }
3119 }
3120 else {
3121 my $color = _color($opts{'color'});
3122 if ($opts{d2} - $opts{d1} >= 360) {
3123 $good = i_circle_out_aa($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{'r'}, $color);
3124 }
3125 else {
3126 $good = i_arc_out_aa($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{'r'}, $opts{'d1'}, $opts{'d2'}, $color);
3127 }
3128 }
3129 }
3130 else {
3131 if ($opts{fill}) {
3132 unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) {
3133 # assume it's a hash ref
3134 require 'Imager/Fill.pm';
3135 unless ($opts{fill} = Imager::Fill->new(%{$opts{fill}})) {
3136 $self->{ERRSTR} = $Imager::ERRSTR;
3137 return;
3138 }
3139 }
3140 i_arc_cfill($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'},$opts{'d1'},
3141 $opts{'d2'}, $opts{fill}{fill});
3142 }
3143 else {
3144 my $color = _color($opts{'color'});
3145 unless ($color) {
3146 $self->{ERRSTR} = $Imager::ERRSTR;
3147 return;
3148 }
3149 if ($opts{filled}) {
3150 i_arc($self->{IMG},$opts{'x'},$opts{'y'},$opts{'r'},
3151 $opts{'d1'}, $opts{'d2'}, $color);
3152 }
3153 else {
3154 if ($opts{d1} == 0 && $opts{d2} == 361) {
3155 $good = i_circle_out($self->{IMG}, $opts{x}, $opts{y}, $opts{r}, $color);
3156 }
3157 else {
3158 $good = i_arc_out($self->{IMG}, $opts{x}, $opts{y}, $opts{r}, $opts{d1}, $opts{d2}, $color);
3159 }
3160 }
3161 }
3162 }
3163 unless ($good) {
3164 $self->_set_error($self->_error_as_msg);
3165 return;
3166 }
3167
3168 return $self;
3169 }
3170
3171 # Draws a line from one point to the other
3172 # the endpoint is set if the endp parameter is set which it is by default.
3173 # to turn of the endpoint being set use endp=>0 when calling line.
3174
3175 sub line {
3176 my $self=shift;
3177 my $dflcl=i_color_new(0,0,0,0);
3178 my %opts=(color=>$dflcl,
3179 endp => 1,
3180 @_);
3181
3182 $self->_valid_image("line")
3183 or return;
3184
3185 unless (exists $opts{x1} and exists $opts{y1}) { $self->{ERRSTR}='missing begining coord'; return undef; }
3186 unless (exists $opts{x2} and exists $opts{y2}) { $self->{ERRSTR}='missing ending coord'; return undef; }
3187
3188 my $color = _color($opts{'color'});
3189 unless ($color) {
3190 $self->{ERRSTR} = $Imager::ERRSTR;
3191 return;
3192 }
3193
3194 $opts{antialias} = $opts{aa} if defined $opts{aa};
3195 if ($opts{antialias}) {
3196 i_line_aa($self->{IMG},$opts{x1}, $opts{y1}, $opts{x2}, $opts{y2},
3197 $color, $opts{endp});
3198 } else {
3199 i_line($self->{IMG},$opts{x1}, $opts{y1}, $opts{x2}, $opts{y2},
3200 $color, $opts{endp});
3201 }
3202 return $self;
3203 }
3204
3205 # Draws a line between an ordered set of points - It more or less just transforms this
3206 # into a list of lines.
3207
3208 sub polyline {
3209 my $self=shift;
3210 my ($pt,$ls,@points);
3211 my $dflcl=i_color_new(0,0,0,0);
3212 my %opts=(color=>$dflcl,@_);
3213
3214 $self->_valid_image("polyline")
3215 or return;
3216
3217 if (exists($opts{points})) { @points=@{$opts{points}}; }
3218 if (!exists($opts{points}) and exists($opts{'x'}) and exists($opts{'y'}) ) {
3219 @points=map { [ $opts{'x'}->[$_],$opts{'y'}->[$_] ] } (0..(scalar @{$opts{'x'}}-1));
3220 }
3221
3222 # print Dumper(\@points);
3223
3224 my $color = _color($opts{'color'});
3225 unless ($color) {
3226 $self->{ERRSTR} = $Imager::ERRSTR;
3227 return;
3228 }
3229 $opts{antialias} = $opts{aa} if defined $opts{aa};
3230 if ($opts{antialias}) {
3231 for $pt(@points) {
3232 if (defined($ls)) {
3233 i_line_aa($self->{IMG},$ls->[0],$ls->[1],$pt->[0],$pt->[1],$color, 1);
3234 }
3235 $ls=$pt;
3236 }
3237 } else {
3238 for $pt(@points) {
3239 if (defined($ls)) {
3240 i_line($self->{IMG},$ls->[0],$ls->[1],$pt->[0],$pt->[1],$color,1);
3241 }
3242 $ls=$pt;
3243 }
3244 }
3245 return $self;
3246 }
3247
3248 sub polygon {
3249 my $self = shift;
3250 my ($pt,$ls,@points);
3251 my $dflcl = i_color_new(0,0,0,0);
3252 my %opts = (color=>$dflcl, @_);
3253
3254 $self->_valid_image("polygon")
3255 or return;
3256
3257 if (exists($opts{points})) {
3258 $opts{'x'} = [ map { $_->[0] } @{$opts{points}} ];
3259 $opts{'y'} = [ map { $_->[1] } @{$opts{points}} ];
3260 }
3261
3262 if (!exists $opts{'x'} or !exists $opts{'y'}) {
3263 $self->{ERRSTR} = 'no points array, or x and y arrays.'; return undef;
3264 }
3265
3266 my $mode = _first($opts{mode}, 0);
3267
3268 if ($opts{'fill'}) {
3269 unless (UNIVERSAL::isa($opts{'fill'}, 'Imager::Fill')) {
3270 # assume it's a hash ref
3271 require 'Imager/Fill.pm';
3272 unless ($opts{'fill'} = Imager::Fill->new(%{$opts{'fill'}})) {
3273 $self->{ERRSTR} = $Imager::ERRSTR;
3274 return undef;
3275 }
3276 }
3277 unless (i_poly_aa_cfill_m($self->{IMG}, $opts{'x'}, $opts{'y'},
3278 $mode, $opts{'fill'}{'fill'})) {
3279 return $self->_set_error($self->_error_as_msg);
3280 }
3281 }
3282 else {
3283 my $color = _color($opts{'color'});
3284 unless ($color) {
3285 $self->{ERRSTR} = $Imager::ERRSTR;
3286 return;
3287 }
3288 unless (i_poly_aa_m($self->{IMG}, $opts{'x'}, $opts{'y'}, $mode, $color)) {
3289 return $self->_set_error($self->_error_as_msg);
3290 }
3291 }
3292
3293 return $self;
3294 }
3295
3296 sub polypolygon {
3297 my ($self, %opts) = @_;
3298
3299 $self->_valid_image("polypolygon")
3300 or return;
3301
3302 my $points = $opts{points};
3303 $points
3304 or return $self->_set_error("polypolygon: missing required points");
3305
3306 my $mode = _first($opts{mode}, "evenodd");
3307
3308 if ($opts{filled}) {
3309 my $color = _color(_first($opts{color}, [ 0, 0, 0, 0 ]))
3310 or return $self->_set_error($Imager::ERRSTR);
3311
3312 i_poly_poly_aa($self->{IMG}, $points, $mode, $color)
3313 or return $self->_set_error($self->_error_as_msg);
3314 }
3315 elsif ($opts{fill}) {
3316 my $fill = $opts{fill};
3317 $self->_valid_fill($fill, "polypolygon")
3318 or return;
3319
3320 i_poly_poly_aa_cfill($self->{IMG}, $points, $mode, $fill->{fill})
3321 or return $self->_set_error($self->_error_as_msg);
3322 }
3323 else {
3324 my $color = _color(_first($opts{color}, [ 0, 0, 0, 255 ]))
3325 or return $self->_set_error($Imager::ERRSTR);
3326
3327 my $rimg = $self->{IMG};
3328
3329 if (_first($opts{aa}, 1)) {
3330 for my $poly (@$points) {
3331 my $xp = $poly->[0];
3332 my $yp = $poly->[1];
3333 for my $i (0 .. $#$xp - 1) {
3334 i_line_aa($rimg, $xp->[$i], $yp->[$i], $xp->[$i+1], $yp->[$i+1],
3335 $color, 0);
3336 }
3337 i_line_aa($rimg, $xp->[$#$xp], $yp->[$#$yp], $xp->[0], $yp->[0],
3338 $color, 0);
3339 }
3340 }
3341 else {
3342 for my $poly (@$points) {
3343 my $xp = $poly->[0];
3344 my $yp = $poly->[1];
3345 for my $i (0 .. $#$xp - 1) {
3346 i_line($rimg, $xp->[$i], $yp->[$i], $xp->[$i+1], $yp->[$i+1],
3347 $color, 0);
3348 }
3349 i_line($rimg, $xp->[$#$xp], $yp->[$#$yp], $xp->[0], $yp->[0],
3350 $color, 0);
3351 }
3352 }
3353 }
3354
3355 return $self;
3356 }
3357
3358 # this the multipoint bezier curve
3359 # this is here more for testing that actual usage since
3360 # this is not a good algorithm. Usually the curve would be
3361 # broken into smaller segments and each done individually.
3362
3363 sub polybezier {
3364 my $self=shift;
3365 my ($pt,$ls,@points);
3366 my $dflcl=i_color_new(0,0,0,0);
3367 my %opts=(color=>$dflcl,@_);
3368
3369 $self->_valid_image("polybezier")
3370 or return;
3371
3372 if (exists $opts{points}) {
3373 $opts{'x'}=map { $_->[0]; } @{$opts{'points'}};
3374 $opts{'y'}=map { $_->[1]; } @{$opts{'points'}};
3375 }
3376
3377 unless ( @{$opts{'x'}} and @{$opts{'x'}} == @{$opts{'y'}} ) {
3378 $self->{ERRSTR}='Missing or invalid points.';
3379 return;
3380 }
3381
3382 my $color = _color($opts{'color'});
3383 unless ($color) {
3384 $self->{ERRSTR} = $Imager::ERRSTR;
3385 return;
3386 }
3387 i_bezier_multi($self->{IMG},$opts{'x'},$opts{'y'},$color);
3388 return $self;
3389 }
3390
3391 sub flood_fill {
3392 my $self = shift;
3393 my %opts = ( color=>Imager::Color->new(255, 255, 255), @_ );
3394 my $rc;
3395
3396 $self->_valid_image("flood_fill")
3397 or return;
3398
3399 unless (exists $opts{'x'} && exists $opts{'y'}) {
3400 $self->{ERRSTR} = "missing seed x and y parameters";
3401 return undef;
3402 }
3403
3404 if ($opts{border}) {
3405 my $border = _color($opts{border});
3406 unless ($border) {
3407 $self->_set_error($Imager::ERRSTR);
3408 return;
3409 }
3410 if ($opts{fill}) {
3411 unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) {
3412 # assume it's a hash ref
3413 require Imager::Fill;
3414 unless ($opts{fill} = Imager::Fill->new(%{$opts{fill}})) {
3415 $self->{ERRSTR} = $Imager::ERRSTR;
3416 return;
3417 }
3418 }
3419 $rc = i_flood_cfill_border($self->{IMG}, $opts{'x'}, $opts{'y'},
3420 $opts{fill}{fill}, $border);
3421 }
3422 else {
3423 my $color = _color($opts{'color'});
3424 unless ($color) {
3425 $self->{ERRSTR} = $Imager::ERRSTR;
3426 return;
3427 }
3428 $rc = i_flood_fill_border($self->{IMG}, $opts{'x'}, $opts{'y'},
3429 $color, $border);
3430 }
3431 if ($rc) {
3432 return $self;
3433 }
3434 else {
3435 $self->{ERRSTR} = $self->_error_as_msg();
3436 return;
3437 }
3438 }
3439 else {
3440 if ($opts{fill}) {
3441 unless (UNIVERSAL::isa($opts{fill}, 'Imager::Fill')) {
3442 # assume it's a hash ref
3443 require 'Imager/Fill.pm';
3444 unless ($opts{fill} = Imager::Fill->new(%{$opts{fill}})) {
3445 $self->{ERRSTR} = $Imager::ERRSTR;
3446 return;
3447 }
3448 }
3449 $rc = i_flood_cfill($self->{IMG}, $opts{'x'}, $opts{'y'}, $opts{fill}{fill});
3450 }
3451 else {
3452 my $color = _color($opts{'color'});
3453 unless ($color) {
3454 $self->{ERRSTR} = $Imager::ERRSTR;
3455 return;
3456 }
3457 $rc = i_flood_fill($self->{IMG}, $opts{'x'}, $opts{'y'}, $color);
3458 }
3459 if ($rc) {
3460 return $self;
3461 }
3462 else {
3463 $self->{ERRSTR} = $self->_error_as_msg();
3464 return;
3465 }
3466 }
3467 }
3468
3469 sub setpixel {
3470 my ($self, %opts) = @_;
3471
3472 $self->_valid_image("setpixel")
3473 or return;
3474
3475 my $color = $opts{color};
3476 unless (defined $color) {
3477 $color = $self->{fg};
3478 defined $color or $color = NC(255, 255, 255);
3479 }
3480
3481 unless (ref $color && UNIVERSAL::isa($color, "Imager::Color")) {
3482 unless ($color = _color($color, 'setpixel')) {
3483 $self->_set_error("setpixel: " . Imager->errstr);
3484 return;
3485 }
3486 }
3487
3488 unless (exists $opts{'x'} && exists $opts{'y'}) {
3489 $self->_set_error('setpixel: missing x or y parameter');
3490 return;
3491 }
3492
3493 my $x = $opts{'x'};
3494 my $y = $opts{'y'};
3495 if (ref $x || ref $y) {
3496 $x = ref $x ? $x : [ $x ];
3497 $y = ref $y ? $y : [ $y ];
3498 unless (@$x) {
3499 $self->_set_error("setpixel: x is a reference to an empty array");
3500 return;
3501 }
3502 unless (@$y) {
3503 $self->_set_error("setpixel: y is a reference to an empty array");
3504 return;
3505 }
3506
3507 # make both the same length, replicating the last element
3508 if (@$x < @$y) {
3509 $x = [ @$x, ($x->[-1]) x (@$y - @$x) ];
3510 }
3511 elsif (@$y < @$x) {
3512 $y = [ @$y, ($y->[-1]) x (@$x - @$y) ];
3513 }
3514
3515 my $set = 0;
3516 if ($color->isa('Imager::Color')) {
3517 for my $i (0..$#$x) {
3518 i_ppix($self->{IMG}, $x->[$i], $y->[$i], $color)
3519 or ++$set;
3520 }
3521 }
3522 else {
3523 for my $i (0..$#$x) {
3524 i_ppixf($self->{IMG}, $x->[$i], $y->[$i], $color)
3525 or ++$set;
3526 }
3527 }
3528
3529 return $set;
3530 }
3531 else {
3532 if ($color->isa('Imager::Color')) {
3533 i_ppix($self->{IMG}, $x, $y, $color)
3534 and return "0 but true";
3535 }
3536 else {
3537 i_ppixf($self->{IMG}, $x, $y, $color)
3538 and return "0 but true";
3539 }
3540
3541 return 1;
3542 }
3543 }
3544
3545 sub getpixel {
3546 my $self = shift;
3547
3548 my %opts = ( "type"=>'8bit', @_);
3549
3550 $self->_valid_image("getpixel")
3551 or return;
3552
3553 unless (exists $opts{'x'} && exists $opts{'y'}) {
3554 $self->_set_error('getpixel: missing x or y parameter');
3555 return;
3556 }
3557
3558 my $x = $opts{'x'};
3559 my $y = $opts{'y'};
3560 my $type = $opts{'type'};
3561 if (ref $x || ref $y) {
3562 $x = ref $x ? $x : [ $x ];
3563 $y = ref $y ? $y : [ $y ];
3564 unless (@$x) {
3565 $self->_set_error("getpixel: x is a reference to an empty array");
3566 return;
3567 }
3568 unless (@$y) {
3569 $self->_set_error("getpixel: y is a reference to an empty array");
3570 return;
3571 }
3572
3573 # make both the same length, replicating the last element
3574 if (@$x < @$y) {
3575 $x = [ @$x, ($x->[-1]) x (@$y - @$x) ];
3576 }
3577 elsif (@$y < @$x) {
3578 $y = [ @$y, ($y->[-1]) x (@$x - @$y) ];
3579 }
3580
3581 my @result;
3582 if ($type eq '8bit') {
3583 for my $i (0..$#$x) {
3584 push(@result, i_get_pixel($self->{IMG}, $x->[$i], $y->[$i]));
3585 }
3586 }
3587 elsif ($type eq 'float' || $type eq 'double') {
3588 for my $i (0..$#$x) {
3589 push(@result, i_gpixf($self->{IMG}, $x->[$i], $y->[$i]));
3590 }
3591 }
3592 else {
3593 $self->_set_error("getpixel: type must be '8bit' or 'float'");
3594 return;
3595 }
3596 return wantarray ? @result : \@result;
3597 }
3598 else {
3599 if ($type eq '8bit') {
3600 return i_get_pixel($self->{IMG}, $x, $y);
3601 }
3602 elsif ($type eq 'float' || $type eq 'double') {
3603 return i_gpixf($self->{IMG}, $x, $y);
3604 }
3605 else {
3606 $self->_set_error("getpixel: type must be '8bit' or 'float'");
3607 return;
3608 }
3609 }
3610 }
3611
3612 sub getscanline {
3613 my $self = shift;
3614 my %opts = ( type => '8bit', x=>0, @_);
3615
3616 $self->_valid_image("getscanline")
3617 or return;
3618
3619 defined $opts{width} or $opts{width} = $self->getwidth - $opts{x};
3620
3621 unless (defined $opts{'y'}) {
3622 $self->_set_error("missing y parameter");
3623 return;
3624 }
3625
3626 if ($opts{type} eq '8bit') {
3627 return i_glin($self->{IMG}, $opts{x}, $opts{x}+$opts{width},
3628 $opts{'y'});
3629 }
3630 elsif ($opts{type} eq 'float') {
3631 return i_glinf($self->{IMG}, $opts{x}, $opts{x}+$opts{width},
3632 $opts{'y'});
3633 }
3634 elsif ($opts{type} eq 'index') {
3635 unless (i_img_type($self->{IMG})) {
3636 $self->_set_error("type => index only valid on paletted images");
3637 return;
3638 }
3639 return i_gpal($self->{IMG}, $opts{x}, $opts{x} + $opts{width},
3640 $opts{'y'});
3641 }
3642 else {
3643 $self->_set_error("invalid type parameter - must be '8bit' or 'float'");
3644 return;
3645 }
3646 }
3647
3648 sub setscanline {
3649 my $self = shift;
3650 my %opts = ( x=>0, @_);
3651
3652 $self->_valid_image("setscanline")
3653 or return;
3654
3655 unless (defined $opts{'y'}) {
3656 $self->_set_error("missing y parameter");
3657 return;
3658 }
3659
3660 if (!$opts{type}) {
3661 if (ref $opts{pixels} && @{$opts{pixels}}) {
3662 # try to guess the type
3663 if ($opts{pixels}[0]->isa('Imager::Color')) {
3664 $opts{type} = '8bit';
3665 }
3666 elsif ($opts{pixels}[0]->isa('Imager::Color::Float')) {
3667 $opts{type} = 'float';
3668 }
3669 else {
3670 $self->_set_error("missing type parameter and could not guess from pixels");
3671 return;
3672 }
3673 }
3674 else {
3675 # default
3676 $opts{type} = '8bit';
3677 }
3678 }
3679
3680 if ($opts{type} eq '8bit') {
3681 if (ref $opts{pixels}) {
3682 return i_plin($self->{IMG}, $opts{x}, $opts{'y'}, @{$opts{pixels}});
3683 }
3684 else {
3685 return i_plin($self->{IMG}, $opts{x}, $opts{'y'}, $opts{pixels});
3686 }
3687 }
3688 elsif ($opts{type} eq 'float') {
3689 if (ref $opts{pixels}) {
3690 return i_plinf($self->{IMG}, $opts{x}, $opts{'y'}, @{$opts{pixels}});
3691 }
3692 else {
3693 return i_plinf($self->{IMG}, $opts{x}, $opts{'y'}, $opts{pixels});
3694 }
3695 }
3696 elsif ($opts{type} eq 'index') {
3697 if (ref $opts{pixels}) {
3698 return i_ppal($self->{IMG}, $opts{x}, $opts{'y'}, @{$opts{pixels}});
3699 }
3700 else {
3701 return i_ppal_p($self->{IMG}, $opts{x}, $opts{'y'}, $opts{pixels});
3702 }
3703 }
3704 else {
3705 $self->_set_error("invalid type parameter - must be '8bit' or 'float'");
3706 return;
3707 }
3708 }
3709
3710 sub getsamples {
3711 my $self = shift;
3712 my %opts = ( type => '8bit', x=>0, offset => 0, @_);
3713
3714 $self->_valid_image("getsamples")
3715 or return;
3716
3717 defined $opts{width} or $opts{width} = $self->getwidth - $opts{x};
3718
3719 unless (defined $opts{'y'}) {
3720 $self->_set_error("missing y parameter");
3721 return;
3722 }
3723
3724 if ($opts{target}) {
3725 my $target = $opts{target};
3726 my $offset = $opts{offset};
3727 if ($opts{type} eq '8bit') {
3728 my @samples = i_gsamp($self->{IMG}, $opts{x}, $opts{x}+$opts{width},
3729 $opts{y}, $opts{channels})
3730 or return;
3731 @{$target}[$offset .. $offset + @samples - 1] = @samples;
3732 return scalar(@samples);
3733 }
3734 elsif ($opts{type} eq 'float') {
3735 my @samples = i_gsampf($self->{IMG}, $opts{x}, $opts{x}+$opts{width},
3736 $opts{y}, $opts{channels});
3737 @{$target}[$offset .. $offset + @samples - 1] = @samples;
3738 return scalar(@samples);
3739 }
3740 elsif ($opts{type} =~ /^(\d+)bit$/) {
3741 my $bits = $1;
3742
3743 my @data;
3744 my $count = i_gsamp_bits($self->{IMG}, $opts{x}, $opts{x}+$opts{width},
3745 $opts{y}, $bits, $target,
3746 $offset, $opts{channels});
3747 unless (defined $count) {
3748 $self->_set_error(Imager->_error_as_msg);
3749 return;
3750 }
3751
3752 return $count;
3753 }
3754 else {
3755 $self->_set_error("invalid type parameter - must be '8bit' or 'float'");
3756 return;
3757 }
3758 }
3759 else {
3760 if ($opts{type} eq '8bit') {
3761 return i_gsamp($self->{IMG}, $opts{x}, $opts{x}+$opts{width},
3762 $opts{y}, $opts{channels});
3763 }
3764 elsif ($opts{type} eq 'float') {
3765 return i_gsampf($self->{IMG}, $opts{x}, $opts{x}+$opts{width},
3766 $opts{y}, $opts{channels});
3767 }
3768 elsif ($opts{type} =~ /^(\d+)bit$/) {
3769 my $bits = $1;
3770
3771 my @data;
3772 i_gsamp_bits($self->{IMG}, $opts{x}, $opts{x}+$opts{width},
3773 $opts{y}, $bits, \@data, 0, $opts{channels})
3774 or return;
3775 return @data;
3776 }
3777 else {
3778 $self->_set_error("invalid type parameter - must be '8bit' or 'float'");
3779 return;
3780 }
3781 }
3782 }
3783
3784 sub setsamples {
3785 my $self = shift;
3786
3787 $self->_valid_image("setsamples")
3788 or return;
3789
3790 my %opts = ( x => 0, offset => 0 );
3791 my $data_index;
3792 # avoid duplicating the data parameter, it may be a large scalar
3793 my $i = 0;
3794 while ($i < @_ -1) {
3795 if ($_[$i] eq 'data') {
3796 $data_index = $i+1;
3797 }
3798 else {
3799 $opts{$_[$i]} = $_[$i+1];
3800 }
3801
3802 $i += 2;
3803 }
3804
3805 unless(defined $data_index) {
3806 $self->_set_error('setsamples: data parameter missing');
3807 return;
3808 }
3809 unless (defined $_[$data_index]) {
3810 $self->_set_error('setsamples: data parameter not defined');
3811 return;
3812 }
3813
3814 my $type = $opts{type};
3815 defined $type or $type = '8bit';
3816
3817 my $width = defined $opts{width} ? $opts{width}
3818 : $self->getwidth() - $opts{x};
3819
3820 my $count;
3821 if ($type eq '8bit') {
3822 $count = i_psamp($self->{IMG}, $opts{x}, $opts{y}, $opts{channels},
3823 $_[$data_index], $opts{offset}, $width);
3824 }
3825 elsif ($type eq 'float') {
3826 $count = i_psampf($self->{IMG}, $opts{x}, $opts{y}, $opts{channels},
3827 $_[$data_index], $opts{offset}, $width);
3828 }
3829 elsif ($type =~ /^([0-9]+)bit$/) {
3830 my $bits = $1;
3831
3832 unless (ref $_[$data_index]) {
3833 $self->_set_error("setsamples: data must be an array ref for type not 8bit or float");
3834 return;
3835 }
3836
3837 $count = i_psamp_bits($self->{IMG}, $opts{x}, $opts{y}, $bits,
3838 $opts{channels}, $_[$data_index], $opts{offset},
3839 $width);
3840 }
3841 else {
3842 $self->_set_error('setsamples: type parameter invalid');
3843 return;
3844 }
3845
3846 unless (defined $count) {
3847 $self->_set_error(Imager->_error_as_msg);
3848 return;
3849 }
3850
3851 return $count;
3852 }
3853
3854 # make an identity matrix of the given size
3855 sub _identity {
3856 my ($size) = @_;
3857
3858 my $matrix = [ map { [ (0) x $size ] } 1..$size ];
3859 for my $c (0 .. ($size-1)) {
3860 $matrix->[$c][$c] = 1;
3861 }
3862 return $matrix;
3863 }
3864
3865 # general function to convert an image
3866 sub convert {
3867 my ($self, %opts) = @_;
3868 my $matrix;
3869
3870 $self->_valid_image("convert")
3871 or return;
3872
3873 unless (defined wantarray) {
3874 my @caller = caller;
3875 warn "convert() called in void context - convert() returns the converted image at $caller[1] line $caller[2]\n";
3876 return;
3877 }
3878
3879 # the user can either specify a matrix or preset
3880 # the matrix overrides the preset
3881 if (!exists($opts{matrix})) {
3882 unless (exists($opts{preset})) {
3883 $self->{ERRSTR} = "convert() needs a matrix or preset";
3884 return;
3885 }
3886 else {
3887 if ($opts{preset} eq 'gray' || $opts{preset} eq 'grey') {
3888 # convert to greyscale, keeping the alpha channel if any
3889 if ($self->getchannels == 3) {
3890 $matrix = [ [ 0.222, 0.707, 0.071 ] ];
3891 }
3892 elsif ($self->getchannels == 4) {
3893 # preserve the alpha channel
3894 $matrix = [ [ 0.222, 0.707, 0.071, 0 ],
3895 [ 0, 0, 0, 1 ] ];
3896 }
3897 else {
3898 # an identity
3899 $matrix = _identity($self->getchannels);
3900 }
3901 }
3902 elsif ($opts{preset} eq 'noalpha') {
3903 # strip the alpha channel
3904 if ($self->getchannels == 2 or $self->getchannels == 4) {
3905 $matrix = _identity($self->getchannels);
3906 pop(@$matrix); # lose the alpha entry
3907 }
3908 else {
3909 $matrix = _identity($self->getchannels);
3910 }
3911 }
3912 elsif ($opts{preset} eq 'red' || $opts{preset} eq 'channel0') {
3913 # extract channel 0
3914 $matrix = [ [ 1 ] ];
3915 }
3916 elsif ($opts{preset} eq 'green' || $opts{preset} eq 'channel1') {
3917 $matrix = [ [ 0, 1 ] ];
3918 }
3919 elsif ($opts{preset} eq 'blue' || $opts{preset} eq 'channel2') {
3920 $matrix = [ [ 0, 0, 1 ] ];
3921 }
3922 elsif ($opts{preset} eq 'alpha') {
3923 if ($self->getchannels == 2 or $self->getchannels == 4) {
3924 $matrix = [ [ (0) x ($self->getchannels-1), 1 ] ];
3925 }
3926 else {
3927 # the alpha is just 1 <shrug>
3928 $matrix = [ [ (0) x $self->getchannels, 1 ] ];
3929 }
3930 }
3931 elsif ($opts{preset} eq 'rgb') {
3932 if ($self->getchannels == 1) {
3933 $matrix = [ [ 1 ], [ 1 ], [ 1 ] ];
3934 }
3935 elsif ($self->getchannels == 2) {
3936 # preserve the alpha channel
3937 $matrix = [ [ 1, 0 ], [ 1, 0 ], [ 1, 0 ], [ 0, 1 ] ];
3938 }
3939 else {
3940 $matrix = _identity($self->getchannels);
3941 }
3942 }
3943 elsif ($opts{preset} eq 'addalpha') {
3944 if ($self->getchannels == 1) {
3945 $matrix = _identity(2);
3946 }
3947 elsif ($self->getchannels == 3) {
3948 $matrix = _identity(4);
3949 }
3950 else {
3951 $matrix = _identity($self->getchannels);
3952 }
3953 }
3954 else {
3955 $self->{ERRSTR} = "Unknown convert preset $opts{preset}";
3956 return undef;
3957 }
3958 }
3959 }
3960 else {
3961 $matrix = $opts{matrix};
3962 }
3963
3964 my $new = Imager->new;
3965 $new->{IMG} = i_convert($self->{IMG}, $matrix);
3966 unless ($new->{IMG}) {
3967 # most likely a bad matrix
3968 i_push_error(0, "convert");
3969 $self->{ERRSTR} = _error_as_msg();
3970 return undef;
3971 }
3972 return $new;
3973 }
3974
3975 # combine channels from multiple input images, a class method
3976 sub combine {
3977 my ($class, %opts) = @_;
3978
3979 my $src = delete $opts{src};
3980 unless ($src) {
3981 $class->_set_error("src parameter missing");
3982 return;
3983 }
3984 my @imgs;
3985 my $index = 0;
3986 for my $img (@$src) {
3987 unless (eval { $img->isa("Imager") }) {
3988 $class->_set_error("src must contain image objects");
3989 return;
3990 }
3991 unless ($img->_valid_image("combine")) {
3992 $Imager::ERRSTR = $img->{ERRSTR} . " (src->[$index])";
3993 return;
3994 }
3995 push @imgs, $img->{IMG};
3996 }
3997 my $result;
3998 if (my $channels = delete $opts{channels}) {
3999 $result = i_combine(\@imgs, $channels);
4000 }
4001 else {
4002 $result = i_combine(\@imgs);
4003 }
4004 unless ($result) {
4005 $class->_set_error($class->_error_as_msg);
4006 return;
4007 }
4008
4009 my $img = $class->new;
4010 $img->{IMG} = $result;
4011
4012 return $img;
4013 }
4014
4015
4016 # general function to map an image through lookup tables
4017
4018 sub map {
4019 my ($self, %opts) = @_;
4020 my @chlist = qw( red green blue alpha );
4021
4022 $self->_valid_image("map")
4023 or return;
4024
4025 if (!exists($opts{'maps'})) {
4026 # make maps from channel maps
4027 my $chnum;
4028 for $chnum (0..$#chlist) {
4029 if (exists $opts{$chlist[$chnum]}) {
4030 $opts{'maps'}[$chnum] = $opts{$chlist[$chnum]};
4031 } elsif (exists $opts{'all'}) {
4032 $opts{'maps'}[$chnum] = $opts{'all'};
4033 }
4034 }
4035 }
4036 if ($opts{'maps'} and $self->{IMG}) {
4037 i_map($self->{IMG}, $opts{'maps'} );
4038 }
4039 return $self;
4040 }
4041
4042 sub difference {
4043 my ($self, %opts) = @_;
4044
4045 $self->_valid_image("difference")
4046 or return;
4047
4048 defined $opts{mindist} or $opts{mindist} = 0;
4049
4050 defined $opts{other}
4051 or return $self->_set_error("No 'other' parameter supplied");
4052 unless ($opts{other}->_valid_image("difference")) {
4053 $self->_set_error($opts{other}->errstr . " (other image)");
4054 return;
4055 }
4056
4057 my $result = Imager->new;
4058 $result->{IMG} = i_diff_image($self->{IMG}, $opts{other}{IMG},
4059 $opts{mindist})
4060 or return $self->_set_error($self->_error_as_msg());
4061
4062 return $result;
4063 }
4064
4065 sub rgb_difference {
4066 my ($self, %opts) = @_;
4067
4068 $self->_valid_image("rgb_difference")
4069 or return;
4070
4071 defined $opts{other}
4072 or return $self->_set_error("No 'other' parameter supplied");
4073 unless ($opts{other}->_valid_image("rgb_difference")) {
4074 $self->_set_error($opts{other}->errstr . " (other image)");
4075 return;
4076 }
4077
4078 my $result = Imager->new;
4079 $result->{IMG} = i_rgbdiff_image($self->{IMG}, $opts{other}{IMG})
4080 or return $self->_set_error($self->_error_as_msg());
4081
4082 return $result;
4083 }
4084
4085 # destructive border - image is shrunk by one pixel all around
4086
4087 sub border {
4088 my ($self,%opts)=@_;
4089 my($tx,$ty)=($self->getwidth()-1,$self->getheight()-1);
4090 $self->polyline('x'=>[0,$tx,$tx,0,0],'y'=>[0,0,$ty,$ty,0],%opts);
4091 }
4092
4093
4094 # Get the width of an image
4095
4096 sub getwidth {
4097 my $self = shift;
4098
4099 $self->_valid_image("getwidth")
4100 or return;
4101
4102 return i_img_get_width($self->{IMG});
4103 }
4104
4105 # Get the height of an image
4106
4107 sub getheight {
4108 my $self = shift;
4109
4110 $self->_valid_image("getheight")
4111 or return;
4112
4113 return i_img_get_height($self->{IMG});
4114 }
4115
4116 # Get number of channels in an image
4117
4118 sub getchannels {
4119 my $self = shift;
4120
4121 $self->_valid_image("getchannels")
4122 or return;
4123
4124 return i_img_getchannels($self->{IMG});
4125 }
4126
4127 my @model_names = qw(unknown gray graya rgb rgba);
4128
4129 sub colormodel {
4130 my ($self, %opts) = @_;
4131
4132 $self->_valid_image("colormodel")
4133 or return;
4134
4135 my $model = i_img_color_model($self->{IMG});
4136
4137 return $opts{numeric} ? $model : $model_names[$model];
4138 }
4139
4140 sub colorchannels {
4141 my ($self) = @_;
4142
4143 $self->_valid_image("colorchannels")
4144 or return;
4145
4146 return i_img_color_channels($self->{IMG});
4147 }
4148
4149 sub alphachannel {
4150 my ($self) = @_;
4151
4152 $self->_valid_image("alphachannel")
4153 or return;
4154
4155 return scalar(i_img_alpha_channel($self->{IMG}));
4156 }
4157
4158 # Get channel mask
4159
4160 sub getmask {
4161 my $self = shift;
4162
4163 $self->_valid_image("getmask")
4164 or return;
4165
4166 return i_img_getmask($self->{IMG});
4167 }
4168
4169 # Set channel mask
4170
4171 sub setmask {
4172 my $self = shift;
4173 my %opts = @_;
4174
4175 warnings::warnif("Imager::channelmask", "setmask: image channel masks are deprecated")
4176 if $] >= 5.014;
4177
4178 $self->_valid_image("setmask")
4179 or return;
4180
4181 unless (defined $opts{mask}) {
4182 $self->_set_error("mask parameter required");
4183 return;
4184 }
4185
4186 i_img_setmask( $self->{IMG} , $opts{mask} );
4187
4188 1;
4189 }
4190
4191 # Get number of colors in an image
4192
4193 sub getcolorcount {
4194 my $self=shift;
4195 my %opts=('maxcolors'=>2**30,@_);
4196
4197 $self->_valid_image("getcolorcount")
4198 or return;
4199
4200 my $rc=i_count_colors($self->{IMG},$opts{'maxcolors'});
4201 return ($rc==-1? undef : $rc);
4202 }
4203
4204 # Returns a reference to a hash. The keys are colour named (packed) and the
4205 # values are the number of pixels in this colour.
4206 sub getcolorusagehash {
4207 my $self = shift;
4208
4209 $self->_valid_image("getcolorusagehash")
4210 or return;
4211
4212 my %opts = ( maxcolors => 2**30, @_ );
4213 my $max_colors = $opts{maxcolors};
4214 unless (defined $max_colors && $max_colors > 0) {
4215 $self->_set_error('maxcolors must be a positive integer');
4216 return;
4217 }
4218
4219 my $channels= $self->getchannels;
4220 # We don't want to look at the alpha channel, because some gifs using it
4221 # doesn't define it for every colour (but only for some)
4222 $channels -= 1 if $channels == 2 or $channels == 4;
4223 my %color_use;
4224 my $height = $self->getheight;
4225 for my $y (0 .. $height - 1) {
4226 my $colors = $self->getsamples('y' => $y, channels => [ 0 .. $channels - 1 ]);
4227 while (length $colors) {
4228 $color_use{ substr($colors, 0, $channels, '') }++;
4229 }
4230 keys %color_use > $max_colors
4231 and return;
4232 }
4233 return \%color_use;
4234 }
4235
4236 # This will return a ordered array of the colour usage. Kind of the sorted
4237 # version of the values of the hash returned by getcolorusagehash.
4238 # You might want to add safety checks and change the names, etc...
4239 sub getcolorusage {
4240 my $self = shift;
4241
4242 $self->_valid_image("getcolorusage")
4243 or return;
4244
4245 my %opts = ( maxcolors => 2**30, @_ );
4246 my $max_colors = $opts{maxcolors};
4247 unless (defined $max_colors && $max_colors > 0) {
4248 $self->_set_error('maxcolors must be a positive integer');
4249 return;
4250 }
4251
4252 return i_get_anonymous_color_histo($self->{IMG}, $max_colors);
4253 }
4254
4255 # draw string to an image
4256
4257 sub string {
4258 my $self = shift;
4259
4260 $self->_valid_image("string")
4261 or return;
4262
4263 my %input=('x'=>0, 'y'=>0, @_);
4264 defined($input{string}) or $input{string} = $input{text};
4265
4266 unless(defined $input{string}) {
4267 $self->{ERRSTR}="missing required parameter 'string'";
4268 return;
4269 }
4270
4271 unless($input{font}) {
4272 $self->{ERRSTR}="missing required parameter 'font'";
4273 return;
4274 }
4275
4276 unless ($input{font}->draw(image=>$self, %input)) {
4277 return;
4278 }
4279
4280 return $self;
4281 }
4282
4283 sub align_string {
4284 my $self = shift;
4285
4286 my $img;
4287 if (ref $self) {
4288 $self->_valid_image("align_string")
4289 or return;
4290
4291 $img = $self;
4292 }
4293 else {
4294 $img = undef;
4295 }
4296
4297 my %input=('x'=>0, 'y'=>0, @_);
4298 defined $input{string}
4299 or $input{string} = $input{text};
4300
4301 unless(exists $input{string}) {
4302 $self->_set_error("missing required parameter 'string'");
4303 return;
4304 }
4305
4306 unless($input{font}) {
4307 $self->_set_error("missing required parameter 'font'");
4308 return;
4309 }
4310
4311 my @result;
4312 unless (@result = $input{font}->align(image=>$img, %input)) {
4313 return;
4314 }
4315
4316 return wantarray ? @result : $result[0];
4317 }
4318
4319 my @file_limit_names = qw/width height bytes/;
4320
4321 sub set_file_limits {
4322 shift;
4323
4324 my %opts = @_;
4325 my %values;
4326
4327 if ($opts{reset}) {
4328 @values{@file_limit_names} = (0) x @file_limit_names;
4329 }
4330 else {
4331 @values{@file_limit_names} = i_get_image_file_limits();
4332 }
4333
4334 for my $key (keys %values) {
4335 defined $opts{$key} and $values{$key} = $opts{$key};
4336 }
4337
4338 i_set_image_file_limits($values{width}, $values{height}, $values{bytes});
4339 }
4340
4341 sub get_file_limits {
4342 i_get_image_file_limits();
4343 }
4344
4345 my @check_args = qw(width height channels sample_size);
4346
4347 sub check_file_limits {
4348 my $class = shift;
4349
4350 my %opts =
4351 (
4352 channels => 3,
4353 sample_size => 1,
4354 @_,
4355 );
4356
4357 if ($opts{sample_size} && $opts{sample_size} eq 'float') {
4358 $opts{sample_size} = length(pack("d", 0));
4359 }
4360
4361 for my $name (@check_args) {
4362 unless (defined $opts{$name}) {
4363 $class->_set_error("check_file_limits: $name must be defined");
4364 return;
4365 }
4366 unless ($opts{$name} == int($opts{$name})) {
4367 $class->_set_error("check_file_limits: $name must be a positive integer");
4368 return;
4369 }
4370 }
4371
4372 my $result = i_int_check_image_file_limits(@opts{@check_args});
4373 unless ($result) {
4374 $class->_set_error($class->_error_as_msg());
4375 }
4376
4377 return $result;
4378 }
4379
4380 # Shortcuts that can be exported
4381
4382 sub newcolor { Imager::Color->new(@_); }
4383 sub newfont { Imager::Font->new(@_); }
4384 sub NCF {
4385 require Imager::Color::Float;
4386 return Imager::Color::Float->new(@_);
4387 }
4388
4389 *NC=*newcolour=*newcolor;
4390 *NF=*newfont;
4391
4392 *open=\&read;
4393 *circle=\&arc;
4394
4395
4396 #### Utility routines
4397
4398 sub errstr {
4399 ref $_[0] ? $_[0]->{ERRSTR} : $ERRSTR
4400 }
4401
4402 sub _set_error {
4403 my ($self, $msg) = @_;
4404
4405 if (ref $self) {
4406 $self->{ERRSTR} = $msg;
4407 }
4408 else {
4409 $ERRSTR = $msg;
4410 }
4411 return;
4412 }
4413
4414 # Default guess for the type of an image from extension
4415
4416 my @simple_types = qw(png tga gif raw ico cur xpm mng jng ilbm pcx psd eps webp xwd xpm dng ras qoi jxl);
4417
4418 my %ext_types =
4419 (
4420 ( map { $_ => $_ } @simple_types ),
4421 tiff => "tiff",
4422 tif => "tiff",
4423 pbm => "pnm",
4424 pgm => "pnm",
4425 ppm => "pnm",
4426 pnm => "pnm", # technically wrong, but historically it works in Imager
4427 jpeg => "jpeg",
4428 jpg => "jpeg",
4429 bmp => "bmp",
4430 dib => "bmp",
4431 rgb => "sgi",
4432 bw => "sgi",
4433 sgi => "sgi",
4434 fit => "fits",
4435 fits => "fits",
4436 rle => "utah",
4437 avifs => "avif", # AVIF image sequence
4438 avif => "avif",
4439 );
4440
4441 sub def_guess_type {
4442 my $name=lc(shift);
4443
4444 my ($ext) = $name =~ /\.([^.]+)$/
4445 or return;
4446
4447 my $type = $ext_types{$ext};
4448 unless ($type) {
4449 $type = $ext_types{lc $ext};
4450 }
4451
4452 if (!defined $type && $ext =~ /\A[a-zA-Z0-9_]{2,}\z/) {
4453 # maybe a reasonable assumption
4454 $type = lc $ext;
4455 }
4456
4457 return $type;
4458 }
4459
4460 sub add_type_extensions {
4461 my ($class, $type, @exts) = @_;
4462
4463 for my $ext (@exts) {
4464 exists $ext_types{lc $ext} or $ext_types{lc $ext} = lc $type;
4465 }
4466 1;
4467 }
4468
4469 sub combines {
4470 return @combine_types;
4471 }
4472
4473 # get the minimum of a list
4474
4475 sub _min {
4476 my $mx=shift;
4477 for(@_) { if ($_<$mx) { $mx=$_; }}
4478 return $mx;
4479 }
4480
4481 # get the maximum of a list
4482
4483 sub _max {
4484 my $mx=shift;
4485 for(@_) { if ($_>$mx) { $mx=$_; }}
4486 return $mx;
4487 }
4488
4489 # string stuff for iptc headers
4490
4491 sub _clean {
4492 my($str)=$_[0];
4493 $str = substr($str,3);
4494 $str =~ s/[\n\r]//g;
4495 $str =~ s/\s+/ /g;
4496 $str =~ s/^\s//;
4497 $str =~ s/\s$//;
4498 return $str;
4499 }
4500
4501 # A little hack to parse iptc headers.
4502
4503 sub parseiptc {
4504 my $self=shift;
4505 my(@sar,$item,@ar);
4506 my($caption,$photogr,$headln,$credit);
4507
4508 my $str=$self->{IPTCRAW};
4509
4510 defined $str
4511 or return;
4512
4513 @ar=split(/8BIM/,$str);
4514
4515 my $i=0;
4516 foreach (@ar) {
4517 if (/^\004\004/) {
4518 @sar=split(/\034\002/);
4519 foreach $item (@sar) {
4520 if ($item =~ m/^x/) {
4521 $caption = _clean($item);
4522 $i++;
4523 }
4524 if ($item =~ m/^P/) {
4525 $photogr = _clean($item);
4526 $i++;
4527 }
4528 if ($item =~ m/^i/) {
4529 $headln = _clean($item);
4530 $i++;
4531 }
4532 if ($item =~ m/^n/) {
4533 $credit = _clean($item);
4534 $i++;
4535 }
4536 }
4537 }
4538 }
4539 return (caption=>$caption,photogr=>$photogr,headln=>$headln,credit=>$credit);
4540 }
4541
4542 sub Inline {
4543 # Inline added a new argument at the beginning
4544 my $lang = $_[-1];
4545
4546 $lang eq 'C' || $lang eq 'CPP'
4547 or die "Only C or C++ (CPP) language supported";
4548
4549 require Imager::ExtUtils;
4550 return Imager::ExtUtils->inline_config;
4551 }
4552
4553 # threads shouldn't try to close raw Imager objects
4554 sub Imager::ImgRaw::CLONE_SKIP { 1 }
4555
4556 sub preload {
4557 # this serves two purposes:
4558 # - a class method to load the file support modules included with Imager
4559 # (or were included, once the library dependent modules are split out)
4560 # - something for Module::ScanDeps to analyze
4561 # https://rt.cpan.org/Ticket/Display.html?id=6566
4562 local $@;
4563 local @INC = @INC;
4564 pop @INC if $INC[-1] eq '.';
4565 eval { require Imager::File::GIF };
4566 eval { require Imager::File::JPEG };
4567 eval { require Imager::File::PNG };
4568 eval { require Imager::File::SGI };
4569 eval { require Imager::File::TIFF };
4570 eval { require Imager::File::ICO };
4571 eval { require Imager::Font::W32 };
4572 eval { require Imager::Font::FT2 };
4573 eval { require Imager::Font::T1 };
4574 eval { require Imager::Color::Table };
4575
4576 1;
4577 }
4578
4579 package Imager::IO;
4580 use IO::Seekable;
4581
4582 sub new_fh {
4583 my ($class, $fh) = @_;
4584
4585 if (tied(*$fh)) {
4586 return $class->new_cb
4587 (
4588 sub {
4589 local $\;
4590
4591 return print $fh $_[0];
4592 },
4593 sub {
4594 my $tmp;
4595 my $count = CORE::read $fh, $tmp, $_[1];
4596 defined $count
4597 or return undef;
4598 $count
4599 or return "";
4600 return $tmp;
4601 },
4602 sub {
4603 if ($_[1] != SEEK_CUR || $_[0] != 0) {
4604 unless (CORE::seek $fh, $_[0], $_[1]) {
4605 return -1;
4606 }
4607 }
4608
4609 return tell $fh;
4610 },
4611 undef,
4612 );
4613 }
4614 else {
4615 return $class->_new_perlio($fh);
4616 }
4617 }
4618
4619 # backward compatibility for %formats
4620 package Imager::FORMATS;
4621 use strict;
4622 use constant IX_FORMATS => 0;
4623 use constant IX_LIST => 1;
4624 use constant IX_INDEX => 2;
4625 use constant IX_CLASSES => 3;
4626
4627 sub TIEHASH {
4628 my ($class, $formats, $classes) = @_;
4629
4630 return bless [ $formats, [ ], 0, $classes ], $class;
4631 }
4632
4633 sub _check {
4634 my ($self, $key) = @_;
4635
4636 (my $file = $self->[IX_CLASSES]{$key} . ".pm") =~ s(::)(/)g;
4637 my $value;
4638 my $error;
4639 my $loaded = Imager::_load_file($file, \$error);
4640 if ($loaded) {
4641 $value = 1;
4642 }
4643 else {
4644 if ($error =~ /^Can't locate /) {
4645 $error = "Can't locate $file";
4646 }
4647 $reader_load_errors{$key} = $writer_load_errors{$key} = $error;
4648 $value = undef;
4649 }
4650 $self->[IX_FORMATS]{$key} = $value;
4651
4652 return $value;
4653 }
4654
4655 sub FETCH {
4656 my ($self, $key) = @_;
4657
4658 exists $self->[IX_FORMATS]{$key} and return $self->[IX_FORMATS]{$key};
4659
4660 $self->[IX_CLASSES]{$key} or return undef;
4661
4662 return $self->_check($key);
4663 }
4664
4665 sub STORE {
4666 die "%Imager::formats is not user monifiable";
4667 }
4668
4669 sub DELETE {
4670 die "%Imager::formats is not user monifiable";
4671 }
4672
4673 sub CLEAR {
4674 die "%Imager::formats is not user monifiable";
4675 }
4676
4677 sub EXISTS {
4678 my ($self, $key) = @_;
4679
4680 if (exists $self->[IX_FORMATS]{$key}) {
4681 my $value = $self->[IX_FORMATS]{$key}
4682 or return;
4683 return 1;
4684 }
4685
4686 $self->_check($key) or return 1==0;
4687
4688 return 1==1;
4689 }
4690
4691 sub FIRSTKEY {
4692 my ($self) = @_;
4693
4694 unless (@{$self->[IX_LIST]}) {
4695 # full populate it
4696 @{$self->[IX_LIST]} = grep $self->[IX_FORMATS]{$_},
4697 keys %{$self->[IX_FORMATS]};
4698
4699 for my $key (keys %{$self->[IX_CLASSES]}) {
4700 $self->[IX_FORMATS]{$key} and next;
4701 $self->_check($key)
4702 and push @{$self->[IX_LIST]}, $key;
4703 }
4704 }
4705
4706 @{$self->[IX_LIST]} or return;
4707 $self->[IX_INDEX] = 1;
4708 return $self->[IX_LIST][0];
4709 }
4710
4711 sub NEXTKEY {
4712 my ($self) = @_;
4713
4714 $self->[IX_INDEX] < @{$self->[IX_LIST]}
4715 or return;
4716
4717 return $self->[IX_LIST][$self->[IX_INDEX]++];
4718 }
4719
4720 sub SCALAR {
4721 my ($self) = @_;
4722
4723 return scalar @{$self->[IX_LIST]};
4724 }
4725
4726 1;
4727 __END__
4728
4729 =head1 NAME
4730
4731 Imager - Perl extension for Generating 24 bit Images
4732
4733 =head1 SYNOPSIS
4734
4735 # Thumbnail example
4736
4737 #!/usr/bin/perl -w
4738 use strict;
4739 use Imager;
4740
4741 die "Usage: thumbmake.pl filename\n" if !-f $ARGV[0];
4742 my $file = shift;
4743
4744 my $format;
4745
4746 # see Imager::Files for information on the read() method
4747 my $img = Imager->new(file=>$file)
4748 or die Imager->errstr();
4749
4750 $file =~ s/\.[^.]*$//;
4751
4752 # Create smaller version
4753 # documented in Imager::Transformations
4754 my $thumb = $img->scale(scalefactor=>.3);
4755
4756 # Autostretch individual channels
4757 $thumb->filter(type=>'autolevels');
4758
4759 # try to save in one of these formats
4760 SAVE:
4761
4762 for $format ( qw( png gif jpeg tiff ppm ) ) {
4763 # Check if given format is supported
4764 if ($Imager::formats{$format}) {
4765 $file.="_low.$format";
4766 print "Storing image as: $file\n";
4767 # documented in Imager::Files
4768 $thumb->write(file=>$file) or
4769 die $thumb->errstr;
4770 last SAVE;
4771 }
4772 }
4773
4774 =head1 DESCRIPTION
4775
4776 Imager is a module for creating and altering images. It can read and
4777 write various image formats, draw primitive shapes like lines,and
4778 polygons, blend multiple images together in various ways, scale, crop,
4779 render text and more.
4780
4781 =head2 Overview of documentation
4782
4783 =over
4784
4785 =item *
4786
4787 Imager - This document - Synopsis, Example, Table of Contents and
4788 Overview.
4789
4790 =item *
4791
4792 L<Imager::Install> - installation notes for Imager.
4793
4794 =item *
4795
4796 L<Imager::Tutorial> - a brief introduction to Imager.
4797
4798 =item *
4799
4800 L<Imager::Cookbook> - how to do various things with Imager.
4801
4802 =item *
4803
4804 L<Imager::ImageTypes> - Basics of constructing image objects with
4805 C<new()>: Direct type/virtual images, RGB(A)/paletted images,
4806 8/16/double bits/channel, color maps, channel masks, image tags, color
4807 quantization. Also discusses basic image information methods.
4808
4809 =item *
4810
4811 L<Imager::Files> - IO interaction, reading/writing images, format
4812 specific tags.
4813
4814 =item *
4815
4816 L<Imager::Draw> - Drawing Primitives, lines, boxes, circles, arcs,
4817 flood fill.
4818
4819 =item *
4820
4821 L<Imager::Color> - Color specification.
4822
4823 =item *
4824
4825 L<Imager::Fill> - Fill pattern specification.
4826
4827 =item *
4828
4829 L<Imager::Font> - General font rendering, bounding boxes and font
4830 metrics.
4831
4832 =item *
4833
4834 L<Imager::Transformations> - Copying, scaling, cropping, flipping,
4835 blending, pasting, convert and map.
4836
4837 =item *
4838
4839 L<Imager::Engines> - Programmable transformations through
4840 C<transform()>, C<transform2()> and C<matrix_transform()>.
4841
4842 =item *
4843
4844 L<Imager::Filters> - Filters, sharpen, blur, noise, convolve etc. and
4845 filter plug-ins.
4846
4847 =item *
4848
4849 L<Imager::Expr> - Expressions for evaluation engine used by
4850 transform2().
4851
4852 =item *
4853
4854 L<Imager::Matrix2d> - Helper class for affine transformations.
4855
4856 =item *
4857
4858 L<Imager::Fountain> - Helper for making gradient profiles.
4859
4860 =item *
4861
4862 L<Imager::IO> - Imager I/O abstraction.
4863
4864 =item *
4865
4866 L<Imager::API> - using Imager's C API
4867
4868 =item *
4869
4870 L<Imager::APIRef> - API function reference
4871
4872 =item *
4873
4874 L<Imager::Inline> - using Imager's C API from Inline::C
4875
4876 =item *
4877
4878 L<Imager::ExtUtils> - tools to get access to Imager's C API.
4879
4880 =item *
4881
4882 L<Imager::Security> - brief security notes.
4883
4884 =item *
4885
4886 L<Imager::Threads> - brief information on working with threads.
4887
4888 =back
4889
4890 =head2 Basic Overview
4891
4892 An Image object is created with C<$img = Imager-E<gt>new()>.
4893 Examples:
4894
4895 $img=Imager->new(); # create empty image
4896 $img->read(file=>'lena.png',type=>'png') or # read image from file
4897 die $img->errstr(); # give an explanation
4898 # if something failed
4899
4900 or if you want to create an empty image:
4901
4902 $img=Imager->new(xsize=>400,ysize=>300,channels=>4);
4903
4904 This example creates a completely black image of width 400 and height
4905 300 and 4 channels.
4906
4907 =head1 ERROR HANDLING
4908
4909 In general a method will return false when it fails, if it does use
4910 the C<errstr()> method to find out why:
4911
4912 =over
4913
4914 =item errstr()
4915
4916 Returns the last error message in that context.
4917
4918 If the last error you received was from calling an object method, such
4919 as read, call errstr() as an object method to find out why:
4920
4921 my $image = Imager->new;
4922 $image->read(file => 'somefile.gif')
4923 or die $image->errstr;
4924
4925 If it was a class method then call errstr() as a class method:
4926
4927 my @imgs = Imager->read_multi(file => 'somefile.gif')
4928 or die Imager->errstr;
4929
4930 Note that in some cases object methods are implemented in terms of
4931 class methods so a failing object method may set both.
4932
4933 =back
4934
4935 The C<Imager-E<gt>new> method is described in detail in
4936 L<Imager::ImageTypes>.
4937
4938 =head1 METHOD INDEX
4939
4940 Where to find information on methods for Imager class objects.
4941
4942 addcolors() - L<Imager::ImageTypes/addcolors()> - add colors to a
4943 paletted image
4944
4945 add_file_magic() - L<Imager::Files/add_file_magic()> - add magic to
4946 Imager's file type detector.
4947
4948 addtag() - L<Imager::ImageTypes/addtag()> - add image tags
4949
4950 add_type_extensions() - L<Imager::Files/add_file_magic()> - add magic
4951 for new image file types.
4952
4953 L<Imager::Files/add_type_extensions($type, $ext, ...)> - add extensions for
4954 new image file types.
4955
4956 align_string() - L<Imager::Draw/align_string()> - draw text aligned on a
4957 point
4958
4959 alphachannel() - L<Imager::ImageTypes/alphachannel()> - return the
4960 channel index of the alpha channel (if any).
4961
4962 arc() - L<Imager::Draw/arc()> - draw a filled arc
4963
4964 bits() - L<Imager::ImageTypes/bits()> - number of bits per sample for the
4965 image
4966
4967 box() - L<Imager::Draw/box()> - draw a filled or outline box.
4968
4969 check_file_limits() - L<Imager::Files/check_file_limits()>
4970
4971 circle() - L<Imager::Draw/circle()> - draw a filled circle
4972
4973 close_log() - L<Imager::ImageTypes/close_log()> - close the Imager
4974 debugging log.
4975
4976 colorchannels() - L<Imager::ImageTypes/colorchannels()> - the number
4977 of channels used for color.
4978
4979 colorcount() - L<Imager::ImageTypes/colorcount()> - the number of
4980 colors in an image's palette (paletted images only)
4981
4982 colormodel() - L<Imager::ImageTypes/colorcount()> - how color is
4983 represented.
4984
4985 combine() - L<Imager::Transformations/combine()> - combine channels
4986 from one or more images.
4987
4988 combines() - L<Imager::Draw/combines()> - return a list of the
4989 different combine type keywords
4990
4991 compose() - L<Imager::Transformations/compose()> - compose one image
4992 over another.
4993
4994 convert() - L<Imager::Transformations/convert()> - transform the color
4995 space
4996
4997 copy() - L<Imager::Transformations/copy()> - make a duplicate of an
4998 image
4999
5000 crop() - L<Imager::Transformations/crop()> - extract part of an image
5001
5002 def_guess_type() - L<Imager::Files/def_guess_type()> - default function
5003 used to guess the output file format based on the output file name
5004
5005 deltag() - L<Imager::ImageTypes/deltag()> - delete image tags
5006
5007 difference() - L<Imager::Filters/difference()> - produce a difference
5008 images from two input images.
5009
5010 errstr() - L</errstr()> - the error from the last failed operation.
5011
5012 filter() - L<Imager::Filters/filter()> - image filtering
5013
5014 findcolor() - L<Imager::ImageTypes/findcolor()> - search the image
5015 palette, if it has one
5016
5017 flip() - L<Imager::Transformations/flip()> - flip an image, vertically,
5018 horizontally
5019
5020 flood_fill() - L<Imager::Draw/flood_fill()> - fill an enclosed or same
5021 color area
5022
5023 getchannels() - L<Imager::ImageTypes/getchannels()> - the number of
5024 samples per pixel for an image
5025
5026 getcolorcount() - L<Imager::ImageTypes/getcolorcount()> - the number of
5027 different colors used by an image (works for direct color images)
5028
5029 getcolors() - L<Imager::ImageTypes/getcolors()> - get colors from the image
5030 palette, if it has one
5031
5032 getcolorusage() - L<Imager::ImageTypes/getcolorusage()>
5033
5034 getcolorusagehash() - L<Imager::ImageTypes/getcolorusagehash()>
5035
5036 get_file_limits() - L<Imager::Files/get_file_limits()>
5037
5038 getheight() - L<Imager::ImageTypes/getheight()> - height of the image in
5039 pixels
5040
5041 getmask() - L<Imager::ImageTypes/getmask()> - write mask for the image
5042
5043 getpixel() - L<Imager::Draw/getpixel()> - retrieve one or more pixel
5044 colors
5045
5046 getsamples() - L<Imager::Draw/getsamples()> - retrieve samples from a
5047 row or partial row of pixels.
5048
5049 getscanline() - L<Imager::Draw/getscanline()> - retrieve colors for a
5050 row or partial row of pixels.
5051
5052 getwidth() - L<Imager::ImageTypes/getwidth()> - width of the image in
5053 pixels.
5054
5055 img_set() - L<Imager::ImageTypes/img_set()> - re-use an Imager object
5056 for a new image.
5057
5058 init() - L<Imager::ImageTypes/init()>
5059
5060 is_bilevel() - L<Imager::ImageTypes/is_bilevel()> - returns whether
5061 image write functions should write the image in their bilevel (blank
5062 and white, no gray levels) format
5063
5064 is_logging() - L<Imager::ImageTypes/is_logging()> - test if the debug
5065 log is active.
5066
5067 line() - L<Imager::Draw/line()> - draw an interval
5068
5069 load_plugin() - L<Imager::Filters/load_plugin()>
5070
5071 log() - L<Imager::ImageTypes/log()> - send a message to the debugging
5072 log.
5073
5074 make_palette() - L<Imager::ImageTypes/make_palette()> - produce a
5075 color palette from one or more input images.
5076
5077 map() - L<Imager::Transformations/map()> - remap color
5078 channel values
5079
5080 masked() - L<Imager::ImageTypes/masked()> - make a masked image
5081
5082 matrix_transform() - L<Imager::Engines/matrix_transform()>
5083
5084 maxcolors() - L<Imager::ImageTypes/maxcolors()>
5085
5086 NC() - L<Imager::Handy/NC()>
5087
5088 NCF() - L<Imager::Handy/NCF()>
5089
5090 new() - L<Imager::ImageTypes/new()>
5091
5092 newcolor() - L<Imager::Handy/newcolor()>
5093
5094 newcolour() - L<Imager::Handy/newcolour()>
5095
5096 newfont() - L<Imager::Handy/newfont()>
5097
5098 NF() - L<Imager::Handy/NF()>
5099
5100 open() - L<Imager::Files/read()> - an alias for read()
5101
5102 open_log() - L<Imager::ImageTypes/open_log()> - open the debug log.
5103
5104 =for stopwords IPTC
5105
5106 parseiptc() - L<Imager::Files/parseiptc()> - parse IPTC data from a JPEG
5107 image
5108
5109 paste() - L<Imager::Transformations/paste()> - draw an image onto an
5110 image
5111
5112 polygon() - L<Imager::Draw/polygon()>
5113
5114 polyline() - L<Imager::Draw/polyline()>
5115
5116 polypolygon() - L<Imager::Draw/polypolygon()>
5117
5118 preload() - L<Imager::Files/preload()>
5119
5120 read() - L<Imager::Files/read()> - read a single image from an image file
5121
5122 read_multi() - L<Imager::Files/read_multi()> - read multiple images from an image
5123 file
5124
5125 read_types() - L<Imager::Files/read_types()> - list image types Imager
5126 can read.
5127
5128 register_filter() - L<Imager::Filters/register_filter()>
5129
5130 register_reader() - L<Imager::Files/register_reader()>
5131
5132 register_writer() - L<Imager::Files/register_writer()>
5133
5134 rgb_difference() - L<Imager::Filters/rgb_difference()> - produce a difference
5135 images from two input images.
5136
5137 rotate() - L<Imager::Transformations/rotate()>
5138
5139 rubthrough() - L<Imager::Transformations/rubthrough()> - draw an image
5140 onto an image and use the alpha channel
5141
5142 scale() - L<Imager::Transformations/scale()>
5143
5144 scale_calculate() - L<Imager::Transformations/scale_calculate()>
5145
5146 scaleX() - L<Imager::Transformations/scaleX()>
5147
5148 scaleY() - L<Imager::Transformations/scaleY()>
5149
5150 setcolors() - L<Imager::ImageTypes/setcolors()> - set palette colors
5151 in a paletted image
5152
5153 set_file_limits() - L<Imager::Files/set_file_limits()>
5154
5155 setmask() - L<Imager::ImageTypes/setmask()>
5156
5157 setpixel() - L<Imager::Draw/setpixel()>
5158
5159 setsamples() - L<Imager::Draw/setsamples()>
5160
5161 setscanline() - L<Imager::Draw/setscanline()>
5162
5163 settag() - L<Imager::ImageTypes/settag()>
5164
5165 string() - L<Imager::Draw/string()> - draw text on an image
5166
5167 tags() - L<Imager::ImageTypes/tags()> - fetch image tags
5168
5169 to_paletted() - L<Imager::ImageTypes/to_paletted()>
5170
5171 to_rgb16() - L<Imager::ImageTypes/to_rgb16()>
5172
5173 to_rgb8() - L<Imager::ImageTypes/to_rgb8()>
5174
5175 to_rgb_double() - L<Imager::ImageTypes/to_rgb_double()> - convert to
5176 double per sample image.
5177
5178 transform() - L<Imager::Engines/"transform()">
5179
5180 transform2() - L<Imager::Engines/"transform2()">
5181
5182 trim() - L<Imager::Transformations/trim()> - return a cropped image
5183 based on border transparency or border colors.
5184
5185 trim_rect() - L<Imager::Transformations/trim_rect()> - return how much
5186 trim() would remove.
5187
5188 type() - L<Imager::ImageTypes/type()> - type of image (direct vs paletted)
5189
5190 unload_plugin() - L<Imager::Filters/unload_plugin()>
5191
5192 virtual() - L<Imager::ImageTypes/virtual()> - whether the image has it's own
5193 data
5194
5195 write() - L<Imager::Files/write()> - write an image to a file
5196
5197 write_multi() - L<Imager::Files/write_multi()> - write multiple image to an image
5198 file.
5199
5200 write_types() - L<Imager::Files/read_types()> - list image types Imager
5201 can write.
5202
5203 =head1 CONCEPT INDEX
5204
5205 animated GIF - L<Imager::Files/"Writing an animated GIF">
5206
5207 aspect ratio - C<i_xres>, C<i_yres>, C<i_aspect_only> in
5208 L<Imager::ImageTypes/"Common Tags">.
5209
5210 blend - alpha blending one image onto another
5211 L<Imager::Transformations/rubthrough()>
5212
5213 blur - L<< Imager::Filters/C<gaussian> >>, L<< Imager::Filters/C<conv> >>
5214
5215 boxes, drawing - L<Imager::Draw/box()>
5216
5217 changes between image - L<Imager::Filters/"Image Difference">
5218
5219 channels, combine into one image - L<Imager::Transformations/combine()>
5220
5221 color - L<Imager::Color>
5222
5223 color names - L<Imager::Color>, L<Imager::Color::Table>
5224
5225 combine modes - L<Imager::Draw/"Combine Types">
5226
5227 compare images - L<Imager::Filters/"Image Difference">
5228
5229 contrast - L<< Imager::Filters/C<contrast> >>, L<< Imager::Filters/C<autolevels> >>
5230
5231 convolution - L<< Imager::Filters/C<conv> >>
5232
5233 cropping - L<Imager::Transformations/crop()>, L<Imager::Transformations/trim()>
5234
5235 CUR files - L<Imager::Files/"ICO (Microsoft Windows Icon) and CUR (Microsoft Windows Cursor)">
5236
5237 C<diff> images - L<Imager::Filters/"Image Difference">
5238
5239 dpi - C<i_xres>, C<i_yres> in L<Imager::ImageTypes/"Common Tags">,
5240 L<Imager::Cookbook/"Image spatial resolution">
5241
5242 drawing boxes - L<Imager::Draw/box()>
5243
5244 drawing lines - L<Imager::Draw/line()>
5245
5246 drawing text - L<Imager::Draw/string()>, L<Imager::Draw/align_string()>
5247
5248 error message - L</"ERROR HANDLING">
5249
5250 files, font - L<Imager::Font>
5251
5252 files, image - L<Imager::Files>
5253
5254 filling, types of fill - L<Imager::Fill>
5255
5256 filling, boxes - L<Imager::Draw/box()>
5257
5258 filling, flood fill - L<Imager::Draw/flood_fill()>
5259
5260 flood fill - L<Imager::Draw/flood_fill()>
5261
5262 fonts - L<Imager::Font>
5263
5264 fonts, drawing with - L<Imager::Draw/string()>,
5265 L<Imager::Draw/align_string()>, L<Imager::Font::Wrap>
5266
5267 fonts, metrics - L<Imager::Font/bounding_box()>, L<Imager::Font::BBox>
5268
5269 fonts, multiple master - L<Imager::Font/"MULTIPLE MASTER FONTS">
5270
5271 fountain fill - L<Imager::Fill/"Fountain fills">,
5272 L<< Imager::Filters/C<fountain> >>, L<Imager::Fountain>,
5273 L<< Imager::Filters/C<gradgen> >>
5274
5275 GIF files - L<Imager::Files/"GIF">
5276
5277 GIF files, animated - L<Imager::Files/"Writing an animated GIF">
5278
5279 gradient fill - L<Imager::Fill/"Fountain fills">,
5280 L<< Imager::Filters/C<fountain> >>, L<Imager::Fountain>,
5281 L<< Imager::Filters/C<gradgen> >>
5282
5283 gray scale, convert image to - L<Imager::Transformations/convert()>
5284
5285 gaussian blur - L<< Imager::Filters/C<gaussian> >>, L<< Imager::Filters/C<gaussian2> >>
5286
5287 hatch fills - L<Imager::Fill/"Hatched fills">
5288
5289 ICO files - L<Imager::Files/"ICO (Microsoft Windows Icon) and CUR (Microsoft Windows Cursor)">
5290
5291 invert image - L<< Imager::Filters/C<hardinvert> >>,
5292 L<< Imager::Filters/C<hardinvertall> >>
5293
5294 JPEG - L<Imager::Files/"JPEG">
5295
5296 limiting image sizes - L<Imager::Files/"Limiting the sizes of images you read">
5297
5298 lines, drawing - L<Imager::Draw/line()>
5299
5300 matrix - L<Imager::Matrix2d>,
5301 L<Imager::Engines/"Matrix Transformations">,
5302 L<Imager::Font/transform()>
5303
5304 metadata, image - L<Imager::ImageTypes/"Tags">, L<Image::ExifTool>
5305
5306 mosaic - L<< Imager::Filters/C<mosaic> >>
5307
5308 noise, filter - L<< Imager::Filters/C<noise> >>
5309
5310 noise, rendered - L<< Imager::Filters/C<turbnoise> >>,
5311 L<< Imager::Filters/C<radnoise> >>
5312
5313 paste - L<Imager::Transformations/paste()>,
5314 L<Imager::Transformations/rubthrough()>
5315
5316 pseudo-color image - L<Imager::ImageTypes/to_paletted()>,
5317 L<Imager::ImageTypes/new()>
5318
5319 =for stopwords posterize
5320
5321 posterize - L<< Imager::Filters/C<postlevels> >>
5322
5323 PNG files - L<Imager::Files>, L<Imager::Files/"PNG">
5324
5325 PNM - L<Imager::Files/"PNM (Portable aNy Map)">
5326
5327 rectangles, drawing - L<Imager::Draw/box()>
5328
5329 resizing an image - L<Imager::Transformations/scale()>,
5330 L<Imager::Transformations/crop()>
5331
5332 RGB (SGI) files - L<Imager::Files/"SGI (RGB, BW)">
5333
5334 saving an image - L<Imager::Files>
5335
5336 scaling - L<Imager::Transformations/scale()>
5337
5338 security - L<Imager::Security>
5339
5340 SGI files - L<Imager::Files/"SGI (RGB, BW)">
5341
5342 sharpen - L<< Imager::Filters/C<unsharpmask> >>, L<< Imager::Filters/C<conv> >>
5343
5344 size, image - L<Imager::ImageTypes/getwidth()>,
5345 L<Imager::ImageTypes/getheight()>
5346
5347 size, text - L<Imager::Font/bounding_box()>
5348
5349 tags, image metadata - L<Imager::ImageTypes/"Tags">
5350
5351 text, drawing - L<Imager::Draw/string()>, L<Imager::Draw/align_string()>,
5352 L<Imager::Font::Wrap>
5353
5354 text, wrapping text in an area - L<Imager::Font::Wrap>
5355
5356 text, measuring - L<Imager::Font/bounding_box()>, L<Imager::Font::BBox>
5357
5358 threads - L<Imager::Threads>
5359
5360 tiles, color - L<< Imager::Filters/C<mosaic> >>
5361
5362 transparent images - L<Imager::ImageTypes>,
5363 L<Imager::Cookbook/"Transparent PNG">
5364
5365 =for stopwords unsharp
5366
5367 unsharp mask - L<< Imager::Filters/C<unsharpmask> >>
5368
5369 watermark - L<< Imager::Filters/C<watermark> >>
5370
5371 writing an image to a file - L<Imager::Files>
5372
5373 =head1 SUPPORT
5374
5375 The best place to get help with Imager is the mailing list.
5376
5377 To subscribe send a message with C<subscribe> in the body to:
5378
5379 [email protected]
5380
5381 or use the form at:
5382
5383 =over
5384
5385 L<http://www.molar.is/en/lists/imager-devel/>
5386
5387 =back
5388
5389 where you can also find the mailing list archive.
5390
5391 You can report bugs either via github at:
5392
5393 =over
5394
5395 L<https://github.com/tonycoz/imager/issues>
5396
5397 =back
5398
5399 or at:
5400
5401 =over
5402
5403 L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Imager>
5404
5405 =back
5406
5407 or by sending an email to:
5408
5409 =over
5410
5411 [email protected]
5412
5413 =back
5414
5415 Please remember to include the versions of Imager, perl, supporting
5416 libraries, and any relevant code. If you have specific images that
5417 cause the problems, please include those too.
5418
5419 =head1 CONTRIBUTING TO IMAGER
5420
5421 =head2 Feedback
5422
5423 I like feedback.
5424
5425 You can send email to the maintainer below.
5426
5427 If you send me a bug report via email, it will be copied to Request
5428 Tracker.
5429
5430 =head2 Patches
5431
5432 I accept patches, preferably against the master branch in git. Please
5433 include an explanation of the reason for why the patch is needed or
5434 useful.
5435
5436 Your patch should include regression tests where possible, otherwise
5437 it will be delayed until I get a chance to write them.
5438
5439 To browse Imager's git repository:
5440
5441 https://github.com/tonycoz/imager.git
5442
5443 To clone:
5444
5445 git clone git://github.com/tonycoz/imager.git
5446
5447 Or you can create a fork as usual on github and submit a github pull
5448 request.
5449
5450 Patches can either be submitted as a github pull request or by using
5451 C<git format-patch>, for example, if you made your changes in a branch
5452 from master you might do:
5453
5454 git format-patch -k --stdout master >my-patch.txt
5455
5456 and then attach that to your bug report, either by adding it as an
5457 attachment in your email client, or by using the Request Tracker
5458 attachment mechanism.
5459
5460 =head1 AUTHOR
5461
5462 Tony Cook <[email protected]> is the current maintainer for Imager.
5463
5464 Arnar M. Hrafnkelsson is the original author of Imager.
5465
5466 Many others have contributed to Imager, please see the C<README> for a
5467 complete list.
5468
5469 =head1 LICENSE
5470
5471 Imager is licensed under the same terms as perl itself.
5472
5473 =for stopwords
5474 makeblendedfont Fontforge
5475
5476 A test font, generated by the Debian packaged Fontforge,
5477 F<FT2/fontfiles/MMOne.pfb>, contains a Postscript operator definition
5478 copyrighted by Adobe. See F<adobe.txt> in the source for license
5479 information.
5480
5481 =head1 SEE ALSO
5482
5483 L<perl>(1), L<Imager::ImageTypes>(3), L<Imager::Files>(3),
5484 L<Imager::Draw>(3), L<Imager::Color>(3), L<Imager::Fill>(3),
5485 L<Imager::Font>(3), L<Imager::Transformations>(3),
5486 L<Imager::Engines>(3), L<Imager::Filters>(3), L<Imager::Expr>(3),
5487 L<Imager::Matrix2d>(3), L<Imager::Fountain>(3)
5488
5489 L<http://imager.perl.org/>
5490
5491 L<Affix::Infix2Postfix>(3), L<Parse::RecDescent>(3)
5492
5493 Other perl imaging modules include:
5494
5495 L<GD>(3), L<Image::Magick>(3),
5496 L<Graphics::Magick|http://www.graphicsmagick.org/perl.html>(3),
5497 L<Prima::Image>, L<IPA>.
5498
5499 For manipulating image metadata see L<Image::ExifTool>.
5500
5501 If you're trying to use Imager for array processing, you should
5502 probably using L<PDL>.
5503
5504 =cut