| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Imager::AverageGray; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 20094 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 4 | 1 |  |  | 1 |  | 5 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 32 |  | 
| 5 | 1 |  |  | 1 |  | 1324 | use Imager; | 
|  | 1 |  |  |  |  | 53589 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | =head1 NAME | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | Imager::AverageGray - Finds the average gray for a Imager object or image. | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 VERSION | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | Version 0.0.0 | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =cut | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | our $VERSION = '0.0.0'; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | Quick summary of what the module does. | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | Perhaps a little code snippet. | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | use Imager::AverageGray; | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | my $ag = Imager::AverageGray->new(); | 
| 29 |  |  |  |  |  |  | ... | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | =head1 METHODES | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =head2 new | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | Initiates the object. | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | my $ag=Imager::AverageGray->new; | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | =cut | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | sub new{ | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 0 |  |  | 0 | 1 |  | my $self={error=>undef, errorString=>''}; | 
| 44 | 0 |  |  |  |  |  | bless $self; | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 0 |  |  |  |  |  | return $self; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =head2 fromFile | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | This returns the average gray from a specified file. | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | my $gray=$ag->fromFile('image.jpg'); | 
| 54 |  |  |  |  |  |  | if($ag->{error}){ | 
| 55 |  |  |  |  |  |  | print 'Error:'.$ag->{error}.': '.$ag->{errorString}."\n"; | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | =cut | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub fromFile{ | 
| 61 | 0 |  |  | 0 | 1 |  | my $self=$_[0]; | 
| 62 | 0 |  |  |  |  |  | my $file=$_[1]; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 0 |  |  |  |  |  | $self->errorblank; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 |  |  |  |  |  | my $img = Imager->new(); | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 0 | 0 |  |  |  |  | if (! -e $file) { | 
| 69 | 0 |  |  |  |  |  | warn('Imager-AverageGray fromFile:1: The file, "'.$file | 
| 70 |  |  |  |  |  |  | .'", does not exist'); | 
| 71 | 0 |  |  |  |  |  | $self->{error}=1; | 
| 72 | 0 |  |  |  |  |  | $self->{errorString}='The file, "'.$file.'", does not exist'; | 
| 73 | 0 |  |  |  |  |  | return undef; | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 0 | 0 |  |  |  |  | if (! -f $file) { | 
| 77 | 0 |  |  |  |  |  | warn('Imager-AverageGray fromFile:1: "'.$file.'", is not a file'); | 
| 78 | 0 |  |  |  |  |  | $self->{error}=2; | 
| 79 | 0 |  |  |  |  |  | $self->{errorString}='"'.$file.'", is not a file'; | 
| 80 | 0 |  |  |  |  |  | return undef; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 0 | 0 |  |  |  |  | if (!$img->read(file=>$file)){ | 
| 84 | 0 |  |  |  |  |  | warn('Imager-AverageGray fromFile:3: Imager failed reading the file. error="' | 
| 85 |  |  |  |  |  |  | .$img->errstr.'"'); | 
| 86 | 0 |  |  |  |  |  | $self->{error}=3; | 
| 87 | 0 |  |  |  |  |  | $self->{errorString}='Imager failed reading the file. error="'.$img->errstr.'"'; | 
| 88 | 0 |  |  |  |  |  | return undef; | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 0 |  |  |  |  |  | my $ag=$self->fromObject($img); | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 0 |  |  |  |  |  | return $ag; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | =head2 fromObject | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | This finds the average gray for a Imager object. | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | my $gray=$ag->fromObject($img); | 
| 101 |  |  |  |  |  |  | if($ag->{error}){ | 
| 102 |  |  |  |  |  |  | print 'Error:'.$ag->{error}.': '.$ag->{errorString}."\n"; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | =cut | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | sub fromObject{ | 
| 108 | 0 |  |  | 0 | 1 |  | my $self=$_[0]; | 
| 109 | 0 |  |  |  |  |  | my $img=$_[1]; | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 0 |  |  |  |  |  | $self->errorblank; | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | #create a gray scale image; | 
| 114 | 0 |  |  |  |  |  | my $gimg=$img->convert(preset=>'grey'); | 
| 115 |  |  |  |  |  |  |  | 
| 116 | 0 |  |  |  |  |  | my $maxX=$gimg->getwidth; | 
| 117 | 0 |  |  |  |  |  | my $maxY=$gimg->getheight; | 
| 118 |  |  |  |  |  |  |  | 
| 119 | 0 |  |  |  |  |  | $maxX--; | 
| 120 | 0 |  |  |  |  |  | $maxY--; | 
| 121 |  |  |  |  |  |  |  | 
| 122 | 0 |  |  |  |  |  | my $x=0; | 
| 123 | 0 |  |  |  |  |  | my $y=0; | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 0 |  |  |  |  |  | my @values; | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 0 |  |  |  |  |  | while ($x <= $maxX) { | 
| 128 | 0 |  |  |  |  |  | while ($y <= $maxY) { | 
| 129 | 0 |  |  |  |  |  | my $color=$gimg->getpixel(x=>$x, y=>$y); | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 0 |  |  |  |  |  | my ($red, $green, $blue, $alpha) = $color->rgba(); | 
| 132 |  |  |  |  |  |  |  | 
| 133 | 0 |  |  |  |  |  | push(@values, $red); | 
| 134 |  |  |  |  |  |  |  | 
| 135 | 0 |  |  |  |  |  | $y++; | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 | 0 |  |  |  |  |  | $x++; | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 0 |  |  |  |  |  | my $int=0; | 
| 142 | 0 |  |  |  |  |  | my $total=0; | 
| 143 | 0 |  |  |  |  |  | while (defined($values[$int])) { | 
| 144 | 0 |  |  |  |  |  | $total=$values[$int] + $total; | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 0 |  |  |  |  |  | $int++; | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 0 |  |  |  |  |  | my $ag=$total/$int; | 
| 150 |  |  |  |  |  |  |  | 
| 151 | 0 |  |  |  |  |  | return $ag; | 
| 152 |  |  |  |  |  |  | } | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =head2 errorblank | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | This blanks the error storage and is only meant for internal usage. | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | It does the following. | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | $self->{error}=undef; | 
| 161 |  |  |  |  |  |  | $self->{errorString}=""; | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | =cut | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | #blanks the error flags | 
| 166 |  |  |  |  |  |  | sub errorblank{ | 
| 167 | 0 |  |  | 0 | 1 |  | my $self=$_[0]; | 
| 168 |  |  |  |  |  |  |  | 
| 169 | 0 |  |  |  |  |  | $self->{error}=undef; | 
| 170 | 0 |  |  |  |  |  | $self->{errorString}=""; | 
| 171 |  |  |  |  |  |  |  | 
| 172 | 0 |  |  |  |  |  | return 1; | 
| 173 |  |  |  |  |  |  | } | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | =head1 ERROR CODES | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | =head2 1 | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | File does not exist. | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | =head2 2 | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | The specified file is not a file. | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | =head2 3 | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | Imager failed to read the file. | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | =head1 AUTHOR | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | Zane C. Bowers, C<<  >> | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | =head1 BUGS | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | Please report any bugs or feature requests to C, or through | 
| 196 |  |  |  |  |  |  | the web interface at L.  I will be notified, and then you'll | 
| 197 |  |  |  |  |  |  | automatically be notified of progress on your bug as I make changes. | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | =head1 SUPPORT | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | perldoc Image::AverageGray | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | You can also look for information at: | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | =over 4 | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | =item * RT: CPAN's request tracker | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | L | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | L | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | L | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | =item * Search CPAN | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | L | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | =back | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  |  | 
| 232 |  |  |  |  |  |  | =head1 ACKNOWLEDGEMENTS | 
| 233 |  |  |  |  |  |  |  | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | =head1 COPYRIGHT & LICENSE | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | Copyright 2009 Zane C. Bowers, all rights reserved. | 
| 238 |  |  |  |  |  |  |  | 
| 239 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it | 
| 240 |  |  |  |  |  |  | under the same terms as Perl itself. | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | =cut | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | 1; # End of Image::AverageGray |