Skip to content

Commit

Permalink
Clamp image files (png, jpeg & jpg) PreviewImages loading for recent …
Browse files Browse the repository at this point in the history
…opened files
  • Loading branch information
Xiphereal committed Oct 10, 2021
1 parent 8dcbec6 commit 5327516
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions PixiEditor/Models/DataHolders/RecentlyOpenedDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private WriteableBitmap LoadPreviewBitmap()
BitmapUtils.GeneratePreviewBitmap(serializableDocument.Layers, serializableDocument.Width, serializableDocument.Height, 80, 50)
: null;
}
else if (FileExtension == ".png" || FileExtension == ".jpg" || FileExtension == ".jpeg")
else if (FileExtension is ".png" or ".jpg" or ".jpeg")
{
WriteableBitmap bitmap = null;

Expand All @@ -102,7 +102,7 @@ private WriteableBitmap LoadPreviewBitmap()
corrupt = true;
}

return bitmap;
return ImageFileMaxSizeChecker.IsFileUnderMaxSize(bitmap) ? bitmap : null;
}

return null;
Expand Down
15 changes: 15 additions & 0 deletions PixiEditor/Models/IO/ImageFileMaxSizeChecker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Windows.Media.Imaging;

namespace PixiEditor.Models.IO
{
internal static class ImageFileMaxSizeChecker
{
// Result of 2048 (Width) * 2048 (Height).
private const int MaxBitCountAllowed = 4194304;

public static bool IsFileUnderMaxSize(WriteableBitmap fileToCheck)
{
return fileToCheck.PixelHeight * fileToCheck.PixelWidth < MaxBitCountAllowed;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,33 @@ public void TestThatForSmallEnoughPixiFilesPreviewImageIsLoaded()

Assert.NotNull(smallEnoughFilePreviewImage);
}

[Theory]
[InlineData("png")]
[InlineData("jpg")]
[InlineData("jpeg")]
public void TestThatForBigImageFilesPreviewImageIsNotLoaded(string imageFormat)
{
string bigImageFilePath = $@"{Environment.CurrentDirectory}\..\..\..\ModelsTests\IO\BigImage.{imageFormat}";
RecentlyOpenedDocument recentlyOpenedDocument = new RecentlyOpenedDocument(bigImageFilePath);

var bigImagePreviewImage = recentlyOpenedDocument.PreviewBitmap;

Assert.Null(bigImagePreviewImage);
}

[Theory]
[InlineData("png")]
[InlineData("jpg")]
[InlineData("jpeg")]
public void TestThatForSmallEnoughImageFilesPreviewImageIsLoaded(string imageFormat)
{
string smallEnoughImageFilePath = $@"{Environment.CurrentDirectory}\..\..\..\ModelsTests\IO\SmallEnoughImage.{imageFormat}";
RecentlyOpenedDocument recentlyOpenedDocument = new RecentlyOpenedDocument(smallEnoughImageFilePath);

var smallEnoughImagePreviewImage = recentlyOpenedDocument.PreviewBitmap;

Assert.NotNull(smallEnoughImagePreviewImage);
}
}
}
Binary file added PixiEditorTests/ModelsTests/IO/BigImage.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PixiEditorTests/ModelsTests/IO/BigImage.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PixiEditorTests/ModelsTests/IO/BigImage.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5327516

Please sign in to comment.