extract YCbCr values from a jpeg-image

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
Wilbert

extract YCbCr values from a jpeg-image

Post by Wilbert »

Hi there!

I'm using MagickCore API to read an image and extract pixels. I would like to know to know if it is possible to extract the YCbCr values from a jpeg image for example (without any conversion to RGB in between).

I'm using the following code to extract pixels:

Code: Select all

void main(int argc, char *argv[])
	
{
    ExceptionInfo *exception;
    Image *image, *images;
    ImageInfo *image_info;
    int z0,z1,z2,z3;

	MagickCoreGenesis(argv[1], MagickTrue);
	image_info = CloneImageInfo((ImageInfo *) NULL);
	exception = AcquireExceptionInfo();
	
	(void) strcpy(image_info->filename, "red.jpg");
	image_info->colorspace = YCbCrColorspace;
	images = ReadImage(image_info, exception);
	
	images = CoalesceImages(images, exception);
	image = GetImageFromList(images, 0);
	
	register const PixelPacket *p;
	
	for (int y=0; y < 480; y++) {
		p = AcquireImagePixels(image, 0, 480-1-y, 640, 1, exception);
		for (int x=0; x < 480; x++) {
			z0=p->blue;
			z1=p->green;
			z2=p->red;
			z3=255-p->opacity;
char BUF[256];
sprintf(BUF, "blue %d, green %d, red %d\n", z0, z1, z2);
OutputDebugString(BUF);
			p++;
		}
	}

    DestroyImage(image);
    DestroyImageInfo(image_info);
    DestroyExceptionInfo(exception);
    MagickCoreTerminus();
}
But as I understand it, PixelPacket supports only RGBA and CMYK. Is there some other way to get the YCbCr values (if the image is stored as YCbCr)?
Last edited by Wilbert on 2009-09-19T06:52:49-07:00, edited 1 time in total.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: extract YCbCr values from a jpeg-image

Post by magick »

Before you read your JPEG image, set the image_info->colorspace member to YCbCrColorspace.
Wilbert

Re: extract YCbCr values from a jpeg-image

Post by Wilbert »

Before you read your JPEG image, set the image_info->colorspace member to YCbCrColorspace.
I can't get it to work. I replaced my code above with something compilable and i added the 'image_info->colorspace = YCbCrColorspace' line before reading the image.

If i extract the blue, green and red values with PixelPacket i get B=G=0, R=254. But I expected the YCbCr values, thus something like G=76, B=85, R=254.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: extract YCbCr values from a jpeg-image

Post by magick »

See if it works from the command line. We're using ImageMagick 6.5.6-1:
  • convert logo: logo.jpg
    convert -colorspace ycbcr logo.jpg logo.miff
    identify -verbose logo.miff
We get a colorspace of YCbCr:
  • -> identify -verbose logo.miff
    Image: logo.miff
    Format: MIFF (Magick Image File Format)
    Class: DirectClass
    Geometry: 640x480+0+0
    Resolution: 72x72
    Print size: 8.88889x6.66667
    Units: PixelsPerInch
    Type: TrueColor
    Base type: TrueColor
    Endianess: Undefined
    Colorspace: YCbCr
    ...
as expected.
Wilbert

Re: extract YCbCr values from a jpeg-image

Post by Wilbert »

Thanks for the fast response!

I get the following

Code: Select all

-> identify -verbose red.miff
Image: red.miff
Format: MIFF (Magick Image File Format)
Class: DirectClass
Geometry: 640x480+0+0
Resolution: 72x72
Print size: 8.88889x6.66667
Units: PixelsPerInch
Type: Palette
Base type: TrueColor
Endianess: Undefined
Colorspace: YCbCr
Channel depth:
  red: 7-bit
  green: 2-bit
  blue: 1-bit
...
The Type is different. Is that a problem? (I'm using 6.5.5.)

edit:

after '(void) strcpy(image_info->filename, "red.jpg")':

image_info->colorspace returns 0

after 'image_info->colorspace = YCbCrColorspace;' :

image_info->colorspace returns 7

edit2: the value of image->colorspace is 1 and doesn't change when adding the 'image_info->colorspace = YCbCrColorspace' line.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: extract YCbCr values from a jpeg-image

Post by magick »

We applied a patch to ImageMagick 6.5.6-2 Beta to fix the problem you reported. The patch will be available by sometime tomorrow. Thanks.
Wilbert

Re: extract YCbCr values from a jpeg-image

Post by Wilbert »

It seems to work fine in the latest build! I'm wondering about one thing though. How can i see in which format the picture is stored?

If i use this code:

Code: Select all

   MagickCoreGenesis(argv[1], MagickTrue);
   image_info = CloneImageInfo((ImageInfo *) NULL);
   exception = AcquireExceptionInfo();
   
   (void) strcpy(image_info->filename, "red.jpg");
   images = ReadImage(image_info, exception);
   
   images = CoalesceImages(images, exception);
   image = GetImageFromList(images, 0);
   sprintf(BUF, "color space %d\n", image->colorspace);
Then the colorspace is 1 (RGB), although i know that for jpeg it's always YCbCr or Y. But for other formats i have no idea in which color format it is stored.
tobeFrank

Re: extract YCbCr values from a jpeg-image

Post by tobeFrank »

Hi there!

I successfully created a ycbcr colospace .miff file using this:
magick wrote:
  • convert -colorspace ycbcr logo.jpg logo.miff
    identify -verbose logo.miff
but now, how can I convert it to a ycbcr colospace .jpg?

Hope the question is not too stupid but I am totally new with image editing...

Thanks a lot for the help!!!
Post Reply