@loaders.gl/textures 4.2.0-alpha.4 → 4.2.0-alpha.6

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 (110) hide show
  1. package/dist/basis-loader.js +27 -19
  2. package/dist/basis-worker-node.js +183 -127
  3. package/dist/basis-worker.js +5 -3
  4. package/dist/compressed-texture-loader.js +51 -31
  5. package/dist/compressed-texture-worker.js +5 -3
  6. package/dist/compressed-texture-writer.js +32 -19
  7. package/dist/crunch-loader.js +19 -13
  8. package/dist/crunch-worker.js +5 -3
  9. package/dist/dist.dev.js +326 -212
  10. package/dist/dist.min.js +10 -0
  11. package/dist/index.cjs +41 -60
  12. package/dist/index.cjs.map +7 -0
  13. package/dist/index.d.ts +16 -16
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +21 -13
  16. package/dist/ktx2-basis-writer-worker-node.js +183 -127
  17. package/dist/ktx2-basis-writer-worker.js +5 -3
  18. package/dist/ktx2-basis-writer.js +21 -15
  19. package/dist/lib/encoders/encode-ktx.js +6 -3
  20. package/dist/lib/encoders/encode-ktx2-basis-texture.js +38 -33
  21. package/dist/lib/encoders/encode-texture.d.ts +1 -1
  22. package/dist/lib/encoders/encode-texture.d.ts.map +1 -1
  23. package/dist/lib/encoders/encode-texture.js +23 -9
  24. package/dist/lib/gl-extensions.js +70 -59
  25. package/dist/lib/parsers/basis-module-loader.js +97 -61
  26. package/dist/lib/parsers/crunch-module-loader.js +29 -13
  27. package/dist/lib/parsers/parse-basis.js +217 -205
  28. package/dist/lib/parsers/parse-compressed-texture.js +20 -11
  29. package/dist/lib/parsers/parse-crunch.js +89 -59
  30. package/dist/lib/parsers/parse-dds.js +82 -53
  31. package/dist/lib/parsers/parse-ktx.js +37 -18
  32. package/dist/lib/parsers/parse-npy.js +70 -53
  33. package/dist/lib/parsers/parse-pvr.js +203 -112
  34. package/dist/lib/texture-api/async-deep-map.js +33 -28
  35. package/dist/lib/texture-api/deep-load.js +7 -5
  36. package/dist/lib/texture-api/generate-url.d.ts +1 -1
  37. package/dist/lib/texture-api/generate-url.d.ts.map +1 -1
  38. package/dist/lib/texture-api/generate-url.js +12 -10
  39. package/dist/lib/texture-api/load-image-array.d.ts +1 -1
  40. package/dist/lib/texture-api/load-image-array.d.ts.map +1 -1
  41. package/dist/lib/texture-api/load-image-array.js +13 -15
  42. package/dist/lib/texture-api/load-image-cube.d.ts +1 -1
  43. package/dist/lib/texture-api/load-image-cube.d.ts.map +1 -1
  44. package/dist/lib/texture-api/load-image-cube.js +32 -51
  45. package/dist/lib/texture-api/load-image.d.ts +1 -1
  46. package/dist/lib/texture-api/load-image.d.ts.map +1 -1
  47. package/dist/lib/texture-api/load-image.js +30 -36
  48. package/dist/lib/texture-api/texture-api-types.js +3 -1
  49. package/dist/lib/utils/extract-mipmap-images.js +43 -29
  50. package/dist/lib/utils/ktx-format-helper.js +119 -68
  51. package/dist/lib/utils/texture-formats.js +40 -26
  52. package/dist/lib/utils/version.js +7 -2
  53. package/dist/npy-loader.d.ts +1 -1
  54. package/dist/npy-loader.d.ts.map +1 -1
  55. package/dist/npy-loader.js +24 -15
  56. package/dist/npy-worker.js +4 -2
  57. package/dist/workers/basis-worker-node.js +4 -1
  58. package/dist/workers/basis-worker.js +3 -1
  59. package/dist/workers/compressed-texture-worker.js +4 -1
  60. package/dist/workers/crunch-worker.d.ts +1 -1
  61. package/dist/workers/crunch-worker.d.ts.map +1 -1
  62. package/dist/workers/crunch-worker.js +8 -3
  63. package/dist/workers/ktx2-basis-writer-worker-node.js +23 -26
  64. package/dist/workers/ktx2-basis-writer-worker.js +22 -26
  65. package/dist/workers/npy-worker.js +3 -1
  66. package/package.json +12 -10
  67. package/dist/basis-loader.js.map +0 -1
  68. package/dist/compressed-texture-loader.js.map +0 -1
  69. package/dist/compressed-texture-writer.js.map +0 -1
  70. package/dist/crunch-loader.js.map +0 -1
  71. package/dist/index.js.map +0 -1
  72. package/dist/ktx2-basis-writer.js.map +0 -1
  73. package/dist/lib/encoders/encode-ktx.js.map +0 -1
  74. package/dist/lib/encoders/encode-ktx2-basis-texture.js.map +0 -1
  75. package/dist/lib/encoders/encode-texture.js.map +0 -1
  76. package/dist/lib/gl-extensions.js.map +0 -1
  77. package/dist/lib/parsers/basis-module-loader.js.map +0 -1
  78. package/dist/lib/parsers/crunch-module-loader.js.map +0 -1
  79. package/dist/lib/parsers/parse-basis.js.map +0 -1
  80. package/dist/lib/parsers/parse-compressed-texture.js.map +0 -1
  81. package/dist/lib/parsers/parse-crunch.js.map +0 -1
  82. package/dist/lib/parsers/parse-dds.js.map +0 -1
  83. package/dist/lib/parsers/parse-ktx.js.map +0 -1
  84. package/dist/lib/parsers/parse-npy.js.map +0 -1
  85. package/dist/lib/parsers/parse-pvr.js.map +0 -1
  86. package/dist/lib/texture-api/async-deep-map.js.map +0 -1
  87. package/dist/lib/texture-api/deep-load.js.map +0 -1
  88. package/dist/lib/texture-api/generate-url.js.map +0 -1
  89. package/dist/lib/texture-api/load-image-array.js.map +0 -1
  90. package/dist/lib/texture-api/load-image-cube.js.map +0 -1
  91. package/dist/lib/texture-api/load-image.js.map +0 -1
  92. package/dist/lib/texture-api/texture-api-types.js.map +0 -1
  93. package/dist/lib/utils/extract-mipmap-images.js.map +0 -1
  94. package/dist/lib/utils/ktx-format-helper.js.map +0 -1
  95. package/dist/lib/utils/texture-formats.js.map +0 -1
  96. package/dist/lib/utils/version.js.map +0 -1
  97. package/dist/libs/libs/README.md +0 -9
  98. package/dist/libs/libs/basis_encoder.js +0 -21
  99. package/dist/libs/libs/basis_encoder.wasm +0 -0
  100. package/dist/libs/libs/basis_transcoder.js +0 -22
  101. package/dist/libs/libs/basis_transcoder.wasm +0 -0
  102. package/dist/libs/libs/crunch.js +0 -136
  103. package/dist/npy-loader.js.map +0 -1
  104. package/dist/workers/basis-worker-node.js.map +0 -1
  105. package/dist/workers/basis-worker.js.map +0 -1
  106. package/dist/workers/compressed-texture-worker.js.map +0 -1
  107. package/dist/workers/crunch-worker.js.map +0 -1
  108. package/dist/workers/ktx2-basis-writer-worker-node.js.map +0 -1
  109. package/dist/workers/ktx2-basis-writer-worker.js.map +0 -1
  110. package/dist/workers/npy-worker.js.map +0 -1
