pdf-codec 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/README.md +67 -9
  2. package/dist/cff-bounds.cjs +432 -0
  3. package/dist/cff-bounds.d.cts +9 -0
  4. package/dist/cff-bounds.d.ts +9 -0
  5. package/dist/cff-bounds.js +431 -0
  6. package/dist/cff-probe.cjs +8 -106
  7. package/dist/cff-probe.js +9 -107
  8. package/dist/cff.cjs +158 -0
  9. package/dist/cff.d.cts +17 -0
  10. package/dist/cff.d.ts +17 -0
  11. package/dist/cff.js +150 -0
  12. package/dist/filters.cjs +45 -2
  13. package/dist/filters.d.cts +4 -3
  14. package/dist/filters.d.ts +4 -3
  15. package/dist/filters.js +45 -2
  16. package/dist/formula.d.cts +1 -1
  17. package/dist/formula.d.ts +1 -1
  18. package/dist/glyf.cjs +62 -6
  19. package/dist/glyf.d.cts +2 -0
  20. package/dist/glyf.d.ts +2 -0
  21. package/dist/glyf.js +62 -6
  22. package/dist/glyph-bounds-BV_Z40KS.d.cts +10 -0
  23. package/dist/glyph-bounds-BV_Z40KS.d.ts +10 -0
  24. package/dist/glyph-bounds.cjs +12 -0
  25. package/dist/glyph-bounds.d.cts +2 -0
  26. package/dist/glyph-bounds.d.ts +2 -0
  27. package/dist/glyph-bounds.js +11 -0
  28. package/dist/image/jbig2-arith.cjs +432 -0
  29. package/dist/image/jbig2-arith.d.cts +23 -0
  30. package/dist/image/jbig2-arith.d.ts +23 -0
  31. package/dist/image/jbig2-arith.js +426 -0
  32. package/dist/image/jbig2-bitmap.cjs +80 -0
  33. package/dist/image/jbig2-bitmap.d.cts +20 -0
  34. package/dist/image/jbig2-bitmap.d.ts +20 -0
  35. package/dist/image/jbig2-bitmap.js +72 -0
  36. package/dist/image/jbig2-errors.cjs +17 -0
  37. package/dist/image/jbig2-errors.d.cts +2 -0
  38. package/dist/image/jbig2-errors.d.ts +2 -0
  39. package/dist/image/jbig2-errors.js +15 -0
  40. package/dist/image/jbig2-generic.cjs +242 -0
  41. package/dist/image/jbig2-generic.d.cts +27 -0
  42. package/dist/image/jbig2-generic.d.ts +27 -0
  43. package/dist/image/jbig2-generic.js +237 -0
  44. package/dist/image/jbig2-text.cjs +175 -0
  45. package/dist/image/jbig2-text.d.cts +58 -0
  46. package/dist/image/jbig2-text.d.ts +58 -0
  47. package/dist/image/jbig2-text.js +170 -0
  48. package/dist/image/jbig2.cjs +333 -0
  49. package/dist/image/jbig2.d.cts +15 -0
  50. package/dist/image/jbig2.d.ts +15 -0
  51. package/dist/image/jbig2.js +332 -0
  52. package/dist/image/jp2-boxes.cjs +148 -0
  53. package/dist/image/jp2-boxes.d.cts +26 -0
  54. package/dist/image/jp2-boxes.d.ts +26 -0
  55. package/dist/image/jp2-boxes.js +146 -0
  56. package/dist/image/jpeg2000-codestream.cjs +309 -0
  57. package/dist/image/jpeg2000-codestream.d.cts +78 -0
  58. package/dist/image/jpeg2000-codestream.d.ts +78 -0
  59. package/dist/image/jpeg2000-codestream.js +308 -0
  60. package/dist/image/jpeg2000-dwt.cjs +148 -0
  61. package/dist/image/jpeg2000-dwt.d.cts +34 -0
  62. package/dist/image/jpeg2000-dwt.d.ts +34 -0
  63. package/dist/image/jpeg2000-dwt.js +145 -0
  64. package/dist/image/jpeg2000-errors.cjs +17 -0
  65. package/dist/image/jpeg2000-errors.d.cts +9 -0
  66. package/dist/image/jpeg2000-errors.d.ts +9 -0
  67. package/dist/image/jpeg2000-errors.js +15 -0
  68. package/dist/image/jpeg2000-t1.cjs +254 -0
  69. package/dist/image/jpeg2000-t1.d.cts +18 -0
  70. package/dist/image/jpeg2000-t1.d.ts +18 -0
  71. package/dist/image/jpeg2000-t1.js +253 -0
  72. package/dist/image/jpeg2000-t2.cjs +316 -0
  73. package/dist/image/jpeg2000-t2.d.cts +79 -0
  74. package/dist/image/jpeg2000-t2.d.ts +79 -0
  75. package/dist/image/jpeg2000-t2.js +311 -0
  76. package/dist/image/jpeg2000-tagtree.cjs +105 -0
  77. package/dist/image/jpeg2000-tagtree.d.cts +29 -0
  78. package/dist/image/jpeg2000-tagtree.d.ts +29 -0
  79. package/dist/image/jpeg2000-tagtree.js +103 -0
  80. package/dist/image/jpeg2000.cjs +322 -0
  81. package/dist/image/jpeg2000.d.cts +54 -0
  82. package/dist/image/jpeg2000.d.ts +54 -0
  83. package/dist/image/jpeg2000.js +320 -0
  84. package/dist/images-read.cjs +78 -1
  85. package/dist/images-read.js +78 -1
  86. package/dist/index.cjs +17 -0
  87. package/dist/index.d.cts +11 -2
  88. package/dist/index.d.ts +11 -2
  89. package/dist/index.js +7 -1
  90. package/dist/jbig2-errors-Bj6MPxrx.d.cts +9 -0
  91. package/dist/jbig2-errors-Bj6MPxrx.d.ts +9 -0
  92. package/dist/math-font.cjs +43 -18
  93. package/dist/math-font.d.cts +8 -1
  94. package/dist/math-font.d.ts +8 -1
  95. package/dist/math-font.js +43 -18
  96. package/dist/math-stretch-Bwk_bxh1.d.ts +24 -0
  97. package/dist/math-stretch-DXWjs8gL.d.cts +24 -0
  98. package/dist/math-stretch.cjs +96 -0
  99. package/dist/math-stretch.d.cts +2 -0
  100. package/dist/math-stretch.d.ts +2 -0
  101. package/dist/math-stretch.js +94 -0
  102. package/dist/math-table-DhPrNoPa.d.ts +70 -0
  103. package/dist/math-table-gFNB8DmQ.d.cts +70 -0
  104. package/dist/math-table.cjs +69 -1
  105. package/dist/math-table.d.cts +2 -45
  106. package/dist/math-table.d.ts +2 -45
  107. package/dist/math-table.js +69 -1
  108. package/dist/{math-types-Ba8qAf1p.d.cts → math-types-YSQUTviw.d.cts} +2 -0
  109. package/dist/{math-types-Ba8qAf1p.d.ts → math-types-YSQUTviw.d.ts} +2 -0
  110. package/dist/math-types.d.cts +1 -1
  111. package/dist/math-types.d.ts +1 -1
  112. package/package.json +1 -1
