File Coverage

blib/lib/Device/PCD8544/ConvertImage.pm
Criterion Covered Total %
statement 15 42 35.7
branch 0 6 0.0
condition 0 6 0.0
subroutine 5 7 71.4
pod 0 1 0.0
total 20 62 32.2


line stmt bran cond sub pod time code
1             # Copyright (c) 2015 Timm Murray
2             # All rights reserved.
3             #
4             # Redistribution and use in source and binary forms, with or without
5             # modification, are permitted provided that the following conditions are met:
6             #
7             # * Redistributions of source code must retain the above copyright notice,
8             # this list of conditions and the following disclaimer.
9             # * Redistributions in binary form must reproduce the above copyright
10             # notice, this list of conditions and the following disclaimer in the
11             # documentation and/or other materials provided with the distribution.
12             #
13             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14             # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16             # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23             # POSSIBILITY OF SUCH DAMAGE.
24             package Device::PCD8544::ConvertImage;
25             $Device::PCD8544::ConvertImage::VERSION = '0.0268329147520525';
26 1     1   747 use v5.14;
  1         3  
  1         35  
27 1     1   4 use warnings;
  1         1  
  1         29  
28 1     1   5 use Device::PCD8544::Exceptions;
  1         1  
  1         35  
29              
30 1     1   4 use constant WIDTH => 84;
  1         1  
  1         54  
31 1     1   3 use constant HEIGHT => 48;
  1         2  
  1         293  
32              
33              
34             sub convert
35             {
36 0     0 0   my ($img) = @_;
37 0           my $width = $img->getwidth;
38 0           my $height = $img->getheight;
39 0 0 0       if( ($width != WIDTH) || ($height != HEIGHT) ) {
40 0           Device::PCD8544::ImageSizeException->throw( 'Width/height is'
41             . " $width/$height"
42             . ', expected is ' . WIDTH . '/' . HEIGHT
43             . '. Please rescale image.' );
44             }
45              
46 0           my @lcd_bitmap = ();
47            
48 0           my $total_pixels = $width * $height;
49 0           for( my $i = 0; $i < $total_pixels; $i += 8 ) {
50 0           my $max_i = $i + 7;
51 0 0         $max_i = $total_pixels - 1 if $max_i >= $total_pixels;
52 0           my @pixels = _get_pixels( $img, $width, $height, $i, $max_i );
53              
54 0           my $val = $pixels[0];
55 0           foreach my $j (1 .. 7) {
56 0           $val <<= 1;
57 0           $val |= $pixels[$j];
58             }
59              
60 0           push @lcd_bitmap, $val;
61             }
62              
63 0           return \@lcd_bitmap;
64             }
65              
66              
67             sub _get_pixels
68             {
69 0     0     my ($img, $width, $height, $i_min, $i_max) = @_;
70              
71 0           my @pixels;
72 0           foreach my $i ($i_min .. $i_max) {
73 0           my $x = $i % $width;
74 0           my $y = int( $i / $width );
75 0           my $pixel = $img->getpixel( x => $x, y => $y );
76 0           my ($r, $g, $b, $a) = $pixel->rgba;
77 0 0 0       my $val = ($r || $g || $b) ? 0 : 1;
78 0           push @pixels, $val;
79             }
80              
81 0           return @pixels;
82             }
83              
84              
85             1;
86             __END__