Imagesharp: Negative height rectangles cause ArgumentOutOfRangeException for property MaxDegreeOfParallelism in IterateRows

Created on 5 Apr 2019  路  2Comments  路  Source: SixLabors/ImageSharp

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] I have verified that I am running the latest version of ImageSharp
  • [x] I have verified if the problem exist in both DEBUG and RELEASE mode
  • [x] I have searched open and closed issues to ensure it has not already been reported

Description

I made a mistake when cloning and cropping an image.

The problem is that the exception thrown was quite obscure. The error was related to MaxDegreeOfParallelism being invalid. Here is the stack trace:

SixLabors.ImageSharp.ImageProcessingException: An error occurred when processing the image using DrawImageProcessor`2. See the inner exception for more detail. ---> SixLabors.ImageSharp.ImageProcessingException: An error occurred when processing the image using DrawImageProcessor`2. See the inner exception for more detail. ---> System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: MaxDegreeOfParallelism
   at System.Threading.Tasks.ParallelOptions.set_MaxDegreeOfParallelism(Int32 value)
   at SixLabors.ImageSharp.ParallelUtils.ParallelHelper.IterateRows(Rectangle rectangle, ParallelExecutionSettings& parallelSettings, Action`1 body)
   at SixLabors.ImageSharp.Processing.Processors.Drawing.DrawImageProcessor`2.OnFrameApply(ImageFrame`1 source, Rectangle sourceRectangle, Configuration configuration)
   at SixLabors.ImageSharp.Processing.Processors.ImageProcessor`1.Apply(ImageFrame`1 source, Rectangle sourceRectangle, Configuration configuration)
   --- End of inner exception stack trace ---
   at SixLabors.ImageSharp.Processing.Processors.ImageProcessor`1.Apply(ImageFrame`1 source, Rectangle sourceRectangle, Configuration configuration)
   at SixLabors.ImageSharp.Processing.Processors.ImageProcessor`1.Apply(Image`1 source, Rectangle sourceRectangle)
   --- End of inner exception stack trace ---
   at SixLabors.ImageSharp.Processing.Processors.ImageProcessor`1.Apply(Image`1 source, Rectangle sourceRectangle)
   at SixLabors.ImageSharp.Processing.DefaultInternalImageProcessorContext`1.ApplyProcessor(IImageProcessor`1 processor, Rectangle rectangle)
   at SixLabors.ImageSharp.Processing.DefaultInternalImageProcessorContext`1.ApplyProcessor(IImageProcessor`1 processor)
   at SixLabors.ImageSharp.Processing.DrawImageExtensions.DrawImage[TPixelDst,TPixelSrc](IImageProcessingContext`1 source, Image`1 image, Point location, GraphicsOptions options)

I've tracked down the root cause to:
https://github.com/SixLabors/ImageSharp/blob/2849fd9c1dc19a7338235f6c33f4970e5a12dee3/src/ImageSharp/Common/ParallelUtils/ParallelHelper.cs#L50-L64

If the rectangle height is negative, then maxSteps will be negative, which will then set the MaxDegreeOfParallelism to a negative value.

Steps to Reproduce

It took me ages to work out what happened! I got my top and left variables back to front.

My crop was:

image.Mutate(x => x.DrawImage(source, new Point(top, left), GraphicsOptions.Default));

And should have been:

image.Mutate(x => x.DrawImage(source, new Point(left, top), GraphicsOptions.Default));

System Configuration

  • ImageSharp version: (local build) 2849fd9c1dc19a7338235f6c33f4970e5a12dee3
  • Other ImageSharp packages and versions: (local build) 2849fd9c1dc19a7338235f6c33f4970e5a12dee3
  • Environment (Operating system, version and so on): Win 10, 1809
  • .NET Framework version: ".NETFramework,Version=v4.6.2"
  • Additional information:

I think the ideal solution here is a couple more guards to provide a better error message when someone (like me) does something silly and messes up their coordinates.

good first issue

Most helpful comment

Yeah, I'll give it a go.

All 2 comments

Well discovered!

This is the kind of simple fix that would be perfect for a first time PR if you're up for that.

Yeah, I'll give it a go.

Was this page helpful?
0 / 5 - 0 ratings