2024-05-19 23:23:27 +02:00
|
|
|
|
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
|
2025-01-01 18:57:55 +01:00
|
|
|
|
// Copyright © Clemens Fischer
|
2024-05-19 23:23:27 +02:00
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
|
|
|
|
|
public static partial class ImageLoader
|
|
|
|
|
|
{
|
|
|
|
|
|
public static IImage LoadImage(Uri uri)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static IImage LoadImage(Stream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Bitmap(stream);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Task<IImage> LoadImageAsync(Stream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Task.FromResult(LoadImage(stream));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Task<IImage> LoadImageAsync(string path)
|
|
|
|
|
|
{
|
2024-05-22 16:15:29 +02:00
|
|
|
|
if (!File.Exists(path))
|
2024-05-19 23:23:27 +02:00
|
|
|
|
{
|
2024-05-22 16:15:29 +02:00
|
|
|
|
return Task.FromResult<IImage>(null);
|
|
|
|
|
|
}
|
2024-05-19 23:23:27 +02:00
|
|
|
|
|
2024-05-22 16:15:29 +02:00
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
|
{
|
2024-05-19 23:23:27 +02:00
|
|
|
|
using var stream = File.OpenRead(path);
|
|
|
|
|
|
|
|
|
|
|
|
return LoadImage(stream);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2024-05-21 23:18:00 +02:00
|
|
|
|
|
2024-05-22 16:15:29 +02:00
|
|
|
|
internal static async Task<IImage> LoadMergedImageAsync(Uri uri1, Uri uri2, IProgress<double> progress)
|
2024-05-21 23:18:00 +02:00
|
|
|
|
{
|
2025-01-01 19:45:40 +01:00
|
|
|
|
WriteableBitmap mergedBitmap = null;
|
2025-01-02 10:48:51 +01:00
|
|
|
|
var p1 = 0d;
|
|
|
|
|
|
var p2 = 0d;
|
2024-05-22 16:15:29 +02:00
|
|
|
|
|
2025-01-02 10:48:51 +01:00
|
|
|
|
var images = await Task.WhenAll(
|
|
|
|
|
|
LoadImageAsync(uri1, new Progress<double>(p => { p1 = p; progress.Report((p1 + p2) / 2d); })),
|
|
|
|
|
|
LoadImageAsync(uri2, new Progress<double>(p => { p2 = p; progress.Report((p1 + p2) / 2d); })));
|
2024-05-22 16:15:29 +02:00
|
|
|
|
|
|
|
|
|
|
if (images.Length == 2 &&
|
2025-01-01 19:45:40 +01:00
|
|
|
|
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)
|
2024-05-22 16:15:29 +02:00
|
|
|
|
{
|
2025-01-01 19:45:40 +01:00
|
|
|
|
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;
|
2024-05-22 16:45:34 +02:00
|
|
|
|
var stride = bpp * pixelSize.Width;
|
|
|
|
|
|
var bufferSize = stride * pixelSize.Height;
|
2024-05-22 16:15:29 +02:00
|
|
|
|
|
|
|
|
|
|
unsafe
|
|
|
|
|
|
{
|
2024-05-22 16:45:34 +02:00
|
|
|
|
fixed (byte* ptr = new byte[stride * pixelSize.Height])
|
2024-05-22 16:15:29 +02:00
|
|
|
|
{
|
2024-05-22 16:45:34 +02:00
|
|
|
|
var buffer = (nint)ptr;
|
2024-05-22 16:15:29 +02:00
|
|
|
|
|
2025-01-01 19:45:40 +01:00
|
|
|
|
bitmap1.CopyPixels(new PixelRect(bitmap1.PixelSize), buffer, bufferSize, stride);
|
|
|
|
|
|
bitmap2.CopyPixels(new PixelRect(bitmap2.PixelSize), buffer + stride1, bufferSize, stride);
|
2024-05-22 16:15:29 +02:00
|
|
|
|
|
2025-01-01 19:45:40 +01:00
|
|
|
|
mergedBitmap = new WriteableBitmap(bitmap1.Format.Value, bitmap1.AlphaFormat.Value, buffer, pixelSize, bitmap1.Dpi, stride);
|
2024-05-22 16:15:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-01 19:45:40 +01:00
|
|
|
|
return mergedBitmap;
|
2024-05-21 23:18:00 +02:00
|
|
|
|
}
|
2024-05-19 23:23:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|