Skip to content

Commit

Permalink
fix the bug where you can draw to the left of the canvas, make crash …
Browse files Browse the repository at this point in the history
…when merging misaligned layers explicit
  • Loading branch information
Equbuxu committed Apr 20, 2021
1 parent d24e5f6 commit 3aed62d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
25 changes: 16 additions & 9 deletions PixiEditor/Models/ImageManipulation/BitmapUtils.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using PixiEditor.Models.DataHolders;
using PixiEditor.Models.Layers;
using PixiEditor.Models.Position;
using PixiEditor.Parser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using PixiEditor.Models.DataHolders;
using PixiEditor.Models.Layers;
using PixiEditor.Models.Position;
using PixiEditor.Parser;

namespace PixiEditor.Models.ImageManipulation
{
Expand All @@ -34,9 +34,9 @@ public static WriteableBitmap BytesToWriteableBitmap(int currentBitmapWidth, int
/// <summary>
/// Converts layers bitmaps into one bitmap.
/// </summary>
/// <param name="layers">Layers to combine.</param>
/// <param name="width">Width of final bitmap.</param>
/// <param name="height">Height of final bitmap.</param>.
/// <param name="layers">Layers to combine.</param>
/// <returns>WriteableBitmap of layered bitmaps.</returns>
public static WriteableBitmap CombineLayers(int width, int height, params Layer[] layers)
{
Expand All @@ -49,6 +49,13 @@ public static WriteableBitmap CombineLayers(int width, int height, params Layer[
float layerOpacity = layers[i].Opacity;
Layer layer = layers[i];

if (layer.OffsetX < 0 || layer.OffsetY < 0 ||
layer.Width + layer.OffsetX > layer.MaxWidth ||
layer.Height + layer.OffsetY > layer.MaxHeight)
{
throw new InvalidOperationException("Layers must not extend beyond canvas borders");
}

for (int y = 0; y < layers[i].Height; y++)
{
for (int x = 0; x < layers[i].Width; x++)
Expand Down Expand Up @@ -81,7 +88,7 @@ public static WriteableBitmap CombineLayers(int width, int height, params Layer[
return finalBitmap;
}

/// <summary>
/// <summary>
/// Generates simplified preview from Document, very fast, great for creating small previews. Creates uniform streched image.
/// </summary>
/// <param name="document">Document which be used to generate preview.</param>
Expand Down Expand Up @@ -118,7 +125,7 @@ public static WriteableBitmap GeneratePreviewBitmap(IEnumerable<SerializableLaye

public static Dictionary<Guid, Color[]> GetPixelsForSelection(Layer[] layers, Coordinates[] selection)
{
Dictionary<Guid, Color[]> result = new ();
Dictionary<Guid, Color[]> result = new();

foreach (Layer layer in layers)
{
Expand Down Expand Up @@ -187,4 +194,4 @@ public static WriteableBitmap GeneratePreviewBitmap(IEnumerable<SerializableLaye
return previewBitmap.Resize(newWidth, newHeight, WriteableBitmapExtensions.Interpolation.NearestNeighbor);
}
}
}
}
26 changes: 13 additions & 13 deletions PixiEditor/Models/Layers/Layer.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using PixiEditor.Models.DataHolders;
using PixiEditor.Models.Position;
using PixiEditor.Models.Undo;
using PixiEditor.ViewModels;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using PixiEditor.Models.DataHolders;
using PixiEditor.Models.Position;
using PixiEditor.Models.Undo;
using PixiEditor.ViewModels;

namespace PixiEditor.Models.Layers
{
Expand Down Expand Up @@ -367,14 +367,14 @@ public void DynamicResize(BitmapPixelChanges pixels)

if (!(pixels.WasBuiltAsSingleColored && pixels.ChangedPixels.First().Value.A == 0))
{
if (newMaxX + 1 > Width || newMaxY + 1 > Height)
if ((newMaxX + 1 > Width && Width < MaxWidth) || (newMaxY + 1 > Height && Height < MaxHeight))
{
IncreaseSizeToBottom(newMaxX, newMaxY);
IncreaseSizeToBottomAndRight(newMaxX, newMaxY);
}

if (newMinX < 0 || newMinY < 0)
if ((newMinX < 0 && Width < MaxWidth) || (newMinY < 0 && Height < MaxHeight))
{
IncreaseSizeToTop(newMinX, newMinY);
IncreaseSizeToTopAndLeft(newMinX, newMinY);
}
}

Expand Down Expand Up @@ -493,7 +493,7 @@ private void ClipIfNecessary()
}
}

private void IncreaseSizeToBottom(int newMaxX, int newMaxY)
private void IncreaseSizeToBottomAndRight(int newMaxX, int newMaxY)
{
if (MaxWidth - OffsetX < 0 || MaxHeight - OffsetY < 0)
{
Expand All @@ -506,7 +506,7 @@ private void IncreaseSizeToBottom(int newMaxX, int newMaxY)
ResizeCanvas(0, 0, 0, 0, newMaxX, newMaxY);
}

private void IncreaseSizeToTop(int newMinX, int newMinY)
private void IncreaseSizeToTopAndLeft(int newMinX, int newMinY)
{
newMinX = Math.Clamp(Math.Min(newMinX, Width), Math.Min(-OffsetX, OffsetX), 0);
newMinY = Math.Clamp(Math.Min(newMinY, Height), Math.Min(-OffsetY, OffsetY), 0);
Expand Down Expand Up @@ -538,8 +538,8 @@ private void ResetOffset(BitmapPixelChanges pixels)
{
if (Width == 0 || Height == 0)
{
int offsetX = pixels.ChangedPixels.Min(x => x.Key.X);
int offsetY = pixels.ChangedPixels.Min(x => x.Key.Y);
int offsetX = Math.Max(pixels.ChangedPixels.Min(x => x.Key.X), 0);
int offsetY = Math.Max(pixels.ChangedPixels.Min(x => x.Key.Y), 0);
Offset = new Thickness(offsetX, offsetY, 0, 0);
}
}
Expand Down

0 comments on commit 3aed62d

Please sign in to comment.