aboutsummaryrefslogtreecommitdiff
path: root/bmp.c
diff options
context:
space:
mode:
Diffstat (limited to 'bmp.c')
-rw-r--r--bmp.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/bmp.c b/bmp.c
new file mode 100644
index 0000000..14a8d61
--- /dev/null
+++ b/bmp.c
@@ -0,0 +1,101 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.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].parent;
+ }
+ }
+
+ /* 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");
+ fwrite(header, 1, 54, fout);
+ fwrite((char*)buf, 1, imagesize, fout);
+ fclose(fout);
+ free(buf);
+
+ return;
+}