File: IconColorizer.cs

package info (click to toggle)
keepass2 2.60%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,892 kB
  • sloc: cs: 119,878; xml: 6,087; ansic: 2,033; cpp: 738; sh: 50; makefile: 42
file content (260 lines) | stat: -rw-r--r-- 7,323 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
  KeePass Password Safe - The Open-Source Password Manager
  Copyright (C) 2003-2025 Dominik Reichl <dominik.reichl@t-online.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

using KeePass.Native;

using KeePassLib.Utility;

using ICONDIR = KeePass.Native.NativeMethods.ICONDIR;
using ICONDIRENTRY = KeePass.Native.NativeMethods.ICONDIRENTRY;
using BITMAPINFOHEADER = KeePass.Native.NativeMethods.BITMAPINFOHEADER;

namespace KeePass.UI
{
	// This code has been written specifically for recoloring KeePass icons;
	// other icons may be unsupported.
	internal static class IconColorizer
	{
		private sealed class IcImage
		{
			public ICONDIRENTRY Entry;
			public byte[] Data;
		}

		public static Icon Recolor(Icon ico, Color clr)
		{
			if(ico == null) { Debug.Assert(false); return null; }

			try
			{
				float fHue, fSaturation, fValue;
				UIUtil.ColorToHsv(clr, out fHue, out fSaturation, out fValue);

				List<IcImage> l = GetImages(ico);

				for(int i = l.Count - 1; i >= 0; --i)
				{
					if(!RecolorImage(l[i], fHue)) l.RemoveAt(i);
				}

				Icon icoNew = CreateIcon(l);
				if(icoNew != null) return icoNew;
			}
			catch(Exception) { Debug.Assert(false); }

			return (ico.Clone() as Icon);
		}

		private static List<IcImage> GetImages(Icon ico)
		{
			List<IcImage> l = new List<IcImage>();

			byte[] pb;
			using(MemoryStream ms = new MemoryStream())
			{
				ico.Save(ms);
				pb = ms.ToArray();
			}

			int cbDir = Marshal.SizeOf(typeof(ICONDIR));
			int cbEntry = Marshal.SizeOf(typeof(ICONDIRENTRY));
			int iPos = 0;

			ICONDIR d = MemUtil.BytesToStruct<ICONDIR>(pb, iPos);
			iPos += cbDir;

			if(d.idReserved != 0) { Debug.Assert(false); return l; }
			if(d.idType != 1) { Debug.Assert(false); return l; }

			for(uint u = 0; u < d.idCount; ++u)
			{
				IcImage img = new IcImage();

				img.Entry = MemUtil.BytesToStruct<ICONDIRENTRY>(pb, iPos);
				iPos += cbEntry;

				img.Data = MemUtil.Mid(pb, (int)img.Entry.dwImageOffset,
					(int)img.Entry.dwBytesInRes);

				l.Add(img);
			}

#if DEBUG
			int cbSum = cbDir + (l.Count * cbEntry);
			foreach(IcImage img in l) cbSum += img.Data.Length;
			Debug.Assert(cbSum == pb.Length); // No extra data
#endif

			return l;
		}

		private static Icon CreateIcon(List<IcImage> l)
		{
			int n = l.Count;
			if((n <= 0) || (n > ushort.MaxValue)) { Debug.Assert(false); return null; }

			int cbDir = Marshal.SizeOf(typeof(ICONDIR));
			int cbEntry = Marshal.SizeOf(typeof(ICONDIRENTRY));

			int iData = cbDir + (n * cbEntry);
			for(int i = 0; i < n; ++i)
			{
				int cb = l[i].Data.Length;

				Debug.Assert(l[i].Entry.dwBytesInRes == cb);
				l[i].Entry.dwBytesInRes = (uint)cb;
				l[i].Entry.dwImageOffset = (uint)iData;
				iData += cb;
			}

			byte[] pbIco;
			using(MemoryStream ms = new MemoryStream(iData))
			{
				ICONDIR d = new ICONDIR();
				d.idType = 1;
				d.idCount = (ushort)n;

				byte[] pb = MemUtil.StructToBytes<ICONDIR>(ref d);
				Debug.Assert(pb.Length == cbDir);
				MemUtil.Write(ms, pb);

				for(int i = 0; i < n; ++i)
				{
					ICONDIRENTRY e = l[i].Entry;
					pb = MemUtil.StructToBytes<ICONDIRENTRY>(ref e);
					Debug.Assert(pb.Length == cbEntry);
					MemUtil.Write(ms, pb);
				}

				for(int i = 0; i < n; ++i)
					MemUtil.Write(ms, l[i].Data);

				pbIco = ms.ToArray();
			}
			Debug.Assert(pbIco.Length == iData);

			Icon ico;
			using(MemoryStream msRead = new MemoryStream(pbIco, false))
			{
				ico = new Icon(msRead);
			}
			return ico;
		}

