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

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