blib/lib/Math/DCT.pm

filter: Statementstmt 100% 47 / 47 Branchbran 100% 24 / 24 Conditioncond 100% 15 / 15 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 100% 104 / 104 CC 19 SCAR 22.0
CC
19
Cov
100%
dct
14.0
dct2d
4.0
dct1d
3.0
CRAP 9.0
↑ Summary DCT.xs »
1 package Math::DCT;
2  
3 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Math::DCT - 1D and NxN 2D Fast Discreet Cosine Transforms (DCT-II)
10  
11 =head1 SYNOPSIS
12  
13 use Math::DCT qw/dct dct1d dct2d/;
14  
15 my $dct1d = dct([[1,2,3,4]]);
16 $dct1d = dct1d([1,2,3,4]);
17  
18 my $dct2d = dct([[1,2],[3,4]]);
19 $dct2d = dct2d([1,2,3,4]);
20  
21 =head1 VERSION
22  
23 Version 0.03
24  
25 =cut
26  
27 4 our $VERSION = '0.03';
28  
29 4 require XSLoader;
30 4 XSLoader::load('Math::DCT', $VERSION);
31  
32 4 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 4 our @EXPORT_OK = qw(
35 dct
36 dct1d
37 dct2d
38 );
39  
40 4 our %EXPORT_TAGS;
41 4 $EXPORT_TAGS{all} = [@EXPORT_OK];
42  
43 =head1 DESCRIPTION
44  
45 An unscaled DCT-II implementation for 1D and NxN 2D matrices implemented in XS.
46 For array sizes which are a power of 2, a fast algorithm (FCT) described by Lee is
47 used (with the addition of a coefficient table that makes it even faster than some
48 common implementations), plus an unscaled version of the Arai, Agui, Nakajima
49 algorithm is used for size 1x8, 8x8 matrices.
50  
51 The module was written for a perceptual hash project that needed 32x32 DCT-II,
52 and on a 2.5GHz 2015 Macbook Pro over 11500/s per thread are processed (C<dct2d> function).
53 The common 8x8 DCT-II uses a special path, (abut 250000 transforms per sec on the same CPU),
54 although for most image/video applications that require 8x8 DCT there are much faster
55 implementations (SIMD, approximations etc) that usually produce an already scaled result
56 for the specific application.
57  
58 None of the algorithms used on this module are approximate, the test suite verifies
59 against a naive DCT-II implementation with a tolerance of 1e-08.
60  
61 =head1 METHODS
62
63 =head2 C<dct>
64  
65 my $dct = dct([[1,2],[3,4]]);
66  
67 Pass an array (ref) of either a single array, or N N-length arrays for 1D and 2D
68 DCT-II calculation respectivelly. The output will be an arrayref of array(s) with
69 the result of the transform.
70  
71 =cut
72  
73 sub dct {
74 19 ▶ my $vector = shift;
Subroutine dct: called CC=14 SCAR=26.4
Pod documented
75 19 ▶ die "Expect array of array(s)"
Branch: 1 branch
BranchTrueFalseTotal
unless $vector and ref $vector eq "ARRAY" and $vector->[0] and ref $vector->[0] eq "ARRAY" 2 15 17
Condition100%9/9
$vector and ref $vector eq "ARRAY" and $vector->[0] and ref $vector->[0] eq "ARRAY"
!ll&&!rl&&r
3115
$vector and ref $vector eq "ARRAY" and $vector->[0]
!ll&&!rl&&r
2116
$vector and ref $vector eq "ARRAY"
!ll&&!rl&&r
1117
MC/DC100%4/4
$vector and ref $vector eq "ARRAY" and $vector->[0] and ref $vector->[0] eq "ARRAY"
$vector ref $vector eq "ARRAY" $vector->[0] ref $vector->[0] eq "ARRAY"
Truth table100%5/5
A and B and C and D
A= $vectorB= ref $vector eq "ARRAY"C= $vector->[0]D= ref $vector->[0] eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
76 unless $vector && ref $vector eq 'ARRAY'
77 && $vector->[0] && ref $vector->[0] eq 'ARRAY';
78  
79 15 my $dim = scalar(@$vector);
80 15 my $sz = scalar(@{$vector->[0]});
81 15 ▶ die "Expect 1d or NxN 2d arrays" unless $dim == 1 || $dim == $sz;
Branch: 1 branch
BranchTrueFalseTotal
unless $dim == 1 or $dim == $sz 1 14 15
Condition100%3/3
$dim == 1 or $dim == $sz
l!l&&r!l&&!r
771
MC/DC100%2/2
$dim == 1 or $dim == $sz
$dim == 1 $dim == $sz
Truth table100%3/3
A or B
A= $dim == 1B= $dim == $sz
ABresult
1X1
011
000
82  
83 14 my $pack;
84 14 ▶ for (my $x = 0; $x < $dim; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < $dim) 131 14 145
85 131 $pack .= pack "d$sz", @{$vector->[$x]}
86 }
87  
88 14 ▶ if ($dim > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dim > 1) { } 7 7 14
89 7 ▶ $sz == 8
Branch: 2 branches
BranchTrueFalseTotal
$sz == 8 ? : 1 6 7
0 == ($sz & $sz - 1) ? : 4 2 6
90 ? fct8_2d($pack)
91 : 0 == ($sz & ($sz - 1)) ? fast_dct_2d($pack, $sz) : dct_2d($pack, $sz);
92 } else {
93 7 ▶ $sz == 8
Branch: 2 branches
BranchTrueFalseTotal
$sz == 8 ? : 1 6 7
0 == ($sz & $sz - 1) ? : 4 2 6
94 ? fct8_1d($pack)
95 : 0 == ($sz & ($sz - 1)) ? fast_dct_1d($pack, $sz) : dct_1d($pack, $sz);
96 }
97  
98 14 my $result;
99 14 foreach (0..$dim-1) {
100 131 $result->[$_] = [unpack "d".($sz), substr $pack, $_ * $sz*8, $sz*8];
101 }
102 14 return $result;
103 }
104  
105 =head2 C<dct1d>
106  
107 my $dct = dct1d([1,2,3]);
108  
109 Pass an array (ref) for a 1D DCT-II calculation. The output will be an arrayref
110 with the result of the transform.
111  
112 =cut
113  
114 sub dct1d {
115 7 ▶ my $input = shift;
Subroutine dct1d: called CC=3 SCAR=11.0
Pod documented
116 7 my $sz = scalar @$input;
117 7 my $pack = pack "d$sz", @$input;
118 7 ▶ $sz == 8
Branch: 2 branches
BranchTrueFalseTotal
$sz == 8 ? : 1 6 7
0 == ($sz & $sz - 1) ? : 4 2 6
119 ? fct8_1d($pack)
120 : 0 == ($sz & ($sz - 1)) ? fast_dct_1d($pack, $sz) : dct_1d($pack, $sz);
121 7 my @result = unpack "d$sz", $pack;
122 7 return \@result;
123 }
124  
125 =head2 C<dct2d>
126  
127 my $dct = dct2d(
128 [1,2,3,4], # Arrayref containing your NxN matrix
129 2 # Optionally, the size N of your array (sqrt of its length)
130 );
131  
132 Pass an array (ref) for a 2D DCT-II calculation. The length of the array is expected
133 to be a square (as only NxN arrays are supported) - you can optionally pass N as
134 the second argument to avoid a C<sqrt> calculation.
135 The output will be an arrayref with the result of the transform.
136  
137 If your 2D data is available in a 1d array as is usual with most image manipulation
138 etc cases, this function will be faster than C<dct>, as the DCT calculation is
139 in any case done on a 1D array, hence you save the cost of conversion.
140  
141 =cut
142  
143 sub dct2d {
144 10 ▶ my $input = shift;
Subroutine dct2d: called CC=4 SCAR=13.9
Pod documented
145 10 ▶ my $sz = shift || sqrt(scalar @$input);
Condition100%3/3
shift() || sqrt scalar @$input
l!l&&r!l&&!r
172
MC/DC100%2/2
shift() || sqrt scalar @$input
shift() sqrt scalar @$input
Truth table100%3/3
A || B
A= shift()B= sqrt scalar @$input
ABresult
1X1
011
000
146 9 my $pack = pack "d".($sz*$sz), @$input;
147 9 ▶ $sz == 8
Branch: 2 branches
BranchTrueFalseTotal
$sz == 8 ? : 1 8 9
0 == ($sz & $sz - 1) ? : 6 2 8
148 ? fct8_2d($pack)
149 : 0 == ($sz & ($sz - 1)) ? fast_dct_2d($pack, $sz) : dct_2d($pack, $sz);
150 9 my @result = unpack "d".($sz*$sz), $pack;
151 9 return \@result;
152 }
153  
154 =head1 USAGE NOTES
155  
156 The C functions are not exported, but theoretically you could use them directly
157 if you do your own C<pack/unpack>. The fast versions for power-of-2 size arrays
158 are C<fast_dct_1d> and C<fast_dct_2d>, while the generic versions are C<dct_1d>
159 and C<dct_2d>. The specialized size-8 versions are C<fct8_1d> and C<fct8_2d>.
160 First argument is a C<char *> (use C<pack "dN">), second is the size N (except
161 for the fct8* functions which don't need a second argument).
162  
163 =head1 ACKNOWLEDGEMENTS
164  
165 Some code from Project Nayuki was adapted and improved upon.
166  
167 (L<https://www.nayuki.io/page/fast-discrete-cosine-transform-algorithms>)
168  
169 =head1 AUTHOR
170  
171 Dimitrios Kechagias, C<< <dkechag at cpan.org> >>
172
173 =head1 BUGS
174  
175 Please report any bugs or feature requests to C<bug-math-dct at rt.cpan.org>,
176 or through the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Math-DCT>.
177 I will be notified, and then you'll automatically be notified of progress on your
178 bug as I make changes.
179  
180 =head1 GIT
181  
182 L<https://github.com/SpareRoom/Math-DCT>
183
184 =head1 COPYRIGHT & LICENSE
185  
186 Copyright (C) 2019, SpareRoom.com
187  
188 This program is free software; you can redistribute
189 it and/or modify it under the same terms as Perl itself.
190  
191 =cut
192  
193 4 1;
↑ Summary DCT.xs »