@@ -1,75 +1,104 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { assert } from '@loaders.gl/loader-utils';
2
5
  import { GL_EXTENSIONS_CONSTANTS } from "../gl-extensions.js";
3
6
  import { extractMipmapImages } from "../utils/extract-mipmap-images.js";
4
7
  const DDS_CONSTANTS = {
5
- MAGIC_NUMBER: 0x20534444,
6
- HEADER_LENGTH: 31,
7
- MAGIC_NUMBER_INDEX: 0,
8
- HEADER_SIZE_INDEX: 1,
9
- HEADER_FLAGS_INDEX: 2,
10
- HEADER_HEIGHT_INDEX: 3,
11
- HEADER_WIDTH_INDEX: 4,
12
- MIPMAPCOUNT_INDEX: 7,
13
- HEADER_PF_FLAGS_INDEX: 20,
14
- HEADER_PF_FOURCC_INDEX: 21,
15
- DDSD_MIPMAPCOUNT: 0x20000,
16
- DDPF_FOURCC: 0x4
8
+ MAGIC_NUMBER: 0x20534444,
9
+ HEADER_LENGTH: 31,
10
+ MAGIC_NUMBER_INDEX: 0,
11
+ HEADER_SIZE_INDEX: 1,
12
+ HEADER_FLAGS_INDEX: 2,
13
+ HEADER_HEIGHT_INDEX: 3,
14
+ HEADER_WIDTH_INDEX: 4,
15
+ MIPMAPCOUNT_INDEX: 7,
16
+ HEADER_PF_FLAGS_INDEX: 20,
17
+ HEADER_PF_FOURCC_INDEX: 21,
18
+ DDSD_MIPMAPCOUNT: 0x20000,
19
+ DDPF_FOURCC: 0x4
17
20
  };
