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?".
Locked
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.

Alexvb6
Posts: 53
Joined: 2012-10-12T16:50:15-07:00
Authentication code: 67789

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

Post by Alexvb6 »

Hi,

I have finally found a way to write the ImageMagick output to an in-memory BLOB, and send it directly to the broswer using Response.BinaryWrite, without writing any file on the disk I/O !
This is very great for in-memory manipulation (for example generating on-the-fly Captcha).

I hope this helps a lot of you reading this old thread :)

Code: Select all

<%
'This example will write the output to a "Classic ASP 1D Array acting like an in-memory blob", instead of a file, to avoid disk I/O.
'You can then return this "Classic ASP 1D Array acting like an in-memory blob" the browser !
'Solution found from this page : https://magick-users.imagemagick.narkive.com/Hea8yl2T/converting-blob-to-blob

'Declare the 1D Array that will contain the output image data returned by ImageMagick
Dim outputImgData(0)

''Example : Set the Desired Image Format : GIF
'outputImgData(0) = "GIF:"

''Example : Set the Desired Image Format : JPEG
outputImgData(0) = "JPEG:"

'Create IM Instance
Set IM = Server.CreateObject("ImageMagickObject.MagickImage.1")
	
	'Convert the output image ... then do whatever you want with it... then save it to the "Classic ASP 1D Array acting like an in-memory blob" (instead of a file)
	IM.Convert "logo:", "-resize", "300x300", "-flip", outputImgData

'Release IM Instance Resources
Set IM = Nothing
IM = Empty

'Send the output image from the "Classic ASP 1D Array acting like an in-memory blob" to the Browser
'Response.ContentType = "image/gif"
Response.ContentType = "image/jpeg"
Response.BinaryWrite outputImgData
%>

Locked