|
#!/usr/bin/perl
sub round { sprintf("%.$_[1]f", $_[0]);}
sub rgb2ycbcr {
my ($r,$g,$b,$y,$cb,$cr)=@_;
$y = ( 306*$r+601*$g+117*$b)/1024;
$cb=((-173*$r-339*$g+512*$b)/1024)+128;
$cr=(( 512*$r-429*$g- 83*$b)/1024)+128;
if($y <0){$y =0;} if($y >255){$y =255;}
if($cb<0){$cb=0;} if($cb>255){$cb=255;}
if($cr<0){$cr=0;} if($cr>255){$cr=255;}
(int $y,int $cb,int $cr);
}
sub ycbcr2rgb {
my ($y,$cb,$cr,$r,$g,$b)=@_;
$r=$y+(( $cr*1436)>>10)-179;
$g=$y-(($cb* 352+$cr* 731)>>10)+135;
$b=$y+(($cb*1815 )>>10)-227;
if($r<0){$r=0;} if($r>1023){$r=1023};
if($g<0){$g=0;} if($g>1023){$g=1023};
if($b<0){$b=0;} if($b>1023){$b=1023};
(int $r,int $g,int $b);
}
sub de2_rgb2ycbcr {
my ($r,$g,$b,$y,$cb,$cr)=@_;
$r=(($r*4)-1)*128+105555;
$g=(($g*4)-1)*128 -64218;
$b=(($b*4)-1)*128+131072;
$y =(( 306.0295*$r+600.9306*$g+117.0399*$b)/1024/551); #+ 16.0451;
$cb=((-176.5678*$r-346.7149*$g+523.2827*$b)/1024/551); #+127.9907;
$cr=(( 523.2827*$r-437.9799*$g- 85.3029*$b)/1024/551); #+127.9288;
if($y <0){$y =0;} if($y >255){$y =255;}
if($cb<0){$cb=0;} if($cb>255){$cb=255;}
if($cr<0){$cr=0;} if($cr>255){$cr=255;}
#(int $y,int $cb,int $cr);
(round($y,0),round($cb,0),round($cr,0));
}
sub de2_ycbcr2rgb {
my ($y,$cb,$cr,$r,$g,$b)=@_;
$r=((551*$y +756*$cr-105555)/128)+1;
$g=((551*$y-186*$cb-385*$cr+ 64218)/128)+1;
$b=((551*$y+955*$cb -131072)/128)+1;
$r/=4; if($r<0){$r=0;} if($r>255){$r=255};
$g/=4; if($g<0){$g=0;} if($g>255){$g=255};
$b/=4; if($b<0){$b=0;} if($b>255){$b=255};
#($r,$g,$b);
#(round($r,0),round($g,0),round($b,0));
(int $r,int $g,int $b);
}
for(my $r=0;$r<256;$r+=15) {
for(my $g=0;$g<256;$g+=15) {
for(my $b=0;$b<256;$b+=15) {
my($y,$cb,$cr) =de2_rgb2ycbcr($r,$g,$b);
($r,$g,$b) =de2_ycbcr2rgb($y,$cb,$cr);
($y,$cb,$cr) =de2_rgb2ycbcr($r,$g,$b);
my($r2,$g2,$b2) =de2_ycbcr2rgb($y,$cb,$cr);
if(($r!=$r2)||($g!=$g2)||($b!=$b2)) {
print "$r $g $b";
print "\t-> $y $cb $cr";
print "\t-> $r2 $g2 $b2";
($y,$cb,$cr)=de2_rgb2ycbcr($r2,$g2,$b2);
print "\t-> $y $cb $cr";
($r2,$g2,$b2)=de2_ycbcr2rgb($y,$cb,$cr);
print "\t-> $r2 $g2 $b2\n";
}
}}} |
|