18
21
  const DDS_PIXEL_FORMATS = {
19
- DXT1: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGB_S3TC_DXT1_EXT,
20
- DXT3: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_S3TC_DXT3_EXT,
21
- DXT5: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_S3TC_DXT5_EXT,
22
- 'ATC ': GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGB_ATC_WEBGL,
23
- ATCA: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_ATC_EXPLICIT_ALPHA_WEBGL,
24
- ATCI: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_ATC_INTERPOLATED_ALPHA_WEBGL
22
+ DXT1: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGB_S3TC_DXT1_EXT,
23
+ DXT3: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_S3TC_DXT3_EXT,
24
+ DXT5: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_S3TC_DXT5_EXT,
25
+ 'ATC ': GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGB_ATC_WEBGL,
26
+ ATCA: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_ATC_EXPLICIT_ALPHA_WEBGL,
27
+ ATCI: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_ATC_INTERPOLATED_ALPHA_WEBGL
25
28
  };
26
29
  const getATCLevelSize = getDxt1LevelSize;
27
30
  const getATCALevelSize = getDxtXLevelSize;
28
31
  const getATCILevelSize = getDxtXLevelSize;
29
32
  const DDS_SIZE_FUNCTIONS = {
30
- DXT1: getDxt1LevelSize,
31
- DXT3: getDxtXLevelSize,
32
- DXT5: getDxtXLevelSize,
33
- 'ATC ': getATCLevelSize,
34
- ATCA: getATCALevelSize,
35
- ATCI: getATCILevelSize
33
+ DXT1: getDxt1LevelSize,
34
+ DXT3: getDxtXLevelSize,
35
+ DXT5: getDxtXLevelSize,
36
+ 'ATC ': getATCLevelSize,
37
+ ATCA: getATCALevelSize,
38
+ ATCI: getATCILevelSize
36
39
  };
