Brightness and Contrast

  • Brightness

dapat dilakukan dengan cara meningkatkan atau menurunkan nilai piksel dari seluruh bagian dalam citra tersebut. Untuk melakukan hal ini digunakan formula seperti terdapat di bawah ini.

Ko= Ki+ C

Dimana Ko adalah nilai piksel output, Ki adalah nilai piksel input dan adalah konstanta yang bernilai positif atau negatif.

Untuk citra berwarna yang mempunyai format warna RGB (red, green, blue), modifikasibrightness dilakukan terhadap setiap elemen warna yang masing-masing nilainya dapat berbeda dengan yang lain. Misalnya citra akan dibuat lebih merah, maka nilai merah pada setiap titik ditambah dengan nilai tertentu. Sebaliknya, jika warna biru citra akan dikurangi, maka nilai elemen biru setiap titik dikurangi.

  • Contrast

Jika nilai-nilai warna piksel dalam sebuah citra terdiri atas nilai-nilai dengan rentang yang tidak terlalu berbeda secara keseluruhan, maka citra tersebut akan kelihatan kurang kontras. Hal ini disebabkan citra tersebut memiliki kurva histogram yang sempit dengan tepi kiri dan tepi kanan yang berdekatan, sehingga titik tergelap dalam citra tersebut tidak mencapai hitam pekat dan titik paling terang dalam citra itu tidak berwarna putih cemerlang [2]. Peningkatan kontras dapat dilakukan dengan bermacam-macam fungsi. Salah satu fungsi yang dapat digunakan adalah seperti terdapat di bawah ini.

Jika Ki > 127 lalu Ko = Ki + P else Ko = Ki – P

Dimana Ko adalah nilai piksel output, Ki adalah nilai piksel input dan adalah konstanta pengaturan kontras.

Ok. mari kita coba untuk membuat aplikasi Brightness dan Contrast gambar dengan menggunakan Visual Basic 6.0.
untuk membuat aplikasinya, ikutilah langkah langkah dibawah ini !
  1. buatlah form baru
  2. pada form tambahkan 2 buah Command Botton, 2 buah Picture Box, dan sebuah HScrollBar.
  3. tambahkan sebuah modul.
  4. copy code di bawah ini ke dalam modul.
Type SHELLEXECUTEINFO
    cbSize As Long
    fMask As Long
    hwnd As Long
    lpVerb As String
    lpFile As String
    lpParameters As String
    lpDirectory As String
    nShow As Long
    hInstApp As Long
    lpIDList As Long
    lpClass As String
    hkeyClass As Long
    dwHotKey As Long
    hIcon As Long
    hProcess As Long
End Type

Public Const SEE_MASK_INVOKEIDLIST = &HC
Public Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Const SEE_MASK_FLAG_NO_UI = &H400

Declare Function ShellExecuteEX Lib "shell32.dll" Alias _
"ShellExecuteEx" (SEI As SHELLEXECUTEINFO) As Long

Public Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Public Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long

Public Type Pixel
    blue As Integer
    green As Integer
    red As Integer
    Trans As Integer
End Type

Public Function NewPixel(inRed As Integer, inGreen As Integer, inBlue As Integer) As Pixel
    With NewPixel
        .red = inRed
        .green = inGreen
        .blue = inBlue
    End With
End Function

Public Function PixToLong(inPix As Pixel) As Long
    With inPix
        PixToLong = RGB(.red, .green, .blue)
    End With
End Function

Public Function LongToPix(inCol As Long) As Pixel
    With LongToPix
        .red = inCol And &HFF
        .green = (inCol \ &H100) And &HFF
        .blue = (inCol \ &H10000) And &HFF
    End With
End Function

Public Function GreyscalePix(inVal As Integer) As Pixel

    GreyscalePix = NewPixel(inVal, inVal, inVal)
End Function

Public Function InvertPix(inPix As Pixel) As Pixel
    With InvertPix
        .red = Not inPix.red
        .green = Not inPix.green
        .blue = Not inPix.blue
    End With
End Function

Public Function SameCol(inPixA As Pixel, inPixB As Pixel) As Boolean

    SameCol = (inPixA.red = inPixB.red) And (inPixA.green = inPixB.green) And (inPixA.blue = inPixB.blue)
End Function

Public Function GetGreyPix(inPix As Pixel) As Integer

    GetGreyPix = ((inPix.red * 0.222) + (inPix.green * 0.707) + (inPix.blue * 0.071))
End Function

