Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

484 rader
14 KiB

  1. #include "config.h"
  2. #include "common.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <math.h>
  7. #include <pipi.h>
  8. #define R 0
  9. #define G 1
  10. #define B 2
  11. #define X 3
  12. #define Y 4
  13. #define A 5
  14. //#define debug printf
  15. #define debug(...) /* */
  16. #define BRIGHT(x) (0.299*(x)[0] + 0.587*(x)[1] + 0.114*(x)[2])
  17. #define MAXCOLORS 16
  18. #define STEPS 256
  19. #define EPSILON (0.000001)
  20. typedef struct
  21. {
  22. double pts[STEPS + 1][MAXCOLORS * (MAXCOLORS - 1) / 2][6];
  23. int hullsize[STEPS + 1];
  24. double bary[STEPS + 1][3];
  25. }
  26. hull_t;
  27. static double const y[3] = { .299, .587, .114 };
  28. static double u[3], v[3];
  29. static int ylen;
  30. /*
  31. * Find two base vectors for the chrominance planes.
  32. */
  33. static void init_uv(void)
  34. {
  35. double tmp;
  36. ylen = sqrt(y[R] * y[R] + y[G] * y[G] + y[B] * y[B]);
  37. u[R] = y[1];
  38. u[G] = -y[0];
  39. u[B] = 0;
  40. tmp = sqrt(u[R] * u[R] + u[G] * u[G] + u[B] * u[B]);
  41. u[R] /= tmp; u[G] /= tmp; u[B] /= tmp;
  42. v[R] = y[G] * u[B] - y[B] * u[G];
  43. v[G] = y[B] * u[R] - y[R] * u[B];
  44. v[B] = y[R] * u[G] - y[G] * u[R];
  45. tmp = sqrt(v[R] * v[R] + v[G] * v[G] + v[B] * v[B]);
  46. v[R] /= tmp; v[G] /= tmp; v[B] /= tmp;
  47. }
  48. /*
  49. * Compute the convex hull of a given palette.
  50. */
  51. static hull_t *compute_hull(int ncolors, double const *palette)
  52. {
  53. hull_t *ret = malloc(sizeof(hull_t));
  54. double tmp;
  55. int i, j;
  56. debug("\n### NEW HULL ###\n\n");
  57. debug("Analysing %i colors\n", ncolors);
  58. double pal[ncolors][3];
  59. for(i = 0; i < ncolors; i++)
  60. {
  61. pal[i][R] = palette[i * 3];
  62. pal[i][G] = palette[i * 3 + 1];
  63. pal[i][B] = palette[i * 3 + 2];
  64. debug(" [%i] (%g,%g,%g)\n", i, pal[i][R], pal[i][G], pal[i][B]);
  65. }
  66. /*
  67. * 1. Find the darkest and lightest colours
  68. */
  69. double *dark = NULL, *light = NULL;
  70. double min = 1.0, max = 0.0;
  71. for(i = 0; i < ncolors; i++)
  72. {
  73. double p = BRIGHT(pal[i]);
  74. if(p < min)
  75. {
  76. dark = pal[i];
  77. min = p;
  78. }
  79. if(p > max)
  80. {
  81. light = pal[i];
  82. max = p;
  83. }
  84. }
  85. double gray[3];
  86. gray[R] = light[R] - dark[R];
  87. gray[G] = light[G] - dark[G];
  88. gray[B] = light[B] - dark[B];
  89. debug(" gray axis (%g,%g,%g) - (%g,%g,%g)\n",
  90. dark[R], dark[G], dark[B], light[R], light[G], light[B]);
  91. /*
  92. * 3. Browse the grey axis and do stuff
  93. */
  94. int n;
  95. for(n = 0; n <= STEPS; n++)
  96. {
  97. double pts[ncolors * (ncolors - 1) / 2][5];
  98. double ptmp[5];
  99. #define SWAP(p1,p2) do { memcpy(ptmp, p1, sizeof(ptmp)); \
  100. memcpy(p1, p2, sizeof(ptmp)); \
  101. memcpy(p2, ptmp, sizeof(ptmp)); } while(0)
  102. double t = n * 1.0 / STEPS;
  103. int npts = 0;
  104. debug("Slice %i/%i\n", n, STEPS);
  105. double p0[3];
  106. p0[R] = dark[R] + t * gray[R];
  107. p0[G] = dark[G] + t * gray[G];
  108. p0[B] = dark[B] + t * gray[B];
  109. debug(" 3D gray (%g,%g,%g)\n", p0[R], p0[G], p0[B]);
  110. /*
  111. * 3.1. Find all edges that intersect the t.y + (u,v) plane
  112. */
  113. for(i = 0; i < ncolors; i++)
  114. {
  115. double k1[3];
  116. k1[R] = pal[i][R] - p0[R];
  117. k1[G] = pal[i][G] - p0[G];
  118. k1[B] = pal[i][B] - p0[B];
  119. tmp = sqrt(k1[R] * k1[R] + k1[G] * k1[G] + k1[B] * k1[B]);
  120. /* If k1.y > t.y.y, we don't want this point */
  121. double yk1 = y[R] * k1[R] + y[G] * k1[G] + y[B] * k1[B];
  122. if(yk1 > t * ylen * ylen + EPSILON)
  123. continue;
  124. for(j = 0; j < ncolors; j++)
  125. {
  126. if(i == j)
  127. continue;
  128. double k2[3];
  129. k2[R] = pal[j][R] - p0[R];
  130. k2[G] = pal[j][G] - p0[G];
  131. k2[B] = pal[j][B] - p0[B];
  132. tmp = sqrt(k2[R] * k2[R] + k2[G] * k2[G] + k2[B] * k2[B]);
  133. /* If k2.y < t.y.y, we don't want this point */
  134. double yk2 = y[R] * k2[R] + y[G] * k2[G] + y[B] * k2[B];
  135. if(yk2 < t * ylen * ylen - EPSILON)
  136. continue;
  137. if(yk2 < yk1)
  138. continue;
  139. double s = yk1 == yk2 ?
  140. 0.5 : (t * ylen * ylen - yk1) / (yk2 - yk1);
  141. pts[npts][R] = p0[R] + k1[R] + s * (k2[R] - k1[R]);
  142. pts[npts][G] = p0[G] + k1[G] + s * (k2[G] - k1[G]);
  143. pts[npts][B] = p0[B] + k1[B] + s * (k2[B] - k1[B]);
  144. npts++;
  145. }
  146. }
  147. /*
  148. * 3.2. Find the barycentre of these points' convex hull. We use
  149. * the Graham Scan technique.
  150. */
  151. /* Make our problem a 2-D problem. */
  152. for(i = 0; i < npts; i++)
  153. {
  154. pts[i][X] = (pts[i][R] - p0[R]) * u[R]
  155. + (pts[i][G] - p0[G]) * u[G]
  156. + (pts[i][B] - p0[B]) * u[B];
  157. pts[i][Y] = (pts[i][R] - p0[R]) * v[R]
  158. + (pts[i][G] - p0[G]) * v[G]
  159. + (pts[i][B] - p0[B]) * v[B];
  160. }
  161. /* Find the leftmost point */
  162. int left = -1;
  163. tmp = 10.;
  164. for(i = 0; i < npts; i++)
  165. if(pts[i][X] < tmp)
  166. {
  167. left = i;
  168. tmp = pts[i][X];
  169. }
  170. SWAP(pts[0], pts[left]);
  171. /* Sort the remaining points radially around pts[0]. Bubble sort
  172. * is okay for small sizes, I don't care. */
  173. for(i = 1; i < npts; i++)
  174. for(j = 1; j < npts - i; j++)
  175. {
  176. double k1 = (pts[j][X] - pts[0][X])
  177. * (pts[j + 1][Y] - pts[0][Y]);
  178. double k2 = (pts[j + 1][X] - pts[0][X])
  179. * (pts[j][Y] - pts[0][Y]);
  180. if(k1 < k2 - EPSILON)
  181. SWAP(pts[j], pts[j + 1]);
  182. else if(k1 < k2 + EPSILON)
  183. {
  184. /* Aligned! keep the farthest point */
  185. double ax = pts[j][X] - pts[0][X];
  186. double ay = pts[j][Y] - pts[0][Y];
  187. double bx = pts[j + 1][X] - pts[0][X];
  188. double by = pts[j + 1][Y] - pts[0][Y];
  189. if(ax * ax + ay * ay > bx * bx + by * by)
  190. SWAP(pts[j], pts[j + 1]);
  191. }
  192. }
  193. /* Remove points not in the convex hull */
  194. for(i = 2; i < npts; /* */)
  195. {
  196. if(i < 2)
  197. {
  198. i++;
  199. continue;
  200. }
  201. double k1 = (pts[i - 1][X] - pts[i - 2][X])
  202. * (pts[i][Y] - pts[i - 2][Y]);
  203. double k2 = (pts[i][X] - pts[i - 2][X])
  204. * (pts[i - 1][Y] - pts[i - 2][Y]);
  205. if(k1 <= k2 + EPSILON)
  206. {
  207. for(j = i - 1; j < npts - 1; j++)
  208. SWAP(pts[j], pts[j + 1]);
  209. npts--;
  210. }
  211. else
  212. i++;
  213. }
  214. /* FIXME: check the last point */
  215. for(i = 0; i < npts; i++)
  216. debug(" 2D pt[%i] (%g,%g)\n", i, pts[i][X], pts[i][Y]);
  217. /* Compute the barycentre coordinates */
  218. double ctx = 0., cty = 0., weight = 0.;
  219. for(i = 2; i < npts; i++)
  220. {
  221. double abx = pts[i - 1][X] - pts[0][X];
  222. double aby = pts[i - 1][Y] - pts[0][Y];
  223. double acx = pts[i][X] - pts[0][X];
  224. double acy = pts[i][Y] - pts[0][Y];
  225. double sqarea = (abx * abx + aby * aby) * (acx * acx + acy * acy)
  226. - (abx * acx + aby * acy) * (abx * acx + aby * acy);
  227. if(sqarea <= 0.)
  228. continue;
  229. double area = sqrt(sqarea);
  230. ctx += area * (abx + acx) / 3;
  231. cty += area * (aby + acy) / 3;
  232. weight += area;
  233. }
  234. if(weight > EPSILON)
  235. {
  236. ctx = pts[0][X] + ctx / weight;
  237. cty = pts[0][Y] + cty / weight;
  238. }
  239. else
  240. {
  241. int right = -1;
  242. tmp = -10.;
  243. for(i = 0; i < npts; i++)
  244. if(pts[i][X] > tmp)
  245. {
  246. right = i;
  247. tmp = pts[i][X];
  248. }
  249. ctx = 0.5 * (pts[0][X] + pts[right][X]);
  250. cty = 0.5 * (pts[0][Y] + pts[right][Y]);
  251. }
  252. debug(" 2D bary (%g,%g)\n", ctx, cty);
  253. /*
  254. * 3.3. Store the barycentre and convex hull information.
  255. */
  256. ret->bary[n][R] = p0[R] + ctx * u[R] + cty * v[R];
  257. ret->bary[n][G] = p0[G] + ctx * u[G] + cty * v[G];
  258. ret->bary[n][B] = p0[B] + ctx * u[B] + cty * v[B];
  259. for(i = 0; i < npts; i++)
  260. {
  261. ret->pts[n][i][R] = pts[i][R];
  262. ret->pts[n][i][G] = pts[i][G];
  263. ret->pts[n][i][B] = pts[i][B];
  264. ret->pts[n][i][X] = pts[i][X] - ctx;
  265. ret->pts[n][i][Y] = pts[i][Y] - cty;
  266. ret->pts[n][i][A] = atan2(pts[i][Y] - cty, pts[i][X] - ctx);
  267. debug(" 3D pt[%i] (%g,%g,%g) angle %g\n",
  268. i, pts[i][R], pts[i][G], pts[i][B], ret->pts[n][i][A]);
  269. }
  270. ret->hullsize[n] = npts;
  271. debug(" 3D bary (%g,%g,%g)\n",
  272. ret->bary[n][R], ret->bary[n][G], ret->bary[n][B]);
  273. }
  274. return ret;
  275. }
  276. int main(int argc, char *argv[])
  277. {
  278. static double const rgbpal[] =
  279. {
  280. 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1,
  281. 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1,
  282. };
  283. static double const mypal[] =
  284. {
  285. 0.900, 0.001, 0.001, /* red */
  286. 0.001, 0.800, 0.001, /* green */
  287. 0.005, 0.001, 0.500, /* blue */
  288. 0.900, 0.900, 0.900, /* white */
  289. 0.900, 0.900, 0.001, /* yellow */
  290. 0.800, 0.400, 0.001, /* orange */
  291. };
  292. int i, j;
  293. init_uv();
  294. hull_t *rgbhull = compute_hull(8, rgbpal);
  295. hull_t *myhull = compute_hull(6, mypal);
  296. /*
  297. * 4. Load image and change its palette.
  298. */
  299. debug("\n### PROCESSING IMAGE ###\n\n");
  300. pipi_image_t *src = pipi_load(argv[1]);
  301. pipi_pixels_t *srcp = pipi_getpixels(src, PIPI_PIXELS_RGBA_F);
  302. float *srcdata = (float *)srcp->pixels;
  303. int w = srcp->w, h = srcp->h;
  304. pipi_image_t *dst = pipi_new(w, h);
  305. pipi_pixels_t *dstp = pipi_getpixels(dst, PIPI_PIXELS_RGBA_F);
  306. float *dstdata = (float *)dstp->pixels;
  307. for(j = 0; j < h; j++)
  308. for(i = 0; i < w; i++)
  309. {
  310. double p[3];
  311. /* FIXME: Imlib fucks up the RGB order. */
  312. p[B] = srcdata[4 * (j * w + i)];
  313. p[G] = srcdata[4 * (j * w + i) + 1];
  314. p[R] = srcdata[4 * (j * w + i) + 2];
  315. debug("Pixel +%i+%i (%g,%g,%g)\n", i, j, p[R], p[G], p[B]);
  316. int slice = (int)(BRIGHT(p) * STEPS + 0.5);
  317. debug(" slice %i\n", slice);
  318. /* Convert to 2D. The origin is the slice's barycentre. */
  319. double xp = (p[R] - rgbhull->bary[slice][R]) * u[R]
  320. + (p[G] - rgbhull->bary[slice][G]) * u[G]
  321. + (p[B] - rgbhull->bary[slice][B]) * u[B];
  322. double yp = (p[R] - rgbhull->bary[slice][R]) * v[R]
  323. + (p[G] - rgbhull->bary[slice][G]) * v[G]
  324. + (p[B] - rgbhull->bary[slice][B]) * v[B];
  325. debug(" 2D pt (%g,%g)\n", xp, yp);
  326. /* 1. find the excentricity in RGB space. There is an easier
  327. * way to do this, which is to find the intersection of our
  328. * line with the RGB cube itself, but we'd lose the possibility
  329. * of having an original colour space other than RGB. */
  330. /* First, find the relevant triangle. */
  331. int n, count = rgbhull->hullsize[slice];
  332. double angle = atan2(yp, xp);
  333. for(n = 0; n < count; n++)
  334. {
  335. double a1 = rgbhull->pts[slice][n][A];
  336. double a2 = rgbhull->pts[slice][(n + 1) % count][A];
  337. if(a1 > a2)
  338. {
  339. if(angle >= a1)
  340. a2 += 2 * M_PI;
  341. else
  342. a1 -= 2 * M_PI;
  343. }
  344. if(angle >= a1 && angle <= a2)
  345. break;
  346. }
  347. /* Now compute the distance to the triangle's edge. If the edge
  348. * intersection is M, then t is such as P = t.M (can be zero) */
  349. double xa = rgbhull->pts[slice][n % count][X];
  350. double ya = rgbhull->pts[slice][n % count][Y];
  351. double xb = rgbhull->pts[slice][(n + 1) % count][X];
  352. double yb = rgbhull->pts[slice][(n + 1) % count][Y];
  353. double t = (xp * (yb - ya) - yp * (xb - xa)) / (xa * yb - xb * ya);
  354. debug(" best RGB %g (%g,%g) (%g,%g)\n", t, xa, ya, xb, yb);
  355. /* 2. apply the excentricity in reduced space. */
  356. count = myhull->hullsize[slice];
  357. for(n = 0; n < count; n++)
  358. {
  359. double a1 = myhull->pts[slice][n][A];
  360. double a2 = myhull->pts[slice][(n + 1) % count][A];
  361. if(a1 > a2)
  362. {
  363. if(angle >= a1)
  364. a2 += 2 * M_PI;
  365. else
  366. a1 -= 2 * M_PI;
  367. }
  368. if(angle >= a1 && angle <= a2)
  369. break;
  370. }
  371. /* If the edge intersection is M', s is such as P = s.M'. We
  372. * want P' = t.M' = t.P/s */
  373. xa = myhull->pts[slice][n % count][X];
  374. ya = myhull->pts[slice][n % count][Y];
  375. xb = myhull->pts[slice][(n + 1) % count][X];
  376. yb = myhull->pts[slice][(n + 1) % count][Y];
  377. double s = (xp * (yb - ya) - yp * (xb - xa)) / (xa * yb - xb * ya);
  378. debug(" best custom %g (%g,%g) (%g,%g)\n", s, xa, ya, xb, yb);
  379. if(s > 0)
  380. {
  381. xp *= t / s;
  382. yp *= t / s;
  383. }
  384. p[R] = myhull->bary[slice][R] + xp * u[R] + yp * v[R];
  385. p[G] = myhull->bary[slice][G] + xp * u[G] + yp * v[G];
  386. p[B] = myhull->bary[slice][B] + xp * u[B] + yp * v[B];
  387. /* Clipping should not be necessary, but the above code
  388. * is unfortunately not perfect. */
  389. if(p[R] < 0.0) p[R] = 0.0; else if(p[R] > 1.0) p[R] = 1.0;
  390. if(p[G] < 0.0) p[G] = 0.0; else if(p[G] > 1.0) p[G] = 1.0;
  391. if(p[B] < 0.0) p[B] = 0.0; else if(p[B] > 1.0) p[B] = 1.0;
  392. dstdata[4 * (j * w + i)] = p[B];
  393. dstdata[4 * (j * w + i) + 1] = p[G];
  394. dstdata[4 * (j * w + i) + 2] = p[R];
  395. }
  396. free(rgbhull);
  397. free(myhull);
  398. pipi_save(dst, argv[2]);
  399. return 0;
  400. }