40
+ /**
41
+ * Check if data is in "DDS" format by its magic number
42
+ * @param data - binary data of compressed texture
43
+ * @returns true - data in "DDS" format, else - false
44
+ */
37
45
  export function isDDS(data) {
38
- const header = new Uint32Array(data, 0, DDS_CONSTANTS.HEADER_LENGTH);
39
- const magic = header[DDS_CONSTANTS.MAGIC_NUMBER_INDEX];
40
- return magic === DDS_CONSTANTS.MAGIC_NUMBER;
46
+ const header = new Uint32Array(data, 0, DDS_CONSTANTS.HEADER_LENGTH);
47
+ const magic = header[DDS_CONSTANTS.MAGIC_NUMBER_INDEX];
48
+ return magic === DDS_CONSTANTS.MAGIC_NUMBER;
41
49
  }
50
+ /**
51
+ * Parse texture data as "DDS" format
52
+ * @param data - binary data of compressed texture
53
+ * @returns Array of the texture levels
54
+ */
42
55
  export function parseDDS(data) {
43
- const header = new Int32Array(data, 0, DDS_CONSTANTS.HEADER_LENGTH);
44
- const pixelFormatNumber = header[DDS_CONSTANTS.HEADER_PF_FOURCC_INDEX];
45
- assert(Boolean(header[DDS_CONSTANTS.HEADER_PF_FLAGS_INDEX] & DDS_CONSTANTS.DDPF_FOURCC), 'DDS: Unsupported format, must contain a FourCC code');
46
- const fourCC = int32ToFourCC(pixelFormatNumber);
47
- const internalFormat = DDS_PIXEL_FORMATS[fourCC];
48
- const sizeFunction = DDS_SIZE_FUNCTIONS[fourCC];
49
- assert(internalFormat && sizeFunction, `DDS: Unknown pixel format ${pixelFormatNumber}`);
50
- let mipMapLevels = 1;
51
- if (header[DDS_CONSTANTS.HEADER_FLAGS_INDEX] & DDS_CONSTANTS.DDSD_MIPMAPCOUNT) {
52
- mipMapLevels = Math.max(1, header[DDS_CONSTANTS.MIPMAPCOUNT_INDEX]);
53
- }
54
- const width = header[DDS_CONSTANTS.HEADER_WIDTH_INDEX];
55
- const height = header[DDS_CONSTANTS.HEADER_HEIGHT_INDEX];
56
- const dataOffset = header[DDS_CONSTANTS.HEADER_SIZE_INDEX] + 4;
57
- const image = new Uint8Array(data, dataOffset);
58
- return extractMipmapImages(image, {
59
- mipMapLevels,
60
- width,
61
- height,
62
- sizeFunction,
63
- internalFormat
64
- });
56
+ const header = new Int32Array(data, 0, DDS_CONSTANTS.HEADER_LENGTH);
57
+ const pixelFormatNumber = header[DDS_CONSTANTS.HEADER_PF_FOURCC_INDEX];
58
+ assert(Boolean(header[DDS_CONSTANTS.HEADER_PF_FLAGS_INDEX] & DDS_CONSTANTS.DDPF_FOURCC), 'DDS: Unsupported format, must contain a FourCC code');
59
+ const fourCC = int32ToFourCC(pixelFormatNumber);
60
+ const internalFormat = DDS_PIXEL_FORMATS[fourCC];
61
+ const sizeFunction = DDS_SIZE_FUNCTIONS[fourCC];
62
+ assert(internalFormat && sizeFunction, `DDS: Unknown pixel format ${pixelFormatNumber}`);
63
+ let mipMapLevels = 1;
64
+ if (header[DDS_CONSTANTS.HEADER_FLAGS_INDEX] & DDS_CONSTANTS.DDSD_MIPMAPCOUNT) {
65
+ mipMapLevels = Math.max(1, header[DDS_CONSTANTS.MIPMAPCOUNT_INDEX]);
66
+ }
67
+ const width = header[DDS_CONSTANTS.HEADER_WIDTH_INDEX];
68
+ const height = header[DDS_CONSTANTS.HEADER_HEIGHT_INDEX];
69
+ const dataOffset = header[DDS_CONSTANTS.HEADER_SIZE_INDEX] + 4;
70
+ const image = new Uint8Array(data, dataOffset);
71
+ return extractMipmapImages(image, {
72
+ mipMapLevels,
73
+ width,
74
+ height,
75
+ sizeFunction,
76
+ internalFormat
77
+ });
65
78
  }
