Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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