import java.io.*; import java.util.*; public class Grid { public static final double ZMIN = 0; public static final double ZMAX = 1000; public static void writeMap(OutputStream out, float[][] map) throws IOException { writeMap(out, map, 0, map.length, 0, map[0].length, ZMIN, ZMAX, false); } public static void writeMap(OutputStream out, float[][] map, double zmin, double zmax) throws IOException { writeMap(out, map, 0, map.length, 0, map[0].length, zmin, zmax, false); } public static void writeMap(OutputStream out, float[][] map, double xmin, double xmax, double ymin, double ymax) throws IOException { writeMap(out, map, xmin, xmax, ymin, ymax, ZMIN, ZMAX, false); } public static void writeMap(OutputStream out, float[][] map, double xmin, double xmax, double ymin, double ymax, double zmin, double zmax) throws IOException { writeMap(out, map, xmin, xmax, ymin, ymax, zmin, zmax, false); } public static void writeMap(OutputStream out, float[][] map, boolean ascii) throws IOException { writeMap(out, map, 0, map.length, 0, map[0].length, ZMIN, ZMAX, ascii); } public static void writeMap(OutputStream out, float[][] map, double zmin, double zmax, boolean ascii) throws IOException { writeMap(out, map, 0, map.length, 0, map[0].length, zmin, zmax, ascii); } public static void writeMap(OutputStream out, float[][] map, double xmin, double xmax, double ymin, double ymax, boolean ascii) throws IOException { writeMap(out, map, xmin, xmax, ymin, ymax, ZMIN, ZMAX, ascii); } public static void writeMap(OutputStream out, float[][] map, double xmin, double xmax, double ymin, double ymax, double zmin, double zmax, boolean ascii) throws IOException { PrintWriter writer; int width = map.length, height = map[0].length; int x, y, n = 0; if(ascii) { writer = new PrintWriter(new OutputStreamWriter(out)); writer.println("DSAA"); writer.println(width + " " + height); writer.println(xmin + " " + xmax); writer.println(ymin + " " + ymax); writer.println(zmin + " " + zmax); for(y = 0; y < height; y++) for(x = 0; x < width; x++) if((++n % 8) == 0) writer.println(map[x][y]); else writer.print(map[x][y] + "\t"); writer.flush(); } else { pWriteString(out, "DSBB"); pWriteShort(out, width); pWriteShort(out, height); pWriteDouble(out, xmin); pWriteDouble(out, xmax); pWriteDouble(out, ymin); pWriteDouble(out, ymax); pWriteDouble(out, zmin); pWriteDouble(out, zmax); for(y = 0; y < height; y++) for(x = 0; x < width; x++) pWriteFloat(out, map[x][y]); out.flush(); } } private static void pWriteString(OutputStream out, String s) throws IOException { for(int i = 0; i < s.length(); i++) out.write(s.charAt(i)); } private static void pWriteShort(OutputStream out, int s) throws IOException { out.write(s); out.write(s >> 8); } private static void pWriteDouble(OutputStream out, double d) throws IOException { long b = Double.doubleToRawLongBits(d); int b1 = (int) (b >> 32), b2 = (int) b; out.write(b2); out.write(b2 >> 8); out.write(b2 >> 16); out.write(b2 >> 24); out.write(b1); out.write(b1 >> 8); out.write(b1 >> 16); out.write(b1 >> 24); } private static void pWriteFloat(OutputStream out, float f) throws IOException { int b = Float.floatToRawIntBits(f); out.write(b); out.write(b >> 8); out.write(b >> 16); out.write(b >> 24); } }