79
+ /**
80
+ * DXT1 applicable function to calculate level size
81
+ * @param width - level width
82
+ * @param height - level height
83
+ * @returns level size in bytes
84
+ */
66
85
  export function getDxt1LevelSize(width, height) {
67
- return (width + 3 >> 2) * (height + 3 >> 2) * 8;
86
+ return ((width + 3) >> 2) * ((height + 3) >> 2) * 8;
68
87
  }
88
+ /**
89
+ * DXT3 & DXT5 applicable function to calculate level size
90
+ * @param width - level width
91
+ * @param height - level height
92
+ * @returns level size in bytes
93
+ */
69
94
  export function getDxtXLevelSize(width, height) {
70
- return (width + 3 >> 2) * (height + 3 >> 2) * 16;
95
+ return ((width + 3) >> 2) * ((height + 3) >> 2) * 16;
71
96
  }
97
+ /**
98
+ * Convert every byte of Int32 value to char
99
+ * @param value - Int32 number
100
+ * @returns string of 4 characters
101
+ */
72
102
  function int32ToFourCC(value) {
73
- return String.fromCharCode(value & 0xff, value >> 8 & 0xff, value >> 16 & 0xff, value >> 24 & 0xff);
103
+ return String.fromCharCode(value & 0xff, (value >> 8) & 0xff, (value >> 16) & 0xff, (value >> 24) & 0xff);
74
104
  }
75
- //# sourceMappingURL=parse-dds.js.map
@@ -1,25 +1,44 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  import { read } from 'ktx-parse';
2
5
  import { extractMipmapImages } from "../utils/extract-mipmap-images.js";
3
6
  import { mapVkFormatToWebGL } from "../utils/ktx-format-helper.js";
4
- const KTX2_ID = [0xab, 0x4b, 0x54, 0x58, 0x20, 0x32, 0x30, 0xbb, 0x0d, 0x0a, 0x1a, 0x0a];
7
+ const KTX2_ID = [
8
+ // '´', 'K', 'T', 'X', '2', '0', 'ª', '\r', '\n', '\x1A', '\n'
9
+ 0xab, 0x4b, 0x54, 0x58, 0x20, 0x32, 0x30, 0xbb, 0x0d, 0x0a, 0x1a, 0x0a
10
+ ];
11
+ // eslint-disable-next-line complexity
5
12
  export function isKTX(data) {
6
- const id = new Uint8Array(data);
7
- const notKTX = id.byteLength < KTX2_ID.length || id[0] !== KTX2_ID[0] || id[1] !== KTX2_ID[1] || id[2] !== KTX2_ID[2] || id[3] !== KTX2_ID[3] || id[4] !== KTX2_ID[4] || id[5] !== KTX2_ID[5] || id[6] !== KTX2_ID[6] || id[7] !== KTX2_ID[7] || id[8] !== KTX2_ID[8] || id[9] !== KTX2_ID[9] || id[10] !== KTX2_ID[10] || id[11] !== KTX2_ID[11];
8
- return !notKTX;
13
+ // const id = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
14
+ const id = new Uint8Array(data);
15
+ const notKTX = id.byteLength < KTX2_ID.length ||
16
+ id[0] !== KTX2_ID[0] || // '´'
17
+ id[1] !== KTX2_ID[1] || // 'K'
18
+ id[2] !== KTX2_ID[2] || // 'T'
19
+ id[3] !== KTX2_ID[3] || // 'X'
20
+ id[4] !== KTX2_ID[4] || // ' '
21
+ id[5] !== KTX2_ID[5] || // '2'
22
+ id[6] !== KTX2_ID[6] || // '0'
23
+ id[7] !== KTX2_ID[7] || // 'ª'
24
+ id[8] !== KTX2_ID[8] || // '\r'
25
+ id[9] !== KTX2_ID[9] || // '\n'
26
+ id[10] !== KTX2_ID[10] || // '\x1A'
27
+ id[11] !== KTX2_ID[11]; // '\n'
28
+ return !notKTX;
9
29
  }
