打印本文 打印本文 关闭窗口 关闭窗口
Drawing & Animation III
作者:武汉SEO闵涛  文章来源:敏韬网  点击数4671  更新时间:2009/4/23 16:39:28  文章录入:mintao  责任编辑:mintao

ReDim ByteArray(1 To bm.bmWidthBytes, 1 To bm.bmHeight)

For I = 1 To bm.bmWidthBytes Step 3
    For J = 1 To bm.bmHeight
       
        TempColor = OriginalBits(I, J)
        TempColor = TempColor + OriginalBits(I + 1, J)
        TempColor = TempColor + OriginalBits(I + 2, J)
        TempColor = TempColor / 3
       
        ByteArray(I, J) = TempColor
        ByteArray(I + 1, J) = TempColor
        ByteArray(I + 2, J) = TempColor
           

    Next J
Next I

SetBitmapBits hbm, bm.bmWidthBytes * bm.bmHeight, ByteArray(1, 1)

BitBlt Me.hdc, 0, 0, BitmapWidth, BitmapHeight, BitmapImage, 0, 0, vbSrcCopy
Me.Refresh

End Sub

--------------------------------------------------------------------------------

As you can see we loop through the local ByteArray() array and set each byte with the appropriate color. The problem with the one byte vs. three bytes pr pixel we discussed above is overcome by using a loop with the Step 3 option. This makes the outer loop able to set the variable-counter I, to first byte of each pixel in the byte array. We then simply add 1 or 2 to this value and we can access the other colors of the same pixel.

We add each of the individual color values of one pixel together into the TempValue variable. Then we will divide the value in the TempValue variable by 3, and the each color in the pixel is set to this color, which makes the pixel gray.

Blue, Red, Green

Making the bitmap one of either color is actually very simple. If you want to make a pixel blue, you simply set the red and green color values to 0 (black), and you have a pixel dominated by the blue color. Of course if the pixel didn抰 have any blue color in it, then it would turn colorless (black). The same thing goes when a pixel red or green.

The code for this procedure (only Blue shown here) is:


Private Sub cmdBlue_Click()

Dim BitmapWidthBytes As Long
Dim ByteArray() As Byte
Dim I As Long, J As Long

ReDim ByteArray(1 To bm.bmWidthBytes, 1 To bm.bmHeight)

For I = 1 To bm.bmWidthBytes Step 3
    For J = 1 To bm.bmHeight
       
        ByteArray(I, J) = 0
        ByteArray(I + 1, J) = 0
        ByteArray(I + 2, J) = OriginalBits(I + 2, J)
       
    Next J
Next I

SetBitmapBits hbm, bm.bmWidthBytes * bm.bmHeight, ByteArray(1, 1)

BitBlt Me.hdc, 0, 0, BitmapWidth, BitmapHeight, BitmapImage, 0, 0, vbSrcCopy
Me.Refresh

End Sub


Notice again that we use the Step 3 option to set the counter-variable to the start of a pixel.

Brightness

To make a pixel brighter we simply add a value to each of the colors in the pixel. To make it darker we subtract a value from each of the colors in the pixel. It is really as simple as that (isn抰 it disappointing?).

The way we implement it here is to first make a so-called LookUp table. Such a beast is actually no more than an array of calculated values. The lookup table we will generate contains values from 0 to 255, just like a typical byte. The values in the lookup table will increase as the index increases, so any given index will always have a value at least equal to or greater than any lower indexes.

The values in the lookup table are calculated by multiplying the current index with a variable lighting factor. This means that a variable lighting factor of less than 1 will darken the picture instead of lighting it up. The code used in the sample project to build a lookup table is like this:


For I = 0 To 255
    TempValue = I * Val(txtBright.Text)
   
    If TempValue > 255 Then
        BrightTable(I) = 255
    Else
        BrightTable(I) = TempValue
    End If
Next I

The lookup table in this sample code is the BrightTable. The variable lighting factor is entered in the text box next to the Brightness button on the form.

Now that we have this lookup table, let take a peek at the actual code, which manipulates the pixels:

For I = 1 To bm.bmWidthBytes Step 3
    For J = 1 To bm.bmHeight
       
        ByteArray(I, J) = BrightTable(OriginalBits(I, J))
        ByteArray(I + 1, J) = BrightTable(OriginalBits(I + 1, J))
        ByteArray(I + 2, J) = BrightTable(OriginalBits(I + 2, J))
       
    Next J
Next I


We set each of the bytes to the corresponding value in the lookup table, and thus either makes it lighter or darker, depending on the variable lighting factor.

Ripple

The ripple effect is a bit different than any of the other effect we have applied. This is because the rippling does not directly change the color of a specific pixel, but instead set the color of a given pixel equal to the color of another pixel.

As with the Brightness effect, we build a lookup table to store intermediate values in. This lookup table must not be longer than the width of the bitmap, since the index of the table is considered an X-position into the bitmap. So by defining the actual index in the lookup table as a position in the bitmap, the actual value in the index is the distortion pixel, i.e. the pixel that the index X position will be moved to.

So how do we calculate these distortion pixel values? Since we want a ripple (wave like) effect, so why not use the Sin function which define values in waves (I know this is not correct, just accept it in our usage).

So by using a formula with a Sine calculated value we can distort (move) the pixel in the lookup table by adding the index from the table to result of the formula, and thus get a wave like result. The building of the lookup table looks like this:


 

For I = 1 To BitmapWidth
    TempValue = I + Sin(I / 5) * Val(txtRipple.Text)
    If TempValue > BitmapWidth Then
        RippleTable(I) = BitmapWidth
    ElseIf TempValue < 1 Then
        RippleTable(I) = 1
    Else
        RippleTable(I) = TempValue
    End If
Next I

 

We still have to keep things from moving out of bounds, so we make sure that any values over 200 and below 1 are correct to either the least (1) or the greatest (200) value.

So the actual manipulation is then quite simple and looks like this:


For I = 1 To bm.bmWidthBytes Step 3
    For J = 1 To bm.bmHeight
        ByteArray(I, J) = OriginalBits(I, RippleTable(J))
        ByteArray(I + 1, J) = OriginalBits(I + 1, RippleTable(J))
        ByteArray(I + 2, J) = OriginalBits(I + 2, RippleTable(J))
    Next J
Next I


We simply replace the horizontal pixel with the color of the distorted pixel, which is stored in the lookup table, and we have a wave like ripple on the bitmap.

Please note that there are many different algorithms and implementations to achieve each these different effects, the ones shown in this section is just our way of demonstrating pixel manipulation in VB. So do not complain if you discover a nicer or better algorithm, we were just demonstrating pixel manipulation and not algorithm effectiveness and implement.

8-Bit bitmaps

So how do we apply the same effects to 8-bit bitmaps? Well, the actual algorithms are the same, but the implement is not. As stated pr

上一页  [1] [2] [3] [4] [5] [6]  下一页

打印本文 打印本文 关闭窗口 关闭窗口