Example #1
0
        private void ProcessStream()
        {
            bool blockY0HasAcComponents = false;
            bool blockY1HasAcComponents = false;
            bool blockY2HasAcComponents = false;
            bool blockY3HasAcComponents = false;
            bool blockCbHasAcComponents = false;
            bool blockCrHasAcComponents = false;

            //Set StreamFieldBitIndex to 32 to make sure that the first call to ReadStreamData
            //actually consumes data from the stream
            StreamFieldBitIndex = 32;
            StreamField         = 0;
            StreamIndex         = 0;
            SliceIndex          = 0;
            PictureComplete     = false;

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            while (!PictureComplete && StreamIndex < (ImageStream.Length >> 2))
            {
                ReadHeader();

                if (!PictureComplete)
                {
                    for (int count = 0; count < BlockCount; count++)
                    {
                        uint macroBlockEmpty = ReadStreamData(1);

                        if (macroBlockEmpty == 0)
                        {
                            uint acCoefficients = ReadStreamData(8);

                            blockY0HasAcComponents = (acCoefficients >> 0 & 1) == 1;
                            blockY1HasAcComponents = (acCoefficients >> 1 & 1) == 1;
                            blockY2HasAcComponents = (acCoefficients >> 2 & 1) == 1;
                            blockY3HasAcComponents = (acCoefficients >> 3 & 1) == 1;
                            blockCbHasAcComponents = (acCoefficients >> 4 & 1) == 1;
                            blockCrHasAcComponents = (acCoefficients >> 5 & 1) == 1;

                            if ((acCoefficients >> 6 & 1) == 1)
                            {
                                uint quantizerMode = ReadStreamData(2);
                                QuantizerMode = (int)((quantizerMode < 2) ? ~quantizerMode : quantizerMode);
                            }

                            #region Block Y0

                            GetBlockBytes(blockY0HasAcComponents);
                            InverseTransform(count, 0);

                            #endregion

                            #region Block Y1

                            GetBlockBytes(blockY1HasAcComponents);
                            InverseTransform(count, 1);

                            #endregion

                            #region Block Y2

                            GetBlockBytes(blockY2HasAcComponents);
                            InverseTransform(count, 2);

                            #endregion

                            #region Block Y3

                            GetBlockBytes(blockY3HasAcComponents);
                            InverseTransform(count, 3);

                            #endregion

                            #region Block Cb

                            GetBlockBytes(blockCbHasAcComponents);
                            InverseTransform(count, 4);

                            #endregion

                            #region Block Cr

                            GetBlockBytes(blockCrHasAcComponents);
                            InverseTransform(count, 5);

                            #endregion
                        }
                    }

                    ComposeImageSlice();
                }
            }


            uint[] PxData = new uint[PixelData.Length];
            for (int i = 0; i < PixelData.Length; i++)
            {
                PxData[i] = rgb565_to_premult_argb(PixelData[i]);
            }

            var data = InternalImageSource.LockBits(new Rectangle(0, 0, InternalImageSource.Width, InternalImageSource.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            unsafe {
                uint *dst = (uint *)data.Scan0;
                fixed(uint *src = PxData)
                {
                    for (int i = 0; i < PxData.Length; i++)
                    {
                        *(dst + i) = *(src + i);
                    }
                }
            }

            if (ImageComplete != null)
            {
                ImageComplete(this, new ImageCompleteEventArgs(ImageSource));
            }
        }
        private void ProcessStream()
        {
            bool blockY0HasAcComponents = false;
            bool blockY1HasAcComponents = false;
            bool blockY2HasAcComponents = false;
            bool blockY3HasAcComponents = false;
            bool blockCbHasAcComponents = false;
            bool blockCrHasAcComponents = false;

            //Set StreamFieldBitIndex to 32 to make sure that the first call to ReadStreamData
            //actually consumes data from the stream
            StreamFieldBitIndex = 32;
            StreamField         = 0;
            StreamIndex         = 0;
            SliceIndex          = 0;
            PictureComplete     = false;

            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            while (!PictureComplete && StreamIndex < (ImageStream.Length >> 2))
            {
                ReadHeader();

                if (!PictureComplete)
                {
                    for (int count = 0; count < BlockCount; count++)
                    {
                        uint macroBlockEmpty = ReadStreamData(1);

                        if (macroBlockEmpty == 0)
                        {
                            uint acCoefficients = ReadStreamData(8);

                            blockY0HasAcComponents = (acCoefficients >> 0 & 1) == 1;
                            blockY1HasAcComponents = (acCoefficients >> 1 & 1) == 1;
                            blockY2HasAcComponents = (acCoefficients >> 2 & 1) == 1;
                            blockY3HasAcComponents = (acCoefficients >> 3 & 1) == 1;
                            blockCbHasAcComponents = (acCoefficients >> 4 & 1) == 1;
                            blockCrHasAcComponents = (acCoefficients >> 5 & 1) == 1;

                            if ((acCoefficients >> 6 & 1) == 1)
                            {
                                uint quantizerMode = ReadStreamData(2);
                                QuantizerMode = (int)((quantizerMode < 2) ? ~quantizerMode : quantizerMode);
                            }

                            #region Block Y0

                            GetBlockBytes(blockY0HasAcComponents);
                            InverseTransform(count, 0);

                            #endregion

                            #region Block Y1

                            GetBlockBytes(blockY1HasAcComponents);
                            InverseTransform(count, 1);

                            #endregion

                            #region Block Y2

                            GetBlockBytes(blockY2HasAcComponents);
                            InverseTransform(count, 2);

                            #endregion

                            #region Block Y3

                            GetBlockBytes(blockY3HasAcComponents);
                            InverseTransform(count, 3);

                            #endregion

                            #region Block Cb

                            GetBlockBytes(blockCbHasAcComponents);
                            InverseTransform(count, 4);

                            #endregion

                            #region Block Cr

                            GetBlockBytes(blockCrHasAcComponents);
                            InverseTransform(count, 5);

                            #endregion
                        }
                    }

                    ComposeImageSlice();
                }
            }


            uint[] PxData = new uint[PixelData.Length];
            for (int i = 0; i < PixelData.Length; i++)
            {
                PxData[i] = rgb565_to_premult_argb(PixelData[i]);
            }

            Array.Copy(PxData, 0, InternalImageSource.Pixels, 0, PxData.Length);
            InternalImageSource.Invalidate();

            if (ImageComplete != null)
            {
                ImageComplete(this, new ImageCompleteEventArgs(ImageSource));
            }
        }