Create an image from a raw pointer


#1

Hi JUCE!!

may someone explain me whether there is a possibility of constructing an image object from some raw data (which I represents the pixels of the image, and of course I know its size)

Thank you
Nathaniel


#2

Hi Nathaniel!

You can create an ImageComponent and then call this on it:

setImage (ImageCache::getFromMemory (const void* imageData, const int dataSize))

#3

I think getFromMemory doesn’t take raw pixel data, but any known encoded format. Otherwise the cache would not be able to guess the size of the resulting image, if there was an image reader for raw format?

I didn’t try it myself, but eventually Image::BitmapData could help. I guess, it works like

Image myImage (Image::RGB, 640, 480);
Image::BitmapData imageData (myImage, Image::BitmapData::writeOnly);
memcpy (imageData.data, yourRawData, yourRawData_size);

I’m curious too if it works…
Good luck


#4

Hi,

@luigisambuy: indeed Daniel seems to be right, how getFromMemory is supposed to know if my raw data represents RGB or ARGB?

@daniel: the thing is there is apparently no constructor for juce::Image taking BitmapData as an argument

Thank you


#5

No, I think it works the other way round: you create the image as a blank image. The BitmapData acts as accesor to the data (note it takes a reference to the image, hence I think it will work on the images pixel data). So your memcopy to the imageData.data pointer should actually land at the pixel data of the image?

I got there via the actual pixel data ImagePixelData, but they write not to use that one directly…


#6

oh right! got you.
It works nice!!

Thank you


#7

Cool, good to know, thanks