Page 1 of 1
Fred does image buffer remain static after StopDrawing()?
Posted: Wed Feb 17, 2016 10:11 am
by Keya
Fred (youre the only person who can answer this it seems!) if we obtain the image buffer address with:
Code: Select all
StartDrawing(ImageOutput(hImg))
*pbuffer = DrawingBuffer()
StopDrawing()
Are we then free to modify the buffer, or is there a chance the buffer will for whatever reason get relocated? (ie. do we have to modify the buffer BEFORE calling StopDrawing()?)
Thankyou!
Re: Fred does image buffer remain static after StopDrawing()
Posted: Wed Feb 17, 2016 12:14 pm
by DontTalkToMe
While you are waiting...
The help of DrawingBuffer() says "This function has to be called again if other drawing commands of this library where used since the last pixel manipulation". The library is the 2DDrawing library.
So my interpretation is that until you don't further manipulate the image with PB command you should be safe.
Anyway an answer from Fred would be appreciated, even if I suspect only some commands of the library really cause a reallocation of the buffer.
Re: Fred does image buffer remain static after StopDrawing()
Posted: Wed Feb 17, 2016 12:50 pm
by Fred
StopDrawing() invalidate all drawing operations including DrawingBuffer(). It's not safe to use it after StopDrawing() (it can work by luck, but it's not supported. I will update the doc to make it clear.
Re: Fred does image buffer remain static after StopDrawing()
Posted: Wed Feb 17, 2016 1:13 pm
by Keya
I often need to access two or more image buffers at the same time, so does this mean having to create a 2nd thread with each thread using its own Start/StopDrawing() block and using inter-thread communications? because that sounds tricky and a lot of extra work considering the images really are just static objects in memory, or at least there's no real reason for them to be moving?? could you perhaps add a function like GetDrawingBufferAddress() that can work outside of a Start/StopDrawing block then?
Thankyou!
Re: Fred does image buffer remain static after StopDrawing()
Posted: Wed Feb 17, 2016 1:16 pm
by Fred
I would copy the image into a third buffer and work from there, it shouldn't have a big performance impact.
Re: Fred does image buffer remain static after StopDrawing()
Posted: Wed Feb 17, 2016 1:32 pm
by Keya
Wouldn't a GetDrawingBufferAddress() function make more sense though? Then there's no reason to make a copy of a buffer and copy it back over the one you intended to modify anyway - there's no efficiency in that especially in batch processing
And although I can understand why StopDrawing() might change the buffer location, once that's done there's no reason for it to move again, so the buffer should be accessible at that stage no problems - we just need its address!?
Code: Select all
StartDrawing(ImageOutput(hImg))
*pbuf = DrawingBuffer()
StopDrawing()
;... *pbuf may no longer be valid now, BUT the image buffer (wherever it now is)
;should be static and accessible to us to directly modify? just need the addy!
Re: Fred does image buffer remain static after StopDrawing()
Posted: Wed Feb 17, 2016 2:31 pm
by Fred
It depends of the underlying API. Some give you the real buffer address (Image on Windows), other only a working buffer (OpenGL/DirectX sprite for example) and you need to copy it back to the image. So we can't have such function, unfortunately. Anyway AllocateMemory/CopyMemory are very cheap these days..
Re: Fred does image buffer remain static after StopDrawing()
Posted: Wed Feb 17, 2016 2:33 pm
by Keya
thankyou kindly for clarifying

Re: Fred does image buffer remain static after StopDrawing()
Posted: Wed Feb 17, 2016 3:40 pm
by wilbert
I believe I requested it long ago but what would be convenient is a few fast functions
GetPixelData(#Image, *PixelBuffer)
SetPixelData(#Image, *PixelBuffer)
GetPixelDataRGBA(#Image, *PixelBufferRGBA)
SetPixelDataRGBA(#Image, *PixelBufferRGBA)
The first two functions for working with data in the native platform format.
The second two functions for working with a RGBA top to bottom buffer without any bytes for padding where PureBasic does the format conversion.
Especially the last two would make things so much easier...
- cross platform; not having to write four versions when working with raw data (RGB, BGR, RGBA, BGRA)
- no concerns about flipped images
- no concerns about pitch/stride; a line is always 4 * width bytes
- no problem with drawing buffers open or closed
Edit:
Simply getting the pixel buffer pointer on OSX is possible like this
Code: Select all
Procedure GetImageBuffer(Image)
Protected rep = CocoaMessage(0, CocoaMessage(0, ImageID(Image), "representations"), "firstObject")
ProcedureReturn CocoaMessage(0, rep, "bitmapData")
EndProcedure
[/i]
Re: Fred does image buffer remain static after StopDrawing()
Posted: Wed Feb 17, 2016 5:47 pm
by Michael Vogel
Wilberts request is a fine one!
I would also kindly ask for a possibility to push/pop the drawing state if technical possible. This would allow to write procedures using Start/StopDrawing which can be called from inside different other Start/StopDrawing settings...
Code: Select all
Procedure DrawShadow(x,y,w,h,depth)
Protected temp
temp=CreateImage(#PB_Any,w,h,32|#PB_Image_Transparent)
PushDrawingState(); ***
StartDrawing(ImageOutput(temp))
; Prepare Shadow
StopDrawing()
PopDrawingState(); ***
DrawAlphaImage(ImageID(temp),x,y,depth)
FreeImage(temp)
EndProcedure
CreateImage(1,800,800,32)
StartDrawing(ImageOutput(1))
x=10
y=10
DrawText(x,y,"Test")
DrawShadow(x,y,200,200,20)
StopDrawing()