Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require("fs");
- const { createCanvas, loadImage, Canvas } = require("canvas");
- // Dimensions for the image
- const width = 2000;
- const height = 1000;
- // Instantiate the canvas object
- const canvas = createCanvas(width, height);
- const context = canvas.getContext("2d");
- loadImage("./place.png").then((image) => {
- context.drawImage(image, 0, 0);
- const pixels = context.getImageData(0, 0, width, height);
- const mask = context.createImageData(width, height);
- for (var x = 0; x < width; x++) {
- for (var y = 0; y < height; y++) {
- //var color = getPixel(pixels, x, y);
- //if (color.equals(makePixel(255, 69, 0, 255))) {
- // setPixel(mask, color, x, y);
- //}
- detectSus([
- [0, 0, 0],
- [0, 1, 1],
- [0, 0, 0],
- [0, 0, 0],
- [0, 2, 0],
- ], 3, 5, 3, x, y, pixels, mask);
- detectSus([
- [0, 0, 0],
- [1, 1, 0],
- [0, 0, 0],
- [0, 0, 0],
- [0, 2, 0],
- ], 3, 5, 3, x, y, pixels, mask);
- detectSus([
- [0, 0, 0],
- [1, 1, 0],
- [0, 0, 0],
- [0, 2, 0],
- ], 3, 4, 3, x, y, pixels, mask);
- detectSus([
- [0, 0, 0],
- [0, 1, 1],
- [0, 0, 0],
- [0, 2, 0],
- ], 3, 4, 3, x, y, pixels, mask);
- }
- }
- context.putImageData(mask, 0, 0);
- const buffer = canvas.toBuffer("image/png");
- fs.writeFileSync("./image.png", buffer);
- });
- function detectSus(shape, shapeWidth, shapeHeight, colorCount, x, y, pixels, mask) {
- var shapeColors = new Array(colorCount);
- var shapeMatched = true;
- for (var y2 = 0; y2 < shapeHeight; y2++) {
- for (var x2 = 0; x2 < shapeWidth; x2++) {
- var colorIndex = shape[y2][x2];
- var matchColor = getPixel(pixels, x + x2, y + y2);
- if (shapeColors[colorIndex] === undefined && !containsColor(shapeColors, matchColor) && colorIndex >= 0) {
- shapeColors[colorIndex] = matchColor;
- } else {
- if (shapeColors[colorIndex] !== undefined && !shapeColors[colorIndex].equals(matchColor)) {
- shapeMatched = false;
- }
- }
- }
- }
- if (shapeMatched && !shapeColors.includes(undefined)) {
- //console.log(x + " " + y);
- for (var y2 = 0; y2 < shapeHeight; y2++) {
- for (var x2 = 0; x2 < shapeWidth; x2++) {
- var colorIndex = shape[y2][x2];
- var matchColor = setPixel(mask, shapeColors[colorIndex], x + x2, y + y2);
- }
- }
- }
- }
- // Extensions
- function makePixel(r, g, b, a) {
- return [r, g, b, a];
- }
- getPixel = function (image, x, y) {
- var offset = (y * width + x) * 4;
- return [ image.data[offset], image.data[offset + 1], image.data[offset + 2], image.data[offset + 3] ];
- }
- setPixel = function (image, pixel, x, y) {
- var offset = (y * width + x) * 4;
- if (pixel !== undefined) {
- image.data[offset] = pixel[0];
- image.data[offset+1] = pixel[1];
- image.data[offset+2] = pixel[2];
- image.data[offset+3] = pixel[3];
- }
- }
- containsColor = function (array, color) {
- for (var i = 0; i < array.length; i++) {
- var element = array[i];
- if (element !== undefined) {
- if (element.equals(color)) {
- return true;
- }
- }
- }
- return false;
- }
- Array.prototype.equals = function (arr) {
- return this.length == arr.length && this.every((u, i) => u === arr[i]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement