public static void saveCompressedPFrame(int[] vectors, int[] data, int width, int height) { byte[] bytesToSave = new byte[data.Length + 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]; } for (int i = 0; i < data.Length; i++) { sbyte currentData = (sbyte)data[i]; bytesToSave[i + byteOffset] = (byte)currentData; } File.WriteAllBytes("TestFile.cmpr", bytesToSave); //int[] fileread = openCompressed("TestFile.cmpr"); //for (int i = 0; i < 10; i++) // { //Debug.Write(data[i]+"="+fileread[i]+","); //} }
/*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 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; }
/*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; }
/*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 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; }
//------------------------------------------------------------------------------------------------------------------ /** 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); }