Page 1 of 1

reading a bitmap buffer instead of point/plot

Posted: Mon Apr 20, 2009 1:14 pm
by superadnim
how can i read a bitmap buffer directly instead of doing point/plot?

i'd like to still be able to do an X and Y for loop but without having to use the aforementioned slow routines.

my image was created using CreateImage so i have a valid handle to work with, i just need to know how to go about this in a nice way. im sure its got something to do with GetDIBits() but im not certain.

thanks

Posted: Mon Apr 20, 2009 1:35 pm
by eesau
See the CopyImageToMemory and CopyMemoryToImage -procedures here.

Posted: Mon Apr 20, 2009 1:46 pm
by superadnim
It's all good but the examples make use of peek/poke and im trying to avoid any calls. ideally i would like to either keep the 2 loops or just do 1 loop for the entire run and then whenever i want to be able to retrieve the x and y position out of that given iteration / offset. any examples?

edit: I know that given x and y i can get the linear offset by doing y*width+x but how do i do the opposite? (retrieve x and y from the offset)
edit2: perhaps doing x = (i % Width) : y = (i / Height) ? but how can i avoid the % operator?

Posted: Mon Apr 20, 2009 1:59 pm
by Kaeru Gaman
:?: I don't get your point....

if not using Peek and Poke, how would you access the bitmap?

and what is the problem with the % operator?

Posted: Mon Apr 20, 2009 2:06 pm
by eesau
Kaeru Gaman wrote:if not using Peek and Poke, how would you access the bitmap?
By using a structured pointer?

Re: reading a bitmap buffer instead of point/plot

Posted: Mon Apr 20, 2009 2:14 pm
by Joakim Christiansen
superadnim wrote:how can i read a bitmap buffer directly instead of doing point/plot?
This works great:

Code: Select all

image = CreateImage(#PB_Any,640,480,32)
imageID = ImageID(image)

GetObject_(imageID,SizeOf(BITMAP),@bmp.BITMAP)
imageSize = bmp\bmWidthBytes * bmp\bmHeight
*bits = bmp\bmBits

For x=0 To 640-1
  For y=0 To 480-1
    pos = (y * bmp\bmWidth + x) << 2
    color = PeekL(*bits+pos)
  Next
Next
edit: I know that given x and y i can get the linear offset by doing y*width+x but how do i do the opposite? (retrieve x and y from the offset)
I also wonder about that...

Re: reading a bitmap buffer instead of point/plot

Posted: Mon Apr 20, 2009 2:26 pm
by srod
Joakim Christiansen wrote:
superadnim wrote:edit: I know that given x and y i can get the linear offset by doing y*width+x but how do i do the opposite? (retrieve x and y from the offset)
I also wonder about that...
As Kaeru indicated...

Code: Select all

pixel = byteOffsetIntoPixelData >> 2

y = pixel / bmp\bmWidth
x = pixel % bmp\bmWidth
(Hard coded for 32-bits per pixel).

Or...

Code: Select all

y = byteOffsetIntoPixelData / bmp\bmWidthBytes 
x = (byteOffsetIntoPixelData % bmp\bmWidthBytes) >> 2
as appropriate.

Posted: Mon Apr 20, 2009 9:57 pm
by idle
for 32bit dib using the CopyImageToMemory routine linked a few posts above

Code: Select all

    

     *px.LONG   
     *px = *mem + ((x + (y * width)) << 2)  
 
      R=*px\l & $FF
      G=*px\l >> 8 & $FF
      B=*px\l >> 16 & $FF