public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
* Saving YUVY image from V4L2 buffer to file
@ 2010-02-03 17:40 Owen O' Hehir
  2010-02-03 17:54 ` Devin Heitmueller
  2010-02-03 18:38 ` Darren Longhorn
  0 siblings, 2 replies; 12+ messages in thread
From: Owen O' Hehir @ 2010-02-03 17:40 UTC (permalink / raw)
  To: video4linux-list


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@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Saving YUVY image from V4L2 buffer to file
  2010-02-03 17:40 Saving YUVY image from V4L2 buffer to file Owen O' Hehir
@ 2010-02-03 17:54 ` Devin Heitmueller
  2010-02-03 18:06   ` Owen O' Hehir
  2010-02-03 18:38 ` Darren Longhorn
  1 sibling, 1 reply; 12+ messages in thread
From: Devin Heitmueller @ 2010-02-03 17:54 UTC (permalink / raw)
  To: Owen O' Hehir; +Cc: video4linux-list

On Wed, Feb 3, 2010 at 12:40 PM, Owen O' Hehir <oo_hehir@hotmail.com> wrote:
>
> 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:

Independent of the conversion function, are you sure you are getting a
valid YUV frame at all?  A completely green frame is what you will get
back if you had a buffer which was memset(0).  Hence it's possible
that the data you are passing *into* your conversion function is
completely blank.

Devin

-- 
Devin J. Heitmueller - Kernel Labs
http://www.kernellabs.com

--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: Saving YUVY image from V4L2 buffer to file
  2010-02-03 17:54 ` Devin Heitmueller
@ 2010-02-03 18:06   ` Owen O' Hehir
  2010-02-03 18:18     ` Devin Heitmueller
  2010-02-03 18:57     ` Saving YUVY image from V4L2 buffer to file Charlie X. Liu
  0 siblings, 2 replies; 12+ messages in thread
From: Owen O' Hehir @ 2010-02-03 18:06 UTC (permalink / raw)
  To: video4linux-list


Devin,

Many thanks for the quick reply.

Yes I'm getting some sort of an image. When I was experimenting I managed to get an image but in grayscale & showing multiple copies of the same image covering the top half of the image. I imagine it was distorted because I was not converting to RGB correctly.

All the best,

 

Owen




> Date: Wed, 3 Feb 2010 12:54:02 -0500
> Subject: Re: Saving YUVY image from V4L2 buffer to file
> From: dheitmueller@kernellabs.com
> To: oo_hehir@hotmail.com
> CC: video4linux-list@redhat.com
> 
> On Wed, Feb 3, 2010 at 12:40 PM, Owen O' Hehir <oo_hehir@hotmail.com> wrote:
> >
> > 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:
> 
> Independent of the conversion function, are you sure you are getting a
> valid YUV frame at all?  A completely green frame is what you will get
> back if you had a buffer which was memset(0).  Hence it's possible
> that the data you are passing *into* your conversion function is
> completely blank.
> 
> Devin
> 
> -- 
> Devin J. Heitmueller - Kernel Labs
> http://www.kernellabs.com
 		 	   		  
_________________________________________________________________
Hotmail: Powerful Free email with security by Microsoft.
https://signup.live.com/signup.aspx?id=60969
--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Saving YUVY image from V4L2 buffer to file
  2010-02-03 18:06   ` Owen O' Hehir
@ 2010-02-03 18:18     ` Devin Heitmueller
  2010-02-06 20:48       ` Saving YUVY image from V4L2 buffer to file - SOLVED Owen O' Hehir
  2010-02-03 18:57     ` Saving YUVY image from V4L2 buffer to file Charlie X. Liu
  1 sibling, 1 reply; 12+ messages in thread
From: Devin Heitmueller @ 2010-02-03 18:18 UTC (permalink / raw)
  To: Owen O' Hehir; +Cc: video4linux-list

On Wed, Feb 3, 2010 at 1:06 PM, Owen O' Hehir <oo_hehir@hotmail.com> wrote:
>
> Devin,
>
> Many thanks for the quick reply.
>
> Yes I'm getting some sort of an image. When I was experimenting I managed to get an image but in grayscale & showing multiple copies of the same image covering the top half of the image. I imagine it was distorted because I was not converting to RGB correctly.
>
> All the best,

Well, a picture is worth a thousand words, so if you perhaps would
consider throwing one up on imagebin and providing a link, someone
might be able to give you some insight as to the nature of the
problem.

Devin

-- 
Devin J. Heitmueller - Kernel Labs
http://www.kernellabs.com

--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Saving YUVY image from V4L2 buffer to file
  2010-02-03 17:40 Saving YUVY image from V4L2 buffer to file Owen O' Hehir
  2010-02-03 17:54 ` Devin Heitmueller
