reading a bitmap buffer instead of point/plot

Just starting out? Need help? Post your questions and find answers here.
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

reading a bitmap buffer instead of point/plot

Post 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

:lol: should I bash the keyboard and give up?
:?
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Post by eesau »

See the CopyImageToMemory and CopyMemoryToImage -procedures here.
superadnim
Enthusiast
Enthusiast
Posts: 480
Joined: Thu Jul 27, 2006 4:06 am

Post 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?

:lol: should I bash the keyboard and give up?
:?
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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?
oh... and have a nice day.
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Post by eesau »

Kaeru Gaman wrote:if not using Peek and Poke, how would you access the bitmap?
By using a structured pointer?
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: reading a bitmap buffer instead of point/plot

Post 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...
I like logic, hence I dislike humans but love computers.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: reading a bitmap buffer instead of point/plot

Post 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.
I may look like a mule, but I'm not a complete ass.
User avatar
idle
Always Here
Always Here
Posts: 5917
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post 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

Post Reply