Web lists-archives.com

Saving YUVY image from V4L2 buffer to file




Hello All,

I'm trying to save a captured image from a USB camera to a file. The capture is based on V4L2 video capture example from the V4L2 API spec. http://v4l2spec.bytesex.org/spec/a16706.htm

The V4L2 set pointers (via mmap) to to the USB image (in YUV 4:2:2 (YUYV)) and as far as I can see the simplest way to save the image in a recognised format is in RGB format, specifically in PPM (Netpbm color image format).

As such I've expanded the process_image function:


static void
process_image                   (const void *           p)
{
    static int count = 0;

    static int r,g,b;
    static int y1,y2,cb,cr;

    int pixel=0;

        FILE* fp = fopen("datadump", "w" );
        // Write PNM header
        fprintf( fp, "P6\n" );
        fprintf( fp, "# YUV422 frame -> RGB \n" );
        fprintf( fp, "%d %d\n", userfmt.fmt.pix.width, userfmt.fmt.pix.height );

        fprintf( fp, "255\n" );

        while(pixel < (userfmt.fmt.pix.width * userfmt.fmt.pix.height)){

        y1 = *(p+pixel);
        pixel++;
        cb= *(p+pixel);    //modified U
        pixel++;
        y2=*(p+pixel);
        pixel++;
        cr= *(p+pixel);    //modified V
        pixel++;

        r =y1 + (1.402*cb);
        g = y1 - (0.344*cb) - (0.714*cr);
            b = y1 + (1.772*cr);

        if (r > 255) r = 255;
        if (g > 255) g = 255;
        if (b > 255) b = 255;

        if (r < 0) r = 0;
        if (g < 0) g = 0;
        if (b < 0) b = 0;

            fprintf( fp, "%c%c%c",r,g,b);

        //Second pixel,reuse cb & cr, new y value

        r =y2 + (1.402*cb);
        g = y2 - (0.344*cb) - (0.714*cr);
        b = y2 + (1.772*cr);

        if (r > 255) r = 255;
        if (g > 255) g = 255;
        if (b > 255) b = 255;

        if (r < 0) r = 0;
        if (g < 0) g = 0;
        if (b < 0) b = 0;

        fprintf( fp, "%c%c%c",r,g,b);

            }

        fclose( fp );
        fprintf( stderr, "frame saved\n" );

    fflush (stdout);
}

However I'm only getting a green frame out. Could anybody point me in the right direction? 

Many thanks,

Owen
 		 	   		  
_________________________________________________________________
Hotmail: Trusted email with powerful SPAM protection.
https://signup.live.com/signup.aspx?id=60969
--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@xxxxxxxxxx?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list