Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains bitmap dithering functions.
  16. */
  17. #include "config.h"
  18. #if !defined(__KERNEL__)
  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. #endif
  27. #include "caca.h"
  28. #include "caca_internals.h"
  29. #define CP437 0
  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 uint8_t hsv_distances[LOOKUP_VAL][LOOKUP_SAT][LOOKUP_HUE];
  39. static uint16_t lookup_colors[8];
  40. static int lookup_initialised = 0;
  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. /* List of glyphs */
  79. static uint32_t ascii_glyphs[] =
  80. {
  81. ' ', '.', ':', ';', 't', '%', 'S', 'X', '@', '8', '?'
  82. };
  83. static uint32_t shades_glyphs[] =
  84. {
  85. /* ' '. '·', '░', '▒', '?' */
  86. ' ', 0xb7, 0x2591, 0x2592, '?'
  87. };
  88. static uint32_t blocks_glyphs[] =
  89. {
  90. /* ' ', '▘', '▚', '?' */
  91. ' ', 0x2598, 0x259a, '?'
  92. };
  93. #if !defined(_DOXYGEN_SKIP_ME)
  94. enum color_mode
  95. {
  96. COLOR_MODE_MONO,
  97. COLOR_MODE_GRAY,
  98. COLOR_MODE_8,
  99. COLOR_MODE_16,
  100. COLOR_MODE_FULLGRAY,
  101. COLOR_MODE_FULL8,
  102. COLOR_MODE_FULL16
  103. };
  104. struct caca_dither
  105. {
  106. int bpp, has_palette, has_alpha;
  107. int w, h, pitch;
  108. int rmask, gmask, bmask, amask;
  109. int rright, gright, bright, aright;
  110. int rleft, gleft, bleft, aleft;
  111. void (*get_hsv)(caca_dither_t *, char *, int, int);
  112. int red[256], green[256], blue[256], alpha[256];
  113. /* Colour features */
  114. float gamma, brightness, contrast;
  115. int gammatab[4097];
  116. /* Dithering features */
  117. char const *antialias_name;
  118. int antialias;
  119. char const *color_name;
  120. enum color_mode color;
  121. char const *algo_name;
  122. void (*init_dither) (int);
  123. int (*get_dither) (void);
  124. void (*increment_dither) (void);
  125. char const *glyph_name;
  126. uint32_t const * glyphs;
  127. int glyph_count;
  128. int invert;
  129. };
  130. #define HSV_XRATIO 6
  131. #define HSV_YRATIO 3
  132. #define HSV_HRATIO 3
  133. #define HSV_DISTANCE(h, s, v, index) \
  134. (hsv_palette[index * 4] \
  135. * ((HSV_XRATIO * ((v) - hsv_palette[index * 4 + 3]) \
  136. * ((v) - hsv_palette[index * 4 + 3])) \
  137. + (hsv_palette[index * 4 + 3] \
  138. ? (HSV_YRATIO * ((s) - hsv_palette[index * 4 + 2]) \
  139. * ((s) - hsv_palette[index * 4 + 2])) \
  140. : 0) \
  141. + (hsv_palette[index * 4 + 2] \
  142. ? (HSV_HRATIO * ((h) - hsv_palette[index * 4 + 1]) \
  143. * ((h) - hsv_palette[index * 4 + 1])) \
  144. : 0)))
  145. #endif
  146. /*
  147. * Local prototypes
  148. */
  149. static void mask2shift(uint32_t, int *, int *);
  150. static float gammapow(float x, float y);
  151. static void get_rgba_default(caca_dither_t const *, uint8_t const *, int, int,
  152. unsigned int *);
  153. static int init_lookup(void);
  154. /* Dithering algorithms */
  155. static void init_no_dither(int);
  156. static int get_no_dither(void);
  157. static void increment_no_dither(void);
  158. static void init_fstein_dither(int);
  159. static int get_fstein_dither(void);
  160. static void increment_fstein_dither(void);
  161. static void init_ordered2_dither(int);
  162. static int get_ordered2_dither(void);
  163. static void increment_ordered2_dither(void);
  164. static void init_ordered4_dither(int);
  165. static int get_ordered4_dither(void);
  166. static void increment_ordered4_dither(void);
  167. static void init_ordered8_dither(int);
  168. static int get_ordered8_dither(void);
  169. static void increment_ordered8_dither(void);
  170. static void init_random_dither(int);
  171. static int get_random_dither(void);
  172. static void increment_random_dither(void);
  173. static inline int sq(int x)
  174. {
  175. return x * x;
  176. }
  177. static inline void rgb2hsv_default(int r, int g, int b,
  178. int *hue, int *sat, int *val)
  179. {
  180. int min, max, delta;
  181. min = r; max = r;
  182. if(min > g) min = g; if(max < g) max = g;
  183. if(min > b) min = b; if(max < b) max = b;
  184. delta = max - min; /* 0 - 0xfff */
  185. *val = max; /* 0 - 0xfff */
  186. if(delta)
  187. {
  188. *sat = 0xfff * delta / max; /* 0 - 0xfff */
  189. /* Generate *hue between 0 and 0x5fff */
  190. if( r == max )
  191. *hue = 0x1000 + 0x1000 * (g - b) / delta;
  192. else if( g == max )
  193. *hue = 0x3000 + 0x1000 * (b - r) / delta;
  194. else
  195. *hue = 0x5000 + 0x1000 * (r - g) / delta;
  196. }
  197. else
  198. {
  199. *sat = 0;
  200. *hue = 0;
  201. }
  202. }
  203. /** \brief Create an internal dither object.
  204. *
  205. * Create a dither structure from its coordinates (depth, width, height and
  206. * pitch) and pixel mask values. If the depth is 8 bits per pixel, the mask
  207. * values are ignored and the colour palette should be set using the
  208. * caca_set_dither_palette() function. For depths greater than 8 bits per
  209. * pixel, a zero alpha mask causes the alpha values to be ignored.
  210. *
  211. * If an error occurs, NULL is returned and \b errno is set accordingly:
  212. * - \c EINVAL Requested width, height, pitch or bits per pixel value was
  213. * invalid.
  214. * - \c ENOMEM Not enough memory to allocate dither structure.
  215. *
  216. * \param bpp Bitmap depth in bits per pixel.
  217. * \param w Bitmap width in pixels.
  218. * \param h Bitmap height in pixels.
  219. * \param pitch Bitmap pitch in bytes.
  220. * \param rmask Bitmask for red values.
  221. * \param gmask Bitmask for green values.
  222. * \param bmask Bitmask for blue values.
  223. * \param amask Bitmask for alpha values.
  224. * \return Dither object upon success, NULL if an error occurred.
  225. */
  226. caca_dither_t *caca_create_dither(int bpp, int w, int h, int pitch,
  227. uint32_t rmask, uint32_t gmask,
  228. uint32_t bmask, uint32_t amask)
  229. {
  230. caca_dither_t *d;
  231. int i;
  232. /* Minor sanity test */
  233. if(w < 0 || h < 0 || pitch < 0 || bpp > 32 || bpp < 8)
  234. {
  235. seterrno(EINVAL);
  236. return NULL;
  237. }
  238. d = malloc(sizeof(caca_dither_t));
  239. if(!d)
  240. {
  241. seterrno(ENOMEM);
  242. return NULL;
  243. }
  244. if(!lookup_initialised)
  245. {
  246. /* XXX: because we do not wish to be thread-safe, there is a slight
  247. * chance that the following code will be executed twice. It is
  248. * totally harmless. */
  249. init_lookup();
  250. lookup_initialised = 1;
  251. }
  252. d->bpp = bpp;
  253. d->has_palette = 0;
  254. d->has_alpha = amask ? 1 : 0;
  255. d->w = w;
  256. d->h = h;
  257. d->pitch = pitch;
  258. d->rmask = rmask;
  259. d->gmask = gmask;
  260. d->bmask = bmask;
  261. d->amask = amask;
  262. /* Load bitmasks */
  263. if(rmask || gmask || bmask || amask)
  264. {
  265. mask2shift(rmask, &d->rright, &d->rleft);
  266. mask2shift(gmask, &d->gright, &d->gleft);
  267. mask2shift(bmask, &d->bright, &d->bleft);
  268. mask2shift(amask, &d->aright, &d->aleft);
  269. }
  270. /* In 8 bpp mode, default to a grayscale palette */
  271. if(bpp == 8)
  272. {
  273. d->has_palette = 1;
  274. d->has_alpha = 0;
  275. for(i = 0; i < 256; i++)
  276. {
  277. d->red[i] = i * 0xfff / 256;
  278. d->green[i] = i * 0xfff / 256;
  279. d->blue[i] = i * 0xfff / 256;
  280. }
  281. }
  282. /* Default gamma value */
  283. d->gamma = 1.0;
  284. for(i = 0; i < 4096; i++)
  285. d->gammatab[i] = i;
  286. /* Default colour properties */
  287. d->brightness = 1.0;
  288. d->contrast = 1.0;
  289. /* Default features */
  290. d->antialias_name = "prefilter";
  291. d->antialias = 1;
  292. d->color_name = "full16";
  293. d->color = COLOR_MODE_FULL16;
  294. d->glyph_name = "ascii";
  295. d->glyphs = ascii_glyphs;
  296. d->glyph_count = sizeof(ascii_glyphs) / sizeof(*ascii_glyphs);
  297. d->algo_name = "fstein";
  298. d->init_dither = init_fstein_dither;
  299. d->get_dither = get_fstein_dither;
  300. d->increment_dither = increment_fstein_dither;
  301. d->invert = 0;
  302. return d;
  303. }
  304. /** \brief Set the palette of an 8bpp dither object.
  305. *
  306. * Set the palette of an 8 bits per pixel bitmap. Values should be between
  307. * 0 and 4095 (0xfff).
  308. *
  309. * If an error occurs, -1 is returned and \b errno is set accordingly:
  310. * - \c EINVAL Dither bits per pixel value is not 8, or one of the pixel
  311. * values was outside the range 0 - 4095.
  312. *
  313. * \param d Dither object.
  314. * \param red Array of 256 red values.
  315. * \param green Array of 256 green values.
  316. * \param blue Array of 256 blue values.
  317. * \param alpha Array of 256 alpha values.
  318. * \return 0 in case of success, -1 if an error occurred.
  319. */
  320. int caca_set_dither_palette(caca_dither_t *d,
  321. uint32_t red[], uint32_t green[],
  322. uint32_t blue[], uint32_t alpha[])
  323. {
  324. int i, has_alpha = 0;
  325. if(d->bpp != 8)
  326. {
  327. seterrno(EINVAL);
  328. return -1;
  329. }
  330. for(i = 0; i < 256; i++)
  331. {
  332. if((red[i] | green[i] | blue[i] | alpha[i]) >= 0x1000)
  333. {
  334. seterrno(EINVAL);
  335. return -1;
  336. }
  337. }
  338. for(i = 0; i < 256; i++)
  339. {
  340. d->red[i] = red[i];
  341. d->green[i] = green[i];
  342. d->blue[i] = blue[i];
  343. if(alpha[i])
  344. {
  345. d->alpha[i] = alpha[i];
  346. has_alpha = 1;
  347. }
  348. }
  349. d->has_alpha = has_alpha;
  350. return 0;
  351. }
  352. /** \brief Set the brightness of a dither object.
  353. *
  354. * Set the brightness of dither.
  355. *
  356. * If an error occurs, -1 is returned and \b errno is set accordingly:
  357. * - \c EINVAL Brightness value was out of range.
  358. *
  359. * \param d Dither object.
  360. * \param brightness brightness value.
  361. * \return 0 in case of success, -1 if an error occurred.
  362. */
  363. int caca_set_dither_brightness(caca_dither_t *d, float brightness)
  364. {
  365. /* FIXME */
  366. d->brightness = brightness;
  367. return 0;
  368. }
  369. /** \brief Get the brightness of a dither object.
  370. *
  371. * Get the brightness of the given dither object.
  372. *
  373. * This function never fails.
  374. *
  375. * \param d Dither object.
  376. * \return Brightness value.
  377. */
  378. float caca_get_dither_brightness(caca_dither_t const *d)
  379. {
  380. return d->brightness;
  381. }
  382. /** \brief Set the gamma of a dither object.
  383. *
  384. * Set the gamma of the given dither object. A negative value causes
  385. * colour inversion.
  386. *
  387. * If an error occurs, -1 is returned and \b errno is set accordingly:
  388. * - \c EINVAL Gamma value was out of range.
  389. *
  390. * \param d Dither object.
  391. * \param gamma Gamma value.
  392. * \return 0 in case of success, -1 if an error occurred.
  393. */
  394. int caca_set_dither_gamma(caca_dither_t *d, float gamma)
  395. {
  396. /* FIXME: we don't need 4096 calls to gammapow(), we could just compute
  397. * a few of them and do linear interpolation for the rest. This will
  398. * probably speed up things a lot. */
  399. int i;
  400. if(gamma < 0.0)
  401. {
  402. d->invert = 1;
  403. gamma = -gamma;
  404. }
  405. else if(gamma == 0.0)
  406. {
  407. seterrno(EINVAL);
  408. return -1;
  409. }
  410. d->gamma = gamma;
  411. for(i = 0; i < 4096; i++)
  412. d->gammatab[i] = 4096.0 * gammapow((float)i / 4096.0, 1.0 / gamma);
  413. return 0;
  414. }
  415. /** \brief Get the gamma of a dither object.
  416. *
  417. * Get the gamma of the given dither object.
  418. *
  419. * This function never fails.
  420. *
  421. * \param d Dither object.
  422. * \return Gamma value.
  423. */
  424. float caca_get_dither_gamma(caca_dither_t const *d)
  425. {
  426. return d->gamma;
  427. }
  428. /** \brief Set the contrast of a dither object.
  429. *
  430. * Set the contrast of dither.
  431. *
  432. * If an error occurs, -1 is returned and \b errno is set accordingly:
  433. * - \c EINVAL Contrast value was out of range.
  434. *
  435. * \param d Dither object.
  436. * \param contrast contrast value.
  437. * \return 0 in case of success, -1 if an error occurred.
  438. */
  439. int caca_set_dither_contrast(caca_dither_t *d, float contrast)
  440. {
  441. /* FIXME */
  442. d->contrast = contrast;
  443. return 0;
  444. }
  445. /** \brief Get the contrast of a dither object.
  446. *
  447. * Get the contrast of the given dither object.
  448. *
  449. * This function never fails.
  450. *
  451. * \param d Dither object.
  452. * \return Contrast value.
  453. */
  454. float caca_get_dither_contrast(caca_dither_t const *d)
  455. {
  456. return d->contrast;
  457. }
  458. /** \brief Set dither antialiasing
  459. *
  460. * Tell the renderer whether to antialias the dither. Antialiasing smoothens
  461. * the rendered image and avoids the commonly seen staircase effect.
  462. * - \c "none": no antialiasing.
  463. * - \c "prefilter" or \c "default": simple prefilter antialiasing. This
  464. * is the default value.
  465. *
  466. * If an error occurs, -1 is returned and \b errno is set accordingly:
  467. * - \c EINVAL Invalid antialiasing mode.
  468. *
  469. * \param d Dither object.
  470. * \param str A string describing the antialiasing method that will be used
  471. * for the dithering.
  472. * \return 0 in case of success, -1 if an error occurred.
  473. */
  474. int caca_set_dither_antialias(caca_dither_t *d, char const *str)
  475. {
  476. if(!strcasecmp(str, "none"))
  477. {
  478. d->antialias_name = "none";
  479. d->antialias = 0;
  480. }
  481. else if(!strcasecmp(str, "prefilter") || !strcasecmp(str, "default"))
  482. {
  483. d->antialias_name = "prefilter";
  484. d->antialias = 1;
  485. }
  486. else
  487. {
  488. seterrno(EINVAL);
  489. return -1;
  490. }
  491. return 0;
  492. }
  493. /** \brief Get available antialiasing methods
  494. *
  495. * Return a list of available antialiasing methods for a given dither. The
  496. * list is a NULL-terminated array of strings, interleaving a string
  497. * containing the internal value for the antialiasing method to be used with
  498. * caca_set_dither_antialias(), and a string containing the natural
  499. * language description for that antialiasing method.
  500. *
  501. * This function never fails.
  502. *
  503. * \param d Dither object.
  504. * \return An array of strings.
  505. */
  506. char const * const *
  507. caca_get_dither_antialias_list(caca_dither_t const *d)
  508. {
  509. static char const * const list[] =
  510. {
  511. "none", "No antialiasing",
  512. "prefilter", "Prefilter antialiasing",
  513. NULL, NULL
  514. };
  515. return list;
  516. }
  517. /** \brief Get current antialiasing method
  518. *
  519. * Return the given dither's current antialiasing method.
  520. *
  521. * This function never fails.
  522. *
  523. * \param d Dither object.
  524. * \return A static string.
  525. */
  526. char const * caca_get_dither_antialias(caca_dither_t const *d)
  527. {
  528. return d->antialias_name;
  529. }
  530. /** \brief Choose colours used for dithering
  531. *
  532. * Tell the renderer which colours should be used to render the
  533. * bitmap. Valid values for \c str are:
  534. * - \c "mono": use light gray on a black background.
  535. * - \c "gray": use white and two shades of gray on a black background.
  536. * - \c "8": use the 8 ANSI colours on a black background.
  537. * - \c "16": use the 16 ANSI colours on a black background.
  538. * - \c "fullgray": use black, white and two shades of gray for both the
  539. * characters and the background.
  540. * - \c "full8": use the 8 ANSI colours for both the characters and the
  541. * background.
  542. * - \c "full16" or \c "default": use the 16 ANSI colours for both the
  543. * characters and the background. This is the default value.
  544. *
  545. * If an error occurs, -1 is returned and \b errno is set accordingly:
  546. * - \c EINVAL Invalid colour set.
  547. *
  548. * \param d Dither object.
  549. * \param str A string describing the colour set that will be used
  550. * for the dithering.
  551. * \return 0 in case of success, -1 if an error occurred.
  552. */
  553. int caca_set_dither_color(caca_dither_t *d, char const *str)
  554. {
  555. if(!strcasecmp(str, "mono"))
  556. {
  557. d->color_name = "mono";
  558. d->color = COLOR_MODE_MONO;
  559. }
  560. else if(!strcasecmp(str, "gray"))
  561. {
  562. d->color_name = "gray";
  563. d->color = COLOR_MODE_GRAY;
  564. }
  565. else if(!strcasecmp(str, "8"))
  566. {
  567. d->color_name = "8";
  568. d->color = COLOR_MODE_8;
  569. }
  570. else if(!strcasecmp(str, "16"))
  571. {
  572. d->color_name = "16";
  573. d->color = COLOR_MODE_16;
  574. }
  575. else if(!strcasecmp(str, "fullgray"))
  576. {
  577. d->color_name = "fullgray";
  578. d->color = COLOR_MODE_FULLGRAY;
  579. }
  580. else if(!strcasecmp(str, "full8"))
  581. {
  582. d->color_name = "full8";
  583. d->color = COLOR_MODE_FULL8;
  584. }
  585. else if(!strcasecmp(str, "full16") || !strcasecmp(str, "default"))
  586. {
  587. d->color_name = "full16";
  588. d->color = COLOR_MODE_FULL16;
  589. }
  590. else
  591. {
  592. seterrno(EINVAL);
  593. return -1;
  594. }
  595. return 0;
  596. }
  597. /** \brief Get available colour modes
  598. *
  599. * Return a list of available colour modes for a given dither. The list
  600. * is a NULL-terminated array of strings, interleaving a string containing
  601. * the internal value for the colour mode, to be used with
  602. * caca_set_dither_color(), and a string containing the natural
  603. * language description for that colour mode.
  604. *
  605. * This function never fails.
  606. *
  607. * \param d Dither object.
  608. * \return An array of strings.
  609. */
  610. char const * const *
  611. caca_get_dither_color_list(caca_dither_t const *d)
  612. {
  613. static char const * const list[] =
  614. {
  615. "mono", "white on black",
  616. "gray", "grayscale on black",
  617. "8", "8 colours on black",
  618. "16", "16 colours on black",
  619. "fullgray", "full grayscale",
  620. "full8", "full 8 colours",
  621. "full16", "full 16 colours",
  622. NULL, NULL
  623. };
  624. return list;
  625. }
  626. /** \brief Get current colour mode
  627. *
  628. * Return the given dither's current colour mode.
  629. *
  630. * This function never fails.
  631. *
  632. * \param d Dither object.
  633. * \return A static string.
  634. */
  635. char const * caca_get_dither_color(caca_dither_t const *d)
  636. {
  637. return d->color_name;
  638. }
  639. /** \brief Choose characters used for dithering
  640. *
  641. * Tell the renderer which characters should be used to render the
  642. * dither. Valid values for \c str are:
  643. * - \c "ascii" or \c "default": use only ASCII characters. This is the
  644. * default value.
  645. * - \c "shades": use Unicode characters "U+2591 LIGHT SHADE", "U+2592
  646. * MEDIUM SHADE" and "U+2593 DARK SHADE". These characters are also
  647. * present in the CP437 codepage available on DOS and VGA.
  648. * - \c "blocks": use Unicode quarter-cell block combinations. These
  649. * characters are only found in the Unicode set.
  650. *
  651. * If an error occurs, -1 is returned and \b errno is set accordingly:
  652. * - \c EINVAL Invalid character set.
  653. *
  654. * \param d Dither object.
  655. * \param str A string describing the characters that need to be used
  656. * for the dithering.
  657. * \return 0 in case of success, -1 if an error occurred.
  658. */
  659. int caca_set_dither_charset(caca_dither_t *d, char const *str)
  660. {
  661. if(!strcasecmp(str, "shades"))
  662. {
  663. d->glyph_name = "shades";
  664. d->glyphs = shades_glyphs;
  665. d->glyph_count = sizeof(shades_glyphs) / sizeof(*shades_glyphs);
  666. }
  667. else if(!strcasecmp(str, "blocks"))
  668. {
  669. d->glyph_name = "blocks";
  670. d->glyphs = blocks_glyphs;
  671. d->glyph_count = sizeof(blocks_glyphs) / sizeof(*blocks_glyphs);
  672. }
  673. else if(!strcasecmp(str, "ascii") || !strcasecmp(str, "default"))
  674. {
  675. d->glyph_name = "ascii";
  676. d->glyphs = ascii_glyphs;
  677. d->glyph_count = sizeof(ascii_glyphs) / sizeof(*ascii_glyphs);
  678. }
  679. else
  680. {
  681. seterrno(EINVAL);
  682. return -1;
  683. }
  684. return 0;
  685. }
  686. /** \brief Get available dither character sets
  687. *
  688. * Return a list of available character sets for a given dither. The list
  689. * is a NULL-terminated array of strings, interleaving a string containing
  690. * the internal value for the character set, to be used with
  691. * caca_set_dither_charset(), and a string containing the natural
  692. * language description for that character set.
  693. *
  694. * This function never fails.
  695. *
  696. * \param d Dither object.
  697. * \return An array of strings.
  698. */
  699. char const * const * caca_get_dither_charset_list(caca_dither_t const *d)
  700. {
  701. static char const * const list[] =
  702. {
  703. "ascii", "plain ASCII",
  704. "shades", "CP437 shades",
  705. "blocks", "Unicode blocks",
  706. NULL, NULL
  707. };
  708. return list;
  709. }
  710. /** \brief Get current character set
  711. *
  712. * Return the given dither's current character set.
  713. *
  714. * This function never fails.
  715. *
  716. * \param d Dither object.
  717. * \return A static string.
  718. */
  719. char const * caca_get_dither_charset(caca_dither_t const *d)
  720. {
  721. return d->glyph_name;
  722. }
  723. /** \brief Set dithering algorithm
  724. *
  725. * Tell the renderer which dithering algorithm should be used. Dithering is
  726. * necessary because the picture being rendered has usually far more colours
  727. * than the available palette. Valid values for \c str are:
  728. * - \c "none": no dithering is used, the nearest matching colour is used.
  729. * - \c "ordered2": use a 2x2 Bayer matrix for dithering.
  730. * - \c "ordered4": use a 4x4 Bayer matrix for dithering.
  731. * - \c "ordered8": use a 8x8 Bayer matrix for dithering.
  732. * - \c "random": use random dithering.
  733. * - \c "fstein": use Floyd-Steinberg dithering. This is the default value.
  734. *
  735. * If an error occurs, -1 is returned and \b errno is set accordingly:
  736. * - \c EINVAL Unknown dithering mode.
  737. *
  738. * \param d Dither object.
  739. * \param str A string describing the algorithm that needs to be used
  740. * for the dithering.
  741. * \return 0 in case of success, -1 if an error occurred.
  742. */
  743. int caca_set_dither_algorithm(caca_dither_t *d, char const *str)
  744. {
  745. if(!strcasecmp(str, "none"))
  746. {
  747. d->algo_name = "none";
  748. d->init_dither = init_no_dither;
  749. d->get_dither = get_no_dither;
  750. d->increment_dither = increment_no_dither;
  751. }
  752. else if(!strcasecmp(str, "ordered2"))
  753. {
  754. d->algo_name = "ordered2";
  755. d->init_dither = init_ordered2_dither;
  756. d->get_dither = get_ordered2_dither;
  757. d->increment_dither = increment_ordered2_dither;
  758. }
  759. else if(!strcasecmp(str, "ordered4"))
  760. {
  761. d->algo_name = "ordered4";
  762. d->init_dither = init_ordered4_dither;
  763. d->get_dither = get_ordered4_dither;
  764. d->increment_dither = increment_ordered4_dither;
  765. }
  766. else if(!strcasecmp(str, "ordered8"))
  767. {
  768. d->algo_name = "ordered8";
  769. d->init_dither = init_ordered8_dither;
  770. d->get_dither = get_ordered8_dither;
  771. d->increment_dither = increment_ordered8_dither;
  772. }
  773. else if(!strcasecmp(str, "random"))
  774. {
  775. d->algo_name = "random";
  776. d->init_dither = init_random_dither;
  777. d->get_dither = get_random_dither;
  778. d->increment_dither = increment_random_dither;
  779. }
  780. else if(!strcasecmp(str, "fstein") || !strcasecmp(str, "default"))
  781. {
  782. d->algo_name = "fstein";
  783. d->init_dither = init_fstein_dither;
  784. d->get_dither = get_fstein_dither;
  785. d->increment_dither = increment_fstein_dither;
  786. }
  787. else
  788. {
  789. seterrno(EINVAL);
  790. return -1;
  791. }
  792. return 0;
  793. }
  794. /** \brief Get dithering algorithms
  795. *
  796. * Return a list of available dithering algorithms for a given dither. The
  797. * list is a NULL-terminated array of strings, interleaving a string
  798. * containing the internal value for the dithering algorithm, to be used
  799. * with caca_set_dither_dithering(), and a string containing the natural
  800. * language description for that algorithm.
  801. *
  802. * This function never fails.
  803. *
  804. * \param d Dither object.
  805. * \return An array of strings.
  806. */
  807. char const * const * caca_get_dither_algorithm_list(caca_dither_t const *d)
  808. {
  809. static char const * const list[] =
  810. {
  811. "none", "no dithering",
  812. "ordered2", "2x2 ordered dithering",
  813. "ordered4", "4x4 ordered dithering",
  814. "ordered8", "8x8 ordered dithering",
  815. "random", "random dithering",
  816. "fstein", "Floyd-Steinberg dithering",
  817. NULL, NULL
  818. };
  819. return list;
  820. }
  821. /** \brief Get current dithering algorithm
  822. *
  823. * Return the given dither's current dithering algorithm.
  824. *
  825. * This function never fails.
  826. *
  827. * \param d Dither object.
  828. * \return A static string.
  829. */
  830. char const * caca_get_dither_algorithm(caca_dither_t const *d)
  831. {
  832. return d->algo_name;
  833. }
  834. /** \brief Dither a bitmap on the canvas.
  835. *
  836. * Dither a bitmap at the given coordinates. The dither can be of any size
  837. * and will be stretched to the text area.
  838. *
  839. * This function never fails.
  840. *
  841. * \param cv A handle to the libcaca canvas.
  842. * \param x X coordinate of the upper-left corner of the drawing area.
  843. * \param y Y coordinate of the upper-left corner of the drawing area.
  844. * \param w Width of the drawing area.
  845. * \param h Height of the drawing area.
  846. * \param d Dither object to be drawn.
  847. * \param pixels Bitmap's pixels.
  848. * \return This function always returns 0.
  849. */
  850. int caca_dither_bitmap(caca_canvas_t *cv, int x, int y, int w, int h,
  851. caca_dither_t const *d, void const *pixels)
  852. {
  853. int *floyd_steinberg, *fs_r, *fs_g, *fs_b;
  854. uint32_t savedattr;
  855. int fs_length;
  856. int x1, y1, x2, y2, pitch, deltax, deltay, dchmax;
  857. if(!d || !pixels)
  858. return 0;
  859. savedattr = caca_get_attr(cv, -1, -1);
  860. x1 = x; x2 = x + w - 1;
  861. y1 = y; y2 = y + h - 1;
  862. /* FIXME: do not overwrite arguments */
  863. w = d->w;
  864. h = d->h;
  865. pitch = d->pitch;
  866. deltax = x2 - x1 + 1;
  867. deltay = y2 - y1 + 1;
  868. dchmax = d->glyph_count;
  869. fs_length = ((int)cv->width <= x2 ? (int)cv->width : x2) + 1;
  870. floyd_steinberg = malloc(3 * (fs_length + 2) * sizeof(int));
  871. memset(floyd_steinberg, 0, 3 * (fs_length + 2) * sizeof(int));
  872. fs_r = floyd_steinberg + 1;
  873. fs_g = fs_r + fs_length + 2;
  874. fs_b = fs_g + fs_length + 2;
  875. for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)cv->height; y++)
  876. {
  877. int remain_r = 0, remain_g = 0, remain_b = 0;
  878. for(x = x1 > 0 ? x1 : 0, d->init_dither(y);
  879. x <= x2 && x <= (int)cv->width;
  880. x++)
  881. {
  882. unsigned int rgba[4];
  883. int error[3];
  884. int i, ch = 0, distmin;
  885. int fg_r = 0, fg_g = 0, fg_b = 0, bg_r, bg_g, bg_b;
  886. int fromx, fromy, tox, toy, myx, myy, dots, dist;
  887. int outfg = 0, outbg = 0;
  888. uint32_t outch;
  889. rgba[0] = rgba[1] = rgba[2] = rgba[3] = 0;
  890. /* First get RGB */
  891. if(d->antialias)
  892. {
  893. fromx = (x - x1) * w / deltax;
  894. fromy = (y - y1) * h / deltay;
  895. tox = (x - x1 + 1) * w / deltax;
  896. toy = (y - y1 + 1) * h / deltay;
  897. /* We want at least one pixel */
  898. if(tox == fromx) tox++;
  899. if(toy == fromy) toy++;
  900. dots = 0;
  901. for(myx = fromx; myx < tox; myx++)
  902. for(myy = fromy; myy < toy; myy++)
  903. {
  904. dots++;
  905. get_rgba_default(d, pixels, myx, myy, rgba);
  906. }
  907. /* Normalize */
  908. rgba[0] /= dots;
  909. rgba[1] /= dots;
  910. rgba[2] /= dots;
  911. rgba[3] /= dots;
  912. }
  913. else
  914. {
  915. fromx = (x - x1) * w / deltax;
  916. fromy = (y - y1) * h / deltay;
  917. tox = (x - x1 + 1) * w / deltax;
  918. toy = (y - y1 + 1) * h / deltay;
  919. /* tox and toy can overflow the canvas, but they cannot overflow
  920. * when averaged with fromx and fromy because these are guaranteed
  921. * to be within the pixel boundaries. */
  922. myx = (fromx + tox) / 2;
  923. myy = (fromy + toy) / 2;
  924. get_rgba_default(d, pixels, myx, myy, rgba);
  925. }
  926. /* FIXME: hack to force greyscale */
  927. if(d->color == COLOR_MODE_FULLGRAY)
  928. {
  929. unsigned int gray = (3 * rgba[0] + 4 * rgba[1] + rgba[2] + 4) / 8;
  930. rgba[0] = rgba[1] = rgba[2] = gray;
  931. }
  932. if(d->has_alpha && rgba[3] < 0x800)
  933. {
  934. remain_r = remain_g = remain_b = 0;
  935. fs_r[x] = 0;
  936. fs_g[x] = 0;
  937. fs_b[x] = 0;
  938. continue;
  939. }
  940. /* XXX: OMG HAX */
  941. if(d->init_dither == init_fstein_dither)
  942. {
  943. rgba[0] += remain_r;
  944. rgba[1] += remain_g;
  945. rgba[2] += remain_b;
  946. }
  947. else
  948. {
  949. rgba[0] += (d->get_dither() - 0x80) * 4;
  950. rgba[1] += (d->get_dither() - 0x80) * 4;
  951. rgba[2] += (d->get_dither() - 0x80) * 4;
  952. }
  953. distmin = INT_MAX;
  954. for(i = 0; i < 16; i++)
  955. {
  956. if(d->color == COLOR_MODE_FULLGRAY
  957. && (rgb_palette[i * 3] != rgb_palette[i * 3 + 1]
  958. || rgb_palette[i * 3] != rgb_palette[i * 3 + 2]))
  959. continue;
  960. dist = sq(rgba[0] - rgb_palette[i * 3])
  961. + sq(rgba[1] - rgb_palette[i * 3 + 1])
  962. + sq(rgba[2] - rgb_palette[i * 3 + 2]);
  963. dist *= rgb_weight[i];
  964. if(dist < distmin)
  965. {
  966. outbg = i;
  967. distmin = dist;
  968. }
  969. }
  970. bg_r = rgb_palette[outbg * 3];
  971. bg_g = rgb_palette[outbg * 3 + 1];
  972. bg_b = rgb_palette[outbg * 3 + 2];
  973. /* FIXME: we currently only honour "full16" */
  974. if(d->color == COLOR_MODE_FULL16 || d->color == COLOR_MODE_FULLGRAY)
  975. {
  976. distmin = INT_MAX;
  977. for(i = 0; i < 16; i++)
  978. {
  979. if(i == outbg)
  980. continue;
  981. if(d->color == COLOR_MODE_FULLGRAY
  982. && (rgb_palette[i * 3] != rgb_palette[i * 3 + 1]
  983. || rgb_palette[i * 3] != rgb_palette[i * 3 + 2]))
  984. continue;
  985. dist = sq(rgba[0] - rgb_palette[i * 3])
  986. + sq(rgba[1] - rgb_palette[i * 3 + 1])
  987. + sq(rgba[2] - rgb_palette[i * 3 + 2]);
  988. dist *= rgb_weight[i];
  989. if(dist < distmin)
  990. {
  991. outfg = i;
  992. distmin = dist;
  993. }
  994. }
  995. fg_r = rgb_palette[outfg * 3];
  996. fg_g = rgb_palette[outfg * 3 + 1];
  997. fg_b = rgb_palette[outfg * 3 + 2];
  998. distmin = INT_MAX;
  999. for(i = 0; i < dchmax - 1; i++)
  1000. {
  1001. int newr = i * fg_r + ((2*dchmax-1) - i) * bg_r;
  1002. int newg = i * fg_g + ((2*dchmax-1) - i) * bg_g;
  1003. int newb = i * fg_b + ((2*dchmax-1) - i) * bg_b;
  1004. dist = abs(rgba[0] * (2*dchmax-1) - newr)
  1005. + abs(rgba[1] * (2*dchmax-1) - newg)
  1006. + abs(rgba[2] * (2*dchmax-1) - newb);
  1007. if(dist < distmin)
  1008. {
  1009. ch = i;
  1010. distmin = dist;
  1011. }
  1012. }
  1013. outch = d->glyphs[ch];
  1014. /* XXX: OMG HAX */
  1015. if(d->init_dither == init_fstein_dither)
  1016. {
  1017. error[0] = rgba[0] - (fg_r * ch + bg_r * ((2*dchmax-1) - ch)) / (2*dchmax-1);
  1018. error[1] = rgba[1] - (fg_g * ch + bg_g * ((2*dchmax-1) - ch)) / (2*dchmax-1);
  1019. error[2] = rgba[2] - (fg_b * ch + bg_b * ((2*dchmax-1) - ch)) / (2*dchmax-1);
  1020. }
  1021. }
  1022. else
  1023. {
  1024. unsigned int lum = rgba[0];
  1025. if(rgba[1] > lum) lum = rgba[1];
  1026. if(rgba[2] > lum) lum = rgba[2];
  1027. outfg = outbg;
  1028. outbg = CACA_BLACK;
  1029. ch = lum * dchmax / 0x1000;
  1030. if(ch < 0)
  1031. ch = 0;
  1032. else if(ch > (int)(dchmax - 1))
  1033. ch = dchmax - 1;
  1034. outch = d->glyphs[ch];
  1035. /* XXX: OMG HAX */
  1036. if(d->init_dither == init_fstein_dither)
  1037. {
  1038. error[0] = rgba[0] - bg_r * ch / (dchmax-1);
  1039. error[1] = rgba[1] - bg_g * ch / (dchmax-1);
  1040. error[2] = rgba[2] - bg_b * ch / (dchmax-1);
  1041. }
  1042. }
  1043. /* XXX: OMG HAX */
  1044. if(d->init_dither == init_fstein_dither)
  1045. {
  1046. remain_r = fs_r[x+1] + 7 * error[0] / 16;
  1047. remain_g = fs_g[x+1] + 7 * error[1] / 16;
  1048. remain_b = fs_b[x+1] + 7 * error[2] / 16;
  1049. fs_r[x-1] += 3 * error[0] / 16;
  1050. fs_g[x-1] += 3 * error[1] / 16;
  1051. fs_b[x-1] += 3 * error[2] / 16;
  1052. fs_r[x] = 5 * error[0] / 16;
  1053. fs_g[x] = 5 * error[1] / 16;
  1054. fs_b[x] = 5 * error[2] / 16;
  1055. fs_r[x+1] = 1 * error[0] / 16;
  1056. fs_g[x+1] = 1 * error[1] / 16;
  1057. fs_b[x+1] = 1 * error[2] / 16;
  1058. }
  1059. if(d->invert)
  1060. {
  1061. outfg = 15 - outfg;
  1062. outbg = 15 - outbg;
  1063. }
  1064. /* Now output the character */
  1065. caca_set_color_ansi(cv, outfg, outbg);
  1066. caca_put_char(cv, x, y, outch);
  1067. d->increment_dither();
  1068. }
  1069. /* end loop */
  1070. }
  1071. free(floyd_steinberg);
  1072. caca_set_attr(cv, savedattr);
  1073. return 0;
  1074. }
  1075. /** \brief Free the memory associated with a dither.
  1076. *
  1077. * Free the memory allocated by caca_create_dither().
  1078. *
  1079. * This function never fails.
  1080. *
  1081. * \param d Dither object.
  1082. * \return This function always returns 0.
  1083. */
  1084. int caca_free_dither(caca_dither_t *d)
  1085. {
  1086. if(!d)
  1087. return 0;
  1088. free(d);
  1089. return 0;
  1090. }
  1091. /*
  1092. * XXX: The following functions are local.
  1093. */
  1094. /* Convert a mask, eg. 0x0000ff00, to shift values, eg. 8 and -4. */
  1095. static void mask2shift(uint32_t mask, int *right, int *left)
  1096. {
  1097. int rshift = 0, lshift = 0;
  1098. if(!mask)
  1099. {
  1100. *right = *left = 0;
  1101. return;
  1102. }
  1103. while(!(mask & 1))
  1104. {
  1105. mask >>= 1;
  1106. rshift++;
  1107. }
  1108. *right = rshift;
  1109. while(mask & 1)
  1110. {
  1111. mask >>= 1;
  1112. lshift++;
  1113. }
  1114. *left = 12 - lshift;
  1115. }
  1116. /* Compute x^y without relying on the math library */
  1117. static float gammapow(float x, float y)
  1118. {
  1119. #ifdef HAVE_FLDLN2
  1120. register double logx;
  1121. register long double v, e;
  1122. #else
  1123. register float tmp, t, t2, r;
  1124. int i;
  1125. #endif
  1126. if(x == 0.0)
  1127. return y == 0.0 ? 1.0 : 0.0;
  1128. #ifdef HAVE_FLDLN2
  1129. /* FIXME: this can be optimised by directly calling fyl2x for x and y */
  1130. asm volatile("fldln2; fxch; fyl2x"
  1131. : "=t" (logx) : "0" (x) : "st(1)");
  1132. asm volatile("fldl2e\n\t"
  1133. "fmul %%st(1)\n\t"
  1134. "fst %%st(1)\n\t"
  1135. "frndint\n\t"
  1136. "fxch\n\t"
  1137. "fsub %%st(1)\n\t"
  1138. "f2xm1\n\t"
  1139. : "=t" (v), "=u" (e) : "0" (y * logx));
  1140. v += 1.0;
  1141. asm volatile("fscale"
  1142. : "=t" (v) : "0" (v), "u" (e));
  1143. return v;
  1144. #else
  1145. /* Compute ln(x) for x ∈ ]0,1]
  1146. * ln(x) = 2 * (t + t^3/3 + t^5/5 + ...) with t = (x-1)/(x+1)
  1147. * The convergence is a bit slow, especially when x is near 0. */
  1148. t = (x - 1.0) / (x + 1.0);
  1149. t2 = t * t;
  1150. tmp = r = t;
  1151. for(i = 3; i < 20; i += 2)
  1152. {
  1153. r *= t2;
  1154. tmp += r / i;
  1155. }
  1156. /* Compute -y*ln(x) */
  1157. tmp = - y * 2.0 * tmp;
  1158. /* Compute x^-y as e^t where t = -y*ln(x):
  1159. * e^t = 1 + t/1! + t^2/2! + t^3/3! + t^4/4! + t^5/5! ...
  1160. * The convergence is quite faster here, thanks to the factorial. */
  1161. r = t = tmp;
  1162. tmp = 1.0 + t;
  1163. for(i = 2; i < 16; i++)
  1164. {
  1165. r = r * t / i;
  1166. tmp += r;
  1167. }
  1168. /* Return x^y as 1/(x^-y) */
  1169. return 1.0 / tmp;
  1170. #endif
  1171. }
  1172. static void get_rgba_default(caca_dither_t const *d, uint8_t const *pixels,
  1173. int x, int y, unsigned int *rgba)
  1174. {
  1175. uint32_t bits;
  1176. pixels += (d->bpp / 8) * x + d->pitch * y;
  1177. switch(d->bpp / 8)
  1178. {
  1179. case 4:
  1180. bits = *(uint32_t const *)pixels;
  1181. break;
  1182. case 3:
  1183. {
  1184. #if defined(HAVE_ENDIAN_H)
  1185. if(__BYTE_ORDER == __BIG_ENDIAN)
  1186. #else
  1187. /* This is compile-time optimised with at least -O1 or -Os */
  1188. uint32_t const tmp = 0x12345678;
  1189. if(*(uint8_t const *)&tmp == 0x12)
  1190. #endif
  1191. bits = ((uint32_t)pixels[0] << 16) |
  1192. ((uint32_t)pixels[1] << 8) |
  1193. ((uint32_t)pixels[2]);
  1194. else
  1195. bits = ((uint32_t)pixels[2] << 16) |
  1196. ((uint32_t)pixels[1] << 8) |
  1197. ((uint32_t)pixels[0]);
  1198. break;
  1199. }
  1200. case 2:
  1201. bits = *(uint16_t const *)pixels;
  1202. break;
  1203. case 1:
  1204. default:
  1205. bits = pixels[0];
  1206. break;
  1207. }
  1208. if(d->has_palette)
  1209. {
  1210. rgba[0] += d->gammatab[d->red[bits]];
  1211. rgba[1] += d->gammatab[d->green[bits]];
  1212. rgba[2] += d->gammatab[d->blue[bits]];
  1213. rgba[3] += d->alpha[bits];
  1214. }
  1215. else
  1216. {
  1217. rgba[0] += d->gammatab[((bits & d->rmask) >> d->rright) << d->rleft];
  1218. rgba[1] += d->gammatab[((bits & d->gmask) >> d->gright) << d->gleft];
  1219. rgba[2] += d->gammatab[((bits & d->bmask) >> d->bright) << d->bleft];
  1220. rgba[3] += ((bits & d->amask) >> d->aright) << d->aleft;
  1221. }
  1222. }
  1223. /*
  1224. * No dithering
  1225. */
  1226. static void init_no_dither(int line)
  1227. {
  1228. ;
  1229. }
  1230. static int get_no_dither(void)
  1231. {
  1232. return 0x80;
  1233. }
  1234. static void increment_no_dither(void)
  1235. {
  1236. return;
  1237. }
  1238. /*
  1239. * Floyd-Steinberg dithering
  1240. */
  1241. static void init_fstein_dither(int line)
  1242. {
  1243. ;
  1244. }
  1245. static int get_fstein_dither(void)
  1246. {
  1247. return 0x80;
  1248. }
  1249. static void increment_fstein_dither(void)
  1250. {
  1251. return;
  1252. }
  1253. /*
  1254. * Ordered 2 dithering
  1255. */
  1256. static int const *ordered2_table;
  1257. static int ordered2_index;
  1258. static void init_ordered2_dither(int line)
  1259. {
  1260. static int const dither2x2[] =
  1261. {
  1262. 0x00, 0x80,
  1263. 0xc0, 0x40,
  1264. };
  1265. ordered2_table = dither2x2 + (line % 2) * 2;
  1266. ordered2_index = 0;
  1267. }
  1268. static int get_ordered2_dither(void)
  1269. {
  1270. return ordered2_table[ordered2_index];
  1271. }
  1272. static void increment_ordered2_dither(void)
  1273. {
  1274. ordered2_index = (ordered2_index + 1) % 2;
  1275. }
  1276. /*
  1277. * Ordered 4 dithering
  1278. */
  1279. /*static int dither4x4[] = { 5, 0, 1, 6,
  1280. -1, -6, -5, 2,
  1281. -2, -7, -8, 3,
  1282. 4, -3, -4, -7};*/
  1283. static int const *ordered4_table;
  1284. static int ordered4_index;
  1285. static void init_ordered4_dither(int line)
  1286. {
  1287. static int const dither4x4[] =
  1288. {
  1289. 0x00, 0x80, 0x20, 0xa0,
  1290. 0xc0, 0x40, 0xe0, 0x60,
  1291. 0x30, 0xb0, 0x10, 0x90,
  1292. 0xf0, 0x70, 0xd0, 0x50
  1293. };
  1294. ordered4_table = dither4x4 + (line % 4) * 4;
  1295. ordered4_index = 0;
  1296. }
  1297. static int get_ordered4_dither(void)
  1298. {
  1299. return ordered4_table[ordered4_index];
  1300. }
  1301. static void increment_ordered4_dither(void)
  1302. {
  1303. ordered4_index = (ordered4_index + 1) % 4;
  1304. }
  1305. /*
  1306. * Ordered 8 dithering
  1307. */
  1308. static int const *ordered8_table;
  1309. static int ordered8_index;
  1310. static void init_ordered8_dither(int line)
  1311. {
  1312. static int const dither8x8[] =
  1313. {
  1314. 0x00, 0x80, 0x20, 0xa0, 0x08, 0x88, 0x28, 0xa8,
  1315. 0xc0, 0x40, 0xe0, 0x60, 0xc8, 0x48, 0xe8, 0x68,
  1316. 0x30, 0xb0, 0x10, 0x90, 0x38, 0xb8, 0x18, 0x98,
  1317. 0xf0, 0x70, 0xd0, 0x50, 0xf8, 0x78, 0xd8, 0x58,
  1318. 0x0c, 0x8c, 0x2c, 0xac, 0x04, 0x84, 0x24, 0xa4,
  1319. 0xcc, 0x4c, 0xec, 0x6c, 0xc4, 0x44, 0xe4, 0x64,
  1320. 0x3c, 0xbc, 0x1c, 0x9c, 0x34, 0xb4, 0x14, 0x94,
  1321. 0xfc, 0x7c, 0xdc, 0x5c, 0xf4, 0x74, 0xd4, 0x54,
  1322. };
  1323. ordered8_table = dither8x8 + (line % 8) * 8;
  1324. ordered8_index = 0;
  1325. }
  1326. static int get_ordered8_dither(void)
  1327. {
  1328. return ordered8_table[ordered8_index];
  1329. }
  1330. static void increment_ordered8_dither(void)
  1331. {
  1332. ordered8_index = (ordered8_index + 1) % 8;
  1333. }
  1334. /*
  1335. * Random dithering
  1336. */
  1337. static void init_random_dither(int line)
  1338. {
  1339. ;
  1340. }
  1341. static int get_random_dither(void)
  1342. {
  1343. return caca_rand(0x00, 0x100);
  1344. }
  1345. static void increment_random_dither(void)
  1346. {
  1347. return;
  1348. }
  1349. /*
  1350. * Lookup tables
  1351. */
  1352. static int init_lookup(void)
  1353. {
  1354. int v, s, h;
  1355. /* These ones are constant */
  1356. lookup_colors[0] = CACA_BLACK;
  1357. lookup_colors[1] = CACA_DARKGRAY;
  1358. lookup_colors[2] = CACA_LIGHTGRAY;
  1359. lookup_colors[3] = CACA_WHITE;
  1360. /* These ones will be overwritten */
  1361. lookup_colors[4] = CACA_MAGENTA;
  1362. lookup_colors[5] = CACA_LIGHTMAGENTA;
  1363. lookup_colors[6] = CACA_RED;
  1364. lookup_colors[7] = CACA_LIGHTRED;
  1365. for(v = 0; v < LOOKUP_VAL; v++)
  1366. for(s = 0; s < LOOKUP_SAT; s++)
  1367. for(h = 0; h < LOOKUP_HUE; h++)
  1368. {
  1369. int i, distbg, distfg, dist;
  1370. int val, sat, hue;
  1371. uint8_t outbg, outfg;
  1372. val = 0xfff * v / (LOOKUP_VAL - 1);
  1373. sat = 0xfff * s / (LOOKUP_SAT - 1);
  1374. hue = 0xfff * h / (LOOKUP_HUE - 1);
  1375. /* Initialise distances to the distance between pure black HSV
  1376. * coordinates and our white colour (3) */
  1377. outbg = outfg = 3;
  1378. distbg = distfg = HSV_DISTANCE(0, 0, 0, 3);
  1379. /* Calculate distances to eight major colour values and store the
  1380. * two nearest points in our lookup table. */
  1381. for(i = 0; i < 8; i++)
  1382. {
  1383. dist = HSV_DISTANCE(hue, sat, val, i);
  1384. if(dist <= distbg)
  1385. {
  1386. outfg = outbg;
  1387. distfg = distbg;
  1388. outbg = i;
  1389. distbg = dist;
  1390. }
  1391. else if(dist <= distfg)
  1392. {
  1393. outfg = i;
  1394. distfg = dist;
  1395. }
  1396. }
  1397. hsv_distances[v][s][h] = (outfg << 4) | outbg;
  1398. }
  1399. return 0;
  1400. }
  1401. /*
  1402. * XXX: The following functions are aliases.
  1403. */
  1404. cucul_dither_t *cucul_create_dither(int, int, int, int, uint32_t, uint32_t,
  1405. uint32_t, uint32_t)
  1406. CACA_ALIAS(caca_create_dither);
  1407. int cucul_set_dither_palette(cucul_dither_t *, uint32_t r[], uint32_t g[],
  1408. uint32_t b[], uint32_t a[])
  1409. CACA_ALIAS(caca_set_dither_palette);
  1410. int cucul_set_dither_brightness(cucul_dither_t *, float)
  1411. CACA_ALIAS(caca_set_dither_brightness);
  1412. float cucul_get_dither_brightness(cucul_dither_t const *)
  1413. CACA_ALIAS(caca_get_dither_brightness);
  1414. int cucul_set_dither_gamma(cucul_dither_t *, float)
  1415. CACA_ALIAS(caca_set_dither_gamma);
  1416. float cucul_get_dither_gamma(cucul_dither_t const *)
  1417. CACA_ALIAS(caca_get_dither_gamma);
  1418. int cucul_set_dither_contrast(cucul_dither_t *, float)
  1419. CACA_ALIAS(caca_set_dither_contrast);
  1420. float cucul_get_dither_contrast(cucul_dither_t const *)
  1421. CACA_ALIAS(caca_get_dither_contrast);
  1422. int cucul_set_dither_antialias(cucul_dither_t *, char const *)
  1423. CACA_ALIAS(caca_set_dither_antialias);
  1424. char const * const * cucul_get_dither_antialias_list(cucul_dither_t const *)
  1425. CACA_ALIAS(caca_get_dither_antialias_list);
  1426. char const * cucul_get_dither_antialias(cucul_dither_t const *)
  1427. CACA_ALIAS(caca_get_dither_antialias);
  1428. int cucul_set_dither_color(cucul_dither_t *, char const *)
  1429. CACA_ALIAS(caca_set_dither_color);
  1430. char const * const * cucul_get_dither_color_list(cucul_dither_t const *)
  1431. CACA_ALIAS(caca_get_dither_color_list);
  1432. char const * cucul_get_dither_color(cucul_dither_t const *)
  1433. CACA_ALIAS(caca_get_dither_color);
  1434. int cucul_set_dither_charset(cucul_dither_t *, char const *)
  1435. CACA_ALIAS(caca_set_dither_charset);
  1436. char const * const * cucul_get_dither_charset_list(cucul_dither_t const *)
  1437. CACA_ALIAS(caca_get_dither_charset_list);
  1438. char const * cucul_get_dither_charset(cucul_dither_t const *)
  1439. CACA_ALIAS(caca_get_dither_charset);
  1440. int cucul_set_dither_algorithm(cucul_dither_t *, char const *)
  1441. CACA_ALIAS(caca_set_dither_algorithm);
  1442. char const * const * cucul_get_dither_algorithm_list(cucul_dither_t const *)
  1443. CACA_ALIAS(caca_get_dither_algorithm_list);
  1444. char const * cucul_get_dither_algorithm(cucul_dither_t const *)
  1445. CACA_ALIAS(caca_get_dither_algorithm);
  1446. int cucul_dither_bitmap(cucul_canvas_t *, int, int, int, int,
  1447. cucul_dither_t const *, void *)
  1448. CACA_ALIAS(caca_dither_bitmap);
  1449. int cucul_free_dither(cucul_dither_t *) CACA_ALIAS(caca_free_dither);