Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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