module.exports = class SpaceImageFormatLayer { /** * A layer of a Space Image Format image * * @param {number} layerWidth The width of the layer * @param {number} layerHeight The height of the layer * @param {string} rawData The raw data for the layer */ constructor(layerWidth, layerHeight, rawData) { /** The width of the layer */ this.width = layerWidth; /** The height of the layer */ this.height = layerHeight; /** The raw data of the layer */ this._rawData = rawData; /** The rows of pixels in this layer */ this.rows = this._parseLayerData(rawData); } /** * Parse the raw layer data into rows * * @param {string} rawData The raw layer data * @returns {array[]} The rows of pixels for the layer */ _parseLayerData(rawData) { const rows = []; const rowSplit = new RegExp(`(\\d{${this.width}})`, "g"); const tempRows = rawData.split(rowSplit).filter((r) => !!r); tempRows.forEach((row) => { rows.push(row.split("").map((pixel) => Number(pixel))); }); return rows; } };