forked from Windower/ResourceExtractor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitmapParser.cs
118 lines (106 loc) · 4.36 KB
/
BitmapParser.cs
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
// <copyright file="BitmapParser.cs" company="Windower Team">
// Copyright © 2014 Windower Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// </copyright>
namespace ResourceExtractor
{
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
internal static class BitmapParser
{
internal static Bitmap Parse(BinaryReader reader, ImageHeader header, bool ignoreAlpha = false)
{
var Result = new Bitmap(header.Width, header.Height, PixelFormat.Format32bppArgb);
try
{
var Raw = Result.LockBits(new Rectangle(0, 0, Result.Width, Result.Height), ImageLockMode.WriteOnly, Result.PixelFormat);
var PixelCount = header.Height * header.Width;
unsafe
{
var Buffer = (byte*)Raw.Scan0;
Color[] Palette = null;
byte[] BitFields = null;
bool EightCount = header.BitCount == 8;
if (EightCount)
{ // 8-bit, with palette
Palette = new Color[256];
for (var i = 0; i < 256; ++i)
{
Palette[i] = ReadColor(reader, 32);
}
BitFields = reader.ReadBytes(PixelCount);
}
for (var Pixel = 0; Pixel < PixelCount; ++Pixel)
{
var Color = 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;
}
return Result;
}
private static Color DecodeRGB565(ushort C)
{
return Color.FromArgb(((C & 0xF800) >> 11) * 0xFF / 0x1F, ((C & 0x07E0) >> 5) * 0xFF / 0x3F, (C & 0x001F) * 0xFF / 0x1F);
}
public static Color ReadColor(BinaryReader reader, int bitDepth)
{
switch (bitDepth)
{
case 8:
{
int GrayScale = reader.ReadByte();
return Color.FromArgb(GrayScale, GrayScale, GrayScale);
}
case 16: return DecodeRGB565(reader.ReadUInt16());
case 24:
case 32:
{
int B = reader.ReadByte();
int G = reader.ReadByte();
int R = reader.ReadByte();
int A = 255;
if (bitDepth == 32)
{
byte SemiAlpha = reader.ReadByte();
if (SemiAlpha < 0x80)
{
A = 2 * SemiAlpha;
}
}
return Color.FromArgb(A, R, G, B);
}
default:
return Color.HotPink;
}
}
}
}