Skip to content

Commit

Permalink
Lots of reworking the cursor and icon resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
lkinsella committed Aug 14, 2016
1 parent a0996d3 commit 4f1eb52
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 78 deletions.
65 changes: 44 additions & 21 deletions Src/Workshell.PE.Resources/Graphics/CursorResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ namespace Workshell.PE.Resources
public sealed class CursorInfo
{

internal CursorInfo(CursorResource cursor, uint languageId, ushort hotspotX, ushort hotspotY, byte[] dib)
internal CursorInfo(CursorResource cursor, uint languageId, ushort hotspotX, ushort hotspotY, ushort width, ushort height, byte colors, byte[] dib)
{
Cursor = cursor;
Language = languageId;
Hotspot = new Point(hotspotX, hotspotY);
Size = new Size(width, height);
Colors = colors;
DIB = dib;
}

Expand All @@ -70,6 +72,18 @@ public Point Hotspot
private set;
}

public Size Size
{
get;
private set;
}

public byte Colors
{
get;
private set;
}

public byte[] DIB
{
get;
Expand All @@ -85,7 +99,8 @@ public enum CursorSaveFormat
Raw,
Cursor,
Icon,
Bitmap
Bitmap,
PNG
}

public class CursorResource : Resource
Expand Down Expand Up @@ -116,32 +131,30 @@ public CursorInfo GetInfo()
public CursorInfo GetInfo(uint languageId)
{
Tuple<ushort, ushort, byte[]> tuple = Load(languageId);
CursorInfo result = new CursorInfo(this, languageId, tuple.Item1, tuple.Item2, tuple.Item3);

return result;
}
using (MemoryStream dib_mem = new MemoryStream(tuple.Item3))
{
BITMAPINFOHEADER header = Utils.Read<BITMAPINFOHEADER>(dib_mem);

public Icon ToIcon()
{
return ToIcon(Color.Transparent);
}
ushort width = Convert.ToUInt16(header.biWidth);
ushort height = Convert.ToUInt16(header.biHeight / 2);
byte color_count = Convert.ToByte(header.biBitCount);
CursorInfo result = new CursorInfo(this, languageId, tuple.Item1, tuple.Item2, width, height, color_count, tuple.Item3);

public Icon ToIcon(Color backgroundColor)
{
return ToIcon(DEFAULT_LANGUAGE, Color.Transparent);
return result;
}
}

public Icon ToIcon(uint languageId)
public Icon ToIcon()
{
return ToIcon(languageId, Color.Transparent);
return ToIcon(DEFAULT_LANGUAGE);
}

public Icon ToIcon(uint languageId, Color backgroundColor)
public Icon ToIcon(uint languageId)
{
Tuple<ushort, ushort, byte[]> tuple = Load(languageId);
MemoryStream dib_mem = new MemoryStream(tuple.Item3);

using (dib_mem)
using (MemoryStream dib_mem = new MemoryStream(tuple.Item3))
{
BITMAPINFOHEADER header = Utils.Read<BITMAPINFOHEADER>(dib_mem);

Expand Down Expand Up @@ -199,20 +212,21 @@ public Bitmap ToBitmap(uint languageId)

public Bitmap ToBitmap(uint languageId, Color backgroundColor)
{
using (Icon icon = ToIcon(languageId, backgroundColor))
using (Icon icon = ToIcon(languageId))
{
Rectangle rect = new Rectangle(0, 0, icon.Width, icon.Height);
Bitmap bitmap = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);

using (Graphics graphics = Graphics.FromImage(bitmap))
{
using (SolidBrush brush = new SolidBrush(backgroundColor))
{
graphics.FillRectangle(brush, rect);
graphics.DrawIcon(icon, rect);
}

graphics.DrawIcon(icon, rect);
}

bitmap.MakeTransparent(backgroundColor);

return bitmap;
}
}
Expand Down Expand Up @@ -249,6 +263,15 @@ public void Save(Stream stream, uint languageId, CursorSaveFormat format)
bitmap.Save(stream, ImageFormat.Bmp);
}

