File Coverage

blib/lib/Imager/Trim.pm
Criterion Covered Total %
statement 9 77 11.6
branch 0 28 0.0
condition 0 18 0.0
subroutine 3 11 27.2
pod 0 7 0.0
total 12 141 8.5


line stmt bran cond sub pod time code
1 1     1   434 use strict;
  1         1  
  1         50  
2             package Imager::Trim;
3             our $AUTHORITY = 'cpan:SPOT';
4             # ABSTRACT: automatic cropping for images using Imager.
5             $Imager::Trim::VERSION = '0.005';
6             our $VERSION = '0.005'; # VERSION
7              
8 1     1   356 use parent 'Imager';
  1         225  
  1         3  
9 1     1   29745 use Imager::Color;
  1         2  
  1         470  
10              
11             sub fuzz {
12 0     0 0   my ($self, $value) = @_;
13 0 0 0       if (defined $value and ($value+0) > 0) {
    0          
14 0           $self->{fuzz} = $value+0;
15             } elsif (!$self->{fuzz}) {
16 0           $self->{fuzz} = 0;
17             }
18 0           return $self->{fuzz};
19             }
20              
21             sub color {
22 0     0 0   my ($self, @values) = @_;
23 0 0         if (@_ > 1) {
    0          
24 0 0         if (ref($values[0]) eq 'Imager::Color') {
25 0           $self->{color} = $values[0];
26             } else {
27 0           $self->{color} = Imager::Color->new(@values);
28             }
29             } elsif (!$self->{color}) {
30 0           $self->{color} = $self->getpixel( x => 0, y => 0 );
31             }
32 0           return $self->{color};
33             }
34              
35             sub trim_top {
36 0     0 0   return shift->{trim_top};
37             }
38              
39             sub trim_left {
40 0     0 0   return shift->{trim_left};
41             }
42              
43             sub trim_right {
44 0     0 0   return shift->{trim_right};
45             }
46              
47             sub trim_bottom {
48 0     0 0   return shift->{trim_bottom};
49             }
50              
51             sub _match_colors {
52 0     0     my ($self, $x, $y) = @_;
53              
54 0           my ($color1_red, $color1_green, $color1_blue) = $self->color->rgba();
55 0           my ($color2_red, $color2_green, $color2_blue) = $self->getpixel( x => $x, y => $y )->rgba();
56              
57 0 0 0       return 1 if
      0        
      0        
      0        
      0        
58             ($color1_red < ($color2_red - $self->fuzz) or $color1_red > ($color2_red + $self->fuzz))
59             and ($color1_green < ($color2_green - $self->fuzz) or $color1_green > ($color2_green + $self->fuzz))
60             and ($color1_blue < ($color2_blue - $self->fuzz) or $color1_blue > ($color2_blue + $self->fuzz))
61             ;
62             }
63              
64             sub trim {
65 0     0 0   my ($self, %params) = @_;
66              
67 0           $self->fuzz($params{fuzz});
68              
69 0           $self->color($params{color});
70              
71 0           my $top = 0;
72 0           my $left = 0;
73 0           my $right = $self->getwidth();
74 0           my $bottom = $self->getheight();
75              
76 0           for (my $y = $top; $y < $bottom; $y++) {
77 0           my $match = 0;
78 0           for (my $x = $left; $x < $right; $x++) {
79 0 0         if ($self->_match_colors($x, $y)) {
80 0           $match = 1;
81 0           last;
82             }
83             }
84 0 0         if ($match) {
85 0           $top = $y;
86 0           last;
87             }
88             }
89              
90 0           for (my $y = $bottom; $y > $top; $y--) {
91 0           my $match = 0;
92 0           for (my $x = $left; $x < $right; $x++) {
93 0 0         if ($self->_match_colors($x, $y-1)) {
94 0           $match = 1;
95 0           last;
96             }
97             }
98 0 0         if ($match) {
99 0           $bottom = $y;
100 0           last;
101             }
102             }
103              
104 0           for (my $x = $left; $x < $right; $x++) {
105 0           my $match = 0;
106 0           for (my $y = $top; $y < $bottom; $y++) {
107 0 0         if ($self->_match_colors($x, $y)) {
108 0           $match = 1;
109 0           last;
110             }
111             }
112 0 0         if ($match) {
113 0           $left = $x;
114 0           last;
115             }
116             }
117              
118 0           for (my $x = $right; $x > $left; $x--) {
119 0           my $match = 0;
120 0           for (my $y = $top; $y < $bottom; $y++) {
121 0 0         if ($self->_match_colors($x-1, $y)) {
122 0           $match = 1;
123 0           last;
124             }
125             }
126 0 0         if ($match) {
127 0           $right = $x;
128 0           last;
129             }
130             }
131              
132 0           $self->{trim_top} = $top;
133 0           $self->{trim_left} = $left;
134 0           $self->{trim_right} = $right;
135 0           $self->{trim_bottom} = $bottom;
136              
137 0           return $self->crop(
138             left => $self->trim_left,
139             right => $self->trim_right,
140             top => $self->trim_top,
141             bottom => $self->trim_bottom,
142             );
143             }
144              
145             1;
146              
147             __END__