How to read raw image data?

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
simba.buffalo

How to read raw image data?

Post by simba.buffalo »

Hi guys, I have a question about how to read raw image data to MagickWand.

I have got a raw image data (char *data) by other methods ( from OpenCV ), now I want to put these data into my MagickWand and do some more processing and save it.

I tried to figure out how to do it but no good result.
Is there any example for this?

Thanks
simba
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: How to read raw image data?

Post by el_supremo »

I know of two ways. The one I am more sure of though is to first read the file in to memory (e.g. unsigned char *data).
Something like this:

Code: Select all

	// ... read the data in to memory and then do this
	magick_wand = NewMagickWand();
	MagickConstituteImage(magick_wand,width,height,"RGB",CharPixel,data);
The "RGB" could be "BGR" if the data is in the reverse order, or if you also have transparency it could be something like "RGBA".

The other way is to read the file directly. I think this will work but haven't tried it:

Code: Select all

	magick_wand = NewMagickWand();
	MagickSetSize(magick_wand,width,height);
	MagickReadImage(magick_wand,"rgb:filename.raw");
Pete
simba.buffalo

Re: How to read raw image data?

Post by simba.buffalo »

Thanks Pete, it works:)

Another question is when I destroy magickwand by using DestroyMagickWand(), it seems delete those data also.

Is there any way un-combine data and my magickwand?

I have tried MagickRemoveImage() but seems no good for me.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: How to read raw image data?

Post by el_supremo »

I presume that you now want to extract the image from the magickwand. In that case you use this function:
MagickBooleanType MagickGetImagePixels(MagickWand *wand,
const long x,const long y,const unsigned long columns,
const unsigned long rows,const char *map,const StorageType storage,
void *pixels)

for example:

Code: Select all

	MagickGetImagePixels(magick_wand,0,0,width,height,"RGB",CharPixel,data);
Pete
simba.buffalo

Re: How to read raw image data?

Post by simba.buffalo »

Actually what I want to do is to read/write a image file by using MagickWand, but process those image data using other library.

So what I did is, load image file and get image pixels to my structure "img" in one routine:

Code: Select all

    
  MagickWandGenesis();
  img_wand = NewMagickWand();
  status   = MagickReadImage(img_wand, filename);
  status = MagickGetImagePixels ( img_wand, 0, 0, width, height,
                                              map, CharPixel, img->data.ptr );
  DestroyMagickWand( img_wand );
  MagickWandTerminus(); 

  return img;
Then do some image processing stuff, and save this image in another routine:

Code: Select all

    MagickWandGenesis();
    img_wand = NewMagickWand();
    data = (char*)malloc(sizeof(char) * img->step * img->rows);
    memcpy( data, img->data.ptr, img->step * img->rows );
    MagickConstituteImage(img_wand, img->cols, img->rows,
                                    map, CharPixel, data);
    status = MagickWriteImages(img_wand, filename, MagickTrue);
    if (status == MagickFalse) {
#ifdef NDEBUG
        ThrowWandException(img_wand);
#endif
        return -1;
    }
    DestroyMagickWand( img_wand );
    MagickWandTerminus(); 
Now the problem I have met are:
If loaded image is RGB format(32bits for each pixel), how can I get pixels as grayscale level(8 bits for each pixel), and how to do it reversely?
In the save image routine, when I destroy magickwand, it cause memory leak, so I am doubt that I misunderstood paramter "map" and "StorageType".

Thanks for reply.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: How to read raw image data?

Post by el_supremo »

The types you can use when you call MagickConstituteImage or MagickGetImagePixels are:

Code: Select all

CharPixel, DoublePixel, FloatPixel, IntegerPixel, LongPixel, QuantumPixel, or ShortPixel
so I presume that instead of using CharPixel your 32 bit images would need IntegerPixel, which is a 32-bit unsigned integer.
If loaded image is RGB format(32bits for each pixel), how can I get pixels as grayscale level(8 bits for each pixel)
You might be able to do this by just extracting an intensity. I haven't tried it but this might work:

Code: Select all

   MagickGetImagePixels(magick_wand,0,0,width,height,"I",CharPixel,data);
This should create one byte per pixel containing the grayscale intensity.
and how to do it reversely?
Just use "I",CharPixel in the call to constitute the image.

Pete
simba.buffalo

Re: How to read raw image data?

Post by simba.buffalo »

Thanks Pete, it is almost done.

The remain problem is when I save my grayscale image (MagickWriteImages), the file is still 24-bits for each pixels not 8bits for each.

Do I need to set something before saving?

sim
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: How to read raw image data?

Post by el_supremo »

If your image is already grayscale then I think all it needs is:

Code: Select all

MagickSetImageDepth(magick_wand,8);
but you might also need

Code: Select all

MagicketImageColorspace(magick_wand,GRAYColorspace);
Pete
simba.buffalo

Re: How to read raw image data?

Post by simba.buffalo »

Now I have a array char * pixels = (char*)malloc(256*256); which corresponds to an gray level image 256*256.

Code: Select all

    img_wand = NewMagickWand();
    status = MagickConstituteImage(img_wand, 256, 256,
                                   "I", CharPixel, pixels);
    status = MagickWriteImages(img_wand, filename, MagickTrue);
    img_wand = DestroyMagickWand( img_wand );
No matter I am using MagickSetImageType, MagickSetImageDepth, MagickSetImageColorspace or any combination of them, it dose not work. The saved image is still 24-bits for each pixel.

The odd thing is that using MagickGetImageDepth, it returns 1, not 8. Or using MagickGetImageType, the return value is "BilevelType", not "GrayscaleType" instead.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: How to read raw image data?

Post by el_supremo »

What is filename? If you write the image as a JPG, for example, it will be 24 bit.
When I create a grayscale image in memory, use constitute and then write it as a PNG file (with no depth or colorspace setting) the resulting PNG file is 8-bit.

Pete
simba.buffalo

Re: How to read raw image data?

Post by simba.buffalo »

what I saved is a bmp file.

I just read a grayscale bmp file whose size is 937426 bytes and save it, then the size becoms 2800306 bytes, almost 3 time of original one.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: How to read raw image data?

Post by el_supremo »

Add this after the MagickConstituteImage:

Code: Select all

	MagickQuantizeImage(magick_wand,256,GRAYColorspace,0,MagickFalse,MagickFalse);
Pete
Post Reply