@loaders.gl/textures 3.1.0-alpha.5 → 3.1.0-beta.5

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 (231) hide show
  1. package/dist/basis-loader.d.ts +1 -0
  2. package/dist/basis-loader.d.ts.map +1 -0
  3. package/dist/basis-loader.js +40 -0
  4. package/dist/basis-worker.js +675 -2
  5. package/dist/bundle.d.ts +1 -0
  6. package/dist/bundle.d.ts.map +1 -0
  7. package/dist/bundle.js +2107 -0
  8. package/dist/compressed-texture-loader.d.ts +1 -0
  9. package/dist/compressed-texture-loader.d.ts.map +1 -0
  10. package/dist/compressed-texture-loader.js +64 -0
  11. package/dist/compressed-texture-worker.js +1179 -2
  12. package/dist/compressed-texture-writer.d.ts +1 -0
  13. package/dist/compressed-texture-writer.d.ts.map +1 -0
  14. package/dist/compressed-texture-writer.js +28 -0
  15. package/dist/crunch-loader.d.ts +1 -0
  16. package/dist/crunch-loader.d.ts.map +1 -0
  17. package/dist/crunch-loader.js +25 -0
  18. package/dist/crunch-worker.js +486 -2
  19. package/dist/es5/index.js +3 -3
  20. package/dist/es5/index.js.map +1 -1
  21. package/dist/es5/lib/{gl-constants.js → gl-extensions.js} +4 -4
  22. package/dist/es5/lib/gl-extensions.js.map +1 -0
  23. package/dist/es5/lib/parsers/basis-module-loader.js +1 -1
  24. package/dist/es5/lib/parsers/basis-module-loader.js.map +1 -1
  25. package/dist/es5/lib/parsers/crunch-module-loader.js.map +1 -1
  26. package/dist/es5/lib/parsers/parse-basis.js +9 -9
  27. package/dist/es5/lib/parsers/parse-basis.js.map +1 -1
  28. package/dist/es5/lib/parsers/parse-crunch.js +4 -4
  29. package/dist/es5/lib/parsers/parse-crunch.js.map +1 -1
  30. package/dist/es5/lib/parsers/parse-dds.js +23 -23
  31. package/dist/es5/lib/parsers/parse-dds.js.map +1 -1
  32. package/dist/es5/lib/parsers/parse-ktx.js +2 -2
  33. package/dist/es5/lib/parsers/parse-ktx.js.map +1 -1
  34. package/dist/es5/lib/parsers/parse-npy.js +3 -1
  35. package/dist/es5/lib/parsers/parse-npy.js.map +1 -1
  36. package/dist/es5/lib/parsers/parse-pvr.js +28 -28
  37. package/dist/es5/lib/parsers/parse-pvr.js.map +1 -1
  38. package/dist/es5/lib/texture-api/async-deep-map.js.map +1 -1
  39. package/dist/es5/lib/texture-api/deep-load.js.map +1 -1
  40. package/dist/es5/lib/texture-api/generate-url.js +4 -12
  41. package/dist/es5/lib/texture-api/generate-url.js.map +1 -1
  42. package/dist/es5/lib/texture-api/load-image-array.js.map +1 -1
  43. package/dist/es5/lib/texture-api/load-image-cube.js.map +1 -1
  44. package/dist/es5/lib/texture-api/load-image.js +2 -5
  45. package/dist/es5/lib/texture-api/load-image.js.map +1 -1
  46. package/dist/es5/lib/texture-api/texture-api-types.js +2 -0
  47. package/dist/es5/lib/texture-api/texture-api-types.js.map +1 -0
  48. package/dist/es5/lib/utils/extract-mipmap-images.js.map +1 -1
  49. package/dist/es5/lib/utils/ktx-format-helper.js +67 -67
  50. package/dist/es5/lib/utils/ktx-format-helper.js.map +1 -1
  51. package/dist/es5/lib/utils/version.js +1 -1
  52. package/dist/es5/lib/utils/version.js.map +1 -1
  53. package/dist/es5/libs/README.md +2 -2
  54. package/dist/es5/libs/basis_encoder.js +1 -1
  55. package/dist/es5/libs/basis_transcoder.js +1 -1
  56. package/dist/es5/libs/crunch.js +1 -0
  57. package/dist/es5/npy-loader.js.map +1 -1
  58. package/dist/esm/index.js +1 -1
  59. package/dist/esm/index.js.map +1 -1
  60. package/dist/esm/lib/{gl-constants.js → gl-extensions.js} +2 -2
  61. package/dist/esm/lib/gl-extensions.js.map +1 -0
  62. package/dist/esm/lib/parsers/basis-module-loader.js +1 -1
  63. package/dist/esm/lib/parsers/basis-module-loader.js.map +1 -1
  64. package/dist/esm/lib/parsers/crunch-module-loader.js.map +1 -1
  65. package/dist/esm/lib/parsers/parse-basis.js +8 -8
  66. package/dist/esm/lib/parsers/parse-basis.js.map +1 -1
  67. package/dist/esm/lib/parsers/parse-crunch.js +4 -4
  68. package/dist/esm/lib/parsers/parse-crunch.js.map +1 -1
  69. package/dist/esm/lib/parsers/parse-dds.js +23 -23
  70. package/dist/esm/lib/parsers/parse-dds.js.map +1 -1
  71. package/dist/esm/lib/parsers/parse-ktx.js +2 -2
  72. package/dist/esm/lib/parsers/parse-ktx.js.map +1 -1
  73. package/dist/esm/lib/parsers/parse-npy.js +3 -1
  74. package/dist/esm/lib/parsers/parse-npy.js.map +1 -1
  75. package/dist/esm/lib/parsers/parse-pvr.js +28 -28
  76. package/dist/esm/lib/parsers/parse-pvr.js.map +1 -1
  77. package/dist/esm/lib/texture-api/async-deep-map.js.map +1 -1
  78. package/dist/esm/lib/texture-api/deep-load.js.map +1 -1
  79. package/dist/esm/lib/texture-api/generate-url.js +5 -13
  80. package/dist/esm/lib/texture-api/generate-url.js.map +1 -1
  81. package/dist/esm/lib/texture-api/load-image-array.js.map +1 -1
  82. package/dist/esm/lib/texture-api/load-image-cube.js.map +1 -1
  83. package/dist/esm/lib/texture-api/load-image.js +2 -5
  84. package/dist/esm/lib/texture-api/load-image.js.map +1 -1
  85. package/dist/esm/lib/texture-api/texture-api-types.js +2 -0
  86. package/dist/esm/lib/texture-api/texture-api-types.js.map +1 -0
  87. package/dist/esm/lib/utils/extract-mipmap-images.js.map +1 -1
  88. package/dist/esm/lib/utils/ktx-format-helper.js +67 -67
  89. package/dist/esm/lib/utils/ktx-format-helper.js.map +1 -1
  90. package/dist/esm/lib/utils/version.js +1 -1
  91. package/dist/esm/lib/utils/version.js.map +1 -1
  92. package/dist/esm/libs/README.md +2 -2
  93. package/dist/esm/libs/basis_encoder.js +1 -1
  94. package/dist/esm/libs/basis_transcoder.js +1 -1
  95. package/dist/esm/libs/crunch.js +1 -0
  96. package/dist/esm/npy-loader.js.map +1 -1
  97. package/dist/index.d.ts +2 -1
  98. package/dist/index.d.ts.map +1 -0
  99. package/dist/index.js +37 -0
  100. package/dist/ktx2-basis-universal-texture-writer.d.ts +1 -0
  101. package/dist/ktx2-basis-universal-texture-writer.d.ts.map +1 -0
  102. package/dist/ktx2-basis-universal-texture-writer.js +25 -0
  103. package/dist/lib/encoders/encode-ktx.d.ts +1 -0
  104. package/dist/lib/encoders/encode-ktx.d.ts.map +1 -0
  105. package/dist/lib/encoders/encode-ktx.js +10 -0
  106. package/dist/lib/encoders/encode-ktx2-basis-texture.d.ts +1 -0
  107. package/dist/lib/encoders/encode-ktx2-basis-texture.d.ts.map +1 -0
  108. package/dist/lib/encoders/encode-ktx2-basis-texture.js +41 -0
  109. package/dist/lib/encoders/encode-texture.d.ts +1 -0
  110. package/dist/lib/encoders/encode-texture.d.ts.map +1 -0
  111. package/dist/lib/encoders/encode-texture.js +30 -0
  112. package/dist/lib/{gl-constants.d.ts → gl-extensions.d.ts} +2 -1
  113. package/dist/lib/gl-extensions.d.ts.map +1 -0
  114. package/dist/lib/gl-extensions.js +72 -0
  115. package/dist/lib/parsers/basis-module-loader.d.ts +1 -0
  116. package/dist/lib/parsers/basis-module-loader.d.ts.map +1 -0
  117. package/dist/lib/parsers/basis-module-loader.js +114 -0
  118. package/dist/lib/parsers/crunch-module-loader.d.ts +1 -0
  119. package/dist/lib/parsers/crunch-module-loader.d.ts.map +1 -0
  120. package/dist/lib/parsers/crunch-module-loader.js +35 -0
  121. package/dist/lib/parsers/parse-basis.d.ts +1 -0
  122. package/dist/lib/parsers/parse-basis.d.ts.map +1 -0
  123. package/dist/lib/parsers/parse-basis.js +245 -0
  124. package/dist/lib/parsers/parse-compressed-texture.d.ts +1 -0
  125. package/dist/lib/parsers/parse-compressed-texture.d.ts.map +1 -0
  126. package/dist/lib/parsers/parse-compressed-texture.js +26 -0
  127. package/dist/lib/parsers/parse-crunch.d.ts +1 -0
  128. package/dist/lib/parsers/parse-crunch.d.ts.map +1 -0
  129. package/dist/lib/parsers/parse-crunch.js +105 -0
  130. package/dist/lib/parsers/parse-dds.d.ts +1 -0
  131. package/dist/lib/parsers/parse-dds.d.ts.map +1 -0
  132. package/dist/lib/parsers/parse-dds.js +108 -0
  133. package/dist/lib/parsers/parse-ktx.d.ts +3 -2
  134. package/dist/lib/parsers/parse-ktx.d.ts.map +1 -0
  135. package/dist/lib/parsers/parse-ktx.js +46 -0
  136. package/dist/lib/parsers/parse-npy.d.ts +8 -2
  137. package/dist/lib/parsers/parse-npy.d.ts.map +1 -0
  138. package/dist/lib/parsers/parse-npy.js +85 -0
  139. package/dist/lib/parsers/parse-pvr.d.ts +1 -0
  140. package/dist/lib/parsers/parse-pvr.d.ts.map +1 -0
  141. package/dist/lib/parsers/parse-pvr.js +250 -0
  142. package/dist/lib/texture-api/async-deep-map.d.ts +5 -2
  143. package/dist/lib/texture-api/async-deep-map.d.ts.map +1 -0
  144. package/dist/lib/texture-api/async-deep-map.js +41 -0
  145. package/dist/lib/texture-api/deep-load.d.ts +5 -2
  146. package/dist/lib/texture-api/deep-load.d.ts.map +1 -0
  147. package/dist/lib/texture-api/deep-load.js +16 -0
  148. package/dist/lib/texture-api/generate-url.d.ts +3 -1
  149. package/dist/lib/texture-api/generate-url.d.ts.map +1 -0
  150. package/dist/lib/texture-api/generate-url.js +17 -0
  151. package/dist/lib/texture-api/load-image-array.d.ts +4 -2
  152. package/dist/lib/texture-api/load-image-array.d.ts.map +1 -0
  153. package/dist/lib/texture-api/load-image-array.js +21 -0
  154. package/dist/lib/texture-api/load-image-cube.d.ts +12 -2
  155. package/dist/lib/texture-api/load-image-cube.d.ts.map +1 -0
  156. package/dist/lib/texture-api/load-image-cube.js +47 -0
  157. package/dist/lib/texture-api/load-image.d.ts +7 -5
  158. package/dist/lib/texture-api/load-image.d.ts.map +1 -0
  159. package/dist/lib/texture-api/load-image.js +43 -0
  160. package/dist/lib/texture-api/texture-api-types.d.ts +10 -0
  161. package/dist/lib/texture-api/texture-api-types.d.ts.map +1 -0
  162. package/dist/lib/texture-api/texture-api-types.js +3 -0
  163. package/dist/lib/utils/extract-mipmap-images.d.ts +2 -1
  164. package/dist/lib/utils/extract-mipmap-images.d.ts.map +1 -0
  165. package/dist/lib/utils/extract-mipmap-images.js +50 -0
  166. package/dist/lib/utils/ktx-format-helper.d.ts +4 -3
  167. package/dist/lib/utils/ktx-format-helper.d.ts.map +1 -0
  168. package/dist/lib/utils/ktx-format-helper.js +125 -0
  169. package/dist/lib/utils/texture-formats.d.ts +1 -0
  170. package/dist/lib/utils/texture-formats.d.ts.map +1 -0
  171. package/dist/lib/utils/texture-formats.js +51 -0
  172. package/dist/lib/utils/version.d.ts +1 -0
  173. package/dist/lib/utils/version.d.ts.map +1 -0
  174. package/dist/lib/utils/version.js +8 -0
  175. package/dist/libs/README.md +2 -2
  176. package/dist/libs/basis_encoder.js +1 -1
  177. package/dist/libs/basis_transcoder.js +1 -1
  178. package/dist/libs/crunch.js +1 -0
  179. package/dist/npy-loader.d.ts +7 -3
  180. package/dist/npy-loader.d.ts.map +1 -0
  181. package/dist/npy-loader.js +34 -0
  182. package/dist/npy-worker.js +240 -2
  183. package/dist/types.d.ts +1 -0
  184. package/dist/types.d.ts.map +1 -0
  185. package/dist/types.js +2 -0
  186. package/dist/workers/basis-worker.d.ts +1 -0
  187. package/dist/workers/basis-worker.d.ts.map +1 -0
  188. package/dist/workers/basis-worker.js +5 -0
  189. package/dist/workers/compressed-texture-worker.d.ts +1 -0
  190. package/dist/workers/compressed-texture-worker.d.ts.map +1 -0
  191. package/dist/workers/compressed-texture-worker.js +5 -0
  192. package/dist/workers/crunch-worker.d.ts +1 -0
  193. package/dist/workers/crunch-worker.d.ts.map +1 -0
  194. package/dist/workers/crunch-worker.js +14 -0
  195. package/dist/workers/npy-worker.d.ts +1 -0
  196. package/dist/workers/npy-worker.d.ts.map +1 -0
  197. package/dist/workers/npy-worker.js +5 -0
  198. package/package.json +10 -11
  199. package/src/index.ts +1 -1
  200. package/src/lib/{gl-constants.ts → gl-extensions.ts} +1 -1
  201. package/src/lib/parsers/crunch-module-loader.ts +1 -0
  202. package/src/lib/parsers/parse-basis.js +32 -8
  203. package/src/lib/parsers/parse-crunch.ts +4 -4
  204. package/src/lib/parsers/parse-dds.ts +29 -26
  205. package/src/lib/parsers/parse-ktx.js +3 -6
  206. package/src/lib/parsers/parse-ktx.ts +47 -0
  207. package/src/lib/parsers/parse-npy.js +3 -17
  208. package/src/lib/parsers/parse-npy.ts +98 -0
  209. package/src/lib/parsers/parse-pvr.ts +90 -48
  210. package/src/lib/texture-api/async-deep-map.ts +15 -8
  211. package/src/lib/texture-api/deep-load.ts +7 -3
  212. package/src/lib/texture-api/generate-url.ts +10 -8
  213. package/src/lib/texture-api/load-image-array.ts +8 -2
  214. package/src/lib/texture-api/load-image-cube.ts +18 -4
  215. package/src/lib/texture-api/load-image.ts +15 -5
  216. package/src/lib/texture-api/texture-api-types.ts +12 -0
  217. package/src/lib/utils/extract-mipmap-images.ts +16 -3
  218. package/src/lib/utils/ktx-format-helper.ts +71 -71
  219. package/src/libs/README.md +2 -2
  220. package/src/libs/basis_encoder.js +1 -1
  221. package/src/libs/basis_transcoder.js +1 -1
  222. package/src/libs/crunch.js +1 -0
  223. package/src/npy-loader.ts +2 -2
  224. package/dist/basis-worker.js.map +0 -1
  225. package/dist/compressed-texture-worker.js.map +0 -1
  226. package/dist/crunch-worker.js.map +0 -1
  227. package/dist/dist.min.js +0 -2
  228. package/dist/dist.min.js.map +0 -1
  229. package/dist/es5/lib/gl-constants.js.map +0 -1
  230. package/dist/esm/lib/gl-constants.js.map +0 -1
  231. package/dist/npy-worker.js.map +0 -1