Public Function LightenPix(inCol As Pixel, inAmt As Integer) As Pixel

    LightenPix = NewPixel(CheckHighByte(inCol.red + inAmt), _
        CheckHighByte(inCol.green + inAmt), CheckHighByte(inCol.blue + inAmt))
End Function

Public Function DarkenPix(inCol As Pixel, inAmt As Integer) As Pixel

    DarkenPix = NewPixel(CheckLowByte(inCol.red - inAmt), _
        CheckLowByte(inCol.green - inAmt), CheckLowByte(inCol.blue - inAmt))
End Function

Public Function TransPix(PixA As Pixel, PixB As Pixel, inAmt As Single) As Pixel
    With TransPix
        .red = LinearB(PixA.red, PixB.red, inAmt)
        .green = LinearB(PixA.green, PixB.green, inAmt)
        .blue = LinearB(PixA.blue, PixB.blue, inAmt)
    End With
End Function

Public Function TransAddPix(inPixA As Pixel, inPixB As Pixel, inAmt As Single) As Pixel
    TransAddPix = TransPix(BlendAdd(inPixA, inPixB), inPixB, inAmt)
End Function

Public Function TransSubPix(inPixA As Pixel, inPixB As Pixel, inAmt As Single) As Pixel
    TransSubPix = TransPix(BlendSub(inPixA, inPixB), inPixB, inAmt)
End Function

Public Function TransLightPix(inPixA As Pixel, inPixB As Pixel, inAmt As Single) As Pixel
    TransLightPix = TransPix(BlendLight(inPixA, inPixB), inPixB, inAmt)
End Function

Public Function TransDarkPix(inPixA As Pixel, inPixB As Pixel, inAmt As Single) As Pixel
    TransDarkPix = TransPix(BlendDark(inPixA, inPixB), inPixB, inAmt)
End Function

Public Function TransDiffPix(inPixA As Pixel, inPixB As Pixel, inAmt As Single) As Pixel
    TransDiffPix = TransPix(BlendDiff(inPixA, inPixB), inPixB, inAmt)
End Function

Public Function TransScrnPix(inPixA As Pixel, inPixB As Pixel, inAmt As Single) As Pixel
    TransScrnPix = TransPix(BlendScrn(inPixA, inPixB), inPixB, inAmt)
End Function

Public Function TransExclPix(inPixA As Pixel, inPixB As Pixel, inAmt As Single) As Pixel
    TransExclPix = TransPix(BlendExcl(inPixA, inPixB), inPixB, inAmt)
End Function

Public Function BlendAdd(inPixA As Pixel, inPixB As Pixel) As Pixel
    With BlendAdd
        .red = CheckHighByte(inPixA.red + inPixB.red)
        .green = CheckHighByte(inPixA.green + inPixB.green)
        .blue = CheckHighByte(inPixA.blue + inPixB.blue)
    End With
End Function

Public Function BlendSub(inPixA As Pixel, inPixB As Pixel) As Pixel
    With BlendSub
        .red = CheckLowByte(inPixA.red - inPixB.red)
        .green = CheckLowByte(inPixA.green - inPixB.green)
        .blue = CheckLowByte(inPixA.blue - inPixB.blue)
    End With
End Function

Public Function BlendLight(inPixA As Pixel, inPixB As Pixel) As Pixel
    With BlendLight
        .red = MaxB(inPixA.red, inPixB.red)
        .green = MaxB(inPixA.green, inPixB.green)
        .blue = MaxB(inPixA.blue, inPixB.blue)
    End With
End Function

Public Function BlendDark(inPixA As Pixel, inPixB As Pixel) As Pixel
    With BlendDark
        .red = MinB(inPixA.red, inPixB.red)
        .green = MinB(inPixA.green, inPixB.green)
        .blue = MinB(inPixA.blue, inPixB.blue)
    End With
End Function

Public Function BlendDiff(inPixA As Pixel, inPixB As Pixel) As Pixel
    With BlendDiff
        .red = Abs(inPixA.red - inPixB.red)
        .green = Abs(inPixA.green - inPixB.green)
        .blue = Abs(inPixA.blue - inPixB.blue)
    End With
End Function

Public Function BlendScrn(inPixA As Pixel, inPixB As Pixel) As Pixel
    With BlendScrn
        .red = CheckHighByte(inPixA.red * (1 + (inPixB.red / 255)))
        .green = CheckHighByte(inPixA.green * (1 + (inPixB.green / 255)))
        .blue = CheckHighByte(inPixA.blue * (1 + (inPixB.blue / 255)))
    End With
End Function

