GRAPHIC GET BITS statement

Purpose | Retrieve a copy of a bitmap , storing it as a device-independent bitmap in a dynamic string variable.
Syntax | GRAPHIC GET BITS TO bitvar$

Remarks

This statement retrieves a copy of the entire bitmap for the selected graphic target, assigning it to the dynamic string variable specified by bitvar$. This allows you to make many modifications to the bitmap very quickly, particularly operations which may not be directly supported by GRAPHIC code. For example, you might change all red pixels in a bitmap to blue. Once your operations are complete, the bitmap is replaced using GRAPHIC SET BITS.

The bitvar$ string will contain a series of four-byte values, each of which represents a long integer. You can convert the four-byte string sections to numeric values with the CVL function, and convert a numeric value to a four-byte string with MKL$. The first four-byte value specifies the width of the bitmap, in pixels, and the second specifies the height. Following that will be one four-byte value for each pixel in the bitmap, which represents the color of that pixel. So, a 20 by 20 bitmap would have 400 pixels and require 1600 bytes (400 * 4), plus 4 bytes for the width and 4 bytes for the height, or a total of 1608 bytes.

The first four-byte pixel value in the string represents the top-left corner of the image, the second represents the second pixel of the first row, and so on. After the last pixel of the first row will be the first pixel of the second row, etc.

If execution speed is most important, it's likely that the string can be manipulated most efficiently with pointer variables.

Some Windows API functions, namely those which reference Device-Independent Bitmaps (DIB), require that colors be specified in the reverse of normal RGB sequence (Blue-Green-Red instead of Red-Green-Blue). To maximize performance, GRAPHIC GET BITS uses BGR format as well. You can use the BGR() function to translate an RGB value to its BGR equivalent.

See also | Built In RGB Color Equates, BGR, CVL, GRAPHIC SET BITS, MKL$, RGB

Example

' Change all red pixels to blue

LOCAL PixelPtr AS LONG PTR

GRAPHIC GET BITS TO bmp$

xsize& = CVL(bmp$,1)

ysize& = CVL(bmp$,5)

PixelPtr = STRPTR(bmp$) + 8

FOR i& = 1 TO xsize& * ysize&

IF @PixelPtr = BGR(%red) THEN @PixelPtr = BGR(%blue)

INCR PixelPtr

NEXT

GRAPHIC SET BITS bmp$