vbAccelerator - Contents of code file: cGammaCorrect.cls

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "cGammaCorrect"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
    lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)

Private Type SAFEARRAYBOUND
    cElements As Long
    lLbound As Long
End Type
Private Type SAFEARRAY2D
    cDims As Integer
    fFeatures As Integer
    cbElements As Long
    cLocks As Long
    pvData As Long
    Bounds(0 To 1) As SAFEARRAYBOUND
End Type
Private Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" (Ptr()
 As Any) As Long


Private m_fGamma As Double
Private m_red(0 To 255) As Byte
Private m_green(0 To 255) As Byte
Private m_blue(0 To 255) As Byte

Public Property Get Gamma() As Double
   Gamma = m_fGamma
End Property
Public Property Let Gamma(ByVal fGamma As Double)
   m_fGamma = fGamma
   createGammaTable
End Property

Public Sub Correct( _
      cSrc As cDIBSection, _
      cDst As cDIBSection _
   )
      Dim bDib() As Byte
      Dim bDibDst() As Byte
      Dim tSA As SAFEARRAY2D
      Dim tSADst As SAFEARRAY2D
    
      ' Get the bits in the from DIB section:
      With tSA
          .cbElements = 1
          .cDims = 2
          .Bounds(0).lLbound = 0
          .Bounds(0).cElements = cSrc.Height
          .Bounds(1).lLbound = 0
          .Bounds(1).cElements = cSrc.BytesPerScanLine
          .pvData = cSrc.DIBSectionBitsPtr
      End With
      CopyMemory ByVal VarPtrArray(bDib()), VarPtr(tSA), 4
   
      ' Get the bits in the from DIB section:
      With tSADst
          .cbElements = 1
          .cDims = 2
          .Bounds(0).lLbound = 0
          .Bounds(0).cElements = cDst.Height
          .Bounds(1).lLbound = 0
          .Bounds(1).cElements = cDst.BytesPerScanLine()
          .pvData = cDst.DIBSectionBitsPtr
      End With
      CopyMemory ByVal VarPtrArray(bDibDst()), VarPtr(tSADst), 4
      
      Dim x As Long
      Dim y As Long
      Dim xEnd As Long
      Dim yEnd As Long
      
      xEnd = cSrc.BytesPerScanLine() - 3
      yEnd = cSrc.Height - 1
      
      For x = 0 To xEnd Step 3
         For y = 0 To yEnd
            bDibDst(x + 2, y) = m_red(bDib(x + 2, y))
            bDibDst(x + 1, y) = m_green(bDib(x + 1, y))
            bDibDst(x, y) = m_blue(bDib(x, y))
         Next y
      Next x
      
      CopyMemory ByVal VarPtrArray(bDibDst), 0&, 4
      CopyMemory ByVal VarPtrArray(bDib), 0&, 4
End Sub

Private Sub createGammaTable()
Dim i As Long
Dim lValue As Long
   For i = 0 To 255
      lValue = (255# * ((i / 255#) ^ (1# / m_fGamma))) + 0.5
      If (lValue > 255) Then lValue = 255
      m_red(i) = lValue
      m_green(i) = lValue
      m_blue(i) = lValue
   Next i
End Sub