/// <summary> /// applies an AND mask found in the stream to the specified bitmap /// </summary> public static void StampToBitmapData(SimpleReader rdr, BitmapData bd) { 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 = rdr.ReadInt32(); for (int x = 0; x < bd.Width; x++) { if (maskpos >= 32) //read next value { maskdata = rdr.ReadInt32(); maskpos -= 32; } if ((maskdata & (1 << maskpos)) != 0) //pixel is transparent { scan0[x] = 0; } //every byte holds 8 pixels, its highest order bit //representing the leftmost pixel of those if (maskpos % 8 == 0) { maskpos += 15; } else { maskpos--; } } } }
/// <summary> /// constructs a 32bpp icon image from a stream, /// not accessible outside the dll /// </summary> internal IconImage32bpp(Stream str, Size sz) { #region read 32bpp 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.ReadWrite, PixelFormat.Format32bppArgb); int *scan0 = (int *)bd.Scan0; //read pixels, note: bottom-up, left-right 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++) { scan0[x] = rdr.ReadInt32(); } } ANDMap.StampToBitmapData(rdr, bd); } _bitmap.UnlockBits(bd); #endregion _bitsperpixel = 32; }
/// <summary> /// constructs a indexed icon image from a stream, /// not accessible outside the dll /// </summary> 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; }
/// <summary> /// constructs a 24bpp icon image from a stream, /// not accessible outside the dll /// </summary> 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; }