Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

993 linhas
26 KiB

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