Last active
August 29, 2015 14:25
-
-
Save cameronjacobson/2a8369a29ff311175e57 to your computer and use it in GitHub Desktop.
Convert YUY2 image format to RGB (.ppm) file via tcp connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 160x120 image pixel dimensions | |
// PPM Header | |
echo "P3\n"; | |
echo "160 120\n"; | |
echo "255\n"; | |
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); | |
$result = socket_connect($socket, '127.0.0.1', 5000); | |
for($rows=0;$rows<120;$rows++){ | |
$rowbuffer = array(); | |
for($cols=0;$cols<80;$cols++){ | |
$pixel = unpack('C*',socket_read($socket,4)); | |
$c = $pixel[1]; | |
$d = $pixel[0]; | |
$e = $pixel[2]-128; | |
$rgb1 = solve($c,$d,$e); | |
$c = $pixel[3]; | |
$d = $pixel[0]; | |
$e = $pixel[2]-128; | |
$rgb2 = solve($c,$d,$e); | |
$rowbuffer[] = $rgb1; | |
$rowbuffer[] = $rgb2; | |
} | |
echo implode(' ',$rowbuffer); | |
echo "\n"; | |
} | |
socket_close($socket); | |
function solve($c,$d,$e){ | |
$r = max(0,min(255,((298*$c+409*$e+128) >> 8))); | |
$g = max(0,min(255,((298*$c-100*$d-208*$e+128) >> 8))); | |
$b = max(0,min(255,((298*$c+516*$d+128) >> 8))); | |
return $r.' '.$g.' '.$b; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Still not getting perfect color representation. Need to look for better conversion formula?