/
dither.c
1577 lines (1398 loc) · 41.2 KB
/
dither.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* libcaca Colour ASCII-Art library
* Copyright © 2002—2018 Sam Hocevar <sam@hocevar.net>
* All Rights Reserved
*
* This library is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What the Fuck You Want
* to Public License, Version 2, as published by the WTFPL Task Force.
* See http://www.wtfpl.net/ for more details.
*/
/*
* This file contains bitmap dithering functions.
*/
#include "config.h"
#if !defined(__KERNEL__)
# if defined(HAVE_ENDIAN_H)
# include <endian.h>
# endif
# include <stdio.h>
# include <stdlib.h>
# include <limits.h>
# include <string.h>
#endif
#include "caca.h"
#include "caca_internals.h"
#define CP437 0
/*
* Local variables
*/
#if !defined(_DOXYGEN_SKIP_ME)
# define LOOKUP_VAL 32
# define LOOKUP_SAT 32
# define LOOKUP_HUE 16
#endif
static uint8_t hsv_distances[LOOKUP_VAL][LOOKUP_SAT][LOOKUP_HUE];
static uint16_t lookup_colors[8];
static int lookup_initialised = 0;
static int const hsv_palette[] =
{
/* weight, hue, saturation, value */
4, 0x0, 0x0, 0x0, /* black */
5, 0x0, 0x0, 0x5ff, /* 30% */
5, 0x0, 0x0, 0x9ff, /* 70% */
4, 0x0, 0x0, 0xfff, /* white */
3, 0x1000, 0xfff, 0x5ff, /* dark yellow */
2, 0x1000, 0xfff, 0xfff, /* light yellow */
3, 0x0, 0xfff, 0x5ff, /* dark red */
2, 0x0, 0xfff, 0xfff /* light red */
};
/* RGB palette for the new colour picker */
static int const rgb_palette[] =
{
0x0, 0x0, 0x0,
0x0, 0x0, 0x7ff,
0x0, 0x7ff, 0x0,
0x0, 0x7ff, 0x7ff,
0x7ff, 0x0, 0x0,
0x7ff, 0x0, 0x7ff,
0x7ff, 0x7ff, 0x0,
0xaaa, 0xaaa, 0xaaa,
0x555, 0x555, 0x555,
0x000, 0x000, 0xfff,
0x000, 0xfff, 0x000,
0x000, 0xfff, 0xfff,
0xfff, 0x000, 0x000,
0xfff, 0x000, 0xfff,
0xfff, 0xfff, 0x000,
0xfff, 0xfff, 0xfff,
};
static int const rgb_weight[] =
{
/* 2, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2 */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
/* List of glyphs */
static uint32_t ascii_glyphs[] =
{
' ', '.', ':', ';', 't', '%', 'S', 'X', '@', '8', '?'
};
static uint32_t shades_glyphs[] =
{
/* ' '. '·', '░', '▒', '?' */
' ', 0xb7, 0x2591, 0x2592, '?'
};
static uint32_t blocks_glyphs[] =
{
/* ' ', '▘', '▚', '?' */
' ', 0x2598, 0x259a, '?'
};
#if !defined(_DOXYGEN_SKIP_ME)
enum color_mode
{
COLOR_MODE_MONO,
COLOR_MODE_GRAY,
COLOR_MODE_8,
COLOR_MODE_16,
COLOR_MODE_FULLGRAY,
COLOR_MODE_FULL8,
COLOR_MODE_FULL16
};
struct caca_dither
{
int bpp, has_palette, has_alpha;
size_t w, h, pitch;
int rmask, gmask, bmask, amask;
int rright, gright, bright, aright;
int rleft, gleft, bleft, aleft;
void (*get_hsv)(caca_dither_t *, char *, int, int);
int red[256], green[256], blue[256], alpha[256];
/* Colour features */
float gamma, brightness, contrast;
int gammatab[4097];
/* Dithering features */
char const *antialias_name;
int antialias;
char const *color_name;
enum color_mode color;
char const *algo_name;
void (*init_dither) (int);
int (*get_dither) (void);
void (*increment_dither) (void);
char const *glyph_name;
uint32_t const * glyphs;
int glyph_count;
int invert;
};
#define HSV_XRATIO 6
#define HSV_YRATIO 3
#define HSV_HRATIO 3
#define HSV_DISTANCE(h, s, v, index) \
(hsv_palette[index * 4] \
* ((HSV_XRATIO * ((v) - hsv_palette[index * 4 + 3]) \
* ((v) - hsv_palette[index * 4 + 3])) \
+ (hsv_palette[index * 4 + 3] \
? (HSV_YRATIO * ((s) - hsv_palette[index * 4 + 2]) \
* ((s) - hsv_palette[index * 4 + 2])) \
: 0) \
+ (hsv_palette[index * 4 + 2] \
? (HSV_HRATIO * ((h) - hsv_palette[index * 4 + 1]) \
* ((h) - hsv_palette[index * 4 + 1])) \
: 0)))
#endif
/*
* Local prototypes
*/
static void mask2shift(uint32_t, int *, int *);
static float gammapow(float x, float y);
static void get_rgba_default(caca_dither_t const *, uint8_t const *, int, int,
unsigned int *);
static int init_lookup(void);
/* Dithering algorithms */
static void init_no_dither(int);
static int get_no_dither(void);
static void increment_no_dither(void);
static void init_fstein_dither(int);
static int get_fstein_dither(void);
static void increment_fstein_dither(void);
static void init_ordered2_dither(int);
static int get_ordered2_dither(void);
static void increment_ordered2_dither(void);
static void init_ordered4_dither(int);
static int get_ordered4_dither(void);
static void increment_ordered4_dither(void);
static void init_ordered8_dither(int);
static int get_ordered8_dither(void);
static void increment_ordered8_dither(void);
static void init_random_dither(int);
static int get_random_dither(void);
static void increment_random_dither(void);
static inline int sq(int x)
{
return x * x;
}
static inline void rgb2hsv_default(int r, int g, int b,
int *hue, int *sat, int *val)
{
int min, max, delta;
min = r; max = r;
if(min > g) min = g; if(max < g) max = g;
if(min > b) min = b; if(max < b) max = b;
delta = max - min; /* 0 - 0xfff */
*val = max; /* 0 - 0xfff */
if(delta)
{
*sat = 0xfff * delta / max; /* 0 - 0xfff */
/* Generate *hue between 0 and 0x5fff */
if( r == max )
*hue = 0x1000 + 0x1000 * (g - b) / delta;
else if( g == max )
*hue = 0x3000 + 0x1000 * (b - r) / delta;
else
*hue = 0x5000 + 0x1000 * (r - g) / delta;
}
else
{
*sat = 0;
*hue = 0;
}
}
/** \brief Create an internal dither object.
*
* Create a dither structure from its coordinates (depth, width, height and
* pitch) and pixel mask values. If the depth is 8 bits per pixel, the mask
* values are ignored and the colour palette should be set using the
* caca_set_dither_palette() function. For depths greater than 8 bits per
* pixel, a zero alpha mask causes the alpha values to be ignored.
*
* If an error occurs, NULL is returned and \b errno is set accordingly:
* - \c EINVAL Requested width, height, pitch or bits per pixel value was
* invalid.
* - \c ENOMEM Not enough memory to allocate dither structure.
*
* \param bpp Bitmap depth in bits per pixel.
* \param w Bitmap width in pixels.
* \param h Bitmap height in pixels.
* \param pitch Bitmap pitch in bytes.
* \param rmask Bitmask for red values.
* \param gmask Bitmask for green values.
* \param bmask Bitmask for blue values.
* \param amask Bitmask for alpha values.
* \return Dither object upon success, NULL if an error occurred.
*/
caca_dither_t *caca_create_dither(int bpp, int w, int h, int pitch,
uint32_t rmask, uint32_t gmask,
uint32_t bmask, uint32_t amask)
{
caca_dither_t *d;
int i;
/* Minor sanity test */
if(w < 0 || h < 0 || pitch < 0 || bpp > 32 || bpp < 8)
{
seterrno(EINVAL);
return NULL;
}
d = malloc(sizeof(caca_dither_t));
if(!d)
{
seterrno(ENOMEM);
return NULL;
}
if(!lookup_initialised)
{
/* XXX: because we do not wish to be thread-safe, there is a slight
* chance that the following code will be executed twice. It is
* totally harmless. */
init_lookup();
lookup_initialised = 1;
}
d->bpp = bpp;
d->has_palette = 0;
d->has_alpha = amask ? 1 : 0;
d->w = w;
d->h = h;
d->pitch = pitch;
d->rmask = rmask;
d->gmask = gmask;
d->bmask = bmask;
d->amask = amask;
/* Load bitmasks */
if(rmask || gmask || bmask || amask)
{
mask2shift(rmask, &d->rright, &d->rleft);
mask2shift(gmask, &d->gright, &d->gleft);
mask2shift(bmask, &d->bright, &d->bleft);
mask2shift(amask, &d->aright, &d->aleft);
}
/* In 8 bpp mode, default to a grayscale palette */
if(bpp == 8)
{
d->has_palette = 1;
d->has_alpha = 0;
for(i = 0; i < 256; i++)
{
d->red[i] = i * 0xfff / 256;
d->green[i] = i * 0xfff / 256;
d->blue[i] = i * 0xfff / 256;
}
}
/* Default gamma value */
d->gamma = 1.0;
for(i = 0; i < 4096; i++)
d->gammatab[i] = i;
/* Default colour properties */
d->brightness = 1.0;
d->contrast = 1.0;
/* Default features */
d->antialias_name = "prefilter";
d->antialias = 1;
d->color_name = "full16";
d->color = COLOR_MODE_FULL16;
d->glyph_name = "ascii";
d->glyphs = ascii_glyphs;
d->glyph_count = sizeof(ascii_glyphs) / sizeof(*ascii_glyphs);
d->algo_name = "fstein";
d->init_dither = init_fstein_dither;
d->get_dither = get_fstein_dither;
d->increment_dither = increment_fstein_dither;
d->invert = 0;
return d;
}
/** \brief Set the palette of an 8bpp dither object.
*
* Set the palette of an 8 bits per pixel bitmap. Values should be between
* 0 and 4095 (0xfff).
*
* If an error occurs, -1 is returned and \b errno is set accordingly:
* - \c EINVAL Dither bits per pixel value is not 8, or one of the pixel
* values was outside the range 0 - 4095.
*
* \param d Dither object.
* \param red Array of 256 red values.
* \param green Array of 256 green values.
* \param blue Array of 256 blue values.
* \param alpha Array of 256 alpha values.
* \return 0 in case of success, -1 if an error occurred.
*/
int caca_set_dither_palette(caca_dither_t *d,
uint32_t red[], uint32_t green[],
uint32_t blue[], uint32_t alpha[])
{
int i, has_alpha = 0;
if(d->bpp != 8)
{
seterrno(EINVAL);
return -1;
}
for(i = 0; i < 256; i++)
{
if((red[i] | green[i] | blue[i] | alpha[i]) >= 0x1000)
{
seterrno(EINVAL);
return -1;
}
}
for(i = 0; i < 256; i++)
{
d->red[i] = red[i];
d->green[i] = green[i];
d->blue[i] = blue[i];
if(alpha[i])
{
d->alpha[i] = alpha[i];
has_alpha = 1;
}
}
d->has_alpha = has_alpha;
return 0;
}
/** \brief Set the brightness of a dither object.
*
* Set the brightness of dither.
*
* If an error occurs, -1 is returned and \b errno is set accordingly:
* - \c EINVAL Brightness value was out of range.
*
* \param d Dither object.
* \param brightness brightness value.
* \return 0 in case of success, -1 if an error occurred.
*/
int caca_set_dither_brightness(caca_dither_t *d, float brightness)
{
/* FIXME */
d->brightness = brightness;
return 0;
}
/** \brief Get the brightness of a dither object.
*
* Get the brightness of the given dither object.
*
* This function never fails.
*
* \param d Dither object.
* \return Brightness value.
*/
float caca_get_dither_brightness(caca_dither_t const *d)
{
return d->brightness;
}
/** \brief Set the gamma of a dither object.
*
* Set the gamma of the given dither object. A negative value causes
* colour inversion.
*
* If an error occurs, -1 is returned and \b errno is set accordingly:
* - \c EINVAL Gamma value was out of range.
*
* \param d Dither object.
* \param gamma Gamma value.
* \return 0 in case of success, -1 if an error occurred.
*/
int caca_set_dither_gamma(caca_dither_t *d, float gamma)
{
/* FIXME: we don't need 4096 calls to gammapow(), we could just compute
* a few of them and do linear interpolation for the rest. This will
* probably speed up things a lot. */
int i;
if(gamma < 0.0)
{
d->invert = 1;
gamma = -gamma;
}
else if(gamma == 0.0)
{
seterrno(EINVAL);
return -1;
}
d->gamma = gamma;
for(i = 0; i < 4096; i++)
d->gammatab[i] = 4096.0 * gammapow((float)i / 4096.0, 1.0 / gamma);
return 0;
}
/** \brief Get the gamma of a dither object.
*
* Get the gamma of the given dither object.
*
* This function never fails.
*
* \param d Dither object.
* \return Gamma value.
*/
float caca_get_dither_gamma(caca_dither_t const *d)
{
return d->gamma;
}
/** \brief Set the contrast of a dither object.
*
* Set the contrast of dither.
*
* If an error occurs, -1 is returned and \b errno is set accordingly:
* - \c EINVAL Contrast value was out of range.
*
* \param d Dither object.
* \param contrast contrast value.
* \return 0 in case of success, -1 if an error occurred.
*/
int caca_set_dither_contrast(caca_dither_t *d, float contrast)
{
/* FIXME */
d->contrast = contrast;
return 0;
}
/** \brief Get the contrast of a dither object.
*
* Get the contrast of the given dither object.
*
* This function never fails.
*
* \param d Dither object.
* \return Contrast value.
*/
float caca_get_dither_contrast(caca_dither_t const *d)
{
return d->contrast;
}
/** \brief Set dither antialiasing
*
* Tell the renderer whether to antialias the dither. Antialiasing smoothens
* the rendered image and avoids the commonly seen staircase effect.
* - \c "none": no antialiasing.
* - \c "prefilter" or \c "default": simple prefilter antialiasing. This
* is the default value.
*
* If an error occurs, -1 is returned and \b errno is set accordingly:
* - \c EINVAL Invalid antialiasing mode.
*
* \param d Dither object.
* \param str A string describing the antialiasing method that will be used
* for the dithering.
* \return 0 in case of success, -1 if an error occurred.
*/
int caca_set_dither_antialias(caca_dither_t *d, char const *str)
{
if(!strcasecmp(str, "none"))
{
d->antialias_name = "none";
d->antialias = 0;
}
else if(!strcasecmp(str, "prefilter") || !strcasecmp(str, "default"))
{
d->antialias_name = "prefilter";
d->antialias = 1;
}
else
{
seterrno(EINVAL);
return -1;
}
return 0;
}
/** \brief Get available antialiasing methods
*
* Return a list of available antialiasing methods for a given dither. The
* list is a NULL-terminated array of strings, interleaving a string
* containing the internal value for the antialiasing method to be used with
* caca_set_dither_antialias(), and a string containing the natural
* language description for that antialiasing method.
*
* This function never fails.
*
* \param d Dither object.
* \return An array of strings.
*/
char const * const *
caca_get_dither_antialias_list(caca_dither_t const *d)
{
static char const * const list[] =
{
"none", "No antialiasing",
"prefilter", "Prefilter antialiasing",
NULL, NULL
};
return list;
}
/** \brief Get current antialiasing method
*
* Return the given dither's current antialiasing method.
*
* This function never fails.
*
* \param d Dither object.
* \return A static string.
*/
char const * caca_get_dither_antialias(caca_dither_t const *d)
{
return d->antialias_name;
}
/** \brief Choose colours used for dithering
*
* Tell the renderer which colours should be used to render the
* bitmap. Valid values for \c str are:
* - \c "mono": use light gray on a black background.
* - \c "gray": use white and two shades of gray on a black background.
* - \c "8": use the 8 ANSI colours on a black background.
* - \c "16": use the 16 ANSI colours on a black background.
* - \c "fullgray": use black, white and two shades of gray for both the
* characters and the background.
* - \c "full8": use the 8 ANSI colours for both the characters and the
* background.
* - \c "full16" or \c "default": use the 16 ANSI colours for both the
* characters and the background. This is the default value.
*
* If an error occurs, -1 is returned and \b errno is set accordingly:
* - \c EINVAL Invalid colour set.
*
* \param d Dither object.
* \param str A string describing the colour set that will be used
* for the dithering.
* \return 0 in case of success, -1 if an error occurred.
*/
int caca_set_dither_color(caca_dither_t *d, char const *str)
{
if(!strcasecmp(str, "mono"))
{
d->color_name = "mono";
d->color = COLOR_MODE_MONO;
}
else if(!strcasecmp(str, "gray"))
{
d->color_name = "gray";
d->color = COLOR_MODE_GRAY;
}
else if(!strcasecmp(str, "8"))
{
d->color_name = "8";
d->color = COLOR_MODE_8;
}
else if(!strcasecmp(str, "16"))
{
d->color_name = "16";
d->color = COLOR_MODE_16;
}
else if(!strcasecmp(str, "fullgray"))
{
d->color_name = "fullgray";
d->color = COLOR_MODE_FULLGRAY;
}
else if(!strcasecmp(str, "full8"))
{
d->color_name = "full8";
d->color = COLOR_MODE_FULL8;
}
else if(!strcasecmp(str, "full16") || !strcasecmp(str, "default"))
{
d->color_name = "full16";
d->color = COLOR_MODE_FULL16;
}
else
{
seterrno(EINVAL);
return -1;
}
return 0;
}
/** \brief Get available colour modes
*
* Return a list of available colour modes for a given dither. The list
* is a NULL-terminated array of strings, interleaving a string containing
* the internal value for the colour mode, to be used with
* caca_set_dither_color(), and a string containing the natural
* language description for that colour mode.
*
* This function never fails.
*
* \param d Dither object.
* \return An array of strings.
*/
char const * const *
caca_get_dither_color_list(caca_dither_t const *d)
{
static char const * const list[] =
{
"mono", "white on black",
"gray", "grayscale on black",
"8", "8 colours on black",
"16", "16 colours on black",
"fullgray", "full grayscale",
"full8", "full 8 colours",
"full16", "full 16 colours",
NULL, NULL
};
return list;
}
/** \brief Get current colour mode
*
* Return the given dither's current colour mode.
*
* This function never fails.
*
* \param d Dither object.
* \return A static string.
*/
char const * caca_get_dither_color(caca_dither_t const *d)
{
return d->color_name;
}
/** \brief Choose characters used for dithering
*
* Tell the renderer which characters should be used to render the
* dither. Valid values for \c str are:
* - \c "ascii" or \c "default": use only ASCII characters. This is the
* default value.
* - \c "shades": use Unicode characters "U+2591 LIGHT SHADE", "U+2592
* MEDIUM SHADE" and "U+2593 DARK SHADE". These characters are also
* present in the CP437 codepage available on DOS and VGA.
* - \c "blocks": use Unicode quarter-cell block combinations. These
* characters are only found in the Unicode set.
*
* If an error occurs, -1 is returned and \b errno is set accordingly:
* - \c EINVAL Invalid character set.
*
* \param d Dither object.
* \param str A string describing the characters that need to be used
* for the dithering.
* \return 0 in case of success, -1 if an error occurred.
*/
int caca_set_dither_charset(caca_dither_t *d, char const *str)
{
if(!strcasecmp(str, "shades"))
{
d->glyph_name = "shades";
d->glyphs = shades_glyphs;
d->glyph_count = sizeof(shades_glyphs) / sizeof(*shades_glyphs);
}
else if(!strcasecmp(str, "blocks"))
{
d->glyph_name = "blocks";
d->glyphs = blocks_glyphs;
d->glyph_count = sizeof(blocks_glyphs) / sizeof(*blocks_glyphs);
}
else if(!strcasecmp(str, "ascii") || !strcasecmp(str, "default"))
{
d->glyph_name = "ascii";
d->glyphs = ascii_glyphs;
d->glyph_count = sizeof(ascii_glyphs) / sizeof(*ascii_glyphs);
}
else
{
seterrno(EINVAL);
return -1;
}
return 0;
}
/** \brief Get available dither character sets
*
* Return a list of available character sets for a given dither. The list
* is a NULL-terminated array of strings, interleaving a string containing
* the internal value for the character set, to be used with
* caca_set_dither_charset(), and a string containing the natural
* language description for that character set.
*
* This function never fails.
*
* \param d Dither object.
* \return An array of strings.
*/
char const * const * caca_get_dither_charset_list(caca_dither_t const *d)
{
static char const * const list[] =
{
"ascii", "plain ASCII",
"shades", "CP437 shades",
"blocks", "Unicode blocks",
NULL, NULL
};
return list;
}
/** \brief Get current character set
*
* Return the given dither's current character set.
*
* This function never fails.
*
* \param d Dither object.
* \return A static string.
*/
char const * caca_get_dither_charset(caca_dither_t const *d)
{
return d->glyph_name;
}
/** \brief Set dithering algorithm
*
* Tell the renderer which dithering algorithm should be used. Dithering is
* necessary because the picture being rendered has usually far more colours
* than the available palette. Valid values for \c str are:
* - \c "none": no dithering is used, the nearest matching colour is used.
* - \c "ordered2": use a 2x2 Bayer matrix for dithering.
* - \c "ordered4": use a 4x4 Bayer matrix for dithering.
* - \c "ordered8": use a 8x8 Bayer matrix for dithering.
* - \c "random": use random dithering.
* - \c "fstein": use Floyd-Steinberg dithering. This is the default value.
*
* If an error occurs, -1 is returned and \b errno is set accordingly:
* - \c EINVAL Unknown dithering mode.
*
* \param d Dither object.
* \param str A string describing the algorithm that needs to be used
* for the dithering.
* \return 0 in case of success, -1 if an error occurred.
*/
int caca_set_dither_algorithm(caca_dither_t *d, char const *str)
{
if(!strcasecmp(str, "none"))
{
d->algo_name = "none";
d->init_dither = init_no_dither;
d->get_dither = get_no_dither;
d->increment_dither = increment_no_dither;
}
else if(!strcasecmp(str, "ordered2"))
{
d->algo_name = "ordered2";
d->init_dither = init_ordered2_dither;
d->get_dither = get_ordered2_dither;
d->increment_dither = increment_ordered2_dither;
}
else if(!strcasecmp(str, "ordered4"))
{
d->algo_name = "ordered4";
d->init_dither = init_ordered4_dither;
d->get_dither = get_ordered4_dither;
d->increment_dither = increment_ordered4_dither;
}
else if(!strcasecmp(str, "ordered8"))
{
d->algo_name = "ordered8";
d->init_dither = init_ordered8_dither;
d->get_dither = get_ordered8_dither;
d->increment_dither = increment_ordered8_dither;
}
else if(!strcasecmp(str, "random"))
{
d->algo_name = "random";
d->init_dither = init_random_dither;
d->get_dither = get_random_dither;
d->increment_dither = increment_random_dither;
}
else if(!strcasecmp(str, "fstein") || !strcasecmp(str, "default"))
{
d->algo_name = "fstein";
d->init_dither = init_fstein_dither;
d->get_dither = get_fstein_dither;
d->increment_dither = increment_fstein_dither;
}
else
{
seterrno(EINVAL);
return -1;
}
return 0;
}
/** \brief Get dithering algorithms
*
* Return a list of available dithering algorithms for a given dither. The
* list is a NULL-terminated array of strings, interleaving a string
* containing the internal value for the dithering algorithm, to be used
* with caca_set_dither_dithering(), and a string containing the natural
* language description for that algorithm.
*
* This function never fails.
*
* \param d Dither object.
* \return An array of strings.
*/
char const * const * caca_get_dither_algorithm_list(caca_dither_t const *d)
{
static char const * const list[] =
{
"none", "no dithering",
"ordered2", "2x2 ordered dithering",
"ordered4", "4x4 ordered dithering",
"ordered8", "8x8 ordered dithering",
"random", "random dithering",
"fstein", "Floyd-Steinberg dithering",
NULL, NULL
};
return list;
}
/** \brief Get current dithering algorithm
*
* Return the given dither's current dithering algorithm.
*
* This function never fails.
*
* \param d Dither object.
* \return A static string.
*/
char const * caca_get_dither_algorithm(caca_dither_t const *d)
{
return d->algo_name;
}
/** \brief Dither a bitmap on the canvas.
*
* Dither a bitmap at the given coordinates. The dither can be of any size
* and will be stretched to the text area.
*
* This function never fails.
*
* \param cv A handle to the libcaca canvas.
* \param x X coordinate of the upper-left corner of the drawing area.
* \param y Y coordinate of the upper-left corner of the drawing area.
* \param w Width of the drawing area.
* \param h Height of the drawing area.
* \param d Dither object to be drawn.
* \param pixels Bitmap's pixels.
* \return This function always returns 0.
*/
int caca_dither_bitmap(caca_canvas_t *cv, int x, int y, int w, int h,
caca_dither_t const *d, void const *pixels)
{
int *floyd_steinberg, *fs_r, *fs_g, *fs_b;
uint32_t savedattr;
int fs_length;
int x1, y1, x2, y2, pitch, deltax, deltay, dchmax;
if(!d || !pixels)
return 0;
savedattr = caca_get_attr(cv, -1, -1);
x1 = x; x2 = x + w - 1;
y1 = y; y2 = y + h - 1;
/* FIXME: do not overwrite arguments */
w = d->w;
h = d->h;
pitch = d->pitch;
deltax = x2 - x1 + 1;
deltay = y2 - y1 + 1;
dchmax = d->glyph_count;
fs_length = ((int)cv->width <= x2 ? (int)cv->width : x2) + 1;
floyd_steinberg = malloc(3 * (fs_length + 2) * sizeof(int));
memset(floyd_steinberg, 0, 3 * (fs_length + 2) * sizeof(int));
fs_r = floyd_steinberg + 1;
fs_g = fs_r + fs_length + 2;
fs_b = fs_g + fs_length + 2;
for(y = y1 > 0 ? y1 : 0; y <= y2 && y <= (int)cv->height; y++)
{
int remain_r = 0, remain_g = 0, remain_b = 0;
for(x = x1 > 0 ? x1 : 0, d->init_dither(y);
x <= x2 && x <= (int)cv->width;
x++)
{
unsigned int rgba[4];
int error[3];
int i, ch = 0, distmin;
int fg_r = 0, fg_g = 0, fg_b = 0, bg_r, bg_g, bg_b;
int fromx, fromy, tox, toy, myx, myy, dots, dist;
int outfg = 0, outbg = 0;
uint32_t outch;
rgba[0] = rgba[1] = rgba[2] = rgba[3] = 0;
/* First get RGB */
if(d->antialias)
{
fromx = (uint64_t)(x - x1) * w / deltax;
fromy = (uint64_t)(y - y1) * h / deltay;
tox = (uint64_t)(x - x1 + 1) * w / deltax;
toy = (uint64_t)(y - y1 + 1) * h / deltay;
/* We want at least one pixel */
if(tox == fromx) tox++;