@@ -0,0 +1,58 @@
1
+ import { ArithContexts, MqDecoder } from "./jbig2-arith.cjs";
2
+ import { Jbig2Bitmap, Jbig2CombinationOperator } from "./jbig2-bitmap.cjs";
3
+ import { Jbig2AtPixel } from "./jbig2-generic.cjs";
4
+ //#region src/image/jbig2-text.d.ts
5
+ interface Jbig2ReferenceCorner {
6
+ readonly right: boolean;
7
+ readonly bottom: boolean;
8
+ }
9
+ declare function referenceCornerFromCode(code: number): Jbig2ReferenceCorner | undefined;
10
+ declare function symbolCodeLength(symbolCount: number): number;
11
+ interface TextArithContexts {
12
+ readonly iadh: ArithContexts;
13
+ readonly iadw: ArithContexts;
14
+ readonly iaex: ArithContexts;
15
+ readonly iaai: ArithContexts;
16
+ readonly iadt: ArithContexts;
17
+ readonly iafs: ArithContexts;
18
+ readonly iads: ArithContexts;
19
+ readonly iait: ArithContexts;
20
+ readonly iari: ArithContexts;
21
+ readonly iardw: ArithContexts;
22
+ readonly iardh: ArithContexts;
23
+ readonly iardx: ArithContexts;
24
+ readonly iardy: ArithContexts;
25
+ readonly iaid: ArithContexts;
26
+ readonly generic: ArithContexts;
27
+ readonly refinement: ArithContexts;
28
+ }
29
+ declare function createTextArithContexts(genericTemplate: number, refinementTemplate: number, symbolIdBits: number): TextArithContexts;
30
+ interface TextRegionParams {
31
+ readonly width: number;
32
+ readonly height: number;
33
+ readonly instanceCount: number;
34
+ readonly stripSize: number;
35
+ readonly symbols: readonly Jbig2Bitmap[];
36
+ readonly defaultPixel: number;
37
+ readonly combinationOperator: Jbig2CombinationOperator;
38
+ readonly transposed: boolean;
39
+ readonly referenceCorner: Jbig2ReferenceCorner;
40
+ readonly dsOffset: number;
41
+ readonly refine: boolean;
42
+ readonly refinementTemplate: number;
43
+ readonly refinementAt: readonly Jbig2AtPixel[];
44
+ }
45
+ declare function decodeTextRegion(params: TextRegionParams, mq: MqDecoder, contexts: TextArithContexts, symbolIdBits: number): Jbig2Bitmap;
46
+ interface SymbolDictionaryParams {
47
+ readonly template: number;
48
+ readonly at: readonly Jbig2AtPixel[];
49
+ readonly refinementAggregate: boolean;
50
+ readonly refinementTemplate: number;
51
+ readonly refinementAt: readonly Jbig2AtPixel[];
52
+ readonly newSymbolCount: number;
53
+ readonly exportedSymbolCount: number;
54
+ readonly inputSymbols: readonly Jbig2Bitmap[];
55
+ }
56
+ declare function decodeSymbolDictionary(params: SymbolDictionaryParams, mq: MqDecoder, contexts: TextArithContexts, symbolIdBits: number): readonly Jbig2Bitmap[];
57
+ //#endregion
58
+ export { Jbig2ReferenceCorner, SymbolDictionaryParams, TextArithContexts, TextRegionParams, createTextArithContexts, decodeSymbolDictionary, decodeTextRegion, referenceCornerFromCode, symbolCodeLength };
@@ -0,0 +1,58 @@
1
+ import { ArithContexts, MqDecoder } from "./jbig2-arith.js";
2
+ import { Jbig2Bitmap, Jbig2CombinationOperator } from "./jbig2-bitmap.js";
3
+ import { Jbig2AtPixel } from "./jbig2-generic.js";
4
+ //#region src/image/jbig2-text.d.ts
5
+ interface Jbig2ReferenceCorner {
6
+ readonly right: boolean;
7
+ readonly bottom: boolean;
8
+ }
9
+ declare function referenceCornerFromCode(code: number): Jbig2ReferenceCorner | undefined;
10
+ declare function symbolCodeLength(symbolCount: number): number;
11
+ interface TextArithContexts {
12
+ readonly iadh: ArithContexts;
13
+ readonly iadw: ArithContexts;
14
+ readonly iaex: ArithContexts;
15
+ readonly iaai: ArithContexts;
16
+ readonly iadt: ArithContexts;
17
+ readonly iafs: ArithContexts;
18
+ readonly iads: ArithContexts;
19
+ readonly iait: ArithContexts;
20
+ readonly iari: ArithContexts;
21
+ readonly iardw: ArithContexts;
22
+ readonly iardh: ArithContexts;
23
+ readonly iardx: ArithContexts;
24
+ readonly iardy: ArithContexts;
25
+ readonly iaid: ArithContexts;
26
+ readonly generic: ArithContexts;
27
+ readonly refinement: ArithContexts;
28
+ }
29
+ declare function createTextArithContexts(genericTemplate: number, refinementTemplate: number, symbolIdBits: number): TextArithContexts;
30
+ interface TextRegionParams {
31
+ readonly width: number;
32
+ readonly height: number;
33
+ readonly instanceCount: number;
34
+ readonly stripSize: number;
35
+ readonly symbols: readonly Jbig2Bitmap[];
36
+ readonly defaultPixel: number;
37
+ readonly combinationOperator: Jbig2CombinationOperator;
38
+ readonly transposed: boolean;
39
+ readonly referenceCorner: Jbig2ReferenceCorner;
40
+ readonly dsOffset: number;
41
+ readonly refine: boolean;
42
+ readonly refinementTemplate: number;
43
+ readonly refinementAt: readonly Jbig2AtPixel[];
44
+ }
45
+ declare function decodeTextRegion(params: TextRegionParams, mq: MqDecoder, contexts: TextArithContexts, symbolIdBits: number): Jbig2Bitmap;
46
+ interface SymbolDictionaryParams {
47
+ readonly template: number;
48
+ readonly at: readonly Jbig2AtPixel[];
49
+ readonly refinementAggregate: boolean;
50
+ readonly refinementTemplate: number;
51
+ readonly refinementAt: readonly Jbig2AtPixel[];
52
+ readonly newSymbolCount: number;
53
+ readonly exportedSymbolCount: number;
54
+ readonly inputSymbols: readonly Jbig2Bitmap[];
55
+ }
56
+ declare function decodeSymbolDictionary(params: SymbolDictionaryParams, mq: MqDecoder, contexts: TextArithContexts, symbolIdBits: number): readonly Jbig2Bitmap[];
57
+ //#endregion
58
+ export { Jbig2ReferenceCorner, SymbolDictionaryParams, TextArithContexts, TextRegionParams, createTextArithContexts, decodeSymbolDictionary, decodeTextRegion, referenceCornerFromCode, symbolCodeLength };
@@ -0,0 +1,170 @@
1
+ import { createArithContexts, createIntegerContexts, createSymbolIdContexts, decodeInteger, decodeSymbolId } from "./jbig2-arith.js";
2
+ import { Jbig2ParseError, Jbig2UnsupportedError } from "./jbig2-errors.js";
3
+ import { combineBitmap, createBitmap } from "./jbig2-bitmap.js";
4
+ import { GENERIC_CONTEXT_BITS, REFINEMENT_CONTEXT_BITS, decodeGenericRegion, decodeRefinementRegion } from "./jbig2-generic.js";
5
+ //#region src/image/jbig2-text.ts
6
+ const REFERENCE_CORNERS = [
7
+ {
8
+ right: false,
9
+ bottom: true
10
+ },
11
+ {
12
+ right: false,
13
+ bottom: false
14
+ },
15
+ {
16
+ right: true,
17
+ bottom: true
18
+ },
19
+ {
20
+ right: true,
21
+ bottom: false
22
+ }
23
+ ];
24
+ function referenceCornerFromCode(code) {
25
+ return REFERENCE_CORNERS[code];
26
+ }
27
+ function symbolCodeLength(symbolCount) {
28
+ let bits = 1;
29
+ while (1 << bits < symbolCount) bits++;
30
+ return bits;
31
+ }
32
+ function createTextArithContexts(genericTemplate, refinementTemplate, symbolIdBits) {
33
+ return {
34
+ iadh: createIntegerContexts(),
35
+ iadw: createIntegerContexts(),
36
+ iaex: createIntegerContexts(),
37
+ iaai: createIntegerContexts(),
38
+ iadt: createIntegerContexts(),
39
+ iafs: createIntegerContexts(),
40
+ iads: createIntegerContexts(),
41
+ iait: createIntegerContexts(),
42
+ iari: createIntegerContexts(),
43
+ iardw: createIntegerContexts(),
44
+ iardh: createIntegerContexts(),
45
+ iardx: createIntegerContexts(),
46
+ iardy: createIntegerContexts(),
47
+ iaid: createSymbolIdContexts(symbolIdBits),
48
+ generic: createArithContexts(GENERIC_CONTEXT_BITS[genericTemplate] ?? 16),
49
+ refinement: createArithContexts(REFINEMENT_CONTEXT_BITS[refinementTemplate] ?? 13)
50
+ };
51
+ }
52
+ function instanceTopLeft(s, t, width, height, corner, transposed) {
53
+ const across = transposed ? t : s;
54
+ const down = transposed ? s : t;
55
+ return {
56
+ x: corner.right ? across - width + 1 : across,
57
+ y: corner.bottom ? down - height + 1 : down
58
+ };
59
+ }
60
+ function requireInteger(value, field) {
61
+ if (value === void 0) throw new Jbig2ParseError(`JBIG2 ${field} decoded as out-of-band, which is not a value this field can take`);
62
+ return value;
63
+ }
64
+ function decodeTextRegion(params, mq, contexts, symbolIdBits) {
65
+ const region = createBitmap(params.width, params.height, params.defaultPixel);
66
+ let stripT = -requireInteger(decodeInteger(mq, contexts.iadt), "IADT") * params.stripSize;
67
+ let firstS = 0;
68
+ let instances = 0;
69
+ while (instances < params.instanceCount) {
70
+ stripT += requireInteger(decodeInteger(mq, contexts.iadt), "IADT") * params.stripSize;
71
+ firstS += requireInteger(decodeInteger(mq, contexts.iafs), "IAFS");
72
+ let currentS = firstS;
73
+ for (;;) {
74
+ const currentT = params.stripSize === 1 ? 0 : requireInteger(decodeInteger(mq, contexts.iait), "IAIT");
75
+ const t = stripT + currentT;
76
+ const id = decodeSymbolId(mq, contexts.iaid, symbolIdBits);
77
+ let symbol = params.symbols[id];
78
+ if (symbol === void 0) throw new Jbig2ParseError(`text region referenced symbol ${String(id)}, outside the ${String(params.symbols.length)}-symbol set its segment declares`);
79
+ if (params.refine && requireInteger(decodeInteger(mq, contexts.iari), "IARI") !== 0) symbol = refineInstance(symbol, params, mq, contexts);
80
+ const advance = (params.transposed ? symbol.height : symbol.width) - 1;
81
+ const advanceBefore = params.transposed ? params.referenceCorner.bottom : params.referenceCorner.right;
82
+ if (advanceBefore) currentS += advance;
83
+ const position = instanceTopLeft(currentS, t, symbol.width, symbol.height, params.referenceCorner, params.transposed);
84
+ combineBitmap(region, symbol, position.x, position.y, params.combinationOperator);
85
+ if (!advanceBefore) currentS += advance;
86
+ instances++;
87
+ if (instances > params.instanceCount) throw new Jbig2ParseError(`text region coded more symbol instances than the ${String(params.instanceCount)} its own SBNUMINSTANCES declares`);
88
+ const nextDelta = decodeInteger(mq, contexts.iads);
89
+ if (nextDelta === void 0) break;
90
+ currentS += nextDelta + params.dsOffset;
91
+ }
92
+ }
93
+ return region;
94
+ }
95
+ function refineInstance(symbol, params, mq, contexts) {
96
+ const deltaWidth = decodeInteger(mq, contexts.iardw) ?? 0;
97
+ const deltaHeight = decodeInteger(mq, contexts.iardh) ?? 0;
98
+ const deltaX = decodeInteger(mq, contexts.iardx) ?? 0;
99
+ const deltaY = decodeInteger(mq, contexts.iardy) ?? 0;
100
+ return decodeRefinementRegion(symbol.width + deltaWidth, symbol.height + deltaHeight, {
101
+ template: params.refinementTemplate,
102
+ tpgron: false,
103
+ at: params.refinementAt,
104
+ reference: symbol,
105
+ dx: Math.floor(deltaWidth / 2) + deltaX,
106
+ dy: Math.floor(deltaHeight / 2) + deltaY
107
+ }, mq, contexts.refinement);
108
+ }
109
+ function decodeSymbolDictionary(params, mq, contexts, symbolIdBits) {
110
+ const newSymbols = [];
111
+ let heightClassHeight = 0;
112
+ while (newSymbols.length < params.newSymbolCount) {
113
+ const heightDelta = decodeInteger(mq, contexts.iadh);
114
+ if (heightDelta === void 0) throw new Jbig2ParseError("symbol dictionary ended before all of its declared new symbols were decoded");
115
+ heightClassHeight += heightDelta;
116
+ let symbolWidth = 0;
117
+ for (;;) {
118
+ const widthDelta = decodeInteger(mq, contexts.iadw);
119
+ if (widthDelta === void 0) break;
120
+ symbolWidth += widthDelta;
121
+ if (newSymbols.length >= params.newSymbolCount) throw new Jbig2ParseError("symbol dictionary coded more symbols than its own SDNUMNEWSYMS declares");
122
+ newSymbols.push(decodeDictionarySymbol(symbolWidth, heightClassHeight, params, newSymbols, mq, contexts, symbolIdBits));
123
+ }
124
+ }
125
+ return exportSymbols([...params.inputSymbols, ...newSymbols], params.exportedSymbolCount, mq, contexts);
126
+ }
127
+ function decodeDictionarySymbol(width, height, params, newSymbols, mq, contexts, symbolIdBits) {
128
+ if (!params.refinementAggregate) return decodeGenericRegion(width, height, {
129
+ template: params.template,
130
+ tpgdon: false,
131
+ at: params.at
132
+ }, mq, contexts.generic);
133
+ const aggregateInstances = decodeInteger(mq, contexts.iaai) ?? 0;
134
+ if (aggregateInstances !== 1) throw new Jbig2UnsupportedError(`symbol dictionary uses aggregate coding with ${String(aggregateInstances)} instances per symbol; only the single-refinement form of T.88 6.5.8.2.2 is implemented`);
135
+ const available = [...params.inputSymbols, ...newSymbols];
136
+ const id = decodeSymbolId(mq, contexts.iaid, symbolIdBits);
137
+ const deltaX = decodeInteger(mq, contexts.iardx) ?? 0;
138
+ const deltaY = decodeInteger(mq, contexts.iardy) ?? 0;
139
+ const reference = available[id];
140
+ if (reference === void 0) throw new Jbig2ParseError(`symbol dictionary refined against symbol ${String(id)}, which it has not decoded yet`);
141
+ return decodeRefinementRegion(width, height, {
142
+ template: params.refinementTemplate,
143
+ tpgron: false,
144
+ at: params.refinementAt,
145
+ reference,
146
+ dx: deltaX,
147
+ dy: deltaY
148
+ }, mq, contexts.refinement);
149
+ }
150
+ function exportSymbols(allSymbols, exportedCount, mq, contexts) {
151
+ const exported = [];
152
+ const maxRuns = allSymbols.length * 2 + 1;
153
+ let index = 0;
154
+ let currentFlag = false;
155
+ for (let run = 0; index < allSymbols.length; run++) {
156
+ if (run >= maxRuns) throw new Jbig2ParseError("symbol dictionary export flags never accounted for every symbol");
157
+ const runLength = requireInteger(decodeInteger(mq, contexts.iaex), "IAEX");
158
+ if (runLength < 0 || index + runLength > allSymbols.length) throw new Jbig2ParseError(`symbol dictionary export run of ${String(runLength)} overruns its own ${String(allSymbols.length)}-symbol set`);
159
+ if (currentFlag) for (let i = 0; i < runLength; i++) {
160
+ const symbol = allSymbols[index + i];
161
+ if (symbol !== void 0) exported.push(symbol);
162
+ }
163
+ index += runLength;
164
+ currentFlag = !currentFlag;
165
+ }
166
+ if (exported.length !== exportedCount) throw new Jbig2ParseError(`symbol dictionary exported ${String(exported.length)} symbols but its own SDNUMEXSYMS declares ${String(exportedCount)}`);
167
+ return exported;
168
+ }
169
+ //#endregion
170
+ export { createTextArithContexts, decodeSymbolDictionary, decodeTextRegion, referenceCornerFromCode, symbolCodeLength };
@@ -0,0 +1,333 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_image_ccitt = require("./ccitt.cjs");
3
+ const require_image_jbig2_arith = require("./jbig2-arith.cjs");
4
+ const require_image_jbig2_errors = require("./jbig2-errors.cjs");
5
+ const require_image_jbig2_bitmap = require("./jbig2-bitmap.cjs");
6
+ const require_image_jbig2_generic = require("./jbig2-generic.cjs");
7
+ const require_image_jbig2_text = require("./jbig2-text.cjs");
8
+ //#region src/image/jbig2.ts
9
+ const SEGMENT_SYMBOL_DICTIONARY = 0;
10
+ const SEGMENT_INTERMEDIATE_TEXT_REGION = 4;
11
+ const SEGMENT_IMMEDIATE_TEXT_REGION = 6;
12
+ const SEGMENT_IMMEDIATE_LOSSLESS_TEXT_REGION = 7;
13
+ const SEGMENT_PATTERN_DICTIONARY = 16;
14
+ const SEGMENT_INTERMEDIATE_HALFTONE_REGION = 20;
15
+ const SEGMENT_IMMEDIATE_HALFTONE_REGION = 22;
16
+ const SEGMENT_IMMEDIATE_LOSSLESS_HALFTONE_REGION = 23;
17
+ const SEGMENT_INTERMEDIATE_GENERIC_REGION = 36;
18
+ const SEGMENT_IMMEDIATE_GENERIC_REGION = 38;
19
+ const SEGMENT_IMMEDIATE_LOSSLESS_GENERIC_REGION = 39;
20
+ const SEGMENT_INTERMEDIATE_REFINEMENT_REGION = 40;
21
+ const SEGMENT_IMMEDIATE_REFINEMENT_REGION = 42;
22
+ const SEGMENT_IMMEDIATE_LOSSLESS_REFINEMENT_REGION = 43;
23
+ const SEGMENT_PAGE_INFORMATION = 48;
24
+ const SEGMENT_END_OF_PAGE = 49;
25
+ const SEGMENT_END_OF_STRIPE = 50;
26
+ const SEGMENT_END_OF_FILE = 51;
27
+ const SEGMENT_PROFILES = 52;
28
+ const SEGMENT_TABLES = 53;
29
+ const SEGMENT_EXTENSION = 62;
30
+ const UNKNOWN_DATA_LENGTH = 4294967295;
31
+ const UNKNOWN_PAGE_HEIGHT = 4294967295;
32
+ var ByteCursor = class {
33
+ data;
34
+ position;
35
+ constructor(data, start = 0) {
36
+ this.data = data;
37
+ this.position = start;
38
+ }
39
+ get remaining() {
40
+ return this.data.length - this.position;
41
+ }
42
+ uint8() {
43
+ const value = this.data[this.position];
44
+ if (value === void 0) throw new require_image_jbig2_errors.Jbig2ParseError("JBIG2 stream ended in the middle of a segment header");
45
+ this.position++;
46
+ return value;
47
+ }
48
+ uint16() {
49
+ return this.uint8() << 8 | this.uint8();
50
+ }
51
+ uint32() {
52
+ return (this.uint8() << 24 | this.uint8() << 16 | this.uint8() << 8 | this.uint8()) >>> 0;
53
+ }
54
+ int8() {
55
+ const value = this.uint8();
56
+ return value > 127 ? value - 256 : value;
57
+ }
58
+ };
59
+ function readSegmentHeader(cursor, dataLength) {
60
+ const number = cursor.uint32();
61
+ const flags = cursor.uint8();
62
+ const type = flags & 63;
63
+ const pageAssociationIsLong = (flags & 64) !== 0;
64
+ let referredCount = cursor.uint8() >> 5;
65
+ if (referredCount === 7) {
66
+ cursor.position -= 1;
67
+ referredCount = cursor.uint32() & 536870911;
68
+ cursor.position += Math.ceil((referredCount + 1) / 8);
69
+ }
70
+ const referredSize = number <= 256 ? 1 : number <= 65536 ? 2 : 4;
71
+ const referredTo = [];
72
+ for (let i = 0; i < referredCount; i++) referredTo.push(referredSize === 1 ? cursor.uint8() : referredSize === 2 ? cursor.uint16() : cursor.uint32());
73
+ const pageAssociation = pageAssociationIsLong ? cursor.uint32() : cursor.uint8();
74
+ const declaredLength = cursor.uint32();
75
+ if (declaredLength === UNKNOWN_DATA_LENGTH) throw new require_image_jbig2_errors.Jbig2UnsupportedError("JBIG2 segment declares an unknown data length (T.88 7.2.7), which this decoder does not scan for a terminator");
76
+ const dataStart = cursor.position;
77
+ const dataEnd = dataStart + declaredLength;
78
+ if (dataEnd > dataLength) throw new require_image_jbig2_errors.Jbig2ParseError(`JBIG2 segment ${String(number)} declares ${String(declaredLength)} bytes of data but only ${String(dataLength - dataStart)} remain`);
79
+ return {
80
+ number,
81
+ type,
82
+ referredTo,
83
+ pageAssociation,
84
+ dataStart,
85
+ dataEnd
86
+ };
87
+ }
88
+ function readRegionInfo(cursor) {
89
+ const width = cursor.uint32();
90
+ const height = cursor.uint32();
91
+ const x = cursor.uint32();
92
+ const y = cursor.uint32();
93
+ const flags = cursor.uint8();
94
+ const combinationOperator = require_image_jbig2_bitmap.combinationOperatorFromCode(flags & 7);
95
+ if (combinationOperator === void 0) throw new require_image_jbig2_errors.Jbig2ParseError(`JBIG2 region declares external combination operator ${String(flags & 7)}, outside the 0-4 range T.88 Table 12 defines`);
96
+ return {
97
+ width,
98
+ height,
99
+ x,
100
+ y,
101
+ combinationOperator
102
+ };
103
+ }
104
+ function readAtPixels(cursor, count) {
105
+ const at = [];
106
+ for (let i = 0; i < count; i++) at.push({
107
+ x: cursor.int8(),
108
+ y: cursor.int8()
109
+ });
110
+ return at;
111
+ }
112
+ var Jbig2Decoder = class {
113
+ options;
114
+ page;
115
+ exportedSymbols = /* @__PURE__ */ new Map();
116
+ constructor(options) {
117
+ this.options = options;
118
+ }
119
+ warn(message) {
120
+ this.options.onWarning?.(message);
121
+ }
122
+ run(data) {
123
+ const cursor = new ByteCursor(data);
124
+ while (cursor.remaining > 0) {
125
+ const header = readSegmentHeader(cursor, data.length);
126
+ this.handleSegment(header, data);
127
+ cursor.position = header.dataEnd;
128
+ }
129
+ }
130
+ handleSegment(header, data) {
131
+ const cursor = new ByteCursor(data, header.dataStart);
132
+ switch (header.type) {
133
+ case SEGMENT_PAGE_INFORMATION:
134
+ this.readPageInformation(cursor);
135
+ return;
136
+ case SEGMENT_SYMBOL_DICTIONARY:
137
+ this.exportedSymbols.set(header.number, this.readSymbolDictionary(header, cursor));
138
+ return;
139
+ case SEGMENT_IMMEDIATE_GENERIC_REGION:
140
+ case SEGMENT_IMMEDIATE_LOSSLESS_GENERIC_REGION:
141
+ this.readGenericRegion(cursor, header.dataEnd);
142
+ return;
143
+ case SEGMENT_IMMEDIATE_TEXT_REGION:
144
+ case SEGMENT_IMMEDIATE_LOSSLESS_TEXT_REGION:
145
+ this.readTextRegion(header, cursor);
146
+ return;
147
+ case SEGMENT_IMMEDIATE_REFINEMENT_REGION:
148
+ case SEGMENT_IMMEDIATE_LOSSLESS_REFINEMENT_REGION:
149
+ this.readRefinementRegion(cursor, header.dataEnd);
150
+ return;
151
+ case SEGMENT_END_OF_PAGE:
152
+ case SEGMENT_END_OF_STRIPE:
153
+ case SEGMENT_END_OF_FILE:
154
+ case SEGMENT_PROFILES:
155
+ case SEGMENT_EXTENSION: return;
156
+ case SEGMENT_INTERMEDIATE_GENERIC_REGION:
157
+ case SEGMENT_INTERMEDIATE_TEXT_REGION:
158
+ case SEGMENT_INTERMEDIATE_REFINEMENT_REGION:
159
+ case SEGMENT_INTERMEDIATE_HALFTONE_REGION: throw new require_image_jbig2_errors.Jbig2UnsupportedError(`JBIG2 segment ${String(header.number)} is an intermediate region (type ${String(header.type)}), which is retained in an auxiliary buffer rather than composed onto the page; auxiliary buffers are not implemented`);
160
+ case SEGMENT_PATTERN_DICTIONARY:
161
+ case SEGMENT_IMMEDIATE_HALFTONE_REGION:
162
+ case SEGMENT_IMMEDIATE_LOSSLESS_HALFTONE_REGION: throw new require_image_jbig2_errors.Jbig2UnsupportedError(`JBIG2 segment ${String(header.number)} is a halftone region or pattern dictionary (type ${String(header.type)}), which is not implemented`);
163
+ case SEGMENT_TABLES: throw new require_image_jbig2_errors.Jbig2UnsupportedError(`JBIG2 segment ${String(header.number)} is a custom Huffman table, which only the Huffman-coded region forms use; those are not implemented`);
164
+ default: throw new require_image_jbig2_errors.Jbig2UnsupportedError(`JBIG2 segment ${String(header.number)} has unrecognised type ${String(header.type)}`);
165
+ }
166
+ }
167
+ readPageInformation(cursor) {
168
+ const declaredWidth = cursor.uint32();
169
+ const declaredHeight = cursor.uint32();
170
+ cursor.uint32();
171
+ cursor.uint32();
172
+ const flags = cursor.uint8();
173
+ cursor.uint16();
174
+ const width = this.options.width ?? declaredWidth;
175
+ const declaredOrHintedHeight = declaredHeight === UNKNOWN_PAGE_HEIGHT ? this.options.height : this.options.height ?? declaredHeight;
176
+ if (declaredOrHintedHeight === void 0) throw new require_image_jbig2_errors.Jbig2UnsupportedError("JBIG2 page information declares an unknown height and the caller supplied none; a striped page with no known height is not resolvable from the page segments alone");
177
+ const defaultPixel = flags >> 2 & 1;
178
+ const defaultCombinationOperator = require_image_jbig2_bitmap.combinationOperatorFromCode(flags >> 3 & 3);
179
+ if (defaultCombinationOperator === void 0) throw new require_image_jbig2_errors.Jbig2ParseError("JBIG2 page information declares an unrecognised default combination operator");
180
+ this.page = {
181
+ bitmap: require_image_jbig2_bitmap.createBitmap(width, declaredOrHintedHeight, defaultPixel),
182
+ defaultCombinationOperator,
183
+ combinationOperatorOverridden: (flags & 64) !== 0
184
+ };
185
+ }
186
+ requirePage() {
187
+ if (this.page === void 0) throw new require_image_jbig2_errors.Jbig2ParseError("JBIG2 stream composed a region before any page information segment declared the page");
188
+ return this.page;
189
+ }
190
+ compose(region, bitmap) {
191
+ const page = this.requirePage();
192
+ require_image_jbig2_bitmap.combineBitmap(page.bitmap, bitmap, region.x, region.y, page.combinationOperatorOverridden ? region.combinationOperator : page.defaultCombinationOperator);
193
+ }
194
+ readGenericRegion(cursor, dataEnd) {
195
+ const region = readRegionInfo(cursor);
196
+ const flags = cursor.uint8();
197
+ const mmr = (flags & 1) !== 0;
198
+ const template = flags >> 1 & 3;
199
+ const tpgdon = (flags & 8) !== 0;
200
+ if ((flags & 16) !== 0) throw new require_image_jbig2_errors.Jbig2UnsupportedError("JBIG2 generic region sets EXTTEMPLATE, the twelve-adaptive-pixel template of T.88 Amendment 2, which is not implemented");
201
+ const at = mmr ? [] : readAtPixels(cursor, template === 0 ? 4 : 1);
202
+ const payload = cursor.position;
203
+ if (mmr) {
204
+ const fax = require_image_ccitt.decodeCcittFax(cursor.data.subarray(payload, dataEnd), {
205
+ k: -1,
206
+ columns: region.width,
207
+ rows: region.height,
208
+ blackIs1: true,
209
+ onWarning: (message) => this.warn(message)
210
+ });
211
+ this.compose(region, require_image_jbig2_bitmap.unpackBitmapRows(fax.bytes, region.width, region.height));
212
+ return;
213
+ }
214
+ const mq = new require_image_jbig2_arith.MqDecoder(cursor.data, payload, dataEnd);
215
+ const contexts = require_image_jbig2_arith.createArithContexts(require_image_jbig2_generic.GENERIC_CONTEXT_BITS[template] ?? 16);
216
+ this.compose(region, require_image_jbig2_generic.decodeGenericRegion(region.width, region.height, {
217
+ template,
218
+ tpgdon,
219
+ at
220
+ }, mq, contexts));
221
+ }
222
+ readRefinementRegion(cursor, dataEnd) {
223
+ const region = readRegionInfo(cursor);
224
+ const flags = cursor.uint8();
225
+ const template = flags & 1;
226
+ const tpgron = (flags & 2) !== 0;
227
+ const at = template === 0 ? readAtPixels(cursor, 2) : [...require_image_jbig2_generic.NOMINAL_REFINEMENT_AT];
228
+ const page = this.requirePage();
229
+ const reference = require_image_jbig2_bitmap.createBitmap(region.width, region.height);
230
+ for (let y = 0; y < region.height; y++) for (let x = 0; x < region.width; x++) reference.data[y * region.width + x] = require_image_jbig2_bitmap.getPixel(page.bitmap, region.x + x, region.y + y);
231
+ const mq = new require_image_jbig2_arith.MqDecoder(cursor.data, cursor.position, dataEnd);
232
+ const contexts = require_image_jbig2_arith.createArithContexts(require_image_jbig2_generic.REFINEMENT_CONTEXT_BITS[template] ?? 13);
233
+ const refined = require_image_jbig2_generic.decodeRefinementRegion(region.width, region.height, {
234
+ template,
235
+ tpgron,
236
+ at,
237
+ reference,
238
+ dx: 0,
239
+ dy: 0
240
+ }, mq, contexts);
241
+ require_image_jbig2_bitmap.combineBitmap(page.bitmap, refined, region.x, region.y, region.combinationOperator);
242
+ }
243
+ readSymbolDictionary(header, cursor) {
244
+ const flags = cursor.uint16();
245
+ if ((flags & 1) !== 0) throw new require_image_jbig2_errors.Jbig2UnsupportedError("JBIG2 symbol dictionary is Huffman-coded (SDHUFF = 1); only the arithmetic form is implemented");
246
+ if ((flags & 256) !== 0 || (flags & 512) !== 0) throw new require_image_jbig2_errors.Jbig2UnsupportedError("JBIG2 symbol dictionary uses or retains a shared bitmap coding context (T.88 7.4.3.1.1 bits 8-9), which is not implemented");
247
+ const refinementAggregate = (flags & 2) !== 0;
248
+ const template = flags >> 10 & 3;
249
+ const refinementTemplate = flags >> 12 & 1;
250
+ const at = readAtPixels(cursor, template === 0 ? 4 : 1);
251
+ const refinementAt = refinementAggregate && refinementTemplate === 0 ? readAtPixels(cursor, 2) : [...require_image_jbig2_generic.NOMINAL_REFINEMENT_AT];
252
+ const exportedSymbolCount = cursor.uint32();
253
+ const newSymbolCount = cursor.uint32();
254
+ const inputSymbols = this.gatherSymbols(header.referredTo);
255
+ const symbolIdBits = require_image_jbig2_text.symbolCodeLength(inputSymbols.length + newSymbolCount);
256
+ const mq = new require_image_jbig2_arith.MqDecoder(cursor.data, cursor.position, header.dataEnd);
257
+ const contexts = require_image_jbig2_text.createTextArithContexts(template, refinementTemplate, symbolIdBits);
258
+ return require_image_jbig2_text.decodeSymbolDictionary({
259
+ template,
260
+ at,
261
+ refinementAggregate,
262
+ refinementTemplate,
263
+ refinementAt,
264
+ newSymbolCount,
265
+ exportedSymbolCount,
266
+ inputSymbols
267
+ }, mq, contexts, symbolIdBits);
268
+ }
269
+ readTextRegion(header, cursor) {
270
+ const region = readRegionInfo(cursor);
271
+ const flags = cursor.uint16();
272
+ if ((flags & 1) !== 0) throw new require_image_jbig2_errors.Jbig2UnsupportedError("JBIG2 text region is Huffman-coded (SBHUFF = 1); only the arithmetic form is implemented");
273
+ const refine = (flags & 2) !== 0;
274
+ const stripSize = 1 << (flags >> 2 & 3);
275
+ const referenceCorner = require_image_jbig2_text.referenceCornerFromCode(flags >> 4 & 3);
276
+ if (referenceCorner === void 0) throw new require_image_jbig2_errors.Jbig2ParseError("JBIG2 text region declares an unrecognised REFCORNER");
277
+ const transposed = (flags & 64) !== 0;
278
+ const combinationOperator = require_image_jbig2_bitmap.combinationOperatorFromCode(flags >> 7 & 3);
279
+ if (combinationOperator === void 0) throw new require_image_jbig2_errors.Jbig2ParseError("JBIG2 text region declares an unrecognised SBCOMBOP");
280
+ const defaultPixel = flags >> 9 & 1;
281
+ const dsOffset = flags << 17 >> 27 | 0;
282
+ const refinementTemplate = flags >> 15 & 1;
283
+ const refinementAt = refine && refinementTemplate === 0 ? readAtPixels(cursor, 2) : [...require_image_jbig2_generic.NOMINAL_REFINEMENT_AT];
284
+ const instanceCount = cursor.uint32();
285
+ const symbols = this.gatherSymbols(header.referredTo);
286
+ if (symbols.length === 0) throw new require_image_jbig2_errors.Jbig2ParseError("JBIG2 text region refers to no symbol dictionary, so it has no symbols to place");
287
+ const symbolIdBits = require_image_jbig2_text.symbolCodeLength(symbols.length);
288
+ const mq = new require_image_jbig2_arith.MqDecoder(cursor.data, cursor.position, header.dataEnd);
289
+ const contexts = require_image_jbig2_text.createTextArithContexts(0, refinementTemplate, symbolIdBits);
290
+ const params = {
291
+ width: region.width,
292
+ height: region.height,
293
+ instanceCount,
294
+ stripSize,
295
+ symbols,
296
+ defaultPixel,
297
+ combinationOperator,
298
+ transposed,
299
+ referenceCorner,
300
+ dsOffset,
301
+ refine,
302
+ refinementTemplate,
303
+ refinementAt
304
+ };
305
+ this.compose(region, require_image_jbig2_text.decodeTextRegion(params, mq, contexts, symbolIdBits));
306
+ }
307
+ gatherSymbols(referredTo) {
308
+ const symbols = [];
309
+ for (const segmentNumber of referredTo) {
310
+ const exported = this.exportedSymbols.get(segmentNumber);
311
+ if (exported !== void 0) symbols.push(...exported);
312
+ }
313
+ return symbols;
314
+ }
315
+ finish() {
316
+ const page = this.requirePage();
317
+ const width = this.options.width ?? page.bitmap.width;
318
+ const height = this.options.height ?? page.bitmap.height;
319
+ return {
320
+ bytes: require_image_jbig2_bitmap.packBitmapRows(page.bitmap, width, height),
321
+ width,
322
+ height
323
+ };
324
+ }
325
+ };
326
+ function decodeJbig2Embedded(data, options = {}) {
327
+ const decoder = new Jbig2Decoder(options);
328
+ if (options.globals !== void 0) decoder.run(options.globals);
329
+ decoder.run(data);
330
+ return decoder.finish();
331
+ }
332
+ //#endregion
333
+ exports.decodeJbig2Embedded = decodeJbig2Embedded;
@@ -0,0 +1,15 @@
1
+ //#region src/image/jbig2.d.ts
2
+ interface Jbig2DecodeOptions {
3
+ readonly globals?: Uint8Array<ArrayBuffer>;
4
+ readonly width?: number;
5
+ readonly height?: number;
6
+ readonly onWarning?: (message: string) => void;
7
+ }
8
+ interface Jbig2Image {
9
+ readonly bytes: Uint8Array<ArrayBuffer>;
10
+ readonly width: number;
11
+ readonly height: number;
12
+ }
13
+ declare function decodeJbig2Embedded(data: Uint8Array<ArrayBuffer>, options?: Jbig2DecodeOptions): Jbig2Image;
14
+ //#endregion
15
+ export { Jbig2DecodeOptions, Jbig2Image, decodeJbig2Embedded };
@@ -0,0 +1,15 @@
1
+ //#region src/image/jbig2.d.ts
2
+ interface Jbig2DecodeOptions {
3
+ readonly globals?: Uint8Array<ArrayBuffer>;
4
+ readonly width?: number;
5
+ readonly height?: number;
6
+ readonly onWarning?: (message: string) => void;
7
+ }
8
+ interface Jbig2Image {
9
+ readonly bytes: Uint8Array<ArrayBuffer>;
10
+ readonly width: number;
11
+ readonly height: number;
12
+ }
13
+ declare function decodeJbig2Embedded(data: Uint8Array<ArrayBuffer>, options?: Jbig2DecodeOptions): Jbig2Image;
14
+ //#endregion
15
+ export { Jbig2DecodeOptions, Jbig2Image, decodeJbig2Embedded };