pdf-parser.js 5.2.13 → 5.2.14

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.
@@ -1,12 +1,8 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_image_jpeg2000_errors = require("./jpeg2000-errors.cjs");
3
+ const require_image_jpeg2000_dwt = require("./jpeg2000-dwt.cjs");
3
4
  const require_image_jpeg2000_tagtree = require("./jpeg2000-tagtree.cjs");
4
5
  //#region src/image/jpeg2000-t2.ts
5
- const HIGHER_RESOLUTION_BANDS = [
6
- "HL",
7
- "LH",
8
- "HH"
9
- ];
10
6
  const SUBBAND_GAIN_LOG2 = {
11
7
  LL: 0,
12
8
  HL: 1,
@@ -50,10 +46,9 @@ function precinctGrid(bounds, ppx, ppy) {
50
46
  }
51
47
  function tileHasSubdividedPrecincts(siz, tileX, tileY, codingPerComponent) {
52
48
  const tile = tileBounds(siz, tileX, tileY);
53
- for (let c = 0; c < siz.components.length; c++) {
54
- const size = siz.components[c];
49
+ for (const [c, size] of siz.components.entries()) {
55
50
  const coding = codingPerComponent[c];
56
- if (size === void 0 || coding === void 0) continue;
51
+ if (coding === void 0) continue;
57
52
  const component = componentBounds(tile, size.dx, size.dy);
58
53
  for (let r = 0; r <= coding.decompositionLevels; r++) {
59
54
  const precinctSize = coding.precinctSizes[r];
@@ -65,7 +60,6 @@ function tileHasSubdividedPrecincts(siz, tileX, tileY, codingPerComponent) {
65
60
  return false;
66
61
  }
67
62
  function bandCoordinate(componentCoordinate, levels, orientation) {
68
- if (levels === 0) return componentCoordinate;
69
63
  const half = 2 ** (levels - 1);
70
64
  return Math.ceil((componentCoordinate - half * orientation) / (2 * half));
71
65
  }
@@ -137,6 +131,11 @@ function buildTileGeometry(siz, tileX, tileY, codingPerComponent, quantizationPe
137
131
  if (componentSize === void 0 || coding === void 0 || quantization === void 0) throw new require_image_jpeg2000_errors.Jpeg2000ParseError(`no coding style or quantization is defined for component ${String(c)}`);
138
132
  const bounds = componentBounds(tile, componentSize.dx, componentSize.dy);
139
133
  const levels = coding.decompositionLevels;
134
+ const higherResolutionBands = [
135
+ "HL",
136
+ "LH",
137
+ "HH"
138
+ ];
140
139
  const resolutions = [];
141
140
  for (let r = 0; r <= levels; r++) {
142
141
  const level = resolutionBounds(bounds, levels, r);
@@ -148,7 +147,7 @@ function buildTileGeometry(siz, tileX, tileY, codingPerComponent, quantizationPe
148
147
  const { wide: precinctsWide, high: precinctsHigh } = precinctGrid(level, ppx, ppy);
149
148
  const bandPpx = r === 0 ? ppx : ppx - 1;
150
149
  const bandPpy = r === 0 ? ppy : ppy - 1;
151
- const bandTypes = r === 0 ? ["LL"] : HIGHER_RESOLUTION_BANDS;
150
+ const bandTypes = r === 0 ? ["LL"] : higherResolutionBands;
152
151
  const subbands = [];
153
152
  for (let b = 0; b < bandTypes.length; b++) {
154
153
  const type = bandTypes[b] ?? "LL";
@@ -229,23 +228,29 @@ function buildPacketSequence(tile, order, layers) {
229
228
  });
230
229
  };
231
230
  if (order === "LRCP") {
232
- for (let l = 0; l < layers; l++) for (let r = 0; r < maxResolutions; r++) for (let c = 0; c < tile.components.length; c++) for (let p = 0; p < precinctCount(c, r); p++) push(l, r, c, p);
231
+ for (let l = 0; l < layers; l++) require_image_jpeg2000_dwt.times(maxResolutions, (r) => {
232
+ for (const [c] of tile.components.entries()) for (let p = 0; p < precinctCount(c, r); p++) push(l, r, c, p);
233
+ });
233
234
  return packets;
234
235
  }
235
236
  if (order === "RLCP") {
236
- for (let r = 0; r < maxResolutions; r++) for (let l = 0; l < layers; l++) for (let c = 0; c < tile.components.length; c++) for (let p = 0; p < precinctCount(c, r); p++) push(l, r, c, p);
237
+ require_image_jpeg2000_dwt.times(maxResolutions, (r) => {
238
+ for (let l = 0; l < layers; l++) for (const [c] of tile.components.entries()) for (let p = 0; p < precinctCount(c, r); p++) push(l, r, c, p);
239
+ });
237
240
  return packets;
238
241
  }
239
- for (let c = 0; c < tile.components.length; c++) for (let r = 0; r < maxResolutions; r++) if (precinctCount(c, r) > 1) throw new require_image_jpeg2000_errors.Jpeg2000UnsupportedError(`progression order ${order} is only decoded here when every resolution level holds a single precinct, and this codestream subdivides at least one of them`);
242
+ for (const [c] of tile.components.entries()) require_image_jpeg2000_dwt.times(maxResolutions, (r) => {
243
+ if (precinctCount(c, r) > 1) throw new require_image_jpeg2000_errors.Jpeg2000UnsupportedError(`progression order ${order} is only decoded here when every resolution level holds a single precinct, and this codestream subdivides at least one of them`);
244
+ });
240
245
  if (order === "RPCL") {
241
- for (let r = 0; r < maxResolutions; r++) for (let c = 0; c < tile.components.length; c++) for (let l = 0; l < layers; l++) if (precinctCount(c, r) > 0) push(l, r, c, 0);
242
- return packets;
243
- }
244
- if (order === "PCRL") {
245
- for (let c = 0; c < tile.components.length; c++) for (let r = 0; r < maxResolutions; r++) for (let l = 0; l < layers; l++) if (precinctCount(c, r) > 0) push(l, r, c, 0);
246
+ require_image_jpeg2000_dwt.times(maxResolutions, (r) => {
247
+ for (const [c] of tile.components.entries()) for (let l = 0; l < layers; l++) if (precinctCount(c, r) > 0) push(l, r, c, 0);
248
+ });
246
249
  return packets;
247
250
  }
248
- for (let c = 0; c < tile.components.length; c++) for (let r = 0; r < maxResolutions; r++) for (let l = 0; l < layers; l++) if (precinctCount(c, r) > 0) push(l, r, c, 0);
251
+ for (const [c] of tile.components.entries()) require_image_jpeg2000_dwt.times(maxResolutions, (r) => {
252
+ for (let l = 0; l < layers; l++) if (precinctCount(c, r) > 0) push(l, r, c, 0);
253
+ });
249
254
  return packets;
250
255
  }
251
256
  const MARKER_SOP_HIGH = 255;
@@ -255,7 +260,7 @@ const SOP_SEGMENT_BYTES = 6;
255
260
  const EPH_MARKER_BYTES = 2;
256
261
  function readTilePackets(data, start, end, tile, sequence, options) {
257
262
  let position = start;
258
- for (let packetIndex = 0; packetIndex < sequence.length; packetIndex++) {
263
+ for (let packetIndex = 0; packetIndex !== sequence.length; packetIndex++) {
259
264
  const packet = sequence[packetIndex];
260
265
  if (packet === void 0) break;
261
266
  if (position >= end) {
@@ -263,9 +268,8 @@ function readTilePackets(data, start, end, tile, sequence, options) {
263
268
  return position;
264
269
  }
265
270
  if (options.useSop && position + SOP_SEGMENT_BYTES <= end && data[position] === MARKER_SOP_HIGH && data[position + 1] === MARKER_SOP_LOW) position += SOP_SEGMENT_BYTES;
266
- const component = tile.components[packet.component];
267
- const resolution = component?.resolutions[packet.resolution];
268
- if (component === void 0 || resolution === void 0) throw new require_image_jpeg2000_errors.Jpeg2000ParseError("the progression sequence names a resolution level the tile does not have");
271
+ const resolution = tile.components[packet.component]?.resolutions[packet.resolution];
272
+ if (resolution === void 0) throw new require_image_jpeg2000_errors.Jpeg2000ParseError("the progression sequence names a resolution level the tile does not have");
269
273
  const reader = new require_image_jpeg2000_tagtree.PacketBitReader(data, position, end);
270
274
  const included = [];
271
275
  const lengths = [];
@@ -274,12 +278,10 @@ function readTilePackets(data, start, end, tile, sequence, options) {
274
278
  if (precinct === void 0) continue;
275
279
  for (const block of precinct.codeBlocks) {
276
280
  if (!(block.included ? reader.readBit() === 1 : precinct.inclusion.decode(reader, block.gridX, block.gridY, packet.layer + 1))) continue;
277
- if (!block.included) {
278
- block.included = true;
279
- let threshold = 1;
280
- while (!precinct.zeroBitPlaneTree.decode(reader, block.gridX, block.gridY, threshold)) threshold++;
281
- block.zeroBitPlanes = threshold - 1;
282
- }
281
+ block.included = true;
282
+ let threshold = 1;
283
+ while (!precinct.zeroBitPlaneTree.decode(reader, block.gridX, block.gridY, threshold)) threshold++;
284
+ block.zeroBitPlanes = threshold - 1;
283
285
  const passes = readCodingPasses(reader);
284
286
  while (reader.readBit() === 1) block.lblock++;
285
287
  const lengthBits = block.lblock + Math.floor(Math.log2(passes));
@@ -292,10 +294,8 @@ function readTilePackets(data, start, end, tile, sequence, options) {
292
294
  reader.alignToByte();
293
295
  position = reader.offset;
294
296
  if (options.useEph && position + EPH_MARKER_BYTES <= end && data[position] === MARKER_SOP_HIGH && data[position + 1] === MARKER_EPH_LOW) position += EPH_MARKER_BYTES;
295
- for (let i = 0; i < included.length; i++) {
296
- const block = included[i];
297
+ for (const [i, block] of included.entries()) {
297
298
  const length = lengths[i] ?? 0;
298
- if (block === void 0) continue;
299
299
  const chunkEnd = Math.min(position + length, end);
300
300
  if (chunkEnd < position + length) options.onWarning("a code-block declares more coded bytes than the tile carries; decoding it from the bytes that did arrive");
301
301
  block.chunks.push({
@@ -76,4 +76,4 @@ interface PacketReadOptions {
76
76
  }
77
77
  declare function readTilePackets(data: Uint8Array<ArrayBuffer>, start: number, end: number, tile: Jpeg2000TileGeometry, sequence: readonly PacketPosition[], options: PacketReadOptions): number;
78
78
  //#endregion
79
- export { Jpeg2000CodeBlock, Jpeg2000Precinct, Jpeg2000Resolution, Jpeg2000Subband, Jpeg2000TileComponent, Jpeg2000TileGeometry, PacketReadOptions, buildPacketSequence, buildTileGeometry, readTilePackets, subbandGainLog2, tileHasSubdividedPrecincts };
79
+ export { Jpeg2000CodeBlock, Jpeg2000Precinct, Jpeg2000Resolution, Jpeg2000Subband, Jpeg2000TileComponent, Jpeg2000TileGeometry, PacketPosition, PacketReadOptions, buildPacketSequence, buildTileGeometry, readTilePackets, subbandGainLog2, tileHasSubdividedPrecincts };
@@ -76,4 +76,4 @@ interface PacketReadOptions {
76
76
  }
77
77
  declare function readTilePackets(data: Uint8Array<ArrayBuffer>, start: number, end: number, tile: Jpeg2000TileGeometry, sequence: readonly PacketPosition[], options: PacketReadOptions): number;
78
78
  //#endregion
79
- export { Jpeg2000CodeBlock, Jpeg2000Precinct, Jpeg2000Resolution, Jpeg2000Subband, Jpeg2000TileComponent, Jpeg2000TileGeometry, PacketReadOptions, buildPacketSequence, buildTileGeometry, readTilePackets, subbandGainLog2, tileHasSubdividedPrecincts };
79
+ export { Jpeg2000CodeBlock, Jpeg2000Precinct, Jpeg2000Resolution, Jpeg2000Subband, Jpeg2000TileComponent, Jpeg2000TileGeometry, PacketPosition, PacketReadOptions, buildPacketSequence, buildTileGeometry, readTilePackets, subbandGainLog2, tileHasSubdividedPrecincts };
@@ -1,11 +1,7 @@
1
1
  import { Jpeg2000ParseError, Jpeg2000UnsupportedError } from "./jpeg2000-errors.js";
2
+ import { times } from "./jpeg2000-dwt.js";
2
3
  import { PacketBitReader, TagTree } from "./jpeg2000-tagtree.js";
3
4
  //#region src/image/jpeg2000-t2.ts
4
- const HIGHER_RESOLUTION_BANDS = [
5
- "HL",
6
- "LH",
7
- "HH"
8
- ];
9
5
  const SUBBAND_GAIN_LOG2 = {
10
6
  LL: 0,
11
7
  HL: 1,
@@ -49,10 +45,9 @@ function precinctGrid(bounds, ppx, ppy) {
49
45
  }
50
46
  function tileHasSubdividedPrecincts(siz, tileX, tileY, codingPerComponent) {
51
47
  const tile = tileBounds(siz, tileX, tileY);
52
- for (let c = 0; c < siz.components.length; c++) {
53
- const size = siz.components[c];
48
+ for (const [c, size] of siz.components.entries()) {
54
49
  const coding = codingPerComponent[c];
55
- if (size === void 0 || coding === void 0) continue;
50
+ if (coding === void 0) continue;
56
51
  const component = componentBounds(tile, size.dx, size.dy);
57
52
  for (let r = 0; r <= coding.decompositionLevels; r++) {
58
53
  const precinctSize = coding.precinctSizes[r];
@@ -64,7 +59,6 @@ function tileHasSubdividedPrecincts(siz, tileX, tileY, codingPerComponent) {
64
59
  return false;
65
60
  }
66
61
  function bandCoordinate(componentCoordinate, levels, orientation) {
67
- if (levels === 0) return componentCoordinate;
68
62
  const half = 2 ** (levels - 1);
69
63
  return Math.ceil((componentCoordinate - half * orientation) / (2 * half));
70
64
  }
@@ -136,6 +130,11 @@ function buildTileGeometry(siz, tileX, tileY, codingPerComponent, quantizationPe
136
130
  if (componentSize === void 0 || coding === void 0 || quantization === void 0) throw new Jpeg2000ParseError(`no coding style or quantization is defined for component ${String(c)}`);
137
131
  const bounds = componentBounds(tile, componentSize.dx, componentSize.dy);
138
132
  const levels = coding.decompositionLevels;
133
+ const higherResolutionBands = [
134
+ "HL",
135
+ "LH",
136
+ "HH"
137
+ ];
139
138
  const resolutions = [];
140
139
  for (let r = 0; r <= levels; r++) {
141
140
  const level = resolutionBounds(bounds, levels, r);
@@ -147,7 +146,7 @@ function buildTileGeometry(siz, tileX, tileY, codingPerComponent, quantizationPe
147
146
  const { wide: precinctsWide, high: precinctsHigh } = precinctGrid(level, ppx, ppy);
148
147
  const bandPpx = r === 0 ? ppx : ppx - 1;
149
148
  const bandPpy = r === 0 ? ppy : ppy - 1;
150
- const bandTypes = r === 0 ? ["LL"] : HIGHER_RESOLUTION_BANDS;
149
+ const bandTypes = r === 0 ? ["LL"] : higherResolutionBands;
151
150
  const subbands = [];
152
151
  for (let b = 0; b < bandTypes.length; b++) {
153
152
  const type = bandTypes[b] ?? "LL";
@@ -228,23 +227,29 @@ function buildPacketSequence(tile, order, layers) {
228
227
  });
229
228
  };
230
229
  if (order === "LRCP") {
231
- for (let l = 0; l < layers; l++) for (let r = 0; r < maxResolutions; r++) for (let c = 0; c < tile.components.length; c++) for (let p = 0; p < precinctCount(c, r); p++) push(l, r, c, p);
230
+ for (let l = 0; l < layers; l++) times(maxResolutions, (r) => {
231
+ for (const [c] of tile.components.entries()) for (let p = 0; p < precinctCount(c, r); p++) push(l, r, c, p);
232
+ });
232
233
  return packets;
233
234
  }
234
235
  if (order === "RLCP") {
235
- for (let r = 0; r < maxResolutions; r++) for (let l = 0; l < layers; l++) for (let c = 0; c < tile.components.length; c++) for (let p = 0; p < precinctCount(c, r); p++) push(l, r, c, p);
236
+ times(maxResolutions, (r) => {
237
+ for (let l = 0; l < layers; l++) for (const [c] of tile.components.entries()) for (let p = 0; p < precinctCount(c, r); p++) push(l, r, c, p);
238
+ });
236
239
  return packets;
237
240
  }
238
- for (let c = 0; c < tile.components.length; c++) for (let r = 0; r < maxResolutions; r++) if (precinctCount(c, r) > 1) throw new Jpeg2000UnsupportedError(`progression order ${order} is only decoded here when every resolution level holds a single precinct, and this codestream subdivides at least one of them`);
241
+ for (const [c] of tile.components.entries()) times(maxResolutions, (r) => {
242
+ if (precinctCount(c, r) > 1) throw new Jpeg2000UnsupportedError(`progression order ${order} is only decoded here when every resolution level holds a single precinct, and this codestream subdivides at least one of them`);
243
+ });
239
244
  if (order === "RPCL") {
240
- for (let r = 0; r < maxResolutions; r++) for (let c = 0; c < tile.components.length; c++) for (let l = 0; l < layers; l++) if (precinctCount(c, r) > 0) push(l, r, c, 0);
241
- return packets;
242
- }
243
- if (order === "PCRL") {
244
- for (let c = 0; c < tile.components.length; c++) for (let r = 0; r < maxResolutions; r++) for (let l = 0; l < layers; l++) if (precinctCount(c, r) > 0) push(l, r, c, 0);
245
+ times(maxResolutions, (r) => {
246
+ for (const [c] of tile.components.entries()) for (let l = 0; l < layers; l++) if (precinctCount(c, r) > 0) push(l, r, c, 0);
247
+ });
245
248
  return packets;
246
249
  }
247
- for (let c = 0; c < tile.components.length; c++) for (let r = 0; r < maxResolutions; r++) for (let l = 0; l < layers; l++) if (precinctCount(c, r) > 0) push(l, r, c, 0);
250
+ for (const [c] of tile.components.entries()) times(maxResolutions, (r) => {
251
+ for (let l = 0; l < layers; l++) if (precinctCount(c, r) > 0) push(l, r, c, 0);
252
+ });
248
253
  return packets;
249
254
  }
250
255
  const MARKER_SOP_HIGH = 255;
@@ -254,7 +259,7 @@ const SOP_SEGMENT_BYTES = 6;
254
259
  const EPH_MARKER_BYTES = 2;
255
260
  function readTilePackets(data, start, end, tile, sequence, options) {
256
261
  let position = start;
257
- for (let packetIndex = 0; packetIndex < sequence.length; packetIndex++) {
262
+ for (let packetIndex = 0; packetIndex !== sequence.length; packetIndex++) {
258
263
  const packet = sequence[packetIndex];
259
264
  if (packet === void 0) break;
260
265
  if (position >= end) {
@@ -262,9 +267,8 @@ function readTilePackets(data, start, end, tile, sequence, options) {
262
267
  return position;
263
268
  }
264
269
  if (options.useSop && position + SOP_SEGMENT_BYTES <= end && data[position] === MARKER_SOP_HIGH && data[position + 1] === MARKER_SOP_LOW) position += SOP_SEGMENT_BYTES;
265
- const component = tile.components[packet.component];
266
- const resolution = component?.resolutions[packet.resolution];
267
- if (component === void 0 || resolution === void 0) throw new Jpeg2000ParseError("the progression sequence names a resolution level the tile does not have");
270
+ const resolution = tile.components[packet.component]?.resolutions[packet.resolution];
271
+ if (resolution === void 0) throw new Jpeg2000ParseError("the progression sequence names a resolution level the tile does not have");
268
272
  const reader = new PacketBitReader(data, position, end);
269
273
  const included = [];
270
274
  const lengths = [];
@@ -273,12 +277,10 @@ function readTilePackets(data, start, end, tile, sequence, options) {
273
277
  if (precinct === void 0) continue;
274
278
  for (const block of precinct.codeBlocks) {
275
279
  if (!(block.included ? reader.readBit() === 1 : precinct.inclusion.decode(reader, block.gridX, block.gridY, packet.layer + 1))) continue;
276
- if (!block.included) {
277
- block.included = true;
278
- let threshold = 1;
279
- while (!precinct.zeroBitPlaneTree.decode(reader, block.gridX, block.gridY, threshold)) threshold++;
280
- block.zeroBitPlanes = threshold - 1;
281
- }
280
+ block.included = true;
281
+ let threshold = 1;
282
+ while (!precinct.zeroBitPlaneTree.decode(reader, block.gridX, block.gridY, threshold)) threshold++;
283
+ block.zeroBitPlanes = threshold - 1;
282
284
  const passes = readCodingPasses(reader);
283
285
  while (reader.readBit() === 1) block.lblock++;
284
286
  const lengthBits = block.lblock + Math.floor(Math.log2(passes));
@@ -291,10 +293,8 @@ function readTilePackets(data, start, end, tile, sequence, options) {
291
293
  reader.alignToByte();
292
294
  position = reader.offset;
293
295
  if (options.useEph && position + EPH_MARKER_BYTES <= end && data[position] === MARKER_SOP_HIGH && data[position + 1] === MARKER_EPH_LOW) position += EPH_MARKER_BYTES;
294
- for (let i = 0; i < included.length; i++) {
295
- const block = included[i];
296
+ for (const [i, block] of included.entries()) {
296
297
  const length = lengths[i] ?? 0;
297
- if (block === void 0) continue;
298
298
  const chunkEnd = Math.min(position + length, end);
299
299
  if (chunkEnd < position + length) options.onWarning("a code-block declares more coded bytes than the tile carries; decoding it from the bytes that did arrive");
300
300
  block.chunks.push({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdf-parser.js",
3
- "version": "5.2.13",
3
+ "version": "5.2.14",
4
4
  "description": "Hand-written, dependency-minimal PDF codec: parses arbitrary real-world PDFs and generates new ones, built on its own codec-owned LayoutDocument item model and Zod 4 codecs.",
5
5
  "type": "module",
6
6
  "repository": {