How to copy image into memory buffer regardless of encoding?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Erich
User
User
Posts: 49
Joined: Thu Sep 30, 2010 9:21 pm

How to copy image into memory buffer regardless of encoding?

Post 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?
"I have never let my schooling interfere with my education." - Mark Twain
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

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

Post 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
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
User avatar
netmaestro
PureBasic Bullfrog
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

Post 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.
BERESHEIT
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

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

Post 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
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

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

Post 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 ?
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Erich
User
User
Posts: 49
Joined: Thu Sep 30, 2010 9:21 pm

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

Post 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.
"I have never let my schooling interfere with my education." - Mark Twain
Post Reply