947 lines
25 KiB

  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002, 2003 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 GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19. * 02111-1307 USA
  20. */
  21. /** \file bitmap.c
  22. * \version \$Id$
  23. * \author Sam Hocevar <sam@zoy.org>
  24. * \brief Bitmap blitting
  25. *
  26. * This file contains bitmap blitting functions.
  27. */
  28. #include "config.h"
  29. #if defined(HAVE_INTTYPES_H) || defined(_DOXYGEN_SKIP_ME)
  30. # include <inttypes.h>
  31. #else
  32. typedef unsigned char uint8_t;
  33. typedef unsigned short uint16_t;
  34. typedef unsigned int uint32_t;
  35. #endif
  36. #if defined(HAVE_ENDIAN_H)
  37. # include <endian.h>
  38. #endif
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <limits.h>
  42. #include <string.h>
  43. #include "caca.h"
  44. #include "caca_internals.h"
  45. /*
  46. * Global variables
  47. */
  48. #if !defined(_DOXYGEN_SKIP_ME)
  49. enum caca_feature _caca_background;
  50. enum caca_feature _caca_dithering;
  51. enum caca_feature _caca_antialiasing;
  52. #endif
  53. /*
  54. * Local variables
  55. */
  56. #if !defined(_DOXYGEN_SKIP_ME)
  57. # define LOOKUP_VAL 32
  58. # define LOOKUP_SAT 32
  59. # define LOOKUP_HUE 16
  60. #endif
  61. static unsigned char hsv_distances[LOOKUP_VAL][LOOKUP_SAT][LOOKUP_HUE];
  62. static enum caca_color lookup_colors[8];
  63. static int const hsv_palette[] =
  64. {
  65. /* weight, hue, saturation, value */
  66. 4, 0x0, 0x0, 0x0, /* black */
  67. 5, 0x0, 0x0, 0x5ff, /* 30% */
  68. 5, 0x0, 0x0, 0x9ff, /* 70% */
  69. 4, 0x0, 0x0, 0xfff, /* white */
  70. 3, 0x1000, 0xfff, 0x5ff, /* dark yellow */
  71. 2, 0x1000, 0xfff, 0xfff, /* light yellow */
  72. 3, 0x0, 0xfff, 0x5ff, /* dark red */
  73. 2, 0x0, 0xfff, 0xfff /* light red */
  74. };
  75. /* RGB palette for the new colour picker */
  76. static int rgb_palette[] =
  77. {
  78. 0x0, 0x0, 0x0,
  79. 0x0, 0x0, 0x7ff,
  80. 0x0, 0x7ff, 0x0,
  81. 0x0, 0x7ff, 0x7ff,
  82. 0x7ff, 0x0, 0x0,
  83. 0x7ff, 0x0, 0x7ff,
  84. 0x7ff, 0x7ff, 0x0,
  85. 0x7ff, 0x7ff, 0x7ff,
  86. 0x3ff, 0x3ff, 0x3ff,
  87. 0x000, 0x000, 0xfff,
  88. 0x000, 0xfff, 0x000,
  89. 0x000, 0xfff, 0xfff,
  90. 0xfff, 0x000, 0x000,
  91. 0xfff, 0x000, 0xfff,
  92. 0xfff, 0xfff, 0x000,
  93. 0xfff, 0xfff, 0xfff,
  94. };
  95. static int rgb_weight[] =
  96. {
  97. //2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2
  98. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  99. };
  100. #if !defined(_DOXYGEN_SKIP_ME)
  101. #define HSV_XRATIO 6
  102. #define HSV_YRATIO 3
  103. #define HSV_HRATIO 3
  104. #define HSV_DISTANCE(h, s, v, index) \
  105. (hsv_palette[index * 4] \
  106. * ((HSV_XRATIO * ((v) - hsv_palette[index * 4 + 3]) \
  107. * ((v) - hsv_palette[index * 4 + 3])) \
  108. + (hsv_palette[index * 4 + 3] \
  109. ? (HSV_YRATIO * ((s) - hsv_palette[index * 4 + 2]) \
  110. * ((s) - hsv_palette[index * 4 + 2])) \
  111. : 0) \
  112. + (hsv_palette[index * 4 + 2] \
  113. ? (HSV_HRATIO * ((h) - hsv_palette[index * 4 + 1]) \
  114. * ((h) - hsv_palette[index * 4 + 1])) \
  115. : 0)))
  116. #endif
  117. /*
  118. * Local prototypes
  119. */
  120. static void mask2shift(unsigned int, int *, int *);
  121. static void get_rgba_default(struct caca_bitmap const *, uint8_t *, int, int,
  122. unsigned int *, unsigned int *, unsigned int *,
  123. unsigned int *);
  124. static inline void rgb2hsv_default(int, int, int, int *, int *, int *);
  125. static inline int sq(int);
  126. /* Dithering methods */
  127. static void init_no_dither(int);
  128. static unsigned int get_no_dither(void);
  129. static void increment_no_dither(void);
  130. static void init_ordered2_dither(int);
  131. static unsigned int get_ordered2_dither(void);
  132. static void increment_ordered2_dither(void);
  133. static void init_ordered4_dither(int);
  134. static unsigned int get_ordered4_dither(void);
  135. static void increment_ordered4_dither(void);
  136. static void init_ordered8_dither(int);
  137. static unsigned int get_ordered8_dither(void);
  138. static void increment_ordered8_dither(void);
  139. static void init_random_dither(int);
  140. static unsigned int get_random_dither(void);
  141. static void increment_random_dither(void);
  142. #if !defined(_DOXYGEN_SKIP_ME)
  143. struct caca_bitmap
  144. {
  145. int bpp, has_palette, has_alpha;
  146. int w, h, pitch;
  147. int rmask, gmask, bmask, amask;
  148. int rright, gright, bright, aright;
  149. int rleft, gleft, bleft, aleft;
  150. void (*get_hsv)(struct caca_bitmap *, char *, int, int);
  151. int red[256], green[256], blue[256], alpha[256];
  152. };
  153. #endif
  154. static void mask2shift(unsigned int mask, int *right, int *left)
  155. {
  156. int rshift = 0, lshift = 0;
  157. if(!mask)
  158. {
  159. *right = *left = 0;
  160. return;
  161. }
  162. while(!(mask & 1))
  163. {
  164. mask >>= 1;
  165. rshift++;
  166. }
  167. *right = rshift;
  168. while(mask & 1)
  169. {
  170. mask >>= 1;
  171. lshift++;
  172. }
  173. *left = 12 - lshift;
  174. }
  175. /**
  176. * \brief Create an internal bitmap object.
  177. *
  178. * Create a bitmap structure from its coordinates (depth, width, height and
  179. * pitch) and pixel mask values. If the depth is 8 bits per pixel, the mask
  180. * values are ignored and the colour palette should be set using the
  181. * caca_set_bitmap_palette() function. For depths greater than 8 bits per
  182. * pixel, a zero alpha mask causes the alpha values to be ignored.
  183. *
  184. * \param bpp Bitmap depth in bits per pixel.
  185. * \param w Bitmap width in pixels.
  186. * \param h Bitmap height in pixels.
  187. * \param pitch Bitmap pitch in bytes.
  188. * \param rmask Bitmask for red values.
  189. * \param gmask Bitmask for green values.
  190. * \param bmask Bitmask for blue values.
  191. * \param amask Bitmask for alpha values.
  192. * \return Bitmap object, or NULL upon error.
  193. */
  194. struct caca_bitmap *caca_create_bitmap(unsigned int bpp, unsigned int w,
  195. unsigned int h, unsigned int pitch,
  196. unsigned int rmask, unsigned int gmask,
  197. unsigned int bmask, unsigned int amask)
  198. {
  199. struct caca_bitmap *bitmap;
  200. /* Minor sanity test */
  201. if(!w || !h || !pitch || bpp > 32 || bpp < 8)
  202. return NULL;
  203. bitmap = malloc(sizeof(struct caca_bitmap));
  204. if(!bitmap)
  205. return NULL;
  206. bitmap->bpp = bpp;
  207. bitmap->has_palette = 0;
  208. bitmap->has_alpha = amask ? 1 : 0;
  209. bitmap->w = w;
  210. bitmap->h = h;
  211. bitmap->pitch = pitch;
  212. bitmap->rmask = rmask;
  213. bitmap->gmask = gmask;
  214. bitmap->bmask = bmask;
  215. bitmap->amask = amask;
  216. /* Load bitmasks */
  217. if(rmask || gmask || bmask || amask)
  218. {
  219. mask2shift(rmask, &bitmap->rright, &bitmap->rleft);
  220. mask2shift(gmask, &bitmap->gright, &bitmap->gleft);
  221. mask2shift(bmask, &bitmap->bright, &bitmap->bleft);
  222. mask2shift(amask, &bitmap->aright, &bitmap->aleft);
  223. }
  224. /* In 8 bpp mode, default to a grayscale palette */
  225. if(bpp == 8)
  226. {
  227. int i;
  228. bitmap->has_palette = 1;
  229. bitmap->has_alpha = 0;
  230. for(i = 0; i < 256; i++)
  231. {
  232. bitmap->red[i] = i * 0xfff / 256;
  233. bitmap->green[i] = i * 0xfff / 256;
  234. bitmap->blue[i] = i * 0xfff / 256;
  235. }
  236. }
  237. return bitmap;
  238. }
  239. /**
  240. * \brief Set the palette of an 8bpp bitmap object.
  241. *
  242. * Set the palette of an 8 bits per pixel bitmap. Values should be between
  243. * 0 and 4095 (0xfff).
  244. *
  245. * \param bitmap Bitmap object.
  246. * \param red Array of 256 red values.
  247. * \param green Array of 256 green values.
  248. * \param blue Array of 256 blue values.
  249. * \param alpha Array of 256 alpha values.
  250. */
  251. void caca_set_bitmap_palette(struct caca_bitmap *bitmap,
  252. unsigned int red[], unsigned int green[],
  253. unsigned int blue[], unsigned int alpha[])
  254. {
  255. int i, has_alpha = 0;
  256. if(bitmap->bpp != 8)
  257. return;
  258. for(i = 0; i < 256; i++)
  259. {
  260. if(red[i] >= 0 && red[i] < 0x1000 &&
  261. green[i] >= 0 && green[i] < 0x1000 &&
  262. blue[i] >= 0 && blue[i] < 0x1000 &&
  263. alpha[i] >= 0 && alpha[i] < 0x1000)
  264. {
  265. bitmap->red[i] = red[i];
  266. bitmap->green[i] = green[i];
  267. bitmap->blue[i] = blue[i];
  268. if(alpha[i])
  269. {
  270. bitmap->alpha[i] = alpha[i];
  271. has_alpha = 1;
  272. }
  273. }
  274. }
  275. bitmap->has_alpha = has_alpha;
  276. }
  277. /**
  278. * \brief Free the memory associated with a bitmap.
  279. *
  280. * Free the memory allocated by caca_create_bitmap().
  281. *
  282. * \param bitmap Bitmap object.
  283. */
  284. void caca_free_bitmap(struct caca_bitmap *bitmap)
  285. {
  286. if(!bitmap)
  287. return;
  288. free(bitmap);
  289. }
  290. static void get_rgba_default(struct caca_bitmap const *bitmap, uint8_t *pixels,
  291. int x, int y, unsigned int *r, unsigned int *g,
  292. unsigned int *b, unsigned int *a)
  293. {
  294. uint32_t bits;
  295. pixels += (bitmap->bpp / 8) * x + bitmap->pitch * y;
  296. switch(bitmap->bpp / 8)
  297. {
  298. case 4:
  299. bits = *(uint32_t *)pixels;
  300. break;
  301. case 3:
  302. {
  303. #if defined(HAVE_ENDIAN_H)
  304. if(__BYTE_ORDER == __BIG_ENDIAN)
  305. #else
  306. /* This is compile-time optimised with at least -O1 or -Os */
  307. uint32_t const rmask = 0x12345678;
  308. if(*(uint8_t const *)&rmask == 0x12)
  309. #endif
  310. bits = ((uint32_t)pixels[0] << 16) |
  311. ((uint32_t)pixels[1] << 8) |
  312. ((uint32_t)pixels[2]);
  313. else
  314. bits = ((uint32_t)pixels[2] << 16) |
  315. ((uint32_t)pixels[1] << 8) |
  316. ((uint32_t)pixels[0]);
  317. break;
  318. }
  319. case 2:
  320. bits = *(uint16_t *)pixels;
  321. break;
  322. case 1:
  323. default:
  324. bits = pixels[0];
  325. break;
  326. }
  327. if(bitmap->has_palette)
  328. {
  329. *r += bitmap->red[bits];
  330. *g += bitmap->green[bits];
  331. *b += bitmap->blue[bits];
  332. *a += bitmap->alpha[bits];
  333. }
  334. else
  335. {
  336. *r += ((bits & bitmap->rmask) >> bitmap->rright) << bitmap->rleft;
  337. *g += ((bits & bitmap->gmask) >> bitmap->gright) << bitmap->gleft;
  338. *b += ((bits & bitmap->bmask) >> bitmap->bright) << bitmap->bleft;
  339. *a += ((bits & bitmap->amask) >> bitmap->aright) << bitmap->aleft;
  340. }
  341. }
  342. static inline void rgb2hsv_default(int r, int g, int b,
  343. int *hue, int *sat, int *val)
  344. {
  345. int min, max, delta;
  346. min = r; max = r;
  347. if(min > g) min = g; if(max < g) max = g;
  348. if(min > b) min = b; if(max < b) max = b;
  349. delta = max - min; /* 0 - 0xfff */
  350. *val = max; /* 0 - 0xfff */
  351. if(delta)
  352. {
  353. *sat = 0xfff * delta / max; /* 0 - 0xfff */
  354. /* Generate *hue between 0 and 0x5fff */
  355. if( r == max )
  356. *hue = 0x1000 + 0x1000 * (g - b) / delta;
  357. else if( g == max )
  358. *hue = 0x3000 + 0x1000 * (b - r) / delta;
  359. else
  360. *hue = 0x5000 + 0x1000 * (r - g) / delta;
  361. }
  362. else
  363. {
  364. *sat = 0;
  365. *hue = 0;
  366. }
  367. }
  368. static inline int sq(int x)
  369. {
  370. return x * x;
  371. }
  372. /**
  373. * \brief Draw a bitmap on the screen.
  374. *
  375. * Draw a bitmap at the given coordinates. The bitmap can be of any size and
  376. * will be stretched to the text area.
  377. *
  378. * \param x1 X coordinate of the upper-left corner of the drawing area.
  379. * \param y1 Y coordinate of the upper-left corner of the drawing area.
  380. * \param x2 X coordinate of the lower-right corner of the drawing area.
  381. * \param y2 Y coordinate of the lower-right corner of the drawing area.
  382. * \param bitmap Bitmap object to be drawn.
  383. * \param pixels Bitmap's pixels.
  384. */
  385. void caca_draw_bitmap(int x1, int y1, int x2, int y2,
  386. struct caca_bitmap const *bitmap, void *pixels)
  387. {
  388. /* Current dithering method */
  389. void (*_init_dither) (int);
  390. unsigned int (*_get_dither) (void);
  391. void (*_increment_dither) (void);
  392. int *floyd_steinberg, *fs_r, *fs_g, *fs_b;
  393. int fs_length;
  394. /* Only used when background is black */
  395. static int const white_colors[] =
  396. {
  397. CACA_COLOR_BLACK,
  398. CACA_COLOR_DARKGRAY,
  399. CACA_COLOR_LIGHTGRAY,
  400. CACA_COLOR_WHITE
  401. };
  402. static int const light_colors[] =
  403. {
  404. CACA_COLOR_LIGHTMAGENTA,
  405. CACA_COLOR_LIGHTRED,
  406. CACA_COLOR_YELLOW,
  407. CACA_COLOR_LIGHTGREEN,
  408. CACA_COLOR_LIGHTCYAN,
  409. CACA_COLOR_LIGHTBLUE,
  410. CACA_COLOR_LIGHTMAGENTA
  411. };
  412. static int const dark_colors[] =
  413. {
  414. CACA_COLOR_MAGENTA,
  415. CACA_COLOR_RED,
  416. CACA_COLOR_BROWN,
  417. CACA_COLOR_GREEN,
  418. CACA_COLOR_CYAN,
  419. CACA_COLOR_BLUE,
  420. CACA_COLOR_MAGENTA
  421. };
  422. /* FIXME: choose better characters! */
  423. #if !defined(_DOXYGEN_SKIP_ME)
  424. # define DCHMAX ((sizeof(density_chars)/sizeof(char const)/4)-1)
  425. #endif
  426. static char const density_chars[] =
  427. " "
  428. "...."
  429. "::::"
  430. ";=;="
  431. "tftf"
  432. "%$%$"
  433. "SK&Z"
  434. "XWGM"
  435. "@@@@"
  436. "8888"
  437. "####"
  438. "????";
  439. int x, y, w, h, pitch, deltax, deltay;
  440. if(!bitmap || !pixels)
  441. return;
  442. w = bitmap->w;
  443. h = bitmap->h;
  444. pitch = bitmap->pitch;
  445. if(x1 > x2)
  446. {
  447. int tmp = x2; x2 = x1; x1 = tmp;
  448. }
  449. if(y1 > y2)
  450. {
  451. int tmp = y2; y2 = y1; y1 = tmp;
  452. }
  453. deltax = x2 - x1 + 1;
  454. deltay = y2 - y1 + 1;
  455. switch(_caca_dithering)
  456. {
  457. case CACA_DITHERING_NONE:
  458. _init_dither = init_no_dither;
  459. _get_dither = get_no_dither;
  460. _increment_dither = increment_no_dither;
  461. break;
  462. case CACA_DITHERING_ORDERED2:
  463. _init_dither = init_ordered2_dither;
  464. _get_dither = get_ordered2_dither;
  465. _increment_dither = increment_ordered2_dither;
  466. break;
  467. case CACA_DITHERING_ORDERED4:
  468. _init_dither = init_ordered4_dither;
  469. _get_dither = get_ordered4_dither;
  470. _increment_dither = increment_ordered4_dither;
  471. break;
  472. case CACA_DITHERING_ORDERED8:
  473. _init_dither = init_ordered8_dither;
  474. _get_dither = get_ordered8_dither;
  475. _increment_dither = increment_ordered8_dither;
  476. break;
  477. case CACA_DITHERING_RANDOM:
  478. _init_dither = init_random_dither;
  479. _get_dither = get_random_dither;
  480. _increment_dither = increment_random_dither;
  481. break;
  482. case CACA_DITHERING_FSTEIN:
  483. _init_dither = init_no_dither;
  484. _get_dither = get_no_dither;
  485. _increment_dither = increment_no_dither;
  486. break;
  487. default:
  488. /* Something wicked happened! */
  489. return;
  490. }
  491. fs_length = ((int)_caca_width <= x2 ? (int)_caca_width : x2) + 1;
  492. floyd_steinberg = malloc(3 * (fs_length + 2) * sizeof(int));
  493. memset(floyd_steinberg, 0, 3 * (fs_length + 2) * sizeof(int));
  494. fs_r = floyd_steinberg + 1;
  495. fs_g = fs_r + fs_length + 2;
  496. fs_b = fs_g + fs_length + 2;
  497. for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)_caca_height; y++)
  498. {
  499. int remain_r = 0, remain_g = 0, remain_b = 0;
  500. int remain_r_next = 0, remain_g_next = 0, remain_b_next = 0;
  501. for(x = x1 > 0 ? x1 : 0, _init_dither(y);
  502. x <= x2 && x <= (int)_caca_width;
  503. x++)
  504. {
  505. unsigned int i;
  506. int ch = 0, distmin;
  507. int r, g, b, a, fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
  508. int fromx, fromy, tox, toy, myx, myy, dots, dist;
  509. enum caca_color outfg = 0, outbg = 0;
  510. char outch;
  511. r = g = b = a = 0;
  512. /* First get RGB */
  513. if(_caca_antialiasing == CACA_ANTIALIASING_PREFILTER)
  514. {
  515. fromx = (x - x1) * w / deltax;
  516. fromy = (y - y1) * h / deltay;
  517. tox = (x - x1 + 1) * w / deltax;
  518. toy = (y - y1 + 1) * h / deltay;
  519. /* We want at least one pixel */
  520. if(tox == fromx) tox++;
  521. if(toy == fromy) toy++;
  522. dots = 0;
  523. for(myx = fromx; myx < tox; myx++)
  524. for(myy = fromy; myy < toy; myy++)
  525. {
  526. dots++;
  527. get_rgba_default(bitmap, pixels, myx, myy, &r, &g, &b, &a);
  528. }
  529. /* Normalize */
  530. r /= dots;
  531. g /= dots;
  532. b /= dots;
  533. a /= dots;
  534. }
  535. else
  536. {
  537. fromx = (x - x1) * w / deltax;
  538. fromy = (y - y1) * h / deltay;
  539. tox = (x - x1 + 1) * w / deltax;
  540. toy = (y - y1 + 1) * h / deltay;
  541. /* tox and toy can overflow the screen, but they cannot overflow
  542. * when averaged with fromx and fromy because these are guaranteed
  543. * to be within the pixel boundaries. */
  544. myx = (fromx + tox) / 2;
  545. myy = (fromy + toy) / 2;
  546. get_rgba_default(bitmap, pixels, myx, myy, &r, &g, &b, &a);
  547. }
  548. if(bitmap->has_alpha && a < 0x800)
  549. {
  550. remain_r = remain_g = remain_b = 0;
  551. fs_r[x] = 0;
  552. fs_g[x] = 0;
  553. fs_b[x] = 0;
  554. continue;
  555. }
  556. if(_caca_dithering == CACA_DITHERING_FSTEIN)
  557. {
  558. r += remain_r;
  559. g += remain_g;
  560. b += remain_b;
  561. r += remain_r_next;
  562. g += remain_g_next;
  563. b += remain_b_next;
  564. remain_r_next = fs_r[x+1];
  565. remain_g_next = fs_g[x+1];
  566. remain_b_next = fs_b[x+1];
  567. }
  568. else
  569. {
  570. r += (_get_dither() - 0x80) * 4;
  571. g += (_get_dither() - 0x80) * 4;
  572. b += (_get_dither() - 0x80) * 4;
  573. }
  574. distmin = INT_MAX;
  575. for(i = 0; i < 16; i++)
  576. {
  577. dist = sq(r - rgb_palette[i * 3])
  578. + sq(g - rgb_palette[i * 3 + 1])
  579. + sq(b - rgb_palette[i * 3 + 2]);
  580. dist *= rgb_weight[i];
  581. if(dist < distmin)
  582. {
  583. outbg = i;
  584. distmin = dist;
  585. }
  586. }
  587. bg_r = rgb_palette[outbg * 3];
  588. bg_g = rgb_palette[outbg * 3 + 1];
  589. bg_b = rgb_palette[outbg * 3 + 2];
  590. distmin = INT_MAX;
  591. for(i = 0; i < 16; i++)
  592. {
  593. if(i == outbg)
  594. continue;
  595. dist = sq(r - rgb_palette[i * 3])
  596. + sq(g - rgb_palette[i * 3 + 1])
  597. + sq(b - rgb_palette[i * 3 + 2]);
  598. dist *= rgb_weight[i];
  599. if(dist < distmin)
  600. {
  601. outfg = i;
  602. distmin = dist;
  603. }
  604. }
  605. fg_r = rgb_palette[outfg * 3];
  606. fg_g = rgb_palette[outfg * 3 + 1];
  607. fg_b = rgb_palette[outfg * 3 + 2];
  608. distmin = INT_MAX;
  609. for(i = 0; i < DCHMAX - 1; i++)
  610. {
  611. int newr = i * fg_r + ((2*DCHMAX-1) - i) * bg_r;
  612. int newg = i * fg_g + ((2*DCHMAX-1) - i) * bg_g;
  613. int newb = i * fg_b + ((2*DCHMAX-1) - i) * bg_b;
  614. dist = abs(r * (2*DCHMAX-1) - newr)
  615. + abs(g * (2*DCHMAX-1) - newg)
  616. + abs(b * (2*DCHMAX-1) - newb);
  617. if(dist < distmin)
  618. {
  619. ch = i;
  620. distmin = dist;
  621. }
  622. }
  623. outch = density_chars[4 * ch];
  624. if(_caca_dithering == CACA_DITHERING_FSTEIN)
  625. {
  626. remain_r = r - (fg_r * ch + bg_r * ((2*DCHMAX-1) - ch)) / (2*DCHMAX-1);
  627. remain_g = g - (fg_g * ch + bg_g * ((2*DCHMAX-1) - ch)) / (2*DCHMAX-1);
  628. remain_b = b - (fg_b * ch + bg_b * ((2*DCHMAX-1) - ch)) / (2*DCHMAX-1);
  629. remain_r_next = fs_r[x+1];
  630. remain_g_next = fs_g[x+1];
  631. remain_b_next = fs_b[x+1];
  632. fs_r[x-1] += 3 * remain_r / 16;
  633. fs_g[x-1] += 3 * remain_g / 16;
  634. fs_b[x-1] += 3 * remain_b / 16;
  635. fs_r[x] = 5 * remain_r / 16;
  636. fs_g[x] = 5 * remain_g / 16;
  637. fs_b[x] = 5 * remain_b / 16;
  638. fs_r[x+1] = 1 * remain_r / 16;
  639. fs_g[x+1] = 1 * remain_g / 16;
  640. fs_b[x+1] = 1 * remain_b / 16;
  641. remain_r = 7 * remain_r / 16;
  642. remain_g = 7 * remain_g / 16;
  643. remain_b = 7 * remain_b / 16;
  644. }
  645. /* Now output the character */
  646. caca_set_color(outfg, outbg);
  647. caca_putchar(x, y, outch);
  648. _increment_dither();
  649. }
  650. /* end loop */
  651. }
  652. free(floyd_steinberg);
  653. }
  654. #if !defined(_DOXYGEN_SKIP_ME)
  655. int _caca_init_bitmap(void)
  656. {
  657. unsigned int v, s, h;
  658. /* These ones are constant */
  659. lookup_colors[0] = CACA_COLOR_BLACK;
  660. lookup_colors[1] = CACA_COLOR_DARKGRAY;
  661. lookup_colors[2] = CACA_COLOR_LIGHTGRAY;
  662. lookup_colors[3] = CACA_COLOR_WHITE;
  663. /* These ones will be overwritten */
  664. lookup_colors[4] = CACA_COLOR_MAGENTA;
  665. lookup_colors[5] = CACA_COLOR_LIGHTMAGENTA;
  666. lookup_colors[6] = CACA_COLOR_RED;
  667. lookup_colors[7] = CACA_COLOR_LIGHTRED;
  668. for(v = 0; v < LOOKUP_VAL; v++)
  669. for(s = 0; s < LOOKUP_SAT; s++)
  670. for(h = 0; h < LOOKUP_HUE; h++)
  671. {
  672. int i, distbg, distfg, dist;
  673. int val, sat, hue;
  674. unsigned char outbg, outfg;
  675. val = 0xfff * v / (LOOKUP_VAL - 1);
  676. sat = 0xfff * s / (LOOKUP_SAT - 1);
  677. hue = 0xfff * h / (LOOKUP_HUE - 1);
  678. /* Initialise distances to the distance between pure black HSV
  679. * coordinates and our white colour (3) */
  680. outbg = outfg = 3;
  681. distbg = distfg = HSV_DISTANCE(0, 0, 0, 3);
  682. /* Calculate distances to eight major colour values and store the
  683. * two nearest points in our lookup table. */
  684. for(i = 0; i < 8; i++)
  685. {
  686. dist = HSV_DISTANCE(hue, sat, val, i);
  687. if(dist <= distbg)
  688. {
  689. outfg = outbg;
  690. distfg = distbg;
  691. outbg = i;
  692. distbg = dist;
  693. }
  694. else if(dist <= distfg)
  695. {
  696. outfg = i;
  697. distfg = dist;
  698. }
  699. }
  700. hsv_distances[v][s][h] = (outfg << 4) | outbg;
  701. }
  702. return 0;
  703. }
  704. int _caca_end_bitmap(void)
  705. {
  706. return 0;
  707. }
  708. #endif /* _DOXYGEN_SKIP_ME */
  709. /*
  710. * XXX: The following functions are local.
  711. */
  712. /*
  713. * No dithering
  714. */
  715. static void init_no_dither(int line)
  716. {
  717. ;
  718. }
  719. static unsigned int get_no_dither(void)
  720. {
  721. return 0x80;
  722. }
  723. static void increment_no_dither(void)
  724. {
  725. return;
  726. }
  727. /*
  728. * Ordered 2 dithering
  729. */
  730. static unsigned int *ordered2_table;
  731. static unsigned int ordered2_index;
  732. static void init_ordered2_dither(int line)
  733. {
  734. static unsigned int dither2x2[] =
  735. {
  736. 0x00, 0x80,
  737. 0xc0, 0x40,
  738. };
  739. ordered2_table = dither2x2 + (line % 2) * 2;
  740. ordered2_index = 0;
  741. }
  742. static unsigned int get_ordered2_dither(void)
  743. {
  744. return ordered2_table[ordered2_index];
  745. }
  746. static void increment_ordered2_dither(void)
  747. {
  748. ordered2_index = (ordered2_index + 1) % 2;
  749. }
  750. /*
  751. * Ordered 4 dithering
  752. */
  753. /*static int dither4x4[] = { 5, 0, 1, 6,
  754. -1, -6, -5, 2,
  755. -2, -7, -8, 3,
  756. 4, -3, -4, -7};*/
  757. static unsigned int *ordered4_table;
  758. static unsigned int ordered4_index;
  759. static void init_ordered4_dither(int line)
  760. {
  761. static unsigned int dither4x4[] =
  762. {
  763. 0x00, 0x80, 0x20, 0xa0,
  764. 0xc0, 0x40, 0xe0, 0x60,
  765. 0x30, 0xb0, 0x10, 0x90,
  766. 0xf0, 0x70, 0xd0, 0x50
  767. };
  768. ordered4_table = dither4x4 + (line % 4) * 4;
  769. ordered4_index = 0;
  770. }
  771. static unsigned int get_ordered4_dither(void)
  772. {
  773. return ordered4_table[ordered4_index];
  774. }
  775. static void increment_ordered4_dither(void)
  776. {
  777. ordered4_index = (ordered4_index + 1) % 4;
  778. }
  779. /*
  780. * Ordered 8 dithering
  781. */
  782. static unsigned int *ordered8_table;
  783. static unsigned int ordered8_index;
  784. static void init_ordered8_dither(int line)
  785. {
  786. static unsigned int dither8x8[] =
  787. {
  788. 0x00, 0x80, 0x20, 0xa0, 0x08, 0x88, 0x28, 0xa8,
  789. 0xc0, 0x40, 0xe0, 0x60, 0xc8, 0x48, 0xe8, 0x68,
  790. 0x30, 0xb0, 0x10, 0x90, 0x38, 0xb8, 0x18, 0x98,
  791. 0xf0, 0x70, 0xd0, 0x50, 0xf8, 0x78, 0xd8, 0x58,
  792. 0x0c, 0x8c, 0x2c, 0xac, 0x04, 0x84, 0x24, 0xa4,
  793. 0xcc, 0x4c, 0xec, 0x6c, 0xc4, 0x44, 0xe4, 0x64,
  794. 0x3c, 0xbc, 0x1c, 0x9c, 0x34, 0xb4, 0x14, 0x94,
  795. 0xfc, 0x7c, 0xdc, 0x5c, 0xf4, 0x74, 0xd4, 0x54,
  796. };
  797. ordered8_table = dither8x8 + (line % 8) * 8;
  798. ordered8_index = 0;
  799. }
  800. static unsigned int get_ordered8_dither(void)
  801. {
  802. return ordered8_table[ordered8_index];
  803. }
  804. static void increment_ordered8_dither(void)
  805. {
  806. ordered8_index = (ordered8_index + 1) % 8;
  807. }
  808. /*
  809. * Random dithering
  810. */
  811. static void init_random_dither(int line)
  812. {
  813. ;
  814. }
  815. static unsigned int get_random_dither(void)
  816. {
  817. return caca_rand(0x00, 0xff);
  818. }
  819. static void increment_random_dither(void)
  820. {
  821. return;
  822. }