#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; }