Skip to content

Commit c84f603

Browse files
committed
fix colors and image import for 1.16.5
1 parent 2b83012 commit c84f603

3 files changed

Lines changed: 91 additions & 72 deletions

File tree

src/main/java/net/querz/mcmapviewer/DialogHelper.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import net.querz.mcmapviewer.map.MapView;
1111

1212
import javax.imageio.ImageIO;
13+
import java.awt.geom.AffineTransform;
14+
import java.awt.image.AffineTransformOp;
1315
import java.awt.image.BufferedImage;
1416
import java.io.File;
1517
import java.io.IOException;
@@ -30,18 +32,20 @@ public static void openDirectory(Stage primaryStage, FileView fileView) {
3032
}
3133

3234
public static void importImage(Stage primaryStage, MapView mapView) {
33-
File file = createFileChooser(null, new FileChooser.ExtensionFilter("*.png Files", "*.png")).showOpenDialog(primaryStage);
35+
File file = createFileChooser(null, new FileChooser.ExtensionFilter("*.png, *.jpg Files", "*.png", "*.jpg")).showOpenDialog(primaryStage);
3436
if (file != null) {
3537
try {
3638
BufferedImage bufImg = ImageIO.read(file);
3739

40+
bufImg = scaleImage(bufImg, MapView.IMAGE_WIDTH, MapView.IMAGE_HEIGHT);
41+
3842
Image img = SwingFXUtils.toFXImage(bufImg, null);
3943
int[] pixels = new int[MapView.IMAGE_WIDTH * MapView.IMAGE_HEIGHT];
4044
img.getPixelReader().getPixels(0, 0, MapView.IMAGE_WIDTH, MapView.IMAGE_HEIGHT, PixelFormat.getIntArgbPreInstance(), pixels, 0, MapView.IMAGE_WIDTH);
4145

4246
byte[] closest = new byte[pixels.length];
4347
for (int i = 0; i < pixels.length; i++) {
44-
closest[i] = (byte) MapColor.findClosestColor(pixels[i]);
48+
closest[i] = (byte) MapColor.getClosestColorID(pixels[i]);
4549
}
4650

4751
mapView.setImageData(closest);
@@ -102,4 +106,14 @@ public static String getMCDir() {
102106
public static String getHomeDir() {
103107
return System.getProperty("user.home");
104108
}
109+
110+
public static BufferedImage scaleImage(BufferedImage before, int width, int height) {
111+
double w = before.getWidth();
112+
double h = before.getHeight();
113+
BufferedImage after = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
114+
AffineTransform at = new AffineTransform();
115+
at.scale(width / w, height / h);
116+
AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
117+
return scaleOp.filter(before, after);
118+
}
105119
}

src/main/java/net/querz/mcmapviewer/map/MapColor.java

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,58 +6,65 @@
66

77
public enum MapColor {
88

9-
AIR(0, 0x00000000),
10-
GRASS(4, 0xff597d27),
11-
SAND(8, 0xffaea473),
12-
CLOTH(12, 0xff8c8c8c),
13-
TNT(16, 0xffb40000),
14-
ICE(20, 0xff7070b4),
15-
IRON(24, 0xff757575),
16-
FOLIAGE(28, 0xff005700),
17-
SNOW(32, 0xffb4b4b4),
18-
CLAY(36, 0xff737681),
19-
DIRT(40, 0xff6a4c36),
20-
STONE(44, 0xff4f4f4f),
21-
WATER(48, 0xff2d2db4),
22-
WOOD(52, 0xff645432),
23-
QUARTZ(56, 0xffb4b1ac),
24-
ADOBE(60, 0xff985924),
25-
MAGENTA(64, 0xff7d3598),
26-
LIGHT_BLUE(68, 0xff486c98),
27-
YELLOW(72, 0xffa1a124),
28-
LIME(76, 0xff599011),
29-
PINK(80, 0xffaa5974),
30-
GRAY(84, 0xff353535),
31-
SILVER(88, 0xff6c6c6c),
32-
CYAN(92, 0xff35596c),
33-
PURPLE(96, 0xff592c7d),
34-
BLUE(100, 0xff24357d),
35-
BROWN(104, 0xff483524),
36-
GREEN(108, 0xff485924),
37-
RED(112, 0xff6c2424),
38-
BLACK(116, 0xff111111),
39-
GOLD(120, 0xffb0a836),
40-
DIAMOND(124, 0xff409a96),
41-
LAPIS(128, 0xff345ab4),
42-
EMERALD(132, 0xff009928),
43-
OBSIDIAN(136, 0xff5b3c22),
44-
NETHERRACK(140, 0xff4f0100),
45-
WHITE_STAINED_HARDENED_CLAY(144, 0xff937c71),
46-
ORANGE_STAINED_HARDENED_CLAY(148, 0xff703919),
47-
MAGENTA_STAINED_HARDENED_CLAY(152, 0xff693d4c),
48-
LIGHT_BLUE_STAINED_HARDENED_CLAY(156, 0xff4f4c61),
49-
YELLOW_STAINED_HARDENED_CLAY(160, 0xff835d19),
50-
LIME_STAINED_HARDENED_CLAY(164, 0xff485225),
51-
PINK_STAINED_HARDENED_CLAY(168, 0xff703637),
52-
GRAY_STAINED_HARDENED_CLAY(172, 0xff281c18),
53-
SILVER_STAINED_HARDENED_CLAY(176, 0xff5f4b45),
54-
CYAN_STAINED_HARDENED_CLAY(180, 0xff3d4040),
55-
PURPLE_STAINED_HARDENED_CLAY(184, 0xff56333e),
56-
BLUE_STAINED_HARDENED_CLAY(188, 0xff352b40),
57-
BROWN_STAINED_HARDENED_CLAY(192, 0xff352318),
58-
GREEN_STAINED_HARDENED_CLAY(196, 0xff35391d),
59-
RED_STAINED_HARDENED_CLAY(200, 0xff642a20),
60-
BLACK_STAINED_HARDENED_CLAY(204, 0xff1a0f0b);
9+
NONE(0, 0x00000000),
10+
GRASS(4, 0xFF7FB238),
11+
SAND(8, 0xFFF7E9A3),
12+
WOOL(12, 0xFFC7C7C7),
13+
FIRE(16, 0xFFFF0000),
14+
ICE(20, 0xFFA0A0FF),
15+
METAL(24, 0xFFA7A7A7),
16+
PLANT(28, 0xFF007C00),
17+
SNOW(32, 0xFFFFFFFF),
18+
CLAY(36, 0xFFA4A8B8),
19+
DIRT(40, 0xFF976D4D),
20+
STONE(44, 0xFF707070),
21+
WATER(48, 0xFF4040FF),
22+
WOOD(52, 0xFF8F7748),
23+
QUARTZ(56, 0xFFFFFCF5),
24+
COLOR_ORANGE(60, 0xFFD87F33),
25+
COLOR_MAGENTA(64, 0xFFB24CD8),
26+
COLOR_LIGHT_BLUE(68, 0xFF6699D8),
27+
COLOR_YELLOW(72, 0xFFE5E533),
28+
COLOR_LIGHT_GREEN(76, 0xFF7FCC19),
29+
COLOR_PINK(80, 0xFFF27FA5),
30+
COLOR_GRAY(84, 0xFF4C4C4C),
31+
COLOR_LIGHT_GRAY(88, 0xFF999999),
32+
COLOR_CYAN(92, 0xFF4C7F99),
33+
COLOR_PURPLE(96, 0xFF7F3FB2),
34+
COLOR_BLUE(100, 0xFF334CB2),
35+
COLOR_BROWN(104, 0xFF664C33),
36+
COLOR_GREEN(108, 0xFF667F33),
37+
COLOR_RED(112, 0xFF993333),
38+
COLOR_BLACK(116, 0xFF191919),
39+
GOLD(120, 0xFFFAEE4D),
40+
DIAMOND(124, 0xFF5CDBD5),
41+
LAPIS(128, 0xFF4A80FF),
42+
EMERALD(132, 0xFF00D93A),
43+
PODZOL(136, 0xFF815631),
44+
NETHER(140, 0xFF700200),
45+
TERRACOTTA_WHITE(144, 0xFFD1B1A1),
46+
TERRACOTTA_ORANGE(148, 0xFF9F5224),
47+
TERRACOTTA_MAGENTA(152, 0xFF95576C),
48+
TERRACOTTA_LIGHT_BLUE(156, 0xFF706C8A),
49+
TERRACOTTA_YELLOW(160, 0xFFBA8524),
50+
TERRACOTTA_LIGHT_GREEN(164, 0xFF677535),
51+
TERRACOTTA_PINK(168, 0xFFA04D4E),
52+
TERRACOTTA_GRAY(172, 0xFF392923),
53+
TERRACOTTA_LIGHT_GRAY(176, 0xFF876B62),
54+
TERRACOTTA_CYAN(180, 0xFF575C5C),
55+
TERRACOTTA_PURPLE(184, 0xFF7A4958),
56+
TERRACOTTA_BLUE(188, 0xFF4C3E5C),
57+
TERRACOTTA_BROWN(192, 0xFF4C3223),
58+
TERRACOTTA_GREEN(196, 0xFF4C522A),
59+
TERRACOTTA_RED(200, 0xFF8E3C2E),
60+
TERRACOTTA_BLACK(204, 0xFF251610),
61+
CRIMSON_NYLIUM(208, 0xFFBD3031),
62+
CRIMSON_STEM(212, 0xFF943F61),
63+
CRIMSON_HYPHAE(216, 0xFF5C191D),
64+
WARPED_NYLIUM(220, 0xFF167E86),
65+
WARPED_STEM(224, 0xFF3A8E8C),
66+
WARPED_HYPHAE(228, 0xFF562C3E),
67+
WARPED_WART_BLOCK(232, 0xFF14B485);
6168

6269
private int id;
6370
private int color;
@@ -66,7 +73,7 @@ public enum MapColor {
6673
private static Map<Integer, Integer> ids = new HashMap<>();
6774
private static Map<Integer, Color> javaFXColors = new HashMap<>();
6875

69-
private static final int[] mul = new int[]{180, 220, 255, 235};
76+
private static final int[] mul = new int[]{180, 220, 255, 135};
7077

7178
static {
7279
for (MapColor mc : MapColor.values()) {
@@ -110,23 +117,25 @@ public static int getClosestColorID(int color) {
110117
}
111118

112119
public static int findClosestColor(int color) {
120+
if (color >> 24 == 0) {
121+
return MapColor.NONE.color;
122+
}
113123
int r = color >> 16 & 0xFF;
114124
int g = color >> 8 & 0xFF;
115125
int b = color & 0xFF;
116126

117-
int closest = 0x00000000;
118-
int closestDist = Integer.MAX_VALUE;
127+
int closest = 0xFFFFFFFF;
128+
double closestDist = Integer.MAX_VALUE;
119129

120-
for(int i : colors.values()) {
130+
for (int i : colors.values()) {
131+
if (i >> 24 == 0) {
132+
continue;
133+
}
121134
int mr = i >> 16 & 0xFF;
122135
int mg = i >> 8 & 0xFF;
123136
int mb = i & 0xFF;
124137

125-
int dr = Math.abs(mr - r);
126-
int dg = Math.abs(mg - g);
127-
int db = Math.abs(mb - b);
128-
129-
int dist = dr + dg + db;
138+
double dist = Math.pow((mr - r) * 0.3, 2) + Math.pow((mg - g) * 0.59, 2) + Math.pow((mb - b) * 0.11, 2);
130139

131140
if (dist < closestDist) {
132141
if (dist == 0) {

src/main/java/net/querz/mcmapviewer/map/MapView.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import net.querz.nbt.io.NBTUtil;
4949
import net.querz.nbt.tag.StringTag;
5050

51-
import java.io.File;
5251
import java.io.IOException;
5352
import java.util.ArrayList;
5453
import java.util.List;
@@ -336,30 +335,27 @@ public void clear() {
336335
}
337336

338337
private void readFile() throws IOException {
339-
340-
341-
342338
NamedTag tag = NBTUtil.read(mapFile.getFile());
343339
if (tag == null || !(tag.getTag() instanceof CompoundTag)) {
344340
throw new IOException("expected root to be CompoundTag, got " + (tag == null ? "null" : tag.getClass().getSimpleName()));
345341
}
346342

347343
root = (CompoundTag) tag.getTag();
348344
System.out.println(root);
349-
CompoundTag data = catchClassCastException(() -> root.getCompoundTag("data"));
345+
CompoundTag data = catchException(() -> root.getCompoundTag("data"));
350346
if (data == null) {
351347
throw new IOException("unable to parse data tag");
352348
}
353349

354-
ListTag<CompoundTag> banners = catchClassCastException(() -> data.getListTag("banners").asCompoundTagList());
350+
ListTag<CompoundTag> banners = catchException(() -> data.getListTag("banners").asCompoundTagList());
355351
if (banners != null) {
356352
for (CompoundTag banner : banners) {
357353
MapIconData mapIconData = new MapIconData(banner.getString("Name"), banner.getString("Color"), parsePos(banner.getCompoundTag("Pos")));
358354
this.banners.add(mapIconData);
359355
}
360356
}
361357

362-
ListTag<CompoundTag> frames = catchClassCastException(() -> data.getListTag("frames").asCompoundTagList());
358+
ListTag<CompoundTag> frames = catchException(() -> data.getListTag("frames").asCompoundTagList());
363359
if (frames != null) {
364360
for (CompoundTag frame : frames) {
365361
FrameData frameData = new FrameData(frame.getInt("EntityId"), frame.getInt("Rotation"), parsePos(frame.getCompoundTag("Pos")));
@@ -485,10 +481,10 @@ public void save() {
485481
}
486482
}
487483

488-
private <T> T catchClassCastException(Supplier<T> s) {
484+
private <T> T catchException(Supplier<T> s) {
489485
try {
490486
return s.get();
491-
} catch (ClassCastException ex) {
487+
} catch (Exception ex) {
492488
System.out.println("error parsing value: " + ex.getMessage());
493489
return null;
494490
}

0 commit comments

Comments
 (0)