package pl.wroc.pwr.image.converter; import pl.wroc.pwr.IOperator1P; import pl.wroc.pwr.image.IRGBImage; import pl.wroc.pwr.image.IYUVImage; import pl.wroc.pwr.image.model.YUVImage; /** * From: http://en.kioskea.net/contents/video/yuv-ycrcb.php3 * * Y = 0.299R + 0.587 G + 0.114 B * U = -0.147R - 0.289 G + 0.436B = 0.492(B - Y) * V = 0.615R -0.515G -0.100B = 0.877(R-Y) * * @author Mariusz Paradowski */ public class RGBtoYUVConverter implements IOperator1P { public IYUVImage apply(IRGBImage inputImage) { IYUVImage outputImage = new YUVImage(inputImage.getSize()); for ( int i = 0; i < inputImage.getRedChannel( ).getMaxIndex( ); i++ ) { float R = inputImage.getRedChannel().getValue(i); float G = inputImage.getGreenChannel().getValue(i); float B = inputImage.getBlueChannel().getValue(i); float Y = 0.299f*R + 0.587f*G + 0.114f*B; float U = 0.492f*(B-Y); float V = 0.877f*(R-Y); outputImage.getLuminanceYChannel().setValue(i, Y); outputImage.getChrominanceUChannel().setValue(i, U); outputImage.getChrominanceVChannel().setValue(i, V); } return outputImage; } }