コード例 #1
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();

            // !!!}}
        }