10
30
  export function parseKTX(arrayBuffer) {
11
- const uint8Array = new Uint8Array(arrayBuffer);
12
- const ktx = read(uint8Array);
13
- const mipMapLevels = Math.max(1, ktx.levels.length);
14
- const width = ktx.pixelWidth;
15
- const height = ktx.pixelHeight;
16
- const internalFormat = mapVkFormatToWebGL(ktx.vkFormat);
17
- return extractMipmapImages(ktx.levels, {
18
- mipMapLevels,
19
- width,
20
- height,
21
- sizeFunction: level => level.uncompressedByteLength,
22
- internalFormat
23
- });
31
+ const uint8Array = new Uint8Array(arrayBuffer);
32
+ const ktx = read(uint8Array);
33
+ const mipMapLevels = Math.max(1, ktx.levels.length);
34
+ const width = ktx.pixelWidth;
35
+ const height = ktx.pixelHeight;
36
+ const internalFormat = mapVkFormatToWebGL(ktx.vkFormat);
37
+ return extractMipmapImages(ktx.levels, {
38
+ mipMapLevels,
39
+ width,
40
+ height,
41
+ sizeFunction: (level) => level.uncompressedByteLength,
42
+ internalFormat
43
+ });
24
44
  }
25
- //# sourceMappingURL=parse-ktx.js.map
@@ -1,63 +1,80 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
1
4
  const a = new Uint32Array([0x12345678]);
2
5
  const b = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
3
6
  const isLittleEndian = !(b[0] === 0x12);
4
7
  const LITTLE_ENDIAN_OS = isLittleEndian;
8
+ /**
9
+ * The basic string format consists of 3 characters:
10
+ * 1. a character describing the byteorder of the data (<: little-endian, >: big-endian, |: not-relevant)
11
+ * 2. a character code giving the basic type of the array
12
+ * 3. an integer providing the number of bytes the type uses.
13
+ * https://numpy.org/doc/stable/reference/arrays.interface.html
14
+ *
15
+ * Here I only include the second and third characters, and check endianness separately
16
+ */
5
17
  const DTYPES = {
6
- u1: Uint8Array,
7
- i1: Int8Array,
8
- u2: Uint16Array,
9
- i2: Int16Array,
10
- u4: Uint32Array,
11
- i4: Int32Array,
12
- f4: Float32Array,
13
- f8: Float64Array
18
+ u1: Uint8Array,
19
+ i1: Int8Array,
20
+ u2: Uint16Array,
21
+ i2: Int16Array,
22
+ u4: Uint32Array,
23
+ i4: Int32Array,
24
+ f4: Float32Array,
25
+ f8: Float64Array
14
26
  };
