Skip to content

Commit

Permalink
[Avalonia] Side by side fix rotation calculation #89. Refactoring, er…
Browse files Browse the repository at this point in the history
…ror handling, etc.
  • Loading branch information
Ruben2776 committed Sep 7, 2024
1 parent 78d877a commit 37f71f4
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 21 deletions.
3 changes: 0 additions & 3 deletions src/PicView.Avalonia/CustomControls/PicBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,6 @@ private void RenderImageSideBySide(DrawingContext context, IImage source, IImage
return;
}

// Scale the first image to fit within the remaining width while keeping the same height
var scaledSourceSize = new Size(firstImageWidth, sourceSize.Height * scale);

// Calculate the destination rectangles for both images
var sourceDestRect = new Rect(0, 0, firstImageWidth, viewPort.Height);
var secondaryDestRect = new Rect(firstImageWidth, 0, SecondaryImageWidth, viewPort.Height);
Expand Down
16 changes: 14 additions & 2 deletions src/PicView.Avalonia/ImageHandling/ImageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ public static class ImageHelper

private static async Task AddImageAsync(FileInfo fileInfo, ImageModel imageModel)
{
if (fileInfo is null)
{
#if DEBUG
Console.WriteLine($"Error: {nameof(ImageHelper)}:{nameof(AddImageAsync)}: {nameof(fileInfo)} is null");
#endif
return;
}
const int bufferSize = 4096;
await using var fs = new FileStream(
fileInfo.FullName,
Expand Down Expand Up @@ -177,15 +184,20 @@ private static async Task AddDefaultImageAsync(FileInfo fileInfo, ImageModel ima
Add(memoryStream, imageModel);
}
}



#endregion

#region Bitmap

private static void Add(Stream stream, ImageModel imageModel)
{
if (stream is null)
{
#if DEBUG
Console.WriteLine($"Error: {nameof(ImageHelper)}:{nameof(Add)}: {nameof(stream)} is null");
#endif
return;
}
var bitmap = new Bitmap(stream);
imageModel.Image = bitmap;
imageModel.PixelWidth = bitmap?.PixelSize.Width ?? 0;
Expand Down
46 changes: 44 additions & 2 deletions src/PicView.Avalonia/UI/WindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Input;
using Avalonia.Threading;
using ImageMagick;
using PicView.Avalonia.Keybindings;
using PicView.Avalonia.Navigation;
using PicView.Avalonia.ViewModels;
using PicView.Core.ArchiveHandling;
using PicView.Core.Calculations;
using PicView.Core.Config;
using PicView.Core.FileHandling;
using PicView.Core.Navigation;

namespace PicView.Avalonia.UI;

Expand Down Expand Up @@ -418,18 +420,58 @@ public static void SetSize(MainViewModel vm)
return;
}

double firstWidth, firstHeight;
var preloadValue = vm.ImageIterator?.GetCurrentPreLoadValue();
if (preloadValue == null)
{
var magickImage = new MagickImage();
magickImage.Ping(vm.FileInfo);
firstWidth = magickImage.Width;
firstHeight = magickImage.Height;
}
else
{
firstWidth = GetWidth(preloadValue);
firstHeight = GetHeight(preloadValue);
}
if (SettingsHelper.Settings.ImageScaling.ShowImageSideBySide)
{
var secondaryPreloadValue = vm.ImageIterator?.GetNextPreLoadValue();
double secondWidth, secondHeight;
if (secondaryPreloadValue != null)
{
SetSize(preloadValue?.ImageModel?.PixelWidth ?? vm.ImageWidth, preloadValue?.ImageModel?.PixelHeight ?? vm.ImageHeight, secondaryPreloadValue.ImageModel?.PixelWidth ?? vm.ImageWidth, secondaryPreloadValue.ImageModel?.PixelHeight ?? vm.ImageHeight, vm.RotationAngle, vm);
secondWidth = GetWidth(secondaryPreloadValue);
secondHeight = GetHeight(secondaryPreloadValue);
}
else if (vm.ImageIterator is not null)
{
var nextIndex = vm.ImageIterator.GetIteration(vm.ImageIterator.CurrentIndex, vm.ImageIterator.IsReversed ? NavigateTo.Previous : NavigateTo.Next);
var magickImage = new MagickImage();
magickImage.Ping(vm.ImageIterator.ImagePaths[nextIndex]);
secondWidth = magickImage.Width;
secondHeight = magickImage.Height;
}
else
{
secondWidth = 0;
secondHeight = 0;
}
SetSize(firstWidth, firstHeight, secondWidth, secondHeight, vm.RotationAngle, vm);
}
else
{
SetSize(preloadValue?.ImageModel?.PixelWidth ?? vm.ImageWidth, preloadValue?.ImageModel?.PixelHeight ?? vm.ImageHeight, 0, 0, vm.RotationAngle, vm);
SetSize(firstWidth, firstHeight, 0, 0, vm.RotationAngle, vm);
}

return;
double GetWidth(PreLoader.PreLoadValue preloadValue)
{
return preloadValue?.ImageModel?.PixelWidth ?? vm.ImageWidth;
}

double GetHeight(PreLoader.PreLoadValue preloadValue)
{
return preloadValue?.ImageModel?.PixelHeight ?? vm.ImageHeight;
}
}