@ 2010-02-03 18:38 ` Darren Longhorn
  2010-02-04  2:23   ` Andy Walls
  1 sibling, 1 reply; 12+ messages in thread
From: Darren Longhorn @ 2010-02-03 18:38 UTC (permalink / raw)
  To: video4linux-list

Owen O' Hehir wrote:
> 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);

Are you sure that's your real code? I don't think you should dereference
a void pointer like that.

--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: Saving YUVY image from V4L2 buffer to file
  2010-02-03 18:06   ` Owen O' Hehir
  2010-02-03 18:18     ` Devin Heitmueller
@ 2010-02-03 18:57     ` Charlie X. Liu
  2010-02-03 19:18       ` Devin Heitmueller
  1 sibling, 1 reply; 12+ messages in thread
From: Charlie X. Liu @ 2010-02-03 18:57 UTC (permalink / raw)
  To: 'Owen O' Hehir', video4linux-list

Why don't you directly set it with: fmt.fmt.pix.pixelformat =
V4L2_PIX_FMT_BGR24, instead of converting?

Then, save like:

        sprintf( ppmheader, "P6\n#ppm image\n%d %d\n255\n", info->width,
info->height);
        fwrite( ppmheader, 1, strlen(ppmheader), fptr);
        // write out the rows
        for ( i=0; i<info->height; i++)
        {
            //fwrite( &image[info->stride*i], 1, info->stride, fptr);
            for ( j=0; j<info->width; j++ ) {
            fwrite( &image[info->stride*i+j*3+2], 1, 1, fptr);
            fwrite( &image[info->stride*i+j*3+1], 1, 1, fptr);
            fwrite( &image[info->stride*i+j*3+0], 1, 1, fptr);
            }
        }


Charlie X. Liu @ Sensoray Co.


-----Original Message-----
From: video4linux-list-bounces@redhat.com
[mailto:video4linux-list-bounces@redhat.com] On Behalf Of Owen O' Hehir
Sent: Wednesday, February 03, 2010 10:06 AM
To: video4linux-list@redhat.com
Subject: RE: Saving YUVY image from V4L2 buffer to file


Devin,

Many thanks for the quick reply.

Yes I'm getting some sort of an image. When I was experimenting I managed to
get an image but in grayscale & showing multiple copies of the same image
covering the top half of the image. I imagine it was distorted because I was
not converting to RGB correctly.

All the best,

 

Owen




> Date: Wed, 3 Feb 2010 12:54:02 -0500
> Subject: Re: Saving YUVY image from V4L2 buffer to file
> From: dheitmueller@kernellabs.com
> To: oo_hehir@hotmail.com
> CC: video4linux-list@redhat.com
> 
> On Wed, Feb 3, 2010 at 12:40 PM, Owen O' Hehir <oo_hehir@hotmail.com>
wrote:
> >
> > 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:
> 
> Independent of the conversion function, are you sure you are getting a
> valid YUV frame at all?  A completely green frame is what you will get
> back if you had a buffer which was memset(0).  Hence it's possible
> that the data you are passing *into* your conversion function is
> completely blank.
> 
> Devin
> 
> -- 
> Devin J. Heitmueller - Kernel Labs
> http://www.kernellabs.com
 		 	   		  
_________________________________________________________________
Hotmail: Powerful Free email with security by Microsoft.
https://signup.live.com/signup.aspx?id=60969
--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Saving YUVY image from V4L2 buffer to file
  2010-02-03 18:57     ` Saving YUVY image from V4L2 buffer to file Charlie X. Liu
@ 2010-02-03 19:18       ` Devin Heitmueller
  2010-02-03 20:01         ` Charlie X. Liu
  0 siblings, 1 reply; 12+ messages in thread
From: Devin Heitmueller @ 2010-02-03 19:18 UTC (permalink / raw)
  To: Charlie X. Liu; +Cc: Owen O' Hehir, video4linux-list

On Wed, Feb 3, 2010 at 1:57 PM, Charlie X. Liu <charlie@sensoray.com> wrote:
> Why don't you directly set it with: fmt.fmt.pix.pixelformat =
> V4L2_PIX_FMT_BGR24, instead of converting?

That only really works if the hardware supports providing RGB data as
opposed to YUYV (which many do not).  Or, it would work if you link
against libv4l to do the conversion in userland.

Devin

-- 
Devin J. Heitmueller - Kernel Labs
http://www.kernellabs.com

--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: Saving YUVY image from V4L2 buffer to file
  2010-02-03 19:18       ` Devin Heitmueller
