-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from Xiphereal/limit-memory-usage-for-recentl…
…y-opened-documents-preview Limit big file loading for recent opened files
- Loading branch information
Showing
12 changed files
with
142 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Windows.Media.Imaging; | ||
|
||
namespace PixiEditor.Models.IO | ||
{ | ||
internal class ImageFileMaxSizeChecker | ||
{ | ||
public int MaxAllowedWidthInPixels { get; init; } = 2048; | ||
public int MaxAllowedHeightInPixels { get; init; } = 2048; | ||
|
||
public ImageFileMaxSizeChecker() | ||
{ | ||
} | ||
|
||
public bool IsFileUnderMaxSize(WriteableBitmap fileToCheck) | ||
{ | ||
return fileToCheck.PixelWidth <= MaxAllowedWidthInPixels | ||
&& fileToCheck.PixelHeight <= MaxAllowedHeightInPixels; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using PixiEditor.Parser; | ||
|
||
namespace PixiEditor.Models.IO | ||
{ | ||
internal class PixiFileMaxSizeChecker | ||
{ | ||
public int MaxAllowedWidthInPixels { get; init; } = 1080; | ||
public int MaxAllowedHeightInPixels { get; init; } = 1080; | ||
public int MaxAllowedLayerCount { get; init; } = 5; | ||
|
||
public PixiFileMaxSizeChecker() | ||
{ | ||
} | ||
|
||
public bool IsFileUnderMaxSize(SerializableDocument fileToCheck) | ||
{ | ||
return fileToCheck.Width <= MaxAllowedWidthInPixels | ||
&& fileToCheck.Height <= MaxAllowedHeightInPixels | ||
&& fileToCheck.Layers.Count <= MaxAllowedLayerCount; | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
PixiEditorTests/ModelsTests/DataHoldersTests/RecentlyOpenedDocumentTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using PixiEditor.Models.DataHolders; | ||
using PixiEditor.Parser; | ||
using System; | ||
using Xunit; | ||
|
||
namespace PixiEditorTests.ModelsTests.DataHoldersTests | ||
{ | ||
[Collection("Application collection")] | ||
public class RecentlyOpenedDocumentTests | ||
{ | ||
[Fact] | ||
public void TestThatForBigPixiFilesPreviewImageIsResizedToMaxSize() | ||
{ | ||
string bigFilePath = $@"{Environment.CurrentDirectory}\..\..\..\ModelsTests\IO\BigPixiFile.pixi"; | ||
RecentlyOpenedDocument recentlyOpenedDocument = new RecentlyOpenedDocument(bigFilePath); | ||
|
||
var bigPixiFilePreviewImage = recentlyOpenedDocument.PreviewBitmap; | ||
|
||
const int MaxWidthInPixels = 1080; | ||
Assert.True(bigPixiFilePreviewImage.PixelWidth <= MaxWidthInPixels); | ||
|
||
const int MaxHeightInPixels = 1080; | ||
Assert.True(bigPixiFilePreviewImage.PixelHeight <= MaxHeightInPixels); | ||
|
||
// This is a workaround for checking the Pixi file layers. | ||
Assert.True(PixiParser.Deserialize(bigFilePath).Layers.Count <= 5); | ||
} | ||
|
||
[Fact] | ||
public void TestThatForSmallEnoughPixiFilesPreviewImageIsLoaded() | ||
{ | ||
string smallEnoughFilePath = $@"{Environment.CurrentDirectory}\..\..\..\ModelsTests\IO\SmallEnoughPixiFile.pixi"; | ||
RecentlyOpenedDocument recentlyOpenedDocument = new RecentlyOpenedDocument(smallEnoughFilePath); | ||
|
||
var smallEnoughFilePreviewImage = recentlyOpenedDocument.PreviewBitmap; | ||
|
||
Assert.NotNull(smallEnoughFilePreviewImage); | ||
} | ||
|
||
[Theory] | ||
[InlineData("png")] | ||
[InlineData("jpg")] | ||
[InlineData("jpeg")] | ||
public void TestThatForBigImageFilesPreviewImageIsResizedToMaxSize(string imageFormat) | ||
{ | ||
string bigImageFilePath = $@"{Environment.CurrentDirectory}\..\..\..\ModelsTests\IO\BigImage.{imageFormat}"; | ||
RecentlyOpenedDocument recentlyOpenedDocument = new RecentlyOpenedDocument(bigImageFilePath); | ||
|
||
var bigImagePreviewImage = recentlyOpenedDocument.PreviewBitmap; | ||
|
||
const int MaxWidthInPixels = 2048; | ||
Assert.True(bigImagePreviewImage.PixelWidth <= MaxWidthInPixels); | ||
|
||
const int MaxHeightInPixels = 2048; | ||
Assert.True(bigImagePreviewImage.PixelHeight <= MaxHeightInPixels); | ||
} | ||
|
||
[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); | ||
} | ||
} | ||
} |
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.
Binary file not shown.
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.
Binary file not shown.