public void ConvertFromSRTM(SRTMReader srtm, BitmapChannels channels) { int mean = 0, max = 0; if (channels == BitmapChannels.ChannelRed) { for (int i = 0; i < _height; i++) { for (int j = 0; j < _width; j++) { mean += srtm.GetElevationAt(i, j); if (max < srtm.GetElevationAt(i, j)) { max = srtm.GetElevationAt(i, j); } } mean /= _width; } } for (int i = 0; i < _height; i++) { for (int j = 0; j < _width; j++) { Int16 height = srtm.GetElevationAt(i, j); byte[] heightArray = BitConverter.GetBytes(height); if (channels == BitmapChannels.ChannelRed) { int rHeight = height - max; float pointHeight = (float)rHeight / (float)max; _bitmap.SetPixel(j, i, Color.FromArgb(255, (byte)(pointHeight * 255), 0, 0)); } else if (channels == (BitmapChannels.ChannelRed | BitmapChannels.ChannelGreen)) { _bitmap.SetPixel(j, i, Color.FromArgb(255, heightArray[0], heightArray[1], 0)); } } } }
static void Main(string[] args) { if (args.Length < 1) { WriteInstructions(); } else { bool verbose = false; int i = 0, count = args.Length; if (args[0] == "-verbose") { verbose = true; i++; if (args.Length == 1) { WriteInstructions(); Environment.Exit(0); } } for (; i < count; i++) { try { FileStream file = File.Open(args[i], FileMode.Open); BinaryReader reader = new BinaryReader(file); SRTMFormat format = SRTMFormat.Unknown; if (file.Length == 3601 * 3601 * sizeof(Int16)) { format = SRTMFormat.SRTM1; } else if (file.Length == 1201 * 1201 * sizeof(Int16)) { format = SRTMFormat.SRTM3; } if (verbose) { Console.WriteLine("Reading SRTM file: " + args[i]); Console.WriteLine(" - Length: " + file.Length + " Bytes"); Console.WriteLine(" - Format: " + format.ToString() + Environment.NewLine); } SRTMReader srtm = new SRTMReader(reader, format); srtm.ReadAllData(false); BitmapTerrain bmpTerrain = new BitmapTerrain(srtm.GetTileSize(), srtm.GetTileSize()); bmpTerrain.ConvertFromSRTM(srtm, BitmapChannels.ChannelRed | BitmapChannels.ChannelGreen); if (verbose) { Console.WriteLine("Writing TIFF file from SRTM file..."); } string tiffPath = args[i]; int dot = tiffPath.IndexOf('.'); tiffPath = tiffPath.Substring(0, dot); tiffPath += ".tiff"; TIFFWriter.ExportTIFF(bmpTerrain.GetBitmap(), tiffPath); if (verbose) { Console.WriteLine("Success" + Environment.NewLine); } } catch (Exception ex) { if (verbose) { Console.WriteLine("Couldn't convert SRTM file:"); Console.WriteLine(ex.Message + Environment.NewLine); } } } } }