Public Function BlendExcl(inPixA As Pixel, inPixB As Pixel) As Pixel
    With BlendExcl
        .red = LinearB(inPixA.red, Not inPixA.red, inPixB.red / 255)
        .green = LinearB(inPixA.green, Not inPixA.green, inPixB.green / 255)
        .blue = LinearB(inPixA.blue, Not inPixA.blue, inPixB.blue / 255)
    End With
End Function

Function LinearB(inValA As Integer, inValB As Integer, inPos As Single) As Integer

    LinearB = (inValA * (1 - inPos)) + (inValB * inPos)
End Function

Function CheckHighByte(inVal As Integer) As Integer

    CheckHighByte = IIf(inVal > 255, 255, inVal)
End Function

Function CheckLowByte(inVal As Integer) As Integer

    CheckLowByte = IIf(inVal < 0, 0, inVal)
End Function

Function CheckByte(inVal As Integer) As Integer

    If inVal < 0 Then CheckByte = 0 Else If inVal > 255 Then CheckByte = 255 Else CheckByte = inVal
End Function

Public Function MaxB(inValA As Integer, inValB As Integer) As Integer

    MaxB = IIf(inValA > inValB, inValA, inValB)
End Function

Public Function MinB(inValA As Integer, inValB As Integer) As Integer

    MinB = IIf(inValA < inValB, inValA, inValB)
End Function

Public Sub ShowProps(FileName As String, OwnerhWnd As Long)
    Dim SEI As SHELLEXECUTEINFO
    Dim r As Long
   With SEI
        .cbSize = Len(SEI)
        .fMask = SEE_MASK_NOCLOSEPROCESS Or _
         SEE_MASK_INVOKEIDLIST Or SEE_MASK_FLAG_NO_UI
        .hwnd = OwnerhWnd
        .lpVerb = "Properties"
        .lpFile = FileName
        .lpParameters = vbNullChar
        .lpDirectory = vbNullChar
        .nShow = 0
        .hInstApp = 0
        .lpIDList = 0
    End With

    r = ShellExecuteEX(SEI)
End Sub

Public Function Contrast(pix As Pixel, iAmnt As Integer) As Pixel

    If pix.red < 128 Then
        pix.red = CheckLowByte(pix.red + iAmnt)
    Else
        pix.red = CheckHighByte(pix.red - iAmnt)
    End If

    If pix.blue < 128 Then
        pix.blue = CheckLowByte(pix.blue + iAmnt)
    Else
        pix.blue = CheckHighByte(pix.blue - iAmnt)
    End If

    If pix.green < 128 Then
        pix.green = CheckLowByte(pix.green + iAmnt)
    Else
        pix.green = CheckHighByte(pix.green - iAmnt)
    End If

    Contrast = pix

End Function

Public Sub Bright(val As Integer, oPic As PictureBox)
On Error Resume Next

    Dim Brightness As Single
    Dim NewColor As Long
    Dim x, y As Integer
    Dim r, g, b As Integer

    Brightness = val '/ 100

    For x = 0 To oPic.ScaleWidth
        For y = 0 To oPic.ScaleHeight

            NewColor = GetPixel(oPic.hdc, x, y)

            r = (NewColor Mod 256)
            b = (Int(NewColor / 65536))
            g = ((NewColor - (b * 65536) - r) / 256)

            r = r + Brightness
            b = b + Brightness
            g = g + Brightness

            If r > 255 Then r = 255
            If r < 0 Then r = 0
            If b > 255 Then b = 255
            If b < 0 Then b = 0
            If g > 255 Then g = 255
            If g < 0 Then g = 0

            SetPixelV oPic.hdc, x, y, RGB(r, g, b)

        Next y

    If x Mod 10 = 0 Then oPic.Refresh
    Next x

    oPic.Refresh
End Sub

   5. copy code di bawah ini pada Command1_click
Private Sub Command1_Click()
    Dim x As Long
    Dim y As Long
    Dim r As Integer
    Dim g As Integer
    Dim b As Integer
    Dim pix As Pixel

    Picture1.Cls

    Screen.MousePointer = vbHourglass

    For x = 0 To Picture2.ScaleWidth
        For y = 0 To Picture2.ScaleHeight
            pix = LongToPix(Picture2.Point(x, y))
            pix = Contrast(pix, -(HScroll1.Value) / 5)
            Picture1.PSet (x, y), PixToLong(pix)
        Next y
    Next x

    Screen.MousePointer = vbDefault

    Picture2.Picture = Picture2.Image

End Sub

    6. copy code di bawah ini pada Command2_click
 
Private Sub Command2_Click()
    Picture1.Picture = Picture2.Picture
    Bright HScroll1.Value, Picture1
End Sub