DEBUG and RELEASE modeWhen applying an adaptive histogram equalisation to a 21,969 x 9802px image, I receive the following:
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at SixLabors.ImageSharp.Processing.Processors.Normalization.AdaptiveHistogramEqualizationProcessor`1.CdfTileData.RowIntervalOperation.Invoke(RowInterval& rows)
at SixLabors.ImageSharp.Advanced.ParallelRowIterator.RowIntervalOperationWrapper`1.Invoke(Int32 i)
at System.Threading.Tasks.Parallel.<>c__DisplayClass17_0`1.<ForWorker>b__1()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
at System.Threading.Tasks.Task.<>c__DisplayClass176_0.<ExecuteSelfReplicating>b__0(Object <p0>)
at System.Threading.Tasks.Task.Execute()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
at System.Threading.ThreadPoolWorkQueue.Dispatch()
This can be easily replicated using the following code, which fails fast:
c#
using var image = new Image<Rgba32>(20000, 10000);
image.Mutate(c => c.HistogramEqualization(new HistogramEqualizationOptions
{
Method = HistogramEqualizationMethod.AdaptiveTileInterpolation,
ClipHistogram = true
}));
I haven't looked at the code at all, but given the speed at which this fails compared with the length of time it takes to equalise a 10,000px square image, I am assuming this crash happens early in the process during memory allocation.
(Apologies for attempting to process gigantic images)
Can you supply the image?
Can you supply the image?
According to the OP I think this is it:
C#
using var image = new Image<Rgba32>(20000, 10000);
I suppose there is an index overflow here:
https://github.com/SixLabors/ImageSharp/blob/78a584e8482b052d7a9885682299e2f37518d83d/src/ImageSharp/Processing/Processors/Normalization/AdaptiveHistogramEqualizationProcessor%7BTPixel%7D.cs#L636
I think we should limit usages of Unsafe.* to the essentially critical (and well covered) LoadResizeSave stuff (JPEG, Resize, maybe PNG), and remove it from all other processors. Span indexer is fast.
I can reproduce the issue. I will change the pixel access to use GetPixelRowSpan there.
Most helpful comment
I can reproduce the issue. I will change the pixel access to use GetPixelRowSpan there.