Files: 0b29f5c16c806103ae9326149404b382ab3b7cfd / pngspark.c
1931 bytesRaw
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | static const size_t initial_size = 8; |
10 | |
11 | uint8_t parse_color(const char *color) |
12 | { |
13 | return !strcmp("black", color) ? 0x00 : 0x99; |
14 | } |
15 | |
16 | int pngspark_init(struct pngspark *ps, size_t height, const char *color) |
17 | { |
18 | ps->size = initial_size; |
19 | ps->num_values = 0; |
20 | ps->values = malloc(initial_size * sizeof(double)); |
21 | if (!ps->values) return 1; |
22 | ps->color = parse_color(color); |
23 | ps->height = height; |
24 | ps->max_value = 0; |
25 | return 0; |
26 | } |
27 | |
28 | int pngspark_append(struct pngspark *ps, double value) |
29 | { |
30 | size_t i = ps->num_values++; |
31 | if (i >= ps->size) { |
32 | ps->values = realloc(ps->values, sizeof(double) * (ps->size <<= 1)); |
33 | if (!ps->values) return 1; |
34 | } |
35 | ps->values[i] = value; |
36 | if (value > ps->max_value) |
37 | ps->max_value = value; |
38 | return 0; |
39 | } |
40 | |
41 | int pngspark_draw(struct pngspark *ps, uint8_t *data, size_t width, |
42 | size_t height) |
43 | { |
44 | double *values = ps->values; |
45 | uint8_t color = ps->color; |
46 | for (size_t x = 0; x < width; x++) { |
47 | size_t value = values[x] * height; |
48 | data[x] = value; |
49 | for (size_t y = 0; y < height; y++) |
50 | data[x * width + y] = 0x9f; |
51 | /* |
52 | printf("write value: %zu, i: %zu\n", value, 1 * width + x); |
53 | for (size_t y = value+1; y < height; y++) |
54 | data[y * width + x] = color; |
55 | */ |
56 | (void)value; |
57 | (void)color; |
58 | (void)data; |
59 | } |
60 | return 0; |
61 | } |
62 | |
63 | static size_t write_fd(const void *ptr, size_t size, size_t count, |
64 | void *userPtr) |
65 | { |
66 | return fwrite(ptr, size, count, (FILE *)userPtr); |
67 | } |
68 | |
69 | int pngspark_write(struct pngspark *ps, FILE *file) |
70 | { |
71 | LuImage *img = luImageCreate(ps->num_values, ps->max_value+1, 1, 8); |
72 | if (!img) return 1; |
73 | printf("size: %zu\n", img->dataSize); |
74 | |
75 | int ret = pngspark_draw(ps, img->data, ps->num_values, ps->max_value); |
76 | if (!ret) |
77 | ret = luPngWrite(write_fd, file, img); |
78 | luImageRelease(img); |
79 | return ret; |
80 | } |
81 | |
82 | int pngspark_end(struct pngspark *ps) |
83 | { |
84 | free(ps->values); |
85 | return 0; |
86 | } |
87 | |
88 |
Built with git-ssb-web