File Coverage

blib/lib/Algorithm/Scale2x.pm
Criterion Covered Total %
statement 32 32 100.0
branch 16 28 57.1
condition 8 18 44.4
subroutine 5 5 100.0
pod 2 2 100.0
total 63 85 74.1


line stmt bran cond sub pod time code
1             package Algorithm::Scale2x;
2              
3 2     2   88585 use strict;
  2         5  
  2         79  
4 2     2   11 use warnings;
  2         4  
  2         62  
5              
6 2     2   11 use base qw( Exporter );
  2         7  
  2         1183  
7              
8             our $VERSION = '0.04';
9             our @EXPORT_OK = qw( scale2x scale3x );
10              
11             =head1 NAME
12              
13             Algorithm::Scale2x - Generic implementation of the Scale2x algorithm
14              
15             =head1 SYNOPSIS
16              
17             use Algorithm::Scale2x;
18              
19             # optionally exported
20             # use Algorithm::Scale2x qw( scale2x scale3x );
21              
22             # To start, you must grab a pixel plus all 8 surrounding pixels
23             my @pixels = $image->get_pixels( $x, $y );
24              
25             # scale2x - returns a 2x2 grid of scaled pixels
26             my @result2x = Algorithm::Scale2x::scale2x( @pixels );
27              
28             # scale3x - returns a 3x3 grid of scaled pixels
29             my @result3x = Algorithm::Scale2x::scale3x( @pixels );
30              
31             =head1 DESCRIPTION
32              
33             This module provides a generic implementation of the Scale2x and Scale3x algorithms.
34             Scale2x is described as:
35              
36             ...[a] real-time graphics effect able to increase the size of small bitmaps
37             guessing the missing pixels without interpolating pixels and blurring the images.
38              
39             =head1 METHODS
40              
41             =head2 scale2x( @pixels )
42              
43             Given a 3x3 grid of pixels (i.e color index numbers), it will expand the centre pixel
44             into 4 new pixels (i.e. 2x scale).
45              
46             +---+---+---+
47             | 0 | 1 | 2 | +----+----+
48             +---+---+---+ | 4A | 4B |
49             | 3 | 4 | 5 | => +----+----+
50             +---+---+---+ | 4C | 4D |
51             | 6 | 7 | 8 | +----+----+
52             +---+---+---+
53              
54             =cut
55              
56             sub scale2x {
57 2     2 1 662 my @pixels = @_;
58 2         4 my @E;
59              
60 2 100 66     12 if( $pixels[ 1 ] != $pixels[ 7 ] && $pixels[ 3 ] != $pixels[ 5 ] ) {
61 1 50       5 $E[ 0 ] = ( $pixels[ 3 ] == $pixels[ 1 ] ? $pixels[ 3 ] : $pixels[ 4 ] );
62 1 50       3 $E[ 1 ] = ( $pixels[ 1 ] == $pixels[ 5 ] ? $pixels[ 5 ] : $pixels[ 4 ] );
63 1 50       3 $E[ 2 ] = ( $pixels[ 3 ] == $pixels[ 7 ] ? $pixels[ 3 ] : $pixels[ 4 ] );
64 1 50       5 $E[ 3 ] = ( $pixels[ 7 ] == $pixels[ 5 ] ? $pixels[ 5 ] : $pixels[ 4 ] );
65             }
66             else {
67 1         4 @E = ( $pixels[ 4 ] ) x 4;
68             }
69              
70 2         6 return @E;
71             }
72              
73             =head2 scale3x( @pixels )
74              
75             Given a 3x3 grid of pixels (i.e color index numbers), it will expand the centre pixel
76             into 9 new pixels (i.e. 3x scale).
77              
78             +---+---+---+ +----+----+----+
79             | 0 | 1 | 2 | | 4A | 4B | 4C |
80             +---+---+---+ +----+----+----+
81             | 3 | 4 | 5 | => | 4D | 4E | 4F |
82             +---+---+---+ +----+----+----+
83             | 6 | 7 | 8 | | 4G | 4H | 4I |
84             +---+---+---+ +----+----+----+
85              
86             =cut
87              
88             sub scale3x {
89 2     2 1 932 my @pixels = @_;
90 2         3 my @E;
91              
92 2 100 66     10 if( $pixels[ 1 ] != $pixels[ 7 ] && $pixels[ 3 ] != $pixels[ 5 ] ) {
93 1 50       5 $E[ 0 ] = ( $pixels[ 3 ] == $pixels[ 1 ] ? $pixels[ 3 ] : $pixels[ 4 ] );
94 1 50 33     16 $E[ 1 ] = (
95             ( $pixels[ 3 ] == $pixels[ 1 ] && $pixels[ 4 ] != $pixels[ 2 ] ) ||
96             ( $pixels[ 1 ] == $pixels[ 5 ] && $pixels[ 4 ] != $pixels[ 0 ] )
97             ? $pixels[ 1 ] : $pixels[ 4 ]
98             );
99 1 50       3 $E[ 2 ] = ( $pixels[ 1 ] == $pixels[ 5 ] ? $pixels[ 5 ] : $pixels[ 4 ] );
100 1 50 33     11 $E[ 3 ] = (
101             ( $pixels[ 3 ] == $pixels[ 1 ] && $pixels[ 4 ] != $pixels[ 6 ] ) ||
102             ( $pixels[ 3 ] == $pixels[ 7 ] && $pixels[ 4 ] != $pixels[ 0 ] )
103             ? $pixels[ 3 ] : $pixels[ 4 ]
104             );
105 1         15 $E[ 4 ] = $pixels[ 4 ];
106 1 50 33     11 $E[ 5 ] = (
107             ( $pixels[ 1 ] == $pixels[ 5 ] && $pixels[ 4 ] != $pixels[ 8 ] ) ||
108             ( $pixels[ 7 ] == $pixels[ 5 ] && $pixels[ 4 ] != $pixels[ 2 ] )
109             ? $pixels[ 5 ] : $pixels[ 4 ]
110             );
111 1 50       3 $E[ 6 ] = ( $pixels[ 3 ] == $pixels[ 7 ] ? $pixels[ 3 ] : $pixels[ 4 ] );
112 1 50 33     10 $E[ 7 ] = (
113             ( $pixels[ 3 ] == $pixels[ 7 ] && $pixels[ 4 ] != $pixels[ 8 ] ) ||
114             ( $pixels[ 7 ] == $pixels[ 5 ] && $pixels[ 4 ] != $pixels[ 6 ] )
115             ? $pixels[ 7 ] : $pixels[ 4 ]
116             );
117 1 50       3 $E[ 8 ] = ( $pixels[ 7 ] == $pixels[ 5 ] ? $pixels[ 5 ] : $pixels[ 4 ] );
118             }
119             else {
120 1         3 @E = ( $pixels[ 4 ] ) x 9;
121             }
122              
123 2         9 return @E;
124             }
125              
126             =head1 SEE ALSO
127              
128             =over 4
129              
130             =item * http://scale2x.sourceforge.net/
131              
132             =item * http://scale2x.sourceforge.net/algorithm.html
133              
134             =back
135              
136             =head1 AUTHOR
137              
138             Brian Cassidy Ebricas@cpan.orgE
139              
140             =head1 COPYRIGHT AND LICENSE
141              
142             Copyright 2007-2009 by Brian Cassidy
143              
144             This library is free software; you can redistribute it and/or modify
145             it under the same terms as Perl itself.
146              
147             =cut
148              
149             1;