You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1328 lines
35 KiB

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