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