1 | #include "config.h" |
---|
2 | #include "common.h" |
---|
3 | |
---|
4 | #include <stdio.h> |
---|
5 | #include <stdlib.h> |
---|
6 | #include <string.h> |
---|
7 | #include <math.h> |
---|
8 | |
---|
9 | #include <pipi.h> |
---|
10 | |
---|
11 | int main(int argc, char *argv[]) |
---|
12 | { |
---|
13 | static double const mypal[] = |
---|
14 | { |
---|
15 | #if 1 |
---|
16 | 0.800, 0.001, 0.001, /* red */ |
---|
17 | 0.001, 0.700, 0.001, /* green */ |
---|
18 | 0.005, 0.001, 0.600, /* blue */ |
---|
19 | 0.900, 0.900, 0.900, /* white */ |
---|
20 | 0.900, 0.900, 0.001, /* yellow */ |
---|
21 | 0.850, 0.450, 0.001, /* orange */ |
---|
22 | #else |
---|
23 | 1,1,1, |
---|
24 | 0,.05,.60, |
---|
25 | 1,0,0, |
---|
26 | .15,.76,0, |
---|
27 | .96,1,0, |
---|
28 | 1,.61,0, |
---|
29 | #endif |
---|
30 | }; |
---|
31 | #define NCOLORS ((int)(sizeof(mypal)/sizeof(*mypal)/3)) |
---|
32 | |
---|
33 | int x, y, w, h; |
---|
34 | |
---|
35 | pipi_image_t *src = pipi_load(argv[1]); |
---|
36 | pipi_image_t *dst = pipi_reduce(src, NCOLORS, mypal); |
---|
37 | |
---|
38 | pipi_pixels_t *p = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F); |
---|
39 | float *data = (float *)p->pixels; |
---|
40 | w = p->w; |
---|
41 | h = p->h; |
---|
42 | |
---|
43 | for(y = 0; y < h; y++) |
---|
44 | for(x = 0; x < w; x++) |
---|
45 | { |
---|
46 | double pr, pg, pb, qr, qg, qb, er, eg, eb; |
---|
47 | |
---|
48 | pr = data[4 * (y * w + x) + 2]; |
---|
49 | pg = data[4 * (y * w + x) + 1]; |
---|
50 | pb = data[4 * (y * w + x)]; |
---|
51 | |
---|
52 | double dist = 10.0; |
---|
53 | int n, best = -1; |
---|
54 | |
---|
55 | for(n = 0; n < NCOLORS; n++) |
---|
56 | { |
---|
57 | double dr, dg, db, d; |
---|
58 | |
---|
59 | dr = pr - mypal[n * 3]; |
---|
60 | dg = pg - mypal[n * 3 + 1]; |
---|
61 | db = pb - mypal[n * 3 + 2]; |
---|
62 | |
---|
63 | d = dr * dr + dg * dg + db * db; |
---|
64 | //d = 0.299 * dr * dr + 0.587 * dg * dg + 0.114 * db * db; |
---|
65 | |
---|
66 | if(d < dist) |
---|
67 | { |
---|
68 | best = n; |
---|
69 | dist = d; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | qr = mypal[best * 3]; |
---|
74 | qg = mypal[best * 3 + 1]; |
---|
75 | qb = mypal[best * 3 + 2]; |
---|
76 | |
---|
77 | er = (pr - qr) / 16; |
---|
78 | eg = (pg - qg) / 16; |
---|
79 | eb = (pb - qb) / 16; |
---|
80 | |
---|
81 | if(x < w - 1) |
---|
82 | { |
---|
83 | data[4 * (y * w + x + 1) + 2] += 7 * er; |
---|
84 | data[4 * (y * w + x + 1) + 1] += 7 * eg; |
---|
85 | data[4 * (y * w + x + 1)] += 7 * eb; |
---|
86 | } |
---|
87 | |
---|
88 | if(y < h - 1) |
---|
89 | { |
---|
90 | if(x < w - 1) |
---|
91 | { |
---|
92 | data[4 * (y * w + x + w + 1) + 2] += 1 * er; |
---|
93 | data[4 * (y * w + x + w + 1) + 1] += 1 * eg; |
---|
94 | data[4 * (y * w + x + w + 1)] += 1 * eb; |
---|
95 | } |
---|
96 | |
---|
97 | data[4 * (y * w + x + w) + 2] += 5 * er; |
---|
98 | data[4 * (y * w + x + w) + 1] += 5 * eg; |
---|
99 | data[4 * (y * w + x + w)] += 5 * eb; |
---|
100 | |
---|
101 | if(x > 0) |
---|
102 | { |
---|
103 | data[4 * (y * w + x + w - 1) + 2] += 3 * er; |
---|
104 | data[4 * (y * w + x + w - 1) + 1] += 3 * eg; |
---|
105 | data[4 * (y * w + x + w - 1)] += 3 * eb; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | data[4 * (y * w + x) + 2] = qr; |
---|
110 | data[4 * (y * w + x) + 1] = qg; |
---|
111 | data[4 * (y * w + x)] = qb; |
---|
112 | } |
---|
113 | |
---|
114 | pipi_save(dst, argv[2]); |
---|
115 | |
---|
116 | return 0; |
---|
117 | } |
---|
118 | |
---|