Convert Image raw data of Unit8_t type to JPEG

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
images
Posts: 9
Joined: 2019-08-26T15:26:11-07:00
Authentication code: 1152

Convert Image raw data of Unit8_t type to JPEG

Post by images »

Hi

I am having raw data in an image buffer of uint8_t type. I am trying to convert this data to image format (JPG preferably). I am using MagickConstituteImage() to do this.

Code Snippet for reference:

Code: Select all

wand = NewMagickWand();
MagickConstituteImage(wand,2560,1600,"RGB",CharPixel,buffer);
MagickSetImageDepth(wand, 32);
MagickSetImageColorspace(wand, RGBColorspace);
MagickSetImageFormat(wand, "jpg");    
MagickSetImageExtent(wand, 1920, 1080));
MagickWriteImage(wand, "output.jpg");
ClearMagickWand(wand);
P.S buffer size is 2560 X 1600 x (32 >>3) (where 32 is the number of bits per pixel). I am setting the depth to 32 bits after I am using MagickConstituteImage()

I am able to get an image but its faded and looks like a grayscale image. I cannot create a blob using MagickReadImageBlob() before calling MagickConstituteImage() because there are no images in my wand at first. Any suggestions on how can I fix it or if my sequence of function calls is wrong?

Thanks
Last edited by images on 2019-08-26T16:12:08-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert Image raw data of Unit8_t type to JPEG

Post by snibgo »

What version of IM?

JPEG output will be 8 bits/channel/pixel. Setting the depth to 32 bits/channel/pixel is wrong, but may be harmless.

What the the encoding of your input? If (non-linear) sRGB, then don't set it to (linear) RGB.

You have no code that actually writes an output.
snibgo's IM pages: im.snibgo.com
images
Posts: 9
Joined: 2019-08-26T15:26:11-07:00
Authentication code: 1152

Re: Convert Image raw data of Unit8_t type to JPEG

Post by images »

Thanks for your reply.

I have installed libmagickwand-dev - version (8:6.8.9.9-7ubuntu5.14).

I am new to this image processing so not really sure of the linear and non-linear RGB formats. However, I am capturing data from a Virtual Machine's screen via VGA port. So its is VGA frame buffer captured at multiple instants in time which I want to store as screenshots.

I am using MagickWriteImage(wand, "output.jpg"); to store my images.

Please find the updated code here:
wand = NewMagickWand();
MagickConstituteImage(wand,2560,1600,"RGB",CharPixel,buffer);
MagickSetImageDepth(wand, 32);
MagickSetImageColorspace(wand, RGBColorspace);
MagickSetImageFormat(wand, "jpg");
MagickSetImageExtent(wand, 1920, 1080));
MagickWriteImage(wand, "output.jpg");
ClearMagickWand(wand);
images
Posts: 9
Joined: 2019-08-26T15:26:11-07:00
Authentication code: 1152

Re: Convert Image raw data of Unit8_t type to JPEG

Post by images »

My issue got resolved. A small summary of the issue just in case if anybody else needs it.

The buffer I was sending in to MagickConstituteImage(), had image format as BGRP and Since i am using 32 BPP, it has 4 bytes for pixels (1 byte for R, B and G each and 1 additional reserved).

Code flow is as follows

Code: Select all

MagickConstituteImage() -> ConstituteImage() -> ImportImagePixels()-> ImportCharPixel().
A little debugging showed that my data is coming in BGRP format. Since I was sending "RGB" to MagickConstituteImage(), I was getting an inverted image.
Post Reply