Refactored the hardware monitoring code into a library (Issue 101).
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2009-2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
41 using System.Drawing.Imaging;
45 namespace OpenHardwareMonitor.Utilities {
46 public class IconFactory {
48 private struct BITMAPINFOHEADER {
53 public ushort BitCount;
54 public uint Compression;
55 public uint SizeImage;
56 public int XPelsPerMeter;
57 public int YPelsPerMeter;
59 public uint ClrImportant;
61 public BITMAPINFOHEADER(int width, int height, int bitCount) {
66 this.BitCount = (ushort)bitCount;
69 this.XPelsPerMeter = 0;
70 this.YPelsPerMeter = 0;
72 this.ClrImportant = 0;
75 public void Write(BinaryWriter bw) {
81 bw.Write(Compression);
83 bw.Write(XPelsPerMeter);
84 bw.Write(YPelsPerMeter);
86 bw.Write(ClrImportant);
90 private struct ICONIMAGE {
91 public BITMAPINFOHEADER Header;
96 public ICONIMAGE(int width, int height, byte[] colors) {
97 this.Header = new BITMAPINFOHEADER(width, height << 1,
98 (8 * colors.Length) / (width * height));
100 int maskSize = (width * height) >> 3;
101 this.XOR = new byte[maskSize];
102 this.AND = new byte[maskSize];
105 public void Write(BinaryWriter bw) {
107 int stride = Header.Width << 2;
108 for (int i = (Header.Height >> 1) - 1; i >= 0; i--)
109 bw.Write(Colors, i * stride, stride);
115 private struct ICONDIRENTRY {
118 public byte ColorCount;
119 public byte Reserved;
120 public ushort Planes;
121 public ushort BitCount;
122 public uint BytesInRes;
123 public uint ImageOffset;
125 public ICONDIRENTRY(ICONIMAGE image, int imageOffset) {
126 this.Width = (byte)image.Header.Width;
127 this.Height = (byte)(image.Header.Height >> 1);
130 this.Planes = image.Header.Planes;
131 this.BitCount = image.Header.BitCount;
132 this.BytesInRes = (uint)(image.Header.Size +
133 image.Colors.Length + image.XOR.Length + image.AND.Length);
134 this.ImageOffset = (uint)imageOffset;
137 public void Write(BinaryWriter bw) {
140 bw.Write(ColorCount);
144 bw.Write(BytesInRes);
145 bw.Write(ImageOffset);
153 private struct ICONDIR {
154 public ushort Reserved;
157 public ICONDIRENTRY[] Entries;
159 public ICONDIR(ICONDIRENTRY[] entries) {
162 this.Count = (ushort)entries.Length;
163 this.Entries = entries;
166 public void Write(BinaryWriter bw) {
170 for (int i = 0; i < Entries.Length; i++)
171 Entries[i].Write(bw);
175 get { return (uint)(6 + Entries.Length *
176 (Entries.Length > 0 ? Entries[0].Size : 0)); }
180 public static Icon Create(byte[] colors, int width, int height,
181 PixelFormat format) {
182 if (format != PixelFormat.Format32bppArgb)
183 throw new NotImplementedException();
185 ICONIMAGE image = new ICONIMAGE(width, height, colors);
186 ICONDIR dir = new ICONDIR(
187 new ICONDIRENTRY[] { new ICONDIRENTRY(image, 0) } );
188 dir.Entries[0].ImageOffset = dir.Size;
191 using (BinaryWriter bw = new BinaryWriter(new MemoryStream())) {
195 bw.BaseStream.Position = 0;
196 icon = new Icon(bw.BaseStream);