@@ -0,0 +1,245 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.selectSupportedBasisFormat = void 0;
4
+ const basis_module_loader_1 = require("./basis-module-loader");
5
+ const gl_extensions_1 = require("../gl-extensions");
6
+ const texture_formats_1 = require("../utils/texture-formats");
7
+ const parse_ktx_1 = require("./parse-ktx");
8
+ const OutputFormat = {
9
+ etc1: {
10
+ basisFormat: 0,
11
+ compressed: true,
12
+ format: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGB_ETC1_WEBGL
13
+ },
14
+ etc2: { basisFormat: 1, compressed: true },
15
+ bc1: {
16
+ basisFormat: 2,
17
+ compressed: true,
18
+ format: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGB_S3TC_DXT1_EXT
19
+ },
20
+ bc3: {
21
+ basisFormat: 3,
22
+ compressed: true,
23
+ format: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_S3TC_DXT5_EXT
24
+ },
25
+ bc4: { basisFormat: 4, compressed: true },
26
+ bc5: { basisFormat: 5, compressed: true },
27
+ 'bc7-m6-opaque-only': { basisFormat: 6, compressed: true },
28
+ 'bc7-m5': { basisFormat: 7, compressed: true },
29
+ 'pvrtc1-4-rgb': {
30
+ basisFormat: 8,
31
+ compressed: true,
32
+ format: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGB_PVRTC_4BPPV1_IMG
33
+ },
34
+ 'pvrtc1-4-rgba': {
35
+ basisFormat: 9,
36
+ compressed: true,
37
+ format: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG
38
+ },
39
+ 'astc-4x4': {
40
+ basisFormat: 10,
41
+ compressed: true,
42
+ format: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_ASTC_4X4_KHR
43
+ },
44
+ 'atc-rgb': { basisFormat: 11, compressed: true },
45
+ 'atc-rgba-interpolated-alpha': { basisFormat: 12, compressed: true },
46
+ rgba32: { basisFormat: 13, compressed: false },
47
+ rgb565: { basisFormat: 14, compressed: false },
48
+ bgr565: { basisFormat: 15, compressed: false },
49
+ rgba4444: { basisFormat: 16, compressed: false }
50
+ };
51
+ /**
52
+ * parse data with a Binomial Basis_Universal module
53
+ * @param {ArrayBuffer} data
54
+ * @param {*} options
55
+ * @returns compressed texture data
56
+ */
57
+ async function parseBasis(data, options) {
58
+ if (options.basis.containerFormat === 'auto') {
59
+ if ((0, parse_ktx_1.isKTX)(data)) {
60
+ const fileConstructors = await (0, basis_module_loader_1.loadBasisEncoderModule)(options);
61
+ return parseKTX2File(fileConstructors.KTX2File, data, options);
62
+ }
63
+ const { BasisFile } = await (0, basis_module_loader_1.loadBasisTrascoderModule)(options);
64
+ return parseBasisFile(BasisFile, data, options);
65
+ }
66
+ switch (options.basis.module) {
67
+ case 'encoder':
68
+ const fileConstructors = await (0, basis_module_loader_1.loadBasisEncoderModule)(options);
69
+ switch (options.basis.containerFormat) {
70
+ case 'ktx2':
71
+ return parseKTX2File(fileConstructors.KTX2File, data, options);
72
+ case 'basis':
73
+ default:
74
+ return parseBasisFile(fileConstructors.BasisFile, data, options);
75
+ }
76
+ case 'transcoder':
77
+ default:
78
+ const { BasisFile } = await (0, basis_module_loader_1.loadBasisTrascoderModule)(options);
79
+ return parseBasisFile(BasisFile, data, options);
80
+ }
81
+ }
82
+ exports.default = parseBasis;
83
+ /**
84
+ * Parse *.basis file data
85
+ * @param {*} BasisFile - initialized transcoder module
86
+ * @param {*} data
87
+ * @param {*} options
88
+ * @returns compressed texture data
89
+ */
90
+ function parseBasisFile(BasisFile, data, options) {
91
+ const basisFile = new BasisFile(new Uint8Array(data));
92
+ try {
93
+ if (!basisFile.startTranscoding()) {
94
+ return null;
95
+ }
96
+ const imageCount = basisFile.getNumImages();
97
+ const images = [];
98
+ for (let imageIndex = 0; imageIndex < imageCount; imageIndex++) {
99
+ const levelsCount = basisFile.getNumLevels(imageIndex);
100
+ const levels = [];
101
+ for (let levelIndex = 0; levelIndex < levelsCount; levelIndex++) {
102
+ levels.push(transcodeImage(basisFile, imageIndex, levelIndex, options));
103
+ }
104
+ images.push(levels);
105
+ }
106
+ return images;
107
+ }
108
+ finally {
109
+ basisFile.close();
110
+ basisFile.delete();
111
+ }
112
+ }
113
+ /**
114
+ * Parse the particular level image of a basis file
115
+ * @param {*} basisFile
116
+ * @param {*} imageIndex
117
+ * @param {*} levelIndex
118
+ * @param {*} options
119
+ * @returns compressed texture data
120
+ */
121
+ function transcodeImage(basisFile, imageIndex, levelIndex, options) {
122
+ const width = basisFile.getImageWidth(imageIndex, levelIndex);
123
+ const height = basisFile.getImageHeight(imageIndex, levelIndex);
124
+ // See https://github.com/BinomialLLC/basis_universal/pull/83
125
+ const hasAlpha = basisFile.getHasAlpha( /* imageIndex, levelIndex */);
126
+ // Check options for output format etc
127
+ const { compressed, format, basisFormat } = getBasisOptions(options, hasAlpha);
128
+ const decodedSize = basisFile.getImageTranscodedSizeInBytes(imageIndex, levelIndex, basisFormat);
129
+ const decodedData = new Uint8Array(decodedSize);
130
+ if (!basisFile.transcodeImage(decodedData, imageIndex, levelIndex, basisFormat, 0, 0)) {
131
+ return null;
132
+ }
133
+ return {
134
+ // standard loaders.gl image category payload
135
+ width,
136
+ height,
137
+ data: decodedData,
138
+ compressed,
139
+ // Additional fields
140
+ // Add levelSize field.
141
+ hasAlpha,
142
+ format
143
+ };
144
+ }
145
+ /**
146
+ * Parse *.ktx2 file data
147
+ * @param {*} KTX2File
148
+ * @param {*} data
149
+ * @param {*} options
150
+ * @returns compressed texture data
151
+ */
152
+ function parseKTX2File(KTX2File, data, options) {
153
+ const ktx2File = new KTX2File(new Uint8Array(data));
154
+ try {
155
+ if (!ktx2File.startTranscoding()) {
156
+ return null;
157
+ }
158
+ const levelsCount = ktx2File.getLevels();
159
+ const levels = [];
160
+ for (let levelIndex = 0; levelIndex < levelsCount; levelIndex++) {
161
+ levels.push(transcodeKTX2Image(ktx2File, levelIndex, options));
162
+ break; // texture app can only show one level for some reason
163
+ }
164
+ return levels;
165
+ }
166
+ finally {
167
+ ktx2File.close();
168
+ ktx2File.delete();
169
+ }
170
+ }
171
+ /**
172
+ * Parse the particular level image of a ktx2 file
173
+ * @param {*} ktx2File
174
+ * @param {*} levelIndex
175
+ * @param {*} options
176
+ * @returns
177
+ */
178
+ function transcodeKTX2Image(ktx2File, levelIndex, options) {
179
+ const { alphaFlag, height, width } = ktx2File.getImageLevelInfo(levelIndex, 0, 0);
180
+ // Check options for output format etc
181
+ const { compressed, format, basisFormat } = getBasisOptions(options, alphaFlag);
182
+ const decodedSize = ktx2File.getImageTranscodedSizeInBytes(levelIndex, 0 /* layerIndex */, 0 /* faceIndex */, basisFormat);
183
+ const decodedData = new Uint8Array(decodedSize);
184
+ if (!ktx2File.transcodeImage(decodedData, levelIndex, 0 /* layerIndex */, 0 /* faceIndex */, basisFormat, 0, -1 /* channel0 */, -1 /* channel1 */)) {
185
+ return null;
186
+ }
187
+ return {
188
+ // standard loaders.gl image category payload
189
+ width,
190
+ height,
191
+ data: decodedData,
192
+ compressed,
193
+ // Additional fields
194
+ // Add levelSize field.
195
+ alphaFlag,
196
+ format
197
+ };
198
+ }
199
+ /**
200
+ * Get BasisFormat by loader format option
201
+ * @param {*} options
202
+ * @param {*} hasAlpha
203
+ * @returns BasisFormat data
204
+ */
205
+ function getBasisOptions(options, hasAlpha) {
206
+ let format = options && options.basis && options.basis.format;
207
+ if (format === 'auto') {
208
+ format = selectSupportedBasisFormat();
209
+ }
210
+ if (typeof format === 'object') {
211
+ format = hasAlpha ? format.alpha : format.noAlpha;
212
+ }
213
+ format = format.toLowerCase();
214
+ return OutputFormat[format];
215
+ }
216
+ /**
217
+ * Select transcode format from the list of supported formats
218
+ * @returns key for OutputFormat map
219
+ */
220
+ function selectSupportedBasisFormat() {
221
+ const supportedFormats = (0, texture_formats_1.getSupportedGPUTextureFormats)();
222
+ if (supportedFormats.has('astc')) {
223
+ return 'astc-4x4';
224
+ }
225
+ else if (supportedFormats.has('dxt')) {
226
+ return {
227
+ alpha: 'bc3',
228
+ noAlpha: 'bc1'
229
+ };
230
+ }
231
+ else if (supportedFormats.has('pvrtc')) {
232
+ return {
233
+ alpha: 'pvrtc1-4-rgba',
234
+ noAlpha: 'pvrtc1-4-rgb'
235
+ };
236
+ }
237
+ else if (supportedFormats.has('etc1')) {
238
+ return 'etc1';
239
+ }
240
+ else if (supportedFormats.has('etc2')) {
241
+ return 'etc2';
242
+ }
243
+ return 'rgb565';
244
+ }
245
+ exports.selectSupportedBasisFormat = selectSupportedBasisFormat;
@@ -5,3 +5,4 @@ import type { TextureLevel } from '../../types';
5
5
  * @returns Array of the texture levels
