
I'm, trying to implement a format conversion using MagickWand C api.
The source image is an YUV 422p 720x576 frame we get from a custom hardware.
The first part of the image contains all the Y components and the second part contains half U and half V components interlaced.
I can successfuly convert this image to any format using the below command line:
# convert -interlace plane -depth 8 -sampling-factor 4:2:2 -size 720x576 frame_442.yuv frame_442_cmdline.jpg
What I'm not able to do is implement the same conversion using C Wand api, because in the resulting image all the colors are wrong.
This is the code I'm using:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <wand/MagickWand.h>
#define ThrowWandException(wand) \
{ \
char \
*description; \
\
ExceptionType \
severity; \
\
description=MagickGetException(wand,&severity); \
(void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
description=(char *) MagickRelinquishMemory(description); \
exit(-1); \
}
int main(int argc,char **argv)
{
MagickBooleanType status;
MagickWand *wand;
double factors422[3] = { 4.0, 2.0, 2.0 };
double factors211[3] = { 2.0, 1.0, 1.0 };
if (argc != 3) {
fprintf(stdout, "Usage: %s in.yuv out.jpg\n", argv[0]);
exit(0);
}
MagickWandGenesis();
wand = NewMagickWand();
status = MagickSetInterlaceScheme(wand, PlaneInterlace);
if (status != MagickTrue) {
ThrowWandException(wand);
}
status = MagickSetDepth(wand, 8);
if (status != MagickTrue) {
ThrowWandException(wand);
}
status = MagickSetSamplingFactors(wand, 3, factors422);
if (status != MagickTrue) {
ThrowWandException(wand);
}
status = MagickSetSize(wand, 720, 576);
if (status != MagickTrue) {
ThrowWandException(wand);
}
// No difference w/ or w/o
//status = MagickSetFormat(wand, "YUV");
//if (status != MagickTrue) {
// ThrowWandException(wand);
//}
// No difference w/ or w/o
//status = MagickSetColorspace(wand, YUVColorspace);
//if (status != MagickTrue) {
// ThrowWandException(wand);
//}
// convert -interlace plane -depth 8 -sampling-factor 4:2:2 -size 720x576 in.yuv out.jpg
status = MagickReadImage(wand, argv[1]);
if (status == MagickTrue) {
size_t compression = 92;
status = MagickSetFormat(wand, "JPG");
if (status != MagickTrue) {
ThrowWandException(wand);
}
status = MagickSetImageDepth(wand, 8);
if (status != MagickTrue) {
ThrowWandException(wand);
}
status = MagickSetSamplingFactors(wand, 3, factors211);
if (status != MagickTrue) {
ThrowWandException(wand);
}
status = MagickSetImageCompressionQuality(wand, compression);
if (status != MagickTrue) {
ThrowWandException(wand);
}
// No difference w/ or w/o
//status = MagickSetInterlaceScheme(wand, JPEGInterlace);
//if (status != MagickTrue) {
// ThrowWandException(wand);
//}
// No difference w/ or w/o
//status = MagickSetColorspace(wand, RGBColorspace);
//if (status != MagickTrue) {
// ThrowWandException(wand);
//}
status = MagickWriteImage(wand, argv[2]);
if (status != MagickTrue) {
ThrowWandException(wand);
}
}
wand = DestroyMagickWand(wand);
MagickWandTerminus();
return(0);
}
This is the source image: http://www.naustech.com/magick/frame_442.yuv
This is the correct result using command line convert: http://www.naustech.com/magick/frame_442_cmdline.jpg
This is the wrong-colors version using the C api: http://www.naustech.com/magick/frame_442.jpg
Any help would be really appreciated

Thanks in advance, best regards, Gabriele.