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.

dither.c 38 KiB

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