Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

1441 lignes
38 KiB

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