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ů.
 
 
 
 
 
 

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