-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathRRDraw.java
458 lines (387 loc) · 14 KB
/
RRDraw.java
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package RRMap;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.BasicStroke;
import java.awt.geom.Line2D;
import java.awt.geom.Ellipse2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.color.ColorSpace;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class RRDraw extends JFrame {
/**
*
*/
private static final long serialVersionUID = 2623447051590306992L;
JFrame parent;
RRDrawPanel rrDrawPanel;
public RRDraw() {
super("RR Map Viewer");
setSize(350, 400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
parent = this;
rrDrawPanel = new RRDrawPanel();
Container c = getContentPane();
// The default BorderLayout will work better.
// c.setLayout(new FlowLayout());
JButton openButton = new JButton("Open");
final JLabel statusbar = new JLabel("Output of your selection will go here");
// File file = new File("C:\\temp\\map\\roboroommap7.gz");
// RRFileParser loadImage = loadImage(file);
// statusbar.setText(file.getName() + " size " + loadImage.getImgWidth() + "x" + loadImage.getImgHeight());
// rrDrawPanel.setSize(loadImage.getImgWidth(), loadImage.getImgHeight());
openButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser("images");
int option = chooser.showOpenDialog(parent);
if (option == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
RRFileParser loadImage = loadImage(file);
statusbar.setText(file.getName() + " size " + loadImage.getImgWidth() + "x" + loadImage.getImgHeight());
rrDrawPanel.setSize(loadImage.getImgWidth(), loadImage.getImgHeight());
} else {
statusbar.setText("You cancelled.");
}
}
});
JPanel north = new JPanel();
north.add(openButton);
north.add(statusbar);
north.setBackground(Color.GRAY);
north.setForeground(Color.BLUE);
c.add(north, "First");
c.add(new JScrollPane(rrDrawPanel), "Center");
}
/**
* load Gzipped RR file
*/
private RRFileParser loadImage(File file) {
try {
byte[] inputdata = readGZFile(file);
RRFileParser rf = new RRFileParser(inputdata);
System.out.println(rf.toSting());
int l = (int) rf.getImgHeight();
int w = (int) rf.getImgWidth();
byte[] buffer = rf.getImage();
rrDrawPanel.setRRFileDecoder(rf);
setSize(w, l);
return rf;
} catch (IOException e) {
System.out.println("read error: " + e.getMessage());
}
return null;
}
public static void main(String args[]) {
System.setProperty("swing.defaultlaf", "javax.swing.plaf.metal.MetalLookAndFeel");
RRDraw vc = new RRDraw();
vc.setVisible(true);
}
public byte[] readGZFile(File file) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (GZIPInputStream in = new GZIPInputStream(new FileInputStream(file))) {
int bufsize = 1024;
byte[] buf = new byte[bufsize];
int readbytes = 0;
readbytes = in.read(buf);
while (readbytes != -1) {
baos.write(buf, 0, readbytes);
readbytes = in.read(buf);
}
baos.flush();
return baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
}
class RRDrawPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 8791558011928073284L;
BufferedImage image;
Dimension size = new Dimension();
private RRFileParser rf;
public RRDrawPanel() {
}
public void setRRFileDecoder(RRFileParser rf) {
this.rf = rf;
}
void drawMap(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
if (rf != null) {
int z = 0;
for (int y = 0; y < rf.getImgHeight() - 1; y++) {
for (int x = 0; x < rf.getImgWidth() - 1; x++) {
// int type = rf.getImage()[x] ;
switch (rf.getImage()[x + rf.getImgWidth() * y]) {
case 0x00:
g2d.setColor(new Color(19, 87, 148));
break;
case 0x01:
g2d.setColor(new Color(100, 196, 254));
break;
default:
g2d.setColor(new Color(32, 115, 185));
break;
}
float Xpos = (float) 1024 - (x + rf.getLeft()) - rf.getLeft();
float ypos = (float) y;
g2d.draw(new Line2D.Float(Xpos, ypos, Xpos, ypos));
z++;
}
}
}
}
void drawPath(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
float prvX = 0;
float prvY = 0;
g2d.setColor(Color.YELLOW);
g2d.setColor(new Color(147, 194, 238));
for (Float[] point : rf.getRoboPath()) {
if (prvX > 1) {
g2d.draw(new Line2D.Float(prvX, prvY, point[0], point[1]));
}
prvX = point[0];
prvY = point[1];
}
}
void drawRobo(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int radius = 3;
Stroke stroke = new BasicStroke(3f);
g2d.setStroke(stroke);
g2d.setColor(new Color(75, 235, 149));
g2d.draw(new Ellipse2D.Double(rf.getRoboX() - radius, rf.getRoboY() - radius, 2.0 * radius, 2.0 * radius));
radius = 6;
g2d.setColor(Color.YELLOW);
g2d.draw(
new Ellipse2D.Double(rf.getChargerX() - radius, rf.getChargerY() - radius, 2.0 * radius, 2.0 * radius));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// g.drawImage(image, 0, 0, this);
if (rf != null) {
drawMap(g);
drawPath(g);
drawRobo(g);
}
}
@Override
public Dimension getPreferredSize() {
return size;
}
public void setImage(BufferedImage bi) {
image = bi;
setComponentSize();
repaint();
}
private void setComponentSize() {
if (image != null) {
size.width = image.getWidth();
size.height = image.getHeight();
revalidate(); // signal parent/scrollpane
}
}
}
class RRFileParser {
final static int CHARGER = 1;
final static int IMAGE = 2;
final static int PATH = 3;
final static int GOTO_PATH = 4;
final static int GOTO_PREDICTED_PATH = 5;
final static int CURRENTLY_CLEANED_ZONES = 6;
final static int GOTO_TARGET = 7;
final static int ROBOT_POSITION = 8;
final static int NO_GO_AREAS = 9;
final static int VIRTUAL_WALLS = 10;
final static int DIGEST = 1024;
private byte[] image;
private int majorVersion;
private int minorVersion;
private int mapIndex;
private int mapSequence;
private int imgHeight;
private int imgWidth;
private int imagesize;
private int chargerX;
private int chargerY;
private int roboX;
private int roboY;
private int top;
private int left;
private int setPointLength;
private int setPointSize;
private int setAngle;
private ArrayList<Float[]> roboPath = new ArrayList<Float[]>();
final static float MM = 50.0f;
final static int SIZE = 1024;
public RRFileParser(byte[] raw) {
this.majorVersion = getUInt16(raw, 0x08);
this.minorVersion = getUInt16(raw, 0x0A);
this.mapIndex = getUInt32LE(raw, 0x0C);
this.mapSequence = getUInt32LE(raw, 0x10);
long nextpos = 0x14; // startpos first block after the main header
while (nextpos < raw.length) {
byte[] header = getBytes(raw, (int) nextpos, 0x20);
int blocktype = getUInt16(getBytes(header, 0x00, 2));
long l = getUInt32LE(header, 0x04);
switch (blocktype) {
case CHARGER:
this.chargerX = getUInt32LE(header, 0x08);
this.chargerY = getUInt32LE(header, 0x0C);
break;
case IMAGE:
this.setImgHeight((getUInt32LE(header, 0x10)));
this.top = getUInt32LE(header, 0x08);
this.left = getUInt32LE(header, 0x0C);
this.setImgWidth((getUInt32LE(header, 0x14)));
this.setImagesize(getUInt32LE(header, 0x04));
this.image = java.util.Arrays.copyOfRange(raw, (int) (nextpos + 0x18), (int) imagesize);
break;
case PATH:
int pairs = getUInt32LE(header, 0x04) / 4;
this.setPointLength = getUInt32LE(header, 0x08);
this.setPointSize = getUInt32LE(header, 0x0C);
this.setAngle = getUInt32LE(header, 0x10);
long startpos = 0x14 + nextpos;
for (int pathpair = 0; pathpair < pairs; pathpair++) {
Float x = (float) SIZE - (getUInt16(getBytes(raw, (int) (startpos + pathpair * 4), 2))) / MM - left;
Float y = getUInt16(getBytes(raw, (int) (startpos + pathpair * 4 + 2), 2)) / MM - top;
roboPath.add(new Float[] { x, y });
}
break;
case ROBOT_POSITION:
this.roboX = getUInt32LE(header, 0x08);
this.roboY = getUInt32LE(header, 0x0C);
break;
case GOTO_PATH:
case GOTO_PREDICTED_PATH:
case CURRENTLY_CLEANED_ZONES:
case GOTO_TARGET:
case NO_GO_AREAS:
case VIRTUAL_WALLS:
case DIGEST:
System.out.print("Unimplemented blocktype: ");
System.out.println(blocktype);
break;
default: {
System.out.print("Unknown blocktype: ");
System.out.println(blocktype);
}
}
nextpos = nextpos + l + (header[2] & 0xFF);
}
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
public byte[] getBytes(byte[] raw, int pos, int len) {
return java.util.Arrays.copyOfRange(raw, pos, pos + len);
}
public int getUInt32LE(byte[] bytes) {
return getUInt32LE(bytes, 0);
}
public int getUInt32LE(byte[] bytes, int pos) {
int value = bytes[0 + pos] & 0xFF;
value |= (bytes[1 + pos] << 8) & 0xFFFF;
value |= (bytes[2 + pos] << 16) & 0xFFFFFF;
value |= (bytes[3 + pos] << 24) & 0xFFFFFFFF;
return value;
}
public int getUInt16(byte[] bytes) {
return getUInt16(bytes, 0);
}
public int getUInt16(byte[] bytes, int pos) {
int value = bytes[0 + pos] & 0xFF;
value |= (bytes[1 + pos] << 8) & 0xFFFF;
return value;
}
public int getImagesize() {
return imagesize;
}
public void setImagesize(int imagesize) {
this.imagesize = imagesize;
}
public int getImgHeight() {
return imgHeight;
}
public void setImgHeight(int imgHight) {
this.imgHeight = imgHight;
}
public int getImgWidth() {
return imgWidth;
}
public void setImgWidth(int imgWidth) {
this.imgWidth = imgWidth;
}
public int getTop() {
return top;
}
public int getLeft() {
return left;
}
public int getValue() {
return value;
}
public ArrayList<Float[]> getRoboPath() {
return roboPath;
}
public float getRoboX() {
return (float) SIZE - (roboX / MM) - left;
}
public float getRoboY() {
return (float) roboY / MM - top;
}
public float getChargerX() {
return (float) SIZE - (chargerX / MM) - left;
}
public float getChargerY() {
return (float) chargerY / MM - top;
}
public String toSting() {
String s = "ImageData: Major Version: " + majorVersion + " Minor version: " + minorVersion + " map Index: "
+ mapIndex + " Map Sequence: " + mapSequence + " \r\n" + "Image Size: " + Long.toString(imagesize)
+ " top: " + Long.toString(top) + " left: " + Long.toString(left) + " height: "
+ Long.toString(imgHeight) + " width: " + Long.toString(imgWidth) + "\r\n" + "Charger X:"
+ Long.toString(chargerX) + " Charger Y:" + Long.toString(chargerY) + "\r\n" + "Robo X:"
+ Long.toString(roboX) + " Robo Y:" + Long.toString(roboY) + "\r\n" + "Path: Pairs:"
+ Integer.toString(roboPath.size()) + " PointLenght: " + Integer.toString(setPointLength)
+ " PointSize: " + Integer.toString(setPointSize) + " Angle: " + Integer.toString(setAngle);
return s;
}
}