Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathBitmapParser.cs
More file actions
Latest commit
executable file
·78 lines (66 loc) · 2.2 KB
/
Copy pathBitmapParser.cs
File metadata and controls
executable file
·78 lines (66 loc) · 2.2 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
usingSystem.Drawing;
usingSystem.Drawing.Imaging;
usingSystem.IO;
usingSystem.Runtime.Versioning;
namespaceResourceExtractor;
[SupportedOSPlatform("windows")]
internalstaticclassBitmapParser{
internalstaticBitmapParse(BinaryReaderreader,ImageHeaderheader,boolignoreAlpha=false){
varResult=newBitmap(header.Width,header.Height,PixelFormat.Format32bppArgb);
try{
varRaw=Result.LockBits(newRectangle(0,0,Result.Width,Result.Height),ImageLockMode.WriteOnly,Result.PixelFormat);
varPixelCount=header.Height*header.Width;
unsafe {
varBuffer=(byte*)Raw.Scan0;
Color[]Palette=null;
byte[]BitFields=null;
varEightCount=header.BitCount==8;
if(EightCount){// 8-bit, with palette
Palette=newColor[256];
for(vari=0;i<256;++i){
Palette[i]=ReadColor(reader,32);
}
BitFields=reader.ReadBytes(PixelCount);
}
for(varPixel=0;Pixel<PixelCount;++Pixel){
varColor=EightCount?Palette[BitFields[Pixel]]:ReadColor(reader,header.BitCount);
Buffer[4*Pixel+0]=Color.B;
Buffer[4*Pixel+1]=Color.G;
Buffer[4*Pixel+2]=Color.R;
Buffer[4*Pixel+3]=ignoreAlpha?(byte)255:Color.A;
}
}
Result.UnlockBits(Raw);
Result.RotateFlip(RotateFlipType.RotateNoneFlipY);
}catch{
Result.Dispose();
throw;
}
returnResult;
}
privatestaticColorDecodeRGB565(ushortC)=>
Color.FromArgb(((C&0xF800)>>11)*0xFF/0x1F,((C&0x07E0)>>5)*0xFF/0x3F,(C&0x001F)*0xFF/0x1F);
publicstaticColorReadColor(BinaryReaderreader,intbitDepth){
switch(bitDepth){
case8:
returnreader.ReadByte().Let(gray =>Color.FromArgb(gray,gray,gray));
case16:
returnDecodeRGB565(reader.ReadUInt16());
case24:{
varb=reader.ReadByte();
varg=reader.ReadByte();
varr=reader.ReadByte();
returnColor.FromArgb(0xFF,r,g,b);
}
case32:{
varb=reader.ReadByte();
varg=reader.ReadByte();
varr=reader.ReadByte();
vara=reader.ReadByte();
returnColor.FromArgb(a<0x80?2*a:0xFF,r,g,b);
}
default:
returnColor.HotPink;
}
}
}