@loaders.gl/textures 4.0.0-alpha.23 → 4.0.0-alpha.25

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 (64) hide show
  1. package/dist/basis-worker-node.js +19 -11
  2. package/dist/basis-worker.js +15 -8
  3. package/dist/compressed-texture-worker.js +15 -8
  4. package/dist/crunch-loader.d.ts +9 -2
  5. package/dist/crunch-loader.d.ts.map +1 -1
  6. package/dist/crunch-worker.js +15 -8
  7. package/dist/dist.min.js +19 -12
  8. package/dist/es5/crunch-loader.js.map +1 -1
  9. package/dist/es5/ktx2-basis-writer.js +1 -3
  10. package/dist/es5/ktx2-basis-writer.js.map +1 -1
  11. package/dist/es5/lib/utils/version.js +1 -1
  12. package/dist/esm/crunch-loader.js.map +1 -1
  13. package/dist/esm/ktx2-basis-writer.js +0 -1
  14. package/dist/esm/ktx2-basis-writer.js.map +1 -1
  15. package/dist/esm/lib/utils/version.js +1 -1
  16. package/dist/ktx2-basis-writer-worker-node.js +19 -11
  17. package/dist/ktx2-basis-writer-worker.js +15 -8
  18. package/dist/ktx2-basis-writer.d.ts +1 -2
  19. package/dist/ktx2-basis-writer.d.ts.map +1 -1
  20. package/dist/npy-worker.js +1 -1
  21. package/dist/workers/crunch-worker.d.ts +3 -3
  22. package/package.json +7 -7
  23. package/src/crunch-loader.ts +10 -4
  24. package/src/ktx2-basis-writer.ts +1 -4
  25. package/dist/basis-loader.js +0 -37
  26. package/dist/bundle.js +0 -5
  27. package/dist/compressed-texture-loader.js +0 -63
  28. package/dist/compressed-texture-writer.js +0 -34
  29. package/dist/crunch-loader.js +0 -23
  30. package/dist/index.js +0 -57
  31. package/dist/ktx2-basis-writer.js +0 -28
  32. package/dist/lib/encoders/encode-ktx.js +0 -10
  33. package/dist/lib/encoders/encode-ktx2-basis-texture.js +0 -41
  34. package/dist/lib/encoders/encode-texture.js +0 -27
  35. package/dist/lib/gl-extensions.js +0 -72
  36. package/dist/lib/parsers/basis-module-loader.js +0 -118
  37. package/dist/lib/parsers/crunch-module-loader.js +0 -39
  38. package/dist/lib/parsers/parse-basis.js +0 -245
  39. package/dist/lib/parsers/parse-compressed-texture.js +0 -26
  40. package/dist/lib/parsers/parse-crunch.js +0 -106
  41. package/dist/lib/parsers/parse-dds.js +0 -108
  42. package/dist/lib/parsers/parse-ktx.js +0 -46
  43. package/dist/lib/parsers/parse-npy.js +0 -81
  44. package/dist/lib/parsers/parse-pvr.js +0 -250
  45. package/dist/lib/texture-api/async-deep-map.js +0 -41
  46. package/dist/lib/texture-api/deep-load.js +0 -15
  47. package/dist/lib/texture-api/generate-url.js +0 -17
  48. package/dist/lib/texture-api/load-image-array.js +0 -21
  49. package/dist/lib/texture-api/load-image-cube.js +0 -47
  50. package/dist/lib/texture-api/load-image.js +0 -43
  51. package/dist/lib/texture-api/texture-api-types.js +0 -3
  52. package/dist/lib/utils/extract-mipmap-images.js +0 -50
  53. package/dist/lib/utils/ktx-format-helper.js +0 -125
  54. package/dist/lib/utils/texture-formats.js +0 -51
  55. package/dist/lib/utils/version.js +0 -8
  56. package/dist/npy-loader.js +0 -31
  57. package/dist/types.js +0 -1
  58. package/dist/workers/basis-worker-node.js +0 -7
  59. package/dist/workers/basis-worker.js +0 -5
  60. package/dist/workers/compressed-texture-worker.js +0 -6
  61. package/dist/workers/crunch-worker.js +0 -14
  62. package/dist/workers/ktx2-basis-writer-worker-node.js +0 -28
  63. package/dist/workers/ktx2-basis-writer-worker.js +0 -26
  64. package/dist/workers/npy-worker.js +0 -5
@@ -1,245 +0,0 @@
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 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.loadBasisTranscoderModule)(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.loadBasisTranscoderModule)(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
- throw new Error('Failed to start basis transcoding');
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
- throw new Error('failed to start Basis transcoding');
132
- }
133
- return {
134
- // standard loaders.gl image category payload
135
- width,
136
- height,
137
- data: decodedData,
138
- compressed,
139
- format,
140
- // Additional fields
141
- // Add levelSize field.
142
- hasAlpha
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
- throw new Error('failed to start KTX2 transcoding');
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
- throw new Error('Failed to transcode KTX2 image');
186
- }
187
- return {
188
- // standard loaders.gl image category payload
189
- width,
190
- height,
191
- data: decodedData,
192
- compressed,
193
- // Additional fields
194
- levelSize: decodedSize,
195
- hasAlpha: 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;
@@ -1,26 +0,0 @@
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;
@@ -1,106 +0,0 @@
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
- [CRN_FORMAT.cCRNFmtDXT1]: {
21
- pixelFormat: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGB_S3TC_DXT1_EXT,
22
- sizeFunction: parse_dds_1.getDxt1LevelSize
23
- },
24
- [CRN_FORMAT.cCRNFmtDXT3]: {
25
- pixelFormat: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_S3TC_DXT3_EXT,
26
- sizeFunction: parse_dds_1.getDxtXLevelSize
27
- },
28
- [CRN_FORMAT.cCRNFmtDXT5]: {
29
- pixelFormat: gl_extensions_1.GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_S3TC_DXT5_EXT,
30
- sizeFunction: parse_dds_1.getDxtXLevelSize
31
- }
32
- };
33
- let cachedDstSize = 0;
34
- let dst;
35
- /**
36
- * Parse texture data as "CRN" format.
37
- * Function is "async" as emscriptified decoder module is loaded asyncronously
38
- * @param data - binary data of compressed texture
39
- * @returns Promise of Array of the texture levels
40
- */
41
- async function parseCrunch(data, options) {
42
- const crunchModule = await (0, crunch_module_loader_1.loadCrunchModule)(options);
43
- // Copy the contents of the arrayBuffer into emscriptens heap.
44
- const srcSize = data.byteLength;
45
- const bytes = new Uint8Array(data);
46
- const src = crunchModule._malloc(srcSize);
47
- arrayBufferCopy(bytes, crunchModule.HEAPU8, src, srcSize);
48
- // Determine what type of compressed data the file contains.
49
- const format = crunchModule._crn_get_dxt_format(src, srcSize);
50
- (0, loader_utils_1.assert)(Boolean(DXT_FORMAT_MAP[format]), 'Unsupported format');
51
- // Gather basic metrics about the DXT data.
52
- const mipMapLevels = crunchModule._crn_get_levels(src, srcSize);
53
- const width = crunchModule._crn_get_width(src, srcSize);
54
- const height = crunchModule._crn_get_height(src, srcSize);
55
- // const bytesPerBlock = crunchModule._crn_get_bytes_per_block(src, srcSize);
56
- // Determine the size of the decoded DXT data.
57
- const sizeFunction = DXT_FORMAT_MAP[format].sizeFunction;
58
- let dstSize = 0;
59
- for (let i = 0; i < mipMapLevels; ++i) {
60
- dstSize += sizeFunction(width >> i, height >> i);
61
- }
62
- // Allocate enough space on the emscripten heap to hold the decoded DXT data
63
- // or reuse the existing allocation if a previous call to this function has
64
- // already acquired a large enough buffer.
65
- if (cachedDstSize < dstSize) {
66
- if (dst) {
67
- crunchModule._free(dst);
68
- }
69
- dst = crunchModule._malloc(dstSize);
70
- cachedDstSize = dstSize;
71
- }
72
- // Decompress the DXT data from the Crunch file into the allocated space.
73
- crunchModule._crn_decompress(src, srcSize, dst, dstSize, 0, mipMapLevels);
74
- // Release the crunch file data from the emscripten heap.
75
- crunchModule._free(src);
76
- const image = new Uint8Array(crunchModule.HEAPU8.buffer, dst, dstSize).slice();
77
- return (0, extract_mipmap_images_1.extractMipmapImages)(image, {
78
- mipMapLevels,
79
- width,
80
- height,
81
- sizeFunction,
82
- internalFormat: DXT_FORMAT_MAP[format].pixelFormat
83
- });
84
- }
85
- exports.parseCrunch = parseCrunch;
86
- /**
87
- * Copy an array of bytes into or out of the emscripten heap
88
- * @param {Uint8Array} srcData - Source data array
89
- * @param {Uint8Array} dstData - Destination data array
90
- * @param {number} dstByteOffset - Destination data offset
91
- * @param {number} numBytes - number of bytes to copy
92
- * @returns {void}
93
- */
94
- function arrayBufferCopy(srcData, dstData, dstByteOffset, numBytes) {
95
- let i;
96
- const dst32Offset = dstByteOffset / 4;
97
- const tail = numBytes % 4;
98
- const src32 = new Uint32Array(srcData.buffer, 0, (numBytes - tail) / 4);
99
- const dst32 = new Uint32Array(dstData.buffer);
100
- for (i = 0; i < src32.length; i++) {
101
- dst32[dst32Offset + i] = src32[i];
102
- }
103
- for (i = numBytes - tail; i < numBytes; i++) {
104
- dstData[dstByteOffset + i] = srcData[i];
105
- }
106
- }
@@ -1,108 +0,0 @@
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,46 +0,0 @@
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,81 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseNPY = void 0;
4
- const a = new Uint32Array([0x12345678]);
5
- const b = new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
6
- const isLittleEndian = !(b[0] === 0x12);
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
- */
17
- const DTYPES = {
18
- u1: Uint8Array,
19
- i1: Int8Array,
20
- u2: Uint16Array,
21
- i2: Int16Array,
22
- u4: Uint32Array,
23
- i4: Int32Array,
24
- f4: Float32Array,
25
- f8: Float64Array
26
- };
27
- function parseNPY(arrayBuffer, options) {
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
- };
49
- }
50
- exports.parseNPY = parseNPY;
51
- /**
52
- * Parse NPY header
53
- *
54
- * @param view
55
- * @return
56
- */
57
- function parseHeader(view) {
58
- const majorVersion = view.getUint8(6);
59
- // const minorVersion = view.getUint8(7);
60
- let offset = 8;
61
- let headerLength;
62
- if (majorVersion >= 2) {
63
- headerLength = view.getUint32(offset, true);
64
- offset += 4;
65
- }
66
- else {
67
- headerLength = view.getUint16(offset, true);
68
- offset += 2;
69
- }
70
- const encoding = majorVersion <= 2 ? 'latin1' : 'utf-8';
71
- const decoder = new TextDecoder(encoding);
72
- const headerArray = new Uint8Array(view.buffer, offset, headerLength);
73
- const headerText = decoder.decode(headerArray);
74
- offset += headerLength;
75
- const header = JSON.parse(headerText
76
- .replace(/'/g, '"')
77
- .replace('False', 'false')
78
- .replace('(', '[')
79
- .replace(/,*\),*/g, ']'));
80
- return { header, headerEndOffset: offset };
81
- }