Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structured Append #132

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/decoder/decodeData/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
// tslint:disable:no-bitwise
import * as assert from "assert";
import { BitStream } from "./BitStream";
import { shiftJISTable } from "./shiftJISTable";

export interface StructuredAppendTag {
M: number;
N: number;
parity: number;
}

export interface Chunk {
type: Mode;
text: string;
}

export interface StructuredAppendChunk extends StructuredAppendTag {
type: Mode.StructuredAppend;
}

export interface ByteChunk {
type: Mode.Byte | Mode.Kanji;
bytes: number[];
Expand All @@ -17,17 +28,19 @@ export interface ECIChunk {
assignmentNumber: number;
}

export type Chunks = Array<Chunk | ByteChunk | ECIChunk>;
export type Chunks = Array<Chunk | StructuredAppendChunk | ByteChunk | ECIChunk>;

export interface DecodedQR {
text: string;
bytes: number[];
chunks: Chunks;
structuredAppend?: StructuredAppendTag;
}

export enum Mode {
Numeric = "numeric",
Alphanumeric = "alphanumeric",
StructuredAppend = "structuredappend",
Byte = "byte",
Kanji = "kanji",
ECI = "eci",
Expand All @@ -37,10 +50,10 @@ enum ModeByte {
Terminator = 0x0,
Numeric = 0x1,
Alphanumeric = 0x2,
StructuredAppend = 0x3,
Byte = 0x4,
Kanji = 0x8,
ECI = 0x7,
// StructuredAppend = 0x3,
// FNC1FirstPosition = 0x5,
// FNC1SecondPosition = 0x9,
}
Expand Down Expand Up @@ -223,6 +236,26 @@ export function decode(data: Uint8ClampedArray, version: number): DecodedQR {
type: Mode.Alphanumeric,
text: alphanumericResult.text,
});
} else if (mode === ModeByte.StructuredAppend) {
// QR Standard section 9.2:
// > The 4-bit patterns shall be the binary equivalents of (m - 1) and (n - 1) respectively.
const structuredAppend: StructuredAppendTag = {
M: stream.readBits(4) + 1,
N: stream.readBits(4) + 1,
parity: stream.readBits(8),
};
// QR codes sometimes contain duplicate Structured Append tags for redundancy.
// If they exist, they are all supposed to be equal. This checks for that:
if (typeof result.structuredAppend === "undefined") {
result.structuredAppend = structuredAppend;
} else {
// TODO: should this really be an assert? If the user has a corrupt QR code they probably still want to try to read it.
assert.deepStrictEqual(structuredAppend, result.structuredAppend, "QR Code contains inconsistent Structured Append tags.");
}
result.chunks.push({
type: Mode.StructuredAppend,
...result.structuredAppend,
});
} else if (mode === ModeByte.Byte) {
const byteResult = decodeByte(stream, size);
result.text += byteResult.text;
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {binarize} from "./binarizer";
import {BitMatrix} from "./BitMatrix";
import {Chunks} from "./decoder/decodeData";
import {Chunks, StructuredAppendTag} from "./decoder/decodeData";
import {decode} from "./decoder/decoder";
import {extract} from "./extractor";
import {locate, Point} from "./locator";
Expand All @@ -9,6 +9,7 @@ export interface QRCode {
binaryData: number[];
data: string;
chunks: Chunks;
structuredAppend?: StructuredAppendTag;
location: {
topRightCorner: Point;
topLeftCorner: Point;
Expand Down Expand Up @@ -39,6 +40,7 @@ function scan(matrix: BitMatrix): QRCode | null {
binaryData: decoded.bytes,
data: decoded.text,
chunks: decoded.chunks,
structuredAppend: decoded.structuredAppend,
location: {
topRightCorner: extracted.mappingFunction(location.dimension, 0),
topLeftCorner: extracted.mappingFunction(0, 0),
Expand Down
Binary file added tests/amen.mp3
Binary file not shown.
17 changes: 16 additions & 1 deletion tests/end-to-end/report.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"counts": {
"failed": 47,
"successful": 207
"successful": 222
},
"tests": {
"0": true,
Expand Down Expand Up @@ -245,6 +245,21 @@
"stickerbook-4": true,
"stickerbook-5": true,
"stickerbook-6": false,
"structured-append-01": true,
"structured-append-02": true,
"structured-append-03": true,
"structured-append-04": true,
"structured-append-05": true,
"structured-append-06": true,
"structured-append-07": true,
"structured-append-08": true,
"structured-append-09": true,
"structured-append-10": true,
"structured-append-11": true,
"structured-append-12": true,
"structured-append-13": true,
"structured-append-14": true,
"structured-append-15": true,
"subway-station": false,
"tattoo-scan": true,
"trackable-book-1": false,
Expand Down
Binary file added tests/end-to-end/structured-append-01/input.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading