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.

bitmap.c 25 KiB

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