File Coverage

blib/lib/Barcode/DataMatrix/Reed.pm
Criterion Covered Total %
statement 28 28 100.0
branch 7 12 58.3
condition n/a
subroutine 6 6 100.0
pod 3 3 100.0
total 44 49 89.8


line stmt bran cond sub pod time code
1             package Barcode::DataMatrix::Reed;
2              
3             =head1 Barcode::DataMatrix::Reed
4              
5             This is just a renamed version of Algorithm::DataMatrix::Reed
6             by Mons Anderson
7             from http://code.google.com/p/perl-ex/
8              
9             For a rough explanation of the structure of this code, see
10             L.
11             Note that the link is not the basis of the implementation (this is unknown),
12             however it helps explain how Reed-Solomon encoding is implemented and hence
13             how the code below works.
14              
15             =cut
16              
17 2     2   6 use strict;
  2         3  
  2         42  
18 2     2   6 use warnings;
  2         2  
  2         29  
19 2     2   6 use Carp;
  2         2  
  2         707  
20              
21             =head2 DEBUG
22              
23             Control whether or not debugging output is printed.
24              
25             =cut
26              
27 48     48 1 88 sub DEBUG { 0 }
28              
29             =head2 mult (x, y)
30              
31             Multiply two Galois field element values and return the result.
32              
33             =cut
34              
35             sub mult {
36 57966     57966 1 34077 my ($x, $y) = @_;
37 57966 100       58931 return 0 unless $x * $y;
38 57714         60928 return $Barcode::DataMatrix::Constants::GFI[($Barcode::DataMatrix::Constants::GFL[$x] + $Barcode::DataMatrix::Constants::GFL[$y]) % 255];
39             }
40              
41             =head2 encode (ai, j)
42              
43             Encode the message array and return it.
44              
45             =cut
46              
47             sub encode {
48 16     16 1 17 my ($ai,$j) = @_;
49 16         28 my $i = @$ai;
50 16         38 for (0..$#$ai) {
51 1226 50       1476 ( $ai->[$_] & 0xFF ) != $ai->[$_] and warn("number $ai->[$_] at index $_ is not a byte size"), $ai->[$_] = $ai->[$_] & 0xFF;
52             }
53 16 50       34 warn "CalcReed: ai [@$ai], $j\n" if DEBUG;
54              
55 16 50       49 my $p = exists $Barcode::DataMatrix::Constants::POLY{$j} ? $Barcode::DataMatrix::Constants::POLY{$j} : $Barcode::DataMatrix::Constants::POLY{68};
56 16 50       19 warn "CalcReed: poly [@$p]\n" if DEBUG;
57              
58 16         141 @$ai[ $i .. $i + $j - 1 ] = (0) x $j;
59 16         44 for my $l(0 .. $i - 1) {
60 1226         901 my $word0 = ($ai->[$i] ^ $ai->[$l]);
61 1226         1100 for my $i1 (0 .. $j - 2) {
62 56740         52049 $ai->[$i + $i1] = ( $ai->[$i + $i1 + 1] ^ mult($word0, $p->[$i1]) );
63             }
64 1226         1217 $ai->[$i+$j-1] = mult($word0, $p->[$j - 1]);
65             }
66 16 50       31 warn "CalcReed: result [@$ai]\n" if DEBUG;
67 16         74 return $ai;
68             }
69              
70             1;