Skip to content

Commit

Permalink
Resize 'PreviewImage' for big Pixifiles to fit max size allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiphereal committed Nov 7, 2021
1 parent 8296ee6 commit 76af7a8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
15 changes: 12 additions & 3 deletions PixiEditor/Models/DataHolders/RecentlyOpenedDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,18 @@ private WriteableBitmap LoadPreviewBitmap()
return null;
}

return PixiFileMaxSizeChecker.IsFileUnderMaxSize(serializableDocument) ?
BitmapUtils.GeneratePreviewBitmap(serializableDocument.Layers, serializableDocument.Width, serializableDocument.Height, 80, 50)
: null;
WriteableBitmap writeableBitmap =
BitmapUtils.GeneratePreviewBitmap(serializableDocument.Layers, serializableDocument.Width, serializableDocument.Height, 80, 50);

const int MaxWidthInPixels = 1080;
const int MaxHeightInPixels = 1080;
const int MaxLayerCount = 5;
PixiFileMaxSizeChecker pixiFileMaxSizeChecker =
new PixiFileMaxSizeChecker(maxPixelCountAllowed: MaxWidthInPixels * MaxHeightInPixels * MaxLayerCount);

return pixiFileMaxSizeChecker.IsFileUnderMaxSize(serializableDocument) ?
writeableBitmap
: writeableBitmap.Resize(width: 1080, height: 1080, WriteableBitmapExtensions.Interpolation.Bilinear);
}
else if (FileExtension is ".png" or ".jpg" or ".jpeg")
{
Expand Down
14 changes: 9 additions & 5 deletions PixiEditor/Models/IO/PixiFileMaxSizeChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

namespace PixiEditor.Models.IO
{
internal static class PixiFileMaxSizeChecker
internal class PixiFileMaxSizeChecker
{
// Result of 1080 (Width) * 1080 (Height) * 5 (Layers count).
private const int MaxBitCountAllowed = 5832000;
private readonly int maxPixelCountAllowed;

public static bool IsFileUnderMaxSize(SerializableDocument fileToCheck)
public PixiFileMaxSizeChecker(int maxPixelCountAllowed)
{
return fileToCheck.Height * fileToCheck.Width * fileToCheck.Layers.Count < MaxBitCountAllowed;
this.maxPixelCountAllowed = maxPixelCountAllowed;
}

public bool IsFileUnderMaxSize(SerializableDocument fileToCheck)
{
return fileToCheck.Height * fileToCheck.Width * fileToCheck.Layers.Count < maxPixelCountAllowed;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PixiEditor.Models.DataHolders;
using PixiEditor.Parser;
using System;
using Xunit;

Expand All @@ -8,14 +9,21 @@ namespace PixiEditorTests.ModelsTests.DataHoldersTests
public class RecentlyOpenedDocumentTests
{
[Fact]
public void TestThatForBigPixiFilesPreviewImageIsNotLoaded()
public void TestThatForBigPixiFilesPreviewImageIsResizedToMaxSize()
{
string bigFilePath = $@"{Environment.CurrentDirectory}\..\..\..\ModelsTests\IO\BigPixiFile.pixi";
RecentlyOpenedDocument recentlyOpenedDocument = new RecentlyOpenedDocument(bigFilePath);

var bigFilePreviewImage = recentlyOpenedDocument.PreviewBitmap;
var bigPixiFilePreviewImage = recentlyOpenedDocument.PreviewBitmap;

Assert.Null(bigFilePreviewImage);
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]
Expand Down

0 comments on commit 76af7a8

Please sign in to comment.