|
| 1 | +package com.seiferware.java.worldreader; |
| 2 | + |
| 3 | +import com.google.protobuf.CodedInputStream; |
| 4 | +import org.omg.CORBA.DoubleHolder; |
| 5 | +import org.omg.CORBA.IntHolder; |
| 6 | + |
| 7 | +import javax.imageio.ImageIO; |
| 8 | +import java.awt.image.BufferedImage; |
| 9 | +import java.io.File; |
| 10 | +import java.io.FileInputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.stream.Stream; |
| 15 | + |
| 16 | +public class Main { |
| 17 | + private static final String PATH_ROOT = "C:\\software\\apps\\worldengine\\worlds\\"; |
| 18 | + private static void createImage(String path, WorldOuterClass.World.DoubleMatrix data) throws IOException { |
| 19 | + createImage(path, data, ColorCalculator.DEFAULT); |
| 20 | + } |
| 21 | + private static void createImage(String path, WorldOuterClass.World.DoubleMatrix data, ColorCalculator calc) throws IOException { |
| 22 | + DoubleHolder min = new DoubleHolder(Double.MAX_VALUE); |
| 23 | + DoubleHolder max = new DoubleHolder(Double.MIN_VALUE); |
| 24 | + data.getRowsList().stream().flatMap(d -> d.getCellsList().stream()).forEach(d -> { |
| 25 | + if(min.value > d) { |
| 26 | + min.value = d; |
| 27 | + } |
| 28 | + if(max.value < d) { |
| 29 | + max.value = d; |
| 30 | + } |
| 31 | + }); |
| 32 | + BufferedImage bim = new BufferedImage(data.getRows(0).getCellsCount(), data.getRowsCount(), BufferedImage.TYPE_INT_RGB); |
| 33 | + for(int y = 0; y < data.getRowsCount(); y++) { |
| 34 | + WorldOuterClass.World.DoubleRow row = data.getRows(y); |
| 35 | + for(int x = 0; x < row.getCellsCount(); x++) { |
| 36 | + int color = calc.getColorForValue(row.getCells(x), min.value, max.value); |
| 37 | + bim.setRGB(x, y, color); |
| 38 | + } |
| 39 | + } |
| 40 | + ImageIO.write(bim, "png", new File(path)); |
| 41 | + } |
| 42 | + private static void getDistribution(WorldOuterClass.World.DoubleMatrix data, int precision) { |
| 43 | + DoubleHolder min = new DoubleHolder(Double.MAX_VALUE); |
| 44 | + DoubleHolder max = new DoubleHolder(Double.MIN_VALUE); |
| 45 | + IntHolder minInt = new IntHolder(); |
| 46 | + IntHolder maxInt = new IntHolder(); |
| 47 | + Map<Integer, IntHolder> values = new HashMap<>(); |
| 48 | + data.getRowsList().stream().flatMap(d -> d.getCellsList().stream()).forEach(d -> { |
| 49 | + int intVal = Math.toIntExact(Math.round(d * precision)); |
| 50 | + if(min.value > d) { |
| 51 | + min.value = d; |
| 52 | + minInt.value = intVal; |
| 53 | + } |
| 54 | + if(max.value < d) { |
| 55 | + max.value = d; |
| 56 | + maxInt.value = intVal; |
| 57 | + } |
| 58 | + if(!values.containsKey(intVal)) { |
| 59 | + values.put(intVal, new IntHolder(0)); |
| 60 | + } |
| 61 | + values.get(intVal).value++; |
| 62 | + }); |
| 63 | + IntHolder zero = new IntHolder(0); |
| 64 | + for(int i = minInt.value; i <= maxInt.value; i++) { |
| 65 | + System.out.println(i / (float) precision + "\t" + values.getOrDefault(i, zero).value); |
| 66 | + } |
| 67 | + } |
| 68 | + public static void main(String[] args) { |
| 69 | + if(args.length == 1) { |
| 70 | + File f = new File(args[0]); |
| 71 | + if(f.exists()) { |
| 72 | + if(f.isDirectory()) { |
| 73 | + processDirectory(f); |
| 74 | + } else { |
| 75 | + processFile(f.getAbsolutePath()); |
| 76 | + } |
| 77 | + } else { |
| 78 | + System.out.println("Could not locate specified file or directory."); |
| 79 | + } |
| 80 | + } else if(args.length == 0) { |
| 81 | + processDirectory(new File(".")); |
| 82 | + } else { |
| 83 | + System.out.println("The only valid command line argument is either a world file, or a directory with world files. If no argument is specified, the current working directory will be used."); |
| 84 | + } |
| 85 | + } |
| 86 | + private static void processDirectory(File dir) { |
| 87 | + File[] files = dir.listFiles((dir1, name) -> name.endsWith(".world")); |
| 88 | + if(files != null && files.length > 0) { |
| 89 | + Stream.of(files).forEach(file -> { |
| 90 | + System.out.println("Processing " + file.getName()); |
| 91 | + processFile(file.getAbsolutePath()); |
| 92 | + }); |
| 93 | + } else { |
| 94 | + System.out.println("Did not find any *.world files in specified directory."); |
| 95 | + } |
| 96 | + } |
| 97 | + private static void processFile(String fileName) { |
| 98 | + processFile(fileName, fileName.endsWith(".world") ? fileName.substring(0, fileName.length() - 6) : fileName); |
| 99 | + } |
| 100 | + private static void processFile(String fileName, String baseFileName) { |
| 101 | + WorldOuterClass.World world; |
| 102 | + try (FileInputStream fis = new FileInputStream(new File(fileName))) { |
| 103 | + CodedInputStream cis = CodedInputStream.newInstance(fis); |
| 104 | + world = WorldOuterClass.World.parseFrom(cis); |
| 105 | + } catch (IOException e) { |
| 106 | + System.err.println("An error occurred: " + e.getMessage()); |
| 107 | + return; |
| 108 | + } |
| 109 | + //getDistribution(world.getPrecipitationData(), 10); |
| 110 | + try { |
| 111 | + createImage(baseFileName + "-height.png", world.getHeightMapData()); |
| 112 | + createImage(baseFileName + "-rain.png", world.getPrecipitationData()); |
| 113 | + createImage(baseFileName + "-temp.png", world.getTemperatureData()); |
| 114 | + createImage(baseFileName + "-rivers.png", world.getRivermap(), (value, minValue, maxValue) -> { |
| 115 | + if(value == 0) { |
| 116 | + return 0; |
| 117 | + } |
| 118 | + return ColorCalculator.DEFAULT.getColorForValue(value, minValue, maxValue) | 255; |
| 119 | + }); |
| 120 | + } catch (IOException e) { |
| 121 | + System.err.println("An error occurred: " + e.getMessage()); |
| 122 | + } |
| 123 | + } |
| 124 | + private interface ColorCalculator { |
| 125 | + ColorCalculator DEFAULT = (value, minValue, maxValue) -> { |
| 126 | + int result = (int) Math.max(0, Math.min(255, Math.round((value - minValue) * 255 / (maxValue - minValue)))); |
| 127 | + return (result << 16) | (result << 8) | (result); |
| 128 | + }; |
| 129 | + int getColorForValue(double value, double minValue, double maxValue); |
| 130 | + } |
| 131 | +} |
0 commit comments