mirror of
https://github.com/ClemensFischer/XAML-Map-Control.git
synced 2025-12-06 07:12:04 +01:00
ImageLoader
This commit is contained in:
parent
560f44a139
commit
069072ce34
|
|
@ -42,22 +42,22 @@ namespace MapControl
|
||||||
|
|
||||||
internal static async Task<IImage> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
|
internal static async Task<IImage> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
|
||||||
{
|
{
|
||||||
WriteableBitmap mergedImage = null;
|
WriteableBitmap mergedBitmap = null;
|
||||||
|
|
||||||
var images = await LoadImagesAsync(uri1, uri2, progress);
|
var images = await LoadImagesAsync(uri1, uri2, progress);
|
||||||
|
|
||||||
if (images.Length == 2 &&
|
if (images.Length == 2 &&
|
||||||
images[0] is Bitmap image1 &&
|
images[0] is Bitmap bitmap1 &&
|
||||||
images[1] is Bitmap image2 &&
|
images[1] is Bitmap bitmap2 &&
|
||||||
image1.PixelSize.Height == image2.PixelSize.Height &&
|
bitmap1.PixelSize.Height == bitmap2.PixelSize.Height &&
|
||||||
image1.Format.HasValue &&
|
bitmap1.Format.HasValue &&
|
||||||
image1.Format == image2.Format &&
|
bitmap1.Format == bitmap2.Format &&
|
||||||
image1.AlphaFormat.HasValue &&
|
bitmap1.AlphaFormat.HasValue &&
|
||||||
image1.AlphaFormat == image2.AlphaFormat)
|
bitmap1.AlphaFormat == bitmap2.AlphaFormat)
|
||||||
{
|
{
|
||||||
var bpp = image1.Format.Value == PixelFormat.Rgb565 ? 2 : 4;
|
var bpp = bitmap1.Format.Value == PixelFormat.Rgb565 ? 2 : 4;
|
||||||
var pixelSize = new PixelSize(image1.PixelSize.Width + image2.PixelSize.Width, image1.PixelSize.Height);
|
var pixelSize = new PixelSize(bitmap1.PixelSize.Width + bitmap2.PixelSize.Width, bitmap1.PixelSize.Height);
|
||||||
var stride1 = bpp * image1.PixelSize.Width;
|
var stride1 = bpp * bitmap1.PixelSize.Width;
|
||||||
var stride = bpp * pixelSize.Width;
|
var stride = bpp * pixelSize.Width;
|
||||||
var bufferSize = stride * pixelSize.Height;
|
var bufferSize = stride * pixelSize.Height;
|
||||||
|
|
||||||
|
|
@ -67,15 +67,15 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
var buffer = (nint)ptr;
|
var buffer = (nint)ptr;
|
||||||
|
|
||||||
image1.CopyPixels(new PixelRect(image1.PixelSize), buffer, bufferSize, stride);
|
bitmap1.CopyPixels(new PixelRect(bitmap1.PixelSize), buffer, bufferSize, stride);
|
||||||
image2.CopyPixels(new PixelRect(image2.PixelSize), buffer + stride1, bufferSize, stride);
|
bitmap2.CopyPixels(new PixelRect(bitmap2.PixelSize), buffer + stride1, bufferSize, stride);
|
||||||
|
|
||||||
mergedImage = new WriteableBitmap(image1.Format.Value, image1.AlphaFormat.Value, buffer, pixelSize, image1.Dpi, stride);
|
mergedBitmap = new WriteableBitmap(bitmap1.Format.Value, bitmap1.AlphaFormat.Value, buffer, pixelSize, bitmap1.Dpi, stride);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return mergedImage;
|
return mergedBitmap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,16 +35,16 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
private class GeoBitmap
|
private class GeoBitmap
|
||||||
{
|
{
|
||||||
public GeoBitmap(BitmapSource bitmapSource, Matrix transform, MapProjection projection)
|
public GeoBitmap(BitmapSource bitmap, Matrix transform, MapProjection projection)
|
||||||
{
|
{
|
||||||
var p1 = transform.Transform(new Point());
|
var p1 = transform.Transform(new Point());
|
||||||
var p2 = transform.Transform(new Point(
|
var p2 = transform.Transform(new Point(
|
||||||
#if AVALONIA
|
#if AVALONIA
|
||||||
bitmapSource.PixelSize.Width, bitmapSource.PixelSize.Height));
|
bitmap.PixelSize.Width, bitmap.PixelSize.Height));
|
||||||
#else
|
#else
|
||||||
bitmapSource.PixelWidth, bitmapSource.PixelHeight));
|
bitmap.PixelWidth, bitmap.PixelHeight));
|
||||||
#endif
|
#endif
|
||||||
BitmapSource = bitmapSource;
|
BitmapSource = bitmap;
|
||||||
LatLonBox = projection != null
|
LatLonBox = projection != null
|
||||||
? new LatLonBox(projection.MapToBoundingBox(new Rect(p1, p2)))
|
? new LatLonBox(projection.MapToBoundingBox(new Rect(p1, p2)))
|
||||||
: new LatLonBox(p1.Y, p1.X, p2.Y, p2.X);
|
: new LatLonBox(p1.Y, p1.X, p2.Y, p2.X);
|
||||||
|
|
|
||||||
|
|
@ -17,28 +17,28 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
return Task.Run(() =>
|
return Task.Run(() =>
|
||||||
{
|
{
|
||||||
BitmapSource bitmapSource;
|
BitmapSource bitmap;
|
||||||
Matrix transformMatrix;
|
Matrix transform;
|
||||||
MapProjection mapProjection = null;
|
MapProjection projection = null;
|
||||||
|
|
||||||
using (var stream = File.OpenRead(sourcePath))
|
using (var stream = File.OpenRead(sourcePath))
|
||||||
{
|
{
|
||||||
bitmapSource = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
|
bitmap = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
|
||||||
}
|
}
|
||||||
|
|
||||||
var metadata = (BitmapMetadata)bitmapSource.Metadata;
|
var metadata = (BitmapMetadata)bitmap.Metadata;
|
||||||
|
|
||||||
if (metadata.GetQuery(QueryString(ModelPixelScaleTag)) is double[] pixelScale &&
|
if (metadata.GetQuery(QueryString(ModelPixelScaleTag)) is double[] pixelScale &&
|
||||||
pixelScale.Length == 3 &&
|
pixelScale.Length == 3 &&
|
||||||
metadata.GetQuery(QueryString(ModelTiePointTag)) is double[] tiePoint &&
|
metadata.GetQuery(QueryString(ModelTiePointTag)) is double[] tiePoint &&
|
||||||
tiePoint.Length >= 6)
|
tiePoint.Length >= 6)
|
||||||
{
|
{
|
||||||
transformMatrix = new Matrix(pixelScale[0], 0d, 0d, -pixelScale[1], tiePoint[3], tiePoint[4]);
|
transform = new Matrix(pixelScale[0], 0d, 0d, -pixelScale[1], tiePoint[3], tiePoint[4]);
|
||||||
}
|
}
|
||||||
else if (metadata.GetQuery(QueryString(ModelTransformationTag)) is double[] transformValues &&
|
else if (metadata.GetQuery(QueryString(ModelTransformationTag)) is double[] transformValues &&
|
||||||
transformValues.Length == 16)
|
transformValues.Length == 16)
|
||||||
{
|
{
|
||||||
transformMatrix = new Matrix(transformValues[0], transformValues[1],
|
transform = new Matrix(transformValues[0], transformValues[1],
|
||||||
transformValues[4], transformValues[5],
|
transformValues[4], transformValues[5],
|
||||||
transformValues[3], transformValues[7]);
|
transformValues[3], transformValues[7]);
|
||||||
}
|
}
|
||||||
|
|
@ -49,16 +49,16 @@ namespace MapControl
|
||||||
|
|
||||||
if (metadata.GetQuery(QueryString(GeoKeyDirectoryTag)) is short[] geoKeyDirectory)
|
if (metadata.GetQuery(QueryString(GeoKeyDirectoryTag)) is short[] geoKeyDirectory)
|
||||||
{
|
{
|
||||||
mapProjection = GetProjection(geoKeyDirectory);
|
projection = GetProjection(geoKeyDirectory);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (metadata.GetQuery(QueryString(NoDataTag)) is string noData &&
|
if (metadata.GetQuery(QueryString(NoDataTag)) is string noData &&
|
||||||
int.TryParse(noData, out int noDataValue))
|
int.TryParse(noData, out int noDataValue))
|
||||||
{
|
{
|
||||||
bitmapSource = ConvertTransparentPixel(bitmapSource, noDataValue);
|
bitmap = ConvertTransparentPixel(bitmap, noDataValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new GeoBitmap(bitmapSource, transformMatrix, mapProjection);
|
return new GeoBitmap(bitmap, transform, projection);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,36 +54,36 @@ namespace MapControl
|
||||||
|
|
||||||
internal static async Task<ImageSource> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
|
internal static async Task<ImageSource> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
|
||||||
{
|
{
|
||||||
WriteableBitmap mergedImage = null;
|
WriteableBitmap mergedBitmap = null;
|
||||||
|
|
||||||
var images = await LoadImagesAsync(uri1, uri2, progress);
|
var images = await LoadImagesAsync(uri1, uri2, progress);
|
||||||
|
|
||||||
if (images.Length == 2 &&
|
if (images.Length == 2 &&
|
||||||
images[0] is BitmapSource image1 &&
|
images[0] is BitmapSource bitmap1 &&
|
||||||
images[1] is BitmapSource image2 &&
|
images[1] is BitmapSource bitmap2 &&
|
||||||
image1.PixelHeight == image2.PixelHeight &&
|
bitmap1.PixelHeight == bitmap2.PixelHeight &&
|
||||||
image1.Format == image2.Format &&
|
bitmap1.Format == bitmap2.Format &&
|
||||||
image1.Format.BitsPerPixel % 8 == 0)
|
bitmap1.Format.BitsPerPixel % 8 == 0)
|
||||||
{
|
{
|
||||||
var format = image1.Format;
|
var format = bitmap1.Format;
|
||||||
var height = image1.PixelHeight;
|
var height = bitmap1.PixelHeight;
|
||||||
var width1 = image1.PixelWidth;
|
var width1 = bitmap1.PixelWidth;
|
||||||
var width2 = image2.PixelWidth;
|
var width2 = bitmap2.PixelWidth;
|
||||||
var stride1 = width1 * format.BitsPerPixel / 8;
|
var stride1 = width1 * format.BitsPerPixel / 8;
|
||||||
var stride2 = width2 * format.BitsPerPixel / 8;
|
var stride2 = width2 * format.BitsPerPixel / 8;
|
||||||
var buffer1 = new byte[stride1 * height];
|
var buffer1 = new byte[stride1 * height];
|
||||||
var buffer2 = new byte[stride2 * height];
|
var buffer2 = new byte[stride2 * height];
|
||||||
|
|
||||||
image1.CopyPixels(buffer1, stride1, 0);
|
bitmap1.CopyPixels(buffer1, stride1, 0);
|
||||||
image2.CopyPixels(buffer2, stride2, 0);
|
bitmap2.CopyPixels(buffer2, stride2, 0);
|
||||||
|
|
||||||
mergedImage = new WriteableBitmap(width1 + width2, height, 96, 96, format, null);
|
mergedBitmap = new WriteableBitmap(width1 + width2, height, 96, 96, format, null);
|
||||||
mergedImage.WritePixels(new Int32Rect(0, 0, width1, height), buffer1, stride1, 0);
|
mergedBitmap.WritePixels(new Int32Rect(0, 0, width1, height), buffer1, stride1, 0);
|
||||||
mergedImage.WritePixels(new Int32Rect(width1, 0, width2, height), buffer2, stride2, 0);
|
mergedBitmap.WritePixels(new Int32Rect(width1, 0, width2, height), buffer2, stride2, 0);
|
||||||
mergedImage.Freeze();
|
mergedBitmap.Freeze();
|
||||||
}
|
}
|
||||||
|
|
||||||
return mergedImage;
|
return mergedBitmap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,9 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
private static async Task<GeoBitmap> LoadGeoTiffAsync(string sourcePath)
|
private static async Task<GeoBitmap> LoadGeoTiffAsync(string sourcePath)
|
||||||
{
|
{
|
||||||
BitmapSource bitmapSource;
|
BitmapSource bitmap;
|
||||||
Matrix transformMatrix;
|
Matrix transform;
|
||||||
MapProjection mapProjection = null;
|
MapProjection projection = null;
|
||||||
|
|
||||||
var file = await StorageFile.GetFileFromPathAsync(FilePath.GetFullPath(sourcePath));
|
var file = await StorageFile.GetFileFromPathAsync(FilePath.GetFullPath(sourcePath));
|
||||||
|
|
||||||
|
|
@ -28,7 +28,7 @@ namespace MapControl
|
||||||
{
|
{
|
||||||
var decoder = await BitmapDecoder.CreateAsync(stream);
|
var decoder = await BitmapDecoder.CreateAsync(stream);
|
||||||
|
|
||||||
bitmapSource = await ImageLoader.LoadWriteableBitmapAsync(decoder);
|
bitmap = await ImageLoader.LoadWriteableBitmapAsync(decoder);
|
||||||
|
|
||||||
var geoKeyDirectoryQuery = QueryString(GeoKeyDirectoryTag);
|
var geoKeyDirectoryQuery = QueryString(GeoKeyDirectoryTag);
|
||||||
var pixelScaleQuery = QueryString(ModelPixelScaleTag);
|
var pixelScaleQuery = QueryString(ModelPixelScaleTag);
|
||||||
|
|
@ -50,13 +50,13 @@ namespace MapControl
|
||||||
tiePointValue.Value is double[] tiePoint &&
|
tiePointValue.Value is double[] tiePoint &&
|
||||||
tiePoint.Length >= 6)
|
tiePoint.Length >= 6)
|
||||||
{
|
{
|
||||||
transformMatrix = new Matrix(pixelScale[0], 0d, 0d, -pixelScale[1], tiePoint[3], tiePoint[4]);
|
transform = new Matrix(pixelScale[0], 0d, 0d, -pixelScale[1], tiePoint[3], tiePoint[4]);
|
||||||
}
|
}
|
||||||
else if (metadata.TryGetValue(transformationQuery, out BitmapTypedValue transformValue) &&
|
else if (metadata.TryGetValue(transformationQuery, out BitmapTypedValue transformValue) &&
|
||||||
transformValue.Value is double[] transformValues &&
|
transformValue.Value is double[] transformValues &&
|
||||||
transformValues.Length == 16)
|
transformValues.Length == 16)
|
||||||
{
|
{
|
||||||
transformMatrix = new Matrix(transformValues[0], transformValues[1],
|
transform = new Matrix(transformValues[0], transformValues[1],
|
||||||
transformValues[4], transformValues[5],
|
transformValues[4], transformValues[5],
|
||||||
transformValues[3], transformValues[7]);
|
transformValues[3], transformValues[7]);
|
||||||
}
|
}
|
||||||
|
|
@ -68,11 +68,11 @@ namespace MapControl
|
||||||
if (metadata.TryGetValue(geoKeyDirectoryQuery, out BitmapTypedValue geoKeyDirValue) &&
|
if (metadata.TryGetValue(geoKeyDirectoryQuery, out BitmapTypedValue geoKeyDirValue) &&
|
||||||
geoKeyDirValue.Value is short[] geoKeyDirectory)
|
geoKeyDirValue.Value is short[] geoKeyDirectory)
|
||||||
{
|
{
|
||||||
mapProjection = GetProjection(geoKeyDirectory);
|
projection = GetProjection(geoKeyDirectory);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new GeoBitmap(bitmapSource, transformMatrix, mapProjection);
|
return new GeoBitmap(bitmap, transform, projection);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -72,13 +72,15 @@ namespace MapControl
|
||||||
|
|
||||||
internal static async Task<WriteableBitmap> LoadWriteableBitmapAsync(Uri uri)
|
internal static async Task<WriteableBitmap> LoadWriteableBitmapAsync(Uri uri)
|
||||||
{
|
{
|
||||||
WriteableBitmap image = null;
|
WriteableBitmap bitmap = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var stream = await RandomAccessStreamReference.CreateFromUri(uri).OpenReadAsync())
|
using (var stream = await RandomAccessStreamReference.CreateFromUri(uri).OpenReadAsync())
|
||||||
{
|
{
|
||||||
image = await LoadWriteableBitmapAsync(await BitmapDecoder.CreateAsync(stream));
|
var decoder = await BitmapDecoder.CreateAsync(stream);
|
||||||
|
|
||||||
|
bitmap = await LoadWriteableBitmapAsync(decoder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
@ -86,32 +88,32 @@ namespace MapControl
|
||||||
Debug.WriteLine($"{nameof(ImageLoader)}: {uri}: {ex.Message}");
|
Debug.WriteLine($"{nameof(ImageLoader)}: {uri}: {ex.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
return image;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static async Task<ImageSource> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
|
internal static async Task<ImageSource> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
|
||||||
{
|
{
|
||||||
WriteableBitmap mergedImage = null;
|
WriteableBitmap mergedBitmap = null;
|
||||||
|
|
||||||
progress?.Report(0d);
|
progress?.Report(0d);
|
||||||
|
|
||||||
var images = await Task.WhenAll(LoadWriteableBitmapAsync(uri1), LoadWriteableBitmapAsync(uri2));
|
var bitmaps = await Task.WhenAll(LoadWriteableBitmapAsync(uri1), LoadWriteableBitmapAsync(uri2));
|
||||||
|
|
||||||
if (images.Length == 2 &&
|
if (bitmaps.Length == 2 &&
|
||||||
images[0] != null &&
|
bitmaps[0] != null &&
|
||||||
images[1] != null &&
|
bitmaps[1] != null &&
|
||||||
images[0].PixelHeight == images[1].PixelHeight)
|
bitmaps[0].PixelHeight == bitmaps[1].PixelHeight)
|
||||||
{
|
{
|
||||||
var buffer1 = images[0].PixelBuffer;
|
var buffer1 = bitmaps[0].PixelBuffer;
|
||||||
var buffer2 = images[1].PixelBuffer;
|
var buffer2 = bitmaps[1].PixelBuffer;
|
||||||
var stride1 = (uint)images[0].PixelWidth * 4;
|
var stride1 = (uint)bitmaps[0].PixelWidth * 4;
|
||||||
var stride2 = (uint)images[1].PixelWidth * 4;
|
var stride2 = (uint)bitmaps[1].PixelWidth * 4;
|
||||||
var stride = stride1 + stride2;
|
var stride = stride1 + stride2;
|
||||||
var height = images[0].PixelHeight;
|
var height = bitmaps[0].PixelHeight;
|
||||||
|
|
||||||
mergedImage = new WriteableBitmap(images[0].PixelWidth + images[1].PixelWidth, height);
|
mergedBitmap = new WriteableBitmap(bitmaps[0].PixelWidth + bitmaps[1].PixelWidth, height);
|
||||||
|
|
||||||
var buffer = mergedImage.PixelBuffer;
|
var buffer = mergedBitmap.PixelBuffer;
|
||||||
|
|
||||||
for (uint y = 0; y < height; y++)
|
for (uint y = 0; y < height; y++)
|
||||||
{
|
{
|
||||||
|
|
@ -122,7 +124,7 @@ namespace MapControl
|
||||||
|
|
||||||
progress?.Report(1d);
|
progress?.Report(1d);
|
||||||
|
|
||||||
return mergedImage;
|
return mergedBitmap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue