Exemple #1
0
        public unsafe void Roundtrip_Bitmap_719x405()
        {
            int width  = 719;
            int height = 405;

            using (Bitmap bmp = new Bitmap($"img/testpattern_{width}x{height}.bmp"))
            {
                byte[] bgr = BitmapToBuffer(bmp, out int stride);

                byte[] i420 = PixelConverter.BGRtoI420(bgr, width, height, stride);

                Assert.NotNull(i420);

                byte[] rtBgr = PixelConverter.I420toBGR(i420, width, height, out int rtStride);

                Assert.NotNull(rtBgr);

                fixed(byte *pBgr = rtBgr)
                {
                    Bitmap rtBmp = new Bitmap(width, height, rtStride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, new IntPtr(pBgr));

                    rtBmp.Save($"roundtrip_bitmap_{width}x{height}.bmp");
                    rtBmp.Dispose();
                }
            }
        }
        public unsafe void RoundtripBgr24ToI420Test()
        {
            Bitmap bmp = new Bitmap("img/ref-bgr24.bmp");

            // BGR formats get recognised as RGB but when rendered by WPF are treated as BGR.
            Assert.Equal(PixelFormat.Format24bppRgb, bmp.PixelFormat);

            byte[] buffer = BitmapToBuffer(bmp);

            byte[] i420 = PixelConverter.BGRtoI420(buffer, bmp.Width, bmp.Height);
            byte[] bgr  = PixelConverter.I420toBGR(i420, bmp.Width, bmp.Height);

            fixed(byte *s = bgr)
            {
                Bitmap roundTripBmp = new Bitmap(bmp.Width, bmp.Height, (int)bmp.Width * 3, PixelFormat.Format24bppRgb, (IntPtr)s);

                roundTripBmp.Save("RoundtripBgr24ToI420Test.bmp");
                roundTripBmp.Dispose();
            }

            bmp.Dispose();
        }
Exemple #3
0
        private static byte[] BitmapToI420(Bitmap bmp)
        {
            var buffer = BitmapToByteArray(bmp, out int stride);

            return(PixelConverter.BGRtoI420(buffer, bmp.Width, bmp.Height, stride));
        }