-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCompositeTileGenerator.java
More file actions
184 lines (155 loc) · 6.81 KB
/
Copy pathCompositeTileGenerator.java
File metadata and controls
184 lines (155 loc) · 6.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.easywebmap.map;
import com.easywebmap.EasyWebMap;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
/**
* Generates composite tiles at zoomed-out levels using raw pixel compositing.
* Eliminates expensive PNG decode by caching raw RGB pixels.
*/
public class CompositeTileGenerator {
private final EasyWebMap plugin;
private final TileManager tileManager;
private final ConcurrentHashMap<String, int[]> pixelCache;
private static final int MAX_PIXEL_CACHE = 512;
// Thread-local ImageWriter for fast PNG encoding
private static final ThreadLocal<ImageWriter> PNG_WRITER = ThreadLocal.withInitial(() -> {
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("png");
return writers.hasNext() ? writers.next() : null;
});
public CompositeTileGenerator(EasyWebMap plugin, TileManager tileManager) {
this.plugin = plugin;
this.tileManager = tileManager;
this.pixelCache = new ConcurrentHashMap<>();
}
public int getChunksPerAxis(int zoom) {
if (zoom >= 0) return 1;
return 1 << (-zoom);
}
public CompletableFuture<byte[]> generateCompositeTile(String worldName, int zoom, int tileX, int tileZ) {
if (zoom >= 0) {
return this.tileManager.getBaseTile(worldName, tileX, tileZ);
}
int chunksPerAxis = getChunksPerAxis(zoom);
int tileSize = this.plugin.getConfig().getTileSize();
int baseChunkX = tileX * chunksPerAxis;
int baseChunkZ = tileZ * chunksPerAxis;
// Early bail-out: if no chunks in this area are explored, return empty tile immediately
// This saves fetching 64+ base tiles for unexplored areas
if (!this.tileManager.hasAnyExploredChunks(worldName, baseChunkX, baseChunkZ, chunksPerAxis)) {
return CompletableFuture.completedFuture(PngEncoder.encodeEmpty(tileSize));
}
// Fetch all base tiles with pixels in parallel
List<CompletableFuture<TileWithPosition>> futures = new ArrayList<>();
for (int dz = 0; dz < chunksPerAxis; dz++) {
for (int dx = 0; dx < chunksPerAxis; dx++) {
int chunkX = baseChunkX + dx;
int chunkZ = baseChunkZ + dz;
final int posX = dx;
final int posZ = dz;
CompletableFuture<TileWithPosition> tileFuture =
this.tileManager.getBaseTileWithPixels(worldName, chunkX, chunkZ)
.thenApply(data -> new TileWithPosition(data, posX, posZ));
futures.add(tileFuture);
}
}
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.thenApply(v -> {
List<TileWithPosition> tiles = new ArrayList<>();
for (CompletableFuture<TileWithPosition> future : futures) {
tiles.add(future.join());
}
return this.compositeFromPixels(tiles, chunksPerAxis, tileSize);
});
}
/**
* Composite using raw pixel arrays - no PNG decoding needed.
*/
private byte[] compositeFromPixels(List<TileWithPosition> tiles, int chunksPerAxis, int outputSize) {
int[] compositePixels = new int[outputSize * outputSize];
int subTileSize = outputSize / chunksPerAxis;
boolean hasAnyContent = false;
for (TileWithPosition tile : tiles) {
if (tile.data == null || tile.data.pixels == null) {
continue;
}
int[] srcPixels = tile.data.pixels;
int srcSize = tile.data.size;
if (srcPixels.length == 0) continue;
hasAnyContent = true;
int destX = tile.posX * subTileSize;
int destY = tile.posZ * subTileSize;
// Scale and copy pixels directly
float scale = (float) srcSize / subTileSize;
for (int y = 0; y < subTileSize; y++) {
int destRowStart = (destY + y) * outputSize + destX;
int srcY = Math.min((int) (y * scale), srcSize - 1);
int srcRowStart = srcY * srcSize;
for (int x = 0; x < subTileSize; x++) {
int srcX = Math.min((int) (x * scale), srcSize - 1);
compositePixels[destRowStart + x] = srcPixels[srcRowStart + srcX];
}
}
}
if (!hasAnyContent) {
return PngEncoder.encodeEmpty(outputSize);
}
// ARGB: present sub-tiles are opaque, missing/unexplored sub-chunks stay
// at 0 (fully transparent) instead of opaque black — so composite gaps
// show the map background seamlessly rather than black blocks.
BufferedImage composite = new BufferedImage(outputSize, outputSize, BufferedImage.TYPE_INT_ARGB);
composite.setRGB(0, 0, outputSize, outputSize, compositePixels, 0, outputSize);
return encodeFast(composite, outputSize);
}
/**
* Fast PNG encoding with minimal compression for composites.
*/
private byte[] encodeFast(BufferedImage image, int outputSize) {
ByteArrayOutputStream out = new ByteArrayOutputStream(outputSize * outputSize / 2);
ImageWriter writer = PNG_WRITER.get();
if (writer == null) {
try {
ImageIO.write(image, "png", out);
} catch (IOException e) {
return PngEncoder.encodeEmpty(outputSize);
}
return out.toByteArray();
}
try (ImageOutputStream ios = ImageIO.createImageOutputStream(out)) {
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
if (param.canWriteCompressed()) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(1.0f); // Fastest
}
writer.write(null, new IIOImage(image, null, null), param);
writer.reset();
} catch (IOException e) {
return PngEncoder.encodeEmpty(outputSize);
}
return out.toByteArray();
}
public void clearPixelCache() {
this.pixelCache.clear();
}
public static class TileWithPosition {
final PngEncoder.TileData data;
final int posX;
final int posZ;
TileWithPosition(PngEncoder.TileData data, int posX, int posZ) {
this.data = data;
this.posX = posX;
this.posZ = posZ;
}
}
}