Пример #1
0
        /// <summary>
        /// writes the image to the specified stream
        /// </summary>
        /// <param name="str">The string.</param>
        /// <exception cref="System.ArgumentNullException">str</exception>
        public override void Write(Stream str)
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }
            //write header
            BITMAPINFOHEADER header = new BITMAPINFOHEADER(
                _bitmap.Size,
                _bitsperpixel,
                this.SizeInBytes,
                (1 << _bitsperpixel) & 0xFF);

            header.Write(str);
            using (SimpleWriter wrt = new SimpleWriter(str))
            {
                #region write palette
                foreach (ColorBgra value in _octree.Table)
                {
                    wrt.Write(value.B);
                    wrt.Write(value.G);
                    wrt.Write(value.R);
                    wrt.Write((byte)0);                    //reserved
                }
                #endregion
                #region write indexed data
                //write bitmap
                BitmapData bd = _bitmap.LockBits(
                    new Rectangle(Point.Empty, _bitmap.Size),
                    ImageLockMode.ReadWrite,
                    PixelFormat.Format32bppArgb);
                ColorBgra *scan0 = (ColorBgra *)bd.Scan0;
                //write indexed data
                int indexmask = (1 << _bitsperpixel) - 1,
                    indexdata = 0;
                scan0 += bd.Width * bd.Height;
                for (int y = 0; y < bd.Height; y++)
                {
                    scan0 -= bd.Width;
                    int indexpos = 8 - _bitsperpixel;
                    indexdata = 0;
                    for (int x = 0; x < bd.Width; x++)
                    {
                        indexdata |=
                            (_octree.GetOctreeIndex(scan0[x]) & indexmask) << indexpos;
                        if (indexpos % 8 == 0)
                        {
                            indexpos += (16 - _bitsperpixel);
                        }
                        else
                        {
                            indexpos -= _bitsperpixel;
                        }
                        if (indexpos >= 32)
                        {
                            wrt.Write(indexdata);
                            indexdata = 0;
                            indexpos -= 32;
                        }
                    }
                    if (indexpos != (8 - _bitsperpixel))
                    {
                        wrt.Write(indexdata);
                    }
                }
                ANDMap.WriteToStream(bd, wrt);
                #endregion
                _bitmap.UnlockBits(bd);
            }
        }