示例#1
0
        /// <summary>
        /// writes the image to the specified stream
        /// </summary>
        public override void Write(Stream str)
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }
            //write header
            BITMAPINFOHEADER header = new BITMAPINFOHEADER(
                _bitmap.Size,
                24,
                this.SizeInBytes,
                0);

            header.Write(str);
            #region write 24bpp data
            //write bitmap
            BitmapData bd = _bitmap.LockBits(
                new Rectangle(Point.Empty, _bitmap.Size),
                ImageLockMode.ReadWrite,
                PixelFormat.Format32bppArgb);
            ColorBgra *scan0 = (ColorBgra *)bd.Scan0;
            //calculate padding at end of stride
            int    padbytescount = (_bitmap.Width * 3) % 4;
            byte[] padbytes      = null;
            if (padbytescount != 0)
            {
                padbytes = new byte[padbytescount];
            }
            //write pixels, note: bottom-up, left-right
            using (SimpleWriter wrt = new SimpleWriter(str))
            {
                scan0 += bd.Width * bd.Height;
                for (int y = 0; y < bd.Height; y++)
                {
                    scan0 -= bd.Width;
                    for (int x = 0; x < bd.Width; x++)
                    {
                        wrt.Write(scan0[x].Blue);
                        wrt.Write(scan0[x].Green);
                        wrt.Write(scan0[x].Red);
                    }
                    //pad to 32bit
                    if (padbytes != null)
                    {
                        wrt.Write(padbytes);
                    }
                }
                ANDMap.WriteToStream(bd, wrt);
            }
            _bitmap.UnlockBits(bd);
            #endregion
        }
示例#2
0
            /// <summary>
            /// writes an AND mask found in the bitmap to the specified stream
            /// </summary>
            public static void WriteToStream(BitmapData bd, SimpleWriter wrt)
            {
                int *scan0 = (int *)bd.Scan0;

                scan0 += bd.Width * bd.Height;
                for (int y = 0; y < bd.Height; y++)
                {
                    scan0 -= bd.Width;
                    int maskpos  = 7,
                        maskdata = 0;                      //no bits set, all transparent
                    for (int x = 0; x < bd.Width; x++)
                    {
                        if ((scan0[x] & 0xFF000000) == 0)                   //transparent
                        {
                            maskdata |= (1 << maskpos);
                        }

                        //every byte holds 8 pixels, its highest order bit
                        //representing the leftmost pixel of those
                        if (maskpos % 8 == 0)
                        {
                            maskpos += 15;
                        }
                        else
                        {
                            maskpos--;
                        }
                        if (maskpos >= 32)
                        {
                            wrt.Write(maskdata);
                            maskdata = 0;
                            maskpos -= 32;
                        }
                    }
                    if (maskpos != 7)
                    {
                        wrt.Write(maskdata);
                    }
                }
            }
示例#3
0
        /// <summary>
        /// writes the image to the specified stream
        /// </summary>
        public override void Write(Stream str)
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }
            //write header
            BITMAPINFOHEADER header = new BITMAPINFOHEADER(
                _bitmap.Size,
                32,
                this.SizeInBytes,
                0);

            header.Write(str);
            #region write 32bpp data
            //write bitmap
            BitmapData bd = _bitmap.LockBits(
                new Rectangle(Point.Empty, _bitmap.Size),
                ImageLockMode.ReadWrite,
                PixelFormat.Format32bppArgb);
            int *scan0 = (int *)bd.Scan0;
            //write pixels, note: bottom-up, left-right
            using (SimpleWriter wrt = new SimpleWriter(str))
            {
                scan0 += bd.Width * bd.Height;
                for (int y = 0; y < bd.Height; y++)
                {
                    scan0 -= bd.Width;
                    for (int x = 0; x < bd.Width; x++)
                    {
                        wrt.Write(scan0[x]);
                    }
                }
                ANDMap.WriteToStream(bd, wrt);
            }
            _bitmap.UnlockBits(bd);
            #endregion
        }
示例#4
0
        /// <summary>
        /// writes the image to the specified stream
        /// </summary>
        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);
            }
        }