@ 2010-02-03 20:01         ` Charlie X. Liu
  2010-02-03 20:12           ` Devin Heitmueller
  0 siblings, 1 reply; 12+ messages in thread
From: Charlie X. Liu @ 2010-02-03 20:01 UTC (permalink / raw)
  To: 'Devin Heitmueller'; +Cc: 'Owen O' Hehir', video4linux-list

Do most of Webcams have no RGB support? I'm not experienced on that. Does
anybody know the percentage of RGB support by Webcams?


-----Original Message-----
From: Devin Heitmueller [mailto:dheitmueller@kernellabs.com] 
Sent: Wednesday, February 03, 2010 11:19 AM
To: Charlie X. Liu
Cc: Owen O' Hehir; video4linux-list@redhat.com
Subject: Re: Saving YUVY image from V4L2 buffer to file

On Wed, Feb 3, 2010 at 1:57 PM, Charlie X. Liu <charlie@sensoray.com> wrote:
> Why don't you directly set it with: fmt.fmt.pix.pixelformat =
> V4L2_PIX_FMT_BGR24, instead of converting?

That only really works if the hardware supports providing RGB data as
opposed to YUYV (which many do not).  Or, it would work if you link
against libv4l to do the conversion in userland.

Devin

-- 
Devin J. Heitmueller - Kernel Labs
http://www.kernellabs.com

--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Saving YUVY image from V4L2 buffer to file
  2010-02-03 20:01         ` Charlie X. Liu
@ 2010-02-03 20:12           ` Devin Heitmueller
  0 siblings, 0 replies; 12+ messages in thread
From: Devin Heitmueller @ 2010-02-03 20:12 UTC (permalink / raw)
  To: Charlie X. Liu; +Cc: Owen O' Hehir, video4linux-list

On Wed, Feb 3, 2010 at 3:01 PM, Charlie X. Liu <charlie@sensoray.com> wrote:
> Do most of Webcams have no RGB support? I'm not experienced on that. Does
> anybody know the percentage of RGB support by Webcams?

Well, the V4L interface doesn't make the distinction between webcams
and other analog capture devices.  That said, webcams do tend to
provide some variant of RGB (and in a number of cases it uses
proprietary formats that include compression that must be handled in
userland).  So there are different formats that would all be
considered some form of RGB.

Other capture devices, such as tuners and home movie converters tend to use YUV.

So, if you're looking to write a simple app for internal use to work
with a particular webcam, you can disregard the above.  But if you are
trying to write a generic capture application that is expected to work
with many products, then you need to take into consideration all the
different formats that can be used.

Devin

-- 
Devin J. Heitmueller - Kernel Labs
http://www.kernellabs.com

--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Saving YUVY image from V4L2 buffer to file
  2010-02-03 18:38 ` Darren Longhorn
@ 2010-02-04  2:23   ` Andy Walls
  2010-02-04 15:58     ` Darren Longhorn
  0 siblings, 1 reply; 12+ messages in thread
From: Andy Walls @ 2010-02-04  2:23 UTC (permalink / raw)
  To: Darren Longhorn; +Cc: video4linux-list

On Wed, 2010-02-03 at 18:38 +0000, Darren Longhorn wrote:
> Owen O' Hehir wrote:
> > 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);
> 
> Are you sure that's your real code? I don't think you should dereference
> a void pointer like that.

Old-ish C-compilers treated that as a char * in that case.  The behavior
is unreliable of course.  This certainly could be a cause of problems.

Regards,
Andy

> --
> video4linux-list mailing list
> Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
> https://www.redhat.com/mailman/listinfo/video4linux-list
> 

--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Saving YUVY image from V4L2 buffer to file
  2010-02-04  2:23   ` Andy Walls