		private static bool RecolorImage(IcImage img, float fHue)
		{
			int cbHeader = Marshal.SizeOf(typeof(BITMAPINFOHEADER));

			byte[] pb = img.Data;
			if(pb.Length < cbHeader) { Debug.Assert(false); return false; }

			BITMAPINFOHEADER h = MemUtil.BytesToStruct<BITMAPINFOHEADER>(pb, 0);
			if(h.biSize != cbHeader)
			{
				Debug.Assert(h.biSize == 0x474E5089); // PNG
				return false;
			}
			Debug.Assert(h.biPlanes == 1);
			Debug.Assert((h.biClrUsed == 0) && (h.biClrImportant == 0));
			if(h.biCompression != 0) { Debug.Assert(false); return false; }

			int w = h.biWidth;
			if(w <= 0) { Debug.Assert(false); return false; }
			int hAbs = Math.Abs(h.biHeight);
			if((hAbs != w) && (hAbs != (w << 1))) { Debug.Assert(false); return false; }

			ushort uBpp = h.biBitCount;
			if((uBpp == 1) || (uBpp == 4) || (uBpp == 8))
				RecolorPalette(pb, cbHeader, 1 << uBpp, fHue);
			else if((uBpp == 24) || (uBpp == 32))
			{
				if(!RecolorPixelData(pb, cbHeader, w, w, uBpp, fHue)) return false;
			}
			else { Debug.Assert(false); return false; }

			Debug.Assert(((w < 256) && (img.Entry.bWidth == w)) ||
				((w >= 256) && (img.Entry.bWidth == 0)));
			Debug.Assert(((w < 256) && (img.Entry.bHeight == w)) ||
				((w >= 256) && (img.Entry.bHeight == 0)));
			Debug.Assert(((uBpp < 8) && (img.Entry.bColorCount == (1 << uBpp))) ||
				((uBpp >= 8) && (img.Entry.bColorCount == 0)));
			Debug.Assert(img.Entry.bReserved == 0);
			Debug.Assert(img.Entry.wPlanes == h.biPlanes);
			Debug.Assert(img.Entry.wBitCount == uBpp);

			return true;
		}

		private static void RecolorPalette(byte[] pb, int iOffset,
			int cColors, float fHue)
		{
			int iMax = iOffset + (cColors * 4);
			for(int i = iOffset; i < iMax; i += 4)
			{
				RecolorColorBgr(pb, i, fHue);
				Debug.Assert(pb[i + 3] == 0); // rgbReserved of RGBQUAD must be 0
			}
		}

		private static bool RecolorPixelData(byte[] pb, int iOffset,
			int w, int h, ushort uBpp, float fHue)
		{
			int cbPixel;
			if(uBpp == 24)
			{
				// The code below does not support row padding to a
				// multiple of 4
				if((w & 3) != 0) { Debug.Assert(false); return false; }

				cbPixel = 3;
			}
			else if(uBpp == 32) cbPixel = 4;
			else { Debug.Assert(false); return false; }

			int iMax = iOffset + (w * h * cbPixel);
			for(int i = iOffset; i < iMax; i += cbPixel)
				RecolorColorBgr(pb, i, fHue);

			return true;
		}

		private static void RecolorColorBgr(byte[] pb, int iOffset, float fHue)
		{
			Color clr = Color.FromArgb(pb[iOffset + 2], pb[iOffset + 1],
				pb[iOffset]);

			float fH, fS, fV;
			UIUtil.ColorToHsv(clr, out fH, out fS, out fV);

			clr = UIUtil.ColorFromHsv(fHue, fS, fV);

			pb[iOffset] = clr.B;
			pb[iOffset + 1] = clr.G;
			pb[iOffset + 2] = clr.R;
		}
	}
}