Advertisement
albusmw

Histo UInt32

Apr 4th, 2020
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''<summary>Class to calculate 2D matrix statistics multi-threaded.</summary>
  2. '''<remarks>Calculation as for UInt16 is not possible as a vector with all entries would be 2^32 entries long.</remarks>
  3. Public Class cStatMultiThread_UInt32
  4.  
  5.     Private Const OneUInt64 As UInt64 = CType(1, UInt64)
  6.  
  7.     '''<summary>The real image data.</summary>
  8.    Public ImageData(,) As UInt32
  9.  
  10.     '''<summary>Object for each thread.</summary>
  11.    Public Class cStateObj
  12.         Friend XOffset As Integer = -1
  13.         Friend YOffset As Integer = -1
  14.         Friend HistDataBayer As New Collections.Generic.Dictionary(Of Int64, UInt64)
  15.         Friend Done As Boolean = False
  16.     End Class
  17.  
  18.     '''<summary>Perform a calculation with 4 threads, one for each bayer channel.</summary>
  19.    Public Sub Calculate(ByRef Results(,) As cStateObj)
  20.  
  21.         'Data are processed
  22.        Dim StObj(3) As cStateObj
  23.         For Idx As Integer = 0 To StObj.GetUpperBound(0)
  24.             StObj(Idx) = New cStateObj
  25.         Next Idx
  26.         StObj(0).XOffset = 0 : StObj(0).YOffset = 0
  27.         StObj(1).XOffset = 0 : StObj(1).YOffset = 1
  28.         StObj(2).XOffset = 1 : StObj(2).YOffset = 0
  29.         StObj(3).XOffset = 1 : StObj(3).YOffset = 1
  30.  
  31.         'Start all threads
  32.        For Each Slice As cStateObj In StObj
  33.             System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf HistoCalc), Slice)
  34.         Next Slice
  35.  
  36.         'Join all threads
  37.        Do
  38.             'System.Threading.Thread.Sleep(1)
  39.            Dim AllDone As Boolean = True
  40.             For Each Slice As cStateObj In StObj
  41.                 If Slice.Done = False Then
  42.                     AllDone = False : Exit For
  43.                 End If
  44.             Next Slice
  45.             If AllDone Then Exit Do
  46.         Loop Until 1 = 0
  47.  
  48.         'Collect all results
  49.        ReDim Results(1, 1)
  50.         Results(0, 0) = StObj(0)
  51.         Results(0, 1) = StObj(1)
  52.         Results(1, 0) = StObj(2)
  53.         Results(1, 1) = StObj(3)
  54.  
  55.     End Sub
  56.  
  57.     '''<summary>Histogramm calculation itself - the histogram of one bayer channel is calculated.</summary>
  58.    Private Sub HistoCalc(ByVal Arguments As Object)
  59.  
  60.         Dim StateObj As cStateObj = CType(Arguments, cStateObj)
  61.         StateObj.Done = False
  62.  
  63.         'Count one bayer part
  64.        StateObj.HistDataBayer = New Collections.Generic.Dictionary(Of Int64, UInt64)
  65.         For IdxX As Integer = StateObj.XOffset To ImageData.GetUpperBound(0) - 1 + StateObj.XOffset Step 2
  66.             For IdxY As Integer = StateObj.YOffset To ImageData.GetUpperBound(1) - 1 + StateObj.YOffset Step 2
  67.                 Dim PixelValue As UInt32 = ImageData(IdxX, IdxY)
  68.                 If StateObj.HistDataBayer.ContainsKey(PixelValue) = False Then
  69.                     StateObj.HistDataBayer.Add(PixelValue, OneUInt64)
  70.                 Else
  71.                     StateObj.HistDataBayer(PixelValue) += OneUInt64
  72.                 End If
  73.             Next IdxY
  74.         Next IdxX
  75.  
  76.         StateObj.Done = True
  77.  
  78.     End Sub
  79.  
  80. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement