Advertisement
Markcreator

r/place Crewmate Finder

Apr 3rd, 2022
989
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require("fs");
  2. const { createCanvas, loadImage, Canvas } = require("canvas");
  3.  
  4. // Dimensions for the image
  5. const width = 2000;
  6. const height = 1000;
  7.  
  8. // Instantiate the canvas object
  9. const canvas = createCanvas(width, height);
  10. const context = canvas.getContext("2d");
  11.  
  12. loadImage("./place.png").then((image) => {
  13.     context.drawImage(image, 0, 0);
  14.  
  15.     const pixels = context.getImageData(0, 0, width, height);
  16.     const mask = context.createImageData(width, height);
  17.    
  18.     for (var x = 0; x < width; x++) {
  19.         for (var y = 0; y < height; y++) {
  20.             //var color = getPixel(pixels, x, y);
  21.  
  22.             //if (color.equals(makePixel(255, 69, 0, 255))) {
  23.             //    setPixel(mask, color, x, y);
  24.             //}
  25.  
  26.             detectSus([
  27.                 [0, 0, 0],
  28.                 [0, 1, 1],
  29.                 [0, 0, 0],
  30.                 [0, 0, 0],
  31.                 [0, 2, 0],
  32.             ], 3, 5, 3, x, y, pixels, mask);
  33.  
  34.             detectSus([
  35.                 [0, 0, 0],
  36.                 [1, 1, 0],
  37.                 [0, 0, 0],
  38.                 [0, 0, 0],
  39.                 [0, 2, 0],
  40.             ], 3, 5, 3, x, y, pixels, mask);
  41.  
  42.             detectSus([
  43.                 [0, 0, 0],
  44.                 [1, 1, 0],
  45.                 [0, 0, 0],
  46.                 [0, 2, 0],
  47.             ], 3, 4, 3, x, y, pixels, mask);
  48.  
  49.             detectSus([
  50.                 [0, 0, 0],
  51.                 [0, 1, 1],
  52.                 [0, 0, 0],
  53.                 [0, 2, 0],
  54.             ], 3, 4, 3, x, y, pixels, mask);
  55.         }
  56.     }
  57.  
  58.     context.putImageData(mask, 0, 0);
  59.     const buffer = canvas.toBuffer("image/png");
  60.     fs.writeFileSync("./image.png", buffer);
  61. });
  62.  
  63. function detectSus(shape, shapeWidth, shapeHeight, colorCount, x, y, pixels, mask) {
  64.     var shapeColors = new Array(colorCount);
  65.     var shapeMatched = true;
  66.  
  67.     for (var y2 = 0; y2 < shapeHeight; y2++) {
  68.         for (var x2 = 0; x2 < shapeWidth; x2++) {
  69.             var colorIndex = shape[y2][x2];
  70.             var matchColor = getPixel(pixels, x + x2, y + y2);
  71.  
  72.             if (shapeColors[colorIndex] === undefined && !containsColor(shapeColors, matchColor) && colorIndex >= 0) {
  73.                 shapeColors[colorIndex] = matchColor;
  74.             } else {
  75.                 if (shapeColors[colorIndex] !== undefined && !shapeColors[colorIndex].equals(matchColor)) {
  76.                     shapeMatched = false;
  77.                 }
  78.             }
  79.         }
  80.     }
  81.  
  82.     if (shapeMatched && !shapeColors.includes(undefined)) {
  83.         //console.log(x + " " + y);
  84.  
  85.         for (var y2 = 0; y2 < shapeHeight; y2++) {
  86.             for (var x2 = 0; x2 < shapeWidth; x2++) {
  87.                 var colorIndex = shape[y2][x2];
  88.                 var matchColor = setPixel(mask, shapeColors[colorIndex], x + x2, y + y2);
  89.             }
  90.         }
  91.     }
  92. }
  93.  
  94. // Extensions
  95. function makePixel(r, g, b, a) {
  96.     return [r, g, b, a];
  97. }
  98. getPixel = function (image, x, y) {
  99.     var offset = (y * width + x) * 4;
  100.  
  101.     return [ image.data[offset], image.data[offset + 1], image.data[offset + 2], image.data[offset + 3] ];
  102. }
  103. setPixel = function (image, pixel, x, y) {
  104.     var offset = (y * width + x) * 4;
  105.  
  106.     if (pixel !== undefined) {
  107.         image.data[offset] = pixel[0];
  108.         image.data[offset+1] = pixel[1];
  109.         image.data[offset+2] = pixel[2];
  110.         image.data[offset+3] = pixel[3];
  111.     }
  112. }
  113.  
  114. containsColor = function (array, color) {
  115.     for (var i = 0; i < array.length; i++) {
  116.         var element = array[i];
  117.  
  118.         if (element !== undefined) {
  119.             if (element.equals(color)) {
  120.                 return true;
  121.             }
  122.         }
  123.     }
  124.     return false;
  125. }
  126.  
  127. Array.prototype.equals = function (arr) {
  128.     return this.length == arr.length && this.every((u, i) => u === arr[i]);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement