forked from rapyuta-robotics/amphion
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprocessing.ts
164 lines (152 loc) · 5.26 KB
/
processing.ts
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
160
161
162
163
164
export const COLOR_TYPES = {
OCCUPIED: 100,
UNOCCUPIED: 0,
UNKNOWN: -1,
OTHER: 127,
};
export const populateImageDataFromNavMsg = (
imageData: ImageData,
width: number,
height: number,
dataSource: number[],
) => {
for (let row = 0; row < height; row++) {
for (let col = 0; col < width; col++) {
const mapI = col + (height - row - 1) * width;
const data = dataSource[mapI];
const val = new DataView(Uint8Array.from([data]).buffer).getUint8(0);
let i = (col + row * width) * 4;
if (val >= 0 && val <= 100) {
const v = 255 - (255 * val) / 100;
imageData.data[i] = v; // red
imageData.data[++i] = v; // green
imageData.data[++i] = v; // blue
imageData.data[++i] = 255; // alpha
} else if (val >= 101 && val <= 127) {
// illegal positive values in green
imageData.data[i] = 0; // red
imageData.data[++i] = 255; // green
imageData.data[++i] = 0; // blue
imageData.data[++i] = 255; // alpha
} else if (val >= 128 && val <= 254) {
// illegal negative (char) values in shades of red/yellow
imageData.data[i] = 255; // red
imageData.data[++i] = (255 * (val - 128)) / (254 - 128); // green
imageData.data[++i] = 0; // blue
imageData.data[++i] = 255; // alpha
} else {
// legal -1 value is tasteful blueish greenish grayish color
imageData.data[i] = 0x70; // red
imageData.data[++i] = 0x89; // green
imageData.data[++i] = 0x86; // blue
imageData.data[++i] = 255; // alpha
}
}
}
};
export const populateRawImageDataFromNavMsg = (
imageData: ImageData,
width: number,
height: number,
dataSource: number[],
) => {
for (let row = 0; row < height; row++) {
for (let col = 0; col < width; col++) {
const mapI = col + (height - row - 1) * width;
const data = dataSource[mapI];
let i = (col + row * width) * 4;
const Uint8DV = new DataView(Uint8Array.from([data]).buffer);
const val = Uint8DV.getUint8(0);
imageData.data[i] = val;
imageData.data[++i] = val;
imageData.data[++i] = val;
imageData.data[++i] = 255;
}
}
};
export const populateConstImageDataFromNavMsg = (
imageData: ImageData,
width: number,
height: number,
dataSource: number[],
) => {
for (let row = 0; row < height; row++) {
for (let col = 0; col < width; col++) {
const mapI = col + (height - row - 1) * width;
const data = dataSource[mapI];
const val = new DataView(Uint8Array.from([data]).buffer).getUint8(0);
let i = (col + row * width) * 4;
if (val === 0) {
imageData.data[i] = 0;
imageData.data[++i] = 0;
imageData.data[++i] = 0;
imageData.data[++i] = 0; // alpha
} else if (val >= 1 && val <= 98) {
// Blue to red spectrum for most normal cost values
const v = (255 * val) / 100;
imageData.data[i] = v; // red
imageData.data[++i] = 0; // green
imageData.data[++i] = 255 - v; // blue
imageData.data[++i] = 255; // alpha
} else if (val === 99) {
// inscribed obstacle values (99) in cyan
imageData.data[i] = 0; // red
imageData.data[++i] = 255; // green
imageData.data[++i] = 255; // blue
imageData.data[++i] = 255; // alpha
} else if (val === 100) {
// lethal obstacle values (100) in purple
imageData.data[i] = 255; // red
imageData.data[++i] = 0; // green
imageData.data[++i] = 255; // blue
imageData.data[++i] = 255; // alpha
} else if (val > 100 && val <= 127) {
// illegal positive values in green
imageData.data[i] = 0; // red
imageData.data[++i] = 255; // green
imageData.data[++i] = 0; // blue
imageData.data[++i] = 255; // alpha
} else if (val >= 128 && val <= 254) {
// illegal negative (char) values in shades of red/yellow
imageData.data[i] = 255; // red
imageData.data[++i] = (255 * (val - 128)) / (254 - 128); // green
imageData.data[++i] = 0; // blue
imageData.data[++i] = 255; // alpha
} else {
// legal -1 value is tasteful blueish greenish grayish color
imageData.data[i] = 0x70; // red
imageData.data[++i] = 0x89; // green
imageData.data[++i] = 0x86; // blue
imageData.data[++i] = 255; // alpha
}
}
}
};
export const imageDataToCanvas = (imageData: ImageData) => {
const canvas = document.createElement('canvas');
canvas.width = imageData.width;
canvas.height = imageData.height;
const context = canvas.getContext('2d');
context?.putImageData(imageData, 0, 0);
return canvas;
};
export const populateImageDataFromImageMsg = (
message: RosMessage.Image,
offset: number,
rgbaOrder: number[],
imageData: ImageData,
) => {
const { data: rawData, height, step } = message;
const typedArray = Uint8Array.from(rawData);
// endianness is required for > 8bit encodings
const encodedDataView = new DataView(typedArray.buffer);
let j = 0;
for (let i = 0; i < step * height; i += offset) {
for (let k = 0; k < rgbaOrder.length; k++) {
imageData.data[j++] = encodedDataView.getUint8(i + rgbaOrder[k]);
}
if (rgbaOrder.length === 3) {
imageData.data[j++] = 255;
}
}
};