Skip to content

Commit

Permalink
[Avalonia] Fix Exif orientation, use DispatcherPriority.Send, misc
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Sep 21, 2024
1 parent 1392056 commit 56264cb
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 202 deletions.
2 changes: 2 additions & 0 deletions src/PicView.Avalonia/ImageHandling/ImageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ private static async Task AddImageAsync(FileInfo fileInfo, ImageModel imageModel
bufferSize,
useAsync: fileInfo.Length > 1e7);
Add(fs, imageModel);
imageModel.EXIFOrientation = EXIFHelper.GetImageOrientation(fileInfo);
}

private static async Task AddDefaultImageAsync(FileInfo fileInfo, ImageModel imageModel, bool isThumb, uint height)
Expand Down Expand Up @@ -182,6 +183,7 @@ private static async Task AddDefaultImageAsync(FileInfo fileInfo, ImageModel ima
await magickImage.WriteAsync(memoryStream);
memoryStream.Position = 0;
Add(memoryStream, imageModel);
imageModel.EXIFOrientation = EXIFHelper.GetImageOrientation(magickImage);
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/PicView.Avalonia/ImageHandling/ImageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ public double Rotation

return EXIFOrientation switch
{
EXIFHelper.EXIFOrientation.None or EXIFHelper.EXIFOrientation.Normal
or EXIFHelper.EXIFOrientation.Flipped => 0,
EXIFHelper.EXIFOrientation.Rotated180 or EXIFHelper.EXIFOrientation.Rotated180Flipped => 180,
EXIFHelper.EXIFOrientation.Rotated270Flipped or EXIFHelper.EXIFOrientation.Rotated270 => 270,
EXIFHelper.EXIFOrientation.Rotated90 or EXIFHelper.EXIFOrientation.Rotated90Flipped => 90,
EXIFHelper.EXIFOrientation.None or EXIFHelper.EXIFOrientation.Horizontal
or EXIFHelper.EXIFOrientation.MirrorHorizontal => 0,
EXIFHelper.EXIFOrientation.Rotate180 or EXIFHelper.EXIFOrientation.MirrorVertical => 180,
EXIFHelper.EXIFOrientation.MirrorHorizontalRotate270Cw or EXIFHelper.EXIFOrientation.Rotated270Cw => 90,
EXIFHelper.EXIFOrientation.Rotate90Cw => 90,
EXIFHelper.EXIFOrientation.MirrorHorizontalRotate90Cw => 270,
_ => 0
};
}
Expand Down
93 changes: 16 additions & 77 deletions src/PicView.Avalonia/Navigation/ExifHandling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,83 +12,6 @@ namespace PicView.Avalonia.Navigation;

public static class ExifHandling
{
public static void SetImageModel(ImageModel imageModel, MainViewModel vm)
{
vm.FileInfo = imageModel?.FileInfo ?? null;
if (imageModel?.EXIFOrientation.HasValue ?? false)
{
switch (imageModel.EXIFOrientation.Value)
{
default:
vm.ScaleX = 1;
vm.RotationAngle = 0;
vm.GetOrientation = string.Empty;
break;

case EXIFHelper.EXIFOrientation.Normal:
vm.ScaleX = 1;
vm.RotationAngle = 0;
vm.GetOrientation = TranslationHelper.Translation.Normal;
break;

case EXIFHelper.EXIFOrientation.Flipped:
vm.ScaleX = -1;
vm.RotationAngle = 0;
vm.GetOrientation = TranslationHelper.Translation.Flipped;
break;

case EXIFHelper.EXIFOrientation.Rotated180:
vm.RotationAngle = 180;
vm.ScaleX = 1;
vm.GetOrientation = $"{TranslationHelper.Translation.Rotated} 180\u00b0";
break;

case EXIFHelper.EXIFOrientation.Rotated180Flipped:
vm.RotationAngle = 180;
vm.ScaleX = -1;
vm.GetOrientation =
$"{TranslationHelper.Translation.Rotated} 180\u00b0, {TranslationHelper.Translation.Flipped}";
break;

case EXIFHelper.EXIFOrientation.Rotated270Flipped:
vm.RotationAngle = 270;
vm.ScaleX = -1;
vm.GetOrientation =
$"{TranslationHelper.Translation.Rotated} 270\u00b0, {TranslationHelper.Translation.Flipped}";
break;

case EXIFHelper.EXIFOrientation.Rotated90:
vm.RotationAngle = 90;
vm.ScaleX = 1;
vm.GetOrientation = $"{TranslationHelper.Translation.Rotated} 90\u00b0";
break;

case EXIFHelper.EXIFOrientation.Rotated90Flipped:
vm.RotationAngle = 90;
vm.ScaleX = -1;
vm.GetOrientation =
$"{TranslationHelper.Translation.Rotated} 90\u00b0, {TranslationHelper.Translation.Flipped}";
break;

case EXIFHelper.EXIFOrientation.Rotated270:
vm.RotationAngle = 270;
vm.ScaleX = 1;
vm.GetOrientation = $"{TranslationHelper.Translation.Rotated} 270\u00b0";
break;
}
}
else
{
vm.ScaleX = 1;
vm.RotationAngle = 0;
vm.GetOrientation = string.Empty;
}

vm.ZoomValue = 1;
vm.PixelWidth = imageModel?.PixelWidth ?? 0;
vm.PixelHeight = imageModel?.PixelHeight ?? 0;
}

public static void UpdateExifValues(ImageModel imageModel, MainViewModel vm)
{
if (vm.FileInfo is null || vm is { PixelWidth: <= 0, PixelHeight: <= 0 })
Expand Down Expand Up @@ -131,6 +54,22 @@ public static void UpdateExifValues(ImageModel imageModel, MainViewModel vm)
}
}

vm.GetOrientation = imageModel.EXIFOrientation switch
{
EXIFHelper.EXIFOrientation.Horizontal => TranslationHelper.Translation.Normal,
EXIFHelper.EXIFOrientation.MirrorHorizontal => TranslationHelper.Translation.Flipped,
EXIFHelper.EXIFOrientation.Rotate180 => $"{TranslationHelper.Translation.Rotated} 180\u00b0",
EXIFHelper.EXIFOrientation.MirrorVertical =>
$"{TranslationHelper.Translation.Rotated} 180\u00b0, {TranslationHelper.Translation.Flipped}",
EXIFHelper.EXIFOrientation.MirrorHorizontalRotate270Cw =>
$"{TranslationHelper.Translation.Rotated} 270\u00b0, {TranslationHelper.Translation.Flipped}",
EXIFHelper.EXIFOrientation.Rotate90Cw => $"{TranslationHelper.Translation.Rotated} 90\u00b0",
EXIFHelper.EXIFOrientation.MirrorHorizontalRotate90Cw =>
$"{TranslationHelper.Translation.Rotated} 90\u00b0, {TranslationHelper.Translation.Flipped}",
EXIFHelper.EXIFOrientation.Rotated270Cw => $"{TranslationHelper.Translation.Rotated} 270\u00b0",
_ => string.Empty
};

var meter = TranslationHelper.Translation.Meter;
var cm = TranslationHelper.Translation.Centimeters;
var mp = TranslationHelper.Translation.MegaPixels;
Expand Down
48 changes: 24 additions & 24 deletions src/PicView.Avalonia/Navigation/ImageIterator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void InitiateFileSystemWatcher(FileInfo fileInfo)

_watcher = new FileSystemWatcher();
#if DEBUG
Debug.Assert(fileInfo.DirectoryName != null, "fileInfo.DirectoryName != null");
Debug.Assert(fileInfo.DirectoryName != null);
#endif
_watcher.Path = fileInfo.DirectoryName;
_watcher.EnableRaisingEvents = true;
Expand Down Expand Up @@ -503,8 +503,8 @@ public async Task IterateToIndex(int index)
ErrorHandling.ShowStartUpMenu(_vm);
return;
}
await Task.Run(async () =>

await Task.Run(async () =>
{
try
{
Expand Down Expand Up @@ -580,7 +580,8 @@ await Dispatcher.UIThread.InvokeAsync(() =>
});
}
await PreLoader.PreLoadAsync(CurrentIndex, ImagePaths.Count, IsReversed, ImagePaths).ConfigureAwait(false);
await PreLoader.PreLoadAsync(CurrentIndex, ImagePaths.Count, IsReversed, ImagePaths)
.ConfigureAwait(false);
}
await AddAsync(index, preloadValue.ImageModel).ConfigureAwait(false);
Expand All @@ -593,10 +594,10 @@ await Dispatcher.UIThread.InvokeAsync(() =>
}
catch (Exception e)
{
#if DEBUG
#if DEBUG
Console.WriteLine($"{nameof(IterateToIndex)} exception: \n{e.Message}");
await TooltipHelper.ShowTooltipMessageAsync(e.Message);
#endif
#endif
}
finally
{
Expand Down Expand Up @@ -696,31 +697,30 @@ private async Task UpdateSource(int index, PreLoader.PreLoadValue? preLoadValue,
}

_vm.IsLoading = false;
ExifHandling.SetImageModel(preLoadValue.ImageModel, _vm);
if (SettingsHelper.Settings.ImageScaling.ShowImageSideBySide)

await Dispatcher.UIThread.InvokeAsync(() =>
{
_vm.SecondaryImageSource = nextPreloadValue.ImageModel.Image;
}
_vm.ImageViewer.SetTransform(preLoadValue.ImageModel.EXIFOrientation);
if (SettingsHelper.Settings.ImageScaling.ShowImageSideBySide)
{
_vm.SecondaryImageSource = nextPreloadValue.ImageModel.Image;
}
_vm.ImageSource = preLoadValue.ImageModel.Image;
if (preLoadValue.ImageModel.ImageType is ImageType.AnimatedGif or ImageType.AnimatedWebp)
{
_vm.ImageViewer.MainImage.InitialAnimatedSource = preLoadValue.ImageModel.FileInfo.FullName;
}
_vm.ImageSource = preLoadValue.ImageModel.Image;
if (preLoadValue.ImageModel.ImageType is ImageType.AnimatedGif or ImageType.AnimatedWebp)
{
_vm.ImageViewer.MainImage.InitialAnimatedSource = preLoadValue.ImageModel.FileInfo.FullName;
}
_vm.ImageType = preLoadValue.ImageModel.ImageType;
await Dispatcher.UIThread.InvokeAsync(() =>
{
_vm.ImageType = preLoadValue.ImageModel.ImageType;
WindowHelper.SetSize(preLoadValue.ImageModel.PixelWidth, preLoadValue.ImageModel.PixelHeight,
nextPreloadValue?.ImageModel?.PixelWidth ?? 0, nextPreloadValue?.ImageModel?.PixelHeight ?? 0,
preLoadValue.ImageModel.Rotation, _vm);
});
SetTitleHelper.SetTitle(_vm, preLoadValue.ImageModel);
}, DispatcherPriority.Send);

if (_vm.RotationAngle != 0)
{
await Dispatcher.UIThread.InvokeAsync(() => { _vm.ImageViewer.Rotate(_vm.RotationAngle); });
}

SetTitleHelper.SetTitle(_vm, preLoadValue.ImageModel);

if (SettingsHelper.Settings.WindowProperties.KeepCentered)
{
Expand Down
3 changes: 0 additions & 3 deletions src/PicView.Avalonia/Navigation/NavigationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,6 @@ public static async Task LoadPicFromUrlAsync(string url, MainViewModel vm)
var imageModel = await ImageHelper.GetImageModelAsync(fileInfo).ConfigureAwait(false);
SetSingleImage(imageModel.Image, imageModel.ImageType, url, vm);
vm.FileInfo = fileInfo;
ExifHandling.SetImageModel(imageModel, vm);
ExifHandling.UpdateExifValues(imageModel, vm);
FileHistoryNavigation.Add(url);

Expand Down Expand Up @@ -395,7 +394,6 @@ await Task.Run(async () =>
ImageType = ImageType.Bitmap
};
SetSingleImage(imageModel.Image, imageModel.ImageType, TranslationHelper.Translation.Base64Image, vm);
ExifHandling.SetImageModel(imageModel, vm);
ExifHandling.UpdateExifValues(imageModel, vm);
}
catch (Exception e)
Expand Down Expand Up @@ -551,7 +549,6 @@ public static void SetSingleImage(object source, ImageType imageType, string nam
private static async Task PreviewPicAndLoadGallery(FileInfo fileInfo, MainViewModel vm, List<string>? files = null)
{
var imageModel = await ImageHelper.GetImageModelAsync(fileInfo).ConfigureAwait(false);
ExifHandling.SetImageModel(imageModel, vm);
vm.ImageSource = imageModel.Image;
vm.ImageType = imageModel.ImageType;
WindowHelper.SetSize(imageModel.PixelWidth, imageModel.PixelHeight, 0,0, imageModel.Rotation, vm);
Expand Down
28 changes: 2 additions & 26 deletions src/PicView.Avalonia/Navigation/QuickLoad.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using Avalonia.Threading;
using PicView.Avalonia.Gallery;
using PicView.Avalonia.Gallery;
using PicView.Avalonia.ImageHandling;
using PicView.Avalonia.UI;
using PicView.Avalonia.ViewModels;
using PicView.Core.Config;
using PicView.Core.FileHandling;
using PicView.Core.Gallery;
using PicView.Core.ImageDecoding;

namespace PicView.Avalonia.Navigation;

Expand Down Expand Up @@ -42,31 +40,9 @@ public static async Task QuickLoadAsync(MainViewModel vm, string file)
secondaryPreloadValue = await vm.ImageIterator.GetNextPreLoadValueAsync();
vm.SecondaryImageSource = secondaryPreloadValue?.ImageModel?.Image;
}
vm.ImageViewer.SetTransform(imageModel.EXIFOrientation);
WindowHelper.SetSize(imageModel.PixelWidth, imageModel.PixelHeight, secondaryPreloadValue?.ImageModel?.PixelWidth ?? 0, secondaryPreloadValue?.ImageModel?.PixelHeight ?? 0, imageModel.Rotation, vm);
vm.IsLoading = false;
imageModel.EXIFOrientation = EXIFHelper.GetImageOrientation(filePath: file);
ExifHandling.SetImageModel(imageModel, vm);
var changed = false; // Need to recalculate size if changed
if (vm.ScaleX != 1)
{
await Dispatcher.UIThread.InvokeAsync(() =>
{
vm.ImageViewer.SetScaleX();
});
changed = true;
}
if (vm.RotationAngle != 0)
{
await Dispatcher.UIThread.InvokeAsync(() =>
{
vm.ImageViewer.Rotate(vm.RotationAngle);
});
changed = true;
}
if (changed)
{
WindowHelper.SetSize(imageModel.PixelWidth, imageModel.PixelHeight, secondaryPreloadValue?.ImageModel?.PixelWidth ?? 0, secondaryPreloadValue?.ImageModel?.PixelHeight ?? 0, imageModel.Rotation, vm);
}

ExifHandling.UpdateExifValues(imageModel, vm);
vm.ImageIterator ??= new ImageIterator(fileInfo, vm);
Expand Down
2 changes: 1 addition & 1 deletion src/PicView.Avalonia/UI/WindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ public static void SetSize(double width, double height, double secondWidth, doub
desktopMinHeight = desktop.MainWindow.MinHeight;
containerWidth = mainView.Bounds.Width;
containerHeight = mainView.Bounds.Height;
}, DispatcherPriority.Normal).Wait();
}, DispatcherPriority.Send).Wait();
}

if (double.IsNaN(containerWidth) || double.IsNaN(containerHeight) || double.IsNaN(width) || double.IsNaN(height))
Expand Down
Loading

0 comments on commit 56264cb

Please sign in to comment.