public Bitmap RestoreCbCr(DownsampleFormat df)
        {
            Bitmap result = new Bitmap(df.bmpWidth, df.bmpHeight);

            byte[] data = new byte[df.bmpHeight * df.bmpStride];

            int  check = 0;
            int  checkLine = 0;
            int  cb = df.Cblen;
            int  cr = df.Crlen;
            byte c1, c0;

            int j = 0;

            for (int i = 0; i < data.Length; i += 3)
            {
                if (check == 2 || check == 3)
                {
                    c1 = 0;
                    c0 = 0;
                    if (check == 3)
                    {
                        check = 0;
                    }
                }
                else
                {
                    c1 = df.data[j + df.Cblen];
                    c0 = df.data[j + df.Cblen + df.Crlen];
                }

                data[i + 2] = df.data[j];
                data[i + 1] = c1;
                data[i]     = c0;
                check++;
                checkLine += 3;
                j++;
                if (checkLine == df.bmpWidth * 2)
                {
                    if (check != 0)
                    {
                        check = 3;
                    }
                    else
                    {
                        check = 0;
                    }
                    checkLine = 0;
                }
            }

            BitmapData bmData = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            Marshal.Copy(data, 0, bmData.Scan0, data.Length);

            result.UnlockBits(bmData);
            return(result);
        }
 public Bitmap RestoreBitmap(DownsampleFormat df)
 {
     if (df.code == 1)
     {
         return(RestoreCbCr(df));
     }
     else if (df.code == 2)
     {
         return(RestoreYCr(df));
     }
     else
     {
         return(RestoreYCb(df));
     }
 }
        public DownsampleFormat Downsample(int code)
        {
            // BGR
            byte[] YTemp;
            byte[] CbTemp;
            byte[] CrTemp;

            if (code == 1)     // downsample Cb, Cr
            {
                YTemp  = this.YData;
                CbTemp = DownsampleChannel(this.CbData);
                CrTemp = DownsampleChannel(this.CrData);
            }
            else if (code == 2)
            {
                YTemp  = DownsampleChannel(this.YData);
                CbTemp = this.CbData;
                CrTemp = DownsampleChannel(this.CrData);
            }
            else
            {
                YTemp  = DownsampleChannel(this.YData);
                CbTemp = DownsampleChannel(this.CbData);
                CrTemp = this.CrData;
            }


            int Ylen  = YTemp.Length;
            int Cblen = CbTemp.Length;
            int Crlen = CrTemp.Length;

            DownsampleFormat result = new DownsampleFormat(this.bmpStride, this.bmpWidth, this.bmpHeight, Ylen, Cblen, Crlen, code);

            result.data = new byte[Ylen + Cblen + Crlen];
            Array.Copy(YTemp, 0, result.data, 0, Ylen);
            Array.Copy(CbTemp, 0, result.data, Ylen, Cblen);
            Array.Copy(CrTemp, 0, result.data, Ylen + Cblen, Crlen);
            return(result);
        }