GRAPHIC SET BITS statement

Purpose

Replace a copy of a bitmap that was retrieved as a device-independent bitmap.

Syntax

GRAPHIC SET BITS bitexpr$

Remarks

This statement replaces a bitmap that was originally retrieved with the GRAPHIC GET BITS statement. The bitmap is assigned to the selected graphic target from the string expression specified by bitexpr$. This allows you to make many modifications to the bitmap very quickly, particularly operations which may not be directly supported by PowerBASIC. For example, a typical use might be to change all red pixels in a bitmap to blue.

The bitexpr$ string contains 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 SET 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 GET 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$