File Coverage

blib/lib/Crypt/Image/Util.pm
Criterion Covered Total %
statement 20 60 33.3
branch 0 12 0.0
condition n/a
subroutine 7 16 43.7
pod 9 9 100.0
total 36 97 37.1


line stmt bran cond sub pod time code
1             package Crypt::Image::Util;
2              
3             $Crypt::Image::Util::VERSION = '0.11';
4             $Crypt::Image::Util::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             Crypt::Image::Util - Helper for Crypt::Image module.
9              
10             =head1 VERSION
11              
12             Version 0.11
13              
14             =cut
15              
16 1     1   2034 use 5.006;
  1         5  
17 1     1   9 use strict; use warnings;
  1     1   2  
  1         35  
  1         7  
  1         2  
  1         46  
18 1     1   6 use Data::Dumper;
  1         2  
  1         113  
19              
20 1     1   888 use autodie;
  1         23163  
  1         8  
21 1     1   9099 use Math::Random;
  1         8925  
  1         159  
22 1     1   11 use Crypt::Image::Axis;
  1         3  
  1         955  
23              
24             =head1 DESCRIPTION
25              
26             Utility module for Crypt::Image. Methods can be accessed directly.
27              
28             =head1 METHODS
29              
30             =head2 cloneImage()
31              
32             It clone the given image (object of type GD::Image) and returns the clone of type
33             GD::Image.
34              
35             =cut
36              
37             sub cloneImage {
38 0     0 1   my ($image) = @_;
39              
40 0           return $image->clone;
41             }
42              
43             =head2 saveImage()
44              
45             Saves the given image data as given file name of the given type. The parameters
46             are listed below in sequence:
47              
48             =over 3
49              
50             =item * Filename with the complete path.
51              
52             =item * Object of type GD::Image for the image.
53              
54             =item * Type of the given image.
55              
56             =back
57              
58             =cut
59              
60             sub saveImage {
61 0     0 1   my ($file, $image, $type) = @_;
62              
63 0           open(IMAGE, ">$file");
64 0           binmode IMAGE;
65 0 0         print IMAGE $image->png if $type =~ /png/i;
66 0 0         print IMAGE $image->gif if $type =~ /gif/i;
67 0 0         print IMAGE $image->jpeg if $type =~ /jpg/i;
68 0           close(IMAGE);
69             }
70              
71             =head2 moveDown()
72              
73             Moves the given pixel down by given number.
74              
75             =cut
76              
77             sub moveDown {
78 0     0 1   my ($this, $by) = @_;
79              
80 0 0         ($this < 128)?($this += $by):($this -= $by);
81 0           return $this;
82             }
83              
84             =head2 moveUp()
85              
86             Moves the given pixel up by given number.
87              
88             =cut
89              
90             sub moveUp {
91 0     0 1   my ($this, $by) = @_;
92              
93 0 0         ($this >= 128)?($this -= $by):($this += $by);
94 0           return $this;
95             }
96              
97             =head2 getColor()
98              
99             Returns the color index for the given R, G and B.
100              
101             =cut
102              
103             sub getColor {
104 0     0 1   my ($r, $g, $b) = @_;
105              
106 0           my $image = GD::Image->new();
107 0           return $image->colorAllocate($r, $g, $b);
108             }
109              
110             =head2 splitInTwo()
111              
112             It splits the given point into X, Y coordinates and returns an object of type
113             Crypt::Image::Axis.
114              
115             =cut
116              
117             sub splitInTwo {
118 0     0 1   my ($a) = @_;
119              
120 0           my $r = int(random_uniform() * $a);
121 0           $a -= $r;
122 0           return Crypt::Image::Axis->new('x' => $a, 'y' => $r);
123             }
124              
125             =head2 splitInThree()
126              
127             It splits the given point into X, Y, Z coordinates and returns an object of type
128             L.
129              
130             =cut
131              
132             sub splitInThree {
133 0     0 1   my ($a) = @_;
134              
135 0           my $z = 0;
136 0           my $r = int(random_uniform() * $a);
137 0           $a -= $r;
138 0 0         if ($a > $r) {
139 0           $z = int(random_uniform() * $a);
140 0           $a -= $z;
141             }
142             else {
143 0           $z = int(random_uniform() * $r);
144 0           $r -= $z;
145             }
146              
147 0           return Crypt::Image::Axis->new('x' => $a, 'y' => $r, 'z' => $z);
148             }
149              
150             =head2 differenceInAxis()
151              
152             It returns the absolute difference in the R, G and B of the given key and cloned
153             images at X and Y coordinates. The parameters are listed below in sequence:
154              
155             =over 4
156              
157             =item * Object of type GD::Image for key image.
158              
159             =item * Object of type GD::Image for new image.
160              
161             =item * X coordinate.
162              
163             =item * Y coordinate.
164              
165             =back
166              
167             =cut
168              
169             sub differenceInAxis {
170 0     0 1   my ($k, $c, $x, $y) = @_;
171              
172 0           my ($k_r, $k_g, $k_b) = Crypt::Image::Util::getPixelColorRGB($k, $x, $y);
173 0           my ($c_r, $c_g, $c_b) = Crypt::Image::Util::getPixelColorRGB($c, $x, $y);
174              
175 0           return (abs($k_r-$c_r), abs($k_g-$c_g), abs($k_b-$c_b));
176             }
177              
178             =head2 getPixelColorRGB()
179              
180             Returns the R,G,B of the given image at the given X,Y coordinates. The parameters
181             are listed below in sequence:
182              
183             =over 3
184              
185             =item * Object of type GD::Image for the image.
186              
187             =item * X coordinate.
188              
189             =item * Y coordinate.
190              
191             =back
192              
193             =cut
194              
195             sub getPixelColorRGB {
196 0     0 1   my ($image, $x, $y) = @_;
197              
198 0           my $index = $image->getPixel($x, $y);
199 0           my ($r, $g, $b) = $image->rgb($index);
200 0           return ($r, $g, $b);
201             }
202              
203             =head1 AUTHOR
204              
205             Mohammad S Anwar, C<< >>
206              
207             =head1 REPOSITORY
208              
209             L
210              
211             =head1 BUGS
212              
213             Please report any bugs / feature requests to C or
214             through the the web interface at L.
215             I will be notified, and then you'll automatically be notified of progress on your
216             bug as I make changes.
217              
218             =head1 SUPPORT
219              
220             You can find documentation for this module with the perldoc command.
221              
222             perldoc Crypt::Image::Util
223              
224             You can also look for information at:
225              
226             =over 4
227              
228             =item * RT: CPAN's request tracker
229              
230             L
231              
232             =item * AnnoCPAN: Annotated CPAN documentation
233              
234             L
235              
236             =item * CPAN Ratings
237              
238             L
239              
240             =item * Search CPAN
241              
242             L
243              
244             =back
245              
246             =head1 LICENSE AND COPYRIGHT
247              
248             Copyright (C) 2011 - 2016 Mohammad S Anwar.
249              
250             This program is free software; you can redistribute it and/or modify it under
251             the terms of the the Artistic License (2.0). You may obtain a copy of the full
252             license at:
253              
254             L
255              
256             Any use, modification, and distribution of the Standard or Modified Versions is
257             governed by this Artistic License.By using, modifying or distributing the Package,
258             you accept this license. Do not use, modify, or distribute the Package, if you do
259             not accept this license.
260              
261             If your Modified Version has been derived from a Modified Version made by someone
262             other than you,you are nevertheless required to ensure that your Modified Version
263             complies with the requirements of this license.
264              
265             This license does not grant you the right to use any trademark, service mark,
266             tradename, or logo of the Copyright Holder.
267              
268             This license includes the non-exclusive, worldwide, free-of-charge patent license
269             to make, have made, use, offer to sell, sell, import and otherwise transfer the
270             Package with respect to any patent claims licensable by the Copyright Holder that
271             are necessarily infringed by the Package. If you institute patent litigation
272             (including a cross-claim or counterclaim) against any party alleging that the
273             Package constitutes direct or contributory patent infringement,then this Artistic
274             License to you shall terminate on the date that such litigation is filed.
275              
276             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
277             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
278             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
279             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
280             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
281             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
282             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
283              
284             =cut
285              
286             1; # End of Crypt::Image::Util