25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

dither.c 38 KiB

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