SpaceImageFormatLayer.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. module.exports = class SpaceImageFormatLayer {
  2. /**
  3. * A layer of a Space Image Format image
  4. *
  5. * @param {number} layerWidth The width of the layer
  6. * @param {number} layerHeight The height of the layer
  7. * @param {string} rawData The raw data for the layer
  8. */
  9. constructor(layerWidth, layerHeight, rawData) {
  10. /** The width of the layer */
  11. this.width = layerWidth;
  12. /** The height of the layer */
  13. this.height = layerHeight;
  14. /** The raw data of the layer */
  15. this._rawData = rawData;
  16. /** The rows of pixels in this layer */
  17. this.rows = this._parseLayerData(rawData);
  18. }
  19. /**
  20. * Parse the raw layer data into rows
  21. *
  22. * @param {string} rawData The raw layer data
  23. * @returns {array[]} The rows of pixels for the layer
  24. */
  25. _parseLayerData(rawData) {
  26. const rows = [];
  27. const rowSplit = new RegExp(`(\\d{${this.width}})`, "g");
  28. const tempRows = rawData.split(rowSplit).filter((r) => !!r);
  29. tempRows.forEach((row) => {
  30. rows.push(row.split("").map((pixel) => Number(pixel)));
  31. });
  32. return rows;
  33. }
  34. };