Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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