6
6
  */
7
7
  export declare function parseCompressedTexture(data: ArrayBuffer): TextureLevel[];
8
+ //# sourceMappingURL=parse-compressed-texture.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-compressed-texture.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-compressed-texture.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAK9C;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,WAAW,GAAG,YAAY,EAAE,CAaxE"}
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseCompressedTexture = void 0;
4
+ const parse_ktx_1 = require("./parse-ktx");
5
+ const parse_dds_1 = require("./parse-dds");
6
+ const parse_pvr_1 = require("./parse-pvr");
7
+ /**
8
+ * Deduces format and parses compressed texture loaded in ArrayBuffer
9
+ * @param data - binary data of compressed texture
10
+ * @returns Array of the texture levels
11
+ */
12
+ function parseCompressedTexture(data) {
13
+ if ((0, parse_ktx_1.isKTX)(data)) {
14
+ // TODO: remove @ts-ignore when `parseKTX` output is normalized to loaders.gl texture format
15
+ // @ts-ignore
16
+ return (0, parse_ktx_1.parseKTX)(data);
17
+ }
18
+ if ((0, parse_dds_1.isDDS)(data)) {
19
+ return (0, parse_dds_1.parseDDS)(data);
20
+ }
21
+ if ((0, parse_pvr_1.isPVR)(data)) {
22
+ return (0, parse_pvr_1.parsePVR)(data);
23
+ }
24
+ throw new Error('Texture container format not recognized');
25
+ }
26
+ exports.parseCompressedTexture = parseCompressedTexture;
@@ -6,3 +6,4 @@ import type { TextureLevel } from '../../types';
6
6
  * @returns Promise of Array of the texture levels
