123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- const { DeepClone } = require("../IntComp/common");
- const SpaceImageFormatLayer = require("./SpaceImageFormatLayer");
- module.exports = class SpaceImageFormat {
-
- constructor(imageWidth, imageHeight, rawData) {
- this.width = imageWidth;
- this.height = imageHeight;
-
- this.layers = this._parse(rawData);
- this.PIXEL_DEPTH = ["░", "▓"];
- }
-
- Render() {
-
- let renderedImage = "";
-
- let renderedLayerRows = DeepClone(this.layers[0].rows);
-
- 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]; }
- }
- }
-
- if (renderedLayerRows.flat(Infinity).indexOf(2) == -1) { break; }
- }
-
- renderedLayerRows = renderedLayerRows.map((row) => row.map((pixelValue) => this.PIXEL_DEPTH[pixelValue]).join(""));
-
- renderedImage = renderedLayerRows.join("\n");
- console.log(renderedImage);
- }
-
- _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;
- }
- };
|