File Coverage

blib/lib/Barcode/DataMatrix/CharDataFiller.pm
Criterion Covered Total %
statement 78 100 78.0
branch 15 18 83.3
condition 34 42 80.9
subroutine 8 10 80.0
pod 8 8 100.0
total 143 178 80.3


line stmt bran cond sub pod time code
1             package Barcode::DataMatrix::CharDataFiller;
2              
3 2     2   6 use strict;
  2         696  
  2         48  
4 2     2   7 use warnings;
  2         2  
  2         1251  
5              
6             =head1 Barcode::DataMatrix::CharDataFiller
7              
8             Handle filling character data within the data matrix array.
9              
10             The documentation for the methods in this class has been adapted from the
11             comments in
12             L.
13              
14             =head2 new
15              
16             Construct a C object.
17              
18             =cut
19              
20             sub new {
21 12     12 1 31 my $self = bless {}, shift;
22 12         47 @$self{qw( ncol nrow array )} = @_;
23 12         36 $self->fill();
24 12         56 return $self;
25             }
26              
27             =head2 module (i, j, k, l)
28              
29             Places "chr+bit" with the appropriate wrapping within the array.
30              
31             =cut
32              
33             sub module {
34 14064     14064 1 9228 my ($self,$i,$j,$k,$l) = @_;
35 14064 100       14998 if($i < 0) {
36 287         187 $i += $self->{nrow};
37 287         229 $j += 4 - ($self->{nrow} + 4) % 8;
38             }
39 14064 100       14025 if($j < 0) {
40 369         246 $j += $self->{ncol};
41 369         306 $i += 4 - ($self->{ncol} + 4) % 8;
42             }
43 14064         11053 $self->{array}->[$i * $self->{ncol} + $j] = 10 * $k + $l;
44 14064         8778 return;
45             }
46              
47             =head2 utah (i, j, k)
48              
49             Places the 8 bits of a utah-shaped symbol character.
50              
51             =cut
52              
53             sub utah {
54 1754     1754 1 1226 my ($self,$i,$j,$k) = @_;
55 1754         1802 $self->module($i - 2, $j - 2, $k, 1);
56 1754         1797 $self->module($i - 2, $j - 1, $k, 2);
57 1754         1849 $self->module($i - 1, $j - 2, $k, 3);
58 1754         1840 $self->module($i - 1, $j - 1, $k, 4);
59 1754         1647 $self->module($i - 1, $j, $k, 5);
60 1754         1777 $self->module($i, $j - 2, $k, 6);
61 1754         1683 $self->module($i, $j - 1, $k, 7);
62 1754         1528 $self->module($i, $j, $k, 8);
63 1754         1065 return;
64             }
65              
66             =head2 corner1 (i)
67              
68             Places 8 bits of the first of the four special corner cases.
69              
70             =cut
71              
72             sub corner1 {
73 3     3 1 3 my ($self,$i) = @_;
74 3         6 my ($ncol,$nrow) = @$self{qw( ncol nrow )};
75 3         4 $self->module($nrow - 1, 0, $i, 1);
76 3         6 $self->module($nrow - 1, 1, $i, 2);
77 3         5 $self->module($nrow - 1, 2, $i, 3);
78 3         9 $self->module(0, $ncol - 2, $i, 4);
79 3         8 $self->module(0, $ncol - 1, $i, 5);
80 3         6 $self->module(1, $ncol - 1, $i, 6);
81 3         4 $self->module(2, $ncol - 1, $i, 7);
82 3         8 $self->module(3, $ncol - 1, $i, 8);
83 3         3 return;
84             }
85              
86             =head2 corner2 (i)
87              
88             Places 8 bits of the second of the four special corner cases.
89              
90             =cut
91              
92             sub corner2 { #(int i)
93 1     1 1 2 my ($self,$i) = @_;
94 1         3 my ($ncol,$nrow) = @$self{qw( ncol nrow )};
95 1         3 $self->module($nrow - 3, 0, $i, 1);
96 1         3 $self->module($nrow - 2, 0, $i, 2);
97 1         3 $self->module($nrow - 1, 0, $i, 3);
98 1         3 $self->module(0, $ncol - 4, $i, 4);
99 1         2 $self->module(0, $ncol - 3, $i, 5);
100 1         2 $self->module(0, $ncol - 2, $i, 6);
101 1         2 $self->module(0, $ncol - 1, $i, 7);
102 1         2 $self->module(1, $ncol - 1, $i, 8);
103 1         1 return;
104             }
105              
106             =head2 corner3 (i)
107              
108             Places 8 bits of the third of the four special corner cases.
109              
110             =cut
111              
112             sub corner3 { #(int i)
113 0     0 1 0 my ($self,$i) = @_;
114 0         0 my ($ncol,$nrow) = @$self{qw( ncol nrow )};
115 0         0 $self->module($nrow - 3, 0, $i, 1);
116 0         0 $self->module($nrow - 2, 0, $i, 2);
117 0         0 $self->module($nrow - 1, 0, $i, 3);
118 0         0 $self->module(0, $ncol - 2, $i, 4);
119 0         0 $self->module(0, $ncol - 1, $i, 5);
120 0         0 $self->module(1, $ncol - 1, $i, 6);
121 0         0 $self->module(2, $ncol - 1, $i, 7);
122 0         0 $self->module(3, $ncol - 1, $i, 8);
123 0         0 return;
124             }
125              
126             =head2 corner4 (i)
127              
128             Places 8 bits of the fourth of the four special corner cases.
129              
130             =cut
131              
132             sub corner4 { #(int i)
133 0     0 1 0 my ($self,$i) = @_;
134 0         0 my ($ncol,$nrow) = @$self{qw( ncol nrow )};
135 0         0 $self->module($nrow - 1, 0, $i, 1);
136 0         0 $self->module($nrow - 1, $ncol - 1, $i, 2);
137 0         0 $self->module(0, $ncol - 3, $i, 3);
138 0         0 $self->module(0, $ncol - 2, $i, 4);
139 0         0 $self->module(0, $ncol - 1, $i, 5);
140 0         0 $self->module(1, $ncol - 3, $i, 6);
141 0         0 $self->module(1, $ncol - 2, $i, 7);
142 0         0 $self->module(1, $ncol - 1, $i, 8);
143 0         0 return;
144             }
145              
146             =head2 fill
147              
148             Fills an nrow x ncol array with appropriate values.
149              
150             =cut
151              
152             sub fill { # (int ncol; int nrow; int array;) : void
153 12     12 1 17 my $self = shift;
154 12         23 my ($ncol,$nrow,$array) = @$self{qw( ncol nrow array )};
155 12         12 my $i = 1;
156 12         12 my $j = 4;
157 12         14 my $k = 0;
158 12         24 for(my $l = 0; $l < $nrow; $l++) {
159 350         387 for(my $i1 = 0; $i1 < $ncol; $i1++) {
160 14076         16399 $array->[$l * $ncol + $i1] = 0;
161             }
162             }
163 12   100     13 do {
164 89 100 66     141 $self->corner1($i++) if $j == $nrow && $k == 0;
165 89 50 66     159 $self->corner2($i++) if $j == $nrow - 2 && $k == 0 && $ncol % 4 != 0;
      66        
166 89 50 66     156 $self->corner3($i++) if $j == $nrow - 2 && $k == 0 && $ncol % 8 == 4;
      66        
167 89 50 100     191 $self->corner4($i++) if $j == $nrow + 4 && $k == 2 && $ncol % 8 == 0;
      66        
168 89   100     63 do {
169 1010 100 66     3731 $self->utah($j, $k, $i++) if $j < $nrow && $k >= 0 && $array->[$j * $ncol + $k] == 0;
      100        
170 1010         611 $j -= 2;
171 1010         2455 $k += 2;
172             } while($j >= 0 && $k < $ncol);
173 89         55 $j++;
174 89         63 $k += 3;
175 89   100     57 do {
176 1010 100 100     3809 $self->utah($j, $k, $i++) if $j >= 0 && $k < $ncol && $array->[$j * $ncol + $k] == 0;
      66        
177 1010         627 $j += 2;
178 1010         2479 $k -= 2;
179             } while($j < $nrow && $k >= 0);
180 89         60 $j += 3;
181 89         221 $k++;
182             } while($j < $nrow || $k < $ncol);
183 12 100       29 $array->[$nrow * $ncol - 1] = $array->[($nrow - 1) * $ncol - 2] = 1
184             if($array->[$nrow * $ncol - 1] == 0);
185 12         14 return;
186             }
187              
188             1;