7
7
  */
8
8
  export declare function parseCrunch(data: any, options: any): Promise<TextureLevel[]>;
9
+ //# sourceMappingURL=parse-crunch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-crunch.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-crunch.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAqC9C;;;;;GAKG;AACH,wBAAsB,WAAW,CAAC,IAAI,KAAA,EAAE,OAAO,EAAE,GAAG,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,CAmD7E"}
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseCrunch = void 0;
4
+ const crunch_module_loader_1 = require("./crunch-module-loader");
5
+ const gl_extensions_1 = require("../gl-extensions");
6
+ const loader_utils_1 = require("@loaders.gl/loader-utils");
7
+ const parse_dds_1 = require("./parse-dds");
8
+ const extract_mipmap_images_1 = require("../utils/extract-mipmap-images");
9
+ // Taken from crnlib.h
10
+ const CRN_FORMAT = {
11
+ cCRNFmtInvalid: -1,
12
+ cCRNFmtDXT1: 0,
13
+ // cCRNFmtDXT3 is not currently supported when writing to CRN - only DDS.
14
+ cCRNFmtDXT3: 1,
15
+ cCRNFmtDXT5: 2
16
+ // Crunch supports more formats than this.
17
+ };
18
+ // Mapping of Crunch formats to DXT formats.
19
+ const DXT_FORMAT_MAP = {};
20
+ DXT_FORMAT_MAP[CRN_FORMAT.cCRNFmtDXT1] = {
21
+ pixelFormat: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGB_S3TC_DXT1_EXT,
22
+ sizeFunction: parse_dds_1.getDxt1LevelSize
23
+ };
24
+ DXT_FORMAT_MAP[CRN_FORMAT.cCRNFmtDXT3] = {
25
+ pixelFormat: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_S3TC_DXT3_EXT,
26
+ sizeFunction: parse_dds_1.getDxtXLevelSize
27
+ };
28
+ DXT_FORMAT_MAP[CRN_FORMAT.cCRNFmtDXT5] = {
29
+ pixelFormat: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_S3TC_DXT5_EXT,
30
+ sizeFunction: parse_dds_1.getDxtXLevelSize
31
+ };
32
+ let cachedDstSize = 0;
33
+ let dst;
34
+ /**
35
+ * Parse texture data as "CRN" format.
36
+ * Function is "async" as emscriptified decoder module is loaded asyncronously
37
+ * @param data - binary data of compressed texture
38
+ * @returns Promise of Array of the texture levels
39
+ */
40
+ async function parseCrunch(data, options) {
41
+ const crunchModule = await (0, crunch_module_loader_1.loadCrunchModule)(options);
42
+ // Copy the contents of the arrayBuffer into emscriptens heap.
43
+ const srcSize = data.byteLength;
44
+ const bytes = new Uint8Array(data);
45
+ const src = crunchModule._malloc(srcSize);
46
+ arrayBufferCopy(bytes, crunchModule.HEAPU8, src, srcSize);
47
+ // Determine what type of compressed data the file contains.
48
+ const format = crunchModule._crn_get_dxt_format(src, srcSize);
49
+ (0, loader_utils_1.assert)(Boolean(DXT_FORMAT_MAP[format]), 'Unsupported format');
50
+ // Gather basic metrics about the DXT data.
51
+ const mipMapLevels = crunchModule._crn_get_levels(src, srcSize);
52
+ const width = crunchModule._crn_get_width(src, srcSize);
53
+ const height = crunchModule._crn_get_height(src, srcSize);
54
+ // const bytesPerBlock = crunchModule._crn_get_bytes_per_block(src, srcSize);
55
+ // Determine the size of the decoded DXT data.
56
+ const sizeFunction = DXT_FORMAT_MAP[format].sizeFunction;
57
+ let dstSize = 0;
58
+ for (let i = 0; i < mipMapLevels; ++i) {
59
+ dstSize += sizeFunction(width >> i, height >> i);
60
+ }
61
+ // Allocate enough space on the emscripten heap to hold the decoded DXT data
62
+ // or reuse the existing allocation if a previous call to this function has
63
+ // already acquired a large enough buffer.
64
+ if (cachedDstSize < dstSize) {
65
+ if (dst) {
66
+ crunchModule._free(dst);
67
+ }
68
+ dst = crunchModule._malloc(dstSize);
69
+ cachedDstSize = dstSize;
70
+ }
71
+ // Decompress the DXT data from the Crunch file into the allocated space.
72
+ crunchModule._crn_decompress(src, srcSize, dst, dstSize, 0, mipMapLevels);
73
+ // Release the crunch file data from the emscripten heap.
74
+ crunchModule._free(src);
75
+ const image = new Uint8Array(crunchModule.HEAPU8.buffer, dst, dstSize).slice();
76
+ return (0, extract_mipmap_images_1.extractMipmapImages)(image, {
77
+ mipMapLevels,
78
+ width,
79
+ height,
80
+ sizeFunction,
81
+ internalFormat: DXT_FORMAT_MAP[format].pixelFormat
82
+ });
83
+ }
84
+ exports.parseCrunch = parseCrunch;
85
+ /**
86
+ * Copy an array of bytes into or out of the emscripten heap
87
+ * @param {Uint8Array} srcData - Source data array
88
+ * @param {Uint8Array} dstData - Destination data array
89
+ * @param {number} dstByteOffset - Destination data offset
90
+ * @param {number} numBytes - number of bytes to copy
91
+ * @returns {void}
92
+ */
93
+ function arrayBufferCopy(srcData, dstData, dstByteOffset, numBytes) {
94
+ let i;
95
+ const dst32Offset = dstByteOffset / 4;
96
+ const tail = numBytes % 4;
97
+ const src32 = new Uint32Array(srcData.buffer, 0, (numBytes - tail) / 4);
98
+ const dst32 = new Uint32Array(dstData.buffer);
99
+ for (i = 0; i < src32.length; i++) {
100
+ dst32[dst32Offset + i] = src32[i];
101
+ }
102
+ for (i = numBytes - tail; i < numBytes; i++) {
103
+ dstData[dstByteOffset + i] = srcData[i];
104
+ }
105
+ }
@@ -25,3 +25,4 @@ export declare function getDxt1LevelSize(width: number, height: number): number;
25
25
  * @returns level size in bytes
