Ejemplo n.º 1
0
 /// <summary>
 /// constructs a indexed icon image from a stream,
 /// not accessible outside the dll
 /// </summary>
 /// <param name="str">The string.</param>
 /// <param name="sz">The sz.</param>
 /// <param name="bpp">The BPP.</param>
 internal IconImageIndexed(Stream str, Size sz, short bpp)
 {
     using (SimpleReader rdr = new SimpleReader(str))
     {
         #region read palette
         int         colorcount = Quantizer.LengthOfPalette(bpp);
         ColorBgra[] palette    = new ColorBgra[colorcount];
         for (int i = 0; i < palette.Length; i++)
         {
             palette[i].A = 255;
             palette[i].B = rdr.ReadByte();
             palette[i].G = rdr.ReadByte();
             palette[i].R = rdr.ReadByte();
             rdr.ReadByte();                    //reserved
         }
         _octree = Octree.FromColorArray(palette);
         #endregion
         #region read indexed data
         //create bitmap and lock it
         _bitmap = new Bitmap(sz.Width, sz.Height, PixelFormat.Format32bppArgb);
         BitmapData bd = _bitmap.LockBits(
             new Rectangle(Point.Empty, _bitmap.Size),
             ImageLockMode.ReadOnly,
             PixelFormat.Format32bppArgb);
         ColorBgra *scan0 = (ColorBgra *)bd.Scan0;
         //read pixels
         int indexmask = (1 << bpp) - 1;
         scan0 += bd.Width * bd.Height;
         for (int y = 0; y < bd.Height; y++)
         {
             scan0 -= bd.Width;
             int indexpos = 8 - bpp,
                 data     = rdr.ReadInt32();
             for (int x = 0; x < bd.Width; x++)
             {
                 if (indexpos >= 32)
                 {
                     data      = rdr.ReadInt32();
                     indexpos -= 32;
                 }
                 scan0[x] = palette[indexmask & (data >> indexpos)];
                 if (indexpos % 8 == 0)
                 {
                     indexpos += 16 - bpp;
                 }
                 else
                 {
                     indexpos -= bpp;
                 }
             }
         }
         ANDMap.StampToBitmapData(rdr, bd);
         _bitmap.UnlockBits(bd);
         #endregion
     }
     _bitsperpixel = bpp;
 }
Ejemplo n.º 2
0
 /// <summary>
 /// constructs a 24bpp icon image from a stream,
 /// not accessible outside the dll
 /// </summary>
 /// <param name="str">The string.</param>
 /// <param name="sz">The sz.</param>
 internal IconImage24bpp(Stream str, Size sz)
 {
     #region read 24bpp data
     //create bitmap and lock it
     _bitmap = new Bitmap(sz.Width, sz.Height, PixelFormat.Format32bppArgb);
     BitmapData bd = _bitmap.LockBits(
         new Rectangle(Point.Empty, _bitmap.Size),
         ImageLockMode.ReadOnly,
         PixelFormat.Format32bppArgb);
     ColorBgra *scan0 = (ColorBgra *)bd.Scan0;
     //calculate padding at end of stride
     int padbytescount = (_bitmap.Width * 3) % 4;
     //read pixels, note: bottom-up, left-right
     ColorBgra color = ColorBgra.Black;
     using (SimpleReader rdr = new SimpleReader(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++)
             {
                 color.Blue  = rdr.ReadByte();
                 color.Green = rdr.ReadByte();
                 color.Red   = rdr.ReadByte();
                 scan0[x]    = color;
             }
             //pad to 32bit
             if (padbytescount != 0)
             {
                 rdr.ReadBytes(padbytescount);
             }
         }
         ANDMap.StampToBitmapData(rdr, bd);
     }
     _bitmap.UnlockBits(bd);
     #endregion
     _bitsperpixel = 24;
 }