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.

преди 18 години
преди 21 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 18 години
преди 19 години
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains bitmap dithering functions.
  15. */
  16. #include "config.h"
  17. #if !defined(__KERNEL__)
  18. # if defined(HAVE_ENDIAN_H)
  19. # include <endian.h>
  20. # endif
  21. # include <stdio.h>
  22. # include <stdlib.h>
  23. # include <limits.h>
  24. # include <string.h>
  25. #endif
  26. #include "cucul.h"
  27. #include "cucul_internals.h"
  28. #define CP437 0
  29. /*
  30. * Local variables
  31. */
  32. #if !defined(_DOXYGEN_SKIP_ME)
  33. # define LOOKUP_VAL 32
  34. # define LOOKUP_SAT 32
  35. # define LOOKUP_HUE 16
  36. #endif
  37. static unsigned char hsv_distances[LOOKUP_VAL][LOOKUP_SAT][LOOKUP_HUE];
  38. static uint16_t lookup_colors[8];
  39. static int const hsv_palette[] =
  40. {
  41. /* weight, hue, saturation, value */
  42. 4, 0x0, 0x0, 0x0, /* black */
  43. 5, 0x0, 0x0, 0x5ff, /* 30% */
  44. 5, 0x0, 0x0, 0x9ff, /* 70% */
  45. 4, 0x0, 0x0, 0xfff, /* white */
  46. 3, 0x1000, 0xfff, 0x5ff, /* dark yellow */
  47. 2, 0x1000, 0xfff, 0xfff, /* light yellow */
  48. 3, 0x0, 0xfff, 0x5ff, /* dark red */
  49. 2, 0x0, 0xfff, 0xfff /* light red */
  50. };
  51. /* RGB palette for the new colour picker */
  52. static int const rgb_palette[] =
  53. {
  54. 0x0, 0x0, 0x0,
  55. 0x0, 0x0, 0x7ff,
  56. 0x0, 0x7ff, 0x0,
  57. 0x0, 0x7ff, 0x7ff,
  58. 0x7ff, 0x0, 0x0,
  59. 0x7ff, 0x0, 0x7ff,
  60. 0x7ff, 0x7ff, 0x0,
  61. 0xaaa, 0xaaa, 0xaaa,
  62. 0x555, 0x555, 0x555,
  63. 0x000, 0x000, 0xfff,
  64. 0x000, 0xfff, 0x000,
  65. 0x000, 0xfff, 0xfff,
  66. 0xfff, 0x000, 0x000,
  67. 0xfff, 0x000, 0xfff,
  68. 0xfff, 0xfff, 0x000,
  69. 0xfff, 0xfff, 0xfff,
  70. };
  71. static int const rgb_weight[] =
  72. {
  73. //2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2
  74. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  75. };
  76. /* List of glyphs */
  77. static char const * ascii_glyphs[] =
  78. {
  79. " ", ".", ":", ";", "t", "%", "S", "X", "@", "8", "?"
  80. };
  81. static char const * shades_glyphs[] =
  82. {
  83. " ", ":", "░", "▒", "?"
  84. };
  85. static char const * blocks_glyphs[] =
  86. {
  87. " ", "▘", "▚", "?"
  88. };
  89. #if !defined(_DOXYGEN_SKIP_ME)
  90. enum color_mode
  91. {
  92. COLOR_MODE_MONO,
  93. COLOR_MODE_GRAY,
  94. COLOR_MODE_8,
  95. COLOR_MODE_16,
  96. COLOR_MODE_FULLGRAY,
  97. COLOR_MODE_FULL8,
  98. COLOR_MODE_FULL16,
  99. };
  100. struct cucul_dither
  101. {
  102. int bpp, has_palette, has_alpha;
  103. int w, h, pitch;
  104. int rmask, gmask, bmask, amask;
  105. int rright, gright, bright, aright;
  106. int rleft, gleft, bleft, aleft;
  107. void (*get_hsv)(cucul_dither_t *, char *, int, int);
  108. int red[256], green[256], blue[256], alpha[256];
  109. float gamma;
  110. int gammatab[4097];
  111. /* Bitmap features */
  112. int invert, antialias;
  113. /* Colour mode used for rendering */
  114. enum color_mode color_mode;
  115. /* Glyphs used for rendering */
  116. char const * const * glyphs;
  117. unsigned glyph_count;
  118. /* Current dithering method */
  119. void (*init_dither) (int);
  120. unsigned int (*get_dither) (void);
  121. void (*increment_dither) (void);
  122. };
  123. #define HSV_XRATIO 6
  124. #define HSV_YRATIO 3
  125. #define HSV_HRATIO 3
  126. #define HSV_DISTANCE(h, s, v, index) \
  127. (hsv_palette[index * 4] \
  128. * ((HSV_XRATIO * ((v) - hsv_palette[index * 4 + 3]) \
  129. * ((v) - hsv_palette[index * 4 + 3])) \
  130. + (hsv_palette[index * 4 + 3] \
  131. ? (HSV_YRATIO * ((s) - hsv_palette[index * 4 + 2]) \
  132. * ((s) - hsv_palette[index * 4 + 2])) \
  133. : 0) \
  134. + (hsv_palette[index * 4 + 2] \
  135. ? (HSV_HRATIO * ((h) - hsv_palette[index * 4 + 1]) \
  136. * ((h) - hsv_palette[index * 4 + 1])) \
  137. : 0)))
  138. #endif
  139. /*
  140. * Local prototypes
  141. */
  142. static void mask2shift(unsigned int, int *, int *);
  143. static float gammapow(float x, float y);
  144. static void get_rgba_default(cucul_dither_t const *, uint8_t *, int, int,
  145. unsigned int *);
  146. /* Dithering methods */
  147. static void init_no_dither(int);
  148. static unsigned int get_no_dither(void);
  149. static void increment_no_dither(void);
  150. static void init_fstein_dither(int);
  151. static unsigned int get_fstein_dither(void);
  152. static void increment_fstein_dither(void);
  153. static void init_ordered2_dither(int);
  154. static unsigned int get_ordered2_dither(void);
  155. static void increment_ordered2_dither(void);
  156. static void init_ordered4_dither(int);
  157. static unsigned int get_ordered4_dither(void);
  158. static void increment_ordered4_dither(void);
  159. static void init_ordered8_dither(int);
  160. static unsigned int get_ordered8_dither(void);
  161. static void increment_ordered8_dither(void);
  162. static void init_random_dither(int);
  163. static unsigned int get_random_dither(void);
  164. static void increment_random_dither(void);
  165. static inline int sq(int x)
  166. {
  167. return x * x;
  168. }
  169. static inline void rgb2hsv_default(int r, int g, int b,
  170. int *hue, int *sat, int *val)
  171. {
  172. int min, max, delta;
  173. min = r; max = r;
  174. if(min > g) min = g; if(max < g) max = g;
  175. if(min > b) min = b; if(max < b) max = b;
  176. delta = max - min; /* 0 - 0xfff */
  177. *val = max; /* 0 - 0xfff */
  178. if(delta)
  179. {
  180. *sat = 0xfff * delta / max; /* 0 - 0xfff */
  181. /* Generate *hue between 0 and 0x5fff */
  182. if( r == max )
  183. *hue = 0x1000 + 0x1000 * (g - b) / delta;
  184. else if( g == max )
  185. *hue = 0x3000 + 0x1000 * (b - r) / delta;
  186. else
  187. *hue = 0x5000 + 0x1000 * (r - g) / delta;
  188. }
  189. else
  190. {
  191. *sat = 0;
  192. *hue = 0;
  193. }
  194. }
  195. /** \brief Create an internal dither object.
  196. *
  197. * Create a dither structure from its coordinates (depth, width, height and
  198. * pitch) and pixel mask values. If the depth is 8 bits per pixel, the mask
  199. * values are ignored and the colour palette should be set using the
  200. * cucul_set_dither_palette() function. For depths greater than 8 bits per
  201. * pixel, a zero alpha mask causes the alpha values to be ignored.
  202. *
  203. * \param bpp Bitmap depth in bits per pixel.
  204. * \param w Bitmap width in pixels.
  205. * \param h Bitmap height in pixels.
  206. * \param pitch Bitmap pitch in bytes.
  207. * \param rmask Bitmask for red values.
  208. * \param gmask Bitmask for green values.
  209. * \param bmask Bitmask for blue values.
  210. * \param amask Bitmask for alpha values.
  211. * \return Dither object, or NULL upon error.
  212. */
  213. cucul_dither_t *cucul_create_dither(unsigned int bpp, unsigned int w,
  214. unsigned int h, unsigned int pitch,
  215. unsigned int rmask, unsigned int gmask,
  216. unsigned int bmask, unsigned int amask)
  217. {
  218. cucul_dither_t *d;
  219. int i;
  220. /* Minor sanity test */
  221. if(!w || !h || !pitch || bpp > 32 || bpp < 8)
  222. return NULL;
  223. d = malloc(sizeof(cucul_dither_t));
  224. if(!d)
  225. return NULL;
  226. d->bpp = bpp;
  227. d->has_palette = 0;
  228. d->has_alpha = amask ? 1 : 0;
  229. d->w = w;
  230. d->h = h;
  231. d->pitch = pitch;
  232. d->rmask = rmask;
  233. d->gmask = gmask;
  234. d->bmask = bmask;
  235. d->amask = amask;
  236. /* Load bitmasks */
  237. if(rmask || gmask || bmask || amask)
  238. {
  239. mask2shift(rmask, &d->rright, &d->rleft);
  240. mask2shift(gmask, &d->gright, &d->gleft);
  241. mask2shift(bmask, &d->bright, &d->bleft);
  242. mask2shift(amask, &d->aright, &d->aleft);
  243. }
  244. /* In 8 bpp mode, default to a grayscale palette */
  245. if(bpp == 8)
  246. {
  247. d->has_palette = 1;
  248. d->has_alpha = 0;
  249. for(i = 0; i < 256; i++)
  250. {
  251. d->red[i] = i * 0xfff / 256;
  252. d->green[i] = i * 0xfff / 256;
  253. d->blue[i] = i * 0xfff / 256;
  254. }
  255. }
  256. /* Default features */
  257. d->invert = 0;
  258. d->antialias = 1;
  259. /* Default gamma value */
  260. for(i = 0; i < 4096; i++)
  261. d->gammatab[i] = i;
  262. /* Default colour mode */
  263. d->color_mode = COLOR_MODE_FULL16;
  264. /* Default character set */
  265. d->glyphs = ascii_glyphs;
  266. d->glyph_count = sizeof(ascii_glyphs) / sizeof(*ascii_glyphs);
  267. /* Default dithering mode */
  268. d->init_dither = init_fstein_dither;
  269. d->get_dither = get_fstein_dither;
  270. d->increment_dither = increment_fstein_dither;
  271. return d;
  272. }
  273. /** \brief Set the palette of an 8bpp dither object.
  274. *
  275. * Set the palette of an 8 bits per pixel bitmap. Values should be between
  276. * 0 and 4095 (0xfff).
  277. *
  278. * \param d Dither object.
  279. * \param red Array of 256 red values.
  280. * \param green Array of 256 green values.
  281. * \param blue Array of 256 blue values.
  282. * \param alpha Array of 256 alpha values.
  283. */
  284. void cucul_set_dither_palette(cucul_dither_t *d,
  285. unsigned int red[], unsigned int green[],
  286. unsigned int blue[], unsigned int alpha[])
  287. {
  288. int i, has_alpha = 0;
  289. if(d->bpp != 8)
  290. return;
  291. for(i = 0; i < 256; i++)
  292. {
  293. if(red[i] >= 0 && red[i] < 0x1000 &&
  294. green[i] >= 0 && green[i] < 0x1000 &&
  295. blue[i] >= 0 && blue[i] < 0x1000 &&
  296. alpha[i] >= 0 && alpha[i] < 0x1000)
  297. {
  298. d->red[i] = red[i];
  299. d->green[i] = green[i];
  300. d->blue[i] = blue[i];
  301. if(alpha[i])
  302. {
  303. d->alpha[i] = alpha[i];
  304. has_alpha = 1;
  305. }
  306. }
  307. }
  308. d->has_alpha = has_alpha;
  309. }
  310. /** \brief Set the brightness of a dither object.
  311. *
  312. * Set the brightness of dither.
  313. *
  314. * \param d Dither object.
  315. * \param brightness brightness value.
  316. */
  317. void cucul_set_dither_brightness(cucul_dither_t *d, float brightness)
  318. {
  319. /* FIXME */
  320. }
  321. /** \brief Set the gamma of a dither object.
  322. *
  323. * Set the gamma of dither.
  324. *
  325. * \param d Dither object.
  326. * \param gamma Gamma value.
  327. */
  328. void cucul_set_dither_gamma(cucul_dither_t *d, float gamma)
  329. {
  330. /* FIXME: we don't need 4096 calls to gammapow(), we can just compute
  331. * 128 of them and do linear interpolation for the rest. This will
  332. * probably speed up things a lot. */
  333. int i;
  334. if(gamma <= 0.0)
  335. return;
  336. d->gamma = gamma;
  337. for(i = 0; i < 4096; i++)
  338. d->gammatab[i] = 4096.0 * gammapow((float)i / 4096.0, 1.0 / gamma);
  339. }
  340. /** \brief Invert colors of dither
  341. *
  342. * Invert colors of dither
  343. *
  344. * \param d Dither object.
  345. * \param value 0 for normal behaviour, 1 for invert
  346. */
  347. void cucul_set_dither_invert(cucul_dither_t *d, int value)
  348. {
  349. d->invert = value ? 1 : 0;
  350. }
  351. /** \brief Set the contrast of a dither object.
  352. *
  353. * Set the contrast of dither.
  354. *
  355. * \param d Dither object.
  356. * \param contrast contrast value.
  357. */
  358. void cucul_set_dither_contrast(cucul_dither_t *d, float contrast)
  359. {
  360. /* FIXME */
  361. }
  362. /** \brief Set dither antialiasing
  363. *
  364. * Tell the renderer whether to antialias the dither. Antialiasing smoothens
  365. * the rendered image and avoids the commonly seen staircase effect.
  366. *
  367. * \li \c "none": no antialiasing.
  368. *
  369. * \li \c "prefilter": simple prefilter antialiasing. This is the default
  370. * value.
  371. *
  372. * \param d Dither object.
  373. * \param str A string describing the antialiasing method that will be used
  374. * for the dithering.
  375. */
  376. void cucul_set_dither_antialias(cucul_dither_t *d, char const *str)
  377. {
  378. if(!strcasecmp(str, "none"))
  379. d->antialias = 0;
  380. else /* "prefilter" is the default */
  381. d->antialias = 1;
  382. }
  383. /** \brief Get available antialiasing methods
  384. *
  385. * Return a list of available antialiasing methods for a given dither. The
  386. * list is a NULL-terminated array of strings, interleaving a string
  387. * containing the internal value for the antialiasing method to be used with
  388. * cucul_set_dither_antialias(), and a string containing the natural
  389. * language description for that antialiasing method.
  390. *
  391. * \param d Dither object.
  392. * \return An array of strings.
  393. */
  394. char const * const *
  395. cucul_get_dither_antialias_list(cucul_dither_t const *d)
  396. {
  397. static char const * const list[] =
  398. {
  399. "none", "No antialiasing",
  400. "prefilter", "Prefilter antialiasing",
  401. NULL, NULL
  402. };
  403. return list;
  404. }
  405. /** \brief Choose colours used for dithering
  406. *
  407. * Tell the renderer which colours should be used to render the
  408. * bitmap. Valid values for \c str are:
  409. *
  410. * \li \c "mono": use light gray on a black background.
  411. *
  412. * \li \c "gray": use white and two shades of gray on a black background.
  413. *
  414. * \li \c "8": use the 8 ANSI colours on a black background.
  415. *
  416. * \li \c "16": use the 16 ANSI colours on a black background.
  417. *
  418. * \li \c "fullgray": use black, white and two shades of gray for both the
  419. * characters and the background.
  420. *
  421. * \li \c "full8": use the 8 ANSI colours for both the characters and the
  422. * background.
  423. *
  424. * \li \c "full16": use the 16 ANSI colours for both the characters and the
  425. * background. This is the default value.
  426. *
  427. * \param d Dither object.
  428. * \param str A string describing the colour set that will be used
  429. * for the dithering.
  430. */
  431. void cucul_set_dither_color(cucul_dither_t *d, char const *str)
  432. {
  433. if(!strcasecmp(str, "mono"))
  434. d->color_mode = COLOR_MODE_MONO;
  435. else if(!strcasecmp(str, "gray"))
  436. d->color_mode = COLOR_MODE_GRAY;
  437. else if(!strcasecmp(str, "8"))
  438. d->color_mode = COLOR_MODE_8;
  439. else if(!strcasecmp(str, "16"))
  440. d->color_mode = COLOR_MODE_16;
  441. else if(!strcasecmp(str, "fullgray"))
  442. d->color_mode = COLOR_MODE_FULLGRAY;
  443. else if(!strcasecmp(str, "full8"))
  444. d->color_mode = COLOR_MODE_FULL8;
  445. else /* "full16" is the default */
  446. d->color_mode = COLOR_MODE_FULL16;
  447. }
  448. /** \brief Get available colour modes
  449. *
  450. * Return a list of available colour modes for a given dither. The list
  451. * is a NULL-terminated array of strings, interleaving a string containing
  452. * the internal value for the colour mode, to be used with
  453. * cucul_set_dither_color(), and a string containing the natural
  454. * language description for that colour mode.
  455. *
  456. * \param d Dither object.
  457. * \return An array of strings.
  458. */
  459. char const * const *
  460. cucul_get_dither_color_list(cucul_dither_t const *d)
  461. {
  462. static char const * const list[] =
  463. {
  464. "mono", "white on black",
  465. "gray", "grayscale on black",
  466. "8", "8 colours on black",
  467. "16", "16 colours on black",
  468. "fullgray", "full grayscale",
  469. "full8", "full 8 colours",
  470. "full16", "full 16 colours",
  471. NULL, NULL
  472. };
  473. return list;
  474. }
  475. /** \brief Choose characters used for dithering
  476. *
  477. * Tell the renderer which characters should be used to render the
  478. * dither. Valid values for \c str are:
  479. *
  480. * \li \c "ascii": use only ASCII characters. This is the default value.
  481. *
  482. * \li \c "shades": use Unicode characters "U+2591 LIGHT SHADE", "U+2592
  483. * MEDIUM SHADE" and "U+2593 DARK SHADE". These characters are also
  484. * present in the CP437 codepage available on DOS and VGA.
  485. *
  486. * \li \c "blocks": use Unicode quarter-cell block combinations. These
  487. * characters are only found in the Unicode set.
  488. *
  489. * \param d Dither object.
  490. * \param str A string describing the characters that need to be used
  491. * for the dithering.
  492. */
  493. void cucul_set_dither_charset(cucul_dither_t *d, char const *str)
  494. {
  495. if(!strcasecmp(str, "shades"))
  496. {
  497. d->glyphs = shades_glyphs;
  498. d->glyph_count = sizeof(shades_glyphs) / sizeof(*shades_glyphs);
  499. }
  500. else if(!strcasecmp(str, "blocks"))
  501. {
  502. d->glyphs = blocks_glyphs;
  503. d->glyph_count = sizeof(blocks_glyphs) / sizeof(*blocks_glyphs);
  504. }
  505. else /* "ascii" is the default */
  506. {
  507. d->glyphs = ascii_glyphs;
  508. d->glyph_count = sizeof(ascii_glyphs) / sizeof(*ascii_glyphs);
  509. }
  510. }
  511. /** \brief Get available dither character sets
  512. *
  513. * Return a list of available character sets for a given dither. The list
  514. * is a NULL-terminated array of strings, interleaving a string containing
  515. * the internal value for the character set, to be used with
  516. * cucul_set_dither_charset(), and a string containing the natural
  517. * language description for that character set.
  518. *
  519. * \param d Dither object.
  520. * \return An array of strings.
  521. */
  522. char const * const * cucul_get_dither_charset_list(cucul_dither_t const *d)
  523. {
  524. static char const * const list[] =
  525. {
  526. "ascii", "plain ASCII",
  527. "shades", "CP437 shades",
  528. "blocks", "Unicode blocks",
  529. NULL, NULL
  530. };
  531. return list;
  532. }
  533. /** \brief Set dithering method
  534. *
  535. * Tell the renderer which dithering method should be used. Dithering is
  536. * necessary because the picture being rendered has usually far more colours
  537. * than the available palette. Valid values for \c str are:
  538. *
  539. * \li \c "none": no dithering is used, the nearest matching colour is used.
  540. *
  541. * \li \c "ordered2": use a 2x2 Bayer matrix for dithering.
  542. *
  543. * \li \c "ordered4": use a 4x4 Bayer matrix for dithering.
  544. *
  545. * \li \c "ordered8": use a 8x8 Bayer matrix for dithering.
  546. *
  547. * \li \c "random": use random dithering.
  548. *
  549. * \li \c "fstein": use Floyd-Steinberg dithering. This is the default value.
  550. *
  551. * \param d Dither object.
  552. * \param str A string describing the method that needs to be used
  553. * for the dithering.
  554. */
  555. void cucul_set_dither_mode(cucul_dither_t *d, char const *str)
  556. {
  557. if(!strcasecmp(str, "none"))
  558. {
  559. d->init_dither = init_no_dither;
  560. d->get_dither = get_no_dither;
  561. d->increment_dither = increment_no_dither;
  562. }
  563. else if(!strcasecmp(str, "ordered2"))
  564. {
  565. d->init_dither = init_ordered2_dither;
  566. d->get_dither = get_ordered2_dither;
  567. d->increment_dither = increment_ordered2_dither;
  568. }
  569. else if(!strcasecmp(str, "ordered4"))
  570. {
  571. d->init_dither = init_ordered4_dither;
  572. d->get_dither = get_ordered4_dither;
  573. d->increment_dither = increment_ordered4_dither;
  574. }
  575. else if(!strcasecmp(str, "ordered4"))
  576. {
  577. d->init_dither = init_ordered8_dither;
  578. d->get_dither = get_ordered8_dither;
  579. d->increment_dither = increment_ordered8_dither;
  580. }
  581. else if(!strcasecmp(str, "random"))
  582. {
  583. d->init_dither = init_random_dither;
  584. d->get_dither = get_random_dither;
  585. d->increment_dither = increment_random_dither;
  586. }
  587. else /* "fstein" is the default */
  588. {
  589. d->init_dither = init_fstein_dither;
  590. d->get_dither = get_fstein_dither;
  591. d->increment_dither = increment_fstein_dither;
  592. }
  593. }
  594. /** \brief Get dithering methods
  595. *
  596. * Return a list of available dithering methods for a given dither. The list
  597. * is a NULL-terminated array of strings, interleaving a string containing
  598. * the internal value for the dithering method, to be used with
  599. * cucul_set_dither_dithering(), and a string containing the natural
  600. * language description for that dithering method.
  601. *
  602. * \param d Dither object.
  603. * \return An array of strings.
  604. */
  605. char const * const * cucul_get_dither_mode_list(cucul_dither_t const *d)
  606. {
  607. static char const * const list[] =
  608. {
  609. "none", "no dithering",
  610. "ordered2", "2x2 ordered dithering",
  611. "ordered2", "2x2 ordered dithering",
  612. "ordered2", "2x2 ordered dithering",
  613. "random", "random dithering",
  614. "fstein", "Floyd-Steinberg dithering",
  615. NULL, NULL
  616. };
  617. return list;
  618. }
  619. /** \brief Dither a bitmap on the canvas.
  620. *
  621. * Dither a bitmap at the given coordinates. The dither can be of any size
  622. * and will be stretched to the text area.
  623. *
  624. * \param cv A handle to the libcucul canvas.
  625. * \param x X coordinate of the upper-left corner of the drawing area.
  626. * \param y Y coordinate of the upper-left corner of the drawing area.
  627. * \param w Width of the drawing area.
  628. * \param h Height of the drawing area.
  629. * \param d Dither object to be drawn.
  630. * \param pixels Bitmap's pixels.
  631. */
  632. void cucul_dither_bitmap(cucul_canvas_t *cv, int x, int y, int w, int h,
  633. cucul_dither_t const *d, void *pixels)
  634. {
  635. int *floyd_steinberg, *fs_r, *fs_g, *fs_b;
  636. int fs_length;
  637. int x1, y1, x2, y2, pitch, deltax, deltay;
  638. unsigned int dchmax;
  639. if(!d || !pixels)
  640. return;
  641. x1 = x; x2 = x + w - 1;
  642. y1 = y; y2 = y + h - 1;
  643. /* FIXME: do not overwrite arguments */
  644. w = d->w;
  645. h = d->h;
  646. pitch = d->pitch;
  647. deltax = x2 - x1 + 1;
  648. deltay = y2 - y1 + 1;
  649. dchmax = d->glyph_count;
  650. fs_length = ((int)cv->width <= x2 ? (int)cv->width : x2) + 1;
  651. floyd_steinberg = malloc(3 * (fs_length + 2) * sizeof(int));
  652. memset(floyd_steinberg, 0, 3 * (fs_length + 2) * sizeof(int));
  653. fs_r = floyd_steinberg + 1;
  654. fs_g = fs_r + fs_length + 2;
  655. fs_b = fs_g + fs_length + 2;
  656. for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)cv->height; y++)
  657. {
  658. int remain_r = 0, remain_g = 0, remain_b = 0;
  659. for(x = x1 > 0 ? x1 : 0, d->init_dither(y);
  660. x <= x2 && x <= (int)cv->width;
  661. x++)
  662. {
  663. unsigned int i;
  664. int ch = 0, distmin;
  665. unsigned int rgba[4];
  666. int fg_r = 0, fg_g = 0, fg_b = 0, bg_r, bg_g, bg_b;
  667. int fromx, fromy, tox, toy, myx, myy, dots, dist;
  668. int error[3];
  669. unsigned int outfg = 0, outbg = 0;
  670. char const *outch;
  671. rgba[0] = rgba[1] = rgba[2] = rgba[3] = 0;
  672. /* First get RGB */
  673. if(d->antialias)
  674. {
  675. fromx = (x - x1) * w / deltax;
  676. fromy = (y - y1) * h / deltay;
  677. tox = (x - x1 + 1) * w / deltax;
  678. toy = (y - y1 + 1) * h / deltay;
  679. /* We want at least one pixel */
  680. if(tox == fromx) tox++;
  681. if(toy == fromy) toy++;
  682. dots = 0;
  683. for(myx = fromx; myx < tox; myx++)
  684. for(myy = fromy; myy < toy; myy++)
  685. {
  686. dots++;
  687. get_rgba_default(d, pixels, myx, myy, rgba);
  688. }
  689. /* Normalize */
  690. rgba[0] /= dots;
  691. rgba[1] /= dots;
  692. rgba[2] /= dots;
  693. rgba[3] /= dots;
  694. }
  695. else
  696. {
  697. fromx = (x - x1) * w / deltax;
  698. fromy = (y - y1) * h / deltay;
  699. tox = (x - x1 + 1) * w / deltax;
  700. toy = (y - y1 + 1) * h / deltay;
  701. /* tox and toy can overflow the canvas, but they cannot overflow
  702. * when averaged with fromx and fromy because these are guaranteed
  703. * to be within the pixel boundaries. */
  704. myx = (fromx + tox) / 2;
  705. myy = (fromy + toy) / 2;
  706. get_rgba_default(d, pixels, myx, myy, rgba);
  707. }
  708. if(d->has_alpha && rgba[3] < 0x800)
  709. {
  710. remain_r = remain_g = remain_b = 0;
  711. fs_r[x] = 0;
  712. fs_g[x] = 0;
  713. fs_b[x] = 0;
  714. continue;
  715. }
  716. /* XXX: OMG HAX */
  717. if(d->init_dither == init_fstein_dither)
  718. {
  719. rgba[0] += remain_r;
  720. rgba[1] += remain_g;
  721. rgba[2] += remain_b;
  722. }
  723. else
  724. {
  725. rgba[0] += (d->get_dither() - 0x80) * 4;
  726. rgba[1] += (d->get_dither() - 0x80) * 4;
  727. rgba[2] += (d->get_dither() - 0x80) * 4;
  728. }
  729. distmin = INT_MAX;
  730. for(i = 0; i < 16; i++)
  731. {
  732. dist = sq(rgba[0] - rgb_palette[i * 3])
  733. + sq(rgba[1] - rgb_palette[i * 3 + 1])
  734. + sq(rgba[2] - rgb_palette[i * 3 + 2]);
  735. dist *= rgb_weight[i];
  736. if(dist < distmin)
  737. {
  738. outbg = i;
  739. distmin = dist;
  740. }
  741. }
  742. bg_r = rgb_palette[outbg * 3];
  743. bg_g = rgb_palette[outbg * 3 + 1];
  744. bg_b = rgb_palette[outbg * 3 + 2];
  745. /* FIXME: we currently only honour "full16" */
  746. if(d->color_mode == COLOR_MODE_FULL16)
  747. {
  748. distmin = INT_MAX;
  749. for(i = 0; i < 16; i++)
  750. {
  751. if(i == outbg)
  752. continue;
  753. dist = sq(rgba[0] - rgb_palette[i * 3])
  754. + sq(rgba[1] - rgb_palette[i * 3 + 1])
  755. + sq(rgba[2] - rgb_palette[i * 3 + 2]);
  756. dist *= rgb_weight[i];
  757. if(dist < distmin)
  758. {
  759. outfg = i;
  760. distmin = dist;
  761. }
  762. }
  763. fg_r = rgb_palette[outfg * 3];
  764. fg_g = rgb_palette[outfg * 3 + 1];
  765. fg_b = rgb_palette[outfg * 3 + 2];
  766. distmin = INT_MAX;
  767. for(i = 0; i < dchmax - 1; i++)
  768. {
  769. int newr = i * fg_r + ((2*dchmax-1) - i) * bg_r;
  770. int newg = i * fg_g + ((2*dchmax-1) - i) * bg_g;
  771. int newb = i * fg_b + ((2*dchmax-1) - i) * bg_b;
  772. dist = abs(rgba[0] * (2*dchmax-1) - newr)
  773. + abs(rgba[1] * (2*dchmax-1) - newg)
  774. + abs(rgba[2] * (2*dchmax-1) - newb);
  775. if(dist < distmin)
  776. {
  777. ch = i;
  778. distmin = dist;
  779. }
  780. }
  781. outch = d->glyphs[ch];
  782. /* XXX: OMG HAX */
  783. if(d->init_dither == init_fstein_dither)
  784. {
  785. error[0] = rgba[0] - (fg_r * ch + bg_r * ((2*dchmax-1) - ch)) / (2*dchmax-1);
  786. error[1] = rgba[1] - (fg_g * ch + bg_g * ((2*dchmax-1) - ch)) / (2*dchmax-1);
  787. error[2] = rgba[2] - (fg_b * ch + bg_b * ((2*dchmax-1) - ch)) / (2*dchmax-1);
  788. }
  789. }
  790. else
  791. {
  792. unsigned int lum = rgba[0];
  793. if(rgba[1] > lum) lum = rgba[1];
  794. if(rgba[2] > lum) lum = rgba[2];
  795. outfg = outbg;
  796. outbg = CUCUL_COLOR_BLACK;
  797. ch = lum * dchmax / 0x1000;
  798. if(ch < 0)
  799. ch = 0;
  800. else if(ch > (int)(dchmax - 1))
  801. ch = dchmax - 1;
  802. outch = d->glyphs[ch];
  803. /* XXX: OMG HAX */
  804. if(d->init_dither == init_fstein_dither)
  805. {
  806. error[0] = rgba[0] - bg_r * ch / (dchmax-1);
  807. error[1] = rgba[1] - bg_g * ch / (dchmax-1);
  808. error[2] = rgba[2] - bg_b * ch / (dchmax-1);
  809. }
  810. }
  811. /* XXX: OMG HAX */
  812. if(d->init_dither == init_fstein_dither)
  813. {
  814. remain_r = fs_r[x+1] + 7 * error[0] / 16;
  815. remain_g = fs_g[x+1] + 7 * error[1] / 16;
  816. remain_b = fs_b[x+1] + 7 * error[2] / 16;
  817. fs_r[x-1] += 3 * error[0] / 16;
  818. fs_g[x-1] += 3 * error[1] / 16;
  819. fs_b[x-1] += 3 * error[2] / 16;
  820. fs_r[x] = 5 * error[0] / 16;
  821. fs_g[x] = 5 * error[1] / 16;
  822. fs_b[x] = 5 * error[2] / 16;
  823. fs_r[x+1] = 1 * error[0] / 16;
  824. fs_g[x+1] = 1 * error[1] / 16;
  825. fs_b[x+1] = 1 * error[2] / 16;
  826. }
  827. if(d->invert)
  828. {
  829. outfg = 15 - outfg;
  830. outbg = 15 - outbg;
  831. }
  832. /* Now output the character */
  833. cucul_set_color(cv, outfg, outbg);
  834. cucul_putstr(cv, x, y, outch);
  835. d->increment_dither();
  836. }
  837. /* end loop */
  838. }
  839. free(floyd_steinberg);
  840. }
  841. /** \brief Free the memory associated with a dither.
  842. *
  843. * Free the memory allocated by cucul_create_dither().
  844. *
  845. * \param d Dither object.
  846. */
  847. void cucul_free_dither(cucul_dither_t *d)
  848. {
  849. if(!d)
  850. return;
  851. free(d);
  852. }
  853. /*
  854. * XXX: The following functions are local.
  855. */
  856. /* Convert a mask, eg. 0x0000ff00, to shift values, eg. 8 and -4. */
  857. static void mask2shift(unsigned int mask, int *right, int *left)
  858. {
  859. int rshift = 0, lshift = 0;
  860. if(!mask)
  861. {
  862. *right = *left = 0;
  863. return;
  864. }
  865. while(!(mask & 1))
  866. {
  867. mask >>= 1;
  868. rshift++;
  869. }
  870. *right = rshift;
  871. while(mask & 1)
  872. {
  873. mask >>= 1;
  874. lshift++;
  875. }
  876. *left = 12 - lshift;
  877. }
  878. /* Compute x^y without relying on the math library */
  879. static float gammapow(float x, float y)
  880. {
  881. #ifdef HAVE_FLDLN2
  882. register double logx;
  883. register long double v, e;
  884. #else
  885. register float tmp, t, t2, r;
  886. int i;
  887. #endif
  888. if(x == 0.0)
  889. return y == 0.0 ? 1.0 : 0.0;
  890. #ifdef HAVE_FLDLN2
  891. /* FIXME: this can be optimised by directly calling fyl2x for x and y */
  892. asm volatile("fldln2; fxch; fyl2x"
  893. : "=t" (logx) : "0" (x) : "st(1)");
  894. asm volatile("fldl2e\n\t"
  895. "fmul %%st(1)\n\t"
  896. "fst %%st(1)\n\t"
  897. "frndint\n\t"
  898. "fxch\n\t"
  899. "fsub %%st(1)\n\t"
  900. "f2xm1\n\t"
  901. : "=t" (v), "=u" (e) : "0" (y * logx));
  902. v += 1.0;
  903. asm volatile("fscale"
  904. : "=t" (v) : "0" (v), "u" (e));
  905. return v;
  906. #else
  907. /* Compute ln(x) for x ∈ ]0,1]
  908. * ln(x) = 2 * (t + t^3/3 + t^5/5 + ...) with t = (x-1)/(x+1)
  909. * The convergence is a bit slow, especially when x is near 0. */
  910. t = (x - 1.0) / (x + 1.0);
  911. t2 = t * t;
  912. tmp = r = t;
  913. for(i = 3; i < 20; i += 2)
  914. {
  915. r *= t2;
  916. tmp += r / i;
  917. }
  918. /* Compute -y*ln(x) */
  919. tmp = - y * 2.0 * tmp;
  920. /* Compute x^-y as e^t where t = -y*ln(x):
  921. * e^t = 1 + t/1! + t^2/2! + t^3/3! + t^4/4! + t^5/5! ...
  922. * The convergence is quite faster here, thanks to the factorial. */
  923. r = t = tmp;
  924. tmp = 1.0 + t;
  925. for(i = 2; i < 16; i++)
  926. {
  927. r = r * t / i;
  928. tmp += r;
  929. }
  930. /* Return x^y as 1/(x^-y) */
  931. return 1.0 / tmp;
  932. #endif
  933. }
  934. static void get_rgba_default(cucul_dither_t const *d, uint8_t *pixels,
  935. int x, int y, unsigned int *rgba)
  936. {
  937. uint32_t bits;
  938. pixels += (d->bpp / 8) * x + d->pitch * y;
  939. switch(d->bpp / 8)
  940. {
  941. case 4:
  942. bits = *(uint32_t *)pixels;
  943. break;
  944. case 3:
  945. {
  946. #if defined(HAVE_ENDIAN_H)
  947. if(__BYTE_ORDER == __BIG_ENDIAN)
  948. #else
  949. /* This is compile-time optimised with at least -O1 or -Os */
  950. uint32_t const rmask = 0x12345678;
  951. if(*(uint8_t const *)&rmask == 0x12)
  952. #endif
  953. bits = ((uint32_t)pixels[0] << 16) |
  954. ((uint32_t)pixels[1] << 8) |
  955. ((uint32_t)pixels[2]);
  956. else
  957. bits = ((uint32_t)pixels[2] << 16) |
  958. ((uint32_t)pixels[1] << 8) |
  959. ((uint32_t)pixels[0]);
  960. break;
  961. }
  962. case 2:
  963. bits = *(uint16_t *)pixels;
  964. break;
  965. case 1:
  966. default:
  967. bits = pixels[0];
  968. break;
  969. }
  970. if(d->has_palette)
  971. {
  972. rgba[0] += d->gammatab[d->red[bits]];
  973. rgba[1] += d->gammatab[d->green[bits]];
  974. rgba[2] += d->gammatab[d->blue[bits]];
  975. rgba[3] += d->alpha[bits];
  976. }
  977. else
  978. {
  979. rgba[0] += d->gammatab[((bits & d->rmask) >> d->rright) << d->rleft];
  980. rgba[1] += d->gammatab[((bits & d->gmask) >> d->gright) << d->gleft];
  981. rgba[2] += d->gammatab[((bits & d->bmask) >> d->bright) << d->bleft];
  982. rgba[3] += ((bits & d->amask) >> d->aright) << d->aleft;
  983. }
  984. }
  985. /*
  986. * No dithering
  987. */
  988. static void init_no_dither(int line)
  989. {
  990. ;
  991. }
  992. static unsigned int get_no_dither(void)
  993. {
  994. return 0x80;
  995. }
  996. static void increment_no_dither(void)
  997. {
  998. return;
  999. }
  1000. /*
  1001. * Floyd-Steinberg dithering
  1002. */
  1003. static void init_fstein_dither(int line)
  1004. {
  1005. ;
  1006. }
  1007. static unsigned int get_fstein_dither(void)
  1008. {
  1009. return 0x80;
  1010. }
  1011. static void increment_fstein_dither(void)
  1012. {
  1013. return;
  1014. }
  1015. /*
  1016. * Ordered 2 dithering
  1017. */
  1018. static unsigned int const *ordered2_table;
  1019. static unsigned int ordered2_index;
  1020. static void init_ordered2_dither(int line)
  1021. {
  1022. static unsigned int const dither2x2[] =
  1023. {
  1024. 0x00, 0x80,
  1025. 0xc0, 0x40,
  1026. };
  1027. ordered2_table = dither2x2 + (line % 2) * 2;
  1028. ordered2_index = 0;
  1029. }
  1030. static unsigned int get_ordered2_dither(void)
  1031. {
  1032. return ordered2_table[ordered2_index];
  1033. }
  1034. static void increment_ordered2_dither(void)
  1035. {
  1036. ordered2_index = (ordered2_index + 1) % 2;
  1037. }
  1038. /*
  1039. * Ordered 4 dithering
  1040. */
  1041. /*static int dither4x4[] = { 5, 0, 1, 6,
  1042. -1, -6, -5, 2,
  1043. -2, -7, -8, 3,
  1044. 4, -3, -4, -7};*/
  1045. static unsigned int const *ordered4_table;
  1046. static unsigned int ordered4_index;
  1047. static void init_ordered4_dither(int line)
  1048. {
  1049. static unsigned int const dither4x4[] =
  1050. {
  1051. 0x00, 0x80, 0x20, 0xa0,
  1052. 0xc0, 0x40, 0xe0, 0x60,
  1053. 0x30, 0xb0, 0x10, 0x90,
  1054. 0xf0, 0x70, 0xd0, 0x50
  1055. };
  1056. ordered4_table = dither4x4 + (line % 4) * 4;
  1057. ordered4_index = 0;
  1058. }
  1059. static unsigned int get_ordered4_dither(void)
  1060. {
  1061. return ordered4_table[ordered4_index];
  1062. }
  1063. static void increment_ordered4_dither(void)
  1064. {
  1065. ordered4_index = (ordered4_index + 1) % 4;
  1066. }
  1067. /*
  1068. * Ordered 8 dithering
  1069. */
  1070. static unsigned int const *ordered8_table;
  1071. static unsigned int ordered8_index;
  1072. static void init_ordered8_dither(int line)
  1073. {
  1074. static unsigned int const dither8x8[] =
  1075. {
  1076. 0x00, 0x80, 0x20, 0xa0, 0x08, 0x88, 0x28, 0xa8,
  1077. 0xc0, 0x40, 0xe0, 0x60, 0xc8, 0x48, 0xe8, 0x68,
  1078. 0x30, 0xb0, 0x10, 0x90, 0x38, 0xb8, 0x18, 0x98,
  1079. 0xf0, 0x70, 0xd0, 0x50, 0xf8, 0x78, 0xd8, 0x58,
  1080. 0x0c, 0x8c, 0x2c, 0xac, 0x04, 0x84, 0x24, 0xa4,
  1081. 0xcc, 0x4c, 0xec, 0x6c, 0xc4, 0x44, 0xe4, 0x64,
  1082. 0x3c, 0xbc, 0x1c, 0x9c, 0x34, 0xb4, 0x14, 0x94,
  1083. 0xfc, 0x7c, 0xdc, 0x5c, 0xf4, 0x74, 0xd4, 0x54,
  1084. };
  1085. ordered8_table = dither8x8 + (line % 8) * 8;
  1086. ordered8_index = 0;
  1087. }
  1088. static unsigned int get_ordered8_dither(void)
  1089. {
  1090. return ordered8_table[ordered8_index];
  1091. }
  1092. static void increment_ordered8_dither(void)
  1093. {
  1094. ordered8_index = (ordered8_index + 1) % 8;
  1095. }
  1096. /*
  1097. * Random dithering
  1098. */
  1099. static void init_random_dither(int line)
  1100. {
  1101. ;
  1102. }
  1103. static unsigned int get_random_dither(void)
  1104. {
  1105. return cucul_rand(0x00, 0x100);
  1106. }
  1107. static void increment_random_dither(void)
  1108. {
  1109. return;
  1110. }
  1111. #if !defined(_DOXYGEN_SKIP_ME)
  1112. int _cucul_init_dither(void)
  1113. {
  1114. unsigned int v, s, h;
  1115. /* These ones are constant */
  1116. lookup_colors[0] = CUCUL_COLOR_BLACK;
  1117. lookup_colors[1] = CUCUL_COLOR_DARKGRAY;
  1118. lookup_colors[2] = CUCUL_COLOR_LIGHTGRAY;
  1119. lookup_colors[3] = CUCUL_COLOR_WHITE;
  1120. /* These ones will be overwritten */
  1121. lookup_colors[4] = CUCUL_COLOR_MAGENTA;
  1122. lookup_colors[5] = CUCUL_COLOR_LIGHTMAGENTA;
  1123. lookup_colors[6] = CUCUL_COLOR_RED;
  1124. lookup_colors[7] = CUCUL_COLOR_LIGHTRED;
  1125. for(v = 0; v < LOOKUP_VAL; v++)
  1126. for(s = 0; s < LOOKUP_SAT; s++)
  1127. for(h = 0; h < LOOKUP_HUE; h++)
  1128. {
  1129. int i, distbg, distfg, dist;
  1130. int val, sat, hue;
  1131. unsigned char outbg, outfg;
  1132. val = 0xfff * v / (LOOKUP_VAL - 1);
  1133. sat = 0xfff * s / (LOOKUP_SAT - 1);
  1134. hue = 0xfff * h / (LOOKUP_HUE - 1);
  1135. /* Initialise distances to the distance between pure black HSV
  1136. * coordinates and our white colour (3) */
  1137. outbg = outfg = 3;
  1138. distbg = distfg = HSV_DISTANCE(0, 0, 0, 3);
  1139. /* Calculate distances to eight major colour values and store the
  1140. * two nearest points in our lookup table. */
  1141. for(i = 0; i < 8; i++)
  1142. {
  1143. dist = HSV_DISTANCE(hue, sat, val, i);
  1144. if(dist <= distbg)
  1145. {
  1146. outfg = outbg;
  1147. distfg = distbg;
  1148. outbg = i;
  1149. distbg = dist;
  1150. }
  1151. else if(dist <= distfg)
  1152. {
  1153. outfg = i;
  1154. distfg = dist;
  1155. }
  1156. }
  1157. hsv_distances[v][s][h] = (outfg << 4) | outbg;
  1158. }
  1159. return 0;
  1160. }
  1161. int _cucul_end_dither(void)
  1162. {
  1163. return 0;
  1164. }
  1165. #endif /* _DOXYGEN_SKIP_ME */