Page 1 of 1

How to copy image into memory buffer regardless of encoding?

Posted: Mon Jan 04, 2016 6:58 pm
by Erich
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?

Re: How to copy image into memory buffer regardless of encod

Posted: Wed Jan 06, 2016 3:20 am
by citystate
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

Re: How to copy image into memory buffer regardless of encod

Posted: Wed Jan 06, 2016 5:19 am
by netmaestro
s.i=w*h*d
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.

Re: How to copy image into memory buffer regardless of encod

Posted: Wed Jan 06, 2016 6:35 am
by citystate
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 :mrgreen: PB help is your friend

Re: How to copy image into memory buffer regardless of encod

Posted: Wed Jan 06, 2016 7:07 am
by wilbert
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 ?

Re: How to copy image into memory buffer regardless of encod

Posted: Wed Jan 06, 2016 11:21 am
by Erich
Thanks folks, I've already found a workaround, and will also take a look at citystates method. :D
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 ?
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.

So for this a direct counterpart to CatchImage would be handy. Perhaps somebody should submit a feature request.