forked from kendarorg/RepositoryCache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageStructs.cs
More file actions
371 lines (340 loc) · 8.92 KB
/
ImageStructs.cs
File metadata and controls
371 lines (340 loc) · 8.92 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Created by Joshua Flanagan
// http://flimflan.com/blog
// May 2004
//
// You may freely use this code as you wish, I only ask that you retain my name in the source code
// Modified by Pavel Janda
// - added support for 32bpp bitmaps
// November 2006
using System;
using System.IO;
using BYTE = System.Byte;
using WORD = System.UInt16;
using DWORD = System.UInt32;
using LONG = System.Int32;
namespace FlimFlan.IconEncoder
{
public struct ICONIMAGE
{
/// <summary>
/// icHeader: DIB format header
/// </summary>
public BITMAPINFOHEADER Header;
/// <summary>
/// icColors: Color table
/// </summary>
public RGBQUAD[] Colors;
/// <summary>
/// icXOR: DIB bits for XOR mask
/// </summary>
public BYTE[] XOR;
/// <summary>
/// icAND: DIB bits for AND mask
/// </summary>
public BYTE[] AND;
public void Populate(BinaryReader br)
{
// read in the header
this.Header.Populate(br);
this.Colors = new RGBQUAD[Header.biClrUsed];
// read in the color table
for(int i=0; i<this.Header.biClrUsed; ++i)
{
this.Colors[i].Populate(br);
}
// read in the XOR mask
this.XOR = br.ReadBytes(numBytesInXor());
// read in the AND mask
this.AND = br.ReadBytes(numBytesInAnd());
}
public void Save(BinaryWriter bw)
{
Header.Save(bw);
for(int i=0; i<Colors.Length; i++)
Colors[i].Save(bw);
bw.Write(XOR);
bw.Write(AND);
}
#region byte count calculation functions
public int numBytesInXor()
{
// number of bytes per pixel depends on bitcount
int bytesPerLine = Convert.ToInt32(Math.Ceiling((Header.biWidth * Header.biBitCount) / 8.0));
// If necessary, a scan line must be zero-padded to end on a 32-bit boundary.
// so there will be some padding, if the icon is less than 32 pixels wide
int padding = (bytesPerLine % 4);
if (padding > 0)
bytesPerLine += (4 - padding);
return bytesPerLine * (Header.biHeight >> 1);
}
public int numBytesInAnd()
{
// each byte can hold 8 pixels (1bpp)
// check for a remainder
int bytesPerLine = Convert.ToInt32(Math.Ceiling(Header.biWidth / 8.0));
// If necessary, a scan line must be zero-padded to end on a 32-bit boundary.
// so there will be some padding, if the icon is less than 32 pixels wide
int padding = (bytesPerLine % 4);
if (padding > 0)
bytesPerLine += (4 - padding);
return bytesPerLine * (Header.biHeight >> 1);
}
#endregion
}
public struct ICONDIR
{
/// <summary>
/// idReserved: Always 0
/// </summary>
public WORD Reserved; // Reserved
/// <summary>
/// idType: Resource type (Always 1 for icons)
/// </summary>
public WORD ResourceType;
/// <summary>
/// idCount: Number of images in directory
/// </summary>
public WORD EntryCount;
/// <summary>
/// idEntries: Directory entries for each image
/// </summary>
public ICONDIRENTRY[] Entries;
public void Save(BinaryWriter bw)
{
bw.Write(Reserved);
bw.Write(ResourceType);
bw.Write(EntryCount);
for (int i=0; i<Entries.Length; ++i)
Entries[i].Save(bw);
}
public void Populate(BinaryReader br)
{
Reserved = br.ReadUInt16();
ResourceType = br.ReadUInt16();
EntryCount = br.ReadUInt16();
Entries = new ICONDIRENTRY[this.EntryCount];
for (int i=0; i < Entries.Length; i++)
{
Entries[i].Populate(br);
}
}
}
public struct ICONDIRENTRY
{
/// <summary>
/// bWidth: In pixels. Must be 16, 32, or 64
/// </summary>
public BYTE Width;
/// <summary>
/// bHeight: In pixels. Must be 16, 32, or 64
/// </summary>
public BYTE Height;
/// <summary>
/// bColorCount: Number of colors in image (0 if >=8bpp)
/// </summary>
public BYTE ColorCount;
/// <summary>
/// bReserved: Must be zero
/// </summary>
public BYTE Reserved;
/// <summary>
/// wPlanes: Number of color planes in the icon bitmap
/// </summary>
public WORD Planes;
/// <summary>
/// wBitCount: Number of bits in each pixel of the icon. Must be 1,4,8, or 24
/// </summary>
public WORD BitCount;
/// <summary>
/// dwBytesInRes: Number of bytes in the resource
/// </summary>
public DWORD BytesInRes;
/// <summary>
/// dwImageOffset: Number of bytes from the beginning of the file to the image
/// </summary>
public DWORD ImageOffset;
public void Save(BinaryWriter bw)
{
bw.Write(Width);
bw.Write(Height);
bw.Write(ColorCount);
bw.Write(Reserved);
bw.Write(Planes);
bw.Write(BitCount);
bw.Write(BytesInRes);
bw.Write(ImageOffset);
}
public void Populate(BinaryReader br)
{
Width = br.ReadByte();
Height = br.ReadByte();
ColorCount = br.ReadByte();
Reserved = br.ReadByte();
Planes = br.ReadUInt16();
BitCount = br.ReadUInt16();
BytesInRes = br.ReadUInt32();
ImageOffset = br.ReadUInt32();
}
}
public struct BITMAPFILEHEADER
{
public WORD Type;
public DWORD Size;
public WORD Reserved1;
public WORD Reserved2;
public DWORD OffBits;
public void Populate(BinaryReader br)
{
Type = br.ReadUInt16();
Size = br.ReadUInt32();
Reserved1 = br.ReadUInt16();
Reserved2 = br.ReadUInt16();
OffBits = br.ReadUInt32();
}
public void Save(BinaryWriter bw)
{
bw.Write(Type);
bw.Write(Size);
bw.Write(Reserved1);
bw.Write(Reserved2);
bw.Write(OffBits);
}
}
public struct BITMAPINFO
{
public BITMAPINFOHEADER infoHeader;
public RGBQUAD[] colorMap;
public void Populate(BinaryReader br)
{
infoHeader.Populate(br);
colorMap = new RGBQUAD[getNumberOfColors()];
// read in the color table
for(int i=0; i<colorMap.Length; ++i)
{
colorMap[i].Populate(br);
}
}
public void Save(BinaryWriter bw)
{
infoHeader.Save(bw);
for(int i=0; i<colorMap.Length; i++)
colorMap[i].Save(bw);
}
private uint getNumberOfColors()
{
if (infoHeader.biClrUsed > 0)
{
// number of colors is specified
return infoHeader.biClrUsed;
}
else
{
// number of colors is based on the bitcount
switch(infoHeader.biBitCount)
{
case 1:
return 2;
case 4:
return 16;
case 8:
return 256;
default:
return 0;
}
}
}
}
/// <summary>
/// Describes the format of the bitmap image
/// </summary>
/// <remarks>
/// BITMAPHEADERINFO struct
/// referenced by http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwui/html/msdn_icons.asp
/// defined by http://www.whisqu.se/per/docs/graphics52.htm
/// Only the following members are used: biSize, biWidth, biHeight, biPlanes, biBitCount, biSizeImage. All other members must be 0. The biHeight member specifies the combined height of the XOR and AND masks. The members of icHeader define the contents and sizes of the other elements of the ICONIMAGE structure in the same way that the BITMAPINFOHEADER structure defines a CF_DIB format DIB
/// </remarks>
public struct BITMAPINFOHEADER
{
public const int Size = 40;
public DWORD biSize;
public LONG biWidth;
/// <summary>
/// Height of bitmap. For icons, this is the height of XOR and AND masks together. Divide by 2 to get true height.
/// </summary>
public LONG biHeight;
public WORD biPlanes;
public WORD biBitCount;
public DWORD biCompression;
public DWORD biSizeImage;
public LONG biXPelsPerMeter;
public LONG biYPelsPerMeter;
public DWORD biClrUsed;
public DWORD biClrImportant;
public void Save(BinaryWriter bw)
{
bw.Write(biSize);
bw.Write(biWidth);
bw.Write(biHeight);
bw.Write(biPlanes);
bw.Write(biBitCount);
bw.Write(biCompression);
bw.Write(biSizeImage);
bw.Write(biXPelsPerMeter);
bw.Write(biYPelsPerMeter);
bw.Write(biClrUsed);
bw.Write(biClrImportant);
}
public void Populate(BinaryReader br)
{
biSize = br.ReadUInt32();
biWidth = br.ReadInt32();
biHeight = br.ReadInt32();
biPlanes = br.ReadUInt16();
biBitCount = br.ReadUInt16();
biCompression = br.ReadUInt32();
biSizeImage = br.ReadUInt32();
biXPelsPerMeter = br.ReadInt32();
biYPelsPerMeter = br.ReadInt32();
biClrUsed = br.ReadUInt32();
biClrImportant = br.ReadUInt32();
}
}
// RGBQUAD structure changed by Pavel Janda on 14/11/2006
public struct RGBQUAD
{
public const int Size = 4;
public BYTE blue;
public BYTE green;
public BYTE red;
public BYTE alpha;
public RGBQUAD(BYTE[] bgr) : this(bgr[0], bgr[1], bgr[2]){}
public RGBQUAD(BYTE blue, BYTE green, BYTE red)
{
this.blue = blue;
this.green = green;
this.red = red;
this.alpha = 0;
}
public RGBQUAD(BYTE blue, BYTE green, BYTE red, BYTE alpha)
{
this.blue = blue;
this.green = green;
this.red = red;
this.alpha = alpha;
}
public void Save(BinaryWriter bw)
{
bw.BaseStream.WriteByte(blue);
bw.BaseStream.WriteByte(green);
bw.BaseStream.WriteByte(red);
bw.BaseStream.WriteByte(alpha);
}
public void Populate(BinaryReader br)
{
blue = br.ReadByte();
green = br.ReadByte();
red = br.ReadByte();
alpha = br.ReadByte();
}
}
}