Page 1 of 1

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

Posted: 2009-03-03T23:50:49-07:00
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

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

Posted: 2009-03-04T12:23:18-07:00
by rmagick
Check out the BlobToImage/ImageToBlob APIs

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

Posted: 2009-03-05T05:21:39-07:00
by dheeraj
Thanks for the tip
Can you point to any examples using these APIs

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

Posted: 2009-03-05T14:42:57-07:00
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.