Skip to content

Commit

Permalink
Sync 3.1 DrawImage fixes (#2612)
Browse files Browse the repository at this point in the history
* Handle dedup of local palette of 256 length

* handle when foreground overhangs bottom of background

* prevent potential overflow

* reduce to a single par of clamping operations

* correctly calculate Rect when negative target set

---------

Co-authored-by: James Jackson-South <james_south@hotmail.com>
  • Loading branch information
tocsoft and JimBobSquarePants authored Dec 10, 2023
1 parent 66b6eb9 commit d885702
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,21 @@ protected override void OnFrameApply(ImageFrame<TPixelBg> source)
if (this.BackgroundLocation.X < 0)
{
foregroundRectangle.Width += this.BackgroundLocation.X;
foregroundRectangle.X -= this.BackgroundLocation.X;
left = 0;
}

if (this.BackgroundLocation.Y < 0)
{
foregroundRectangle.Height += this.BackgroundLocation.Y;
foregroundRectangle.Y -= this.BackgroundLocation.Y;
top = 0;
}

// clamp the height/width to the availible space left to prevent overflowing
foregroundRectangle.Width = Math.Min(source.Width - left, foregroundRectangle.Width);
foregroundRectangle.Height = Math.Min(source.Height - top, foregroundRectangle.Height);

int width = foregroundRectangle.Width;
int height = foregroundRectangle.Height;
if (width <= 0 || height <= 0)
Expand Down
42 changes: 42 additions & 0 deletions tests/ImageSharp.Tests/Drawing/DrawImageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,46 @@ public void Issue2447_C<TPixel>(TestImageProvider<TPixel> provider)
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);
}

[Theory]
[WithFile(TestImages.Png.Issue2447, PixelTypes.Rgba32)]
public void Issue2608_NegOffset<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> foreground = provider.GetImage();
using Image<Rgba32> background = new(100, 100, new Rgba32(0, 255, 255));

background.Mutate(c => c.DrawImage(foreground, new Point(-10, -10), new Rectangle(32, 32, 32, 32), 1F));

background.DebugSave(
provider,
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);

background.CompareToReferenceOutput(
provider,
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);
}

[Theory]
[WithFile(TestImages.Png.Issue2447, PixelTypes.Rgba32)]
public void Issue2603<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using Image<TPixel> foreground = provider.GetImage();
using Image<Rgba32> background = new(100, 100, new Rgba32(0, 255, 255));

background.Mutate(c => c.DrawImage(foreground, new Point(80, 80), new Rectangle(32, 32, 32, 32), 1F));

background.DebugSave(
provider,
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);

background.CompareToReferenceOutput(
provider,
appendPixelTypeToFileName: false,
appendSourceFileOrDescription: false);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
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 d885702

Please sign in to comment.