UNPKG

@tensorflow/tfjs-core

Version:

Hardware-accelerated JavaScript library for machine intelligence

192 lines 8.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var TextureUsage; (function (TextureUsage) { TextureUsage[TextureUsage["RENDER"] = 0] = "RENDER"; TextureUsage[TextureUsage["UPLOAD"] = 1] = "UPLOAD"; TextureUsage[TextureUsage["PIXELS"] = 2] = "PIXELS"; TextureUsage[TextureUsage["DOWNLOAD"] = 3] = "DOWNLOAD"; })(TextureUsage = exports.TextureUsage || (exports.TextureUsage = {})); var PhysicalTextureType; (function (PhysicalTextureType) { PhysicalTextureType[PhysicalTextureType["FLOAT16"] = 0] = "FLOAT16"; PhysicalTextureType[PhysicalTextureType["FLOAT32"] = 1] = "FLOAT32"; PhysicalTextureType[PhysicalTextureType["UNSIGNED_BYTE"] = 2] = "UNSIGNED_BYTE"; })(PhysicalTextureType = exports.PhysicalTextureType || (exports.PhysicalTextureType = {})); function getUnpackedMatrixTextureShapeWidthHeight(rows, columns) { return [columns, rows]; } exports.getUnpackedMatrixTextureShapeWidthHeight = getUnpackedMatrixTextureShapeWidthHeight; function getUnpackedArraySizeFromMatrixSize(matrixSize, channelsPerTexture) { return matrixSize * channelsPerTexture; } exports.getUnpackedArraySizeFromMatrixSize = getUnpackedArraySizeFromMatrixSize; function getColorMatrixTextureShapeWidthHeight(rows, columns) { return [columns * 4, rows]; } exports.getColorMatrixTextureShapeWidthHeight = getColorMatrixTextureShapeWidthHeight; function getMatrixSizeFromUnpackedArraySize(unpackedSize, channelsPerTexture) { if (unpackedSize % channelsPerTexture !== 0) { throw new Error("unpackedSize (" + unpackedSize + ") must be a multiple of " + ("" + channelsPerTexture)); } return unpackedSize / channelsPerTexture; } exports.getMatrixSizeFromUnpackedArraySize = getMatrixSizeFromUnpackedArraySize; function encodeMatrixToUnpackedArray(matrix, unpackedArray, channelsPerTexture) { var requiredSize = getUnpackedArraySizeFromMatrixSize(matrix.length, channelsPerTexture); if (unpackedArray.length < requiredSize) { throw new Error("unpackedArray length (" + unpackedArray.length + ") must be >= " + ("" + requiredSize)); } var dst = 0; for (var src = 0; src < matrix.length; ++src) { unpackedArray[dst] = matrix[src]; dst += channelsPerTexture; } } exports.encodeMatrixToUnpackedArray = encodeMatrixToUnpackedArray; function decodeMatrixFromUnpackedArray(unpackedArray, matrix, channelsPerTexture) { var requiredSize = getMatrixSizeFromUnpackedArraySize(unpackedArray.length, channelsPerTexture); if (matrix.length < requiredSize) { throw new Error("matrix length (" + matrix.length + ") must be >= " + requiredSize); } var dst = 0; for (var src = 0; src < unpackedArray.length; src += channelsPerTexture) { matrix[dst++] = unpackedArray[src]; } } exports.decodeMatrixFromUnpackedArray = decodeMatrixFromUnpackedArray; function decodeMatrixFromUnpackedColorRGBAArray(unpackedArray, matrix, channels) { var requiredSize = unpackedArray.length * channels / 4; if (matrix.length < requiredSize) { throw new Error("matrix length (" + matrix.length + ") must be >= " + requiredSize); } var dst = 0; for (var src = 0; src < unpackedArray.length; src += 4) { for (var c = 0; c < channels; c++) { matrix[dst++] = unpackedArray[src + c]; } } } exports.decodeMatrixFromUnpackedColorRGBAArray = decodeMatrixFromUnpackedColorRGBAArray; function getPackedMatrixTextureShapeWidthHeight(rows, columns) { return [Math.ceil(columns / 2), Math.ceil(rows / 2)]; } exports.getPackedMatrixTextureShapeWidthHeight = getPackedMatrixTextureShapeWidthHeight; function getPackedRGBAArraySizeFromMatrixShape(rows, columns) { var _a = getPackedMatrixTextureShapeWidthHeight(rows, columns), w = _a[0], h = _a[1]; return w * h * 4; } exports.getPackedRGBAArraySizeFromMatrixShape = getPackedRGBAArraySizeFromMatrixShape; function encodeMatrixToPackedRGBA(matrix, rows, columns, packedRGBA) { var requiredSize = getPackedRGBAArraySizeFromMatrixShape(rows, columns); if (packedRGBA.length < requiredSize) { throw new Error("packedRGBA length (" + packedRGBA.length + ") must be >= " + requiredSize); } var _a = getPackedMatrixTextureShapeWidthHeight(rows, columns), textureWidth = _a[0], textureHeight = _a[1]; var oddWidth = (columns % 2) === 1; var oddHeight = (rows % 2) === 1; var widthInFullBlocks = Math.floor(columns / 2); var heightInFullBlocks = Math.floor(rows / 2); { var dstStride = (oddWidth ? 4 : 0); var oneRow = columns; var dst = 0; for (var blockY = 0; blockY < heightInFullBlocks; ++blockY) { var matrixSrcRow = (blockY * 2 * columns); for (var blockX = 0; blockX < widthInFullBlocks; ++blockX) { var matrixSrcCol = blockX * 2; var src = matrixSrcRow + matrixSrcCol; packedRGBA[dst] = matrix[src]; packedRGBA[dst + 1] = matrix[src + 1]; packedRGBA[dst + 2] = matrix[src + oneRow]; packedRGBA[dst + 3] = matrix[src + oneRow + 1]; dst += 4; } dst += dstStride; } } if (oddWidth) { var src = columns - 1; var dst = (textureWidth - 1) * 4; var srcStride = 2 * columns; var dstStride = textureWidth * 4; for (var blockY = 0; blockY < heightInFullBlocks; ++blockY) { packedRGBA[dst] = matrix[src]; packedRGBA[dst + 2] = matrix[src + columns]; src += srcStride; dst += dstStride; } } if (oddHeight) { var src = (rows - 1) * columns; var dst = (textureHeight - 1) * textureWidth * 4; for (var blockX = 0; blockX < widthInFullBlocks; ++blockX) { packedRGBA[dst++] = matrix[src++]; packedRGBA[dst++] = matrix[src++]; dst += 2; } } if (oddWidth && oddHeight) { packedRGBA[packedRGBA.length - 4] = matrix[matrix.length - 1]; } return packedRGBA; } exports.encodeMatrixToPackedRGBA = encodeMatrixToPackedRGBA; function decodeMatrixFromPackedRGBA(packedRGBA, rows, columns, matrix) { var requiredSize = rows * columns; if (requiredSize < matrix.length) { throw new Error("matrix length (" + matrix.length + ") must be >= " + requiredSize); } var oddWidth = (columns % 2) === 1; var oddHeight = (rows % 2) === 1; var widthInFullBlocks = Math.floor(columns / 2); var heightInFullBlocks = Math.floor(rows / 2); var _a = getPackedMatrixTextureShapeWidthHeight(rows, columns), textureWidth = _a[0], textureHeight = _a[1]; { var srcStride = oddWidth ? 4 : 0; var dstStride = columns + (oddWidth ? 1 : 0); var src = 0; var dstRow1 = 0; var dstRow2 = columns; for (var blockY = 0; blockY < heightInFullBlocks; ++blockY) { for (var blockX = 0; blockX < widthInFullBlocks; ++blockX) { matrix[dstRow1++] = packedRGBA[src++]; matrix[dstRow1++] = packedRGBA[src++]; matrix[dstRow2++] = packedRGBA[src++]; matrix[dstRow2++] = packedRGBA[src++]; } src += srcStride; dstRow1 += dstStride; dstRow2 += dstStride; } } if (oddWidth) { var src = (textureWidth - 1) * 4; var dst = columns - 1; var srcStride = textureWidth * 4; var dstStride = 2 * columns; for (var blockY = 0; blockY < heightInFullBlocks; ++blockY) { matrix[dst] = packedRGBA[src]; matrix[dst + columns] = packedRGBA[src + 2]; src += srcStride; dst += dstStride; } } if (oddHeight) { var src = (textureHeight - 1) * textureWidth * 4; var dst = (rows - 1) * columns; for (var blockX = 0; blockX < widthInFullBlocks; ++blockX) { matrix[dst++] = packedRGBA[src++]; matrix[dst++] = packedRGBA[src++]; src += 2; } } if (oddWidth && oddHeight) { matrix[matrix.length - 1] = packedRGBA[packedRGBA.length - 4]; } return matrix; } exports.decodeMatrixFromPackedRGBA = decodeMatrixFromPackedRGBA; //# sourceMappingURL=tex_util.js.map