I need to get a memory buffer directly from an image, without saving the image to disk in between (would be too slow). Basically, the opposite of CatchImage. EncodeImage does not work for me because I don't know the encoding in advance.
Is there a way to do this for any image encoding on any platform?
How to copy image into memory buffer regardless of encoding?
How to copy image into memory buffer regardless of encoding?
"I have never let my schooling interfere with my education." - Mark Twain
Re: How to copy image into memory buffer regardless of encod
haven't tested this, but I think it should work:
Code: Select all
Procedure.i ImageMemory(imgNum.i)
If IsImage(imgNum)
w.w=ImageWidth(imgNum)
h.w=ImageHeight(imgNum)
d.w=ImageDepth(imgNum,#PB_Image_InternalDepth)/8
s.i=w*h*d
*b=AllocateMemory(s)
StartDrawing(ImageOutput(imgNum))
CopyMemory(DrawingBuffer(),*b,s)
StopDrawing()
ProcedureReturn *b
EndIf
EndProcedure
there is no sig, only zuul (and the following disclaimer)
WARNING: may be talking out of his hat
WARNING: may be talking out of his hat
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: How to copy image into memory buffer regardless of encod
Not quite going to work because of alignment padding. Use DrawingBufferFormat() to be sure what you're dealing with and then use DrawingBufferPitch() in place of w.s.i=w*h*d
BERESHEIT
Re: How to copy image into memory buffer regardless of encod
thanks for the correction - I'll have to wait until I get to a computer with PB installed to test and correct it, in the meantime Erich, I'll leave it as an exercise for you to correct
PB help is your friend

there is no sig, only zuul (and the following disclaimer)
WARNING: may be talking out of his hat
WARNING: may be talking out of his hat
Re: How to copy image into memory buffer regardless of encod
Erich wrote:EncodeImage does not work for me because I don't know the encoding in advance.

Can you explain that you don't know the encoding in advance ?
Once an image has been loaded, there is no encoding, only pixels.
For EncodeImage you only need to know in what type of encoding you want the data to be.
Or do you need access to the pixel data ?
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: How to copy image into memory buffer regardless of encod
Thanks folks, I've already found a workaround, and will also take a look at citystates method.
So for this a direct counterpart to CatchImage would be handy. Perhaps somebody should submit a feature request.

The problem is that the image content is stored in an encrypted database along with metadata and reversible operations are performed on it (e.g. rotate) that yield images instead of memory buffers. Changing the encoding to get the memory content would be a nasty hack, because the encoding is not stored in the database, it can only be guessed from the file extension. If the stored data is extracted to a file again, the file must have exactly the same name as the original (that's a requirement), so I'd end up with a file that has an encoding that doesn't match its extension.wilbert wrote:Erich wrote:EncodeImage does not work for me because I don't know the encoding in advance.![]()
Can you explain that you don't know the encoding in advance ?
Once an image has been loaded, there is no encoding, only pixels.
For EncodeImage you only need to know in what type of encoding you want the data to be.
Or do you need access to the pixel data ?
So for this a direct counterpart to CatchImage would be handy. Perhaps somebody should submit a feature request.
"I have never let my schooling interfere with my education." - Mark Twain