Date: 02-16-2022
Return to Index
created by gbSnippets
BMP Tutor - Ref-11 Color Arrays AND Bitstrings
'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.
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_00520
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm