@loaders.gl/textures
Version:
Framework-independent loaders for compressed and super compressed (basis) textures
61 lines • 2.48 kB
JavaScript
// loaders.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import { getTextureFormatFromWebGLFormat, getWebGLFormatFromTextureFormat } from "./texture-format-map.js";
/**
* Extract mipmap images from compressed texture buffer
* @param data - binary data of compressed texture or Array of level objects
* @param options.mipMapLevels - number of mipmap level inside image
* @param options.width - width of 0 - level
* @param options.height - height of 0 - level
* @param options.sizeFunction - format-related function to calculate level size in bytes
* @param options.internalFormat - WebGL compatible format code
* @param options.textureFormat - canonical loaders.gl texture format
* @returns Array of the texture levels
*/
export function extractMipmapImages(data, options) {
const images = new Array(options.mipMapLevels);
const textureFormat = options.textureFormat || getTextureFormatFromWebGLFormat(options.internalFormat);
const format = options.internalFormat || getWebGLFormatFromTextureFormat(options.textureFormat);
let levelWidth = options.width;
let levelHeight = options.height;
let offset = 0;
for (let i = 0; i < options.mipMapLevels; ++i) {
// @ts-expect-error
const levelSize = getLevelSize(options, levelWidth, levelHeight, data, i);
// @ts-expect-error
const levelData = getLevelData(data, i, offset, levelSize);
const image = {
shape: 'texture-level',
compressed: true,
data: levelData,
width: levelWidth,
height: levelHeight,
levelSize
};
if (format !== undefined) {
image.format = format;
}
if (textureFormat) {
image.textureFormat = textureFormat;
}
images[i] = image;
levelWidth = Math.max(1, levelWidth >> 1);
levelHeight = Math.max(1, levelHeight >> 1);
offset += levelSize;
}
return images;
}
function getLevelData(data, index, offset, levelSize) {
if (!Array.isArray(data)) {
return new Uint8Array(data.buffer, data.byteOffset + offset, levelSize);
}
return data[index].levelData;
}
function getLevelSize(options, levelWidth, levelHeight, data, index) {
if (!Array.isArray(data)) {
return options.sizeFunction(levelWidth, levelHeight);
}
return options.sizeFunction(data[index]);
}
//# sourceMappingURL=extract-mipmap-images.js.map