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

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