Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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