Format conversion in memory buffers (no hard disk r/w)

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
dheeraj

Format conversion in memory buffers (no hard disk r/w)

Post by dheeraj »

Hi ,

Can somebody suggest for follwoing scenario:

We will recieve a gif file as a buffer(file handler)
and we want it to conver to a output buffer in png format in a buffer
(Important - I dont want to write the output file in hard disk )

I looked at the example on the site (core.c) and see follwoing 3 main issues

(i) The WriteImageAPI does not provide a way to output to a buffer
(ii) How to specify the destination format ( note- i am not specifying the filename.Is there nay alternative way)
(iii) Tried ReadImage to read from buffer but it is not working

Please see the code i wrote..

Can somebody suggest how to fill the gaps
or ***alternative APIs**** other the ReadImage and WriteImage to achieve the same




//char buffer input contains image data and input_len contais its length

/**************************
QUERY - Is the below way to read supported
as it is not working
**********************/

image_info->file = fmemopen(input, input_len, "r");
images=ReadImage(image_info,exception);


size_t size;
char * output;

FILE *output_file = open_memstream(output, &size);

/***********************

QUERY
How to specify the output buffer/file handler below ( we dont have filename string
as we dont want to write to hard disk) ?

*****/

WriteImage(image_info,images);

Thanks in Advance
rmagick
Posts: 245
Joined: 2006-03-16T17:30:48-07:00
Location: Durham, NC, USA

Re: Format conversion in memory buffers (no hard disk r/w)

Post by rmagick »

Check out the BlobToImage/ImageToBlob APIs
dheeraj

Re: Format conversion in memory buffers (no hard disk r/w)

Post by dheeraj »

Thanks for the tip
Can you point to any examples using these APIs
rmagick
Posts: 245
Joined: 2006-03-16T17:30:48-07:00
Location: Durham, NC, USA

Re: Format conversion in memory buffers (no hard disk r/w)

Post by rmagick »

Not really, unless you want to download the RMagick source code, but they're not all that hard to use. A "blob" is just the image data as if it was in a disk file, except it's in an in-memory buffer. If you call ImageToBlob and write the buffer it returns to disk, then that file is exactly the same as if you'd written the image directly to disk.

There's also an ImagesToBlob API that handles a list of images, such as a GIF animation or Photoshop file with layers.

http://www.imagemagick.org/api/blob.php

I should add: the blob itself needs to be freed after you've finished with it, by calling RelinquishMagickMemory.
Post Reply