Skip to content
Permalink
Browse files

Add zoomed out tile generation

  • Loading branch information...
ferrybig committed Mar 6, 2019
1 parent b028136 commit f6849261d22acb5083174cadb32a746bdb0cf47c
@@ -9,5 +9,6 @@

public static void main(String[] artgs) throws Exception {
Main.main(new String[]{"-i", "C:\\Users\\Fernando\\AppData\\Roaming\\.minecraft\\saves\\mesaSee", "-o", "renderTest/"});
//Main.main(new String[]{"-i", "C:\\Users\\Fernando\\AppData\\Roaming\\.minecraft\\saves\\Debug", "-o", "renderTest/"});
}
}
@@ -9,10 +9,15 @@
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;
import javax.imageio.ImageIO;

/**
@@ -25,6 +30,7 @@
private final Path images;
private final int normalZoom = 10;
private final int normalRes = 256;
private final Set<ImageEntry> zoomedOutLayers = new HashSet<>();

public ComplexImageOutputRenderer(RegionRenderer renderer, Path outputDir) {
super(renderer);
@@ -50,7 +56,59 @@ private Path getImageLocation(int zoom, int x, int z) throws IOException {

@Override
public void finishRender() throws IOException {

Set<ImageEntry> pendingTiles = this.zoomedOutLayers;
final int halfRes = this.normalRes / 2;
int zoomLevel = normalZoom - 1;
BufferedImage renderResult = new BufferedImage(this.normalRes, this.normalRes, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2 = renderResult.createGraphics();
g2.setComposite(AlphaComposite.Src);
int[] emptyArray = renderResult.getRGB(0, 0, this.normalRes, this.normalRes, null, 0, this.normalRes);
try {
while (zoomLevel > 1) {
Set<ImageEntry> newTiles = new HashSet<>();
for (ImageEntry entry : pendingTiles) {
renderResult.setRGB(0, 0, this.normalRes, this.normalRes, emptyArray, 0, this.normalRes);
int found = 0;
for (int x = 0; x < 2; x++) {
for (int z = 0; z < 2; z++) {
try (InputStream in = Files.newInputStream(getImageLocation(
zoomLevel + 1,
entry.getX() * 2 + x,
entry.getZ() * 2 + z
))) {
final BufferedImage img = ImageIO.read(in);
g2.drawImage(
img,
x * halfRes, z * halfRes,
x * halfRes + halfRes, z * halfRes + halfRes,
0, 0,
this.normalRes, this.normalRes,
null
);
found++;
} catch (FileNotFoundException | NoSuchFileException ignore) {
}
}
}
assert found > 0 : "At least 1 image should exist in the layer, otherwise this entry should have never existed in this set";
final Path imageLocation = getImageLocation(
zoomLevel,
entry.getX(),
entry.getZ()
);
System.err.println("Rendering zoom level " + zoomLevel + ", tile: " + entry.getX() + ", " + entry.getZ() + " (" + found + "/4)");
System.err.println(imageLocation);
try (OutputStream out = Files.newOutputStream(imageLocation)) {
ImageIO.write(renderResult, "gif", out);
}
newTiles.add(new ImageEntry(Math.floorDiv(entry.getX(), 2), Math.floorDiv(entry.getZ(), 2)));
}
pendingTiles = newTiles;
zoomLevel -= 1;
}
} finally {
g2.dispose();
}
}

@Override
@@ -69,9 +127,9 @@ protected void addImage(BufferedImage tile, int x, int z) throws IOException {
Graphics2D g2 = renderResult.createGraphics();
g2.setComposite(AlphaComposite.Src);
try {
for(;adjustedImageWidth >= this.normalRes; adjustedImageWidth /= 2, tilesToGenerate *= 2, currentZoom++) {
for(int tileX = 0; tileX < tilesToGenerate; tileX++) {
for(int tileZ = 0; tileZ < tilesToGenerate; tileZ++) {
for (; adjustedImageWidth >= this.normalRes; adjustedImageWidth /= 2, tilesToGenerate *= 2, currentZoom++) {
for (int tileX = 0; tileX < tilesToGenerate; tileX++) {
for (int tileZ = 0; tileZ < tilesToGenerate; tileZ++) {
g2.drawImage(
tile,
// dst x1,y1
@@ -94,18 +152,79 @@ protected void addImage(BufferedImage tile, int x, int z) throws IOException {
try (OutputStream out = Files.newOutputStream(imageLocation)) {
ImageIO.write(renderResult, "gif", out);
}
if (adjustedImageWidth == imageWidth) {
// This image is part of the "main" layer, and will be used to generate the more zoomed out layer
zoomedOutLayers.add(new ImageEntry(Math.floorDiv(x, 2), Math.floorDiv(z, 2)));
}
}
}
if (currentZoom > 11) {
break; // DEBUG
}
}
} finally {
g2.dispose();
}

}

@Override
protected void addLevelDat(CompoundTag level) throws IOException {

}

private static class ImageEntry {

private final int x;
private final int z;

public ImageEntry(int x, int z) {
this.x = x;
this.z = z;
}

public int getX() {
return x;
}

public int getZ() {
return z;
}

@Override
public int hashCode() {
int hash = 7;
hash = 43 * hash + this.x;
hash = 43 * hash + this.z;
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ImageEntry other = (ImageEntry) obj;
if (this.x != other.x) {
return false;
}
if (this.z != other.z) {
return false;
}
return true;
}

@Override
public String toString() {
return "ImageEntry{" + "x=" + x + ", z=" + z + '}';
}

}

}

0 comments on commit f684926

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.