Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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