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

116 lines
4.1 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;
2022-01-08 23:09:01 +01:00
using System.Collections.Generic;
2022-01-08 21:22:45 +01:00
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MapControl.Images
{
public partial class GeoTaggedImage
{
private const string PixelScaleQuery = "/ifd/{ushort=33550}";
private const string TiePointQuery = "/ifd/{ushort=33922}";
private const string TransformationQuery = "/ifd/{ushort=34264}";
private const string NoDataQuery = "/ifd/{ushort=42113}";
public static Task<GeoTaggedImage> ReadGeoTiff(string imageFilePath)
{
return Task.Run(() =>
{
BitmapSource bitmap;
Matrix transform;
using (var stream = File.OpenRead(imageFilePath))
{
bitmap = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
var mdata = bitmap.Metadata as BitmapMetadata;
2022-01-08 23:09:01 +01:00
if (mdata.GetQuery(PixelScaleQuery) is double[] ps &&
mdata.GetQuery(TiePointQuery) is double[] tp &&
ps.Length == 3 && tp.Length >= 6)
2022-01-08 21:22:45 +01:00
{
2022-01-08 23:09:01 +01:00
transform = new Matrix(ps[0], 0d, 0d, -ps[1], tp[3], tp[4]);
2022-01-08 21:22:45 +01:00
}
2022-01-08 23:09:01 +01:00
else if (mdata.GetQuery(TransformationQuery) is double[] tf && tf.Length == 16)
2022-01-08 21:22:45 +01:00
{
2022-01-08 23:09:01 +01:00
transform = new Matrix(tf[0], tf[1], tf[4], tf[5], tf[3], tf[7]);
2022-01-08 21:22:45 +01:00
}
else
{
throw new ArgumentException("No coordinate transformation found in \"" + imageFilePath + "\".");
}
if (mdata.GetQuery(NoDataQuery) is string noData && int.TryParse(noData, out int noDataValue))
{
bitmap = ConvertTransparentPixel(bitmap, noDataValue);
}
return new GeoTaggedImage(bitmap, transform, null);
});
}
public static BitmapSource ConvertTransparentPixel(BitmapSource source, int transparentPixel)
{
2022-01-09 16:44:42 +01:00
var targetFormat = source.Format;
2022-01-08 23:09:01 +01:00
List<Color> colors = null;
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 16:44:42 +01:00
targetFormat = source.Format;
2022-01-08 23:09:01 +01:00
colors = source.Palette.Colors.ToList();
}
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 16:44:42 +01:00
targetFormat = PixelFormats.Indexed8;
colors = BitmapPalettes.Gray256.Colors.ToList();
}
else if (source.Format == PixelFormats.Gray4)
{
2022-01-09 16:51:59 +01:00
targetFormat = PixelFormats.Indexed4;
2022-01-09 16:44:42 +01:00
colors = BitmapPalettes.Gray16.Colors.ToList();
}
else if (source.Format == PixelFormats.Gray2)
{
2022-01-09 16:51:59 +01:00
targetFormat = PixelFormats.Indexed2;
2022-01-09 16:44:42 +01:00
colors = BitmapPalettes.Gray4.Colors.ToList();
2022-01-08 23:09:01 +01:00
}
2022-01-09 16:51:59 +01:00
else if (source.Format == PixelFormats.BlackWhite)
{
targetFormat = PixelFormats.Indexed1;
colors = BitmapPalettes.BlackAndWhite.Colors.ToList();
}
2022-01-08 23:09:01 +01:00
2022-01-09 16:44:42 +01:00
if (colors == null || transparentPixel >= 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 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;
}
}
}