Rebracer update.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
14 using System.Drawing.Imaging;
18 namespace OpenHardwareMonitor.Utilities {
19 public class IconFactory {
21 private struct BITMAPINFOHEADER {
26 public ushort BitCount;
27 public uint Compression;
28 public uint SizeImage;
29 public int XPelsPerMeter;
30 public int YPelsPerMeter;
32 public uint ClrImportant;
34 public BITMAPINFOHEADER(int width, int height, int bitCount) {
39 this.BitCount = (ushort)bitCount;
42 this.XPelsPerMeter = 0;
43 this.YPelsPerMeter = 0;
45 this.ClrImportant = 0;
48 public void Write(BinaryWriter bw) {
54 bw.Write(Compression);
56 bw.Write(XPelsPerMeter);
57 bw.Write(YPelsPerMeter);
59 bw.Write(ClrImportant);
63 private struct ICONIMAGE {
64 public BITMAPINFOHEADER Header;
68 public ICONIMAGE(int width, int height, byte[] colors) {
69 this.Header = new BITMAPINFOHEADER(width, height << 1,
70 (8 * colors.Length) / (width * height));
72 MaskSize = (width * height) >> 3;
75 public void Write(BinaryWriter bw) {
77 int stride = Header.Width << 2;
78 for (int i = (Header.Height >> 1) - 1; i >= 0; i--)
79 bw.Write(Colors, i * stride, stride);
80 for (int i = 0; i < 2 * MaskSize; i++)
85 private struct ICONDIRENTRY {
88 public byte ColorCount;
91 public ushort BitCount;
92 public uint BytesInRes;
93 public uint ImageOffset;
95 public ICONDIRENTRY(ICONIMAGE image, int imageOffset) {
96 this.Width = (byte)image.Header.Width;
97 this.Height = (byte)(image.Header.Height >> 1);
100 this.Planes = image.Header.Planes;
101 this.BitCount = image.Header.BitCount;
102 this.BytesInRes = (uint)(image.Header.Size +
103 image.Colors.Length + image.MaskSize + image.MaskSize);
104 this.ImageOffset = (uint)imageOffset;
107 public void Write(BinaryWriter bw) {
110 bw.Write(ColorCount);
114 bw.Write(BytesInRes);
115 bw.Write(ImageOffset);
123 private struct ICONDIR {
124 public ushort Reserved;
127 public ICONDIRENTRY[] Entries;
129 public ICONDIR(ICONDIRENTRY[] entries) {
132 this.Count = (ushort)entries.Length;
133 this.Entries = entries;
136 public void Write(BinaryWriter bw) {
140 for (int i = 0; i < Entries.Length; i++)
141 Entries[i].Write(bw);
145 get { return (uint)(6 + Entries.Length *
146 (Entries.Length > 0 ? Entries[0].Size : 0)); }
150 private static BinaryWriter binaryWriter =
151 new BinaryWriter(new MemoryStream());
153 public static Icon Create(byte[] colors, int width, int height,
154 PixelFormat format) {
155 if (format != PixelFormat.Format32bppArgb)
156 throw new NotImplementedException();
158 ICONIMAGE image = new ICONIMAGE(width, height, colors);
159 ICONDIR dir = new ICONDIR(
160 new ICONDIRENTRY[] { new ICONDIRENTRY(image, 0) } );
161 dir.Entries[0].ImageOffset = dir.Size;
164 binaryWriter.BaseStream.Position = 0;
165 dir.Write(binaryWriter);
166 image.Write(binaryWriter);
168 binaryWriter.BaseStream.Position = 0;
169 icon = new Icon(binaryWriter.BaseStream);