private static void RoundTripNoEncodingTestPattern() { var testBmp = new Bitmap(TEST_PATTERN_IMAGE_PATH); int w = testBmp.Width; int h = testBmp.Height; var rgbToi420 = new VideoFrameConverter( testBmp.Size.Width, testBmp.Size.Height, AVPixelFormat.AV_PIX_FMT_RGB24, testBmp.Size.Width, testBmp.Size.Height, AVPixelFormat.AV_PIX_FMT_YUV420P); var i420Converter = new VideoFrameConverter( testBmp.Size.Width, testBmp.Size.Height, AVPixelFormat.AV_PIX_FMT_YUV420P, testBmp.Size.Width, testBmp.Size.Height, AVPixelFormat.AV_PIX_FMT_RGB24); BitmapData bmpData = testBmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); IntPtr bmpDataPtr = bmpData.Scan0; int stride = bmpData.Stride; byte[] bmpBuffer = new byte[Math.Abs(stride * h)]; Marshal.Copy(bmpDataPtr, bmpBuffer, 0, bmpBuffer.Length); testBmp.UnlockBits(bmpData); Console.WriteLine($"Test pattern stride {stride}."); // Convert bitmap to i420. var i420Buffer = rgbToi420.ConvertToBuffer(bmpBuffer); Console.WriteLine($"Converted rgb to i420 buffer, length {i420Buffer.Length}."); // Convert i420 back to bmp. var outRgb = i420Converter.ConvertToBuffer(i420Buffer); Console.WriteLine($"Converted i420 to rgb buffer, length {outRgb.Length}."); unsafe { fixed(byte *s = outRgb) { System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(w, h, outRgb.Length / h, PixelFormat.Format24bppRgb, (IntPtr)s); bmpImage.Save("testpattern-result.bmp"); bmpImage.Dispose(); } } }
private static void RoundTripNoEncodingDummyBitmap() { int width = 32; int height = 32; Size sz = new Size(width, height); var rgbToi420 = new VideoFrameConverter( width, height, AVPixelFormat.AV_PIX_FMT_RGB24, width, height, AVPixelFormat.AV_PIX_FMT_YUV420P); var i420Converter = new VideoFrameConverter( width, height, AVPixelFormat.AV_PIX_FMT_YUV420P, width, height, AVPixelFormat.AV_PIX_FMT_RGB24); // Create dummy bitmap. byte[] srcRgb = new byte[width * height * 3]; for (int row = 0; row < 32; row++) { for (int col = 0; col < 32; col++) { int index = row * width * 3 + col * 3; int red = (row < 16 && col < 16) ? 255 : 0; int green = (row < 16 && col > 16) ? 255 : 0; int blue = (row > 16 && col < 16) ? 255 : 0; srcRgb[index] = (byte)red; srcRgb[index + 1] = (byte)green; srcRgb[index + 2] = (byte)blue; } } unsafe { fixed(byte *src = srcRgb) { System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(width, height, srcRgb.Length / height, PixelFormat.Format24bppRgb, (IntPtr)src); bmpImage.Save("test-source.bmp"); bmpImage.Dispose(); } } // Convert bitmap to i420. var i420Buffer = rgbToi420.ConvertToBuffer(srcRgb); Console.WriteLine($"Converted rgb to i420 buffer, length {i420Buffer.Length}."); // Convert i420 back to bmp. var outRgb = i420Converter.ConvertToBuffer(i420Buffer); Console.WriteLine($"Converted i420 to rgb buffer, length {outRgb.Length}."); unsafe { fixed(byte *s = outRgb) { System.Drawing.Bitmap bmpImage = new System.Drawing.Bitmap(width, height, outRgb.Length / height, PixelFormat.Format24bppRgb, (IntPtr)s); bmpImage.Save("test-result.bmp"); bmpImage.Dispose(); } } }