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
|
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <curses.h>
#include "map.h"
#include "path.h"
#include "structs.h"
#include "priority_queue.h"
/* TODO: clean up, do defines or whatever */
void map_to_bmp(Map map, size_t map_width, size_t map_height, Position start, Position goal, Path path, char **visited, char *filename) {
/* Dimensions of the resulting image */
int32_t width = map_width;
int32_t height = map_height;
uint16_t bitcount = 24;
/* Width of a single row in bytes */
int width_in_bytes = ((width * bitcount + 31) / 32) * 4;
/* Size in bytes of the image, without the header */
uint32_t imagesize = width_in_bytes * height;
/* Size of the info header */
const uint32_t biSize = 40;
/* Bitmap bit offset */
const uint32_t bfOffBits = 54;
/* Size in bytes of resulting BMP */
uint32_t filesize = 54 + imagesize;
/* No. of planes */
const uint16_t biPlanes = 1;
/* The header */
unsigned char header[54] = { 0 };
memcpy(header, "BM", 2);
memcpy(header + 2 , &filesize, 4);
memcpy(header + 10, &bfOffBits, 4);
memcpy(header + 14, &biSize, 4);
memcpy(header + 18, &width, 4);
memcpy(header + 22, &height, 4);
memcpy(header + 26, &biPlanes, 2);
memcpy(header + 28, &bitcount, 2);
memcpy(header + 34, &imagesize, 4);
/* The buffer for image data */
unsigned char* buf = malloc(imagesize);
/* Draw the walls */
for(int row = height - 1; row >= 0; row--) {
for(int col = 0; col < width; col++) {
if (map[height - 1 - row][col] == WALL) {
buf[row * width_in_bytes + col * 3 + 0] = 255; /* b */
buf[row * width_in_bytes + col * 3 + 1] = 255; /* g */
buf[row * width_in_bytes + col * 3 + 2] = 255; /* r */
} else if (visited != NULL && visited[height - 1 - row][col]) {
buf[row * width_in_bytes + col * 3 + 0] = 0; /* b */
buf[row * width_in_bytes + col * 3 + 1] = 255; /* g */
buf[row * width_in_bytes + col * 3 + 2] = 0; /* r */
} else {
buf[row * width_in_bytes + col * 3 + 0] = 0; /* b */
buf[row * width_in_bytes + col * 3 + 1] = 0; /* g */
buf[row * width_in_bytes + col * 3 + 2] = 0; /* r */
}
}
}
/* Draw the path */
if (path != NULL) {
Position cur = goal;
while (cur.x != start.x || cur.y != start.y) {
buf[(height - cur.y - 1) * width_in_bytes + cur.x * 3 + 0] = 0; /* b */
buf[(height - cur.y - 1) * width_in_bytes + cur.x * 3 + 1] = 0; /* g */
buf[(height - cur.y - 1) * width_in_bytes + cur.x * 3 + 2] = 255; /* r */
cur = path[cur.y][cur.x];
}
}
/* Start */
buf[(height - start.y - 1) * width_in_bytes + start.x * 3 + 0] = 255; /* b */
buf[(height - start.y - 1) * width_in_bytes + start.x * 3 + 1] = 0; /* g */
buf[(height - start.y - 1) * width_in_bytes + start.x * 3 + 2] = 255; /* r */
/* Goal */
buf[(height - goal.y - 1) * width_in_bytes + goal.x * 3 + 0] = 0; /* b */
buf[(height - goal.y - 1) * width_in_bytes + goal.x * 3 + 1] = 255; /* g */
buf[(height - goal.y - 1) * width_in_bytes + goal.x * 3 + 2] = 255; /* r */
FILE *fout = fopen(filename, "wb");
if (fout == NULL) {
set_message("Couldn't open %s for writing", filename);
print_message(height);
return;
}
fwrite(header, 1, 54, fout);
fwrite((char*)buf, 1, imagesize, fout);
fclose(fout);
free(buf);
set_message("Saved to %s", filename);
print_message(height);
return;
}
|