source: trunk/Packages/lazbarcodes/src/lbc_reedsolomon.pas

Last change on this file was 123, checked in by chronos, 3 years ago
  • Added: QR code image visible in contact others tab. It can be saved as image to file.
File size: 3.2 KB
Line 
1unit lbc_reedsolomon;
2
3{$mode objfpc}{$H+}
4
5interface
6
7procedure rs_init_gf(poly: Integer);
8procedure rs_init_code(nsym: Integer; index: Integer);
9procedure rs_encode(len: Integer; data: PBYTE; res: PBYTE);
10procedure rs_encode_long(len: Integer; data: PCardinal; res: PCardinal);
11procedure rs_free();
12
13implementation
14
15var
16// gfpoly: integer;
17// symsize: integer; // in bits
18 logmod: integer; // 2**symsize - 1
19 rlen: integer;
20
21 logt: PInteger = nil;
22 alog: PInteger = nil;
23 rspoly: PInteger = nil;
24
25procedure rs_init_gf(poly: Integer);
26var
27 b: Integer;
28 p: Integer;
29 v: Integer;
30 m: Integer;
31begin
32 b := 1;
33 m := 0;
34 while b <= poly do
35 begin
36 Inc (m);
37 b := b shl 1;
38 end;
39 b := b shr 1;
40 Dec (m);
41// gfpoly := poly;
42// symsize := m;
43 logmod := (1 shl m) - 1;
44 logt := PInteger (GetMem (SizeOf (Integer) * (logmod + 1)));
45 alog := PInteger (GetMem (SizeOf (Integer) * logmod));
46 p := 1;
47 v := 0;
48 while v < logmod do
49 begin
50 alog[v] := p;
51 logt[p] := v;
52 p := p shl 1;
53 if (p and b)<>0 then
54 begin
55 p:=p xor poly;
56 end;
57 Inc (v);
58 end;
59end;
60
61procedure rs_init_code(nsym: Integer; index: Integer);
62var
63 k: Integer;
64 i: Integer;
65begin
66 rspoly := PInteger (GetMem (SizeOf (Integer) * (nsym + 1)));
67 rlen := nsym;
68 rspoly[0] := 1;
69 i := 1;
70 while i <= nsym do
71 begin
72 rspoly[i] := 1;
73 k := i - 1;
74 while k > 0 do
75 begin
76 if rspoly[k]<>0 then
77 begin
78 rspoly[k] := alog[(logt[rspoly[k]] + index) mod logmod];
79 end;
80 rspoly[k] := rspoly[k] xor rspoly[k - 1];
81 Dec (k);
82 end;
83 rspoly[0] := alog[(logt[rspoly[0]] + index) mod logmod];
84 Inc (index);
85 Inc (i);
86 end;
87end;
88
89procedure rs_encode(len: Integer; data: PBYTE; res: PBYTE);
90var
91 k: Integer;
92 m: Integer;
93 i: Integer;
94begin
95 i := 0;
96 while i < rlen do
97 begin
98 res[i] := 0;
99 Inc (i);
100 end;
101 i := 0;
102 while i < len do
103 begin
104 m := res[rlen - 1] xor data[i];
105 k := rlen - 1;
106 while k > 0 do
107 begin
108 if (m<>0) and (rspoly[k]<>0) then
109 begin
110 res[k] := integer(res[k - 1]) xor alog[(logt[m] + logt[rspoly[k]]) mod logmod];
111 end else begin
112 res[k] := integer(res[k - 1]);
113 end;
114 Dec (k);
115 end;
116 if (m<>0) and (rspoly[0]<>0) then
117 begin
118 res[0] := alog[(logt[m] + logt[rspoly[0]]) mod logmod];
119 end else begin
120 res[0] := 0;
121 end;
122 Inc (i);
123 end;
124end;
125
126procedure rs_encode_long(len: Integer; data: PCardinal; res: PCardinal);
127var
128 k: Integer;
129 m: Integer;
130 i: Integer;
131begin
132 i := 0;
133 while i < rlen do
134 begin
135 res[i] := 0;
136 Inc (i);
137 end;
138 i := 0;
139 while i < len do
140 begin
141 m := res[rlen - 1] xor data[i];
142 k := rlen - 1;
143 while k > 0 do
144 begin
145 if (m<>0) and (rspoly[k]<>0) then
146 begin
147 res[k] := Integer(res[k - 1]) xor alog[(logt[m] + logt[rspoly[k]]) mod logmod];
148 end else begin
149 res[k] := res[k - 1];
150 end;
151 Dec (k);
152 end;
153 if (m<>0) and (rspoly[0]<>0) then
154 begin
155 res[0] := alog[(logt[m] + logt[rspoly[0]]) mod logmod];
156 end else begin
157 res[0] := 0;
158 end;
159 Inc (i);
160 end;
161end;
162
163procedure rs_free();
164begin
165 freeMem (logt);
166 freeMem (alog);
167 freeMem (rspoly);
168 rspoly := nil;
169end;
170
171end.
172
Note: See TracBrowser for help on using the repository browser.