ParallelizingProcessingCommand Class
Image processing command that executes another image processing command parallely.
Custom command can be executed parallely if it overrides the GetRegionOptimalDecomposition(VintasoftImage,Rectangle,Int32,Int32) method and method returns rectangles of several image regions.
Here is a list of standard image processing commands that can be executed parallely:
MaxThreads property defines on how many threads this command will parallely execute another command.
This C#/VB.NET code shows how to blur image parallely.
Public Shared Sub Test()
' create image
Using image As New Vintasoft.Imaging.VintasoftImage(3000, 3000)
' add noise to an image
Dim addNoize As New Vintasoft.Imaging.ImageProcessing.Filters.AddNoiseCommand()
addNoize.ExecuteInPlace(image)
' blur image - this call prevents the wrong results of next calls
Dim command As Vintasoft.Imaging.ImageProcessing.ProcessingCommandBase = New Vintasoft.Imaging.ImageProcessing.Filters.BlurCommand(11)
Using processedImage As Vintasoft.Imaging.VintasoftImage = command.Execute(image)
End Using
System.Console.WriteLine("Multithread tests of {0} command:", command)
For maxThreads As Integer = 1 To System.Environment.ProcessorCount
' blur image and output an execution time in milliseconds
Dim dt As System.DateTime = System.DateTime.Now
Using processedImage As Vintasoft.Imaging.VintasoftImage = ExecuteProcessingCommand(command, maxThreads, image)
End Using
Dim milliseconds As Integer = CInt((System.DateTime.Now - dt).TotalMilliseconds)
System.Console.WriteLine("{0} thread(s): {1} ms", maxThreads, milliseconds)
Next
End Using
End Sub
''' <summary>
''' Executes a processing command on specific number of threads.
''' </summary>
Private Shared Function ExecuteProcessingCommand(command As Vintasoft.Imaging.ImageProcessing.ProcessingCommandBase, maxThreadsCount As Integer, image As Vintasoft.Imaging.VintasoftImage) As Vintasoft.Imaging.VintasoftImage
If maxThreadsCount = 1 Then
Return command.Execute(image)
End If
Dim parallelizingCommand As New Vintasoft.Imaging.ImageProcessing.ParallelizingProcessingCommand(command)
parallelizingCommand.MaxThreads = maxThreadsCount
Return parallelizingCommand.Execute(image)
End Function
' This code example produces the following output:
Multithread tests of Blur command:
1 thread(s): 6262 ms
2 thread(s): 3455 ms
3 thread(s): 3169 ms
4 thread(s): 3079 ms
public static void Test()
{
// create image
using (Vintasoft.Imaging.VintasoftImage image =
new Vintasoft.Imaging.VintasoftImage(3000, 3000))
{
// add noise to an image
Vintasoft.Imaging.ImageProcessing.Filters.AddNoiseCommand addNoize =
new Vintasoft.Imaging.ImageProcessing.Filters.AddNoiseCommand();
addNoize.ExecuteInPlace(image);
// blur image - this call prevents the wrong results of next calls
Vintasoft.Imaging.ImageProcessing.ProcessingCommandBase command =
new Vintasoft.Imaging.ImageProcessing.Filters.BlurCommand(11);
using (Vintasoft.Imaging.VintasoftImage processedImage =
command.Execute(image))
{
}
System.Console.WriteLine("Multithread tests of {0} command:", command);
for (int maxThreads = 1; maxThreads <= System.Environment.ProcessorCount; maxThreads++)
{
// blur image and output an execution time in milliseconds
System.DateTime dt = System.DateTime.Now;
using (Vintasoft.Imaging.VintasoftImage processedImage =
ExecuteProcessingCommand(command, maxThreads, image))
{
}
int milliseconds = (int)(System.DateTime.Now - dt).TotalMilliseconds;
System.Console.WriteLine("{0} thread(s): {1} ms", maxThreads, milliseconds);
}
}
}
/// <summary>
/// Executes a processing command on specific number of threads.
/// </summary>
private static Vintasoft.Imaging.VintasoftImage ExecuteProcessingCommand(
Vintasoft.Imaging.ImageProcessing.ProcessingCommandBase command,
int maxThreadsCount,
Vintasoft.Imaging.VintasoftImage image)
{
if (maxThreadsCount == 1)
return command.Execute(image);
Vintasoft.Imaging.ImageProcessing.ParallelizingProcessingCommand parallelizingCommand =
new Vintasoft.Imaging.ImageProcessing.ParallelizingProcessingCommand(command);
parallelizingCommand.MaxThreads = maxThreadsCount;
return parallelizingCommand.Execute(image);
}
/* This code example produces the following output:
Multithread tests of Blur command:
1 thread(s): 6262 ms
2 thread(s): 3455 ms
3 thread(s): 3169 ms
4 thread(s): 3079 ms
*/
Target Platforms: .NET9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5