]> gitweb.ps.run Git - separator/blob - main.cpp
Update readem
[separator] / main.cpp
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
5 \r
6 #include <math.h>\r
7 \r
8 void doit(\r
9     const char *filenameIn,\r
10     const char *filenameOut,\r
11     stbi_uc r_fg_old,\r
12     stbi_uc g_fg_old,\r
13     stbi_uc b_fg_old,\r
14     stbi_uc r_bg,\r
15     stbi_uc g_bg,\r
16     stbi_uc b_bg,\r
17     stbi_uc r_fg_new,\r
18     stbi_uc g_fg_new,\r
19     stbi_uc b_fg_new)\r
20 {\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
24 \r
25     int x, y, n;\r
26     stbi_uc *img = stbi_load(filenameIn, &x, &y, &n, 0);\r
27 \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
35 \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
42             \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
46             img[index + 3] =\r
47                 (stbi_uc)(255.0*(1.0 - ((rNorm + gNorm + bNorm) / 3.0)));\r
48         }\r
49     }\r
50 \r
51     stbi_write_png(filenameOut, x, y, n, img, 0);\r
52 \r
53     stbi_image_free(img);\r
54 }\r
55 \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
62     return c0*16 + c1;\r
63 }\r
64 \r
65 int main(int argc, char **argv) {\r
66     if (argc != 6) {\r
67         fprintf(stderr, "Usage: %s filename_old filename_new fgcolor bgcolor fgcolor_new\n", argv[0]);\r
68         return 1;\r
69     }\r
70 \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
81     \r
82     doit(\r
83         argv[1], argv[2],\r
84         r_f, g_f, b_f,\r
85         r_b, g_b, b_b,\r
86         r_n, g_n, b_n);\r
87 \r
88     return 0;\r
89 }\r