From c1236dbc0e13c7fa5d0924d24ef2b06280d0cbf7 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 27 Mar 2022 01:11:56 +0100 Subject: [PATCH] Update CustomMap --- src/de/steamwar/lobby/map/CustomMap.java | 41 ++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/de/steamwar/lobby/map/CustomMap.java b/src/de/steamwar/lobby/map/CustomMap.java index 6bf8536..e9003d5 100644 --- a/src/de/steamwar/lobby/map/CustomMap.java +++ b/src/de/steamwar/lobby/map/CustomMap.java @@ -30,6 +30,7 @@ import org.bukkit.map.MapPalette; import java.awt.*; import java.awt.image.BufferedImage; +import java.awt.image.WritableRaster; import java.lang.reflect.Field; public class CustomMap { @@ -42,6 +43,46 @@ public class CustomMap { throw new IllegalArgumentException("Image height must be a multiple of 128"); } + WritableRaster raster = image.getRaster(); + int numBands = raster.getNumBands(); + int[] pixels = raster.getPixels(0, 0, image.getWidth(), image.getHeight(), new int[image.getWidth() * image.getHeight() * numBands]); + for (int y = 0; y < image.getHeight(); y++) { + for (int x = 0; x < image.getWidth(); x++) { + int red = pixels[(y * image.getWidth() + x) * numBands]; + int green = pixels[(y * image.getWidth() + x) * numBands + 1]; + int blue = pixels[(y * image.getWidth() + x) * numBands + 2]; + Color nearest = getNearest(new Color(red, green, blue)); + pixels[(y * image.getWidth() + x) * numBands] = nearest.getRed(); + pixels[(y * image.getWidth() + x) * numBands + 1] = nearest.getGreen(); + pixels[(y * image.getWidth() + x) * numBands + 2] = nearest.getBlue(); + + int quantErrorRed = red - nearest.getRed(); + int quantErrorGreen = green - nearest.getGreen(); + int quantErrorBlue = blue - nearest.getBlue(); + if (x < image.getWidth() - 1) { + pixels[(y * image.getWidth() + x + 1) * numBands] = clamp(pixels[(y * image.getWidth() + x + 1) * numBands] + quantErrorRed * 7 / 16); + pixels[(y * image.getWidth() + x + 1) * numBands + 1] = clamp(pixels[(y * image.getWidth() + x + 1) * numBands + 1] + quantErrorGreen * 7 / 16); + pixels[(y * image.getWidth() + x + 1) * numBands + 2] = clamp(pixels[(y * image.getWidth() + x + 1) * numBands + 2] + quantErrorBlue * 7 / 16); + } + if (x > 0) { + pixels[(y * image.getWidth() + x - 1) * numBands] = clamp(pixels[(y * image.getWidth() + x - 1) * numBands] + quantErrorRed * 3 / 16); + pixels[(y * image.getWidth() + x - 1) * numBands + 1] = clamp(pixels[(y * image.getWidth() + x - 1) * numBands + 1] + quantErrorGreen * 3 / 16); + pixels[(y * image.getWidth() + x - 1) * numBands + 2] = clamp(pixels[(y * image.getWidth() + x - 1) * numBands + 2] + quantErrorBlue * 3 / 16); + } + if (y < image.getHeight() - 1) { + pixels[((y + 1) * image.getWidth() + x) * numBands] = clamp(pixels[((y + 1) * image.getWidth() + x) * numBands] + quantErrorRed * 5 / 16); + pixels[((y + 1) * image.getWidth() + x) * numBands + 1] = clamp(pixels[((y + 1) * image.getWidth() + x) * numBands + 1] + quantErrorGreen * 5 / 16); + pixels[((y + 1) * image.getWidth() + x) * numBands + 2] = clamp(pixels[((y + 1) * image.getWidth() + x) * numBands + 2] + quantErrorBlue * 5 / 16); + if (x < image.getWidth() - 1) { + pixels[((y + 1) * image.getWidth() + x + 1) * numBands] = clamp(pixels[((y + 1) * image.getWidth() + x + 1) * numBands] + quantErrorRed / 16); + pixels[((y + 1) * image.getWidth() + x + 1) * numBands + 1] = clamp(pixels[((y + 1) * image.getWidth() + x + 1) * numBands + 1] + quantErrorGreen / 16); + pixels[((y + 1) * image.getWidth() + x + 1) * numBands + 2] = clamp(pixels[((y + 1) * image.getWidth() + x + 1) * numBands + 2] + quantErrorBlue / 16); + } + } + } + } + raster.setPixels(0, 0, image.getWidth(), image.getHeight(), pixels); + BufferedImage bufferedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); for (int y = 0; y < image.getHeight(); y++) { for (int x = 0; x < image.getWidth(); x++) {