Private m_NumIgnored As Integer
Private m_IgnoredR() As Byte
Private m_IgnoredG() As Byte
Private m_IgnoredB() As Byte
Private Sub cmdGo_Click()
Const MIN_BRIGHTNESS = 30
Dim bitmap_info As BITMAPINFO
Dim pixels() As Byte
Dim bytes_per_scanLine As Integer
Dim pad_per_scanLine As Integer
Dim X As Integer
Dim Y As Integer
Dim i As Integer
Dim ignore_pixel As Boolean
Dim x_moment As Double
Dim y_moment As Double
Dim brightness As Double
Dim total_brightness As Double
Screen.MousePointer = vbHourglass
cmdGo.Enabled = False
DoEvents
' Prepare the bitmap description.
With bitmap_info.bmiHeader
.biSize = 40
.biWidth = picLaser.ScaleWidth
' Use negative height to scan top-down.
.biHeight = -picLaser.ScaleHeight
.biPlanes = 1
.biBitCount = 32
.biCompression = BI_RGB
bytes_per_scanLine = ((((.biWidth * .biBitCount) + _
31) \ 32) * 4)
pad_per_scanLine = bytes_per_scanLine - (((.biWidth _
* .biBitCount) + 7) \ 8)
.biSizeImage = bytes_per_scanLine * Abs(.biHeight)
End With
' Load the bitmap's data.
ReDim pixels(1 To 4, 1 To picLaser.ScaleWidth, 1 To _
picLaser.ScaleHeight)
GetDIBits picLaser.hdc, picLaser.Image, _
0, picLaser.ScaleHeight, pixels(1, 1, 1), _
bitmap_info, DIB_RGB_COLORS
' Modify the pixels.
For Y = 1 To picLaser.ScaleHeight
For X = 1 To picLaser.ScaleWidth
' See if we should ignore this pixel.
ignore_pixel = False
For i = 1 To m_NumIgnored
If m_IgnoredR(i) = pixels(pixR, X, Y) And _
m_IgnoredG(i) = pixels(pixG, X, Y) And _
m_IgnoredB(i) = pixels(pixB, X, Y) _
Then
ignore_pixel = True
Exit For
End If
Next i
brightness = CDbl(pixels(pixR, X, Y)) + _
pixels(pixG, X, Y) + pixels(pixB, X, Y)
If brightness < MIN_BRIGHTNESS Then _
ignore_pixel = True
' Process the pixel.
If ignore_pixel Then
pixels(pixR, X, Y) = 64
pixels(pixG, X, Y) = 64
pixels(pixB, X, Y) = 64
Else
total_brightness = total_brightness + _
brightness
x_moment = x_moment + X * brightness
y_moment = y_moment + Y * brightness
End If
Next X
Next Y
' Display the result.
SetDIBits picLaser.hdc, picLaser.Image, _
0, picLaser.ScaleHeight, pixels(1, 1, 1), _
bitmap_info, DIB_RGB_COLORS
picLaser.Picture = picLaser.Image
' Display the results.
x_moment = x_moment / total_brightness
y_moment = y_moment / total_brightness
lblX.Caption = Format$(x_moment, "0.00")
lblY.Caption = Format$(y_moment, "0.00")
picLaser.PSet (x_moment, y_moment), vbBlue
picLaser.Circle (x_moment, y_moment), 5, vbBlue
picLaser.Line (x_moment + 5, y_moment + 5)-Step(5, 5), _
vbBlue
picLaser.Line (x_moment - 5, y_moment + 5)-Step(-5, 5), _
vbBlue
picLaser.Line (x_moment - 5, y_moment - 5)-Step(-5, _
-5), vbBlue
picLaser.Line (x_moment + 5, y_moment - 5)-Step(5, -5), _
vbBlue
Screen.MousePointer = vbDefault
End Sub
|