Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get buffer from Imagemagick image in c++

I am using ImageMagick library for image manipulation. I need to load a 'bmp' image, convert it to jpeg, load it in a buffer and send it across the network.

However, I am not able to find any supporting function in ImageMagick which can convert and store data in buffer. I am only able to write in file. Tried using Magick::Blob but still of no use.

Following code is used to load, convert and write in file:

Magick::Image img("Sample.bmp");
img.magick("jpeg");
img.write("Output.jpeg");

EDIT:

Used Magick::Blob as:

Magick::Blob myBlob;
img.write(&myBlob);
const void *myData = myBlob.data();

But here I can't convert myData to const char* buffer without any loss.

like image 439
Aman Avatar asked Mar 23 '23 13:03

Aman


2 Answers

Did you try :

Magick::Image img("Sample.bmp");

Blob blob; 
img.magick("JPEG");
img.write(&blob);

// Then access blob's data with  blob.data()
sendJpegImage(blob.data(), blob.length());

With void sendJpegImage(void* data, size_t length) being your data sending function.

like image 78
Matthieu Rouget Avatar answered Mar 26 '23 04:03

Matthieu Rouget


Thanks a lot for the answers. My existing socket connection accepts string stream at both ends. That's why I needed const char*. Found conversion function base64() in Magick::Blob which returns string. This solved my problem. For reference, the final code becoms:

Magick::Image img("Sample.bmp");
Magick::Blob blob; 
img.magick( "JPEG" )
img.write( &blob );
std::string myStr = myBlob.base64();
like image 28
Aman Avatar answered Mar 26 '23 03:03

Aman