-
Notifications
You must be signed in to change notification settings - Fork 4
/
wasm_main.c
45 lines (32 loc) · 1.16 KB
/
wasm_main.c
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
#include "repalette.h"
#define export extern __attribute__((visibility("default")))
#define PAGE 65536
static size_t memory_capacity = 0;
static u8 *memory = 0;
static void resize(size_t new_size) {
if (new_size > memory_capacity) {
if (memory_capacity == 0)
memory = (u8 *)(__builtin_wasm_memory_size(0) * PAGE);
size_t pages = (new_size - memory_capacity + PAGE - 1) / PAGE;
__builtin_wasm_memory_grow(0, pages);
memory_capacity += pages * PAGE;
}
}
size_t palette_size;
export void palette_clear(void) { palette_size = 0; }
export void palette_add(u8 red, u8 green, u8 blue) {
resize((palette_size + 1) * sizeof(Color));
Color c = {red, green, blue};
((Color *)memory)[palette_size++] = c;
}
export u8 *get_pixels(int width, int height) {
resize(4 * width * height + palette_size * sizeof(Color));
return memory + palette_size * sizeof(Color);
}
export void update_canvas(int width, int height, Ditherer ditherer) {
if (palette_size == 0) return;
u8 *pixels = memory + palette_size * sizeof(Color);
Color *palette = (Color *)memory;
Image img = {pixels, width, height, 4};
recolor(img, palette, palette_size, ditherer);
}