Public Function LongPixels() As Long()
'_____________________________________________________
'
' Purpose: converts the 2-dimensional 3/4 Byte DIB into a one-
' dimensional Long array, for easy summing of long int values.
' scans from left to right, then bottom to top.
' diblongarray(0) = lower left pixel.
' diblongarray(ubound) = upper right pixel.
' Assumptions: the DIB is 24-bit, the byte order of the pixels is
' r-g-b (not the common b-g-r)
' Affects:
' Inputs:
' Returns:
'_____________________________________________________
Dim bDibMag() As Byte '2 dimensional byte array holds pixels by row,col
Dim lX As Long, lY As Long 'x = pixel col, y = pixel row
Dim saDibMag As SAFEARRAY2D
Dim lXEnd As Long
Dim l As Long
Dim lCol As Long
'structure the 2-dimensional safearray
With saDibMag
.cbElements = 1 'bytes to an element
.cDims = 2 'dimensions
.Bounds(0).lLbound = 0 'lbound of 1st dim
.Bounds(0).cElements = Height '# rows = height in pixels
.Bounds(1).lLbound = 0 'lbound of 2nd dim
.Bounds(1).cElements = BytesPerScanLine() 'calculate width with 24/32 row pad
.pvData = lPtrDibMag 'pointer to the DIBits
End With
MoveMemory ByVal VarPtrArray(bDibMag()), VarPtr(saDibMag), 4 'copy safe array to bDibMag
lXEnd = (Width - 1) * 3
l = 0
ReDim lColor(Width * Height - 1)
For lY = 0 To biDibMag.bmiHeader.biHeight - 1
For lX = 0 To lXEnd Step 3
MoveMemory lCol, bDibMag(lX, lY), 3
lColor(l) = lCol
lCol = 0
MoveMemory ByVal VarPtr(lCol), 0&, 3
l = l + 1
Next lX
Next lY
LongPixels = lColor
lPix = l 'store pixelcount
'zero out the moved memory, so as to avoid
'a pacman-like memory-use situation
MoveMemory ByVal VarPtrArray(bDibMag), 0&, 4
End Function