Skip to content

Commit

Permalink
decoding correct value
Browse files Browse the repository at this point in the history
  • Loading branch information
odahcam committed Oct 18, 2019
1 parent 89c0328 commit 31be35d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 83 deletions.
104 changes: 54 additions & 50 deletions src/core/pdf417/decoder/DecodedBitStreamParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,34 @@ function getEXP900(): BigInt[] {
*/
export default /*final*/ class DecodedBitStreamParser {

private static /*final*/ TEXT_COMPACTION_MODE_LATCH: /*int*/ number = 900;
private static /*final*/ BYTE_COMPACTION_MODE_LATCH: /*int*/ number = 901;
private static /*final*/ NUMERIC_COMPACTION_MODE_LATCH: /*int*/ number = 902;
private static /*final*/ BYTE_COMPACTION_MODE_LATCH_6: /*int*/ number = 924;
private static /*final*/ ECI_USER_DEFINED: /*int*/ number = 925;
private static /*final*/ ECI_GENERAL_PURPOSE: /*int*/ number = 926;
private static /*final*/ ECI_CHARSET: /*int*/ number = 927;
private static /*final*/ BEGIN_MACRO_PDF417_CONTROL_BLOCK: /*int*/ number = 928;
private static /*final*/ BEGIN_MACRO_PDF417_OPTIONAL_FIELD: /*int*/ number = 923;
private static /*final*/ MACRO_PDF417_TERMINATOR: /*int*/ number = 922;
private static /*final*/ MODE_SHIFT_TO_BYTE_COMPACTION_MODE: /*int*/ number = 913;
private static /*final*/ MAX_NUMERIC_CODEWORDS: /*int*/ number = 15;

private static /*final*/ MACRO_PDF417_OPTIONAL_FIELD_FILE_NAME: /*int*/ number = 0;
private static /*final*/ MACRO_PDF417_OPTIONAL_FIELD_SEGMENT_COUNT: /*int*/ number = 1;
private static /*final*/ MACRO_PDF417_OPTIONAL_FIELD_TIME_STAMP: /*int*/ number = 2;
private static /*final*/ MACRO_PDF417_OPTIONAL_FIELD_SENDER: /*int*/ number = 3;
private static /*final*/ MACRO_PDF417_OPTIONAL_FIELD_ADDRESSEE: /*int*/ number = 4;
private static /*final*/ MACRO_PDF417_OPTIONAL_FIELD_FILE_SIZE: /*int*/ number = 5;
private static /*final*/ MACRO_PDF417_OPTIONAL_FIELD_CHECKSUM: /*int*/ number = 6;

private static /*final*/ PL: /*int*/ number = 25;
private static /*final*/ LL: /*int*/ number = 27;
private static /*final*/ AS: /*int*/ number = 27;
private static /*final*/ ML: /*int*/ number = 28;
private static /*final*/ AL: /*int*/ number = 28;
private static /*final*/ PS: /*int*/ number = 29;
private static /*final*/ PAL: /*int*/ number = 29;
private static /*final*/ TEXT_COMPACTION_MODE_LATCH: int = 900;
private static /*final*/ BYTE_COMPACTION_MODE_LATCH: int = 901;
private static /*final*/ NUMERIC_COMPACTION_MODE_LATCH: int = 902;
private static /*final*/ BYTE_COMPACTION_MODE_LATCH_6: int = 924;
private static /*final*/ ECI_USER_DEFINED: int = 925;
private static /*final*/ ECI_GENERAL_PURPOSE: int = 926;
private static /*final*/ ECI_CHARSET: int = 927;
private static /*final*/ BEGIN_MACRO_PDF417_CONTROL_BLOCK: int = 928;
private static /*final*/ BEGIN_MACRO_PDF417_OPTIONAL_FIELD: int = 923;
private static /*final*/ MACRO_PDF417_TERMINATOR: int = 922;
private static /*final*/ MODE_SHIFT_TO_BYTE_COMPACTION_MODE: int = 913;
private static /*final*/ MAX_NUMERIC_CODEWORDS: int = 15;

private static /*final*/ MACRO_PDF417_OPTIONAL_FIELD_FILE_NAME: int = 0;
private static /*final*/ MACRO_PDF417_OPTIONAL_FIELD_SEGMENT_COUNT: int = 1;
private static /*final*/ MACRO_PDF417_OPTIONAL_FIELD_TIME_STAMP: int = 2;
private static /*final*/ MACRO_PDF417_OPTIONAL_FIELD_SENDER: int = 3;
private static /*final*/ MACRO_PDF417_OPTIONAL_FIELD_ADDRESSEE: int = 4;
private static /*final*/ MACRO_PDF417_OPTIONAL_FIELD_FILE_SIZE: int = 5;
private static /*final*/ MACRO_PDF417_OPTIONAL_FIELD_CHECKSUM: int = 6;

private static /*final*/ PL: int = 25;
private static /*final*/ LL: int = 27;
private static /*final*/ AS: int = 27;
private static /*final*/ ML: int = 28;
private static /*final*/ AL: int = 28;
private static /*final*/ PS: int = 29;
private static /*final*/ PAL: int = 29;

private static /*final*/ PUNCT_CHARS: string =
';<>@[\\]_`~!\r\t,:\n-.$/"|*()?{}\'';
Expand All @@ -114,7 +114,7 @@ export default /*final*/ class DecodedBitStreamParser {
*/
private static /*final*/ EXP900: BigInt[] = getEXP900();

private static /*final*/ NUMBER_OF_SEQUENCE_CODEWORDS: /*int*/ number = 2;
private static /*final*/ NUMBER_OF_SEQUENCE_CODEWORDS: int = 2;

// private DecodedBitStreamParser() {
// }
Expand All @@ -131,8 +131,8 @@ export default /*final*/ class DecodedBitStreamParser {
// let encoding: Charset = StandardCharsets.ISO_8859_1;
let encoding = CharacterSetECI.ISO8859_1;
// Get compaction mode
let codeIndex: /*int*/ number = 1;
let code: /*int*/ number = codewords[codeIndex++];
let codeIndex: int = 1;
let code: int = codewords[codeIndex++];
let resultMetadata: PDF417ResultMetadata = new PDF417ResultMetadata();
while (codeIndex < codewords[0]) {
switch (code) {
Expand Down Expand Up @@ -204,7 +204,7 @@ export default /*final*/ class DecodedBitStreamParser {
* @throws FormatException
*/
// @SuppressWarnings("deprecation")
static decodeMacroBlock(codewords: Int32Array, codeIndex: /*int*/number, resultMetadata: PDF417ResultMetadata): /*int*/ number {
static decodeMacroBlock(codewords: Int32Array, codeIndex: int, resultMetadata: PDF417ResultMetadata): int {
if (codeIndex + DecodedBitStreamParser.NUMBER_OF_SEQUENCE_CODEWORDS > codewords[0]) {
// we must have at least two bytes left for the segment index
throw FormatException.getFormatInstance();
Expand All @@ -220,7 +220,7 @@ export default /*final*/ class DecodedBitStreamParser {
codeIndex = DecodedBitStreamParser.textCompaction(codewords, codeIndex, fileId);
resultMetadata.setFileId(fileId.toString());

let optionalFieldsStart: /*int*/ number = -1;
let optionalFieldsStart: int = -1;
if (codewords[codeIndex] === DecodedBitStreamParser.BEGIN_MACRO_PDF417_OPTIONAL_FIELD) {
optionalFieldsStart = codeIndex + 1;
}
Expand Down Expand Up @@ -280,7 +280,7 @@ export default /*final*/ class DecodedBitStreamParser {

// copy optional fields to additional options
if (optionalFieldsStart !== -1) {
let optionalFieldsLength: /*int*/ number = codeIndex - optionalFieldsStart;
let optionalFieldsLength: int = codeIndex - optionalFieldsStart;
if (resultMetadata.isLastSegment()) {
// do not include terminator
optionalFieldsLength--;
Expand All @@ -301,16 +301,16 @@ export default /*final*/ class DecodedBitStreamParser {
* @param result The decoded data is appended to the result.
* @return The next index into the codeword array.
*/
private static textCompaction(codewords: Int32Array, codeIndex: /*int*/number, result: StringBuilder): /*int*/ number {
private static textCompaction(codewords: Int32Array, codeIndex: int, result: StringBuilder): int {
// 2 character per codeword
let textCompactionData: Int32Array = new Int32Array((codewords[0] - codeIndex) * 2);
// Used to hold the byte compaction value if there is a mode shift
let byteCompactionData: Int32Array = new Int32Array((codewords[0] - codeIndex) * 2);

let index: /*int*/ number = 0;
let index: int = 0;
let end: boolean = false;
while ((codeIndex < codewords[0]) && !end) {
let code: /*int*/ number = codewords[codeIndex++];
let code: int = codewords[codeIndex++];
if (code < DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH) {
textCompactionData[index] = code / 30;
textCompactionData[index + 1] = code % 30;
Expand Down Expand Up @@ -367,17 +367,17 @@ export default /*final*/ class DecodedBitStreamParser {
*/
private static decodeTextCompaction(textCompactionData: Int32Array,
byteCompactionData: Int32Array,
length: /*int*/ number,
length: int,
result: StringBuilder): void {
// Beginning from an initial state of the Alpha sub-mode
// The default compaction mode for PDF417 in effect at the start of each symbol shall always be Text
// Compaction mode Alpha sub-mode (alphabetic: uppercase). A latch codeword from another mode to the Text
// Compaction mode shall always switch to the Text Compaction Alpha sub-mode.
let subMode: Mode = Mode.ALPHA;
let priorToShiftMode: Mode = Mode.ALPHA;
let i: /*int*/ number = 0;
let i: int = 0;
while (i < length) {
let subModeCh: /*int*/ number = textCompactionData[i];
let subModeCh: int = textCompactionData[i];
let ch: /*char*/ string = '0';
switch (subMode) {
case Mode.ALPHA:
Expand Down Expand Up @@ -556,13 +556,13 @@ export default /*final*/ class DecodedBitStreamParser {
* @param result The decoded data is appended to the result.
* @return The next index into the codeword array.
*/
private static /*int*/ byteCompaction(mode: /*int*/number,
private static /*int*/ byteCompaction(mode: int,
codewords: Int32Array,
encoding: /*Charset*/ CharacterSetECI,
codeIndex: /*int*/number,
codeIndex: int,
result: StringBuilder) {
let decodedBytes: ByteArrayOutputStream = new ByteArrayOutputStream();
let count: /*int*/ number = 0;
let count: int = 0;
let value: /*long*/ number = 0;
let end: boolean = false;

Expand All @@ -572,7 +572,7 @@ export default /*final*/ class DecodedBitStreamParser {
// is not a multiple of 6

let byteCompactedCodewords: Int32Array = new Int32Array(6);
let nextCode: /*int*/ number = codewords[codeIndex++];
let nextCode: int = codewords[codeIndex++];
while ((codeIndex < codewords[0]) && !end) {
byteCompactedCodewords[count++] = nextCode;
// Base 900
Expand All @@ -595,7 +595,11 @@ export default /*final*/ class DecodedBitStreamParser {
// Decode every 5 codewords
// Convert to Base 256
for (let j /*int*/ = 0; j < 6; ++j) {
decodedBytes.write(/*(byte)*/(value >> (8 * (5 - j))));
/* @note
* JavaScript stores numbers as 64 bits floating point numbers, but all bitwise operations are performed on 32 bits binary numbers.
* So the next bitwise operation could not be done with simple numbers
*/
decodedBytes.write(/*(byte)*/Number(BigInt(value) >> BigInt(8 * (5 - j))));
}
value = 0;
count = 0;
Expand All @@ -622,7 +626,7 @@ export default /*final*/ class DecodedBitStreamParser {
// Total number of Byte Compaction characters to be encoded
// is an integer multiple of 6
while (codeIndex < codewords[0] && !end) {
let code: /*int*/ number = codewords[codeIndex++];
let code: int = codewords[codeIndex++];
if (code < DecodedBitStreamParser.TEXT_COMPACTION_MODE_LATCH) {
count++;
// Base 900
Expand Down Expand Up @@ -667,14 +671,14 @@ export default /*final*/ class DecodedBitStreamParser {
*
* @throws FormatException
*/
private static numericCompaction(codewords: Int32Array, codeIndex: number /*int*/, result: StringBuilder): /*int*/ number {
let count: /*int*/ number = 0;
private static numericCompaction(codewords: Int32Array, codeIndex: number /*int*/, result: StringBuilder): int {
let count: int = 0;
let end: boolean = false;

let numericCodewords: Int32Array = new Int32Array(DecodedBitStreamParser.MAX_NUMERIC_CODEWORDS);

while (codeIndex < codewords[0] && !end) {
let code: /*int*/ number = codewords[codeIndex++];
let code: int = codewords[codeIndex++];
if (codeIndex === codewords[0]) {
end = true;
}
Expand Down Expand Up @@ -750,7 +754,7 @@ export default /*final*/ class DecodedBitStreamParser {
*
* @throws FormatException
*/
private static decodeBase900toBase10(codewords: Int32Array, count: /*int*/ number): string {
private static decodeBase900toBase10(codewords: Int32Array, count: int): string {
let result: bigint = BigInt(0);
for (let i /*int*/ = 0; i < count; i++) {
result += BigInt(DecodedBitStreamParser.EXP900[count - i - 1]) * BigInt(codewords[i]);
Expand Down
36 changes: 18 additions & 18 deletions src/core/pdf417/decoder/ec/ErrorCorrection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ export default /*public final*/ class ErrorCorrection {
* @throws ChecksumException if errors cannot be corrected, maybe because of too many errors
*/
public decode(received: Int32Array,
numECCodewords: /*int*/number,
erasures: Int32Array): /*int*/ number {
numECCodewords: int,
erasures: Int32Array): int {

let poly: ModulusPoly = new ModulusPoly(this.field, received);
let S: Int32Array = new Int32Array(numECCodewords);
let error: boolean = false;
for (let i /*int*/ = numECCodewords; i > 0; i--) {
let evaluation: /*int*/ number = poly.evaluateAt(this.field.exp(i));
let evaluation: int = poly.evaluateAt(this.field.exp(i));
S[numECCodewords - i] = evaluation;
if (evaluation !== 0) {
error = true;
Expand All @@ -68,7 +68,7 @@ export default /*public final*/ class ErrorCorrection {
let knownErrors: ModulusPoly = this.field.getOne();
if (erasures != null) {
for (const erasure of erasures) {
let b: /*int*/ number = this.field.exp(received.length - 1 - erasure);
let b: int = this.field.exp(received.length - 1 - erasure);
// Add (1 - bx) term:
let term: ModulusPoly = new ModulusPoly(this.field, new Int32Array([this.field.subtract(0, b), 1]));
knownErrors = knownErrors.multiply(term);
Expand All @@ -89,7 +89,7 @@ export default /*public final*/ class ErrorCorrection {
let errorMagnitudes: Int32Array = this.findErrorMagnitudes(omega, sigma, errorLocations);

for (let i /*int*/ = 0; i < errorLocations.length; i++) {
let position: /*int*/ number = received.length - 1 - this.field.log(errorLocations[i]);
let position: int = received.length - 1 - this.field.log(errorLocations[i]);
if (position < 0) {
throw ChecksumException.getChecksumInstance();
}
Expand Down Expand Up @@ -135,24 +135,24 @@ export default /*public final*/ class ErrorCorrection {
}
r = rLastLast;
let q: ModulusPoly = this.field.getZero();
let denominatorLeadingTerm: /*int*/ number = rLast.getCoefficient(rLast.getDegree());
let dltInverse: /*int*/ number = this.field.inverse(denominatorLeadingTerm);
let denominatorLeadingTerm: int = rLast.getCoefficient(rLast.getDegree());
let dltInverse: int = this.field.inverse(denominatorLeadingTerm);
while (r.getDegree() >= rLast.getDegree() && !r.isZero()) {
let degreeDiff: /*int*/ number = r.getDegree() - rLast.getDegree();
let scale: /*int*/ number = this.field.multiply(r.getCoefficient(r.getDegree()), dltInverse);
let degreeDiff: int = r.getDegree() - rLast.getDegree();
let scale: int = this.field.multiply(r.getCoefficient(r.getDegree()), dltInverse);
q = q.add(this.field.buildMonomial(degreeDiff, scale));
r = r.subtract(rLast.multiplyByMonomial(degreeDiff, scale));
}

t = q.multiply(tLast).subtract(tLastLast).negative();
}

let sigmaTildeAtZero: /*int*/ number = t.getCoefficient(0);
let sigmaTildeAtZero: int = t.getCoefficient(0);
if (sigmaTildeAtZero === 0) {
throw ChecksumException.getChecksumInstance();
}

let inverse: /*int*/ number = this.field.inverse(sigmaTildeAtZero);
let inverse: int = this.field.inverse(sigmaTildeAtZero);
let sigma: ModulusPoly = t.multiply(inverse);
let omega: ModulusPoly = r.multiply(inverse);
return [sigma, omega];
Expand All @@ -165,9 +165,9 @@ export default /*public final*/ class ErrorCorrection {
*/
private findErrorLocations(errorLocator: ModulusPoly): Int32Array {
// This is a direct application of Chien's search
let numErrors: /*int*/ number = errorLocator.getDegree();
let numErrors: int = errorLocator.getDegree();
let result: Int32Array = new Int32Array(numErrors);
let e: /*int*/ number = 0;
let e: int = 0;
for (let i /*int*/ = 1; i < this.field.getSize() && e < numErrors; i++) {
if (errorLocator.evaluateAt(i) === 0) {
result[e] = this.field.inverse(i);
Expand All @@ -183,7 +183,7 @@ export default /*public final*/ class ErrorCorrection {
private findErrorMagnitudes(errorEvaluator: ModulusPoly,
errorLocator: ModulusPoly,
errorLocations: Int32Array): Int32Array {
let errorLocatorDegree: /*int*/ number = errorLocator.getDegree();
let errorLocatorDegree: int = errorLocator.getDegree();
let formalDerivativeCoefficients: Int32Array = new Int32Array(errorLocatorDegree);
for (let i /*int*/ = 1; i <= errorLocatorDegree; i++) {
formalDerivativeCoefficients[errorLocatorDegree - i] =
Expand All @@ -192,12 +192,12 @@ export default /*public final*/ class ErrorCorrection {
let formalDerivative: ModulusPoly = new ModulusPoly(this.field, formalDerivativeCoefficients);

// This is directly applying Forney's Formula
let s: /*int*/ number = errorLocations.length;
let s: int = errorLocations.length;
let result: Int32Array = new Int32Array(s);
for (let i /*int*/ = 0; i < s; i++) {
let xiInverse: /*int*/ number = this.field.inverse(errorLocations[i]);
let numerator: /*int*/ number = this.field.subtract(0, errorEvaluator.evaluateAt(xiInverse));
let denominator: /*int*/ number = this.field.inverse(formalDerivative.evaluateAt(xiInverse));
let xiInverse: int = this.field.inverse(errorLocations[i]);
let numerator: int = this.field.subtract(0, errorEvaluator.evaluateAt(xiInverse));
let denominator: int = this.field.inverse(formalDerivative.evaluateAt(xiInverse));
result[i] = this.field.multiply(numerator, denominator);
}
return result;
Expand Down
10 changes: 6 additions & 4 deletions src/core/util/Arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ export default class Arrays {
}
}

public static copyOf(original: Int32Array, newLength: number) {
const copy = new Int32Array(newLength);
System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
return copy;
public static copyOf(original: Int32Array, newLength: number): Int32Array {
return original.slice(0, newLength);
}

public static copyOfUint8Array(original: Uint8Array, newLength: number): Uint8Array {
return original.slice(0, newLength);
}

public static copyOfRange(original: Int32Array, from: number, to: number): Int32Array {
Expand Down
Loading

0 comments on commit 31be35d

Please sign in to comment.