Image from a buffer

Blitz3D Forums/Blitz3D Programming/Image from a buffer

yiftafr(Posted 2005) [#1]
Hi Guys,
I'm trying to load an image from a bitmap buffer instead of a file.
The basic idea is that I will link to a DLL (this I know how to do already) and the dll function read the bitmap from a file and then return it.
In the Blitz env I take the buffer and create an image from this buffer.
The question is how do I take the buffer and create the image from it's data?
Any code snippest would be great!

Thanks in advance
Yiftah


markcw(Posted 2005) [#2]
ok, you don't have to use a dll to do this, i would only use a dll if i really had to personally.... but onto the task. ok, this is from something i just wrote a few days ago actually, i got the information from the read/writepixelfast examples i think, the docs can cover this stuff if you can pay attention for long enough that is! heh

this is how to load a bmp and read the pixels into a pixel array for writing to later, comments below.

xdim=256 : ydim=256 ;the image dimension vars
Dim Imagedata(xdim,ydim) ;size pixel array
mypic=LoadImage(filename$) ;load a bmp

DrawImage mypic,0,0 ;draw image to screen
LockBuffer ;must have for ReadPixelFast
 For y=0 To ydim ;x/y loop
  For x=0 To xdim
   Imagedata(x,y)=ReadPixelFast(x,y) ;read to array
  Next
 Next
UnlockBuffer
Cls ;clear image from screen


now you've got all pixels in the array, you want to, ehh...
create the image from the data now. ok, this writes the pixel array to a new image.

 mynewpic=CreateImage(xdim,ydim) ;size new image
 SetBuffer ImageBuffer(mynewpic) ;put image in buffer

LockBuffer ;must have for WritePixelFast
 For y=0 To ydim ;draw new image
  For x=0 To xdim
   WritePixelFast x,y,Imagedata(x,y) ;write to image
  Next
 Next
UnlockBuffer

 SetBuffer BackBuffer() ;reset to screen buffer


I cant garantee this code is 100% perfect, as i havent tested, but it should be. One thing id like to mention is that i found writepixelfast doesn't work in fullscreen 16bit!? but plot does, its a little slower and more complex but at least it works, i guess.

Hope that helps some anyway.


jfk EO-11110(Posted 2005) [#3]
It really depends on the way you want to fill the buffer with image data. If blitz will fill the buffer (read the data from somewhere, eg. using RTLMemoryMove with a Pointer given by the DLL) then it's pretty simple, all you need to do is writing them to the image, as muk suggested.

If the DLL will fill the buffer using a pointer sent by Blitz, you need to send a banks physical adress to the dll. you may send the bank handle to the DLL, as usual, but there's something faster that will let the DLL draw directly to a blitz image. See here:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1518


markcw(Posted 2005) [#4]
i just want to add this bit of information. in my post above i was saying that writepixelfast doesn't work in fullscreen 16-bit. this is wrong as i checked, well, spotted what the reason is.

in the Blitz3d version.txt file it says that a pixel by default has its alpha as full by default, which is why in 16-bit it wasn't working for me. the solution is to mask out the alpha channel with $FFFFFF. So the above code for reading pixels should be:

xdim=256 : ydim=256 ;image dimension vars
Dim Imagedata(xdim,ydim) ;size pixel array
mypic=LoadImage(filename$) ;load a bmp

DrawImage mypic,0,0 ;draw image
LockBuffer
 For y=0 To ydim ;read x/y loop
  For x=0 To xdim
   Imagedata(x,y)=ReadPixelFast(x,y) And $FFFFFF ;<- mask out alpha value
  Next
 Next
UnlockBuffer
Cls ;clear image from screen



Ross C(Posted 2005) [#5]
I think he might be reading the image using a .dll for speed reasons.


yiftafr(Posted 2006) [#6]
Thanks for your help.
I managed through the posts on userlibs forum.


jfk EO-11110(Posted 2006) [#7]
Coppercircle was searching for a similar solution, could you show use how you did it?


yiftafr(Posted 2006) [#8]
I used the method described in this thread :
http://www.blitzbasic.com/Community/posts.php?topic=44333

I blit the Image buffer onto the DD surface using StrechDib or DrawDibDraw GDI functions.