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.
 
 
 
 
 
 

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