Browse Source

Move SpaceImageFormat to own folder

To reduce redundencies, and make potential future development easier,
these two classes have been moved to their own files in their own
folder.
ApisNecros 1 year ago
parent
commit
99a9864c2c
4 changed files with 118 additions and 198 deletions
  1. 1 80
      8/8_1.js
  2. 1 118
      8/8_2.js
  3. 78 0
      SpaceImageFormat/SpaceImageFormat.js
  4. 38 0
      SpaceImageFormat/SpaceImageFormatLayer.js

+ 1 - 80
8/8_1.js

@@ -1,83 +1,4 @@
-/* eslint-disable max-classes-per-file */
-
-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;
-    }
-}
-
-class SpaceImageFormat {
-    /**
-     * The Space Image Format
-     *
-     * @param {number} imageWidth The width of the image in pixels
-     * @param {number} imageHeight The height of the image in pixels
-     * @param {string} rawData The raw data for the complete SIF image
-     */
-    constructor(imageWidth, imageHeight, rawData) {
-        this.width = imageWidth;
-        this.height = imageHeight;
-        /**
-         * The layers of the image
-         * @type {SpaceImageFormatLayer[]}
-         */
-        this.layers = this._parse(rawData);
-    }
-
-    /**
-     * Parse raw data in order to build the SIF layers
-     *
-     * @param {string} rawData The raw data of the image
-     * @returns {SpaceImageFormatLayer[]} The layers of an SIF image
-     */
-    _parse(rawData) {
-        const layers = [];
-
-        const layerSize = this.width * this.height;
-        const layerSplit = new RegExp(`(\\d{${layerSize}})`, "g");
-
-        const tempLayers = rawData.split(layerSplit).filter((l) => !!l);
-
-        tempLayers.forEach((layerData) => {
-            layers.push(new SpaceImageFormatLayer(this.width, this.height, layerData));
-        });
-
-        return layers;
-    }
-}
+const SpaceImageFormat = require("../SpaceImageFormat/SpaceImageFormat");
 
 
 const input = {
 const input = {
     width: 25,
     width: 25,

+ 1 - 118
8/8_2.js

@@ -1,121 +1,4 @@
-/* eslint-disable max-classes-per-file */
-
-const { DeepClone } = require("../IntComp/common");
-
-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;
-    }
-}
-
-class SpaceImageFormat {
-    /**
-     * The Space Image Format
-     *
-     * @param {number} imageWidth The width of the image in pixels
-     * @param {number} imageHeight The height of the image in pixels
-     * @param {string} rawData The raw data for the complete SIF image
-     */
-    constructor(imageWidth, imageHeight, rawData) {
-        this.width = imageWidth;
-        this.height = imageHeight;
-        /**
-         * The layers of the image
-         * @type {SpaceImageFormatLayer[]}
-         */
-        this.layers = this._parse(rawData);
-
-        this.PIXEL_DEPTH = ["░", "▓"];
-    }
-
-    /**
-     * Render an image to the console
-     *
-     * Given the simplicity of the image format's color space, we can easily
-     * accomplish this using the "light shade" and "dark shade" ASCII characters.
-     * @returns {void}
-     */
-    Render() {
-        // Will be filled with a string containing the final image
-        let renderedImage = "";
-        // Get the first layer's rows
-        let renderedLayerRows = DeepClone(this.layers[0].rows);
-        // Iterate over subsequent layers
-        for (let i = 1; i < this.layers.length; i++) {
-            const layer = this.layers[i];
-
-            for (let row = 0; row < renderedLayerRows.length; row++) {
-                for (let column = 0; column < renderedLayerRows[row].length; column++) {
-                    if (renderedLayerRows[row][column] == 2 && layer.rows[row][column] !== 2) { renderedLayerRows[row][column] = layer.rows[row][column]; }
-                }
-            }
-
-            // Check if all transparent pixels have been filled in
-            if (renderedLayerRows.flat(Infinity).indexOf(2) == -1) { break; }
-        }
-
-        // Convert each row from an array of 0's and 1's to a string
-        renderedLayerRows = renderedLayerRows.map((row) => row.map((pixelValue) => this.PIXEL_DEPTH[pixelValue]).join(""));
-        // Join all rows with new lines to complete the image
-        renderedImage = renderedLayerRows.join("\n");
-
-        console.log(renderedImage);
-    }
-
-    /**
-     * Parse raw data in order to build the SIF layers
-     *
-     * @param {string} rawData The raw data of the image
-     * @returns {SpaceImageFormatLayer[]} The layers of an SIF image
-     */
-    _parse(rawData) {
-        const layers = [];
-
-        const layerSize = this.width * this.height;
-        const layerSplit = new RegExp(`(\\d{${layerSize}})`, "g");
-
-        const tempLayers = rawData.split(layerSplit).filter((l) => !!l);
-
-        tempLayers.forEach((layerData) => {
-            layers.push(new SpaceImageFormatLayer(this.width, this.height, layerData));
-        });
-
-        return layers;
-    }
-}
+const SpaceImageFormat = require("../SpaceImageFormat/SpaceImageFormat");
 
 
 const input = {
 const input = {
     width: 25,
     width: 25,

+ 78 - 0
SpaceImageFormat/SpaceImageFormat.js

@@ -0,0 +1,78 @@
+const { DeepClone } = require("../IntComp/common");
+const SpaceImageFormatLayer = require("./SpaceImageFormatLayer");
+
+module.exports = class SpaceImageFormat {
+    /**
+     * The Space Image Format
+     *
+     * @param {number} imageWidth The width of the image in pixels
+     * @param {number} imageHeight The height of the image in pixels
+     * @param {string} rawData The raw data for the complete SIF image
+     */
+    constructor(imageWidth, imageHeight, rawData) {
+        this.width = imageWidth;
+        this.height = imageHeight;
+        /**
+         * The layers of the image
+         * @type {SpaceImageFormatLayer[]}
+         */
+        this.layers = this._parse(rawData);
+
+        this.PIXEL_DEPTH = ["░", "▓"];
+    }
+
+    /**
+     * Render an image to the console
+     *
+     * Given the simplicity of the image format's color space, we can easily
+     * accomplish this using the "light shade" and "dark shade" ASCII characters.
+     * @returns {void}
+     */
+    Render() {
+        // Will be filled with a string containing the final image
+        let renderedImage = "";
+        // Get the first layer's rows
+        let renderedLayerRows = DeepClone(this.layers[0].rows);
+        // Iterate over subsequent layers
+        for (let i = 1; i < this.layers.length; i++) {
+            const layer = this.layers[i];
+
+            for (let row = 0; row < renderedLayerRows.length; row++) {
+                for (let column = 0; column < renderedLayerRows[row].length; column++) {
+                    if (renderedLayerRows[row][column] == 2 && layer.rows[row][column] !== 2) { renderedLayerRows[row][column] = layer.rows[row][column]; }
+                }
+            }
+
+            // Check if all transparent pixels have been filled in
+            if (renderedLayerRows.flat(Infinity).indexOf(2) == -1) { break; }
+        }
+
+        // Convert each row from an array of 0's and 1's to a string
+        renderedLayerRows = renderedLayerRows.map((row) => row.map((pixelValue) => this.PIXEL_DEPTH[pixelValue]).join(""));
+        // Join all rows with new lines to complete the image
+        renderedImage = renderedLayerRows.join("\n");
+
+        console.log(renderedImage);
+    }
+
+    /**
+     * Parse raw data in order to build the SIF layers
+     *
+     * @param {string} rawData The raw data of the image
+     * @returns {SpaceImageFormatLayer[]} The layers of an SIF image
+     */
+    _parse(rawData) {
+        const layers = [];
+
+        const layerSize = this.width * this.height;
+        const layerSplit = new RegExp(`(\\d{${layerSize}})`, "g");
+
+        const tempLayers = rawData.split(layerSplit).filter((l) => !!l);
+
+        tempLayers.forEach((layerData) => {
+            layers.push(new SpaceImageFormatLayer(this.width, this.height, layerData));
+        });
+
+        return layers;
+    }
+};

+ 38 - 0
SpaceImageFormat/SpaceImageFormatLayer.js

@@ -0,0 +1,38 @@
+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;
+    }
+};