blob: aa85073fcd4a92504896f96170c9c4df289233fc (
plain)
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
|
#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;
}
|