Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get Bitmap bits array (RGB)
#2
Macro Macro1291
Code:
Copy      Help
;this gets RGB colors in format 0xAARRGGBB. Note that in QM is used format 0xAABBGGRR (COLORREF type in Windows programming). Don't remember how in file.
;Not sure but maybe will not work if display color depth is not 32bit. Then use GetDIBits instead of GetBitmapBits.

int hb
if(!CaptureImageOrColor(&hb 0)) ret

__MemBmp mb.Attach(hb)

BITMAP b
GetObject hb sizeof(BITMAP) &b

int n=b.bmHeight*b.bmWidthBytes
str s.all(n)
if(!GetBitmapBits(mb.bm n s)) end ES_FAILED

;outb s n

This func is from Forum / Resources / first topic.
Function OutPixels
Code:
Copy      Help
;/out pixels from mouse
function RECT&r

;copy the rectangle from screen to memory dc
int wid(r.right-r.left) hei(r.bottom-r.top)
int dc=GetDC(0)
__MemBmp mb.Create(wid hei dc r.left r.top)
ReleaseDC 0 dc

;get pixel colors
ARRAY(int) a.create(wid*hei)
BITMAPINFOHEADER h.biSize=sizeof(h)
h.biBitCount=32; h.biWidth=wid; h.biHeight=-hei; h.biPlanes=1
if(GetDIBits(mb.dc mb.bm 0 hei &a[0] +&h DIB_RGB_COLORS)!=hei) ret

;show pixel colors in hex format. Note that R and B are swapped, ie the least significant byte (at the right) is B, not R.
str s
int row i
for row 0 hei
,for i 0 wid
,,s.formata("%06X " a[row*wid+i]&0xffffff)
,s+"[]"
out s

This possibly also is somewhere in forum.
Function GetRectPixels
Code:
Copy      Help
;/
function! RECT&r ARRAY(int)&a [flags] ;;flags: 1 ARGB->BGR

;Gets colors of a rectangle from screen.
;Returns 1. On error returns 0.
;Faster than calling the pixel() function many times.

;r - rectangle coordinates.
;a - receives colors.
;;;The function creates 2-dim array where dimension 1 is for columns, dimension 2 is for rows.
;;;Color format is 0xAARRGGBB. It is different from the format that is used with various QM functions (0xBBGGRR).
;;;If flags contains 1, the function converts all pixels to the 0xBBGGRR format. However it slows down, especially when the rectangle is big.
;;;If you need 0xBBGGRR only for some pixels, use ColorARGBtoBGR.

;EXAMPLE
;int hwnd=child("7" "Button" "Calculator")
;RECT r; ARRAY(int) a
;GetWindowRect hwnd &r
;if(!GetRectPixels(r a 1)) end "failed"
;int row col
;for row 0 a.len(2)
,;out "row %i" row
,;for col 0 a.len(1)
,,;out "0x%X" a[col row]


;Q &q
;copy the rectangle from screen to memory dc
int wid(r.right-r.left) hei(r.bottom-r.top)
int dc=GetDC(0)
__MemBmp mb.Create(wid hei dc r.left r.top)
ReleaseDC 0 dc

;Q &qq
;get pixel colors
a.create(wid hei)
BITMAPINFOHEADER h.biSize=sizeof(h)
h.biBitCount=32; h.biWidth=wid; h.biHeight=-hei; h.biPlanes=1
if(GetDIBits(mb.dc mb.bm 0 hei &a[0 0] +&h DIB_RGB_COLORS)!=hei) ret

;Q &qqq
;convert color format
if flags&1
,int* p=&a[0 0]
,int i n=wid*hei
,for i 0 n
,,int c=p[i]
,,p[i]=(c&0xff00) | (c&0xff<<16) | (c&0xff0000>>16)

;Q &qqqq
;outq
ret 1
err+

Function ColorARGBtoBGR
Code:
Copy      Help
;/
function# color

;Converts color format from 0xARGB to 0xBGR.


ret (color&0xff00) | (color&0xff<<16) | (color&0xff0000>>16)


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)