moel@40: /*
moel@40:   
moel@40:   Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@40: 
moel@40:   The contents of this file are subject to the Mozilla Public License Version
moel@40:   1.1 (the "License"); you may not use this file except in compliance with
moel@40:   the License. You may obtain a copy of the License at
moel@40:  
moel@40:   http://www.mozilla.org/MPL/
moel@40: 
moel@40:   Software distributed under the License is distributed on an "AS IS" basis,
moel@40:   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@40:   for the specific language governing rights and limitations under the License.
moel@40: 
moel@40:   The Original Code is the Open Hardware Monitor code.
moel@40: 
moel@40:   The Initial Developer of the Original Code is 
moel@40:   Michael Möller <m.moeller@gmx.ch>.
moel@40:   Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@40:   the Initial Developer. All Rights Reserved.
moel@40: 
moel@40:   Contributor(s):
moel@40: 
moel@40:   Alternatively, the contents of this file may be used under the terms of
moel@40:   either the GNU General Public License Version 2 or later (the "GPL"), or
moel@40:   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@40:   in which case the provisions of the GPL or the LGPL are applicable instead
moel@40:   of those above. If you wish to allow use of your version of this file only
moel@40:   under the terms of either the GPL or the LGPL, and not to allow others to
moel@40:   use your version of this file under the terms of the MPL, indicate your
moel@40:   decision by deleting the provisions above and replace them with the notice
moel@40:   and other provisions required by the GPL or the LGPL. If you do not delete
moel@40:   the provisions above, a recipient may use your version of this file under
moel@40:   the terms of any one of the MPL, the GPL or the LGPL.
moel@40:  
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@40:       public byte[] XOR;
moel@40:       public byte[] AND;
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@40:         int maskSize = (width * height) >> 3;
moel@40:         this.XOR = new byte[maskSize];
moel@40:         this.AND = new byte[maskSize];
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@40:         bw.Write(XOR);        
moel@40:         bw.Write(AND);
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@40:           image.Colors.Length + image.XOR.Length + image.AND.Length);
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@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@40:       using (BinaryWriter bw = new BinaryWriter(new MemoryStream())) {
moel@40: 				dir.Write(bw);
moel@40:         image.Write(bw);
moel@40: 
moel@40: 				bw.BaseStream.Position = 0;
moel@40:         icon = new Icon(bw.BaseStream);
moel@40: 			}
moel@40: 
moel@40:       return icon;
moel@40:     }
moel@40: 
moel@40:   }
moel@40: }