BMP Tutor - Ref-11 Extracting Image Color Data

Category: BMP Tutor Series (API)

Date: 02-16-2022

Return to Index


 
 
'Using a simple GET statement, the color data can be read
'directly into to a Byte array. With that color information
'captured, a programmer has several options for working
'with the data. Here are a few popular options:
 
'1. Use As Is
'A programmer can choose to work with the Byte array, which
'provides pixel data in BGR, paddded, upside down format.
 
'2. Use Standard Array
'Remove all padding, flip the scanlines and place the remaining
'color data into an array of RGB or BGR triplet structures. This
'approach allows using coordinates such as (x,y) which correspond
'to both the array element and pixel location on the image.
 
'3. Use PowerBASIC Bitstring
'Remove all padding, flip the scanlines, and place all color
'data into a single string - suitable for use with the GRAPHIC
'SET BITS statement.
 
 
 
   'get pixel data, put into array
   GetDIBits(DC, DIBSection, 0&, Picture1.ScaleHeight, bDIB(1, 1), BI.bmiHeader, 0&)
  '<edit each pixel>
      ...
  'put edited array back into the DIB Section
  SetDIBits(DC, DIBSection, 0&, Picture1.ScaleHeight, bDIB(1, 1), BI.bmiHeader, 0&)
  'copy resulting picture from DC to the control
  BitBlt(Picture1.hdc, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, DC, 0, 0, SRCCOPY)
 
 
 
End Sub
   'create new buffer
   Dim NewBuffer(w*h*3) As Byte
 
   'copy color values to their new positions without touching those padding-bytes
    'fill NewBuffer with contents of old buffer
    Local bufpos, Newpos, x, y as Long
    For y = 0 to h-1                        'each scanline
        For x = 0 to w*3-1 Step 3           'each 3rd byte in a scanline (each 1st byte in each triplet)
            newpos = y * 3 * w + x
bufpos = ( h - y - 1 ) * psw + x
            '// Swap R AND B values
            newBuffer(newpos)     = Buffer(bufpos + 2)
            newBuffer(newpos + 1) = Buffer(bufpos + 1)
            newBuffer(newpos + 2) = Buffer(bufpos)
        Next x
    Next y
 
    'Open output file
    Open bmpfile For Binary as #1
    Put #1, bmfh      'BITMAPFILEHEADER
    Put #1, info      'BITMAPINFOHEADER
    Put #1, Buffer    'image data (size is paddesize)
    Close #1
End Sub
 
'gbs_00519
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm