Skip to content

Commit

Permalink
Make sure images are disposed, improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
DSPaul committed Sep 12, 2024
1 parent 8e78b7c commit 1e9bfa4
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 31 deletions.
6 changes: 5 additions & 1 deletion src/Converters/UriToBitmapConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ class UriToBitmapConverter : IValueConverter
{
bi.UriSource = new Uri(path);
bi.EndInit();
if (bi.CanFreeze)
{
bi.Freeze();
}
return bi;
}
catch
catch
{
return null;
}
Expand Down
57 changes: 38 additions & 19 deletions src/Services/CoverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static async Task GetCover(List<Codex> codices)
}
}

public static void SaveCover(IMagickImage image, Codex destCodex)
public static void SaveCover(Codex destCodex, IMagickImage image)
{
if (String.IsNullOrEmpty(destCodex.CoverArt))
{
Expand All @@ -121,8 +121,7 @@ public static void SaveCover(IMagickImage image, Codex destCodex)
if (image.Width > 850) image.Resize(850, 0);

image.Write(destCodex.CoverArt);
image.Dispose();
CreateThumbnail(destCodex);
CreateThumbnail(destCodex, image);
}

public static async Task SaveCover(string imgURL, Codex destCodex)
Expand All @@ -136,6 +135,7 @@ public static async Task SaveCover(string imgURL, Codex destCodex)
var imgBytes = await IOService.DownloadFileAsync(imgURL);
await File.WriteAllBytesAsync(destCodex.CoverArt, imgBytes);
CreateThumbnail(destCodex);
destCodex.RefreshThumbnail();
}

public static bool GetCoverFromImage(string imagePath, Codex destCodex)
Expand All @@ -156,10 +156,13 @@ public static bool GetCoverFromImage(string imagePath, Codex destCodex)

try
{
using MagickImage image = new(imagePath);
if (image.Width > 1000) image.Resize(1000, 0);
image.Write(destCodex.CoverArt);
CreateThumbnail(destCodex);
using (MagickImage image = new(imagePath))
{
if (image.Width > 1000) image.Resize(1000, 0);
image.Write(destCodex.CoverArt);
CreateThumbnail(destCodex, image);
}
destCodex.RefreshThumbnail();
return true;
}
catch (Exception ex)
Expand All @@ -170,25 +173,41 @@ public static bool GetCoverFromImage(string imagePath, Codex destCodex)
}
}

public static void CreateThumbnail(Codex c)
public static void CreateThumbnail(Codex c, IMagickImage? image = null)
{
if (String.IsNullOrEmpty(c.CoverArt))
if (String.IsNullOrEmpty(c.Thumbnail))
{
Logger.Error("Trying to write thumbnail to empty path", new InvalidOperationException());
return;
}

uint newWidth = 200; //sets resolution of thumbnail in pixels
if (!Path.Exists(c.CoverArt)) return;
using MagickImage image = new(c.CoverArt);
//preserve aspect ratio
uint width = image.Width;
uint height = image.Height;
uint newHeight = newWidth / width * height;
//create thumbnail
image.Thumbnail(newWidth, newHeight);
image.Write(c.Thumbnail);
c.RefreshThumbnail();
bool ownsImage = false;

if (image is null)
{
if (!Path.Exists(c.CoverArt)) return;
image = new MagickImage(c.CoverArt);
ownsImage = true;
}

try
{
//preserve aspect ratio
uint width = image.Width;
uint height = image.Height;
uint newHeight = newWidth / width * height;
//create thumbnail
image.Thumbnail(newWidth, newHeight);
image.Write(c.Thumbnail);
}
finally
{
if (ownsImage)
{
image?.Dispose();
}
}
}

//Take screenshot of specific html element
Expand Down
1 change: 1 addition & 0 deletions src/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ public void RegenAllThumbnails()
{
//codex.Thumbnail = codex.CoverArt.Replace("CoverArt", "Thumbnails");
CoverService.CreateThumbnail(codex);
codex.RefreshThumbnail();
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/ViewModels/Sources/GmBinderSourceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ public override async Task<bool> FetchCover(Codex codex)
await Task.Run(() => driver.Navigate().GoToUrl(codex.Sources.SourceURL));
var coverPage = driver.FindElement(OpenQA.Selenium.By.Id("p1"));
//screenshot and download the image
IMagickImage image = CoverService.GetCroppedScreenShot(driver, coverPage);
CoverService.SaveCover(image, codex);
using (IMagickImage image = CoverService.GetCroppedScreenShot(driver, coverPage))
{
CoverService.SaveCover(codex, image);
}
codex.RefreshThumbnail();
return true;
}
catch (Exception ex)
Expand Down
7 changes: 5 additions & 2 deletions src/ViewModels/Sources/HomebrewerySourceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@ await Task.Run(() =>
location.Y += coverPage.Location.Y;

//screenshot and download the image
IMagickImage image = CoverService.GetCroppedScreenShot(driver, location, coverPage.Size);
CoverService.SaveCover(image, codex);
using (IMagickImage image = CoverService.GetCroppedScreenShot(driver, location, coverPage.Size))
{
CoverService.SaveCover(codex, image);
}
codex.RefreshThumbnail();
return true;
}
catch (Exception ex)
Expand Down
17 changes: 10 additions & 7 deletions src/ViewModels/Sources/PdfSourceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,17 @@ public override async Task<bool> FetchCover(Codex codex)

try //image.Read can throw exception if file can not be opened/read
{
using MagickImage image = new();
await image.ReadAsync(codex.Sources.Path, ReadSettings);
image.Format = MagickFormat.Png;
image.BackgroundColor = new MagickColor("#000000"); //set background color as transparent
image.Trim(); //cut off all transparency
using (MagickImage image = new())
{
await image.ReadAsync(codex.Sources.Path, ReadSettings);
image.Format = MagickFormat.Png;
image.BackgroundColor = new MagickColor("#000000"); //set background color as transparent
image.Trim(); //cut off all transparency

await image.WriteAsync(codex.CoverArt);
CoverService.CreateThumbnail(codex);
await image.WriteAsync(codex.CoverArt);
CoverService.CreateThumbnail(codex, image);
}
codex.RefreshThumbnail();
return true;
}
catch (Exception ex)
Expand Down

0 comments on commit 1e9bfa4

Please sign in to comment.