ImageLoader

This commit is contained in:
ClemensFischer 2025-01-01 19:45:40 +01:00
parent 560f44a139
commit 069072ce34
6 changed files with 73 additions and 71 deletions

View file

@ -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);
});
}

View file

@ -54,36 +54,36 @@ namespace MapControl
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);
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;
}
}
}