選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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