PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
FastPix: Rapid Pixel Processing for Dummies and Dudes-VBForums
Results 1 to 40 of 54

Thread: FastPix: Rapid Pixel Processing for Dummies and Dudes

Threaded View

  1. #1

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,102

    FastPix: easy Rapid Pixel Processing (LockBits wrapper)

    EDIT October 2013: FastPix code modified to handle the Byte Array correctly. See post #38 below.

    People are forever complaining about the slowness of GetPixel and SetPixel. They probably weren't designed for processing whole images (or if they were, they were designed very badly). DotNet has an answer to that, but it's not an easy one to get your head around: the Bitmap class methods LockBits and UnlockBits.

    The main idea of FastPix is to provide a substitute for GetPixel and SetPixel which seem to work about 10 to 15 times as fast as the original ones. But it has more to offer.

    There are plenty of examples on the web of classes that encapsulate LockBits/UnlockBits. The ones I have seen are often hampered because they try to deal with different bitmap formats. But as far as I can see nearly all the bitmaps we deal with are either 24 bits (digital photos, some drawing programs) or 32 bits (drawing programs with transparency). GDI+ converts 24 bit images internally to 32 bits anyway, so I have designed FastPix to work with 32 bits only. It makes the code simpler and opens up the possibility of processing bitmaps as Integer arrays, which are blindingly fast. FastPix includes a ConvertFormat method for converting bitmap pixel formats or you can use:
    vb.net Code:
    1. Dim img = Image.FromFile(filename)
    2. Dim bmp As New Bitmap(img)
    The bitmap New sub converts a loaded image (but not an image file) to 32 bits.

    To start using Fastpix, download the attached zip file and unzip it to a convenient folder. In Visual Studio, select Project/Add Existing Item... and import the unzipped .vb file into your project.

    Here is an example of how you use the FastPix GetPixel and SetPixel substitutes. Their format is the same as the ones in the Bitmap class:
    vb.net Code:
    1. Using fp as New FastPix(myBitmap)
    2.        Dim myColor As Color =  fp.GetPixel(x, y)
    3.        fp.SetPixel(x, y, Color.Orange)
    4.     End Using
    Always declare a FastPix object with a Using loop, or otherwise Dispose it as soon as you have finished with it. The UnLockBits is in the Dispose method, so you will not be able to see your resulting bitmap until Dispose has been called either implicitly (with End Using) or directly.

    FastPix also offers you the bitmap in the form of an Integer Array and its performance leaves even the Fastpix GetPixel/SetPixel in the dust. Here's an example of how you use it:
    vb.net Code:
    1. Using fp as New FastPix(myBitmap)
    2.            'Make a local reference to the array; it is roughly 4x as fast as direct references to fp.PixelArray:
    3.            Dim pixels as Integer() = fp.PixelArray
    4.  
    5.               For i as integer = 0 to pixels.Length - 1
    6.  
    7.                    'example: substitute a color
    8.                    if pixels(i) = Color.Red.ToArgb then pixels(i) = Color.Blue.ToArgb
    9.  
    10.                    'example: invert the color
    11.                    pixels(i) = pixels(i) XOR $HFFFFFF
    12.  
    13.                Next
    14.             End Using
    Note how the inversion is done using only a logical instruction and a bit mask. If you can restrict yourself to techniques like that and to integer arithmetic, and avoid all references to objects outside the Class, you can get fantastic performance. For example, inverting the colors of a 10 MP digital photo can be done over 100x as fast as with old-fashioned GetPixel and SetPixel (150x if you don't count the LockBits/UnlockBits overhead).

    Of course, many kinds of pixel processing require you to get at the A, R, G and B bytes of the pixel. You can extract those from an integer using the BitConverter.GetBytes method, but that would slow things down terribly. Instead, it is possible to extract the bytes with masks and logic/shift operations only. They are probably just as fast as integer arithmetic. Here is an example which fades a bitmap by leveling all the Alpha byte values to a maximum level:
    vb.net Code:
    1. Dim _alpha As Byte 'Level Alpha down to this value
    2.  
    3. Using fp as New FastPix(myBitmap)
    4.    Dim pixels as Integer() = fp.PixelArray
    5.    Dim px As Integer = pixels(j)
    6.    Dim pxAlpha As Integer = (px >> 24) And &HFF 'shift alpha byte to bottom end, clear rest
    7.    If pxAlpha > _alpha Then
    8.       pxAlpha = CInt(_alpha) << 24
    9.       pixels(j) = (px And &HFFFFFF) Or pxAlpha 'replace original alpha value
    10.    End If
    11. End Using

    If you don't like the look of that, FastPix also provides a Byte array to make byte operations a bit less intimidating. Possibly it's not as fast as the integer array version. The bytes are in the order B, G, R, A for each pixel. Here's another example of fading a bitmap. Note the extra parameter in the FastPix declaration:
    vb.net Code:
    1. Using fp As New FastPix(myBitmap, True)
    2.    Dim bytes As byte() = fp.ColorByteArray
    3.    'Modify the Alpha bytes to make the bitmap 50% transparent:
    4.     For i As Integer = 3 to bytes.Length - 1 Step 4    
    5.        bytes(i) = 127
    6.     Next
    7. End Using

    I hope you find it useful. Comments and criticism welcome. BB
    Attached Files Attached Files
    Last edited by boops boops; Dec 9th, 2014 at 09:18 AM. Reason: a few more typos spotted!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width