MagickReadImageBlob()

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
Prince09

MagickReadImageBlob()

Post by Prince09 »

Hello ... I am trying to read an image from a buffer (an RGBA blob) using MagickReadImageBlob(). The program is running but doing something weird - It is actually getting a weirdly downsampled version of the image - It divides the image to 4 quadrants, and puts a downsampled version of the image in the upper two quadrants and leaves the lower two completely blank.

I filled the buffer (an unsigned char* ) by sequentially filling it up in the R-G-B-A for each pixel. The image is read row-by-row. Does the function require the blob to in a format different from this?

Any help will be appreciated :)
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: MagickReadImageBlob()

Post by magick »

Use MagickSetImageDepth() and set the wand depth to 8. See if that fixes the problem.
Prince09

Re: MagickReadImageBlob()

Post by Prince09 »

Hi Magick, thanks for the reply - but it didn't work out yet ... I've used MagickSetSize(wand, width, height), MagickSetFormat(wand, "rgba") along with MagickSetImageDepth(wand, 8).
Anything else I'm missing??
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: MagickReadImageBlob()

Post by magick »

You'll need to post a minimum set of code we can download, compile, and run to reproduce the problem. Our standard mantra is if we can reproduce a problem, we can fix it.
Prince09

Re: MagickReadImageBlob()

Post by Prince09 »

Thanks for the replies ... Sure - I'll post some code !! .. Here's what I think is relevant ...

Filling the Buffer
Pixelwand** pixels;
for (y=0; y < height ; y++) {

pixels=PixelGetNextIteratorRow(iterator,&width);
for (x=0; x < width; x++)
{
int depth = MAGICKCORE_QUANTUM_DEPTH;
int denom = pow(2, depth);
PixelGetMagickColor(pixels[x],&pixel);
*(pixelBuffer + (4 * (width) * y) + 4*x) = (char)(255 * pixel.red/denom); // Force to 8-bits;
*(pixelBuffer + (4 * (width) * y) + 4*x + 1) =(char)(255 * pixel.green/denom); // Force to 8-bits
*(pixelBuffer + (4 * (width) * y) + 4*x + 2) = (char)(255 * pixel.blue/denom); // Force to 8-bits
*(pixelBuffer + (4 * (width) * y) + 4*x + 3) = (char)(255 * PixelGetAlpha(pixels[x])); // Forced to 8-bits
}
}

Read Pixels from buffer

MagickSetSize(magick_wand, width, height);
MagickSetFormat(magick_wand, "rgba");
MagickSetImageDepth(magick_wand, 8 );
status=MagickReadImageBlob(magick_wand, pixelBuffer, 4 * height * width);
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: MagickReadImageBlob()

Post by magick »

To investigate we need a complete source code module that compiles and permits us to reproduce the problem.
Prince09

Re: MagickReadImageBlob()

Post by Prince09 »

Sorry for being obscure here, but I am not able to find a minimal way to put up the code, as the code is spread across different functions and packages I'm working on.

Anyways, I see what is happening wrong with it : Magick, u got it rite when u said it has to do with the Pixel Depth. I traced the program with GDB and I found that the MagickReadImageBlob is reading 16-bit pixels, effectively leaving half-the image (as my buffer is made of 8-bit pixels), leading to the weird effect described in the original post.

I found a solution, though I don't really like it much: I reinstalled IM with the build parameter MAGICKCORE_QUANTUM_DEPTH set to 8. That set things right, though the program would be heavily dependent on the installation of IM.

Additionally, I find that the function MagickSetImageDepth() is not really doing anything, as 5-6 lines into its execution, I see that it finds the parameter wand->images to be NULL and exits the function going into a block of exception-related code.
Prince09

Re: MagickReadImageBlob()

Post by Prince09 »

So, basically it comes down to this: Is there any way that we can change the parameter MAGICKCORE_QUANTUM_DEPTH through MagickWand?
(because this is what MagickReadImageBlob uses as bits per pixel to read images from buffers.).

Alternately, one can just create a copy of the buffer in the pattern the ReadImageBlob function expects it to be, and solve the problem. (This Takes up a lot of memory though and wouldn't sit pretty with low-end devices..)
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: MagickReadImageBlob()

Post by magick »

The quantum depth is a compile time attribute. It cannot be set at run time.
Post Reply