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.
 
 
 
 
 
 

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