Perl GD/playing with YUV422 color space

From Teknologisk videncenter
Jump to: navigation, search
Generated PNG file


PNG file read from v4l2 with capture.c

#!/usr/bin/perl
 use GD;
 use GD::Image;


#Y'UV422
#Input: Read 4 bytes of Y'UV (u, y1, v, y2 )
#Output: Writes 6 bytes of RGB (R, G, B, R, G, B)
#u  = yuv[0];
#y1 = yuv[1];
#v  = yuv[2];
#y2 = yuv[3];
open FILE,'< C:\temp\frame.1';
#open FILE, '< C:\temp\leg';
binmode FILE;

#Quick and dirty - re
#read FILEHANDLE,SCALAR,LENGTH,OFFSET

my $in;
my @tmp;
my @pix;
for (my $x=0 ; $x < 480; $x++) {
    if ( $x % 60 == 0) { print "\n" };
    print ".";
	for ( my $y=0; $y < 640; $y+=2) {
		if ( read(FILE, $in, 4) == 0 ) {
        	print "FEJL; $!\n";
        	die("Read error: x = $x, y = $y\n");
        }
		#my @up = unpack("CCCC",$in);
		@tmp = unpack("CCCC", $in);
		$pix[$x][$y] = $tmp[0];
		$pix[$x][$y+1] = $tmp[2];
    }
}
print "\n";
print "pix is ", ($#pix+1) * ($#{$pix[1]}+1), " in size\n";
#for (my $i=0; $i <= $#up; $i++) {
#	print "Item ", $i+1, " is ",$up[$i];
#}

#Make PNG file

    # create a new image
    $im = new GD::Image(480,640);

    #Grey scale
    my @grey;
    for (my $i = 0; $i <=255; $i++) {
    	$grey[$i] = $im->colorAllocate($i,$i,$i);
    }
    print "Allocated ", $#grey+1, " colors\n";
    # allocate some colors
    # make the background transparent and interlaced
    #$im->transparent($white);
    #$im->interlaced('true');
    for( my $i=0; $i < 480 ;$i++ ) {
    	for( my $j=0; $j < 640;$j++ ) {
            #$im->setPixel($i,$j,@grey[ (log(($i)+1)*$j ) % 256 ]);
            #$im->setPixel($i,$j, $c[($i+$j)%4]);
            $im->setPixel($i,$j,$pix[$i][$j]);
            #printf("%03i ", $pix[$i][$j]);
            #if ( ($i % 16) == 0 ) { print "\n"; }
        }
    }
    print "Der er ialt ", $im->colorsTotal(), " farver\n";


    # Convert the image to PNG and save it.
    open FILE,'> C:\temp\heth.png';
    binmode FILE;
    print FILE $im->png;