From 069072ce340bd9a0784b7feaf33e1e68b4f160d2 Mon Sep 17 00:00:00 2001 From: ClemensFischer Date: Wed, 1 Jan 2025 19:45:40 +0100 Subject: [PATCH] ImageLoader --- MapControl/Avalonia/ImageLoader.Avalonia.cs | 30 ++++++++--------- MapControl/Shared/GeoImage.cs | 8 ++--- MapControl/WPF/GeoImage.WPF.cs | 20 ++++++------ MapControl/WPF/ImageLoader.WPF.cs | 34 +++++++++---------- MapControl/WinUI/GeoImage.WinUI.cs | 16 ++++----- MapControl/WinUI/ImageLoader.WinUI.cs | 36 +++++++++++---------- 6 files changed, 73 insertions(+), 71 deletions(-) diff --git a/MapControl/Avalonia/ImageLoader.Avalonia.cs b/MapControl/Avalonia/ImageLoader.Avalonia.cs index 9f425fad..c8c2d57e 100644 --- a/MapControl/Avalonia/ImageLoader.Avalonia.cs +++ b/MapControl/Avalonia/ImageLoader.Avalonia.cs @@ -42,22 +42,22 @@ namespace MapControl internal static async Task LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress progress) { - WriteableBitmap mergedImage = null; + WriteableBitmap mergedBitmap = null; var images = await LoadImagesAsync(uri1, uri2, progress); if (images.Length == 2 && - images[0] is Bitmap image1 && - images[1] is Bitmap image2 && - image1.PixelSize.Height == image2.PixelSize.Height && - image1.Format.HasValue && - image1.Format == image2.Format && - image1.AlphaFormat.HasValue && - image1.AlphaFormat == image2.AlphaFormat) + images[0] is Bitmap bitmap1 && + images[1] is Bitmap bitmap2 && + bitmap1.PixelSize.Height == bitmap2.PixelSize.Height && + bitmap1.Format.HasValue && + bitmap1.Format == bitmap2.Format && + bitmap1.AlphaFormat.HasValue && + bitmap1.AlphaFormat == bitmap2.AlphaFormat) { - var bpp = image1.Format.Value == PixelFormat.Rgb565 ? 2 : 4; - var pixelSize = new PixelSize(image1.PixelSize.Width + image2.PixelSize.Width, image1.PixelSize.Height); - var stride1 = bpp * image1.PixelSize.Width; + var bpp = bitmap1.Format.Value == PixelFormat.Rgb565 ? 2 : 4; + var pixelSize = new PixelSize(bitmap1.PixelSize.Width + bitmap2.PixelSize.Width, bitmap1.PixelSize.Height); + var stride1 = bpp * bitmap1.PixelSize.Width; var stride = bpp * pixelSize.Width; var bufferSize = stride * pixelSize.Height; @@ -67,15 +67,15 @@ namespace MapControl { var buffer = (nint)ptr; - image1.CopyPixels(new PixelRect(image1.PixelSize), buffer, bufferSize, stride); - image2.CopyPixels(new PixelRect(image2.PixelSize), buffer + stride1, bufferSize, stride); + bitmap1.CopyPixels(new PixelRect(bitmap1.PixelSize), buffer, 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; } } } diff --git a/MapControl/Shared/GeoImage.cs b/MapControl/Shared/GeoImage.cs index e6321f93..b4c2e08d 100644 --- a/MapControl/Shared/GeoImage.cs +++ b/MapControl/Shared/GeoImage.cs @@ -35,16 +35,16 @@ namespace MapControl { 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 p2 = transform.Transform(new Point( #if AVALONIA - bitmapSource.PixelSize.Width, bitmapSource.PixelSize.Height)); + bitmap.PixelSize.Width, bitmap.PixelSize.Height)); #else - bitmapSource.PixelWidth, bitmapSource.PixelHeight)); + bitmap.PixelWidth, bitmap.PixelHeight)); #endif - BitmapSource = bitmapSource; + BitmapSource = bitmap; LatLonBox = projection != null ? new LatLonBox(projection.MapToBoundingBox(new Rect(p1, p2))) : new LatLonBox(p1.Y, p1.X, p2.Y, p2.X); diff --git a/MapControl/WPF/GeoImage.WPF.cs b/MapControl/WPF/GeoImage.WPF.cs index 2466202a..9457d328 100644 --- a/MapControl/WPF/GeoImage.WPF.cs +++ b/MapControl/WPF/GeoImage.WPF.cs @@ -17,28 +17,28 @@ namespace MapControl { return Task.Run(() => { - BitmapSource bitmapSource; - Matrix transformMatrix; - MapProjection mapProjection = null; + BitmapSource bitmap; + Matrix transform; + MapProjection projection = null; 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 && pixelScale.Length == 3 && metadata.GetQuery(QueryString(ModelTiePointTag)) is double[] tiePoint && 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 && transformValues.Length == 16) { - transformMatrix = new Matrix(transformValues[0], transformValues[1], + transform = new Matrix(transformValues[0], transformValues[1], transformValues[4], transformValues[5], transformValues[3], transformValues[7]); } @@ -49,16 +49,16 @@ namespace MapControl if (metadata.GetQuery(QueryString(GeoKeyDirectoryTag)) is short[] geoKeyDirectory) { - mapProjection = GetProjection(geoKeyDirectory); + projection = GetProjection(geoKeyDirectory); } if (metadata.GetQuery(QueryString(NoDataTag)) is string noData && 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); }); } diff --git a/MapControl/WPF/ImageLoader.WPF.cs b/MapControl/WPF/ImageLoader.WPF.cs index 29aad54f..8d1139c9 100644 --- a/MapControl/WPF/ImageLoader.WPF.cs +++ b/MapControl/WPF/ImageLoader.WPF.cs @@ -54,36 +54,36 @@ namespace MapControl internal static async Task LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress progress) { - WriteableBitmap mergedImage = null; + WriteableBitmap mergedBitmap = null; var images = await LoadImagesAsync(uri1, uri2, progress); if (images.Length == 2 && - images[0] is BitmapSource image1 && - images[1] is BitmapSource image2 && - image1.PixelHeight == image2.PixelHeight && - image1.Format == image2.Format && - image1.Format.BitsPerPixel % 8 == 0) + images[0] is BitmapSource bitmap1 && + images[1] is BitmapSource bitmap2 && + bitmap1.PixelHeight == bitmap2.PixelHeight && + bitmap1.Format == bitmap2.Format && + bitmap1.Format.BitsPerPixel % 8 == 0) { - var format = image1.Format; - var height = image1.PixelHeight; - var width1 = image1.PixelWidth; - var width2 = image2.PixelWidth; + var format = bitmap1.Format; + var height = bitmap1.PixelHeight; + var width1 = bitmap1.PixelWidth; + var width2 = bitmap2.PixelWidth; var stride1 = width1 * format.BitsPerPixel / 8; var stride2 = width2 * format.BitsPerPixel / 8; var buffer1 = new byte[stride1 * height]; var buffer2 = new byte[stride2 * height]; - image1.CopyPixels(buffer1, stride1, 0); - image2.CopyPixels(buffer2, stride2, 0); + bitmap1.CopyPixels(buffer1, stride1, 0); + bitmap2.CopyPixels(buffer2, stride2, 0); - mergedImage = new WriteableBitmap(width1 + width2, height, 96, 96, format, null); - mergedImage.WritePixels(new Int32Rect(0, 0, width1, height), buffer1, stride1, 0); - mergedImage.WritePixels(new Int32Rect(width1, 0, width2, height), buffer2, stride2, 0); - mergedImage.Freeze(); + mergedBitmap = new WriteableBitmap(width1 + width2, height, 96, 96, format, null); + mergedBitmap.WritePixels(new Int32Rect(0, 0, width1, height), buffer1, stride1, 0); + mergedBitmap.WritePixels(new Int32Rect(width1, 0, width2, height), buffer2, stride2, 0); + mergedBitmap.Freeze(); } - return mergedImage; + return mergedBitmap; } } } diff --git a/MapControl/WinUI/GeoImage.WinUI.cs b/MapControl/WinUI/GeoImage.WinUI.cs index b5ca2f3a..92ea0326 100644 --- a/MapControl/WinUI/GeoImage.WinUI.cs +++ b/MapControl/WinUI/GeoImage.WinUI.cs @@ -18,9 +18,9 @@ namespace MapControl { private static async Task LoadGeoTiffAsync(string sourcePath) { - BitmapSource bitmapSource; - Matrix transformMatrix; - MapProjection mapProjection = null; + BitmapSource bitmap; + Matrix transform; + MapProjection projection = null; var file = await StorageFile.GetFileFromPathAsync(FilePath.GetFullPath(sourcePath)); @@ -28,7 +28,7 @@ namespace MapControl { var decoder = await BitmapDecoder.CreateAsync(stream); - bitmapSource = await ImageLoader.LoadWriteableBitmapAsync(decoder); + bitmap = await ImageLoader.LoadWriteableBitmapAsync(decoder); var geoKeyDirectoryQuery = QueryString(GeoKeyDirectoryTag); var pixelScaleQuery = QueryString(ModelPixelScaleTag); @@ -50,13 +50,13 @@ namespace MapControl tiePointValue.Value is double[] tiePoint && 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) && transformValue.Value is double[] transformValues && transformValues.Length == 16) { - transformMatrix = new Matrix(transformValues[0], transformValues[1], + transform = new Matrix(transformValues[0], transformValues[1], transformValues[4], transformValues[5], transformValues[3], transformValues[7]); } @@ -68,11 +68,11 @@ namespace MapControl if (metadata.TryGetValue(geoKeyDirectoryQuery, out BitmapTypedValue geoKeyDirValue) && geoKeyDirValue.Value is short[] geoKeyDirectory) { - mapProjection = GetProjection(geoKeyDirectory); + projection = GetProjection(geoKeyDirectory); } } - return new GeoBitmap(bitmapSource, transformMatrix, mapProjection); + return new GeoBitmap(bitmap, transform, projection); } } } diff --git a/MapControl/WinUI/ImageLoader.WinUI.cs b/MapControl/WinUI/ImageLoader.WinUI.cs index 8524a685..4c2b3e82 100644 --- a/MapControl/WinUI/ImageLoader.WinUI.cs +++ b/MapControl/WinUI/ImageLoader.WinUI.cs @@ -72,13 +72,15 @@ namespace MapControl internal static async Task LoadWriteableBitmapAsync(Uri uri) { - WriteableBitmap image = null; + WriteableBitmap bitmap = null; try { 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) @@ -86,32 +88,32 @@ namespace MapControl Debug.WriteLine($"{nameof(ImageLoader)}: {uri}: {ex.Message}"); } - return image; + return bitmap; } internal static async Task LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress progress) { - WriteableBitmap mergedImage = null; + WriteableBitmap mergedBitmap = null; 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 && - images[0] != null && - images[1] != null && - images[0].PixelHeight == images[1].PixelHeight) + if (bitmaps.Length == 2 && + bitmaps[0] != null && + bitmaps[1] != null && + bitmaps[0].PixelHeight == bitmaps[1].PixelHeight) { - var buffer1 = images[0].PixelBuffer; - var buffer2 = images[1].PixelBuffer; - var stride1 = (uint)images[0].PixelWidth * 4; - var stride2 = (uint)images[1].PixelWidth * 4; + var buffer1 = bitmaps[0].PixelBuffer; + var buffer2 = bitmaps[1].PixelBuffer; + var stride1 = (uint)bitmaps[0].PixelWidth * 4; + var stride2 = (uint)bitmaps[1].PixelWidth * 4; 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++) { @@ -122,7 +124,7 @@ namespace MapControl progress?.Report(1d); - return mergedImage; + return mergedBitmap; } } }