Expand Down
59 changes: 45 additions & 14 deletions src/PicView.Core/Calculations/ImageSizeCalculationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,29 +203,60 @@ public static ImageSize GetImageSize(double width,
{
var widthPadding = SettingsHelper.Settings.ImageScaling.StretchImage ? 4 : padding;
var availableWidth = monitorWidth - widthPadding;

// If combined width exceeds available width, scale both images down proportionally
if (combinedWidth > availableWidth)
var availableHeight = monitorHeight - (widthPadding + uiBottomSize + uiTopSize);
if (rotationAngle is 0 or 180)
{
var scaleFactor = availableWidth / combinedWidth;
xWidth1 *= scaleFactor;
xWidth2 *= scaleFactor;
xHeight *= scaleFactor;
// If combined width exceeds available width, scale both images down proportionally
if (combinedWidth > availableWidth)
{
var scaleFactor = availableWidth / combinedWidth;
xWidth1 *= scaleFactor;
xWidth2 *= scaleFactor;
xHeight *= scaleFactor;

combinedWidth = xWidth1 + xWidth2;
combinedWidth = xWidth1 + xWidth2;
}
}
else
{
if (combinedWidth > availableHeight)
{
var scaleFactor = availableHeight / combinedWidth;
xWidth1 *= scaleFactor;
xWidth2 *= scaleFactor;
xHeight *= scaleFactor;

combinedWidth = xWidth1 + xWidth2;
}
}
}
else
{
if (combinedWidth > containerWidth)
if (rotationAngle is 0 or 180)
{
var scaleFactor = containerWidth / combinedWidth;
xWidth1 *= scaleFactor;
xWidth2 *= scaleFactor;
xHeight *= scaleFactor;
if (combinedWidth > containerWidth)
{
var scaleFactor = containerWidth / combinedWidth;
xWidth1 *= scaleFactor;
xWidth2 *= scaleFactor;
xHeight *= scaleFactor;

combinedWidth = xWidth1 + xWidth2;
combinedWidth = xWidth1 + xWidth2;
}
}
else
{
if (combinedWidth > containerHeight)
{
var scaleFactor = containerHeight / combinedWidth;
xWidth1 *= scaleFactor;
xWidth2 *= scaleFactor;
xHeight *= scaleFactor;

combinedWidth = xWidth1 + xWidth2;
}
}

}

double scrollWidth, scrollHeight;
Expand Down

0 comments on commit 37f71f4

Please sign in to comment.