Yatopia/patches/api/0011-Optimise-Bukkit-s-MapPalette.patch

117 lines
4.3 KiB
Diff
Raw Normal View History

2020-12-02 17:55:59 +01:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: epserv <admin@epserv.ru>
2020-12-03 16:41:57 +01:00
Date: Thu, 3 Dec 2020 20:36:42 +0500
2020-12-02 19:19:50 +01:00
Subject: [PATCH] Optimise Bukkit's MapPalette
2020-12-02 17:55:59 +01:00
diff --git a/src/main/java/org/bukkit/map/MapPalette.java b/src/main/java/org/bukkit/map/MapPalette.java
2020-12-03 17:24:49 +01:00
index 95fe3f4d081053a6cf484e4ef07b474f2dc2ab02..91ff76a8dcac38080afa6a021797ac06fb50d97b 100644
2020-12-02 17:55:59 +01:00
--- a/src/main/java/org/bukkit/map/MapPalette.java
+++ b/src/main/java/org/bukkit/map/MapPalette.java
2020-12-03 17:24:49 +01:00
@@ -22,7 +22,9 @@ public final class MapPalette {
2020-12-03 16:41:57 +01:00
return new Color(r, g, b);
2020-12-02 17:55:59 +01:00
}
2020-12-02 20:41:07 +01:00
2020-12-03 16:41:57 +01:00
- private static double getDistance(@NotNull Color c1, @NotNull Color c2) {
+ // Yatopia start - faster method
+ private static int getDistanceSquared(@NotNull Color c1, @NotNull Color c2) {
2020-12-02 19:19:50 +01:00
+ /*
double rmean = (c1.getRed() + c2.getRed()) / 2.0;
double r = c1.getRed() - c2.getRed();
double g = c1.getGreen() - c2.getGreen();
2020-12-03 17:24:49 +01:00
@@ -31,10 +33,24 @@ public final class MapPalette {
2020-12-02 19:19:50 +01:00
double weightG = 4.0;
double weightB = 2 + (255 - rmean) / 256.0;
return weightR * r * r + weightG * g * g + weightB * b * b;
+ */
+ if (c1.getRGB() == c2.getRGB()) {
2020-12-03 16:41:57 +01:00
+ return 0;
2020-12-02 19:19:50 +01:00
+ }
2020-12-03 16:41:57 +01:00
+ int r = c1.getRed() - c2.getRed(),
2020-12-02 19:19:50 +01:00
+ g = c1.getGreen() - c2.getGreen(),
2020-12-03 16:41:57 +01:00
+ b = c1.getBlue() - c2.getBlue();
+ // because of performed tests, this accuracy is enough
+ return r * r + g * g + b * b;
+ }
2020-12-03 17:24:49 +01:00
+ // getDistance method itself
2020-12-03 16:41:57 +01:00
+ private static double getDistance(@NotNull Color c1, @NotNull Color c2) {
+ return Math.sqrt(getDistanceSquared(c1, c2));
2020-12-02 17:55:59 +01:00
}
2020-12-03 16:41:57 +01:00
+ // Yatopia end
2020-12-02 20:41:07 +01:00
2020-12-02 17:55:59 +01:00
@NotNull
2020-12-03 16:41:57 +01:00
- static final Color[] colors = {
2020-12-03 17:24:49 +01:00
+ static Color[] colors = { // Yatopia
c(0, 0, 0), c(0, 0, 0), c(0, 0, 0), c(0, 0, 0),
c(89, 125, 39), c(109, 153, 48), c(127, 178, 56), c(67, 94, 29),
c(174, 164, 115), c(213, 201, 140), c(247, 233, 163), c(130, 123, 86),
@@ -95,6 +111,19 @@ public final class MapPalette {
c(60, 31, 43), c(74, 37, 53), c(86, 44, 62), c(45, 23, 32),
c(14, 127, 93), c(17, 155, 114), c(20, 180, 133), c(10, 95, 70)
2020-12-03 16:41:57 +01:00
};
2020-12-03 17:24:49 +01:00
+ // Yatopia start
2020-12-03 16:41:57 +01:00
+ @NotNull
+ static int[] rgbColors = new int[colors.length];
+ static {
+ for (short i = 0; i < rgbColors.length; i++) {
+ rgbColors[i] = colors[i].getRGB();
+ }
+ java.util.Arrays.sort(rgbColors);
+ for (short i = 0; i < rgbColors.length; i++) {
+ colors[i] = new Color(rgbColors[i]);
+ }
+ }
2020-12-03 17:24:49 +01:00
+ // Yatopia end
2020-12-03 16:41:57 +01:00
// Interface
/**
2020-12-03 17:24:49 +01:00
@@ -235,6 +264,8 @@ public final class MapPalette {
public static byte matchColor(@NotNull Color color) {
2020-12-02 17:55:59 +01:00
if (color.getAlpha() < 128) return 0;
2020-12-02 20:41:07 +01:00
2020-12-03 17:24:49 +01:00
+ // Yatopia start - use binary search
+ /*
2020-12-03 16:41:57 +01:00
int index = 0;
2020-12-03 17:24:49 +01:00
double best = -1;
2020-12-02 20:41:07 +01:00
2020-12-03 17:24:49 +01:00
@@ -245,9 +276,31 @@ public final class MapPalette {
index = i;
}
}
+ */
+ int index;
2020-12-03 16:41:57 +01:00
+ int binaryIndex = java.util.Arrays.binarySearch(rgbColors, color.getRGB());
+ if (binaryIndex >= 0) {
+ return (byte) (binaryIndex < 128 ? binaryIndex : binaryIndex - 256);
+ }
+ int insertionPoint = binaryIndex * -1 - 1;
+ if (insertionPoint == 0) {
+ index = 0;
+ } else if (insertionPoint == colors.length - 1) {
+ index = colors.length - 1;
+ } else {
+ // now don't loop over all the colors, just select one of neighbor colors
+ // we don't need a distance itself, so we can use distance squared
+ int d1 = getDistanceSquared(color, colors[insertionPoint - 1]), d2 = getDistanceSquared(color, colors[insertionPoint + 1]);
+ if (d1 > d2) {
+ index = insertionPoint + 1;
+ } else {
+ index = insertionPoint - 1;
2020-12-03 17:24:49 +01:00
+ }
+ }
2020-12-03 16:41:57 +01:00
+ // Yatopia end
2020-12-02 20:41:07 +01:00
2020-12-02 17:55:59 +01:00
// Minecraft has 143 colors, some of which have negative byte representations
- return (byte) (index < 128 ? index : -129 + (index - 127));
2020-12-02 19:19:50 +01:00
+ return (byte) (index < 128 ? index : index - 256); // Yatopia
2020-12-02 17:55:59 +01:00
}
/**
2020-12-03 17:24:49 +01:00
@@ -267,4 +320,4 @@ public final class MapPalette {
2020-12-03 16:41:57 +01:00
return colors[index >= 0 ? index : index + 256];
}
}
-}
+}
\ No newline at end of file