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.
 
 
 
 
 
 

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