/*Converts a RGB bitmap to YCbCr*/ private void generateYcbcrBitmap(Bitmap uncompressed, ref double[,] Y, ref double[,] Cb, ref double[,] Cr) { YCbCr[,] ycbcrPixels = new YCbCr[uncompressed.Width, uncompressed.Height]; Color pixel; RGB rgb = new RGB(); for (int y = 0; y < ycbcrPixels.GetLength(1); y++) { for (int x = 0; x < ycbcrPixels.GetLength(0); x++) { if (x / 2 >= Cb.GetLength(0) || y / 2 >= Cb.GetLength(1)) continue; pixel = uncompressed.GetPixel(x, y); rgb.setRed(pixel.R); rgb.setGreen(pixel.G); rgb.setBlue(pixel.B); ycbcrPixels[x, y] = convertRgbToYCbCr(rgb); Y[x, y] = ycbcrPixels[x, y].getY(); Cb[x/2,y/2] = ycbcrPixels[x, y].getCb(); Cr[x/2, y/2] = ycbcrPixels[x, y].getCr(); } } }
/*Converts YCbCr to RGB*/ private RGB convertYCbCrToRgb(double curY, double curCb, double curCr) { RGB output = new RGB(); double red = (curY - 16) * 1.164 + (curCb - 128)*0 + (curCr - 128)*1.596; output.setRed((int)Math.Round(red)); double green = (curY - 16) * 1.164 + (curCb - 128) * -0.392 + (curCr - 128) * -0.813; output.setGreen((int)green); double blue = (curY - 16) * 1.164 + (curCb - 128) * 2.017 + (curCr - 128) * 0; output.setBlue((int)blue); return output; }
/*Converts custom cmpr filetype to bitmap object*/ private Bitmap convertFileToBitmap(string filename) { byte[] fileByteArray = File.ReadAllBytes(filename); int byteOffset = 0; byte[] widthBytes = new byte[4]; byte[] heightBytes = new byte[4]; for (int i=0; i<4; i++) { widthBytes[i] = fileByteArray[i]; byteOffset++; heightBytes[i] = fileByteArray[i + 4]; byteOffset++; } if (BitConverter.IsLittleEndian) { Array.Reverse(widthBytes); Array.Reverse(heightBytes); } int width = BitConverter.ToInt32(widthBytes, 0); Console.WriteLine("width: {0}", width); int height = BitConverter.ToInt32(heightBytes, 0); Console.WriteLine("height: {0}", height); Bitmap compressedImage = new Bitmap(width, height); RGB rgb = new RGB(); for (int y=0; y<height; y++) { for (int x = 0; x < width; x++) { rgb.setRed(fileByteArray[byteOffset++]); rgb.setGreen(fileByteArray[byteOffset++]); rgb.setBlue(fileByteArray[byteOffset++]); compressedImage.SetPixel(x, y, Color.FromArgb(rgb.getRed(), rgb.getGreen(), rgb.getBlue())); } } return compressedImage; }