From 1a25c1b1ee588b54422708eb004d50bcc3852012 Mon Sep 17 00:00:00 2001 From: maforget <11904426+maforget@users.noreply.github.com> Date: Wed, 6 Mar 2024 19:55:58 -0500 Subject: [PATCH] random crash due to thumbnail will no longer occur. It could have the effect of a thumbnail having a big red X, which would require a refresh. Added back parallel For --- cYo.Common/Drawing/ImageProcessing.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/cYo.Common/Drawing/ImageProcessing.cs b/cYo.Common/Drawing/ImageProcessing.cs index b3a9c03..5a84db8 100644 --- a/cYo.Common/Drawing/ImageProcessing.cs +++ b/cYo.Common/Drawing/ImageProcessing.cs @@ -895,7 +895,8 @@ public static void EdgeDetectQuick(this Bitmap bitmap) bitmap.EdgeDetectQuick(Rectangle.Empty); } - public unsafe static Bitmap ResizeFast(Bitmap source, int newWidth, int newHeight, PixelFormat format, ResizeFastInterpolation method) + [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions] + public unsafe static Bitmap ResizeFast(Bitmap source, int newWidth, int newHeight, PixelFormat format, ResizeFastInterpolation method) { Bitmap bitmap = source; @@ -956,7 +957,7 @@ public unsafe static Bitmap ResizeFast(Bitmap source, int newWidth, int newHeigh { case ResizeFastInterpolation.NearestNeighbor: // for each line - for (int y = 0; y < newHeight; y++) + Parallel.For(0, newHeight, (int y) => { //Ref: https://github.com/andrewkirillov/AForge.NET/blob/master/Sources/Imaging/Filters/Transform/ResizeNearestNeighbor.cs#L81 byte* dst = orgdst + dstStride * y; @@ -973,7 +974,7 @@ public unsafe static Bitmap ResizeFast(Bitmap source, int newWidth, int newHeigh } dst += dstExtra; } - }; + }); break; case ResizeFastInterpolation.Bilinear: { @@ -983,7 +984,7 @@ public unsafe static Bitmap ResizeFast(Bitmap source, int newWidth, int newHeigh int xmax = width - 1; // for each line - for (int y = 0; y < newHeight; y++) + Parallel.For(0, newHeight, (int y) => { byte* dst = orgdst + y * dstStride; @@ -1014,6 +1015,7 @@ public unsafe static Bitmap ResizeFast(Bitmap source, int newWidth, int newHeigh byte* p3 = tp2 + ox1 * srcPixelSize; byte* p4 = tp2 + ox2 * srcPixelSize; + // interpolate using 4 points for (int i = 0; i < minPixelSize; i++, dst++, p1++, p2++, p3++, p4++) { *dst = (byte)( @@ -1022,7 +1024,7 @@ public unsafe static Bitmap ResizeFast(Bitmap source, int newWidth, int newHeigh } dst += dstExtra; } - }; + }); break; } case ResizeFastInterpolation.Bicubic: @@ -1032,7 +1034,7 @@ public unsafe static Bitmap ResizeFast(Bitmap source, int newWidth, int newHeigh int ymax = height - 1; int xmax = width - 1; - for (int y = 0; y < newHeight; y++) + Parallel.For(0, newHeight, (int y) => { byte* dst = orgdst + y * dstStride; @@ -1092,13 +1094,13 @@ public unsafe static Bitmap ResizeFast(Bitmap source, int newWidth, int newHeigh } dst += dstPixelSize; } - }; + }); break; } } return dstImage; } - catch + catch (AccessViolationException) { return null; }