aboutsummaryrefslogtreecommitdiff
path: root/lib/ff
diff options
context:
space:
mode:
Diffstat (limited to 'lib/ff')
-rw-r--r--lib/ff/ff.c73
-rw-r--r--lib/ff/ff.h25
2 files changed, 98 insertions, 0 deletions
diff --git a/lib/ff/ff.c b/lib/ff/ff.c
new file mode 100644
index 0000000..aa85073
--- /dev/null
+++ b/lib/ff/ff.c
@@ -0,0 +1,73 @@
+#include "ff.h"
+#include "../io/io.h"
+#include "../cstr/cstr.h"
+#include "../malloc/malloc.h"
+
+#define MAGIC_VALUE "farbfeld"
+
+u32 conv_to_int(u32 src)
+{
+ return ((src & 0xff) << 24) | ((src & 0xff00) << 8) | ((src & 0xff0000) >> 8) | ((src & 0xff000000) >> 24);
+}
+
+u32 read_u32_from_fd(int fd)
+{
+ u32 src;
+ read(fd, (char *)&src, 4);
+
+ return ((src & 0xff) << 24) | ((src & 0xff00) << 8) | ((src & 0xff0000) >> 8) | ((src & 0xff000000) >> 24);
+}
+
+
+image_t *new_image_from_fd(int fd)
+{
+ image_t *img = malloc(sizeof(image_t));
+ char magic[9] = { 0 };
+
+ read(fd, magic, 8);
+
+ if (cstr_compare(magic, MAGIC_VALUE) != 0) {
+ free(img);
+ return 0;
+ }
+
+ img->width = read_u32_from_fd(fd);
+ img->height = read_u32_from_fd(fd);
+
+ if (img->width * img->height == 0) {
+ free(img);
+ return 0;
+ }
+
+ img->data = malloc(sizeof(image_pixel_t) * img->width * img->height);
+ u64 s = 0;
+ u64 rs = 0;
+ while ((rs = read(fd, (char*)(img->data) + s, sizeof(image_pixel_t) * img->width * img->height - s))) {
+ s += rs;
+ }
+
+ return img;
+}
+
+
+void free_image(image_t *img)
+{
+ free(img->data);
+ free(img);
+}
+
+canvas_pixel_t image_pixel_to_canvas_pixel(image_pixel_t p)
+{
+ canvas_pixel_t cp;
+
+ cp.r = p.r;
+ cp.g = p.g;
+ cp.b = p.b;
+ cp.a = p.a;
+
+ cp.r = (long double)(cp.r) * ((long double)(p.a) / 0xffff);
+ cp.g = (long double)(cp.g) * ((long double)(p.a) / 0xffff);
+ cp.b = (long double)(cp.b) * ((long double)(p.a) / 0xffff);
+
+ return cp;
+}
diff --git a/lib/ff/ff.h b/lib/ff/ff.h
new file mode 100644
index 0000000..756bd16
--- /dev/null
+++ b/lib/ff/ff.h
@@ -0,0 +1,25 @@
+#ifndef FARBFELD_H
+#define FARBFELD_H
+
+#include "../sys/sizes.h"
+#include "../fb/fb.h"
+
+typedef struct {
+ u16 r;
+ u16 g;
+ u16 b;
+ u16 a;
+} image_pixel_t;
+
+typedef struct {
+ u32 width;
+ u32 height;
+ image_pixel_t *data;
+} image_t;
+
+
+image_t *new_image_from_fd(int fd);
+void free_image(image_t*);
+canvas_pixel_t image_pixel_to_canvas_pixel(image_pixel_t p);
+
+#endif