|
@@ -0,0 +1,186 @@
|
|
|
+package speedtester;
|
|
|
+
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Random;
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+
|
|
|
+public class HighMap
|
|
|
+{
|
|
|
+ private final int width;
|
|
|
+ private final int height;
|
|
|
+ private float data[][];
|
|
|
+ private Random r = new Random(0);
|
|
|
+
|
|
|
+ public HighMap(int width, int height)
|
|
|
+ {
|
|
|
+ this.width = width;
|
|
|
+ this.height = height;
|
|
|
+ this.data = new float[width][height];
|
|
|
+ }
|
|
|
+
|
|
|
+ public void smooth(int radius)
|
|
|
+ {
|
|
|
+ float[][] newData = new float[width][height];
|
|
|
+
|
|
|
+ for(int x = 0; x < width; x++)
|
|
|
+ {
|
|
|
+ for(int y = 0; y < height; y++)
|
|
|
+ {
|
|
|
+ int div = 0;
|
|
|
+ float sum = 0;
|
|
|
+ for(int mx = -radius; mx <= radius; mx++)
|
|
|
+ {
|
|
|
+ for(int my = -radius; my <= radius; my++)
|
|
|
+ {
|
|
|
+ if(x + mx >= 0 && x + mx < width && y + my >= 0 && y + my < height)
|
|
|
+ {
|
|
|
+ sum += data[x + mx][y + my];
|
|
|
+ div++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sum /= div;
|
|
|
+ newData[x][y] = sum;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ data = newData;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void randomize(int bound)
|
|
|
+ {
|
|
|
+ for(int x = 0; x < width; x++)
|
|
|
+ {
|
|
|
+ for(int y = 0; y < height; y++)
|
|
|
+ {
|
|
|
+ data[x][y] = r.nextInt(bound);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public BufferedImage export()
|
|
|
+ {
|
|
|
+ BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
|
|
|
+
|
|
|
+ float min = Float.MAX_VALUE;
|
|
|
+ float max = Float.MIN_VALUE;
|
|
|
+
|
|
|
+ for(int x = 0; x < width; x++)
|
|
|
+ {
|
|
|
+ for(int y = 0; y < height; y++)
|
|
|
+ {
|
|
|
+ if(data[x][y] < min)
|
|
|
+ {
|
|
|
+ min = data[x][y];
|
|
|
+ }
|
|
|
+ if(data[x][y] > max)
|
|
|
+ {
|
|
|
+ max = data[x][y];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ float div = 255.0f / (max - min);
|
|
|
+
|
|
|
+ for(int x = 0; x < width; x++)
|
|
|
+ {
|
|
|
+ for(int y = 0; y < height; y++)
|
|
|
+ {
|
|
|
+ int base = (int) ((data[x][y] - min) * div);
|
|
|
+ image.setRGB(x, y, 0xFF000000 | (base << 16) | (base << 8) | base);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return image;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void threshold(int levels)
|
|
|
+ {
|
|
|
+ float[] values = new float[width * height];
|
|
|
+ int counter = 0;
|
|
|
+ for(int x = 0; x < width; x++)
|
|
|
+ {
|
|
|
+ for(int y = 0; y < height; y++)
|
|
|
+ {
|
|
|
+ values[counter++] = data[x][y];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Arrays.sort(values);
|
|
|
+
|
|
|
+ float[] borders = new float[levels];
|
|
|
+ for(int i = 0; i < levels - 1; i++)
|
|
|
+ {
|
|
|
+ borders[i] = values[(int) (((i + 1.0f) / levels) * values.length)];
|
|
|
+ }
|
|
|
+ borders[borders.length - 1] = Float.MAX_VALUE;
|
|
|
+
|
|
|
+ for(int x = 0; x < width; x++)
|
|
|
+ {
|
|
|
+ for(int y = 0; y < height; y++)
|
|
|
+ {
|
|
|
+ for(int i = 0; i < borders.length; i++)
|
|
|
+ {
|
|
|
+ if(data[x][y] < borders[i])
|
|
|
+ {
|
|
|
+ data[x][y] = i;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public BufferedImage colorExport(int scale, int... colors)
|
|
|
+ {
|
|
|
+ BufferedImage image = new BufferedImage(width * scale, height * scale, BufferedImage.TYPE_4BYTE_ABGR);
|
|
|
+
|
|
|
+ float[] values = new float[width * height];
|
|
|
+ int counter = 0;
|
|
|
+ for(int x = 0; x < width; x++)
|
|
|
+ {
|
|
|
+ for(int y = 0; y < height; y++)
|
|
|
+ {
|
|
|
+ values[counter++] = data[x][y];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Arrays.sort(values);
|
|
|
+
|
|
|
+ float[] borders = new float[colors.length];
|
|
|
+ for(int i = 0; i < colors.length - 1; i++)
|
|
|
+ {
|
|
|
+ borders[i] = values[(int) (((i + 1.0f) / colors.length) * values.length)];
|
|
|
+ }
|
|
|
+ borders[borders.length - 1] = Float.MAX_VALUE;
|
|
|
+
|
|
|
+ for(int x = 0; x < width; x++)
|
|
|
+ {
|
|
|
+ for(int y = 0; y < height; y++)
|
|
|
+ {
|
|
|
+ for(int i = 0; i < borders.length; i++)
|
|
|
+ {
|
|
|
+ if(data[x][y] < borders[i])
|
|
|
+ {
|
|
|
+ for(int mx = 0; mx < scale; mx++)
|
|
|
+ {
|
|
|
+ for(int my = 0; my < scale; my++)
|
|
|
+ {
|
|
|
+ image.setRGB(x * scale + mx, y * scale + my, colors[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return image;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void save(int scale, String name) throws IOException
|
|
|
+ {
|
|
|
+ BufferedImage image = colorExport(scale, 0xFF131ec1, 0xFF131ee0, 0xFF39aa38, 0xFFa57c4d, 0xFF50463a);
|
|
|
+ ImageIO.write(image, "png", new File(name + ".png"));
|
|
|
+ }
|
|
|
+}
|