You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

961 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. 0xaaa, 0xaaa, 0xaaa,
  86. 0x555, 0x555, 0x555,
  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. float gamma;
  153. int gammatab[4097];
  154. };
  155. #endif
  156. static void mask2shift(unsigned int mask, int *right, int *left)
  157. {
  158. int rshift = 0, lshift = 0;
  159. if(!mask)
  160. {
  161. *right = *left = 0;
  162. return;
  163. }
  164. while(!(mask & 1))
  165. {
  166. mask >>= 1;
  167. rshift++;
  168. }
  169. *right = rshift;
  170. while(mask & 1)
  171. {
  172. mask >>= 1;
  173. lshift++;
  174. }
  175. *left = 12 - lshift;
  176. }
  177. /**
  178. * \brief Create an internal bitmap object.
  179. *
  180. * Create a bitmap structure from its coordinates (depth, width, height and
  181. * pitch) and pixel mask values. If the depth is 8 bits per pixel, the mask
  182. * values are ignored and the colour palette should be set using the
  183. * caca_set_bitmap_palette() function. For depths greater than 8 bits per
  184. * pixel, a zero alpha mask causes the alpha values to be ignored.
  185. *
  186. * \param bpp Bitmap depth in bits per pixel.
  187. * \param w Bitmap width in pixels.
  188. * \param h Bitmap height in pixels.
  189. * \param pitch Bitmap pitch in bytes.
  190. * \param rmask Bitmask for red values.
  191. * \param gmask Bitmask for green values.
  192. * \param bmask Bitmask for blue values.
  193. * \param amask Bitmask for alpha values.
  194. * \return Bitmap object, or NULL upon error.
  195. */
  196. struct caca_bitmap *caca_create_bitmap(unsigned int bpp, unsigned int w,
  197. unsigned int h, unsigned int pitch,
  198. unsigned int rmask, unsigned int gmask,
  199. unsigned int bmask, unsigned int amask)
  200. {
  201. struct caca_bitmap *bitmap;
  202. int i;
  203. /* Minor sanity test */
  204. if(!w || !h || !pitch || bpp > 32 || bpp < 8)
  205. return NULL;
  206. bitmap = malloc(sizeof(struct caca_bitmap));
  207. if(!bitmap)
  208. return NULL;
  209. bitmap->bpp = bpp;
  210. bitmap->has_palette = 0;
  211. bitmap->has_alpha = amask ? 1 : 0;
  212. bitmap->w = w;
  213. bitmap->h = h;
  214. bitmap->pitch = pitch;
  215. bitmap->rmask = rmask;
  216. bitmap->gmask = gmask;
  217. bitmap->bmask = bmask;
  218. bitmap->amask = amask;
  219. /* Load bitmasks */
  220. if(rmask || gmask || bmask || amask)
  221. {
  222. mask2shift(rmask, &bitmap->rright, &bitmap->rleft);
  223. mask2shift(gmask, &bitmap->gright, &bitmap->gleft);
  224. mask2shift(bmask, &bitmap->bright, &bitmap->bleft);
  225. mask2shift(amask, &bitmap->aright, &bitmap->aleft);
  226. }
  227. /* In 8 bpp mode, default to a grayscale palette */
  228. if(bpp == 8)
  229. {
  230. bitmap->has_palette = 1;
  231. bitmap->has_alpha = 0;
  232. for(i = 0; i < 256; i++)
  233. {
  234. bitmap->red[i] = i * 0xfff / 256;
  235. bitmap->green[i] = i * 0xfff / 256;
  236. bitmap->blue[i] = i * 0xfff / 256;
  237. }
  238. }
  239. /* Default gamma value */
  240. for(i = 0; i < 4096; i++)
  241. bitmap->gammatab[i] = i;
  242. return bitmap;
  243. }
  244. /**
  245. * \brief Set the palette of an 8bpp bitmap object.
  246. *
  247. * Set the palette of an 8 bits per pixel bitmap. Values should be between
  248. * 0 and 4095 (0xfff).
  249. *
  250. * \param bitmap Bitmap object.
  251. * \param red Array of 256 red values.
  252. * \param green Array of 256 green values.
  253. * \param blue Array of 256 blue values.
  254. * \param alpha Array of 256 alpha values.
  255. */
  256. void caca_set_bitmap_palette(struct caca_bitmap *bitmap,
  257. unsigned int red[], unsigned int green[],
  258. unsigned int blue[], unsigned int alpha[])
  259. {
  260. int i, has_alpha = 0;
  261. if(bitmap->bpp != 8)
  262. return;
  263. for(i = 0; i < 256; i++)
  264. {
  265. if(red[i] >= 0 && red[i] < 0x1000 &&
  266. green[i] >= 0 && green[i] < 0x1000 &&
  267. blue[i] >= 0 && blue[i] < 0x1000 &&
  268. alpha[i] >= 0 && alpha[i] < 0x1000)
  269. {
  270. bitmap->red[i] = red[i];
  271. bitmap->green[i] = green[i];
  272. bitmap->blue[i] = blue[i];
  273. if(alpha[i])
  274. {
  275. bitmap->alpha[i] = alpha[i];
  276. has_alpha = 1;
  277. }
  278. }
  279. }
  280. bitmap->has_alpha = has_alpha;
  281. }
  282. /**
  283. * \brief Set the gamma of a bitmap object.
  284. *
  285. * Set the gamma of bitmap.
  286. *
  287. * \param bitmap Bitmap object.
  288. * \param gamma Gamma value.
  289. */
  290. void caca_set_bitmap_gamma(struct caca_bitmap *bitmap, float gamma)
  291. {
  292. int i;
  293. if(gamma <= 0.0)
  294. return;
  295. bitmap->gamma = gamma;
  296. for(i = 0; i < 4096; i++)
  297. bitmap->gammatab[i] = 4096.0 * caca_powf((float)i / 4096.0, 1.0 / gamma);
  298. }
  299. /**
  300. * \brief Free the memory associated with a bitmap.
  301. *
  302. * Free the memory allocated by caca_create_bitmap().
  303. *
  304. * \param bitmap Bitmap object.
  305. */
  306. void caca_free_bitmap(struct caca_bitmap *bitmap)
  307. {
  308. if(!bitmap)
  309. return;
  310. free(bitmap);
  311. }
  312. static void get_rgba_default(struct caca_bitmap const *bitmap, uint8_t *pixels,
  313. int x, int y, unsigned int *r, unsigned int *g,
  314. unsigned int *b, unsigned int *a)
  315. {
  316. uint32_t bits;
  317. pixels += (bitmap->bpp / 8) * x + bitmap->pitch * y;
  318. switch(bitmap->bpp / 8)
  319. {
  320. case 4:
  321. bits = *(uint32_t *)pixels;
  322. break;
  323. case 3:
  324. {
  325. #if defined(HAVE_ENDIAN_H)
  326. if(__BYTE_ORDER == __BIG_ENDIAN)
  327. #else
  328. /* This is compile-time optimised with at least -O1 or -Os */
  329. uint32_t const rmask = 0x12345678;
  330. if(*(uint8_t const *)&rmask == 0x12)
  331. #endif
  332. bits = ((uint32_t)pixels[0] << 16) |
  333. ((uint32_t)pixels[1] << 8) |
  334. ((uint32_t)pixels[2]);
  335. else
  336. bits = ((uint32_t)pixels[2] << 16) |
  337. ((uint32_t)pixels[1] << 8) |
  338. ((uint32_t)pixels[0]);
  339. break;
  340. }
  341. case 2:
  342. bits = *(uint16_t *)pixels;
  343. break;
  344. case 1:
  345. default:
  346. bits = pixels[0];
  347. break;
  348. }
  349. if(bitmap->has_palette)
  350. {
  351. *r += bitmap->gammatab[bitmap->red[bits]];
  352. *g += bitmap->gammatab[bitmap->green[bits]];
  353. *b += bitmap->gammatab[bitmap->blue[bits]];
  354. *a += bitmap->alpha[bits];
  355. }
  356. else
  357. {
  358. *r += bitmap->gammatab[((bits & bitmap->rmask) >> bitmap->rright) << bitmap->rleft];
  359. *g += bitmap->gammatab[((bits & bitmap->gmask) >> bitmap->gright) << bitmap->gleft];
  360. *b += bitmap->gammatab[((bits & bitmap->bmask) >> bitmap->bright) << bitmap->bleft];
  361. *a += ((bits & bitmap->amask) >> bitmap->aright) << bitmap->aleft;
  362. }
  363. }
  364. static inline void rgb2hsv_default(int r, int g, int b,
  365. int *hue, int *sat, int *val)
  366. {
  367. int min, max, delta;
  368. min = r; max = r;
  369. if(min > g) min = g; if(max < g) max = g;
  370. if(min > b) min = b; if(max < b) max = b;
  371. delta = max - min; /* 0 - 0xfff */
  372. *val = max; /* 0 - 0xfff */
  373. if(delta)
  374. {
  375. *sat = 0xfff * delta / max; /* 0 - 0xfff */
  376. /* Generate *hue between 0 and 0x5fff */
  377. if( r == max )
  378. *hue = 0x1000 + 0x1000 * (g - b) / delta;
  379. else if( g == max )
  380. *hue = 0x3000 + 0x1000 * (b - r) / delta;
  381. else
  382. *hue = 0x5000 + 0x1000 * (r - g) / delta;
  383. }
  384. else
  385. {
  386. *sat = 0;
  387. *hue = 0;
  388. }
  389. }
  390. static inline int sq(int x)
  391. {
  392. return x * x;
  393. }
  394. /**
  395. * \brief Draw a bitmap on the screen.
  396. *
  397. * Draw a bitmap at the given coordinates. The bitmap can be of any size and
  398. * will be stretched to the text area.
  399. *
  400. * \param x1 X coordinate of the upper-left corner of the drawing area.
  401. * \param y1 Y coordinate of the upper-left corner of the drawing area.
  402. * \param x2 X coordinate of the lower-right corner of the drawing area.
  403. * \param y2 Y coordinate of the lower-right corner of the drawing area.
  404. * \param bitmap Bitmap object to be drawn.
  405. * \param pixels Bitmap's pixels.
  406. */
  407. void caca_draw_bitmap(int x1, int y1, int x2, int y2,
  408. struct caca_bitmap const *bitmap, void *pixels)
  409. {
  410. /* Current dithering method */
  411. void (*_init_dither) (int);
  412. unsigned int (*_get_dither) (void);
  413. void (*_increment_dither) (void);
  414. int *floyd_steinberg, *fs_r, *fs_g, *fs_b;
  415. int fs_length;
  416. /* FIXME: choose better characters! */
  417. #if !defined(_DOXYGEN_SKIP_ME)
  418. # define DCHMAX ((sizeof(density_chars)/sizeof(char const)/4)-1)
  419. #endif
  420. static char const density_chars[] =
  421. " "
  422. "...."
  423. "::::"
  424. ";=;="
  425. "tftf"
  426. "%$%$"
  427. "SK&Z"
  428. "XWGM"
  429. "@@@@"
  430. "8888"
  431. "####"
  432. "????";
  433. int x, y, w, h, pitch, deltax, deltay;
  434. if(!bitmap || !pixels)
  435. return;
  436. w = bitmap->w;
  437. h = bitmap->h;
  438. pitch = bitmap->pitch;
  439. if(x1 > x2)
  440. {
  441. int tmp = x2; x2 = x1; x1 = tmp;
  442. }
  443. if(y1 > y2)
  444. {
  445. int tmp = y2; y2 = y1; y1 = tmp;
  446. }
  447. deltax = x2 - x1 + 1;
  448. deltay = y2 - y1 + 1;
  449. switch(_caca_dithering)
  450. {
  451. case CACA_DITHERING_NONE:
  452. _init_dither = init_no_dither;
  453. _get_dither = get_no_dither;
  454. _increment_dither = increment_no_dither;
  455. break;
  456. case CACA_DITHERING_ORDERED2:
  457. _init_dither = init_ordered2_dither;
  458. _get_dither = get_ordered2_dither;
  459. _increment_dither = increment_ordered2_dither;
  460. break;
  461. case CACA_DITHERING_ORDERED4:
  462. _init_dither = init_ordered4_dither;
  463. _get_dither = get_ordered4_dither;
  464. _increment_dither = increment_ordered4_dither;
  465. break;
  466. case CACA_DITHERING_ORDERED8:
  467. _init_dither = init_ordered8_dither;
  468. _get_dither = get_ordered8_dither;
  469. _increment_dither = increment_ordered8_dither;
  470. break;
  471. case CACA_DITHERING_RANDOM:
  472. _init_dither = init_random_dither;
  473. _get_dither = get_random_dither;
  474. _increment_dither = increment_random_dither;
  475. break;
  476. case CACA_DITHERING_FSTEIN:
  477. _init_dither = init_no_dither;
  478. _get_dither = get_no_dither;
  479. _increment_dither = increment_no_dither;
  480. break;
  481. default:
  482. /* Something wicked happened! */
  483. return;
  484. }
  485. fs_length = ((int)_caca_width <= x2 ? (int)_caca_width : x2) + 1;
  486. floyd_steinberg = malloc(3 * (fs_length + 2) * sizeof(int));
  487. memset(floyd_steinberg, 0, 3 * (fs_length + 2) * sizeof(int));
  488. fs_r = floyd_steinberg + 1;
  489. fs_g = fs_r + fs_length + 2;
  490. fs_b = fs_g + fs_length + 2;
  491. for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)_caca_height; y++)
  492. {
  493. int remain_r = 0, remain_g = 0, remain_b = 0;
  494. for(x = x1 > 0 ? x1 : 0, _init_dither(y);
  495. x <= x2 && x <= (int)_caca_width;
  496. x++)
  497. {
  498. unsigned int i;
  499. int ch = 0, distmin;
  500. int r, g, b, a, fg_r = 0, fg_g = 0, fg_b = 0, bg_r, bg_g, bg_b;
  501. int fromx, fromy, tox, toy, myx, myy, dots, dist;
  502. int error[3];
  503. enum caca_color outfg = 0, outbg = 0;
  504. char outch;
  505. r = g = b = a = 0;
  506. /* First get RGB */
  507. if(_caca_antialiasing == CACA_ANTIALIASING_PREFILTER)
  508. {
  509. fromx = (x - x1) * w / deltax;
  510. fromy = (y - y1) * h / deltay;
  511. tox = (x - x1 + 1) * w / deltax;
  512. toy = (y - y1 + 1) * h / deltay;
  513. /* We want at least one pixel */
  514. if(tox == fromx) tox++;
  515. if(toy == fromy) toy++;
  516. dots = 0;
  517. for(myx = fromx; myx < tox; myx++)
  518. for(myy = fromy; myy < toy; myy++)
  519. {
  520. dots++;
  521. get_rgba_default(bitmap, pixels, myx, myy, &r, &g, &b, &a);
  522. }
  523. /* Normalize */
  524. r /= dots;
  525. g /= dots;
  526. b /= dots;
  527. a /= dots;
  528. }
  529. else
  530. {
  531. fromx = (x - x1) * w / deltax;
  532. fromy = (y - y1) * h / deltay;
  533. tox = (x - x1 + 1) * w / deltax;
  534. toy = (y - y1 + 1) * h / deltay;
  535. /* tox and toy can overflow the screen, but they cannot overflow
  536. * when averaged with fromx and fromy because these are guaranteed
  537. * to be within the pixel boundaries. */
  538. myx = (fromx + tox) / 2;
  539. myy = (fromy + toy) / 2;
  540. get_rgba_default(bitmap, pixels, myx, myy, &r, &g, &b, &a);
  541. }
  542. if(bitmap->has_alpha && a < 0x800)
  543. {
  544. remain_r = remain_g = remain_b = 0;
  545. fs_r[x] = 0;
  546. fs_g[x] = 0;
  547. fs_b[x] = 0;
  548. continue;
  549. }
  550. if(_caca_dithering == CACA_DITHERING_FSTEIN)
  551. {
  552. r += remain_r;
  553. g += remain_g;
  554. b += remain_b;
  555. }
  556. else
  557. {
  558. r += (_get_dither() - 0x80) * 4;
  559. g += (_get_dither() - 0x80) * 4;
  560. b += (_get_dither() - 0x80) * 4;
  561. }
  562. distmin = INT_MAX;
  563. for(i = 0; i < 16; i++)
  564. {
  565. dist = sq(r - rgb_palette[i * 3])
  566. + sq(g - rgb_palette[i * 3 + 1])
  567. + sq(b - rgb_palette[i * 3 + 2]);
  568. dist *= rgb_weight[i];
  569. if(dist < distmin)
  570. {
  571. outbg = i;
  572. distmin = dist;
  573. }
  574. }
  575. bg_r = rgb_palette[outbg * 3];
  576. bg_g = rgb_palette[outbg * 3 + 1];
  577. bg_b = rgb_palette[outbg * 3 + 2];
  578. if(_caca_background == CACA_BACKGROUND_SOLID)
  579. {
  580. distmin = INT_MAX;
  581. for(i = 0; i < 16; i++)
  582. {
  583. if(i == outbg)
  584. continue;
  585. dist = sq(r - rgb_palette[i * 3])
  586. + sq(g - rgb_palette[i * 3 + 1])
  587. + sq(b - rgb_palette[i * 3 + 2]);
  588. dist *= rgb_weight[i];
  589. if(dist < distmin)
  590. {
  591. outfg = i;
  592. distmin = dist;
  593. }
  594. }
  595. fg_r = rgb_palette[outfg * 3];
  596. fg_g = rgb_palette[outfg * 3 + 1];
  597. fg_b = rgb_palette[outfg * 3 + 2];
  598. distmin = INT_MAX;
  599. for(i = 0; i < DCHMAX - 1; i++)
  600. {
  601. int newr = i * fg_r + ((2*DCHMAX-1) - i) * bg_r;
  602. int newg = i * fg_g + ((2*DCHMAX-1) - i) * bg_g;
  603. int newb = i * fg_b + ((2*DCHMAX-1) - i) * bg_b;
  604. dist = abs(r * (2*DCHMAX-1) - newr)
  605. + abs(g * (2*DCHMAX-1) - newg)
  606. + abs(b * (2*DCHMAX-1) - newb);
  607. if(dist < distmin)
  608. {
  609. ch = i;
  610. distmin = dist;
  611. }
  612. }
  613. outch = density_chars[4 * ch];
  614. if(_caca_dithering == CACA_DITHERING_FSTEIN)
  615. {
  616. error[0] = r - (fg_r * ch + bg_r * ((2*DCHMAX-1) - ch)) / (2*DCHMAX-1);
  617. error[1] = g - (fg_g * ch + bg_g * ((2*DCHMAX-1) - ch)) / (2*DCHMAX-1);
  618. error[2] = b - (fg_b * ch + bg_b * ((2*DCHMAX-1) - ch)) / (2*DCHMAX-1);
  619. }
  620. }
  621. else
  622. {
  623. int lum = r; if(g > lum) lum = g; if(b > lum) lum = b;
  624. outfg = outbg;
  625. outbg = CACA_COLOR_BLACK;
  626. ch = lum * DCHMAX / 0x1000;
  627. if(ch < 0)
  628. ch = 0;
  629. else if(ch > (int)(DCHMAX - 1))
  630. ch = DCHMAX - 1;
  631. outch = density_chars[4 * ch];
  632. if(_caca_dithering == CACA_DITHERING_FSTEIN)
  633. {
  634. error[0] = r - bg_r * ch / (DCHMAX-1);
  635. error[1] = g - bg_g * ch / (DCHMAX-1);
  636. error[2] = b - bg_b * ch / (DCHMAX-1);
  637. }
  638. }
  639. if(_caca_dithering == CACA_DITHERING_FSTEIN)
  640. {
  641. remain_r = fs_r[x+1] + 7 * error[0] / 16;
  642. remain_g = fs_g[x+1] + 7 * error[1] / 16;
  643. remain_b = fs_b[x+1] + 7 * error[2] / 16;
  644. fs_r[x-1] += 3 * error[0] / 16;
  645. fs_g[x-1] += 3 * error[1] / 16;
  646. fs_b[x-1] += 3 * error[2] / 16;
  647. fs_r[x] = 5 * error[0] / 16;
  648. fs_g[x] = 5 * error[1] / 16;
  649. fs_b[x] = 5 * error[2] / 16;
  650. fs_r[x+1] = 1 * error[0] / 16;
  651. fs_g[x+1] = 1 * error[1] / 16;
  652. fs_b[x+1] = 1 * error[2] / 16;
  653. }
  654. /* Now output the character */
  655. caca_set_color(outfg, outbg);
  656. caca_putchar(x, y, outch);
  657. _increment_dither();
  658. }
  659. /* end loop */
  660. }
  661. free(floyd_steinberg);
  662. }
  663. #if !defined(_DOXYGEN_SKIP_ME)
  664. int _caca_init_bitmap(void)
  665. {
  666. unsigned int v, s, h;
  667. /* These ones are constant */
  668. lookup_colors[0] = CACA_COLOR_BLACK;
  669. lookup_colors[1] = CACA_COLOR_DARKGRAY;
  670. lookup_colors[2] = CACA_COLOR_LIGHTGRAY;
  671. lookup_colors[3] = CACA_COLOR_WHITE;
  672. /* These ones will be overwritten */
  673. lookup_colors[4] = CACA_COLOR_MAGENTA;
  674. lookup_colors[5] = CACA_COLOR_LIGHTMAGENTA;
  675. lookup_colors[6] = CACA_COLOR_RED;
  676. lookup_colors[7] = CACA_COLOR_LIGHTRED;
  677. for(v = 0; v < LOOKUP_VAL; v++)
  678. for(s = 0; s < LOOKUP_SAT; s++)
  679. for(h = 0; h < LOOKUP_HUE; h++)
  680. {
  681. int i, distbg, distfg, dist;
  682. int val, sat, hue;
  683. unsigned char outbg, outfg;
  684. val = 0xfff * v / (LOOKUP_VAL - 1);
  685. sat = 0xfff * s / (LOOKUP_SAT - 1);
  686. hue = 0xfff * h / (LOOKUP_HUE - 1);
  687. /* Initialise distances to the distance between pure black HSV
  688. * coordinates and our white colour (3) */
  689. outbg = outfg = 3;
  690. distbg = distfg = HSV_DISTANCE(0, 0, 0, 3);
  691. /* Calculate distances to eight major colour values and store the
  692. * two nearest points in our lookup table. */
  693. for(i = 0; i < 8; i++)
  694. {
  695. dist = HSV_DISTANCE(hue, sat, val, i);
  696. if(dist <= distbg)
  697. {
  698. outfg = outbg;
  699. distfg = distbg;
  700. outbg = i;
  701. distbg = dist;
  702. }
  703. else if(dist <= distfg)
  704. {
  705. outfg = i;
  706. distfg = dist;
  707. }
  708. }
  709. hsv_distances[v][s][h] = (outfg << 4) | outbg;
  710. }
  711. return 0;
  712. }
  713. int _caca_end_bitmap(void)
  714. {
  715. return 0;
  716. }
  717. #endif /* _DOXYGEN_SKIP_ME */
  718. /*
  719. * XXX: The following functions are local.
  720. */
  721. /*
  722. * No dithering
  723. */
  724. static void init_no_dither(int line)
  725. {
  726. ;
  727. }
  728. static unsigned int get_no_dither(void)
  729. {
  730. return 0x80;
  731. }
  732. static void increment_no_dither(void)
  733. {
  734. return;
  735. }
  736. /*
  737. * Ordered 2 dithering
  738. */
  739. static unsigned int *ordered2_table;
  740. static unsigned int ordered2_index;
  741. static void init_ordered2_dither(int line)
  742. {
  743. static unsigned int dither2x2[] =
  744. {
  745. 0x00, 0x80,
  746. 0xc0, 0x40,
  747. };
  748. ordered2_table = dither2x2 + (line % 2) * 2;
  749. ordered2_index = 0;
  750. }
  751. static unsigned int get_ordered2_dither(void)
  752. {
  753. return ordered2_table[ordered2_index];
  754. }
  755. static void increment_ordered2_dither(void)
  756. {
  757. ordered2_index = (ordered2_index + 1) % 2;
  758. }
  759. /*
  760. * Ordered 4 dithering
  761. */
  762. /*static int dither4x4[] = { 5, 0, 1, 6,
  763. -1, -6, -5, 2,
  764. -2, -7, -8, 3,
  765. 4, -3, -4, -7};*/
  766. static unsigned int *ordered4_table;
  767. static unsigned int ordered4_index;
  768. static void init_ordered4_dither(int line)
  769. {
  770. static unsigned int dither4x4[] =
  771. {
  772. 0x00, 0x80, 0x20, 0xa0,
  773. 0xc0, 0x40, 0xe0, 0x60,
  774. 0x30, 0xb0, 0x10, 0x90,
  775. 0xf0, 0x70, 0xd0, 0x50
  776. };
  777. ordered4_table = dither4x4 + (line % 4) * 4;
  778. ordered4_index = 0;
  779. }
  780. static unsigned int get_ordered4_dither(void)
  781. {
  782. return ordered4_table[ordered4_index];
  783. }
  784. static void increment_ordered4_dither(void)
  785. {
  786. ordered4_index = (ordered4_index + 1) % 4;
  787. }
  788. /*
  789. * Ordered 8 dithering
  790. */
  791. static unsigned int *ordered8_table;
  792. static unsigned int ordered8_index;
  793. static void init_ordered8_dither(int line)
  794. {
  795. static unsigned int dither8x8[] =
  796. {
  797. 0x00, 0x80, 0x20, 0xa0, 0x08, 0x88, 0x28, 0xa8,
  798. 0xc0, 0x40, 0xe0, 0x60, 0xc8, 0x48, 0xe8, 0x68,
  799. 0x30, 0xb0, 0x10, 0x90, 0x38, 0xb8, 0x18, 0x98,
  800. 0xf0, 0x70, 0xd0, 0x50, 0xf8, 0x78, 0xd8, 0x58,
  801. 0x0c, 0x8c, 0x2c, 0xac, 0x04, 0x84, 0x24, 0xa4,
  802. 0xcc, 0x4c, 0xec, 0x6c, 0xc4, 0x44, 0xe4, 0x64,
  803. 0x3c, 0xbc, 0x1c, 0x9c, 0x34, 0xb4, 0x14, 0x94,
  804. 0xfc, 0x7c, 0xdc, 0x5c, 0xf4, 0x74, 0xd4, 0x54,
  805. };
  806. ordered8_table = dither8x8 + (line % 8) * 8;
  807. ordered8_index = 0;
  808. }
  809. static unsigned int get_ordered8_dither(void)
  810. {
  811. return ordered8_table[ordered8_index];
  812. }
  813. static void increment_ordered8_dither(void)
  814. {
  815. ordered8_index = (ordered8_index + 1) % 8;
  816. }
  817. /*
  818. * Random dithering
  819. */
  820. static void init_random_dither(int line)
  821. {
  822. ;
  823. }
  824. static unsigned int get_random_dither(void)
  825. {
  826. return caca_rand(0x00, 0xff);
  827. }
  828. static void increment_random_dither(void)
  829. {
  830. return;
  831. }