XAML-Map-Control/MapImages/WPF/GeoImage.WPF.cs

110 lines
3.9 KiB
C#
Raw Normal View History

2022-01-08 21:22:45 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
// <20> 2022 Clemens Fischer
// Licensed under the Microsoft Public License (Ms-PL)
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MapControl.Images
{
2022-01-16 18:39:50 +01:00
public partial class GeoImage
2022-01-08 21:22:45 +01:00
{
2022-01-16 18:39:50 +01:00
public static Task<GeoImage> ReadGeoTiff(string imageFilePath)
2022-01-08 21:22:45 +01:00
{
2022-01-16 18:50:49 +01:00
return Task.Run(() =>
2022-01-08 21:22:45 +01:00
{
BitmapSource bitmap;
Matrix transform;
using (var stream = File.OpenRead(imageFilePath))
{
bitmap = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
2022-01-09 22:40:52 +01:00
var metadata = bitmap.Metadata as BitmapMetadata;
2022-01-08 21:22:45 +01:00
2022-01-16 18:39:50 +01:00
if (metadata.GetQuery((string)PixelScaleQuery) is double[] pixelScale && pixelScale.Length == 3 &&
metadata.GetQuery((string)TiePointQuery) is double[] tiePoint && tiePoint.Length >= 6)
2022-01-08 21:22:45 +01:00
{
2022-01-09 22:40:52 +01:00
transform = new Matrix(pixelScale[0], 0d, 0d, -pixelScale[1], tiePoint[3], tiePoint[4]);
2022-01-08 21:22:45 +01:00
}
2022-01-16 18:39:50 +01:00
else if (metadata.GetQuery((string)TransformQuery) is double[] tform && tform.Length == 16)
2022-01-08 21:22:45 +01:00
{
2022-01-09 22:40:52 +01:00
transform = new Matrix(tform[0], tform[1], tform[4], tform[5], tform[3], tform[7]);
2022-01-08 21:22:45 +01:00
}
else
{
throw new ArgumentException("No coordinate transformation found in \"" + imageFilePath + "\".");
}
2022-01-16 18:39:50 +01:00
if (metadata.GetQuery((string)NoDataQuery) is string noData && int.TryParse(noData, out int noDataValue))
2022-01-08 21:22:45 +01:00
{
bitmap = ConvertTransparentPixel(bitmap, noDataValue);
}
2022-01-16 18:39:50 +01:00
return new GeoImage(bitmap, transform, (MapProjection)null);
2022-01-16 18:50:49 +01:00
});
2022-01-08 21:22:45 +01:00
}
public static BitmapSource ConvertTransparentPixel(BitmapSource source, int transparentPixel)
{
2022-01-09 22:40:52 +01:00
BitmapPalette sourcePalette = null;
2022-01-09 16:44:42 +01:00
var targetFormat = source.Format;
2022-01-08 23:09:01 +01:00
2022-01-09 16:44:42 +01:00
if (source.Format == PixelFormats.Indexed8 ||
source.Format == PixelFormats.Indexed4 ||
2022-01-09 16:51:59 +01:00
source.Format == PixelFormats.Indexed2 ||
source.Format == PixelFormats.Indexed1)
2022-01-08 23:09:01 +01:00
{
2022-01-09 22:40:52 +01:00
sourcePalette = source.Palette;
2022-01-08 23:09:01 +01:00
}
2022-01-09 16:44:42 +01:00
else if (source.Format == PixelFormats.Gray8)
2022-01-08 23:09:01 +01:00
{
2022-01-09 22:40:52 +01:00
sourcePalette = BitmapPalettes.Gray256;
2022-01-09 16:44:42 +01:00
targetFormat = PixelFormats.Indexed8;
}
else if (source.Format == PixelFormats.Gray4)
{
2022-01-09 22:40:52 +01:00
sourcePalette = BitmapPalettes.Gray16;
2022-01-09 16:51:59 +01:00
targetFormat = PixelFormats.Indexed4;
2022-01-09 16:44:42 +01:00
}
else if (source.Format == PixelFormats.Gray2)
{
2022-01-09 22:40:52 +01:00
sourcePalette = BitmapPalettes.Gray4;
2022-01-09 16:51:59 +01:00
targetFormat = PixelFormats.Indexed2;
2022-01-08 23:09:01 +01:00
}
2022-01-09 16:51:59 +01:00
else if (source.Format == PixelFormats.BlackWhite)
{
2022-01-09 22:40:52 +01:00
sourcePalette = BitmapPalettes.BlackAndWhite;
2022-01-09 16:51:59 +01:00
targetFormat = PixelFormats.Indexed1;
}
2022-01-08 23:09:01 +01:00
2022-01-09 22:40:52 +01:00
if (sourcePalette == null || transparentPixel >= sourcePalette.Colors.Count)
2022-01-08 21:22:45 +01:00
{
2022-01-09 16:44:42 +01:00
return source;
}
2022-01-08 21:22:45 +01:00
2022-01-09 22:40:52 +01:00
var colors = sourcePalette.Colors.ToList();
2022-01-09 16:44:42 +01:00
colors[transparentPixel] = Colors.Transparent;
2022-01-08 21:22:45 +01:00
2022-01-09 16:44:42 +01:00
var stride = (source.PixelWidth * source.Format.BitsPerPixel + 7) / 8;
var buffer = new byte[stride * source.PixelHeight];
2022-01-08 21:22:45 +01:00
2022-01-09 16:44:42 +01:00
source.CopyPixels(buffer, stride, 0);
2022-01-08 21:22:45 +01:00
2022-01-09 16:44:42 +01:00
var target = BitmapSource.Create(
source.PixelWidth, source.PixelHeight, source.DpiX, source.DpiY,
targetFormat, new BitmapPalette(colors), buffer, stride);
target.Freeze();
2022-01-08 21:22:45 +01:00
return target;
}
}
}