Ejemplo n.º 1
0
        public static void ExportImage(MapData mapData, string filename)
        {
            long highestAltitude = mapData.Altitudes.Max();

            byte[] pixelBuffer = new byte[mapData.Width*mapData.Height*3];
            for (long i = 0; i < pixelBuffer.Length; i += 3)
            {
                long altitudeIdx = i/3;

                double normalisedAltitude = Math.Min(Math.Max(mapData.Altitudes[altitudeIdx]/(double) highestAltitude, 0f), 1f);

                var pixel = CalculateColour(normalisedAltitude);

                pixelBuffer[i + 0] = pixel.B;
                pixelBuffer[i + 1] = pixel.G;
                pixelBuffer[i + 2] = pixel.R;
            }

            GCHandle pixelBufferHandle = GCHandle.Alloc(pixelBuffer, GCHandleType.Pinned);

            using (
                Image image = new Bitmap((int) mapData.Width, (int) mapData.Height, (int) (mapData.Width*3),
                    PixelFormat.Format24bppRgb, pixelBufferHandle.AddrOfPinnedObject()))
            {
                var imageName = filename + ".png";
                image.Save(imageName);
            }

            pixelBufferHandle.Free();
        }
Ejemplo n.º 2
0
        public static bool TryParse(StreamReader streamReader, out MapData mapData)
        {
            try
            {
                mapData = Parse(streamReader);
                return true;
            }
            catch (IOException e)
            {
                Console.WriteLine($"Error occurred reading file: '{e.Message}'");
            }
            catch (ParseException e)
            {
                Console.WriteLine($"Parse error: '{e.Message}'");
            }

            mapData = null;
            return false;
        }
Ejemplo n.º 3
0
        public static bool TryParseFile(string filename, out MapData mapData)
        {
            try
            {
                using (var fileStream = File.OpenText(filename))
                {
                    return MapReader.TryParse(fileStream, out mapData);
                }
            }
            catch (IOException e)
            {
                Console.WriteLine($"Failed to open map file '{filename}': '{e.Message}");
            }

            mapData = null;
            return false;
        }