Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get Bitmap bits array (RGB)
#1
Well, I am trying to get a bitmap file in memory, the function SaveBitmap saves a bitmap in hard disk, I need the file in a string without saving it, then I get this function that gets all bitmap format info(still incomplete):

Function bitmapFileFormat
Code:
Copy      Help
function
BITMAPINFOHEADER bmih
bmih.biSize=sizeof(BITMAPINFOHEADER)
;bmih.biBitCount=???param???
bmih.biClrImportant=0
bmih.biCompression=BI_RGB
;bmih.biHeight=???param???
;bmih.biWidth=???param???
bmih.biPlanes=1
;bmih.biSizeImage=width*height*(bitcount/8)

BITMAPFILEHEADER bfh
bfh.bfType=0x4d42
bfh.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)
bfh.bfSize=bfh.bfOffBits+bmih.biSizeImage
ret

The problem is that the above function requires a byte array to save the file, I don't need to save bitmap to hard disk, I only need to get the full file in a string(str), but I don't know hot to get the byte array, I have tryed GetBitmapBits but WinApi reference says that this function should not be used and in fact this function returns zero(maybe I didn't call it correctly).

Function Dlgrant
Code:
Copy      Help
;\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages

;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 223 135 "Dialog"
;1 Button 0x54030001 0x4 120 116 48 14 "OK"
;2 Button 0x54030000 0x4 170 116 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2030109 "" "" ""

ret
;messages
sel message
,case WM_INITDIALOG
,case WM_PAINT
,,PAINTSTRUCT p
,,int dc=BeginPaint(hDlg &p)
,,
,,drawShape 0,0,100,105,dc
,,int bm=CreateCompatibleBitmap(dc 100 105)
,,drawShape 4,70,10,15,dc
,,BITMAP n
,,GetObject bm sizeof(BITMAP) &n
,,;int tsiz=n.bmWidth*n.bmHeight*n.bmBitsPixel
,,;byte* thebytes=calloc(tsiz sizeof(byte))
,,;free(thebytes) <--- here I would like to make some out of RGB byte array  :?:
,,
,,EndPaint hDlg &p
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
ret 1

Thank you

Regards.
#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)
#3
Thank you Gintaras!! Big Grin

Regards.


Forum Jump:


Users browsing this thread: 1 Guest(s)