UNPKG

slimfits

Version:

Package for loading data stored in FITS data format

153 lines 6.47 kB
import { BitPixUtils, BitPix } from '../interfaces'; var ArrayUtils = /** @class */ (function () { function ArrayUtils() { } /** * Copies array from source array buffer to target starting from given offset with optional endianess reversal. * @static * @public * @param {ArrayBuffer} source - The source array buffer. * @param {ArrayBuffer} target - The target array buffer. * @param {number} sourceByteOffset - Byte offset in the source ArrayBuffer from which to start copying. * @param {number} length - Length of copied subarray, cannot be 0 * @param {BitPix} type - Type of items being copied, cannot be Unknown * @param {boolean} changeEndian - Flag indicating whether endianess should be reversed. */ ArrayUtils.copy = function (source, target, sourceByteOffset, length, type, changeEndian, targetByteOffset) { if (changeEndian === void 0) { changeEndian = true; } if (targetByteOffset === void 0) { targetByteOffset = 0; } if (length === 0) { throw new Error('Length of copied array cannot be 0'); } if (type === BitPix.Unknown) { throw new Error('Unknown array element type'); } var _a = [new Uint8Array(source), new Uint8Array(target)], sourceBytes = _a[0], targetBytes = _a[1]; var bytesPerElement = BitPixUtils.getByteSize(type); var bytesLength = length * bytesPerElement; if (changeEndian && bytesPerElement !== 1) { for (var i = 0; i < length; i++) { // reversing endianess loop for (var j = 0; j < bytesPerElement; j++) { var offset = sourceByteOffset + bytesPerElement * i + (bytesPerElement - (j + 1)); targetBytes[targetByteOffset + bytesPerElement * i + j] = sourceBytes[offset]; } } } else { for (var i = 0; i < bytesLength; i++) { targetBytes[targetByteOffset + i] = sourceBytes[sourceByteOffset + i]; } } }; /** * Generates typed array for given type. * @static * @public * @param {BitPix} bitPix - The type of the array. * @param {number} length - The length of the array. * @return {ITypedArray} - Array of given length and type. */ ArrayUtils.generateTypedArray = function (bitPix, length) { if (length === 0) { throw new Error('Length of created array cannot be 0'); } switch (bitPix) { case BitPix.Uint8: { return new Uint8Array(length); } case BitPix.Int16: { return new Int16Array(length); } case BitPix.Int32: { return new Int32Array(length); } case BitPix.Float32: { return new Float32Array(length); } case BitPix.Float64: { return new Float64Array(length); } default: throw new Error('Cannot create array of unknown type'); } }; // Plucks column elements from buffer and lays them down in continous space ArrayUtils.pluckColumn = function (source, target, rows, rowByteWidth, rowByteOffset, width, type, changeEndian) { var _a = [new Uint8Array(source), new Uint8Array(target)], sourceBytes = _a[0], targetBytes = _a[1]; var bytesPerElement = BitPixUtils.getByteSize(type); if (changeEndian && bytesPerElement !== 1) { for (var i = 0; i < rows; i++) { for (var j = 0; j < width; j++) { var targetOffset = (i * width + j) * bytesPerElement; var sourceOffset = i * rowByteWidth + rowByteOffset + j * bytesPerElement; for (var k = 0; k < bytesPerElement; k++) { targetBytes[targetOffset + k] = sourceBytes[sourceOffset + (bytesPerElement - (k + 1))]; } } } } else { for (var i = 0; i < rows; i++) { for (var j = 0; j < width; j++) { for (var k = 0; k < bytesPerElement; k++) { targetBytes[i * width * bytesPerElement + j * bytesPerElement + k] = sourceBytes[i * rowByteWidth + rowByteOffset + j * bytesPerElement + k]; } } } } }; ArrayUtils.chunk = function (buffer, dataType, chunkSize) { var chunkByteSize = BitPixUtils.getByteSize(dataType) * chunkSize; var chunksNumber = buffer.byteLength / chunkByteSize; var column = []; switch (dataType) { case BitPix.Uint8: { for (var i = 0; i < chunksNumber; i++) { column.push(new Uint8Array(buffer, i * chunkByteSize, chunkSize)); } return column; } case BitPix.Int16: { for (var i = 0; i < chunksNumber; i++) { column.push(new Int16Array(buffer, i * chunkByteSize, chunkSize)); } return column; } case BitPix.Int32: { for (var i = 0; i < chunksNumber; i++) { column.push(new Int32Array(buffer, i * chunkByteSize, chunkSize)); } return column; } case BitPix.Float32: { for (var i = 0; i < chunksNumber; i++) { column.push(new Float32Array(buffer, i * chunkByteSize, chunkSize)); } return column; } case BitPix.Float64: { for (var i = 0; i < chunksNumber; i++) { column.push(new Float64Array(buffer, i * chunkByteSize, chunkSize)); } return column; } default: throw new Error('Cannot create array of unknown type'); } }; return ArrayUtils; }()); export { ArrayUtils }; //# sourceMappingURL=ArrayUtils.js.map