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,242 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_image_jbig2_errors = require("./jbig2-errors.cjs");
3
+ const require_image_jbig2_bitmap = require("./jbig2-bitmap.cjs");
4
+ //#region src/image/jbig2-generic.ts
5
+ function fixed(dx, dy) {
6
+ return {
7
+ kind: "fixed",
8
+ dx,
9
+ dy
10
+ };
11
+ }
12
+ function at(index) {
13
+ return {
14
+ kind: "at",
15
+ index
16
+ };
17
+ }
18
+ const GENERIC_TEMPLATES = [
19
+ [
20
+ at(3),
21
+ fixed(-1, -2),
22
+ fixed(0, -2),
23
+ fixed(1, -2),
24
+ at(2),
25
+ at(1),
26
+ fixed(-2, -1),
27
+ fixed(-1, -1),
28
+ fixed(0, -1),
29
+ fixed(1, -1),
30
+ fixed(2, -1),
31
+ at(0),
32
+ fixed(-4, 0),
33
+ fixed(-3, 0),
34
+ fixed(-2, 0),
35
+ fixed(-1, 0)
36
+ ],
37
+ [
38
+ fixed(-1, -2),
39
+ fixed(0, -2),
40
+ fixed(1, -2),
41
+ fixed(2, -2),
42
+ fixed(-2, -1),
43
+ fixed(-1, -1),
44
+ fixed(0, -1),
45
+ fixed(1, -1),
46
+ fixed(2, -1),
47
+ at(0),
48
+ fixed(-3, 0),
49
+ fixed(-2, 0),
50
+ fixed(-1, 0)
51
+ ],
52
+ [
53
+ fixed(-1, -2),
54
+ fixed(0, -2),
55
+ fixed(1, -2),
56
+ fixed(-2, -1),
57
+ fixed(-1, -1),
58
+ fixed(0, -1),
59
+ fixed(1, -1),
60
+ at(0),
61
+ fixed(-2, 0),
62
+ fixed(-1, 0)
63
+ ],
64
+ [
65
+ fixed(-3, -1),
66
+ fixed(-2, -1),
67
+ fixed(-1, -1),
68
+ fixed(0, -1),
69
+ fixed(1, -1),
70
+ at(0),
71
+ fixed(-4, 0),
72
+ fixed(-3, 0),
73
+ fixed(-2, 0),
74
+ fixed(-1, 0)
75
+ ]
76
+ ];
77
+ const GENERIC_SLTP_CONTEXT = [
78
+ 39717,
79
+ 1941,
80
+ 229,
81
+ 405
82
+ ];
83
+ const GENERIC_CONTEXT_BITS = [
84
+ 16,
85
+ 13,
86
+ 10,
87
+ 10
88
+ ];
89
+ function resolveTemplate(template) {
90
+ const positions = GENERIC_TEMPLATES[template];
91
+ if (positions === void 0) throw new require_image_jbig2_errors.Jbig2UnsupportedError(`generic region declares GBTEMPLATE ${String(template)}, which is outside the 0-3 range T.88 6.2.5.3 defines`);
92
+ return positions;
93
+ }
94
+ function flattenTemplate(positions, at) {
95
+ const dx = new Int32Array(positions.length);
96
+ const dy = new Int32Array(positions.length);
97
+ positions.forEach((position, i) => {
98
+ const offset = position.kind === "fixed" ? {
99
+ x: position.dx,
100
+ y: position.dy
101
+ } : at[position.index] ?? {
102
+ x: 0,
103
+ y: 0
104
+ };
105
+ dx[i] = offset.x;
106
+ dy[i] = offset.y;
107
+ });
108
+ return {
109
+ dx,
110
+ dy
111
+ };
112
+ }
113
+ function decodeGenericRegion(width, height, params, mq, contexts) {
114
+ const bitmap = require_image_jbig2_bitmap.createBitmap(width, height);
115
+ const positions = resolveTemplate(params.template);
116
+ const sltpContext = GENERIC_SLTP_CONTEXT[params.template] ?? 0;
117
+ const { dx, dy } = flattenTemplate(positions, params.at);
118
+ const data = bitmap.data;
119
+ const count = positions.length;
120
+ const flatOffset = new Int32Array(count);
121
+ let minDx = 0;
122
+ let maxDx = 0;
123
+ let minDy = 0;
124
+ let maxDy = 0;
125
+ for (let i = 0; i < count; i++) {
126
+ flatOffset[i] = dy[i] * width + dx[i];
127
+ minDx = Math.min(minDx, dx[i]);
128
+ maxDx = Math.max(maxDx, dx[i]);
129
+ minDy = Math.min(minDy, dy[i]);
130
+ maxDy = Math.max(maxDy, dy[i]);
131
+ }
132
+ const interiorFromX = -minDx;
133
+ const interiorToX = width - maxDx;
134
+ let typicalPrediction = 0;
135
+ for (let y = 0; y < height; y++) {
136
+ if (params.tpgdon) {
137
+ typicalPrediction ^= mq.decode(contexts, sltpContext);
138
+ if (typicalPrediction === 1) {
139
+ if (y > 0) data.copyWithin(y * width, (y - 1) * width, y * width);
140
+ continue;
141
+ }
142
+ }
143
+ const rowInterior = y + minDy >= 0 && y + maxDy < height;
144
+ const rowBase = y * width;
145
+ for (let x = 0; x < width; x++) {
146
+ let context = 0;
147
+ if (rowInterior && x >= interiorFromX && x < interiorToX) {
148
+ const base = rowBase + x;
149
+ for (let i = 0; i < count; i++) context = context << 1 | data[base + flatOffset[i]];
150
+ } else for (let i = 0; i < count; i++) context = context << 1 | require_image_jbig2_bitmap.getPixel(bitmap, x + dx[i], y + dy[i]);
151
+ data[rowBase + x] = mq.decode(contexts, context);
152
+ }
153
+ }
154
+ return bitmap;
155
+ }
156
+ function destination(dx, dy) {
157
+ return {
158
+ source: "destination",
159
+ dx,
160
+ dy
161
+ };
162
+ }
163
+ function reference(dx, dy) {
164
+ return {
165
+ source: "reference",
166
+ dx,
167
+ dy
168
+ };
169
+ }
170
+ const REFINEMENT_TEMPLATES = [[
171
+ destination(0, -1),
172
+ destination(1, -1),
173
+ destination(-1, 0),
174
+ {
175
+ source: "destination-at",
176
+ index: 0
177
+ },
178
+ reference(0, -1),
179
+ reference(1, -1),
180
+ reference(-1, 0),
181
+ reference(0, 0),
182
+ reference(1, 0),
183
+ reference(-1, 1),
184
+ reference(0, 1),
185
+ reference(1, 1),
186
+ {
187
+ source: "reference-at",
188
+ index: 1
189
+ }
190
+ ], [
191
+ destination(-1, -1),
192
+ destination(0, -1),
193
+ destination(1, -1),
194
+ destination(-1, 0),
195
+ reference(0, -1),
196
+ reference(-1, 0),
197
+ reference(0, 0),
198
+ reference(1, 0),
199
+ reference(0, 1),
200
+ reference(1, 1)
201
+ ]];
202
+ const TPGRON_UNSUPPORTED = "JBIG2 refinement region sets TPGRON (typical prediction, T.88 6.3.5.6), whose per-template pseudo-context constants this decoder has no way to verify against a real encoder; refusing rather than risking a silently wrong bitmap";
203
+ const REFINEMENT_CONTEXT_BITS = [13, 10];
204
+ const NOMINAL_REFINEMENT_AT = [{
205
+ x: -1,
206
+ y: -1
207
+ }, {
208
+ x: -1,
209
+ y: -1
210
+ }];
211
+ function decodeRefinementRegion(width, height, params, mq, contexts) {
212
+ const positions = REFINEMENT_TEMPLATES[params.template];
213
+ if (positions === void 0) throw new require_image_jbig2_errors.Jbig2UnsupportedError(`refinement region declares GRTEMPLATE ${String(params.template)}, which is outside the 0-1 range T.88 6.3.5.3 defines`);
214
+ if (params.tpgron) throw new require_image_jbig2_errors.Jbig2UnsupportedError(TPGRON_UNSUPPORTED);
215
+ const bitmap = require_image_jbig2_bitmap.createBitmap(width, height);
216
+ for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
217
+ let context = 0;
218
+ for (const position of positions) if (position.source === "destination") context = context << 1 | require_image_jbig2_bitmap.getPixel(bitmap, x + position.dx, y + position.dy);
219
+ else if (position.source === "reference") context = context << 1 | require_image_jbig2_bitmap.getPixel(params.reference, x - params.dx + position.dx, y - params.dy + position.dy);
220
+ else if (position.source === "destination-at") {
221
+ const atPixel = params.at[position.index] ?? {
222
+ x: 0,
223
+ y: 0
224
+ };
225
+ context = context << 1 | require_image_jbig2_bitmap.getPixel(bitmap, x + atPixel.x, y + atPixel.y);
226
+ } else {
227
+ const atPixel = params.at[position.index] ?? {
228
+ x: 0,
229
+ y: 0
230
+ };
231
+ context = context << 1 | require_image_jbig2_bitmap.getPixel(params.reference, x - params.dx + atPixel.x, y - params.dy + atPixel.y);
232
+ }
233
+ bitmap.data[y * width + x] = mq.decode(contexts, context);
234
+ }
235
+ return bitmap;
236
+ }
237
+ //#endregion
238
+ exports.GENERIC_CONTEXT_BITS = GENERIC_CONTEXT_BITS;
239
+ exports.NOMINAL_REFINEMENT_AT = NOMINAL_REFINEMENT_AT;
240
+ exports.REFINEMENT_CONTEXT_BITS = REFINEMENT_CONTEXT_BITS;
241
+ exports.decodeGenericRegion = decodeGenericRegion;
242
+ exports.decodeRefinementRegion = decodeRefinementRegion;
@@ -0,0 +1,27 @@
1
+ import { ArithContexts, MqDecoder } from "./jbig2-arith.cjs";
2
+ import { Jbig2Bitmap } from "./jbig2-bitmap.cjs";
3
+ //#region src/image/jbig2-generic.d.ts
4
+ interface Jbig2AtPixel {
5
+ readonly x: number;
6
+ readonly y: number;
7
+ }
8
+ declare const GENERIC_CONTEXT_BITS: readonly number[];
9
+ interface GenericRegionParams {
10
+ readonly template: number;
11
+ readonly tpgdon: boolean;
12
+ readonly at: readonly Jbig2AtPixel[];
13
+ }
14
+ declare function decodeGenericRegion(width: number, height: number, params: GenericRegionParams, mq: MqDecoder, contexts: ArithContexts): Jbig2Bitmap;
15
+ declare const REFINEMENT_CONTEXT_BITS: readonly number[];
16
+ declare const NOMINAL_REFINEMENT_AT: readonly Jbig2AtPixel[];
17
+ interface RefinementRegionParams {
18
+ readonly template: number;
19
+ readonly tpgron: boolean;
20
+ readonly at: readonly Jbig2AtPixel[];
21
+ readonly reference: Jbig2Bitmap;
22
+ readonly dx: number;
23
+ readonly dy: number;
24
+ }
25
+ declare function decodeRefinementRegion(width: number, height: number, params: RefinementRegionParams, mq: MqDecoder, contexts: ArithContexts): Jbig2Bitmap;
26
+ //#endregion
27
+ export { GENERIC_CONTEXT_BITS, GenericRegionParams, Jbig2AtPixel, NOMINAL_REFINEMENT_AT, REFINEMENT_CONTEXT_BITS, RefinementRegionParams, decodeGenericRegion, decodeRefinementRegion };
@@ -0,0 +1,27 @@
1
+ import { ArithContexts, MqDecoder } from "./jbig2-arith.js";
2
+ import { Jbig2Bitmap } from "./jbig2-bitmap.js";
3
+ //#region src/image/jbig2-generic.d.ts
4
+ interface Jbig2AtPixel {
5
+ readonly x: number;
6
+ readonly y: number;
7
+ }
8
+ declare const GENERIC_CONTEXT_BITS: readonly number[];
9
+ interface GenericRegionParams {
10
+ readonly template: number;
11
+ readonly tpgdon: boolean;
12
+ readonly at: readonly Jbig2AtPixel[];
13
+ }
14
+ declare function decodeGenericRegion(width: number, height: number, params: GenericRegionParams, mq: MqDecoder, contexts: ArithContexts): Jbig2Bitmap;
15
+ declare const REFINEMENT_CONTEXT_BITS: readonly number[];
16
+ declare const NOMINAL_REFINEMENT_AT: readonly Jbig2AtPixel[];
17
+ interface RefinementRegionParams {
18
+ readonly template: number;
19
+ readonly tpgron: boolean;
20
+ readonly at: readonly Jbig2AtPixel[];
21
+ readonly reference: Jbig2Bitmap;
22
+ readonly dx: number;
23
+ readonly dy: number;
24
+ }
25
+ declare function decodeRefinementRegion(width: number, height: number, params: RefinementRegionParams, mq: MqDecoder, contexts: ArithContexts): Jbig2Bitmap;
26
+ //#endregion
27
+ export { GENERIC_CONTEXT_BITS, GenericRegionParams, Jbig2AtPixel, NOMINAL_REFINEMENT_AT, REFINEMENT_CONTEXT_BITS, RefinementRegionParams, decodeGenericRegion, decodeRefinementRegion };
@@ -0,0 +1,237 @@
1
+ import { Jbig2UnsupportedError } from "./jbig2-errors.js";
2
+ import { createBitmap, getPixel } from "./jbig2-bitmap.js";
3
+ //#region src/image/jbig2-generic.ts
4
+ function fixed(dx, dy) {
5
+ return {
6
+ kind: "fixed",
7
+ dx,
8
+ dy
9
+ };
10
+ }
11
+ function at(index) {
12
+ return {
13
+ kind: "at",
14
+ index
15
+ };
16
+ }
17
+ const GENERIC_TEMPLATES = [
18
+ [
19
+ at(3),
20
+ fixed(-1, -2),
21
+ fixed(0, -2),
22
+ fixed(1, -2),
23
+ at(2),
24
+ at(1),
25
+ fixed(-2, -1),
26
+ fixed(-1, -1),
27
+ fixed(0, -1),
28
+ fixed(1, -1),
29
+ fixed(2, -1),
30
+ at(0),
31
+ fixed(-4, 0),
32
+ fixed(-3, 0),
33
+ fixed(-2, 0),
34
+ fixed(-1, 0)
35
+ ],
36
+ [
37
+ fixed(-1, -2),
38
+ fixed(0, -2),
39
+ fixed(1, -2),
40
+ fixed(2, -2),
41
+ fixed(-2, -1),
42
+ fixed(-1, -1),
43
+ fixed(0, -1),
44
+ fixed(1, -1),
45
+ fixed(2, -1),
46
+ at(0),
47
+ fixed(-3, 0),
48
+ fixed(-2, 0),
49
+ fixed(-1, 0)
50
+ ],
51
+ [
52
+ fixed(-1, -2),
53
+ fixed(0, -2),
54
+ fixed(1, -2),
55
+ fixed(-2, -1),
56
+ fixed(-1, -1),
57
+ fixed(0, -1),
58
+ fixed(1, -1),
59
+ at(0),
60
+ fixed(-2, 0),
61
+ fixed(-1, 0)
62
+ ],
63
+ [
64
+ fixed(-3, -1),
65
+ fixed(-2, -1),
66
+ fixed(-1, -1),
67
+ fixed(0, -1),
68
+ fixed(1, -1),
69
+ at(0),
70
+ fixed(-4, 0),
71
+ fixed(-3, 0),
72
+ fixed(-2, 0),
73
+ fixed(-1, 0)
74
+ ]
75
+ ];
76
+ const GENERIC_SLTP_CONTEXT = [
77
+ 39717,
78
+ 1941,
79
+ 229,
80
+ 405
81
+ ];
82
+ const GENERIC_CONTEXT_BITS = [
83
+ 16,
84
+ 13,
85
+ 10,
86
+ 10
87
+ ];
88
+ function resolveTemplate(template) {
89
+ const positions = GENERIC_TEMPLATES[template];
90
+ if (positions === void 0) throw new Jbig2UnsupportedError(`generic region declares GBTEMPLATE ${String(template)}, which is outside the 0-3 range T.88 6.2.5.3 defines`);
91
+ return positions;
92
+ }
93
+ function flattenTemplate(positions, at) {
94
+ const dx = new Int32Array(positions.length);
95
+ const dy = new Int32Array(positions.length);
96
+ positions.forEach((position, i) => {
97
+ const offset = position.kind === "fixed" ? {
98
+ x: position.dx,
99
+ y: position.dy
100
+ } : at[position.index] ?? {
101
+ x: 0,
102
+ y: 0
103
+ };
104
+ dx[i] = offset.x;
105
+ dy[i] = offset.y;
106
+ });
107
+ return {
108
+ dx,
109
+ dy
110
+ };
111
+ }
112
+ function decodeGenericRegion(width, height, params, mq, contexts) {
113
+ const bitmap = createBitmap(width, height);
114
+ const positions = resolveTemplate(params.template);
115
+ const sltpContext = GENERIC_SLTP_CONTEXT[params.template] ?? 0;
116
+ const { dx, dy } = flattenTemplate(positions, params.at);
117
+ const data = bitmap.data;
118
+ const count = positions.length;
119
+ const flatOffset = new Int32Array(count);
120
+ let minDx = 0;
121
+ let maxDx = 0;
122
+ let minDy = 0;
123
+ let maxDy = 0;
124
+ for (let i = 0; i < count; i++) {
125
+ flatOffset[i] = dy[i] * width + dx[i];
126
+ minDx = Math.min(minDx, dx[i]);
127
+ maxDx = Math.max(maxDx, dx[i]);
128
+ minDy = Math.min(minDy, dy[i]);
129
+ maxDy = Math.max(maxDy, dy[i]);
130
+ }
131
+ const interiorFromX = -minDx;
132
+ const interiorToX = width - maxDx;
133
+ let typicalPrediction = 0;
134
+ for (let y = 0; y < height; y++) {
135
+ if (params.tpgdon) {
136
+ typicalPrediction ^= mq.decode(contexts, sltpContext);
137
+ if (typicalPrediction === 1) {
138
+ if (y > 0) data.copyWithin(y * width, (y - 1) * width, y * width);
139
+ continue;
140
+ }
141
+ }
142
+ const rowInterior = y + minDy >= 0 && y + maxDy < height;
143
+ const rowBase = y * width;
144
+ for (let x = 0; x < width; x++) {
145
+ let context = 0;
146
+ if (rowInterior && x >= interiorFromX && x < interiorToX) {
147
+ const base = rowBase + x;
148
+ for (let i = 0; i < count; i++) context = context << 1 | data[base + flatOffset[i]];
149
+ } else for (let i = 0; i < count; i++) context = context << 1 | getPixel(bitmap, x + dx[i], y + dy[i]);
150
+ data[rowBase + x] = mq.decode(contexts, context);
151
+ }
152
+ }
153
+ return bitmap;
154
+ }
155
+ function destination(dx, dy) {
156
+ return {
157
+ source: "destination",
158
+ dx,
159
+ dy
160
+ };
161
+ }
162
+ function reference(dx, dy) {
163
+ return {
164
+ source: "reference",
165
+ dx,
166
+ dy
167
+ };
168
+ }
169
+ const REFINEMENT_TEMPLATES = [[
170
+ destination(0, -1),
171
+ destination(1, -1),
172
+ destination(-1, 0),
173
+ {
174
+ source: "destination-at",
175
+ index: 0
176
+ },
177
+ reference(0, -1),
178
+ reference(1, -1),
179
+ reference(-1, 0),
180
+ reference(0, 0),
181
+ reference(1, 0),
182
+ reference(-1, 1),
183
+ reference(0, 1),
184
+ reference(1, 1),
185
+ {
186
+ source: "reference-at",
187
+ index: 1
188
+ }
189
+ ], [
190
+ destination(-1, -1),
191
+ destination(0, -1),
192
+ destination(1, -1),
193
+ destination(-1, 0),
194
+ reference(0, -1),
195
+ reference(-1, 0),
196
+ reference(0, 0),
197
+ reference(1, 0),
198
+ reference(0, 1),
199
+ reference(1, 1)
200
+ ]];
201
+ const TPGRON_UNSUPPORTED = "JBIG2 refinement region sets TPGRON (typical prediction, T.88 6.3.5.6), whose per-template pseudo-context constants this decoder has no way to verify against a real encoder; refusing rather than risking a silently wrong bitmap";
202
+ const REFINEMENT_CONTEXT_BITS = [13, 10];
203
+ const NOMINAL_REFINEMENT_AT = [{
204
+ x: -1,
205
+ y: -1
206
+ }, {
207
+ x: -1,
208
+ y: -1
209
+ }];
210
+ function decodeRefinementRegion(width, height, params, mq, contexts) {
211
+ const positions = REFINEMENT_TEMPLATES[params.template];
212
+ if (positions === void 0) throw new Jbig2UnsupportedError(`refinement region declares GRTEMPLATE ${String(params.template)}, which is outside the 0-1 range T.88 6.3.5.3 defines`);
213
+ if (params.tpgron) throw new Jbig2UnsupportedError(TPGRON_UNSUPPORTED);
214
+ const bitmap = createBitmap(width, height);
215
+ for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
216
+ let context = 0;
217
+ for (const position of positions) if (position.source === "destination") context = context << 1 | getPixel(bitmap, x + position.dx, y + position.dy);
218
+ else if (position.source === "reference") context = context << 1 | getPixel(params.reference, x - params.dx + position.dx, y - params.dy + position.dy);
219
+ else if (position.source === "destination-at") {
220
+ const atPixel = params.at[position.index] ?? {
221
+ x: 0,
222
+ y: 0
223
+ };
224
+ context = context << 1 | getPixel(bitmap, x + atPixel.x, y + atPixel.y);
225
+ } else {
226
+ const atPixel = params.at[position.index] ?? {
227
+ x: 0,
228
+ y: 0
229
+ };
230
+ context = context << 1 | getPixel(params.reference, x - params.dx + atPixel.x, y - params.dy + atPixel.y);
231
+ }
232
+ bitmap.data[y * width + x] = mq.decode(contexts, context);
233
+ }
234
+ return bitmap;
235
+ }
236
+ //#endregion
237
+ export { GENERIC_CONTEXT_BITS, NOMINAL_REFINEMENT_AT, REFINEMENT_CONTEXT_BITS, decodeGenericRegion, decodeRefinementRegion };
@@ -0,0 +1,175 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_image_jbig2_arith = require("./jbig2-arith.cjs");
3
+ const require_image_jbig2_errors = require("./jbig2-errors.cjs");
4
+ const require_image_jbig2_bitmap = require("./jbig2-bitmap.cjs");
5
+ const require_image_jbig2_generic = require("./jbig2-generic.cjs");
6
+ //#region src/image/jbig2-text.ts
7
+ const REFERENCE_CORNERS = [
8
+ {
9
+ right: false,
10
+ bottom: true
11
+ },
12
+ {
13
+ right: false,
14
+ bottom: false
15
+ },
16
+ {
17
+ right: true,
18
+ bottom: true
19
+ },
20
+ {
21
+ right: true,
22
+ bottom: false
23
+ }
24
+ ];
25
+ function referenceCornerFromCode(code) {
26
+ return REFERENCE_CORNERS[code];
27
+ }
28
+ function symbolCodeLength(symbolCount) {
29
+ let bits = 1;
30
+ while (1 << bits < symbolCount) bits++;
31
+ return bits;
32
+ }
33
+ function createTextArithContexts(genericTemplate, refinementTemplate, symbolIdBits) {
34
+ return {
35
+ iadh: require_image_jbig2_arith.createIntegerContexts(),
36
+ iadw: require_image_jbig2_arith.createIntegerContexts(),
37
+ iaex: require_image_jbig2_arith.createIntegerContexts(),
38
+ iaai: require_image_jbig2_arith.createIntegerContexts(),
39
+ iadt: require_image_jbig2_arith.createIntegerContexts(),
40
+ iafs: require_image_jbig2_arith.createIntegerContexts(),
41
+ iads: require_image_jbig2_arith.createIntegerContexts(),
42
+ iait: require_image_jbig2_arith.createIntegerContexts(),
43
+ iari: require_image_jbig2_arith.createIntegerContexts(),
44
+ iardw: require_image_jbig2_arith.createIntegerContexts(),
45
+ iardh: require_image_jbig2_arith.createIntegerContexts(),
46
+ iardx: require_image_jbig2_arith.createIntegerContexts(),
47
+ iardy: require_image_jbig2_arith.createIntegerContexts(),
48
+ iaid: require_image_jbig2_arith.createSymbolIdContexts(symbolIdBits),
49
+ generic: require_image_jbig2_arith.createArithContexts(require_image_jbig2_generic.GENERIC_CONTEXT_BITS[genericTemplate] ?? 16),
50
+ refinement: require_image_jbig2_arith.createArithContexts(require_image_jbig2_generic.REFINEMENT_CONTEXT_BITS[refinementTemplate] ?? 13)
51
+ };
52
+ }
53
+ function instanceTopLeft(s, t, width, height, corner, transposed) {
54
+ const across = transposed ? t : s;
55
+ const down = transposed ? s : t;
56
+ return {
57
+ x: corner.right ? across - width + 1 : across,
58
+ y: corner.bottom ? down - height + 1 : down
59
+ };
60
+ }
61
+ function requireInteger(value, field) {
62
+ if (value === void 0) throw new require_image_jbig2_errors.Jbig2ParseError(`JBIG2 ${field} decoded as out-of-band, which is not a value this field can take`);
63
+ return value;
64
+ }
65
+ function decodeTextRegion(params, mq, contexts, symbolIdBits) {
66
+ const region = require_image_jbig2_bitmap.createBitmap(params.width, params.height, params.defaultPixel);
67
+ let stripT = -requireInteger(require_image_jbig2_arith.decodeInteger(mq, contexts.iadt), "IADT") * params.stripSize;
68
+ let firstS = 0;
69
+ let instances = 0;
70
+ while (instances < params.instanceCount) {
71
+ stripT += requireInteger(require_image_jbig2_arith.decodeInteger(mq, contexts.iadt), "IADT") * params.stripSize;
72
+ firstS += requireInteger(require_image_jbig2_arith.decodeInteger(mq, contexts.iafs), "IAFS");
73
+ let currentS = firstS;
74
+ for (;;) {
75
+ const currentT = params.stripSize === 1 ? 0 : requireInteger(require_image_jbig2_arith.decodeInteger(mq, contexts.iait), "IAIT");
76
+ const t = stripT + currentT;
77
+ const id = require_image_jbig2_arith.decodeSymbolId(mq, contexts.iaid, symbolIdBits);
78
+ let symbol = params.symbols[id];
79
+ if (symbol === void 0) throw new require_image_jbig2_errors.Jbig2ParseError(`text region referenced symbol ${String(id)}, outside the ${String(params.symbols.length)}-symbol set its segment declares`);
80
+ if (params.refine && requireInteger(require_image_jbig2_arith.decodeInteger(mq, contexts.iari), "IARI") !== 0) symbol = refineInstance(symbol, params, mq, contexts);
81
+ const advance = (params.transposed ? symbol.height : symbol.width) - 1;
82
+ const advanceBefore = params.transposed ? params.referenceCorner.bottom : params.referenceCorner.right;
83
+ if (advanceBefore) currentS += advance;
84
+ const position = instanceTopLeft(currentS, t, symbol.width, symbol.height, params.referenceCorner, params.transposed);
85
+ require_image_jbig2_bitmap.combineBitmap(region, symbol, position.x, position.y, params.combinationOperator);
86
+ if (!advanceBefore) currentS += advance;
87
+ instances++;
88
+ if (instances > params.instanceCount) throw new require_image_jbig2_errors.Jbig2ParseError(`text region coded more symbol instances than the ${String(params.instanceCount)} its own SBNUMINSTANCES declares`);
89
+ const nextDelta = require_image_jbig2_arith.decodeInteger(mq, contexts.iads);
90
+ if (nextDelta === void 0) break;
91
+ currentS += nextDelta + params.dsOffset;
92
+ }
93
+ }
94
+ return region;
95
+ }
96
+ function refineInstance(symbol, params, mq, contexts) {
97
+ const deltaWidth = require_image_jbig2_arith.decodeInteger(mq, contexts.iardw) ?? 0;
98
+ const deltaHeight = require_image_jbig2_arith.decodeInteger(mq, contexts.iardh) ?? 0;
99
+ const deltaX = require_image_jbig2_arith.decodeInteger(mq, contexts.iardx) ?? 0;
100
+ const deltaY = require_image_jbig2_arith.decodeInteger(mq, contexts.iardy) ?? 0;
101
+ return require_image_jbig2_generic.decodeRefinementRegion(symbol.width + deltaWidth, symbol.height + deltaHeight, {
102
+ template: params.refinementTemplate,
103
+ tpgron: false,
104
+ at: params.refinementAt,
105
+ reference: symbol,
106
+ dx: Math.floor(deltaWidth / 2) + deltaX,
107
+ dy: Math.floor(deltaHeight / 2) + deltaY
108
+ }, mq, contexts.refinement);
109
+ }
110
+ function decodeSymbolDictionary(params, mq, contexts, symbolIdBits) {
111
+ const newSymbols = [];
112
+ let heightClassHeight = 0;
113
+ while (newSymbols.length < params.newSymbolCount) {
114
+ const heightDelta = require_image_jbig2_arith.decodeInteger(mq, contexts.iadh);
115
+ if (heightDelta === void 0) throw new require_image_jbig2_errors.Jbig2ParseError("symbol dictionary ended before all of its declared new symbols were decoded");
116
+ heightClassHeight += heightDelta;
117
+ let symbolWidth = 0;
118
+ for (;;) {
119
+ const widthDelta = require_image_jbig2_arith.decodeInteger(mq, contexts.iadw);
120
+ if (widthDelta === void 0) break;
121
+ symbolWidth += widthDelta;
122
+ if (newSymbols.length >= params.newSymbolCount) throw new require_image_jbig2_errors.Jbig2ParseError("symbol dictionary coded more symbols than its own SDNUMNEWSYMS declares");
123
+ newSymbols.push(decodeDictionarySymbol(symbolWidth, heightClassHeight, params, newSymbols, mq, contexts, symbolIdBits));
124
+ }
125
+ }
126
+ return exportSymbols([...params.inputSymbols, ...newSymbols], params.exportedSymbolCount, mq, contexts);
127
+ }
128
+ function decodeDictionarySymbol(width, height, params, newSymbols, mq, contexts, symbolIdBits) {
129
+ if (!params.refinementAggregate) return require_image_jbig2_generic.decodeGenericRegion(width, height, {
130
+ template: params.template,
131
+ tpgdon: false,
132
+ at: params.at
133
+ }, mq, contexts.generic);
134
+ const aggregateInstances = require_image_jbig2_arith.decodeInteger(mq, contexts.iaai) ?? 0;
135
+ if (aggregateInstances !== 1) throw new require_image_jbig2_errors.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`);
136
+ const available = [...params.inputSymbols, ...newSymbols];
137
+ const id = require_image_jbig2_arith.decodeSymbolId(mq, contexts.iaid, symbolIdBits);
138
+ const deltaX = require_image_jbig2_arith.decodeInteger(mq, contexts.iardx) ?? 0;
139
+ const deltaY = require_image_jbig2_arith.decodeInteger(mq, contexts.iardy) ?? 0;
140
+ const reference = available[id];
141
+ if (reference === void 0) throw new require_image_jbig2_errors.Jbig2ParseError(`symbol dictionary refined against symbol ${String(id)}, which it has not decoded yet`);
142
+ return require_image_jbig2_generic.decodeRefinementRegion(width, height, {
143
+ template: params.refinementTemplate,
144
+ tpgron: false,
145
+ at: params.refinementAt,
146
+ reference,
147
+ dx: deltaX,
148
+ dy: deltaY
149
+ }, mq, contexts.refinement);
150
+ }
151
+ function exportSymbols(allSymbols, exportedCount, mq, contexts) {
152
+ const exported = [];
153
+ const maxRuns = allSymbols.length * 2 + 1;
154
+ let index = 0;
155
+ let currentFlag = false;
156
+ for (let run = 0; index < allSymbols.length; run++) {
157
+ if (run >= maxRuns) throw new require_image_jbig2_errors.Jbig2ParseError("symbol dictionary export flags never accounted for every symbol");
158
+ const runLength = requireInteger(require_image_jbig2_arith.decodeInteger(mq, contexts.iaex), "IAEX");
159
+ if (runLength < 0 || index + runLength > allSymbols.length) throw new require_image_jbig2_errors.Jbig2ParseError(`symbol dictionary export run of ${String(runLength)} overruns its own ${String(allSymbols.length)}-symbol set`);
160
+ if (currentFlag) for (let i = 0; i < runLength; i++) {
161
+ const symbol = allSymbols[index + i];
162
+ if (symbol !== void 0) exported.push(symbol);
163
+ }
164
+ index += runLength;
165
+ currentFlag = !currentFlag;
166
+ }
167
+ if (exported.length !== exportedCount) throw new require_image_jbig2_errors.Jbig2ParseError(`symbol dictionary exported ${String(exported.length)} symbols but its own SDNUMEXSYMS declares ${String(exportedCount)}`);
168
+ return exported;
169
+ }
170
+ //#endregion
171
+ exports.createTextArithContexts = createTextArithContexts;
172
+ exports.decodeSymbolDictionary = decodeSymbolDictionary;
173
+ exports.decodeTextRegion = decodeTextRegion;
174
+ exports.referenceCornerFromCode = referenceCornerFromCode;
175
+ exports.symbolCodeLength = symbolCodeLength;