Skip to content

Commit 37f71f4

Browse files
committed
[Avalonia] Side by side fix rotation calculation #89. Refactoring, error handling, etc.
1 parent 78d877a commit 37f71f4

File tree

4 files changed

+103
-21
lines changed

4 files changed

+103
-21
lines changed

src/PicView.Avalonia/CustomControls/PicBox.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,6 @@ private void RenderImageSideBySide(DrawingContext context, IImage source, IImage
359359
return;
360360
}
361361

362-
// Scale the first image to fit within the remaining width while keeping the same height
363-
var scaledSourceSize = new Size(firstImageWidth, sourceSize.Height * scale);
364-
365362
// Calculate the destination rectangles for both images
366363
var sourceDestRect = new Rect(0, 0, firstImageWidth, viewPort.Height);
367364
var secondaryDestRect = new Rect(firstImageWidth, 0, SecondaryImageWidth, viewPort.Height);

src/PicView.Avalonia/ImageHandling/ImageHelper.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ public static class ImageHelper
138138

139139
private static async Task AddImageAsync(FileInfo fileInfo, ImageModel imageModel)
140140
{
141+
if (fileInfo is null)
142+
{
143+
#if DEBUG
144+
Console.WriteLine($"Error: {nameof(ImageHelper)}:{nameof(AddImageAsync)}: {nameof(fileInfo)} is null");
145+
#endif
146+
return;
147+
}
141148
const int bufferSize = 4096;
142149
await using var fs = new FileStream(
143150
fileInfo.FullName,
@@ -177,15 +184,20 @@ private static async Task AddDefaultImageAsync(FileInfo fileInfo, ImageModel ima
177184
Add(memoryStream, imageModel);
178185
}
179186
}
180-
181-
182187

183188
#endregion
184189

185190
#region Bitmap
186191

187192
private static void Add(Stream stream, ImageModel imageModel)
188193
{
194+
if (stream is null)
195+
{
196+
#if DEBUG
197+
Console.WriteLine($"Error: {nameof(ImageHelper)}:{nameof(Add)}: {nameof(stream)} is null");
198+
#endif
199+
return;
200+
}
189201
var bitmap = new Bitmap(stream);
190202
imageModel.Image = bitmap;
191203
imageModel.PixelWidth = bitmap?.PixelSize.Width ?? 0;

src/PicView.Avalonia/UI/WindowHelper.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
using Avalonia.Controls.ApplicationLifetimes;
55
using Avalonia.Input;
66
using Avalonia.Threading;
7+
using ImageMagick;
78
using PicView.Avalonia.Keybindings;
89
using PicView.Avalonia.Navigation;
910
using PicView.Avalonia.ViewModels;
1011
using PicView.Core.ArchiveHandling;
1112
using PicView.Core.Calculations;
1213
using PicView.Core.Config;
1314
using PicView.Core.FileHandling;
15+
using PicView.Core.Navigation;
1416

1517
namespace PicView.Avalonia.UI;
1618

@@ -418,18 +420,58 @@ public static void SetSize(MainViewModel vm)
418420
return;
419421
}
420422

423+
double firstWidth, firstHeight;
421424
var preloadValue = vm.ImageIterator?.GetCurrentPreLoadValue();
425+
if (preloadValue == null)
426+
{
427+
var magickImage = new MagickImage();
428+
magickImage.Ping(vm.FileInfo);
429+
firstWidth = magickImage.Width;
430+
firstHeight = magickImage.Height;
431+
}
432+
else
433+
{
434+
firstWidth = GetWidth(preloadValue);
435+
firstHeight = GetHeight(preloadValue);
436+
}
422437
if (SettingsHelper.Settings.ImageScaling.ShowImageSideBySide)
423438
{
424439
var secondaryPreloadValue = vm.ImageIterator?.GetNextPreLoadValue();
440+
double secondWidth, secondHeight;
425441
if (secondaryPreloadValue != null)
426442
{
427-
SetSize(preloadValue?.ImageModel?.PixelWidth ?? vm.ImageWidth, preloadValue?.ImageModel?.PixelHeight ?? vm.ImageHeight, secondaryPreloadValue.ImageModel?.PixelWidth ?? vm.ImageWidth, secondaryPreloadValue.ImageModel?.PixelHeight ?? vm.ImageHeight, vm.RotationAngle, vm);
443+
secondWidth = GetWidth(secondaryPreloadValue);
444+
secondHeight = GetHeight(secondaryPreloadValue);
428445
}
446+
else if (vm.ImageIterator is not null)
447+
{
448+
var nextIndex = vm.ImageIterator.GetIteration(vm.ImageIterator.CurrentIndex, vm.ImageIterator.IsReversed ? NavigateTo.Previous : NavigateTo.Next);
449+
var magickImage = new MagickImage();
450+
magickImage.Ping(vm.ImageIterator.ImagePaths[nextIndex]);
451+
secondWidth = magickImage.Width;
452+
secondHeight = magickImage.Height;
453+
}
454+
else
455+
{
456+
secondWidth = 0;
457+
secondHeight = 0;
458+
}
459+
SetSize(firstWidth, firstHeight, secondWidth, secondHeight, vm.RotationAngle, vm);
429460
}
430461
else
431462
{
432-
SetSize(preloadValue?.ImageModel?.PixelWidth ?? vm.ImageWidth, preloadValue?.ImageModel?.PixelHeight ?? vm.ImageHeight, 0, 0, vm.RotationAngle, vm);
463+
SetSize(firstWidth, firstHeight, 0, 0, vm.RotationAngle, vm);
464+
}
465+
466+
return;
467+
double GetWidth(PreLoader.PreLoadValue preloadValue)
468+
{
469+
return preloadValue?.ImageModel?.PixelWidth ?? vm.ImageWidth;
470+
}
471+
472+
double GetHeight(PreLoader.PreLoadValue preloadValue)
473+
{
474+
return preloadValue?.ImageModel?.PixelHeight ?? vm.ImageHeight;
433475
}
434476
}
435477