26
26
  */
27
27
  export declare function getDxtXLevelSize(width: number, height: number): number;
28
+ //# sourceMappingURL=parse-dds.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-dds.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-dds.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AA0C9C;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,WAAW,GAAG,OAAO,CAIhD;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,WAAW,GAAG,YAAY,EAAE,CA4B1D;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAEtE;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAEtE"}
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDxtXLevelSize = exports.getDxt1LevelSize = exports.parseDDS = exports.isDDS = void 0;
4
+ const loader_utils_1 = require("@loaders.gl/loader-utils");
5
+ const gl_extensions_1 = require("../gl-extensions");
6
+ const extract_mipmap_images_1 = require("../utils/extract-mipmap-images");
7
+ const DDS_CONSTANTS = {
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
20
+ };
21
+ const DDS_PIXEL_FORMATS = {
22
+ DXT1: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGB_S3TC_DXT1_EXT,
23
+ DXT3: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_S3TC_DXT3_EXT,
24
+ DXT5: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_S3TC_DXT5_EXT,
25
+ 'ATC ': gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGB_ATC_WEBGL,
26
+ ATCA: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_ATC_EXPLICIT_ALPHA_WEBGL,
27
+ ATCI: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_ATC_INTERPOLATED_ALPHA_WEBGL
28
+ };
29
+ const getATCLevelSize = getDxt1LevelSize;
30
+ const getATCALevelSize = getDxtXLevelSize;
31
+ const getATCILevelSize = getDxtXLevelSize;
32
+ const DDS_SIZE_FUNCTIONS = {
33
+ DXT1: getDxt1LevelSize,
34
+ DXT3: getDxtXLevelSize,
35
+ DXT5: getDxtXLevelSize,
36
+ 'ATC ': getATCLevelSize,
37
+ ATCA: getATCALevelSize,
38
+ ATCI: getATCILevelSize
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
+ */
45
+ function isDDS(data) {
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;
49
+ }
50
+ exports.isDDS = isDDS;
51
+ /**
52
+ * Parse texture data as "DDS" format
53
+ * @param data - binary data of compressed texture
54
+ * @returns Array of the texture levels
55
+ */
56
+ function parseDDS(data) {
57
+ const header = new Int32Array(data, 0, DDS_CONSTANTS.HEADER_LENGTH);
58
+ const pixelFormatNumber = header[DDS_CONSTANTS.HEADER_PF_FOURCC_INDEX];
59
+ (0, loader_utils_1.assert)(Boolean(header[DDS_CONSTANTS.HEADER_PF_FLAGS_INDEX] & DDS_CONSTANTS.DDPF_FOURCC), 'DDS: Unsupported format, must contain a FourCC code');
60
+ const fourCC = int32ToFourCC(pixelFormatNumber);
61
+ const internalFormat = DDS_PIXEL_FORMATS[fourCC];
62
+ const sizeFunction = DDS_SIZE_FUNCTIONS[fourCC];
63
+ (0, loader_utils_1.assert)(internalFormat && sizeFunction, `DDS: Unknown pixel format ${pixelFormatNumber}`);
64
+ let mipMapLevels = 1;
65
+ if (header[DDS_CONSTANTS.HEADER_FLAGS_INDEX] & DDS_CONSTANTS.DDSD_MIPMAPCOUNT) {
66
+ mipMapLevels = Math.max(1, header[DDS_CONSTANTS.MIPMAPCOUNT_INDEX]);
67
+ }
68
+ const width = header[DDS_CONSTANTS.HEADER_WIDTH_INDEX];
69
+ const height = header[DDS_CONSTANTS.HEADER_HEIGHT_INDEX];
70
+ const dataOffset = header[DDS_CONSTANTS.HEADER_SIZE_INDEX] + 4;
71
+ const image = new Uint8Array(data, dataOffset);
72
+ return (0, extract_mipmap_images_1.extractMipmapImages)(image, {
73
+ mipMapLevels,
74
+ width,
75
+ height,
76
+ sizeFunction,
77
+ internalFormat
78
+ });
79
+ }
80
+ exports.parseDDS = parseDDS;
81
+ /**
82
+ * DXT1 applicable function to calculate level size
83
+ * @param width - level width
84
+ * @param height - level height
85
+ * @returns level size in bytes
86
+ */
87
+ function getDxt1LevelSize(width, height) {
88
+ return ((width + 3) >> 2) * ((height + 3) >> 2) * 8;
89
+ }
90
+ exports.getDxt1LevelSize = getDxt1LevelSize;
91
+ /**
92
+ * DXT3 & DXT5 applicable function to calculate level size
93
+ * @param width - level width
94
+ * @param height - level height
95
+ * @returns level size in bytes
96
+ */
97
+ function getDxtXLevelSize(width, height) {
98
+ return ((width + 3) >> 2) * ((height + 3) >> 2) * 16;
99
+ }
100
+ exports.getDxtXLevelSize = getDxtXLevelSize;
101
+ /**
102
+ * Convert every byte of Int32 value to char
103
+ * @param value - Int32 number
104
+ * @returns string of 4 characters
105
+ */
106
+ function int32ToFourCC(value) {
107
+ return String.fromCharCode(value & 0xff, (value >> 8) & 0xff, (value >> 16) & 0xff, (value >> 24) & 0xff);
108
+ }
@@ -1,2 +1,3 @@
1
- export function isKTX(data: any): boolean;
2
- export function parseKTX(arrayBuffer: any): import("../../types").TextureLevel[];
1
+ export declare function isKTX(data: ArrayBuffer): boolean;
2
+ export declare function parseKTX(arrayBuffer: ArrayBuffer): import("../../types").TextureLevel[];
3
+ //# sourceMappingURL=parse-ktx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-ktx.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-ktx.ts"],"names":[],"mappings":"AAUA,wBAAgB,KAAK,CAAC,IAAI,EAAE,WAAW,WAmBtC;AAED,wBAAgB,QAAQ,CAAC,WAAW,EAAE,WAAW,wCAehD"}
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseKTX = exports.isKTX = void 0;
4
+ const ktx_parse_1 = require("ktx-parse");
5
+ const extract_mipmap_images_1 = require("../utils/extract-mipmap-images");
6
+ const ktx_format_helper_1 = require("../utils/ktx-format-helper");
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
12
+ function isKTX(data) {
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;
29
+ }
30
+ exports.isKTX = isKTX;
31
+ function parseKTX(arrayBuffer) {
32
+ const uint8Array = new Uint8Array(arrayBuffer);
33
+ const ktx = (0, ktx_parse_1.read)(uint8Array);
34
+ const mipMapLevels = Math.max(1, ktx.levels.length);
35
+ const width = ktx.pixelWidth;
36
+ const height = ktx.pixelHeight;
37
+ const internalFormat = (0, ktx_format_helper_1.mapVkFormatToWebGL)(ktx.vkFormat);
38
+ return (0, extract_mipmap_images_1.extractMipmapImages)(ktx.levels, {
39
+ mipMapLevels,
40
+ width,
41
+ height,
42
+ sizeFunction: (level) => level.uncompressedByteLength,
43
+ internalFormat
44
+ });
45
+ }
46
+ exports.parseKTX = parseKTX;
@@ -1,4 +1,10 @@
1
- export function parseNPY(arrayBuffer: any, options: any): {
1
+ declare type NumpyHeader = {
2
+ descr: string;
3
+ shape: number[];
4
+ };
5
+ export declare function parseNPY(arrayBuffer: ArrayBuffer, options?: unknown): {
2
6
  data: any;
3
- header: any;
7
+ header: NumpyHeader;
4
8
  } | null;
9
+ export {};
10
+ //# sourceMappingURL=parse-npy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-npy.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-npy.ts"],"names":[],"mappings":"AAAA,aAAK,WAAW,GAAG;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAC,CAAC;AA6BpD,wBAAgB,QAAQ,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,OAAO;;;SA+BnE"}
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseNPY = void 0;
4
+ function systemIsLittleEndian() {
5
+ const a = new Uint32Array([0x12345678]);
6
+ const b = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
7
+ return !(b[0] === 0x12);
8
+ }
9
+ const LITTLE_ENDIAN_OS = systemIsLittleEndian();
10
+ // The basic string format consists of 3 characters:
11
+ // 1. a character describing the byteorder of the data (<: little-endian, >: big-endian, |: not-relevant)
12
+ // 2. a character code giving the basic type of the array
13
+ // 3. an integer providing the number of bytes the type uses.
14
+ // https://numpy.org/doc/stable/reference/arrays.interface.html
15
+ //
16
+ // Here I only include the second and third characters, and check endianness
17
+ // separately
18
+ const DTYPES = {
19
+ u1: Uint8Array,
20
+ i1: Int8Array,
21
+ u2: Uint16Array,
22
+ i2: Int16Array,
23
+ u4: Uint32Array,
24
+ i4: Int32Array,
25
+ f4: Float32Array,
26
+ f8: Float64Array
27
+ };
28
+ function parseNPY(arrayBuffer, options) {
29
+ if (!arrayBuffer) {
30
+ return null;
31
+ }
32
+ const view = new DataView(arrayBuffer);
33
+ const { header, headerEndOffset } = parseHeader(view);
34
+ const numpyType = header.descr;
35
+ const ArrayType = DTYPES[numpyType.slice(1, 3)];
36
+ if (!ArrayType) {
37
+ // eslint-disable-next-line no-console, no-undef
38
+ console.warn(`Decoding of npy dtype not implemented: ${numpyType}`);
39
+ return null;
40
+ }
41
+ const nArrayElements = header.shape?.reduce((a, b) => a * b);
42
+ const arrayByteLength = nArrayElements * ArrayType.BYTES_PER_ELEMENT;
43
+ const data = new ArrayType(arrayBuffer.slice(headerEndOffset, headerEndOffset + arrayByteLength));
44
+ // Swap endianness if needed
45
+ if ((numpyType[0] === '>' && LITTLE_ENDIAN_OS) || (numpyType[0] === '<' && !LITTLE_ENDIAN_OS)) {
46
+ // eslint-disable-next-line no-console, no-undef
47
+ console.warn('Data is wrong endianness, byte swapping not yet implemented.');
48
+ }
49
+ return {
50
+ data,
51
+ header
52
+ };
53
+ }
54
+ exports.parseNPY = parseNPY;
55
+ /**
56
+ * Parse NPY header
57
+ *
58
+ * @param view
59
+ * @return
60
+ */
61
+ function parseHeader(view) {
62
+ const majorVersion = view.getUint8(6);
63
+ // const minorVersion = view.getUint8(7);
64
+ let offset = 8;
65
+ let headerLength;
66
+ if (majorVersion >= 2) {
67
+ headerLength = view.getUint32(8, true);
68
+ offset += 4;
69
+ }
70
+ else {
71
+ headerLength = view.getUint16(8, true);
72
+ offset += 2;
73
+ }
74
+ const encoding = majorVersion <= 2 ? 'latin1' : 'utf-8';
75
+ const decoder = new TextDecoder(encoding);
76
+ const headerArray = new Uint8Array(view.buffer, offset, headerLength);
77
+ const headerText = decoder.decode(headerArray);
78
+ offset += headerLength;
79
+ const header = JSON.parse(headerText
80
+ .replace(/'/g, '"')
81
+ .replace('False', 'false')
82
+ .replace('(', '[')
83
+ .replace(/,*\),*/g, ']'));
84
+ return { header, headerEndOffset: offset };
85
+ }