Author |
Topic: Reading and Writing Screen Pixels (Read 937 times) |
|
Kipper
New Member
member is offline


Posts: 8
|
 |
Re: Reading and Writing Screen Pixels
« Reply #6 on: Feb 11th, 2015, 9:32pm » |
|
Hi Richard
Your code worked straight away as shown below (and was really fast plotting!) but I have (hopefully) one last question posed at end of the PROC shown below.
90 DEF PROCcapturewindow2(file$) 100 LOCAL rc{}, hdc%, hbm%, ddc%, oldbm% LOCAL bmp%, width%, height%, size%, data%, res% 110 DIM rc{l%,t%,r%,b%} 120 rc.l%=60 130 rc.t%=20 140 rc.b%=470 150 rc.r%=600 160 SYS "CreateDC", "DISPLAY", 0, 0, 0 TO ddc% 170 SYS "CreateCompatibleDC", @memhdc% TO hdc% 180 SYS "CreateCompatibleBitmap", @memhdc%, rc.r%-rc.l%, rc.b%-rc.t% TO hbm% 190 200 SYS "SelectObject", hdc%, hbm% TO oldbm% 310 SYS "BitBlt", @memhdc%, 0, 0, rc.r%-rc.l%, rc.b%-rc.t%, ddc%, rc.l%, rc.t%, &CC0020 320 SYS "SelectObject", hdc%, oldbm% 350 SYS "InvalidateRect", @hwnd%, 0, 0 360 SYS "DeleteDC", ddc% 380 ENDPROC
I would like to read the pixel values from the image held in memory. I know I can get some data image size data using lines below but how can I index the values for (I assume) RGB and perhaps Alpha? Could you provide a line of code or a short loop that points me to the right place or a link? Many thanks in advance.
430 REM. Find the bitmap dimensions and file size: 440 DIM bmp% LOCAL 26 450 bmp% = (bmp% + 3) AND -4 460 SYS "GetObject", hbm%, 24, bmp% TO res% 470 IF res%=0 ERROR 100, "GetObject failed" 480 490 width% = bmp%!4 500 height% = bmp%!8 510 size% = 54 + height%*((width%*3 + 3) AND -4)
|
|
Logged
|
|
|
|
rtr2
Guest
|
 |
Re: Reading and Writing Screen Pixels
« Reply #7 on: Feb 11th, 2015, 10:13pm » |
|
on Feb 11th, 2015, 9:32pm, Kipper wrote:| I would like to read the pixel values from the image held in memory... how can I index the values for (I assume) RGB and perhaps Alpha? |
|
It's not as straightforward as you hope it will be!
Firstly, to maximise performance, the output bitmap used by BB4W always has the same format as the screen. So it might be 24-bits RGB or 32-bits RGBA or (more likely on older machines) 16-bits 5-6-5 RGB etc.
Secondly, you cannot guarantee that BB4W's output bitmap is actually mapped into your process's address space; it could be resident in kernel memory or graphics memory and therefore inaccessible from BASIC code.
So in general it's either difficult or impossible to read the RGB data directly from memory. If you want to do that, the first thing you must do is to replace BB4W's output bitmap with one which is guaranteed to be memory-mapped and which has a guaranteed format.
The way to do that is to adapt the code you can find in the Help manual under 'Hints and Tips... Using windows larger than 1920 x 1440 pixels'. You don't need to change the window size, but the bitmap created by CreateDIBSection has a format that you specify, and it is guaranteed to be mapped into user memory (it even gives you a handy pointer to the data):
http://www.bbcbasic.co.uk/bbcwin/manual/bbcwini.html#hint2
Richard.
|
| « Last Edit: Feb 11th, 2015, 10:30pm by rtr2 » |
Logged
|
|
|
|
Kipper
New Member
member is offline


Posts: 8
|
 |
Re: Reading and Writing Screen Pixels
« Reply #8 on: Feb 11th, 2015, 11:11pm » |
|
Hi
Trying to make it work but what is the "handy pointer"? Could you give me one line of code showing addressing of data indexed by pointer?
420 SYS "CreateDIBSection", @memhdc%, bmih{}, 0, ^bits%, 0, 0 TO hbm%
Assuming it is in the above line?
Any help appreciated !
Kipper
|
|
Logged
|
|
|
|
rtr2
Guest
|
 |
Re: Reading and Writing Screen Pixels
« Reply #9 on: Feb 12th, 2015, 08:29am » |
|
on Feb 11th, 2015, 11:11pm, Kipper wrote:| Trying to make it work but what is the "handy pointer"? |
|
MSDN has all the details:
https://msdn.microsoft.com/en-us/library/windows/desktop/dd183494.aspx
Quote:| Could you give me one line of code showing addressing of data indexed by pointer? |
|
Sorry, I can't, because I don't know the format of your bitmap.
In general terms you will need to calculate the offset into the bitmap from your x and y coordinates, add that offset to the base address (bits% in the case of the code you listed), then read the pixel data from memory and separate it into its RGB components (assuming you have selected a colour format rather than monochrome).
But the detailed code will depend on your bitmap's format and dimensions (for example that will determine whether you need to pad each line to an exact multiple of DWORDs).
Richard.
|
|
Logged
|
|
|
|
|