src/PicView.Core/Calculations/ImageSizeCalculationHelper.cs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,29 +203,60 @@ public static ImageSize GetImageSize(double width,
203203
{
204204
var widthPadding = SettingsHelper.Settings.ImageScaling.StretchImage ? 4 : padding;
205205
var availableWidth = monitorWidth - widthPadding;
206-
207-
// If combined width exceeds available width, scale both images down proportionally
208-
if (combinedWidth > availableWidth)
206+
var availableHeight = monitorHeight - (widthPadding + uiBottomSize + uiTopSize);
207+
if (rotationAngle is 0 or 180)
209208
{
210-
var scaleFactor = availableWidth / combinedWidth;
211-
xWidth1 *= scaleFactor;
212-
xWidth2 *= scaleFactor;
213-
xHeight *= scaleFactor;
209+
// If combined width exceeds available width, scale both images down proportionally
210+
if (combinedWidth > availableWidth)
211+
{
212+
var scaleFactor = availableWidth / combinedWidth;
213+
xWidth1 *= scaleFactor;
214+
xWidth2 *= scaleFactor;
215+
xHeight *= scaleFactor;
214216

215-
combinedWidth = xWidth1 + xWidth2;
217+
combinedWidth = xWidth1 + xWidth2;
218+
}
219+
}
220+
else
221+
{
222+
if (combinedWidth > availableHeight)
223+
{
224+
var scaleFactor = availableHeight / combinedWidth;
225+
xWidth1 *= scaleFactor;
226+
xWidth2 *= scaleFactor;
227+
xHeight *= scaleFactor;
228+
229+
combinedWidth = xWidth1 + xWidth2;
230+
}
216231
}
217232
}
218233
else
219234
{
220-
if (combinedWidth > containerWidth)
235+
if (rotationAngle is 0 or 180)
221236
{
222-
var scaleFactor = containerWidth / combinedWidth;
223-
xWidth1 *= scaleFactor;
224-
xWidth2 *= scaleFactor;
225-
xHeight *= scaleFactor;
237+
if (combinedWidth > containerWidth)
238+
{
239+
var scaleFactor = containerWidth / combinedWidth;
240+
xWidth1 *= scaleFactor;
241+
xWidth2 *= scaleFactor;
242+
xHeight *= scaleFactor;
226243

227-
combinedWidth = xWidth1 + xWidth2;
244+
combinedWidth = xWidth1 + xWidth2;
245+
}
246+
}
247+
else
248+
{
249+
if (combinedWidth > containerHeight)
250+
{
251+
var scaleFactor = containerHeight / combinedWidth;
252+
xWidth1 *= scaleFactor;
253+
xWidth2 *= scaleFactor;
254+
xHeight *= scaleFactor;
255+
256+
combinedWidth = xWidth1 + xWidth2;
257+
}
228258
}
259+
229260
}
230261

231262
double scrollWidth, scrollHeight;

0 commit comments

Comments
 (0)