summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Chabowski <kevin@kch42.de>2013-08-01 23:01:53 +0200
committerKevin Chabowski <kevin@kch42.de>2013-08-01 23:01:53 +0200
commit96919bf34222c0c3f37e1085342b81c63da386c8 (patch)
treea4e37d4a267dea3a8f2dee077533bc3fd6d8d222
parent8e128e5afd495c46ab54eaff2c602e420eb59c0d (diff)
downloadnebula2-96919bf34222c0c3f37e1085342b81c63da386c8.tar.gz
nebula2-96919bf34222c0c3f37e1085342b81c63da386c8.tar.bz2
nebula2-96919bf34222c0c3f37e1085342b81c63da386c8.zip
Moved BMP code to own file.
-rw-r--r--Makefile5
-rw-r--r--bmp.c54
-rw-r--r--bmp.h12
3 files changed, 69 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 76a46b1..e55ac71 100644
--- a/Makefile
+++ b/Makefile
@@ -6,8 +6,9 @@ SFMTFLAGS=-DHAVE_SSE2 -DSFMT_MEXP=19937
OPTIMIZE=-O3 -fno-strict-aliasing
#OPTIMIZE=-ggdb
-nebula2: nebula2.o config.o render.o statefile.o color.o mutex_helpers.o iniparser/libiniparser.a SFMT/SFMT.c
- $(CC) $(CFLAGS) $(OPTIMIZE) $(SFMTFLAGS) $(LIBS) -o nebula2 nebula2.o config.o render.o statefile.o color.o mutex_helpers.o iniparser/libiniparser.a SFMT/SFMT.c
+OBJECTS=nebula2.o config.o render.o statefile.o color.o mutex_helpers.o bmp.o
+nebula2: $(OBJECTS) iniparser/libiniparser.a SFMT/SFMT.c
+ $(CC) $(CFLAGS) $(OPTIMIZE) $(SFMTFLAGS) $(LIBS) -o nebula2 $(OBJECTS) iniparser/libiniparser.a SFMT/SFMT.c
iniparser/libiniparser.a:
make -C iniparser libiniparser.a
diff --git a/bmp.c b/bmp.c
new file mode 100644
index 0000000..9537704
--- /dev/null
+++ b/bmp.c
@@ -0,0 +1,54 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <errno.h>
+
+#include "color.h"
+#include "bmp.h"
+
+#define BYTES_PER_PIXEL 3
+#define OFFSET_bfSize 2
+#define OFFSET_biWidth 18
+#define OFFSET_biHeight 22
+#define HEADERSIZE 54
+
+static const char* header_template = "BM \0\0\0\0\x36\0\0\0\x28\0\0\0 \x01\0\x18\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
+
+int
+bmp_write_header(FILE* fh, int32_t width, int32_t height) {
+ char* header;
+ uint32_t filesize;
+
+ if(!(header = malloc(HEADERSIZE))) {
+ fputs("Could not allocate memory for BMP header.\n", stderr);
+ return 0;
+ }
+
+ filesize = HEADERSIZE + (width * height * BYTES_PER_PIXEL);
+ height *= -1;
+
+ memcpy(header, header_template, HEADERSIZE);
+ memcpy(header + OFFSET_bfSize, &filesize, 4);
+ memcpy(header + OFFSET_biWidth, &width, 4);
+ memcpy(header + OFFSET_biHeight, &height, 4);
+
+ if(fwrite(header, HEADERSIZE, 1, fh) != 1) {
+ fprintf(stderr, "Could not write BMP header: %s\n", strerror(errno));
+ free(header);
+ return 0;
+ }
+
+ free(header);
+ return 1;
+}
+
+int
+bmp_write_pixel(FILE* fh, color_t col) {
+ uint8_t pixel[3];
+ pixel[0] = col.b;
+ pixel[1] = col.g;
+ pixel[2] = col.r;
+
+ return (fwrite(pixel, 3, 1, fh) == 1);
+}
diff --git a/bmp.h b/bmp.h
new file mode 100644
index 0000000..e9b2821
--- /dev/null
+++ b/bmp.h
@@ -0,0 +1,12 @@
+#ifndef _nebula2_bmp_h_
+#define _nebula2_bmp_h_
+
+#include <stdio.h>
+#include <stdint.h>
+
+#include "color.h"
+
+extern int bmp_write_header(FILE* fh, int32_t width, int32_t height);
+extern int bmp_write_pixel(FILE* fh, color_t col);
+
+#endif \ No newline at end of file