break;
}
case CursorSaveFormat.PNG:
{
using (Bitmap bitmap = ToBitmap(languageId))
{
bitmap.Save(stream, ImageFormat.Png);
}

break;
}
case CursorSaveFormat.Cursor:
Expand Down
180 changes: 123 additions & 57 deletions Src/Workshell.PE.Resources/Graphics/IconResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ internal IconInfo(IconResource resource, uint languageId, ushort width, ushort h
{
Icon = resource;
Language = languageId;
Width = width;
Height = height;
ColorCount = colors;
Size = new Size(width, height);
Colors = colors;
DIB = dib;
IsPNG = isPNG;
}
Expand All @@ -68,19 +67,13 @@ public uint Language
private set;
}

public ushort Width
public Size Size
{
get;
private set;
}

public ushort Height
{
get;
private set;
}

public byte ColorCount
public byte Colors
{
get;
private set;
Expand All @@ -106,7 +99,8 @@ public enum IconSaveFormat
{
Raw,
Icon,
Bitmap
Bitmap,
PNG
}

public class IconResource : Resource
Expand Down Expand Up @@ -144,61 +138,96 @@ public IconInfo GetInfo(uint languageId)

public Icon ToIcon()
{
return ToIcon(Color.Transparent);
}

public Icon ToIcon(Color backgroundColor)
{
return ToIcon(DEFAULT_LANGUAGE, Color.Transparent);
return ToIcon(DEFAULT_LANGUAGE);
}

public Icon ToIcon(uint languageId)
{
return ToIcon(languageId, Color.Transparent);
}

public Icon ToIcon(uint languageId, Color backgroundColor)
{
Tuple<ushort, ushort, byte, byte[], bool> tuple = Load(languageId);
MemoryStream dib_mem = new MemoryStream(tuple.Item4);

using (dib_mem)
if (!tuple.Item5)
{
BITMAPINFOHEADER header = Utils.Read<BITMAPINFOHEADER>(dib_mem);
using (MemoryStream dib_mem = new MemoryStream(tuple.Item4))
{
BITMAPINFOHEADER header = Utils.Read<BITMAPINFOHEADER>(dib_mem);

ushort width = Convert.ToUInt16(header.biWidth);
ushort height = Convert.ToUInt16(header.biHeight / 2);
byte color_count = Convert.ToByte(header.biBitCount);

ushort width = Convert.ToUInt16(header.biWidth);
ushort height = Convert.ToUInt16(header.biHeight / 2);
byte color_count = Convert.ToByte(header.biBitCount);
MemoryStream mem = new MemoryStream();

MemoryStream mem = new MemoryStream();
using (mem)
{
Utils.Write(Convert.ToUInt16(0), mem);
Utils.Write(Convert.ToUInt16(1), mem);
Utils.Write(Convert.ToUInt16(1), mem);

using (mem)
uint colors = 0;

if (color_count != 0 && color_count < 8)
colors = Convert.ToUInt32(Math.Pow(2, color_count));

Utils.Write(Convert.ToByte(width >= 256 ? 0 : width), mem);
Utils.Write(Convert.ToByte(height >= 256 ? 0 : height), mem);
Utils.Write(Convert.ToByte(colors), mem);
Utils.Write(Convert.ToByte(0), mem);
Utils.Write(Convert.ToUInt16(1), mem);
Utils.Write(Convert.ToUInt16(color_count), mem);
Utils.Write(tuple.Item4.Length, mem);
Utils.Write(22, mem);

Utils.Write(tuple.Item4, mem);

mem.Seek(0, SeekOrigin.Begin);

Icon icon = new Icon(mem);

return icon;
}
}
}
else
{
using (MemoryStream dib_mem = new MemoryStream(tuple.Item4))
{
Utils.Write(Convert.ToUInt16(0), mem);
Utils.Write(Convert.ToUInt16(1), mem);
Utils.Write(Convert.ToUInt16(1), mem);
using (Image png = Image.FromStream(dib_mem))
{
ushort width = Convert.ToUInt16(png.Width);
ushort height = Convert.ToUInt16(png.Height);
byte color_count = 32;

MemoryStream mem = new MemoryStream();

uint colors = 0;
using (mem)
{
Utils.Write(Convert.ToUInt16(0), mem);
Utils.Write(Convert.ToUInt16(1), mem);
Utils.Write(Convert.ToUInt16(1), mem);

if (color_count != 0 && color_count < 8)
colors = Convert.ToUInt32(Math.Pow(2, color_count));
uint colors = 0;

Utils.Write(Convert.ToByte(width >= 256 ? 0 : width), mem);
Utils.Write(Convert.ToByte(height >= 256 ? 0 : height), mem);
Utils.Write(Convert.ToByte(colors), mem);
Utils.Write(Convert.ToByte(0), mem);
Utils.Write(Convert.ToUInt16(1), mem);
Utils.Write(Convert.ToUInt16(color_count), mem);
Utils.Write(tuple.Item4.Length, mem);
Utils.Write(22, mem);
if (color_count != 0 && color_count < 8)
colors = Convert.ToUInt32(Math.Pow(2, color_count));

Utils.Write(tuple.Item4, mem);
Utils.Write(Convert.ToByte(width >= 256 ? 0 : width), mem);
Utils.Write(Convert.ToByte(height >= 256 ? 0 : height), mem);
Utils.Write(Convert.ToByte(colors), mem);
Utils.Write(Convert.ToByte(0), mem);
Utils.Write(Convert.ToUInt16(1), mem);
Utils.Write(Convert.ToUInt16(color_count), mem);
Utils.Write(tuple.Item4.Length, mem);
Utils.Write(22, mem);

mem.Seek(0, SeekOrigin.Begin);
Utils.Write(tuple.Item4, mem);

Icon icon = new Icon(mem);
mem.Seek(0, SeekOrigin.Begin);

return icon;
Icon icon = new Icon(mem);

return icon;
}
}
}
}
}
Expand All @@ -220,21 +249,40 @@ public Bitmap ToBitmap(uint languageId)

public Bitmap ToBitmap(uint languageId, Color backgroundColor)
{
using (Icon icon = ToIcon(languageId, backgroundColor))
{
Rectangle rect = new Rectangle(0, 0, icon.Width, icon.Height);
Bitmap bitmap = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
Tuple<ushort, ushort, byte, byte[], bool> tuple = Load(languageId);

using (Graphics graphics = Graphics.FromImage(bitmap))
if (!tuple.Item5)
{
using (Icon icon = ToIcon(languageId))
{
using (SolidBrush brush = new SolidBrush(backgroundColor))
Rectangle rect = new Rectangle(0, 0, icon.Width, icon.Height);
Bitmap bitmap = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);

using (Graphics graphics = Graphics.FromImage(bitmap))
{
graphics.FillRectangle(brush, rect);

using (SolidBrush brush = new SolidBrush(backgroundColor))
graphics.FillRectangle(brush, rect);

graphics.DrawIcon(icon, rect);
}

bitmap.MakeTransparent(backgroundColor);

return bitmap;
}
}
else
{
using (MemoryStream dib_mem = new MemoryStream(tuple.Item4))
{
using (Image png = Image.FromStream(dib_mem))
{
Bitmap bitmap = new Bitmap(png);

return bitmap;
return bitmap;
}
}
}
}

Expand Down Expand Up @@ -271,6 +319,24 @@ public void Save(Stream stream, uint languageId, IconSaveFormat format)
bitmap.Save(stream, ImageFormat.Bmp);
}

break;
}
case IconSaveFormat.PNG:
{
Tuple<ushort, ushort, byte, byte[], bool> tuple = Load(languageId);

if (!tuple.Item5)
{
using (Bitmap bitmap = ToBitmap(languageId))
{
bitmap.Save(stream, ImageFormat.Png);
}
}
else
{
stream.Write(tuple.Item4, 0, tuple.Item4.Length);
}

break;
}
}
Expand Down

0 comments on commit 4f1eb52

Please sign in to comment.