diff --git a/src/MMALSharp.Processing/Processors/Effects/ConvolutionBase.cs b/src/MMALSharp.Processing/Processors/Effects/ConvolutionBase.cs index dc09a933..9f766699 100644 --- a/src/MMALSharp.Processing/Processors/Effects/ConvolutionBase.cs +++ b/src/MMALSharp.Processing/Processors/Effects/ConvolutionBase.cs @@ -59,6 +59,8 @@ public void ApplyConvolution(double[,] kernel, int kernelWidth, int kernelHeight { var localContext = context.Raw ? context : CloneToRawBitmap(context); + bool storeFromRaw = context.StoreFormat != null; + var analyser = new FrameAnalyser { HorizonalCellCount = _horizontalCellCount, @@ -67,9 +69,9 @@ public void ApplyConvolution(double[,] kernel, int kernelWidth, int kernelHeight analyser.Apply(localContext); Parallel.ForEach(analyser.CellRect, (cell) - => ProcessCell(cell, localContext.Data, kernel, kernelWidth, kernelHeight, analyser.Metadata)); + => ProcessCell(cell, localContext.Data, kernel, kernelWidth, kernelHeight, analyser.Metadata, storeFromRaw)); - if (context.StoreFormat != null) + if (storeFromRaw) { FormatRawBitmap(localContext, context); context.Raw = false; // context is never raw after formatting @@ -85,7 +87,7 @@ public void ApplyConvolution(double[,] kernel, int kernelWidth, int kernelHeight } } - private void ProcessCell(Rectangle rect, byte[] image, double[,] kernel, int kernelWidth, int kernelHeight, FrameAnalysisMetadata metadata) + private void ProcessCell(Rectangle rect, byte[] image, double[,] kernel, int kernelWidth, int kernelHeight, FrameAnalysisMetadata metadata, bool storeFromRaw) { // Rectangle and FrameAnalysisMetadata are structures; they are by-value copies and all fields are value-types which makes them thread safe @@ -94,6 +96,21 @@ private void ProcessCell(Rectangle rect, byte[] image, double[,] kernel, int ker int index; + // Indicates RGB needs to be swapped to BGR so that Bitmap.Save works correctly. + if (storeFromRaw) + { + for (var x = rect.X; x < x2; x++) + { + for (var y = rect.Y; y < y2; y++) + { + index = (x * metadata.Bpp) + (y * metadata.Stride); + byte swap = image[index]; + image[index] = image[index + 2]; + image[index + 2] = swap; + } + } + } + for (var x = rect.X; x < x2; x++) { for (var y = rect.Y; y < y2; y++)