25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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