Repository navigation
Expand file tree
/
Copy pathTools.cs
More file actions
132 lines (118 loc) · 4.13 KB
/
Copy pathTools.cs
File metadata and controls
132 lines (118 loc) · 4.13 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
using DotXT;
class Tools
{
public static void LoadBin(Bus b, string file, uint addr)
{
Log.DoLog($"Load {file} at {addr:X6}", LogLevel.INFO);
try
{
using(Stream source = File.Open(file, FileMode.Open))
{
byte[] buffer = new byte[512];
for(;;)
{
int n_read = source.Read(buffer, 0, 512);
if (n_read == 0)
break;
for(int i=0; i<n_read; i++)
b.WriteByte(addr++, buffer[i]);
}
}
}
catch(Exception e)
{
Log.DoLog($"Failed to load {file}: {e}", LogLevel.ERROR);
}
}
public static void Assert(bool v, string reason)
{
if (v == false)
{
Console.WriteLine($"Assertion failed ({reason})");
Console.WriteLine(new System.Diagnostics.StackTrace().ToString());
System.Environment.Exit(1);
}
}
public static int GetValue(string v, bool hex)
{
string[] aparts = v.Split(":");
if (aparts.Length == 2)
return Convert.ToInt32(aparts[0], 16) * 16 + Convert.ToInt32(aparts[1], 16);
if (v.Length > 2 && v[0] == '0' && v[1] == 'x')
return Convert.ToInt32(v.Substring(2), 16);
if (hex)
return Convert.ToInt32(v, 16);
return Convert.ToInt32(v, 10);
}
public static byte[] GraphicalFrameToBmp(GraphicalFrame g)
{
int out_len = g.width * g.height * 3 + 2 + 12 + 40;
byte [] out_ = new byte[out_len];
int offset = 0;
out_[offset++] = (byte)'B';
out_[offset++] = (byte)'M';
out_[offset++] = (byte)out_len; // file size in bytes
out_[offset++] = (byte)(out_len >> 8);
out_[offset++] = (byte)(out_len >> 16);
out_[offset++] = (byte)(out_len >> 24);
out_[offset++] = 0x00; // reserved
out_[offset++] = 0x00;
out_[offset++] = 0x00;
out_[offset++] = 0x00;
out_[offset++] = 54; // offset of start (2 + 12 + 40)
out_[offset++] = 0x00;
out_[offset++] = 0x00;
out_[offset++] = 0x00;
//assert(offset == 0x0e);
out_[offset++] = 40; // header size
out_[offset++] = 0x00;
out_[offset++] = 0x00;
out_[offset++] = 0x00;
out_[offset++] = (byte)g.width;
out_[offset++] = (byte)(g.width >> 8);
out_[offset++] = (byte)(g.width >> 16);
out_[offset++] = 0x00;
out_[offset++] = (byte)g.height;
out_[offset++] = (byte)(g.height >> 8);
out_[offset++] = (byte)(g.height >> 16);
out_[offset++] = 0x00;
out_[offset++] = 0x01; // color planes
out_[offset++] = 0x00;
out_[offset++] = 24; // bits per pixel
out_[offset++] = 0x00;
out_[offset++] = 0x00; // compression method
out_[offset++] = 0x00;
out_[offset++] = 0x00;
out_[offset++] = 0x00;
out_[offset++] = 0x00; // image size
out_[offset++] = 0x00;
out_[offset++] = 0x00;
out_[offset++] = 0x00;
out_[offset++] = (byte)g.width;
out_[offset++] = (byte)(g.width >> 8);
out_[offset++] = (byte)(g.width >> 16);
out_[offset++] = 0x00;
out_[offset++] = (byte)g.height;
out_[offset++] = (byte)(g.height >> 8);
out_[offset++] = (byte)(g.height >> 16);
out_[offset++] = 0x00;
out_[offset++] = 0x00; // color count
out_[offset++] = 0x00;
out_[offset++] = 0x00;
out_[offset++] = 0x00;
out_[offset++] = 0x00; // important colors
out_[offset++] = 0x00;
out_[offset++] = 0x00;
out_[offset++] = 0x00;
for(int y=g.height - 1; y >= 0; y--) {
int in_o = y * g.width * 3;
for(int x=0; x<g.width; x++) {
int in_o2 = in_o + x * 3;
out_[offset++] = g.rgb_pixels[in_o2 + 2];
out_[offset++] = g.rgb_pixels[in_o2 + 1];
out_[offset++] = g.rgb_pixels[in_o2 + 0];
}
}
return out_;
}
}