brewery
notashelf /
237a013e0331cc6f497559079d17f4659ceecd8e

chroma

public

Lightweight wallpaper daemon for Wayland

Star0tarball
clone
ssh://git@git.notashelf.dev:33/notashelf/chroma.git
lib/test_common.cC190 lines5.0 KB
1
#define STB_IMAGE_IMPLEMENTATION
2
#define STB_IMAGE_WRITE_IMPLEMENTATION
3
#include "test_common.h"
4
#include "stb_image.h"
5
#include "stb_image_write.h"
6
#include <math.h>
7
#include <sys/time.h>
8
9
uint8_t *load_image(const char *path, int *width, int *height, int *channels) {
10
    return stbi_load(path, width, height, channels, 4);
11
}
12
13
int save_image(const char *path, uint8_t *data, int width, int height, int channels) {
14
    return stbi_write_png(path, width, height, channels, data, width * channels);
15
}
16
17
uint8_t *downsample_image(uint8_t *src, int sw, int sh, int sc, int *dw, int *dh, float scale) {
18
    if (!src || sw <= 0 || sh <= 0 || sc <= 0 || scale <= 0) {
19
        if (dw) *dw = 0;
20
        if (dh) *dh = 0;
21
        return NULL;
22
    }
23
24
    int tw = (int)(sw * scale);
25
    int th = (int)(sh * scale);
26
27
    if (tw < 1) tw = 1;
28
    if (th < 1) th = 1;
29
30
    uint8_t *dst = malloc(tw * th * sc);
31
    if (!dst) return NULL;
32
33
    float inv_scale = 1.0f / scale;
34
    for (int y = 0; y < th; y++) {
35
        for (int x = 0; x < tw; x++) {
36
            float sx = (x + 0.5f) * inv_scale;
37
            float sy = (y + 0.5f) * inv_scale;
38
39
            int ix = (int)sx;
40
            int iy = (int)sy;
41
42
            if (sw > 1) {
43
                ix = (ix < sw - 2) ? ix : sw - 2;
44
            } else {
45
                ix = 0;
46
            }
47
            if (sh > 1) {
48
                iy = (iy < sh - 2) ? iy : sh - 2;
49
            } else {
50
                iy = 0;
51
            }
52
53
            if (ix < 0) ix = 0;
54
            if (iy < 0) iy = 0;
55
56
            float fx = sx - ix;
57
            float fy = sy - iy;
58
59
            if (sw == 1) fx = 0.0f;
60
            if (sh == 1) fy = 0.0f;
61
62
            for (int c = 0; c < sc; c++) {
63
                uint8_t p00 = src[(iy * sw + ix) * sc + c];
64
                uint8_t p01 = (sw > 1) ? src[(iy * sw + ix + 1) * sc + c] : p00;
65
                uint8_t p10 = (sh > 1) ? src[((iy + 1) * sw + ix) * sc + c] : p00;
66
                uint8_t p11 = (sw > 1 && sh > 1) ? src[((iy + 1) * sw + ix + 1) * sc + c] : p00;
67
68
                float interp = p00 * (1 - fx) * (1 - fy) +
69
                              p01 * fx * (1 - fy) +
70
                              p10 * (1 - fx) * fy +
71
                              p11 * fx * fy;
72
73
                dst[(y * tw + x) * sc + c] = (uint8_t)(interp + 0.5f);
74
            }
75
        }
76
    }
77
78
    *dw = tw;
79
    *dh = th;
80
    return dst;
81
}
82
83
double get_time_ms(void) {
84
    struct timeval tv;
85
    gettimeofday(&tv, NULL);
86
    return tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0;
87
}
88
89
double run_benchmark(double (*fn)(void), int iterations) {
90
    double warmup = fn();
91
    (void)warmup;
92
93
    double total = 0.0;
94
    for (int i = 0; i < iterations; i++) {
95
        total += fn();
96
    }
97
    return total / iterations;
98
}
99
100
int compare_images(uint8_t *a, uint8_t *b, int w, int h, int ch, float threshold) {
101
    int max_diff = 0;
102
    for (int i = 0; i < w * h * ch; i++) {
103
        int diff = abs((int)a[i] - (int)b[i]);
104
        if (diff > max_diff) max_diff = diff;
105
        if (diff > (int)(threshold * 255.0f)) {
106
            return 0;
107
        }
108
    }
109
    return 1;
110
}
111
112
float compute_psnr(uint8_t *a, uint8_t *b, int w, int h, int ch) {
113
    double mse = 0.0;
114
    int total = w * h * ch;
115
116
    for (int i = 0; i < total; i++) {
117
        double diff = (double)a[i] - (double)b[i];
118
        mse += diff * diff;
119
    }
120
121
    mse /= total;
122
123
    if (mse < 1e-10) return 99.99f;
124
125
    return (float)(10.0 * log10(255.0 * 255.0 / mse));
126
}
127
128
uint8_t *create_gradient_image(int w, int h) {
129
    uint8_t *img = malloc(w * h * 4);
130
    if (!img) return NULL;
131
132
    for (int y = 0; y < h; y++) {
133
        for (int x = 0; x < w; x++) {
134
            int idx = (y * w + x) * 4;
135
            img[idx + 0] = (uint8_t)((float)x / w * 255);
136
            img[idx + 1] = (uint8_t)((float)y / h * 255);
137
            img[idx + 2] = 128;
138
            img[idx + 3] = 255;
139
        }
140
    }
141
    return img;
142
}
143
144
uint8_t *create_noise_image(int w, int h, unsigned int seed) {
145
    uint8_t *img = malloc(w * h * 4);
146
    if (!img) return NULL;
147
148
    srand(seed);
149
    for (int i = 0; i < w * h * 4; i++) {
150
        img[i] = (uint8_t)(rand() % 256);
151
    }
152
    return img;
153
}
154
155
uint8_t *create_uniform_image(int w, int h, uint8_t r, uint8_t g, uint8_t b) {
156
    uint8_t *img = malloc(w * h * 4);
157
    if (!img) return NULL;
158
159
    for (int i = 0; i < w * h; i++) {
160
        img[i * 4 + 0] = r;
161
        img[i * 4 + 1] = g;
162
        img[i * 4 + 2] = b;
163
        img[i * 4 + 3] = 255;
164
    }
165
    return img;
166
}
167
168
uint8_t *create_checkerboard(int w, int h, int check_size) {
169
    uint8_t *img = malloc(w * h * 4);
170
    if (!img) return NULL;
171
172
    for (int y = 0; y < h; y++) {
173
        for (int x = 0; x < w; x++) {
174
            int idx = (y * w + x) * 4;
175
            int cx = x / check_size;
176
            int cy = y / check_size;
177
            if ((cx + cy) % 2 == 0) {
178
                img[idx + 0] = 255;
179
                img[idx + 1] = 255;
180
                img[idx + 2] = 255;
181
            } else {
182
                img[idx + 0] = 0;
183
                img[idx + 1] = 0;
184
                img[idx + 2] = 0;
185
            }
186
            img[idx + 3] = 255;
187
        }
188
    }
189
    return img;
190
}