15
27
  export function parseNPY(arrayBuffer, options) {
16
- var _header$shape;
17
- const view = new DataView(arrayBuffer);
18
- const {
19
- header,
20
- headerEndOffset
21
- } = parseHeader(view);
22
- const numpyType = header.descr;
23
- const ArrayType = DTYPES[numpyType.slice(1, 3)];
24
- if (!ArrayType) {
25
- throw new Error(`Unimplemented type ${numpyType}`);
26
- }
27
- const nArrayElements = (_header$shape = header.shape) === null || _header$shape === void 0 ? void 0 : _header$shape.reduce((a, b) => a * b);
28
- const arrayByteLength = nArrayElements * ArrayType.BYTES_PER_ELEMENT;
29
- if (arrayBuffer.byteLength < headerEndOffset + arrayByteLength) {
30
- throw new Error('Buffer overflow');
31
- }
32
- const data = new ArrayType(arrayBuffer.slice(headerEndOffset, headerEndOffset + arrayByteLength));
33
- if (numpyType[0] === '>' && LITTLE_ENDIAN_OS || numpyType[0] === '<' && !LITTLE_ENDIAN_OS) {
34
- throw new Error('Incorrect endianness');
35
- }
36
- return {
37
- data,
38
- header
39
- };
28
+ const view = new DataView(arrayBuffer);
29
+ const { header, headerEndOffset } = parseHeader(view);
30
+ const numpyType = header.descr;
31
+ const ArrayType = DTYPES[numpyType.slice(1, 3)];
32
+ if (!ArrayType) {
33
+ throw new Error(`Unimplemented type ${numpyType}`);
34
+ }
35
+ const nArrayElements = header.shape?.reduce((a, b) => a * b);
36
+ const arrayByteLength = nArrayElements * ArrayType.BYTES_PER_ELEMENT;
37
+ if (arrayBuffer.byteLength < headerEndOffset + arrayByteLength) {
38
+ throw new Error('Buffer overflow');
39
+ }
40
+ const data = new ArrayType(arrayBuffer.slice(headerEndOffset, headerEndOffset + arrayByteLength));
41
+ // Swap endianness if needed
42
+ if ((numpyType[0] === '>' && LITTLE_ENDIAN_OS) || (numpyType[0] === '<' && !LITTLE_ENDIAN_OS)) {
43
+ throw new Error('Incorrect endianness');
44
+ }
45
+ return {
46
+ data,
47
+ header
48
+ };
40
49
  }
50
+ /**
51
+ * Parse NPY header
52
+ *
53
+ * @param view
54
+ * @return
55
+ */
41
56
  function parseHeader(view) {
42
- const majorVersion = view.getUint8(6);
43
- let offset = 8;
44
- let headerLength;
45
- if (majorVersion >= 2) {
46
- headerLength = view.getUint32(offset, true);
47
- offset += 4;
48
- } else {
49
- headerLength = view.getUint16(offset, true);
50
- offset += 2;
51
- }
52
- const encoding = majorVersion <= 2 ? 'latin1' : 'utf-8';
53
- const decoder = new TextDecoder(encoding);
54
- const headerArray = new Uint8Array(view.buffer, offset, headerLength);
55
- const headerText = decoder.decode(headerArray);
56
- offset += headerLength;
57
- const header = JSON.parse(headerText.replace(/'/g, '"').replace('False', 'false').replace('(', '[').replace(/,*\),*/g, ']'));
58
- return {
59
- header,
60
- headerEndOffset: offset
61
- };
57
+ const majorVersion = view.getUint8(6);
58
+ // const minorVersion = view.getUint8(7);
59
+ let offset = 8;
60
+ let headerLength;
61
+ if (majorVersion >= 2) {
62
+ headerLength = view.getUint32(offset, true);
63
+ offset += 4;
64
+ }
65
+ else {
66
+ headerLength = view.getUint16(offset, true);
67
+ offset += 2;
68
+ }
69
+ const encoding = majorVersion <= 2 ? 'latin1' : 'utf-8';
70
+ const decoder = new TextDecoder(encoding);
71
+ const headerArray = new Uint8Array(view.buffer, offset, headerLength);
72
+ const headerText = decoder.decode(headerArray);
73
+ offset += headerLength;
74
+ const header = JSON.parse(headerText
75
+ .replace(/'/g, '"')
76
+ .replace('False', 'false')
77
+ .replace('(', '[')
78
+ .replace(/,*\),*/g, ']'));
79
+ return { header, headerEndOffset: offset };
62
80
  }
63
- //# sourceMappingURL=parse-npy.js.map