@ 2010-02-04 15:58     ` Darren Longhorn
  0 siblings, 0 replies; 12+ messages in thread
From: Darren Longhorn @ 2010-02-04 15:58 UTC (permalink / raw)
  To: video4linux-list

Andy Walls wrote:
> On Wed, 2010-02-03 at 18:38 +0000, Darren Longhorn wrote:
>> Owen O' Hehir wrote:
>>> 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);
>> Are you sure that's your real code? I don't think you should dereference
>> a void pointer like that.
> 
> Old-ish C-compilers treated that as a char * in that case.  The behavior
> is unreliable of course.  This certainly could be a cause of problems.

Ah, yes. Well remembered!

Cheers

Darren

--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: Saving YUVY image from V4L2 buffer to file - SOLVED
  2010-02-03 18:18     ` Devin Heitmueller
@ 2010-02-06 20:48       ` Owen O' Hehir
  0 siblings, 0 replies; 12+ messages in thread
From: Owen O' Hehir @ 2010-02-06 20:48 UTC (permalink / raw)
  To: dheitmueller; +Cc: video4linux-list


Hello All, 

Solved this problem.

For info for others here is a function to unpacked YUYV (Also known as YUY422 & YUY2) -> RGB and save it to a file. I've included other formats that I came across that others might find useful but are untested.

int frame_save(const void *p, const char* filename )
{

    static int packed_value, i;
    FILE* fp = fopen(filename, "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( stderr, "&userframe address %d\n", &userframe );
    // fprintf( stderr, "&p address %d\n", &p );
    // fprintf( stderr, "sizeof(userframe) %d\n", sizeof(userframe) );
    
//     switch (userfmt.fmt.pix.pixelformat){
//     case V4L2_PIX_FMT_RGB24:
//         
//         // Max val
//         // NOTE UNTESTED!!!!!!!!!
//         fprintf( fp, "255\n" );
//         fprintf( stderr, "frame_save(): RGB24 Unsupported type!\n" );
// 
//         //Write image data
//         for ( i = 0; i < ( userfmt.fmt.pix.width * userfmt.fmt.pix.height ); i++ )
//         {
//         // 3 bytes per pixel
// //         rgb = ((FRAME_RGB*)(p))[i];
// // 
// //         fprintf( fp, "%c%c%c",
// //              rgb.blue,
// //              rgb.green,
// //              rgb.red );
//         }
//         break;
//         
//     case V4L2_PIX_FMT_RGB32:
//         // NOTE UNTESTED!!!!!!!!!
//         // Max val
//         fprintf( fp, "255\n" );
//         
//         // Write image data
//         for ( i = 0; i < ( userfmt.fmt.pix.width * userfmt.fmt.pix.height ); i++ )
//         {
//         // Retrieve lower 24 bits of ARGB
//         packed_value = ((int*)(p))[i] & 0x00ffffff;
//         
//         fprintf( fp, "%c%c%c",
//              ( packed_value & 0x00ff0000 ) >> 16, // Blue
//              ( packed_value & 0x0000ff00 ) >>  8, // Green
//              ( packed_value & 0x000000ff )        // Red
//              );
//         }
//         break;
//         
//     case V4L2_PIX_FMT_RGB565:
//     case V4L2_PIX_FMT_RGB555:
//         // NOTE UNTESTED!!!!!!!!!
//         // Max val
//         fprintf( fp, "65535\n" );
//         
//         // Write image data
//         for ( i = 0; i < ( userfmt.fmt.pix.width *userfmt.fmt.pix.height ); i++ ){
//         // Retrieve 16-bit words
//         packed_value = ((short*)(p))[i];
//         
//         fprintf( fp, "%c%c",frame
//              ( packed_value & 0xff00 ) >> 8, // High
//              ( packed_value & 0x00ff )       // Low
//              );
//             }
//         break;
// 
//     case V4L2_PIX_FMT_YUYV:
        int Y0, Y1, Cb, Cr;            /* gamma pre-corrected input [0;255] */
        int ER0,ER1,EG0,EG1,EB0,EB1;    /* output [0;255] */
        double r0,r1,g0,g1,b0,b1;             /* temporaries */
        double y0,y1, pb, pr;

        // Max val
        fprintf( fp, "255\n" );
        //fprintf( stderr, "frame_save(): YUYV file type!\n" );

        while(i < (userfmt.fmt.pix.width * userfmt.fmt.pix.height/2)){

        packed_value = *((int*)p+i);

        Y0 = (char)(packed_value & 0xFF);
        Cb = (char)((packed_value >> 8) & 0xFF);
        Y1 = (char)((packed_value >> 16) & 0xFF);
        Cr = (char)((packed_value >> 24) & 0xFF);

        // Strip sign values after shift (i.e. unsigned shift)
        Y0 = Y0 & 0xFF;
        Cb = Cb & 0xFF;
        Y1 = Y1 & 0xFF;
        Cr = Cr & 0xFF;

        //fprintf( fp, "Value:%x Y0:%x Cb:%x Y1:%x Cr:%x ",packed_value,Y0,Cb,Y1,Cr);

        y0 = (255 / 219.0) * (Y0 - 16);
        y1 = (255 / 219.0) * (Y1 - 16);
        pb = (255 / 224.0) * (Cb - 128);
        pr = (255 / 224.0) * (Cr - 128);

        // Generate first pixel
        r0 = 1.0 * y0 + 0     * pb + 1.402 * pr;
        g0 = 1.0 * y0 - 0.344 * pb - 0.714 * pr;
        b0 = 1.0 * y0 + 1.772 * pb + 0     * pr;

        // Generate next pixel - must reuse pb & pr as 4:2:2
        r1 = 1.0 * y1 + 0     * pb + 1.402 * pr;
        g1 = 1.0 * y1 - 0.344 * pb - 0.714 * pr;
        b1 = 1.0 * y1 + 1.772 * pb + 0     * pr;

        ER0 = clamp (r0);
        ER1 = clamp (r1);
        EG0 = clamp (g0);
        EG1 = clamp (g1);
        EB0 = clamp (b0);
        EB1 = clamp (b1);

        fprintf( fp, "%c%c%c%c%c%c",ER0,EG0,EB0,ER1,EG1,EB1); // Output two pixels
        //fprintf( fp, "Memory:%p Pixel:%d R:%d G:%d B:%d     Pixel:%d R:%d G:%d B:%d \n",location,val,ER0,EG0,EB0,(val+1),ER1,EG1,EB1);

        i++;
        }

        //fprintf( stderr, "Size of packed_value:%d Y0:%d Cb:%d Cr:%d Y1:%d\n", sizeof(packed_value), sizeof(y0), sizeof(cb0), sizeof(y1), sizeof(cr0));

//        break;
// 
//     default:
//         // Unsupported!
//         fprintf( stderr, "frame_save(): Unsupported type!\n" );
//         return -1;
//         }

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

All the best,

 

Owen


> Date: Wed, 3 Feb 2010 13:18:44 -0500
> Subject: Re: Saving YUVY image from V4L2 buffer to file
> From: dheitmueller@kernellabs.com
> To: oo_hehir@hotmail.com
> CC: video4linux-list@redhat.com
> 
> On Wed, Feb 3, 2010 at 1:06 PM, Owen O' Hehir <oo_hehir@hotmail.com> wrote:
> >
> > Devin,
> >
> > Many thanks for the quick reply.
> >
> > Yes I'm getting some sort of an image. When I was experimenting I managed to get an image but in grayscale & showing multiple copies of the same image covering the top half of the image. I imagine it was distorted because I was not converting to RGB correctly.
> >
> > All the best,
> 
> Well, a picture is worth a thousand words, so if you perhaps would
> consider throwing one up on imagebin and providing a link, someone
> might be able to give you some insight as to the nature of the
> problem.
> 
> Devin
> 
> -- 
> Devin J. Heitmueller - Kernel Labs
> http://www.kernellabs.com
 		 	   		  
_________________________________________________________________
Hotmail: Free, trusted and rich email service.
https://signup.live.com/signup.aspx?id=60969
--
video4linux-list mailing list
Unsubscribe mailto:video4linux-list-request@redhat.com?subject=unsubscribe
https://www.redhat.com/mailman/listinfo/video4linux-list

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2010-02-06 20:48 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-02-03 17:40 Saving YUVY image from V4L2 buffer to file Owen O' Hehir
2010-02-03 17:54 ` Devin Heitmueller
2010-02-03 18:06   ` Owen O' Hehir
2010-02-03 18:18     ` Devin Heitmueller
2010-02-06 20:48       ` Saving YUVY image from V4L2 buffer to file - SOLVED Owen O' Hehir
2010-02-03 18:57     ` Saving YUVY image from V4L2 buffer to file Charlie X. Liu
2010-02-03 19:18       ` Devin Heitmueller
2010-02-03 20:01         ` Charlie X. Liu
2010-02-03 20:12           ` Devin Heitmueller
2010-02-03 18:38 ` Darren Longhorn
2010-02-04  2:23   ` Andy Walls
2010-02-04 15:58     ` Darren Longhorn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox