Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions PhotoLocator/JpegTransformCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ await Task.Run(() =>
var selectedItem = _mainViewModel.SelectedItem!;
using (var cursor = new MouseCursorOverride())
{
(var image, metadata) = await Task.Run(() => LoadImageWithMetadata(selectedItem));
(var image, metadata) = await Task.Run(() => LoadImageWithMetadataAsync(selectedItem));
localContrastViewModel = new LocalContrastViewModel() { SourceBitmap = image };
}
var window = new LocalContrastView();
Expand All @@ -128,10 +128,10 @@ await Task.Run(() =>
await SaveProcessedImageAsync(localContrastViewModel, metadata, selectedItem);
}, HasFileSelected);

private static (BitmapSource, BitmapMetadata?) LoadImageWithMetadata(PictureItemViewModel item)
private static async Task<(BitmapSource, BitmapMetadata?)> LoadImageWithMetadataAsync(PictureItemViewModel item)
{
BitmapMetadata? metadata = null;
var image = item.LoadPreview(default, int.MaxValue, true);
var image = await item.LoadPreviewAsync(default, int.MaxValue, preservePixelFormat: true);
try
{
using var file = File.OpenRead(item.FullPath);
Expand Down Expand Up @@ -176,7 +176,7 @@ await Task.Run(() => GeneralFileFormatHandler.SaveToFile(localContrastViewModel.

private async Task BatchProcessLocalContrastAsync(LocalContrastViewModel localContrastViewModel, BitmapMetadata? metadata, PictureItemViewModel[] allSelected, PictureItemViewModel selectedItem)
{
await _mainViewModel.RunProcessWithProgressBarAsync((progressCallback, ct) => Task.Run(() =>
await _mainViewModel.RunProcessWithProgressBarAsync((progressCallback, ct) => Task.Run(async () =>
{
int i = 0;
foreach (var item in allSelected)
Expand All @@ -189,7 +189,7 @@ await _mainViewModel.RunProcessWithProgressBarAsync((progressCallback, ct) => Ta
}
else
{
var (image, itemMetadata) = LoadImageWithMetadata(item);
var (image, itemMetadata) = await LoadImageWithMetadataAsync(item);
image = localContrastViewModel.ApplyOperations(image);
GeneralFileFormatHandler.SaveToFile(image, targetFileName,
ExifHandler.ResetOrientation(itemMetadata), _mainViewModel.Settings.JpegQuality);
Expand Down
8 changes: 6 additions & 2 deletions PhotoLocator/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ public async Task UpdatePreviewPictureAsync(string? skipTo = null)
{
while (_pictureCache.Count > 3)
_pictureCache.RemoveAt(0);
var loaded = await Task.Run(() => selected.LoadPreview(ct, skipTo: skipTo), ct);
var loaded = await Task.Run(() => selected.LoadPreviewAsync(ct, skipTo: skipTo), ct);
if (loaded is not null && skipTo is null)
_pictureCache.Add((selected.FullPath, loaded));
if (selected != SelectedItem) // If another item was selected while preview was being loaded
Expand Down Expand Up @@ -696,7 +696,11 @@ void AddLineSeg(double azimuth, Color color, string text)
PhotoFolderPath = browser.SelectedPath;
});

public ICommand RefreshFolderCommand => new RelayCommand(o => LoadFolderContentsAsync(true).WithExceptionLogging());
public ICommand RefreshFolderCommand => new RelayCommand(o =>
{
_pictureCache.Clear();
LoadFolderContentsAsync(true).WithExceptionLogging();
});

public Action<object>? FocusListBoxItem { get; internal set; }

Expand Down
6 changes: 3 additions & 3 deletions PhotoLocator/PictureFileFormats/CR3FileFormatHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ public static BitmapSource LoadFromStream(Stream stream, Rotation rotation, int
ct.ThrowIfCancellationRequested();

//TODO: There is a risk that the headers cross buffer block boundaries in which case we currently fail to find them
var index = buffer.Slice(0, length).IndexOf(_previewHeader);
var index = buffer[..length].IndexOf(_previewHeader);
if (index < 0)
{
length = stream.Read(buffer);
continue;
}
var index2 = buffer.Slice(index, length - index).IndexOf(_jpegHeader);
var index2 = buffer[index..length].IndexOf(_jpegHeader);
if (index2 < 0)
{
length = stream.Read(buffer);
index2 = buffer.Slice(0, length).IndexOf(_jpegHeader);
index2 = buffer[..length].IndexOf(_jpegHeader);
if (index2 < 0)
continue;
index = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static BitmapSource LoadFromStream(Stream source, Rotation rotation, int
if (maxPixelWidth < int.MaxValue)
bitmap.DecodePixelWidth = maxPixelWidth;
if (preservePixelFormat)
bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
bitmap.CreateOptions = BitmapCreateOptions.PreservePixelFormat | BitmapCreateOptions.IgnoreColorProfile;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
Comment thread
meesoft marked this conversation as resolved.
bitmap.Rotation = rotation;
ct.ThrowIfCancellationRequested();
Expand Down
4 changes: 3 additions & 1 deletion PhotoLocator/PictureItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,12 @@ private async Task LoadMetadataAsync(CancellationToken ct)
}
}

public BitmapSource? LoadPreview(CancellationToken ct, int maxWidth = int.MaxValue, bool preservePixelFormat = false, string? skipTo = null)
public async Task<BitmapSource?> LoadPreviewAsync(CancellationToken ct, int maxWidth = int.MaxValue, bool preservePixelFormat = false, string? skipTo = null)
{
try
{
if (_metadataString is null && IsFile)
await LoadMetadataAsync(ct);
Log.Write("Loading preview of " + Name);
var sw = Stopwatch.StartNew();
var preview = LoadPreviewInternal(maxWidth, preservePixelFormat, skipTo, ct);
Expand Down
6 changes: 2 additions & 4 deletions PhotoLocator/SlideShowWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ async Task UpdatePictureAsync()
_timer.Stop();
await (_resamplerCancellation?.CancelAsync() ?? Task.CompletedTask);
SelectedPicture = _pictures[PictureIndex];
if (SelectedPicture.ThumbnailImage is null)
await SelectedPicture.LoadThumbnailAndMetadataAsync(default);

if (SelectedPicture.IsVideo)
{
Expand All @@ -133,7 +131,7 @@ async Task UpdatePictureAsync()
else
{
_timer.Start();
_sourceImage = SelectedPicture.LoadPreview(default);
_sourceImage = await SelectedPicture.LoadPreviewAsync(default);
UpdateResampledImage();
}
Comment thread
meesoft marked this conversation as resolved.

Expand Down Expand Up @@ -178,7 +176,7 @@ private async void UpdateResampledImage()
var MaxHeight = ScreenGrid.ActualHeight * screenDpi.DpiScaleY;
var scale = Math.Min(maxWidth / _sourceImage.PixelWidth, MaxHeight / _sourceImage.PixelHeight);
var resizeOperation = new LanczosResizeOperation();
_resamplerCancellation = new CancellationTokenSource();
_resamplerCancellation = new CancellationTokenSource(); // Runs in the main thread, so it is safe to access the token
var resampled = await Task.Run(() => resizeOperation.Apply(_sourceImage,
(int)(_sourceImage.PixelWidth * scale), (int)(_sourceImage.PixelHeight * scale),
screenDpi.PixelsPerInchX, screenDpi.PixelsPerInchY, _resamplerCancellation.Token), _resamplerCancellation.Token);
Expand Down
4 changes: 2 additions & 2 deletions PhotoLocator/VideoTransformCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public bool IsLocalContrastChecked
}
}

public ICommand SetupLocalContrastCommand => new RelayCommand(o =>
public ICommand SetupLocalContrastCommand => new RelayCommand(async o =>
{
_localContrastSetup ??= new LocalContrastViewModel();
if (_localContrastSetup.SourceBitmap is null)
Expand All @@ -361,7 +361,7 @@ public bool IsLocalContrastChecked
try
{
var firstSelected = _mainViewModel.GetSelectedItems(true).First();
var preview = firstSelected.LoadPreview(default, skipTo: HasSingleInput && !string.IsNullOrEmpty(SkipTo) ? SkipTo : null)
var preview = await firstSelected.LoadPreviewAsync(default, preservePixelFormat: true, skipTo: HasSingleInput && !string.IsNullOrEmpty(SkipTo) ? SkipTo : null)
?? throw new FileFormatException("LoadPreview returned null");
_localContrastSetup.SourceBitmap = preview;
}
Expand Down
Loading