-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pcx.cs
82 lines (61 loc) · 1.97 KB
/
Pcx.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
using System.IO;
using System;
namespace Nitemare3D
{
public sealed class Pcx
{
const int PcxHeaderStart = 65; //we don't really need much of the header
const int PcxDataStart = 128;
const int Width = 320;
const int Height = 200;
public byte[,] ImageData = new byte[Width, Height];
byte[,] DecodePixels(BinaryReader reader)
{
byte[,] output = new byte[Width, Height];
int y = 0;
int x = 0;
int bytesPerScanline = 320;
while (y < 200)
{
var readByte = reader.ReadByte();
var repeatCount = (readByte & 0x3F);
if (!(x >= bytesPerScanline))
{
if (0xC0 == (readByte & 0xC0))
{
readByte = reader.ReadByte();
while (repeatCount > 0)
{
output[x, y] = readByte;
repeatCount -= 1;
x += 1;
}
}
else
{
output[x, y] = readByte;
x += 1;
}
}
if (x >= bytesPerScanline)
{
x = 0;
y += 1;
}
}
return output;
}
public Pcx(byte[] data)
{
using (var reader = new BinaryReader(new MemoryStream(data)))
{
reader.BaseStream.Position = PcxHeaderStart;
byte colorPlaneCount = reader.ReadByte();
UInt16 scaleLineColorPlane = reader.ReadUInt16();
reader.BaseStream.Position = PcxDataStart;
ImageData = DecodePixels(reader);
reader.BaseStream.Position++; //random last byte
}
}
}
}