1 #define STB_IMAGE_IMPLEMENTATION
\r
2 #define STB_IMAGE_WRITE_IMPLEMENTATION
\r
3 #include "stb_image.h"
\r
4 #include "stb_image_write.h"
\r
9 const char *filenameIn,
\r
10 const char *filenameOut,
\r
21 stbi_uc rDiff = abs(r_bg - r_fg_old);
\r
22 stbi_uc gDiff = abs(g_bg - g_fg_old);
\r
23 stbi_uc bDiff = abs(b_bg - b_fg_old);
\r
26 stbi_uc *img = stbi_load(filenameIn, &x, &y, &n, 0);
\r
28 for (int i = 0; i < x; i++) {
\r
29 for (int j = 0; j < y; j++) {
\r
30 size_t index = (j*x + i) * n;
\r
31 stbi_uc r = img[index + 0];
\r
32 stbi_uc g = img[index + 1];
\r
33 stbi_uc b = img[index + 2];
\r
34 stbi_uc a = img[index + 3];
\r
36 stbi_uc rAbs = abs(r - r_fg_old);
\r
37 double rNorm = (double)rAbs / rDiff;
\r
38 stbi_uc gAbs = abs(g - g_fg_old);
\r
39 double gNorm = (double)gAbs / gDiff;
\r
40 stbi_uc bAbs = abs(b - b_fg_old);
\r
41 double bNorm = (double)bAbs / bDiff;
\r
43 img[index + 0] = r_fg_new;
\r
44 img[index + 1] = g_fg_new;
\r
45 img[index + 2] = b_fg_new;
\r
47 (stbi_uc)(255.0*(1.0 - ((rNorm + gNorm + bNorm) / 3.0)));
\r
51 stbi_write_png(filenameOut, x, y, n, img, 0);
\r
53 stbi_image_free(img);
\r
56 stbi_uc hex(char *c) {
\r
57 stbi_uc c0 = 0, c1 = 0;
\r
58 if (c[0] >= '0' && c[0] <= '9') c0 = c[0] - '0';
\r
59 else if (c[0] >= 'a' && c[0] <= 'f') c0 = c[0] - 'a' + 10;
\r
60 if (c[1] >= '0' && c[1] <= '9') c1 = c[1] - '0';
\r
61 else if (c[1] >= 'a' && c[1] <= 'f') c1 = c[1] - 'a' + 10;
\r
65 int main(int argc, char **argv) {
\r
67 fprintf(stderr, "Usage: %s filename_old filename_new fgcolor bgcolor fgcolor_new\n", argv[0]);
\r
71 // rgb, foreground and background and new foreground
\r
72 stbi_uc r_f = hex(&argv[3][0]);
\r
73 stbi_uc g_f = hex(&argv[3][2]);
\r
74 stbi_uc b_f = hex(&argv[3][4]);
\r
75 stbi_uc r_b = hex(&argv[4][0]);
\r
76 stbi_uc g_b = hex(&argv[4][2]);
\r
77 stbi_uc b_b = hex(&argv[4][4]);
\r
78 stbi_uc r_n = hex(&argv[5][0]);
\r
79 stbi_uc g_n = hex(&argv[5][2]);
\r
80 stbi_uc b_n = hex(&argv[5][4]);
\r