/*Converts RGB pixel to YCbCr*/ private YCbCr convertRgbToYCbCr(RGB rgb) { YCbCr output = new YCbCr(); output.setY(16+(rgb.getRed() * 0.257 + rgb.getGreen() * 0.504 + rgb.getBlue() * 0.098)); output.setCb(128+(rgb.getRed() * -0.148 + rgb.getGreen() * -0.291 + rgb.getBlue() * 0.439)); output.setCr(128+(rgb.getRed() * 0.439 + rgb.getGreen() * -0.368 + rgb.getBlue() * -0.071)); 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; }
/*convert YCbCr values to a RGB bitmap*/ private Bitmap generateRgbBitmapFromYCbCr(double[,] Y, double[,] Cb, double[,] Cr) { int width = Y.GetLength(0); int height = Y.GetLength(1); Bitmap bitmap = new Bitmap(width, height); Color color; RGB rgb = new RGB(); for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (x / 2 >= Cb.GetLength(0) || y / 2 >= Cb.GetLength(1)) continue; rgb = convertYCbCrToRgb(Y[x,y], Cb[x/2,y/2], Cr[x/2,y/2]); color = Color.FromArgb(rgb.getRed(),rgb.getGreen(),rgb.getBlue()); bitmap.SetPixel(x, y, color); } } return bitmap; }
//------------------------------------------------------------------------------------------------------------------ /** Old code for saving to custom fileType. not useable anymore. **/ public void oldSavingStuff(double[,] Y) { int width = Y.GetLength(0); int height = Y.GetLength(1); YCbCr[,] ycbcrPixels = new YCbCr[width,height]; Bitmap testBitmap = new Bitmap(width, height); //testBitmap = generateRgbBitmap(ycbcrPixels); testBitmap.Save("SubsampledImage.bmp", ImageFormat.Bmp); byte[] bytesToSave = new byte[width * height * 3 + 8]; int byteOffset = 0; RGB rgb = new RGB(); byte[] widthBytes = BitConverter.GetBytes(width); byte[] heightBytes = BitConverter.GetBytes(height); if (BitConverter.IsLittleEndian) { Array.Reverse(widthBytes); Array.Reverse(heightBytes); } //Saving image width to beginning of byte array. for (int i = 0; i < widthBytes.Length; i++) { bytesToSave[byteOffset++] = widthBytes[i]; } //saving image Height to beginning of byte array. for (int i = 0; i < heightBytes.Length; i++) { bytesToSave[byteOffset++] = heightBytes[i]; } //Saving image data to byte array. for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { rgb = convertYCbCrToRgb(1,2,3); bytesToSave[y * width + x + byteOffset++] = (byte)rgb.getRed(); bytesToSave[y * width + x + byteOffset++] = (byte)rgb.getGreen(); bytesToSave[y * width + x + byteOffset] = (byte)rgb.getBlue(); //Debug.WriteLine(uncompressed.GetPixel(x,y)); //Debug.WriteLine(testBitmap.GetPixel(x,y)); } } File.WriteAllBytes("TestFile.cmpr", bytesToSave); }