Super Mario 64s source code (from a leak on 4chan so be careful)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

707 lines
17 KiB

5 years ago
  1. /*
  2. ExoQuant v0.7
  3. Copyright (c) 2004 Dennis Ranke
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do
  9. so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #include "exoquant.h"
  21. #include <malloc.h>
  22. #include <math.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #ifndef NULL
  26. #define NULL (0)
  27. #endif
  28. #define SCALE_R 1.0f
  29. #define SCALE_G 1.2f
  30. #define SCALE_B 0.8f
  31. #define SCALE_A 1.0f
  32. exq_data *exq_init()
  33. {
  34. int i;
  35. exq_data *pExq;
  36. pExq = (exq_data*)malloc(sizeof(exq_data));
  37. for(i = 0; i < EXQ_HASH_SIZE; i++)
  38. pExq->pHash[i] = NULL;
  39. pExq->numColors = 0;
  40. pExq->optimized = 0;
  41. pExq->transparency = 1;
  42. pExq->numBitsPerChannel = 8;
  43. return pExq;
  44. }
  45. void exq_no_transparency(exq_data *pExq)
  46. {
  47. pExq->transparency = 0;
  48. }
  49. void exq_free(exq_data *pExq)
  50. {
  51. int i;
  52. exq_histogram *pCur, *pNext;
  53. for(i = 0; i < EXQ_HASH_SIZE; i++)
  54. for(pCur = pExq->pHash[i]; pCur != NULL; pCur = pNext)
  55. {
  56. pNext = pCur->pNextInHash;
  57. free(pCur);
  58. }
  59. free(pExq);
  60. }
  61. static unsigned int exq_make_hash(unsigned int rgba)
  62. {
  63. rgba -= (rgba >> 13) | (rgba << 19);
  64. rgba -= (rgba >> 13) | (rgba << 19);
  65. rgba -= (rgba >> 13) | (rgba << 19);
  66. rgba -= (rgba >> 13) | (rgba << 19);
  67. rgba -= (rgba >> 13) | (rgba << 19);
  68. rgba &= EXQ_HASH_SIZE - 1;
  69. return rgba;
  70. }
  71. void exq_feed(exq_data *pExq, unsigned char *pData, int nPixels)
  72. {
  73. int i;
  74. unsigned int hash;
  75. unsigned char r, g, b, a;
  76. exq_histogram *pCur;
  77. unsigned char channelMask = 0xff00 >> pExq->numBitsPerChannel;
  78. for(i = 0; i < nPixels; i++)
  79. {
  80. r = *pData++; g = *pData++; b = *pData++; a = *pData++;
  81. hash = exq_make_hash(((unsigned int)r) | (((unsigned int)g) << 8) | (((unsigned int)b) << 16) | (((unsigned int)a) << 24));
  82. pCur = pExq->pHash[hash];
  83. while(pCur != NULL && (pCur->ored != r || pCur->ogreen != g ||
  84. pCur->oblue != b || pCur->oalpha != a))
  85. pCur = pCur->pNextInHash;
  86. if(pCur != NULL)
  87. pCur->num++;
  88. else
  89. {
  90. pCur = (exq_histogram*)malloc(sizeof(exq_histogram));
  91. pCur->pNextInHash = pExq->pHash[hash];
  92. pExq->pHash[hash] = pCur;
  93. pCur->ored = r; pCur->ogreen = g; pCur->oblue = b; pCur->oalpha = a;
  94. r &= channelMask; g &= channelMask; b &= channelMask;
  95. pCur->color.r = r / 255.0f * SCALE_R;
  96. pCur->color.g = g / 255.0f * SCALE_G;
  97. pCur->color.b = b / 255.0f * SCALE_B;
  98. pCur->color.a = a / 255.0f * SCALE_A;
  99. if(pExq->transparency)
  100. {
  101. pCur->color.r *= pCur->color.a;
  102. pCur->color.g *= pCur->color.a;
  103. pCur->color.b *= pCur->color.a;
  104. }
  105. pCur->num = 1;
  106. pCur->palIndex = -1;
  107. pCur->ditherScale.r = pCur->ditherScale.g = pCur->ditherScale.b =
  108. pCur->ditherScale.a = -1;
  109. pCur->ditherIndex[0] = pCur->ditherIndex[1] = pCur->ditherIndex[2] =
  110. pCur->ditherIndex[3] = -1;
  111. }
  112. }
  113. }
  114. void exq_quantize(exq_data *pExq, int nColors)
  115. {
  116. exq_quantize_ex(pExq, nColors, 0);
  117. }
  118. void exq_quantize_hq(exq_data *pExq, int nColors)
  119. {
  120. exq_quantize_ex(pExq, nColors, 1);
  121. }
  122. void exq_quantize_ex(exq_data *pExq, int nColors, int hq)
  123. {
  124. int besti;
  125. exq_float beste;
  126. exq_histogram *pCur, *pNext;
  127. int i, j;
  128. if(nColors > 256)
  129. nColors = 256;
  130. if(pExq->numColors == 0)
  131. {
  132. pExq->node[0].pHistogram = NULL;
  133. for(i = 0; i < EXQ_HASH_SIZE; i++)
  134. for(pCur = pExq->pHash[i]; pCur != NULL; pCur = pCur->pNextInHash)
  135. {
  136. pCur->pNext = pExq->node[0].pHistogram;
  137. pExq->node[0].pHistogram = pCur;
  138. }
  139. exq_sum_node(&pExq->node[0]);
  140. pExq->numColors = 1;
  141. }
  142. for(i = pExq->numColors; i < nColors; i++)
  143. {
  144. beste = 0;
  145. besti = 0;
  146. for(j = 0; j < i; j++)
  147. if(pExq->node[j].vdif >= beste)
  148. {
  149. beste = pExq->node[j].vdif;
  150. besti = j;
  151. }
  152. // printf("node %d: %d, %f\n", besti, pExq->node[besti].num, beste);
  153. pCur = pExq->node[besti].pHistogram;
  154. pExq->node[besti].pHistogram = NULL;
  155. pExq->node[i].pHistogram = NULL;
  156. while(pCur != NULL && pCur != pExq->node[besti].pSplit)
  157. {
  158. pNext = pCur->pNext;
  159. pCur->pNext = pExq->node[i].pHistogram;
  160. pExq->node[i].pHistogram = pCur;
  161. pCur = pNext;
  162. }
  163. while(pCur != NULL)
  164. {
  165. pNext = pCur->pNext;
  166. pCur->pNext = pExq->node[besti].pHistogram;
  167. pExq->node[besti].pHistogram = pCur;
  168. pCur = pNext;
  169. }
  170. exq_sum_node(&pExq->node[besti]);
  171. exq_sum_node(&pExq->node[i]);
  172. pExq->numColors = i + 1;
  173. if(hq)
  174. exq_optimize_palette(pExq, 1);
  175. }
  176. pExq->optimized = 0;
  177. }
  178. exq_float exq_get_mean_error(exq_data *pExq)
  179. {
  180. int i, n;
  181. exq_float err;
  182. n = 0;
  183. err = 0;
  184. for(i = 0; i < pExq->numColors; i++)
  185. {
  186. n += pExq->node[i].num;
  187. err += pExq->node[i].err;
  188. }
  189. return sqrt(err / n) * 256;
  190. }
  191. void exq_get_palette(exq_data *pExq, unsigned char *pPal, int nColors)
  192. {
  193. int i, j;
  194. exq_float r, g, b, a;
  195. unsigned char channelMask = 0xff00 >> pExq->numBitsPerChannel;
  196. if(nColors > pExq->numColors)
  197. nColors = pExq->numColors;
  198. if(!pExq->optimized)
  199. exq_optimize_palette(pExq, 4);
  200. for(i = 0; i < nColors; i++)
  201. {
  202. r = pExq->node[i].avg.r;
  203. g = pExq->node[i].avg.g;
  204. b = pExq->node[i].avg.b;
  205. a = pExq->node[i].avg.a;
  206. if(pExq->transparency == 1 && a != 0)
  207. {
  208. r /= a; g/= a; b/= a;
  209. }
  210. pPal[0] = (unsigned char)(r / SCALE_R * 255.9f);
  211. pPal[1] = (unsigned char)(g / SCALE_G * 255.9f);
  212. pPal[2] = (unsigned char)(b / SCALE_B * 255.9f);
  213. pPal[3] = (unsigned char)(a / SCALE_A * 255.9f);
  214. for(j = 0; j < 3; j++)
  215. pPal[j] = (pPal[j] + (1 << (8 - pExq->numBitsPerChannel)) / 2) & channelMask;
  216. pPal += 4;
  217. }
  218. }
  219. void exq_set_palette(exq_data *pExq, unsigned char *pPal, int nColors)
  220. {
  221. int i;
  222. pExq->numColors = nColors;
  223. for(i = 0; i < nColors; i++)
  224. {
  225. pExq->node[i].avg.r = *pPal++ * SCALE_R / 255.9f;
  226. pExq->node[i].avg.g = *pPal++ * SCALE_G / 255.9f;
  227. pExq->node[i].avg.b = *pPal++ * SCALE_B / 255.9f;
  228. pExq->node[i].avg.a = *pPal++ * SCALE_A / 255.9f;
  229. }
  230. pExq->optimized = 1;
  231. }
  232. void exq_sum_node(exq_node *pNode)
  233. {
  234. int n, n2;
  235. exq_color fsum, fsum2, vc, tmp, tmp2, sum, sum2;
  236. exq_histogram *pCur;
  237. exq_float isqrt, nv, v;
  238. n = 0;
  239. fsum.r = fsum.g = fsum.b = fsum.a = 0;
  240. fsum2.r = fsum2.g = fsum2.b = fsum2.a = 0;
  241. for(pCur = pNode->pHistogram; pCur != NULL; pCur = pCur->pNext)
  242. {
  243. n += pCur->num;
  244. fsum.r += pCur->color.r * pCur->num;
  245. fsum.g += pCur->color.g * pCur->num;
  246. fsum.b += pCur->color.b * pCur->num;
  247. fsum.a += pCur->color.a * pCur->num;
  248. fsum2.r += pCur->color.r * pCur->color.r * pCur->num;
  249. fsum2.g += pCur->color.g * pCur->color.g * pCur->num;
  250. fsum2.b += pCur->color.b * pCur->color.b * pCur->num;
  251. fsum2.a += pCur->color.a * pCur->color.a * pCur->num;
  252. }
  253. pNode->num = n;
  254. if(n == 0)
  255. {
  256. pNode->vdif = 0;
  257. pNode->err = 0;
  258. return;
  259. }
  260. pNode->avg.r = fsum.r / n;
  261. pNode->avg.g = fsum.g / n;
  262. pNode->avg.b = fsum.b / n;
  263. pNode->avg.a = fsum.a / n;
  264. vc.r = fsum2.r - fsum.r * pNode->avg.r;
  265. vc.g = fsum2.g - fsum.g * pNode->avg.g;
  266. vc.b = fsum2.b - fsum.b * pNode->avg.b;
  267. vc.a = fsum2.a - fsum.a * pNode->avg.a;
  268. v = vc.r + vc.g + vc.b + vc.a;
  269. pNode->err = v;
  270. pNode->vdif = -v;
  271. if(vc.r > vc.g && vc.r > vc.b && vc.r > vc.a)
  272. exq_sort(&pNode->pHistogram, exq_sort_by_r);
  273. else if(vc.g > vc.b && vc.g > vc.a)
  274. exq_sort(&pNode->pHistogram, exq_sort_by_g);
  275. else if(vc.b > vc.a)
  276. exq_sort(&pNode->pHistogram, exq_sort_by_b);
  277. else
  278. exq_sort(&pNode->pHistogram, exq_sort_by_a);
  279. pNode->dir.r = pNode->dir.g = pNode->dir.b = pNode->dir.a = 0;
  280. for(pCur = pNode->pHistogram; pCur != NULL; pCur = pCur->pNext)
  281. {
  282. tmp.r = (pCur->color.r - pNode->avg.r) * pCur->num;
  283. tmp.g = (pCur->color.g - pNode->avg.g) * pCur->num;
  284. tmp.b = (pCur->color.b - pNode->avg.b) * pCur->num;
  285. tmp.a = (pCur->color.a - pNode->avg.a) * pCur->num;
  286. if(tmp.r * pNode->dir.r + tmp.g * pNode->dir.g +
  287. tmp.b * pNode->dir.b + tmp.a * pNode->dir.a < 0)
  288. {
  289. tmp.r = -tmp.r;
  290. tmp.g = -tmp.g;
  291. tmp.b = -tmp.b;
  292. tmp.a = -tmp.a;
  293. }
  294. pNode->dir.r += tmp.r;
  295. pNode->dir.g += tmp.g;
  296. pNode->dir.b += tmp.b;
  297. pNode->dir.a += tmp.a;
  298. }
  299. isqrt = 1 / sqrt(pNode->dir.r * pNode->dir.r +
  300. pNode->dir.g * pNode->dir.g + pNode->dir.b * pNode->dir.b +
  301. pNode->dir.a * pNode->dir.a);
  302. pNode->dir.r *= isqrt;
  303. pNode->dir.g *= isqrt;
  304. pNode->dir.b *= isqrt;
  305. pNode->dir.a *= isqrt;
  306. exq_sort_dir = pNode->dir;
  307. exq_sort(&pNode->pHistogram, exq_sort_by_dir);
  308. sum.r = sum.g = sum.b = sum.a = 0;
  309. sum2.r = sum2.g = sum2.b = sum2.a = 0;
  310. n2 = 0;
  311. pNode->pSplit = pNode->pHistogram;
  312. for(pCur = pNode->pHistogram; pCur != NULL; pCur = pCur->pNext)
  313. {
  314. if(pNode->pSplit == NULL)
  315. pNode->pSplit = pCur;
  316. n2 += pCur->num;
  317. sum.r += pCur->color.r * pCur->num;
  318. sum.g += pCur->color.g * pCur->num;
  319. sum.b += pCur->color.b * pCur->num;
  320. sum.a += pCur->color.a * pCur->num;
  321. sum2.r += pCur->color.r * pCur->color.r * pCur->num;
  322. sum2.g += pCur->color.g * pCur->color.g * pCur->num;
  323. sum2.b += pCur->color.b * pCur->color.b * pCur->num;
  324. sum2.a += pCur->color.a * pCur->color.a * pCur->num;
  325. if(n == n2)
  326. break;
  327. tmp.r = sum2.r - sum.r*sum.r / n2;
  328. tmp.g = sum2.g - sum.g*sum.g / n2;
  329. tmp.b = sum2.b - sum.b*sum.b / n2;
  330. tmp.a = sum2.a - sum.a*sum.a / n2;
  331. tmp2.r = (fsum2.r - sum2.r) - (fsum.r-sum.r)*(fsum.r-sum.r) / (n - n2);
  332. tmp2.g = (fsum2.g - sum2.g) - (fsum.g-sum.g)*(fsum.g-sum.g) / (n - n2);
  333. tmp2.b = (fsum2.b - sum2.b) - (fsum.b-sum.b)*(fsum.b-sum.b) / (n - n2);
  334. tmp2.a = (fsum2.a - sum2.a) - (fsum.a-sum.a)*(fsum.a-sum.a) / (n - n2);
  335. nv = tmp.r + tmp.g + tmp.b + tmp.a + tmp2.r + tmp2.g + tmp2.b + tmp2.a;
  336. if(-nv > pNode->vdif)
  337. {
  338. pNode->vdif = -nv;
  339. pNode->pSplit = NULL;
  340. }
  341. }
  342. if(pNode->pSplit == pNode->pHistogram)
  343. pNode->pSplit = pNode->pSplit->pNext;
  344. pNode->vdif += v;
  345. // printf("error sum: %f, vdif: %f\n", pNode->err, pNode->vdif);
  346. }
  347. void exq_optimize_palette(exq_data *pExq, int iter)
  348. {
  349. int n, i, j;
  350. exq_histogram *pCur;
  351. pExq->optimized = 1;
  352. for(n = 0; n < iter; n++)
  353. {
  354. for(i = 0; i < pExq->numColors; i++)
  355. pExq->node[i].pHistogram = NULL;
  356. for(i = 0; i < EXQ_HASH_SIZE; i++)
  357. for(pCur = pExq->pHash[i]; pCur != NULL; pCur = pCur->pNextInHash)
  358. {
  359. j = exq_find_nearest_color(pExq, &pCur->color);
  360. pCur->pNext = pExq->node[j].pHistogram;
  361. pExq->node[j].pHistogram = pCur;
  362. }
  363. for(i = 0; i < pExq->numColors; i++)
  364. exq_sum_node(&pExq->node[i]);
  365. }
  366. }
  367. void exq_map_image(exq_data *pExq, int nPixels, unsigned char *pIn,
  368. unsigned char *pOut)
  369. {
  370. int i;
  371. exq_color c;
  372. exq_histogram *pHist;
  373. if(!pExq->optimized)
  374. exq_optimize_palette(pExq, 4);
  375. for(i = 0; i < nPixels; i++)
  376. {
  377. pHist = exq_find_histogram(pExq, pIn);
  378. if(pHist != NULL && pHist->palIndex != -1)
  379. {
  380. *pOut++ = (unsigned char)pHist->palIndex;
  381. pIn += 4;
  382. }
  383. else
  384. {
  385. c.r = *pIn++ / 255.0f * SCALE_R;
  386. c.g = *pIn++ / 255.0f * SCALE_G;
  387. c.b = *pIn++ / 255.0f * SCALE_B;
  388. c.a = *pIn++ / 255.0f * SCALE_A;
  389. if(pExq->transparency)
  390. {
  391. c.r *= c.a; c.g *= c.a; c.b *= c.a;
  392. }
  393. *pOut = exq_find_nearest_color(pExq, &c);
  394. if(pHist != NULL)
  395. pHist->palIndex = *pOut;
  396. pOut++;
  397. }
  398. }
  399. }
  400. void exq_map_image_ordered(exq_data *pExq, int width, int height,
  401. unsigned char *pIn, unsigned char *pOut)
  402. {
  403. exq_map_image_dither(pExq, width, height, pIn, pOut, 1);
  404. }
  405. void exq_map_image_random(exq_data *pExq, int nPixels,
  406. unsigned char *pIn, unsigned char *pOut)
  407. {
  408. exq_map_image_dither(pExq, nPixels, 1, pIn, pOut, 0);
  409. }
  410. void exq_map_image_dither(exq_data *pExq, int width, int height,
  411. unsigned char *pIn, unsigned char *pOut, int ordered)
  412. {
  413. int x, y, i, j, d;
  414. exq_color p, scale, tmp;
  415. exq_histogram *pHist;
  416. const exq_float dither_matrix[4] = { -0.375, 0.125, 0.375, -0.125 };
  417. if(!pExq->optimized)
  418. exq_optimize_palette(pExq, 4);
  419. for(y = 0; y < height; y++)
  420. for(x = 0; x < width; x++)
  421. {
  422. if(ordered)
  423. d = (x & 1) + (y & 1) * 2;
  424. else
  425. d = rand() & 3;
  426. pHist = exq_find_histogram(pExq, pIn);
  427. p.r = *pIn++ / 255.0f * SCALE_R;
  428. p.g = *pIn++ / 255.0f * SCALE_G;
  429. p.b = *pIn++ / 255.0f * SCALE_B;
  430. p.a = *pIn++ / 255.0f * SCALE_A;
  431. if(pExq->transparency)
  432. {
  433. p.r *= p.a; p.g *= p.a; p.b *= p.a;
  434. }
  435. if(pHist == NULL || pHist->ditherScale.r < 0)
  436. {
  437. i = exq_find_nearest_color(pExq, &p);
  438. scale.r = pExq->node[i].avg.r - p.r;
  439. scale.g = pExq->node[i].avg.g - p.g;
  440. scale.b = pExq->node[i].avg.b - p.b;
  441. scale.a = pExq->node[i].avg.a - p.a;
  442. tmp.r = p.r - scale.r / 3;
  443. tmp.g = p.g - scale.g / 3;
  444. tmp.b = p.b - scale.b / 3;
  445. tmp.a = p.a - scale.a / 3;
  446. j = exq_find_nearest_color(pExq, &tmp);
  447. if(i == j)
  448. {
  449. tmp.r = p.r - scale.r * 3;
  450. tmp.g = p.g - scale.g * 3;
  451. tmp.b = p.b - scale.b * 3;
  452. tmp.a = p.a - scale.a * 3;
  453. j = exq_find_nearest_color(pExq, &tmp);
  454. }
  455. if(i != j)
  456. {
  457. scale.r = (pExq->node[j].avg.r - pExq->node[i].avg.r) * 0.8f;
  458. scale.g = (pExq->node[j].avg.g - pExq->node[i].avg.g) * 0.8f;
  459. scale.b = (pExq->node[j].avg.b - pExq->node[i].avg.b) * 0.8f;
  460. scale.a = (pExq->node[j].avg.a - pExq->node[i].avg.a) * 0.8f;
  461. if(scale.r < 0) scale.r = -scale.r;
  462. if(scale.g < 0) scale.g = -scale.g;
  463. if(scale.b < 0) scale.b = -scale.b;
  464. if(scale.a < 0) scale.a = -scale.a;
  465. }
  466. else
  467. scale.r = scale.g = scale.b = scale.a = 0;
  468. if(pHist != NULL)
  469. {
  470. pHist->ditherScale.r = scale.r;
  471. pHist->ditherScale.g = scale.g;
  472. pHist->ditherScale.b = scale.b;
  473. pHist->ditherScale.a = scale.a;
  474. }
  475. }
  476. else
  477. {
  478. scale.r = pHist->ditherScale.r;
  479. scale.g = pHist->ditherScale.g;
  480. scale.b = pHist->ditherScale.b;
  481. scale.a = pHist->ditherScale.a;
  482. }
  483. if(pHist != NULL && pHist->ditherIndex[d] >= 0)
  484. *pOut++ = (unsigned char)pHist->ditherIndex[d];
  485. else
  486. {
  487. tmp.r = p.r + scale.r * dither_matrix[d];
  488. tmp.g = p.g + scale.g * dither_matrix[d];
  489. tmp.b = p.b + scale.b * dither_matrix[d];
  490. tmp.a = p.a + scale.a * dither_matrix[d];
  491. *pOut = exq_find_nearest_color(pExq, &tmp);
  492. if(pHist != NULL)
  493. pHist->ditherIndex[d] = *pOut;
  494. pOut++;
  495. }
  496. }
  497. }
  498. exq_histogram *exq_find_histogram(exq_data *pExq, unsigned char *pCol)
  499. {
  500. unsigned int hash;
  501. int r, g, b, a;
  502. exq_histogram *pCur;
  503. r = *pCol++; g = *pCol++; b = *pCol++; a = *pCol++;
  504. hash = exq_make_hash(((unsigned int)r) | (((unsigned int)g) << 8) | (((unsigned int)b) << 16) | (((unsigned int)a) << 24));
  505. pCur = pExq->pHash[hash];
  506. while(pCur != NULL && (pCur->ored != r || pCur->ogreen != g ||
  507. pCur->oblue != b || pCur->oalpha != a))
  508. pCur = pCur->pNextInHash;
  509. return pCur;
  510. }
  511. unsigned char exq_find_nearest_color(exq_data *pExq, exq_color *pColor)
  512. {
  513. exq_float bestv;
  514. int besti, i;
  515. exq_color dif;
  516. bestv = 16;
  517. besti = 0;
  518. for(i = 0; i < pExq->numColors; i++)
  519. {
  520. dif.r = pColor->r - pExq->node[i].avg.r;
  521. dif.g = pColor->g - pExq->node[i].avg.g;
  522. dif.b = pColor->b - pExq->node[i].avg.b;
  523. dif.a = pColor->a - pExq->node[i].avg.a;
  524. if(dif.r*dif.r + dif.g*dif.g + dif.b*dif.b + dif.a*dif.a < bestv)
  525. {
  526. bestv = dif.r*dif.r + dif.g*dif.g + dif.b*dif.b + dif.a*dif.a;
  527. besti = i;
  528. }
  529. }
  530. return (unsigned char)besti;
  531. }
  532. void exq_sort(exq_histogram **ppHist, exq_float (*sortfunc)(const exq_histogram *pHist))
  533. {
  534. exq_histogram *pLow, *pHigh, *pCur, *pNext;
  535. int n = 0;
  536. exq_float sum = 0;
  537. for(pCur = *ppHist; pCur != NULL; pCur = pCur->pNext)
  538. {
  539. n++;
  540. sum += sortfunc(pCur);
  541. }
  542. if(n < 2)
  543. return;
  544. sum /= n;
  545. pLow = pHigh = NULL;
  546. for(pCur = *ppHist; pCur != NULL; pCur = pNext)
  547. {
  548. pNext = pCur->pNext;
  549. if(sortfunc(pCur) < sum)
  550. {
  551. pCur->pNext = pLow;
  552. pLow = pCur;
  553. }
  554. else
  555. {
  556. pCur->pNext = pHigh;
  557. pHigh = pCur;
  558. }
  559. }
  560. if(pLow == NULL)
  561. {
  562. *ppHist = pHigh;
  563. return;
  564. }
  565. if(pHigh == NULL)
  566. {
  567. *ppHist = pLow;
  568. return;
  569. }
  570. exq_sort(&pLow, sortfunc);
  571. exq_sort(&pHigh, sortfunc);
  572. *ppHist = pLow;
  573. while(pLow->pNext != NULL)
  574. pLow = pLow->pNext;
  575. pLow->pNext = pHigh;
  576. }
  577. exq_float exq_sort_by_r(const exq_histogram *pHist)
  578. {
  579. return pHist->color.r;
  580. }
  581. exq_float exq_sort_by_g(const exq_histogram *pHist)
  582. {
  583. return pHist->color.g;
  584. }
  585. exq_float exq_sort_by_b(const exq_histogram *pHist)
  586. {
  587. return pHist->color.b;
  588. }
  589. exq_float exq_sort_by_a(const exq_histogram *pHist)
  590. {
  591. return pHist->color.a;
  592. }
  593. exq_color exq_sort_dir;
  594. exq_float exq_sort_by_dir(const exq_histogram *pHist)
  595. {
  596. return pHist->color.r * exq_sort_dir.r +
  597. pHist->color.g * exq_sort_dir.g +
  598. pHist->color.b * exq_sort_dir.b +
  599. pHist->color.a * exq_sort_dir.a;
  600. }