-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathImager.pm
5501 lines (4439 loc) · 129 KB
/
Imager.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Imager;
use 5.006;
use strict;
use Scalar::Util;
use Imager::Color;
use Imager::Color::Float;
use Imager::Font;
use Imager::TrimColorList;
use if $] >= 5.014, "warnings::register" => qw(tagcodes channelmask);
our $ERRSTR;
our @EXPORT_OK = qw(
init
init_log
DSO_open
DSO_close
DSO_funclist
DSO_call
load_plugin
unload_plugin
i_list_formats
i_color_new
i_color_set
i_color_info
i_img_info
i_img_setmask
i_img_getmask
i_line
i_line_aa
i_box
i_box_filled
i_arc
i_circle_aa
i_bezier_multi
i_poly_aa
i_poly_aa_cfill
i_copyto
i_rubthru
i_scaleaxis
i_scale_nn
i_haar
i_count_colors
i_gaussian
i_conv
i_convert
i_map
i_img_diff
i_tt_set_aa
i_tt_cp
i_tt_text
i_tt_bbox
i_readpnm_wiol
i_writeppm_wiol
i_readraw_wiol
i_writeraw_wiol
i_contrast
i_hardinvert
i_noise
i_bumpmap
i_postlevels
i_mosaic
i_watermark
malloc_state
list_formats
i_gifquant
newfont
newcolor
newcolour
NC
NF
NCF
);
our @EXPORT=qw(
);
our %EXPORT_TAGS=
(handy => [qw(
newfont
newcolor
NF
NC
NCF
)],
all => [@EXPORT_OK],
default => [qw(
load_plugin
unload_plugin
)]);
# registered file readers
my %readers;
# registered file writers
my %writers;
# modules we attempted to autoload
my %attempted_to_load;
# errors from loading files
my %file_load_errors;
# what happened when we tried to load
my %reader_load_errors;
my %writer_load_errors;
# library keys that are image file formats
my %file_formats = map { $_ => 1 } qw/tiff pnm gif png jpeg raw bmp tga/;
# image pixel combine types
my @combine_types =
qw/none normal multiply dissolve add subtract diff lighten darken
hue saturation value color/;
my %combine_types;
@combine_types{@combine_types} = 0 .. $#combine_types;
$combine_types{mult} = $combine_types{multiply};
$combine_types{'sub'} = $combine_types{subtract};
$combine_types{sat} = $combine_types{saturation};
# this will be used to store global defaults at some point
my %defaults;
our $VERSION;
BEGIN {
require Exporter;
my $ex_version = eval $Exporter::VERSION;
if ($ex_version < 5.57) {
our @ISA = qw(Exporter);
}
$VERSION = '1.025';
require XSLoader;
XSLoader::load(Imager => $VERSION);
}
my %formats_low;
my %format_classes =
(
png => "Imager::File::PNG",
gif => "Imager::File::GIF",
tiff => "Imager::File::TIFF",
jpeg => "Imager::File::JPEG",
w32 => "Imager::Font::W32",
ft2 => "Imager::Font::FT2",
t1 => "Imager::Font::T1",
);
our %formats;
tie %formats, "Imager::FORMATS", \%formats_low, \%format_classes;
our %filters;
our $DEBUG;
our %OPCODES;
our $FORMATGUESS;
our $warn_obsolete;
BEGIN {
for(i_list_formats()) { $formats_low{$_}++; }
%OPCODES=(Add=>[0],Sub=>[1],Mult=>[2],Div=>[3],Parm=>[4],'sin'=>[5],'cos'=>[6],'x'=>[4,0],'y'=>[4,1]);
$DEBUG=0;
# the members of the subhashes under %filters are:
# callseq - a list of the parameters to the underlying filter in the
# order they are passed
# callsub - a code ref that takes a named parameter list and calls the
# underlying filter
# defaults - a hash of default values
# names - defines names for value of given parameters so if the names
# field is foo=> { bar=>1 }, and the user supplies "bar" as the
# foo parameter, the filter will receive 1 for the foo
# parameter
$filters{contrast}={
callseq => ['image','intensity'],
callsub => sub { my %hsh=@_; i_contrast($hsh{image},$hsh{intensity}); }
};
$filters{noise} ={
callseq => ['image', 'amount', 'subtype'],
defaults => { amount=>3,subtype=>0 },
callsub => sub { my %hsh=@_; i_noise($hsh{image},$hsh{amount},$hsh{subtype}); }
};
$filters{hardinvert} ={
callseq => ['image'],
defaults => { },
callsub => sub { my %hsh=@_; i_hardinvert($hsh{image}); }
};
$filters{hardinvertall} =
{
callseq => ['image'],
defaults => { },
callsub => sub { my %hsh=@_; i_hardinvertall($hsh{image}); }
};
$filters{autolevels_skew} ={
callseq => ['image','lsat','usat','skew'],
defaults => { lsat=>0.1,usat=>0.1,skew=>0.0 },
callsub => sub { my %hsh=@_; i_autolevels($hsh{image},$hsh{lsat},$hsh{usat},$hsh{skew}); }
};
$filters{autolevels} ={
callseq => ['image','lsat','usat'],
defaults => { lsat=>0.1,usat=>0.1 },
callsub => sub { my %hsh=@_; i_autolevels_mono($hsh{image},$hsh{lsat},$hsh{usat}); }
};
$filters{turbnoise} ={
callseq => ['image'],
defaults => { xo=>0.0,yo=>0.0,scale=>10.0 },
callsub => sub { my %hsh=@_; i_turbnoise($hsh{image},$hsh{xo},$hsh{yo},$hsh{scale}); }
};
$filters{radnoise} ={
callseq => ['image'],
defaults => { xo=>100,yo=>100,ascale=>17.0,rscale=>0.02 },
callsub => sub { my %hsh=@_; i_radnoise($hsh{image},$hsh{xo},$hsh{yo},$hsh{rscale},$hsh{ascale}); }
};
$filters{conv} =
{
callseq => ['image', 'coef'],
defaults => { },
callsub =>
sub {
my %hsh=@_;
i_conv($hsh{image},$hsh{coef})
or die Imager->_error_as_msg() . "\n";
}
};
$filters{gradgen} =
{
callseq => ['image', 'xo', 'yo', 'colors', 'dist'],
defaults => { dist => 0 },
callsub =>
sub {
my %hsh=@_;
my @colors = @{$hsh{colors}};
$_ = _color($_)
for @colors;
i_gradgen($hsh{image}, $hsh{xo}, $hsh{yo}, \@colors, $hsh{dist});
}
};
$filters{nearest_color} =
{
callseq => ['image', 'xo', 'yo', 'colors', 'dist'],
defaults => { },
callsub =>
sub {
my %hsh=@_;
# make sure the segments are specified with colors
my @colors;
for my $color (@{$hsh{colors}}) {
my $new_color = _color($color)
or die $Imager::ERRSTR."\n";
push @colors, $new_color;
}
i_nearest_color($hsh{image}, $hsh{xo}, $hsh{yo}, \@colors,
$hsh{dist})
or die Imager->_error_as_msg() . "\n";
},
};
$filters{gaussian} = {
callseq => [ 'image', 'stddev' ],
defaults => { },
callsub => sub { my %hsh = @_; i_gaussian($hsh{image}, $hsh{stddev}); },
};
$filters{gaussian2} = {
callseq => [ 'image', 'stddevX', 'stddevY' ],
defaults => { },
callsub => sub { my %hsh = @_; i_gaussian2($hsh{image}, $hsh{stddevX}, $hsh{stddevY}); },
};
$filters{mosaic} =
{
callseq => [ qw(image size) ],
defaults => { size => 20 },
callsub => sub { my %hsh = @_; i_mosaic($hsh{image}, $hsh{size}) },
};
$filters{bumpmap} =
{
callseq => [ qw(image bump elevation lightx lighty st) ],
defaults => { elevation=>0, st=> 2 },
callsub => sub {
my %hsh = @_;
i_bumpmap($hsh{image}, $hsh{bump}{IMG}, $hsh{elevation},
$hsh{lightx}, $hsh{lighty}, $hsh{st});
},
};
$filters{bumpmap_complex} =
{
callseq => [ qw(image bump channel tx ty Lx Ly Lz cd cs n Ia Il Is) ],
defaults => {
channel => 0,
tx => 0,
ty => 0,
Lx => 0.2,
Ly => 0.4,
Lz => -1.0,
cd => 1.0,
cs => 40,
n => 1.3,
Ia => [0,0,0],
Il => [255,255,255],
Is => [255,255,255],
},
callsub => sub {
my %hsh = @_;
for my $cname (qw/Ia Il Is/) {
my $old = $hsh{$cname};
my $new_color = _color($old)
or die $Imager::ERRSTR, "\n";
$hsh{$cname} = $new_color;
}
i_bumpmap_complex($hsh{image}, $hsh{bump}{IMG}, $hsh{channel},
$hsh{tx}, $hsh{ty}, $hsh{Lx}, $hsh{Ly}, $hsh{Lz},
$hsh{cd}, $hsh{cs}, $hsh{n}, $hsh{Ia}, $hsh{Il},
$hsh{Is});
},
};
$filters{postlevels} =
{
callseq => [ qw(image levels) ],
defaults => { levels => 10 },
callsub => sub { my %hsh = @_; i_postlevels($hsh{image}, $hsh{levels}); },
};
$filters{watermark} =
{
callseq => [ qw(image wmark tx ty pixdiff) ],
defaults => { pixdiff=>10, tx=>0, ty=>0 },
callsub =>
sub {
my %hsh = @_;
i_watermark($hsh{image}, $hsh{wmark}{IMG}, $hsh{tx}, $hsh{ty},
$hsh{pixdiff});
},
};
$filters{fountain} =
{
callseq => [ qw(image xa ya xb yb ftype repeat combine super_sample ssample_param segments) ],
names => {
ftype => { linear => 0,
bilinear => 1,
radial => 2,
radial_square => 3,
revolution => 4,
conical => 5 },
repeat => { none => 0,
sawtooth => 1,
triangle => 2,
saw_both => 3,
tri_both => 4,
},
super_sample => {
none => 0,
grid => 1,
random => 2,
circle => 3,
},
combine => {
none => 0,
normal => 1,
multiply => 2, mult => 2,
dissolve => 3,
add => 4,
subtract => 5, 'sub' => 5,
diff => 6,
lighten => 7,
darken => 8,
hue => 9,
sat => 10,
value => 11,
color => 12,
},
},
defaults => { ftype => 0, repeat => 0, combine => 0,
super_sample => 0, ssample_param => 4,
segments=>[
[ 0, 0.5, 1,
[0,0,0],
[255, 255, 255],
0, 0,
],
],
},
callsub =>
sub {
my %hsh = @_;
# make sure the segments are specified with colors
my @segments;
for my $segment (@{$hsh{segments}}) {
my @new_segment = @$segment;
$_ = _color($_) or die $Imager::ERRSTR."\n" for @new_segment[3,4];
push @segments, \@new_segment;
}
i_fountain($hsh{image}, $hsh{xa}, $hsh{ya}, $hsh{xb}, $hsh{yb},
$hsh{ftype}, $hsh{repeat}, $hsh{combine}, $hsh{super_sample},
$hsh{ssample_param}, \@segments)
or die Imager->_error_as_msg() . "\n";
},
};
$filters{unsharpmask} =
{
callseq => [ qw(image stddev scale) ],
defaults => { stddev=>2.0, scale=>1.0 },
callsub =>
sub {
my %hsh = @_;
i_unsharp_mask($hsh{image}, $hsh{stddev}, $hsh{scale});
},
};
$FORMATGUESS=\&def_guess_type;
$warn_obsolete = 1;
}
#
# Non methods
#
# initialize Imager
# NOTE: this might be moved to an import override later on
sub import {
my $i = 1;
while ($i < @_) {
if ($_[$i] eq '-log-stderr') {
init_log(undef, 4);
splice(@_, $i, 1);
}
else {
++$i;
}
}
goto &Exporter::import;
}
sub init_log {
Imager->open_log(log => $_[0], level => $_[1]);
}
sub init {
my %parms=(loglevel=>1,@_);
if (exists $parms{'warn_obsolete'}) {
$warn_obsolete = $parms{'warn_obsolete'};
}
if ($parms{'log'}) {
Imager->open_log(log => $parms{log}, level => $parms{loglevel})
or return;
}
if (exists $parms{'t1log'}) {
if ($formats{t1}) {
if (Imager::Font::T1::i_init_t1($parms{'t1log'})) {
Imager->_set_error(Imager->_error_as_msg);
return;
}
}
}
return 1;
}
{
my $is_logging = 0;
sub open_log {
my $class = shift;
my (%opts) = ( loglevel => 1, @_ );
$is_logging = i_init_log($opts{log}, $opts{loglevel});
unless ($is_logging) {
Imager->_set_error(Imager->_error_as_msg());
return;
}
Imager->log("Imager $VERSION starting\n", 1);
return $is_logging;
}
sub close_log {
i_init_log(undef, -1);
$is_logging = 0;
}
sub log {
my ($class, $message, $level) = @_;
defined $level or $level = 1;
i_log_entry($message, $level);
}
sub is_logging {
return $is_logging;
}
}
END {
if ($DEBUG) {
print "shutdown code\n";
# for(keys %instances) { $instances{$_}->DESTROY(); }
malloc_state(); # how do decide if this should be used? -- store something from the import
print "Imager exiting\n";
}
}
# Load a filter plugin
our %DSOs;
sub load_plugin {
my ($filename)=@_;
my $i;
if ($^O eq 'android') {
require File::Spec;
$filename = File::Spec->rel2abs($filename);
}
my ($DSO_handle,$str)=DSO_open($filename);
if (!defined($DSO_handle)) { $Imager::ERRSTR="Couldn't load plugin '$filename'\n"; return undef; }
my %funcs=DSO_funclist($DSO_handle);
if ($DEBUG) { print "loading module $filename\n"; $i=0; for(keys %funcs) { printf(" %2d: %s\n",$i++,$_); } }
$i=0;
for(keys %funcs) { if ($filters{$_}) { $ERRSTR="filter '$_' already exists\n"; DSO_close($DSO_handle); return undef; } }
$DSOs{$filename}=[$DSO_handle,\%funcs];
for(keys %funcs) {
my $evstr="\$filters{'".$_."'}={".$funcs{$_}.'};';
$DEBUG && print "eval string:\n",$evstr,"\n";
eval $evstr;
print $@ if $@;
}
return 1;
}
# Unload a plugin
sub unload_plugin {
my ($filename)=@_;
if ($^O eq 'android') {
require File::Spec;
$filename = File::Spec->rel2abs($filename);
}
if (!$DSOs{$filename}) { $ERRSTR="plugin '$filename' not loaded."; return undef; }
my ($DSO_handle,$funcref)=@{$DSOs{$filename}};
for(keys %{$funcref}) {
delete $filters{$_};
$DEBUG && print "unloading: $_\n";
}
my $rc=DSO_close($DSO_handle);
if (!defined($rc)) { $ERRSTR="unable to unload plugin '$filename'."; return undef; }
return 1;
}
# take the results of i_error() and make a message out of it
sub _error_as_msg {
return join(": ", map $_->[0], i_errors());
}
# this function tries to DWIM for color parameters
# color objects are used as is
# simple scalars are simply treated as single parameters to Imager::Color->new
# hashrefs are treated as named argument lists to Imager::Color->new
# arrayrefs are treated as list arguments to Imager::Color->new iff any
# parameter is > 1
# other arrayrefs are treated as list arguments to Imager::Color::Float
sub _color {
my $arg = shift;
# perl 5.6.0 seems to do weird things to $arg if we don't make an
# explicitly stringified copy
# I vaguely remember a bug on this on p5p, but couldn't find it
# through bugs.perl.org (I had trouble getting it to find any bugs)
my $copy = $arg . "";
my $result;
if (ref $arg) {
if (UNIVERSAL::isa($arg, "Imager::Color")
|| UNIVERSAL::isa($arg, "Imager::Color::Float")) {
$result = $arg;
}
else {
if ($copy =~ /^HASH\(/) {
$result = Imager::Color->new(%$arg);
}
elsif ($copy =~ /^ARRAY\(/) {
$result = Imager::Color->new(@$arg);
}
else {
$Imager::ERRSTR = "Not a color";
}
}
}
else {
# assume Imager::Color::new knows how to handle it
$result = Imager::Color->new($arg);
}
return $result;
}
sub _combine {
my ($self, $combine, $default) = @_;
if (!defined $combine && ref $self) {
$combine = $self->{combine};
}
defined $combine or $combine = $defaults{combine};
defined $combine or $combine = $default;
if (exists $combine_types{$combine}) {
$combine = $combine_types{$combine};
}
return $combine;
}
sub _valid_image {
my ($self, $method) = @_;
ref $self
or return Imager->_set_error("$method needs an image object");
$self->{IMG} && Scalar::Util::blessed($self->{IMG}) and return 1;
my $msg = $self->{IMG} ? "images do not cross threads" : "empty input image";
$msg = "$method: $msg" if $method;
$self->_set_error($msg);
return;
}
# returns first defined parameter
sub _first {
for (@_) {
return $_ if defined $_;
}
return undef;
}
#
# Methods to be called on objects.
#
# Create a new Imager object takes very few parameters.
# usually you call this method and then call open from
# the resulting object
sub new {
my $class = shift;
my $self ={};
my %hsh=@_;
bless $self,$class;
$self->{IMG}=undef; # Just to indicate what exists
$self->{ERRSTR}=undef; #
$self->{DEBUG}=$DEBUG;
$self->{DEBUG} and print "Initialized Imager\n";
if (defined $hsh{file} ||
defined $hsh{fh} ||
defined $hsh{fd} ||
defined $hsh{callback} ||
defined $hsh{readcb} ||
defined $hsh{data} ||
defined $hsh{io}) {
# allow $img = Imager->new(file => $filename)
my %extras;
# type is already used as a parameter to new(), rename it for the
# call to read()
if ($hsh{filetype}) {
$extras{type} = $hsh{filetype};
}
unless ($self->read(%hsh, %extras)) {
$Imager::ERRSTR = $self->{ERRSTR};
return;
}
}
elsif (defined $hsh{xsize} || defined $hsh{ysize}) {
unless ($self->img_set(%hsh)) {
$Imager::ERRSTR = $self->{ERRSTR};
return;
}
}
elsif (%hsh) {
Imager->_set_error("new: supply xsize and ysize or a file access parameter or no parameters");
return;
}
return $self;
}
# Copy an entire image with no changes
# - if an image has magic the copy of it will not be magical
sub copy {
my $self = shift;
$self->_valid_image("copy")
or return;
unless (defined wantarray) {
my @caller = caller;
warn "copy() called in void context - copy() returns the copied image at $caller[1] line $caller[2]\n";
return;
}
my $newcopy=Imager->new();
$newcopy->{IMG} = i_copy($self->{IMG});
return $newcopy;
}
# Paste a region
sub paste {
my $self = shift;
$self->_valid_image("paste")
or return;
my %input=(left=>0, top=>0, src_minx => 0, src_miny => 0, @_);
my $src = $input{img} || $input{src};
unless($src) {
$self->_set_error("no source image");
return;
}
unless ($src->_valid_image("paste")) {
$self->{ERRSTR} = $src->{ERRSTR} . " (for src)";
return;
}
$input{left}=0 if $input{left} <= 0;
$input{top}=0 if $input{top} <= 0;
my($r,$b)=i_img_info($src->{IMG});
my ($src_left, $src_top) = @input{qw/src_minx src_miny/};
my ($src_right, $src_bottom);
if ($input{src_coords}) {
($src_left, $src_top, $src_right, $src_bottom) = @{$input{src_coords}}
}
else {
if (defined $input{src_maxx}) {
$src_right = $input{src_maxx};
}
elsif (defined $input{width}) {
if ($input{width} <= 0) {
$self->_set_error("paste: width must me positive");
return;
}
$src_right = $src_left + $input{width};
}
else {
$src_right = $r;
}
if (defined $input{src_maxy}) {
$src_bottom = $input{src_maxy};
}
elsif (defined $input{height}) {
if ($input{height} < 0) {
$self->_set_error("paste: height must be positive");
return;
}
$src_bottom = $src_top + $input{height};
}
else {
$src_bottom = $b;
}
}
$src_right > $r and $src_right = $r;
$src_bottom > $b and $src_bottom = $b;
if ($src_right <= $src_left
|| $src_bottom < $src_top) {
$self->_set_error("nothing to paste");
return;
}
i_copyto($self->{IMG}, $src->{IMG},
$src_left, $src_top, $src_right, $src_bottom,
$input{left}, $input{top});
return $self; # What should go here??
}
# Crop an image - i.e. return a new image that is smaller
sub crop {
my $self=shift;
$self->_valid_image("crop")
or return;
unless (defined wantarray) {
my @caller = caller;
warn "crop() called in void context - crop() returns the cropped image at $caller[1] line $caller[2]\n";
return;
}
my %hsh=@_;
my ($w, $h, $l, $r, $b, $t) =
@hsh{qw(width height left right bottom top)};
# work through the various possibilities
if (defined $l) {
if (defined $w) {
$r = $l + $w;
}
elsif (!defined $r) {
$r = $self->getwidth;
}
}
elsif (defined $r) {
if (defined $w) {
$l = $r - $w;
}
else {
$l = 0;
}
}
elsif (defined $w) {
$l = int(0.5+($self->getwidth()-$w)/2);
$r = $l + $w;
}
else {
$l = 0;
$r = $self->getwidth;
}
if (defined $t) {
if (defined $h) {
$b = $t + $h;
}
elsif (!defined $b) {
$b = $self->getheight;
}
}
elsif (defined $b) {
if (defined $h) {
$t = $b - $h;
}
else {
$t = 0;
}
}
elsif (defined $h) {
$t=int(0.5+($self->getheight()-$h)/2);
$b=$t+$h;
}
else {
$t = 0;
$b = $self->getheight;
}
($l,$r)=($r,$l) if $l>$r;
($t,$b)=($b,$t) if $t>$b;
$l < 0 and $l = 0;
$r > $self->getwidth and $r = $self->getwidth;
$t < 0 and $t = 0;
$b > $self->getheight and $b = $self->getheight;
if ($l == $r || $t == $b) {
$self->_set_error("resulting image would have no content");
return;
}
if( $r < $l or $b < $t ) {
$self->_set_error("attempting to crop outside of the image");
return;
}
my $dst = $self->_sametype(xsize=>$r-$l, ysize=>$b-$t);
i_copyto($dst->{IMG},$self->{IMG},$l,$t,$r,$b,0,0);
return $dst;
}
my $empty_trim_colors = Imager::TrimColorList->new();
sub _trim_rect {
my ($self, $name, %hsh) = @_;
$self->_valid_image($name)
or return;
my $auto = delete $hsh{auto};
my $colors = delete $hsh{colors} || $empty_trim_colors;
my $alpha = delete $hsh{alpha} || 0;
my $tolerance = delete $hsh{tolerance};
defined $tolerance or $tolerance = 0.01;
if (keys %hsh) {
$self->_set_error("$name: unexpected arguments:".join(", ", sort keys %hsh));
return;
}
if ($auto) {
if ($colors != $empty_trim_colors) {
$self->_set_error("$name: only one of auto and colors can be supplied");
return;
}
if ($tolerance < 0) {
$self->_set_error("$name: tolerance must be non-negative");
return;
}
$colors = Imager::TrimColorList->auto
(
auto => $auto,
tolerance => $tolerance,
name => $name,
image => $self,
);
unless ($colors) {
$self->_set_error(Imager->errstr);
return;
}
}
unless (ref $colors) {
$self->_set_error("$name: colors must be an arrayref or an Imager::TrimColorList object");
return;
}
unless (UNIVERSAL::isa($colors, "Imager::TrimColorList")) {
unless (Scalar::Util::reftype($colors) eq "ARRAY") {
$self->_set_error("$name: colors must be an arrayref or an Imager::TrimColorList object");
return;
}
$colors = Imager::TrimColorList->new(@$colors);
}
return i_trim_rect($self->{IMG}, $alpha, $colors);
}
sub trim_rect {
my ($self, %hsh) = @_;
return $self->_trim_rect("trim_rect", %hsh);
}
sub trim {
my ($self, %hsh) = @_;
my ($left, $top, $right, $bottom) = $self->_trim_rect("trim", %hsh)
or return;
if ($left == $self->getwidth) {
# the whole image would be trimmed, but we don't support zero
# width or height images.
return $self->crop(width => 1, height => 1);
}
else {
my ($w, $h) = i_img_info($self->{IMG});
return $self->crop(left => $left, right => $w - $right,
top => $top, bottom => $h - $bottom);
}
}
sub _sametype {
my ($self, %opts) = @_;
$self->_valid_image
or return;