moel@40: /* moel@40: moel@344: This Source Code Form is subject to the terms of the Mozilla Public moel@344: License, v. 2.0. If a copy of the MPL was not distributed with this moel@344: file, You can obtain one at http://mozilla.org/MPL/2.0/. moel@40: moel@384: Copyright (C) 2009-2012 Michael Möller moel@344: moel@40: */ moel@40: moel@40: using System; moel@40: using System.Collections.Generic; moel@40: using System.Drawing; moel@40: using System.Drawing.Imaging; moel@40: using System.IO; moel@40: using System.Text; moel@40: moel@40: namespace OpenHardwareMonitor.Utilities { moel@40: public class IconFactory { moel@40: moel@40: private struct BITMAPINFOHEADER { moel@40: public uint Size; moel@40: public int Width; moel@40: public int Height; moel@40: public ushort Planes; moel@40: public ushort BitCount; moel@40: public uint Compression; moel@40: public uint SizeImage; moel@40: public int XPelsPerMeter; moel@40: public int YPelsPerMeter; moel@40: public uint ClrUsed; moel@40: public uint ClrImportant; moel@40: moel@40: public BITMAPINFOHEADER(int width, int height, int bitCount) { moel@40: this.Size = 40; moel@40: this.Width = width; moel@40: this.Height = height; moel@40: this.Planes = 1; moel@40: this.BitCount = (ushort)bitCount; moel@40: this.Compression = 0; moel@40: this.SizeImage = 0; moel@40: this.XPelsPerMeter = 0; moel@40: this.YPelsPerMeter = 0; moel@40: this.ClrUsed = 0; moel@40: this.ClrImportant = 0; moel@40: } moel@40: moel@40: public void Write(BinaryWriter bw) { moel@40: bw.Write(Size); moel@40: bw.Write(Width); moel@40: bw.Write(Height); moel@40: bw.Write(Planes); moel@40: bw.Write(BitCount); moel@40: bw.Write(Compression); moel@40: bw.Write(SizeImage); moel@40: bw.Write(XPelsPerMeter); moel@40: bw.Write(YPelsPerMeter); moel@40: bw.Write(ClrUsed); moel@40: bw.Write(ClrImportant); moel@40: } moel@40: } moel@40: moel@40: private struct ICONIMAGE { moel@40: public BITMAPINFOHEADER Header; moel@40: public byte[] Colors; moel@384: public int MaskSize; moel@40: moel@40: public ICONIMAGE(int width, int height, byte[] colors) { moel@40: this.Header = new BITMAPINFOHEADER(width, height << 1, moel@40: (8 * colors.Length) / (width * height)); moel@40: this.Colors = colors; moel@384: MaskSize = (width * height) >> 3; moel@40: } moel@40: moel@40: public void Write(BinaryWriter bw) { moel@40: Header.Write(bw); moel@40: int stride = Header.Width << 2; moel@40: for (int i = (Header.Height >> 1) - 1; i >= 0; i--) moel@40: bw.Write(Colors, i * stride, stride); moel@384: for (int i = 0; i < 2 * MaskSize; i++) moel@384: bw.Write((byte)0); moel@40: } moel@40: } moel@40: moel@40: private struct ICONDIRENTRY { moel@40: public byte Width; moel@40: public byte Height; moel@40: public byte ColorCount; moel@40: public byte Reserved; moel@40: public ushort Planes; moel@40: public ushort BitCount; moel@40: public uint BytesInRes; moel@40: public uint ImageOffset; moel@40: moel@40: public ICONDIRENTRY(ICONIMAGE image, int imageOffset) { moel@40: this.Width = (byte)image.Header.Width; moel@40: this.Height = (byte)(image.Header.Height >> 1); moel@40: this.ColorCount = 0; moel@40: this.Reserved = 0; moel@40: this.Planes = image.Header.Planes; moel@40: this.BitCount = image.Header.BitCount; moel@40: this.BytesInRes = (uint)(image.Header.Size + moel@384: image.Colors.Length + image.MaskSize + image.MaskSize); moel@40: this.ImageOffset = (uint)imageOffset; moel@40: } moel@40: moel@40: public void Write(BinaryWriter bw) { moel@40: bw.Write(Width); moel@40: bw.Write(Height); moel@40: bw.Write(ColorCount); moel@40: bw.Write(Reserved); moel@40: bw.Write(Planes); moel@40: bw.Write(BitCount); moel@40: bw.Write(BytesInRes); moel@40: bw.Write(ImageOffset); moel@40: } moel@40: moel@40: public uint Size { moel@40: get { return 16; } moel@40: } moel@40: } moel@40: moel@40: private struct ICONDIR { moel@40: public ushort Reserved; moel@40: public ushort Type; moel@40: public ushort Count; moel@40: public ICONDIRENTRY[] Entries; moel@40: moel@40: public ICONDIR(ICONDIRENTRY[] entries) { moel@40: this.Reserved = 0; moel@40: this.Type = 1; moel@40: this.Count = (ushort)entries.Length; moel@40: this.Entries = entries; moel@40: } moel@40: moel@40: public void Write(BinaryWriter bw) { moel@40: bw.Write(Reserved); moel@40: bw.Write(Type); moel@40: bw.Write(Count); moel@40: for (int i = 0; i < Entries.Length; i++) moel@40: Entries[i].Write(bw); moel@40: } moel@40: moel@40: public uint Size { moel@40: get { return (uint)(6 + Entries.Length * moel@40: (Entries.Length > 0 ? Entries[0].Size : 0)); } moel@40: } moel@40: } moel@384: moel@384: private static BinaryWriter binaryWriter = moel@384: new BinaryWriter(new MemoryStream()); moel@40: moel@40: public static Icon Create(byte[] colors, int width, int height, moel@40: PixelFormat format) { moel@40: if (format != PixelFormat.Format32bppArgb) moel@40: throw new NotImplementedException(); moel@40: moel@40: ICONIMAGE image = new ICONIMAGE(width, height, colors); moel@40: ICONDIR dir = new ICONDIR( moel@40: new ICONDIRENTRY[] { new ICONDIRENTRY(image, 0) } ); moel@40: dir.Entries[0].ImageOffset = dir.Size; moel@40: moel@40: Icon icon; moel@384: binaryWriter.BaseStream.Position = 0; moel@384: dir.Write(binaryWriter); moel@384: image.Write(binaryWriter); moel@40: moel@384: binaryWriter.BaseStream.Position = 0; moel@384: icon = new Icon(binaryWriter.BaseStream); moel@40: moel@40: return icon; moel@40: } moel@40: moel@40: } moel@40: }