示例#1
0
        public Bitmap DecodeImage(Stream inps)
        {
            if (inps == null)
            {
                return(null);
            }

            // !!!{{ TODO: add the decoding code here

            IEntropyCodec c = new DeflateCodec();

            c.BinaryStream = inps;
            c.Open(false);

            uint magic = (uint)c.GetBits(32);

            if (magic != MAGIC)
            {
                return(null);
            }

            int width, height;

            width  = (int)c.GetBits(16);
            height = (int)c.GetBits(16);

            if (width < 1 || height < 1)
            {
                return(null);
            }

            Bitmap result = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int gr = c.Get();
                    if (gr < 0)
                    {
                        goto fin;
                    }
                    result.SetPixel(x, y, Color.FromArgb(gr, gr, gr));
                }
            }

fin:
            c.Close();

            return(result);

            // !!!}}
        }
示例#2
0
        public void EncodeImage(Bitmap inp, Stream outs)
        {
            if (inp == null ||
                outs == null)
            {
                return;
            }

            int width  = inp.Width;
            int height = inp.Height;

            if (width < 1 || height < 1)
            {
                return;
            }

            // !!!{{ TODO: add the encoding code here

            IEntropyCodec c = new DeflateCodec();

            c.MaxSymbol = 255;
            if (c.MaxSymbol < 255)
            {
                throw new Exception("Unappropriate codec used (alphabet too small)!");
            }

            c.BinaryStream = outs;
            c.Open(true);

            // file header: [ MAGIC, width, height ]
            c.PutBits(MAGIC, 32);
            c.PutBits(width, 16);
            c.PutBits(height, 16);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    Color col = inp.GetPixel(x, y);
                    int   gr  = Draw.RgbToGray(col.R, col.G, col.B);
                    c.Put(gr & 0xff);
                }
            }

            c.Close();

            // !!!}}
        }
示例#3
0
        public IEntropyCodec EncodeHeader(int width, int height, float fps, Stream outs)
        {
            frameWidth      = width;
            frameHeight     = height;
            framesPerSecond = fps;

            if (outs == null)
            {
                return(null);
            }

            IEntropyCodec c = new DeflateCodec();

            c.MaxSymbol = 255;
            if (c.MaxSymbol < 255)
            {
                throw new Exception("Unappropriate codec used (alphabet too small)!");
            }

            c.BinaryStream = outs;
            c.Open(true);

            // !!!{{ TODO: add the header construction/encoding here

            // video header: [ MAGIC, width, height, fps ]
            c.PutBits(MAGIC, 32);
            c.PutBits(width, 16);
            c.PutBits(height, 16);

            int fpsInt = (int)(100.0f * fps);

            c.PutBits(fpsInt, 16);

            // !!!}}

            return(c);
        }
示例#4
0
        public IEntropyCodec DecodeHeader(Stream inps)
        {
            if (inps == null)
            {
                return(null);
            }

            IEntropyCodec c = new DeflateCodec();

            c.BinaryStream = inps;
            c.Open(false);

            // !!!{{ TODO: add the header decoding here

            // Check the global header:
            uint magic = (uint)c.GetBits(32);

            if (magic != MAGIC)
            {
                return(null);
            }

            frameWidth  = (int)c.GetBits(16);
            frameHeight = (int)c.GetBits(16);

            if (frameWidth < 1 || frameHeight < 1)
            {
                return(null);
            }

            framesPerSecond = (int)c.GetBits(16);

            // !!!}}

            return(c);
        }