lib/App/FuguSeed/Matrix.pm

filter: Statementstmt 100% 112 / 112 Branchbran 100% 28 / 28 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 22 / 22 Podpod 100% 1 / 1 totaltotal 100% 183 / 183 CC 30 SCAR 19.1
CC
30
Cov
100%
_functions
14.0
_place
9.0
_finder
6.0
CRAP 6.7
« blib/lib/App/FuguSeed/Words.pm ↑ Summary blib/lib/App/FuguSeed/Check.pm »
1 # ex:ts=8 sw=4:
2 # $OpenBSD$
3 #
4 # Copyright (c) 2026 Dick Olsson <hi@senzilla.io>
5 #
6 # Permission to use, copy, modify, and distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9 #
10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  
18 package App::FuguSeed::Matrix;
19 2 our $VERSION = '0.2.0';
20  
21 2 ▶use v5.34;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶no feature qw(indirect multidimensional bareword_filehandles);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # App::FuguSeed::Matrix - the 25 x 25 modules of a Standard SeedQR.
27 #
28 # The module places the function patterns, the 44 codewords, the mask
29 # and the format information in the grid of QR-MATRIX. Module (0,0)
30 # is the top left corner, the first index is the row, and the second
31 # is the column. A module is 1 when it is dark, and 0 when it is
32 # light.
33 #
34 # Every method is a class method, and every method is pure: it takes
35 # values and it returns values. The module touches no stream, and it
36 # loads no module.
37 #
38 # The mask is pattern 0, always (D-08), so the module computes no
39 # penalty score and the format bits are one constant.
40 #
41 # Each constant of the QR standard names the clause or the table that
42 # it comes from (QR-PROGRAM-7). The standard is ISO/IEC 18004.
43  
44 # SIZE:
45 # The module count of one side. Version 2 is 25 x 25 modules
46 # (Table 1).
47 2 ▶use constant SIZE => 25;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 # FINDER:
50 # The side of one finder pattern, and the ring of its light
51 # modules (6.3.3).
52 2 ▶use constant FINDER => 7;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 ▶use constant FINDER_RING => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 # TIMING:
56 # The row and the column of the two timing patterns (6.3.5).
57 2 ▶use constant TIMING => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 # ALIGNMENT:
60 # The row and the column of the center of the one alignment
61 # pattern of version 2 (Table E.1). Its ring of light modules
62 # sits one module from the center (6.3.6).
63 2 ▶use constant ALIGNMENT => 18;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶use constant ALIGNMENT_RING => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 # DARK:
67 # The row of the dark module. It sits at (4 * version + 9, 8)
68 # (8.9).
69 2 ▶use constant DARK => 17;
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 # FORMAT:
72 # The 15 bits of the format information for error correction
73 # level L with mask pattern 0 (Table 25), the first bit first.
74 2 ▶use constant FORMAT => '111011111000100';
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 # FORMAT_ONE and FORMAT_TWO:
77 # The two positions of each format bit, in bit order
78 # (Figure 25). The first copy surrounds the finder pattern of
79 # the top left corner, and the second copy sits beside the
80 # other two finder patterns.
81 2 ▶use constant FORMAT_ONE => [
Subroutine BEGIN: called CC=1 SCAR=0.0
82 [ 8, 0 ], [ 8, 1 ], [ 8, 2 ], [ 8, 3 ], [ 8, 4 ], [ 8, 5 ],
83 [ 8, 7 ], [ 8, 8 ], [ 7, 8 ], [ 5, 8 ], [ 4, 8 ], [ 3, 8 ],
84 [ 2, 8 ], [ 1, 8 ], [ 0, 8 ],
85 2 ];
86 2 ▶use constant FORMAT_TWO => [
Subroutine BEGIN: called CC=1 SCAR=0.0
87 [ 24, 8 ], [ 23, 8 ], [ 22, 8 ], [ 21, 8 ], [ 20, 8 ], [ 19, 8 ],
88 [ 18, 8 ], [ 8, 17 ], [ 8, 18 ], [ 8, 19 ], [ 8, 20 ], [ 8, 21 ],
89 [ 8, 22 ], [ 8, 23 ], [ 8, 24 ],
90 2 ];
91  
92 # CODEWORD_BITS:
93 # The bit width of one codeword (8.4.8).
94 2 ▶use constant CODEWORD_BITS => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 # $class->build($codewords):
97 # The matrix of the codewords of the array reference
98 # $codewords, as a reference to 25 rows of 25 modules.
99 4 ▶sub build ( $class, $codewords )
Subroutine build: called CC=1 SCAR=0.0
Pod documented
100 {
101 4 my $matrix = [ map { [ (0) x SIZE ] } 1 .. SIZE ];
102 4 my $reserved = [ map { [ (0) x SIZE ] } 1 .. SIZE ];
103  
104 4 $class->_functions( $matrix, $reserved );
105 4 $class->_place( $matrix, $reserved, $codewords );
106 4 $class->_format($matrix);
107  
108 4 return $matrix;
109 }
110  
111 # $class->_functions($matrix, $reserved):
112 # Draw the function patterns of QR-MATRIX-2, and reserve the
113 # modules that hold the format information.
114 4 ▶sub _functions ( $, $matrix, $reserved )
Subroutine _functions: called CC=14 SCAR=26.4
115 {
116 # The three finder patterns, each with its separator
117 # (6.3.3). The separator is the light frame around the
118 # pattern, and it falls outside the matrix on two sides.
119 4 for my $corner ( [ 0, 0 ], [ 0, SIZE - FINDER ], [ SIZE - FINDER, 0 ] )
120 {
121 12 my ( $top, $left ) = @{$corner};
122 12 for my $row ( -1 .. FINDER ) {
123 108 for my $column ( -1 .. FINDER ) {
124 972 ▶ next if $top + $row < 0 || $top + $row >= SIZE;
Branch: 1 branch
BranchTrueFalseTotal
if $top + $row < 0 or $top + $row >= 25 108 864 972
Condition100%3/3
$top + $row < 0 or $top + $row >= 25
l!l&&r!l&&!r
7236864
MC/DC100%2/2
$top + $row < 0 or $top + $row >= 25
$top + $row < 0 $top + $row >= 25
Truth table100%3/3
A or B
A= $top + $row < 0B= $top + $row >= 25
ABresult
1X1
011
000
125 next
126 864 ▶ if $left + $column < 0
Branch: 1 branch
BranchTrueFalseTotal
if $left + $column < 0 or $left + $column >= 25 96 768 864
Condition100%3/3
$left + $column < 0 or $left + $column >= 25
l!l&&r!l&&!r
6432768
MC/DC100%2/2
$left + $column < 0 or $left + $column >= 25
$left + $column < 0 $left + $column >= 25
Truth table100%3/3
A or B
A= $left + $column < 0B= $left + $column >= 25
ABresult
1X1
011
000
127 || $left + $column >= SIZE;
128 768 _set(
129 $matrix, $reserved,
130 $top + $row,
131 $left + $column,
132 _finder( $row, $column ) );
133 }
134 }
135 }
136  
137 # The two timing patterns (6.3.5). They run between the
138 # separators, and they start with a dark module.
139 4 for my $step ( FINDER + 1 .. SIZE - FINDER - 2 ) {
140 36 ▶ my $dark = $step % 2 == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$step % 2 == 0 ? : 20 16 36
141 36 _set( $matrix, $reserved, TIMING, $step, $dark );
142 36 _set( $matrix, $reserved, $step, TIMING, $dark );
143 }
144  
145 # The one alignment pattern of version 2 (6.3.6).
146 4 my $edge = ALIGNMENT_RING + 1;
147 4 for my $row ( -$edge .. $edge ) {
148 20 for my $column ( -$edge .. $edge ) {
149 100 my $ring = _ring( $row, $column );
150 100 ▶ _set(
Branch: 1 branch
BranchTrueFalseTotal
$ring == 1 ? : 32 68 100
151 $matrix, $reserved,
152 ALIGNMENT + $row,
153 ALIGNMENT + $column,
154 $ring == ALIGNMENT_RING ? 0 : 1
155 );
156 }
157 }
158  
159 # The dark module (8.9), and the modules of the format
160 # information. A format module keeps its value until
161 # _format writes it, and no codeword bit reaches it.
162 4 _set( $matrix, $reserved, DARK, 8, 1 );
163 4 for my $position ( @{ +FORMAT_ONE }, @{ +FORMAT_TWO } ) {
164 120 $reserved->[ $position->[0] ][ $position->[1] ] = 1;
165 }
166  
167 4 return;
168 }
169  
170 # $class->_place($matrix, $reserved, $codewords):
171 # Write the 352 codeword bits in the placement order of
172 # QR-MATRIX-3, and apply the mask of QR-MATRIX-4. The order
173 # starts at the bottom right corner and runs in column pairs,
174 # upward and downward in turn, over the timing column. The 7
175 # remainder bits after the codewords are 0.
176 4 ▶sub _place ( $, $matrix, $reserved, $codewords )
Subroutine _place: called CC=9 SCAR=22.0
177 {
178 my $bits = join q{},
179 4 map { sprintf '%0' . CODEWORD_BITS . 'b', $_ } @{$codewords};
180  
181 4 my $next = 0;
182 4 my $upward = 1;
183 4 my $column = SIZE - 1;
184  
185 4 ▶ while ( $column > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column > 0) 48 4 52
186 48 ▶ $column-- if $column == TIMING;
Branch: 1 branch
BranchTrueFalseTotal
if $column == 6 4 44 48
187 48 ▶ my @rows =
Branch: 1 branch
BranchTrueFalseTotal
$upward ? : 24 24 48
188 $upward ? reverse( 0 .. SIZE - 1 ) : ( 0 .. SIZE - 1 );
189 48 for my $row (@rows) {
190 1200 for my $step ( $column, $column - 1 ) {
191 2400 ▶ next if $reserved->[$row][$step];
Branch: 1 branch
BranchTrueFalseTotal
if $reserved->[$row][$step] 964 1436 2400
192 1436 ▶ my $bit =
Branch: 1 branch
BranchTrueFalseTotal
$next < length $bits ? : 1408 28 1436
193 $next < length $bits
194 ? substr( $bits, $next, 1 )
195 : 0;
196 1436 $next++;
197 1436 ▶ $bit = 1 - $bit if ( $row + $step ) % 2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($row + $step) % 2 == 0 716 720 1436
198 1436 $matrix->[$row][$step] = $bit;
199 }
200 }
201 48 $upward = !$upward;
202 48 $column -= 2;
203 }
204  
205 4 return;
206 }
207  
208 # $class->_format($matrix):
209 # Write the 15 format bits of QR-MATRIX-5 in both positions.
210 4 ▶sub _format ( $, $matrix )
Subroutine _format: called CC=3 SCAR=11.0
211 {
212 4 my @bits = split //, FORMAT;
213  
214 4 for my $bit ( 0 .. $#bits ) {
215 60 for my $copy ( FORMAT_ONE->[$bit], FORMAT_TWO->[$bit] ) {
216 120 $matrix->[ $copy->[0] ][ $copy->[1] ] = $bits[$bit];
217 }
218 }
219  
220 4 return;
221 }
222  
223 # _finder($row, $column):
224 # The module of the finder pattern at ($row, $column), with the
225 # top left corner of the pattern at (0,0). A module of the
226 # separator is light (6.3.3).
227 768 ▶sub _finder ( $row, $column )
Subroutine _finder: called CC=6 SCAR=17.9
228 {
229 768 ▶ return 0 if $row < 0 || $row >= FINDER;
Branch: 1 branch
BranchTrueFalseTotal
if $row < 0 or $row >= 7 96 672 768
Condition100%3/3
$row < 0 or $row >= 7
l!l&&r!l&&!r
3264672
MC/DC100%2/2
$row < 0 or $row >= 7
$row < 0 $row >= 7
Truth table100%3/3
A or B
A= $row < 0B= $row >= 7
ABresult
1X1
011
000
230 672 ▶ return 0 if $column < 0 || $column >= FINDER;
Branch: 1 branch
BranchTrueFalseTotal
if $column < 0 or $column >= 7 84 588 672
Condition100%3/3
$column < 0 or $column >= 7
l!l&&r!l&&!r
2856588
MC/DC100%2/2
$column < 0 or $column >= 7
$column < 0 $column >= 7
Truth table100%3/3
A or B
A= $column < 0B= $column >= 7
ABresult
1X1
011
000
231  
232 588 my $middle = ( FINDER - 1 ) / 2;
233 588 my $ring = _ring( $row - $middle, $column - $middle );
234  
235 588 ▶ return $ring == FINDER_RING ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$ring == 2 ? : 192 396 588
236 }
237  
238 # _ring($row, $column):
239 # The ring of the module at ($row, $column) around the center
240 # (0,0) of a square pattern: 0 at the center, and one more for
241 # each square around it.
242 688 ▶sub _ring ( $row, $column )
Subroutine _ring: called CC=2 SCAR=6.9
243 {
244 688 my $height = abs $row;
245 688 my $width = abs $column;
246  
247 688 ▶ return $height > $width ? $height : $width;
Branch: 1 branch
BranchTrueFalseTotal
$height > $width ? : 248 440 688
248 }
249  
250 # _set($matrix, $reserved, $row, $column, $dark):
251 # Write one function module, and reserve it against the
252 # codeword bits.
253 944 ▶sub _set ( $matrix, $reserved, $row, $column, $dark )
Subroutine _set: called CC=1 SCAR=0.0
254 {
255 944 $matrix->[$row][$column] = $dark;
256 944 $reserved->[$row][$column] = 1;
257  
258 944 return;
259 }
260  
261 2 1;
« blib/lib/App/FuguSeed/Words.pm ↑ Summary blib/lib/App/FuguSeed/Check.pm »