Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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