-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBitmapHeader.cs
More file actions
159 lines (133 loc) · 4.97 KB
/
BitmapHeader.cs
File metadata and controls
159 lines (133 loc) · 4.97 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.MemoryMappedFiles;
namespace RoboNav
{
class BitmapHeader
{
public string Type { private set; get; }
public int FileSize { set; get; }
public int Reserved { set; get; }
public int Offset { get; private set; }
public int BitmapInfoHeaderSize { set; get; }
public BitmapHeader()
{
Type = "BM";
Reserved = 0;
Offset = 54;
BitmapInfoHeaderSize = 40;
}
public static BitmapHeader GetHeader(byte[] data)
{
var header = new BitmapHeader();
using (var s = new MemoryStream(data))
using (var reader = new BinaryReader(s))
{
header.Type = Encoding.ASCII.GetString(data, 0, 2);
s.Seek(2, SeekOrigin.Begin);
header.FileSize = reader.ReadInt32();
header.Reserved = reader.ReadInt32();
header.Offset = reader.ReadInt32();
header.BitmapInfoHeaderSize = reader.ReadInt32();
}
return header;
}
public static BitmapHeader GetHeader(MemoryMappedViewAccessor Reader,out long MMPosition)
{
MMPosition = 0;
var header = new BitmapHeader();
header.Type = Encoding.ASCII.GetString(new byte[] { Reader.ReadByte(MMPosition) });
MMPosition++;
header.Type += Encoding.ASCII.GetString(new byte[] { Reader.ReadByte(MMPosition) });
MMPosition++;
header.FileSize = Reader.ReadInt32(MMPosition);
MMPosition += 4;
header.Reserved = Reader.ReadInt32(MMPosition);
MMPosition += 4;
header.Offset = Reader.ReadInt32(MMPosition);
MMPosition += 4;
header.BitmapInfoHeaderSize = Reader.ReadInt32(MMPosition);
MMPosition += 4;
return header;
}
public byte[] CreateBitmapHeader()
{
using (var s = new MemoryStream())
using (var writer = new BinaryWriter(s))
{
writer.Write(Type[0]);
writer.Write(Type[1]);
writer.Write(FileSize);
writer.Write(Reserved);
writer.Write(Offset);
writer.Write(BitmapInfoHeaderSize);
return s.ToArray();
}
}
}
class BigBitmapHeader
{
public string Type { private set; get; }
public long FileSize { set; get; }
public int Reserved { set; get; }
public int Offset { get; private set; }
public int BitmapInfoHeaderSize { set; get; }
public BigBitmapHeader()
{
Type = "BM";
Reserved = 0;
Offset = 70;
BitmapInfoHeaderSize = 48;
}
public static BigBitmapHeader GetHeader(byte[] data)
{
var header = new BigBitmapHeader();
using (var s = new MemoryStream(data))
using (var reader = new BinaryReader(s))
{
header.Type = Encoding.ASCII.GetString(data, 0, 2);
s.Seek(2, SeekOrigin.Begin);
header.FileSize = reader.ReadInt64();
header.Reserved = reader.ReadInt32();
header.Offset = reader.ReadInt32();
header.BitmapInfoHeaderSize = reader.ReadInt32();
}
return header;
}
public static BigBitmapHeader GetHeader(MemoryMappedViewAccessor Reader, out int MMPosition)
{
MMPosition = 0;
var header = new BigBitmapHeader();
header.Type = Encoding.ASCII.GetString(new byte[] { Reader.ReadByte(MMPosition) });
MMPosition++;
header.Type += Encoding.ASCII.GetString(new byte[] { Reader.ReadByte(MMPosition) });
MMPosition++;
header.FileSize = Reader.ReadInt64(MMPosition);
MMPosition += 8;
header.Reserved = Reader.ReadInt32(MMPosition);
MMPosition += 4;
header.Offset = Reader.ReadInt32(MMPosition);
MMPosition += 4;
header.BitmapInfoHeaderSize = Reader.ReadInt32(MMPosition);
MMPosition += 4;
return header;
}
public byte[] CreateBigBitmapHeader()
{
using (var s = new MemoryStream())
using (var writer = new BinaryWriter(s))
{
writer.Write(Type[0]);
writer.Write(Type[1]);
writer.Write(FileSize);
writer.Write(Reserved);
writer.Write(Offset);
writer.Write(BitmapInfoHeaderSize);
return s.ToArray();
}
}
}
}