Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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