File: attr.c

package info (click to toggle)
libcaca 0.99.beta20-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,540 kB
  • sloc: ansic: 25,091; php: 2,763; python: 2,637; cs: 1,213; cpp: 1,127; java: 916; objc: 836; makefile: 545; perl: 505; sh: 472; asm: 297; ruby: 215; xml: 33
file content (527 lines) | stat: -rw-r--r-- 16,837 bytes parent folder | download | duplicates (3)
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
/*
 *  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 Sam Hocevar. See
 *  http://www.wtfpl.net/ for more details.
 */

/*
 *  This file contains functions for attribute management and colourspace
 *  conversions.
 */

#include "config.h"

#include "caca.h"
#include "caca_internals.h"

static uint8_t nearest_ansi(uint16_t);

/* RGB colours for the ANSI palette. There is no real standard, so we
 * use the same values as gnome-terminal. The 7th colour (brown) is a bit
 * special: 0xfa50 instead of 0xfaa0. */
static const uint16_t ansitab16[16] =
{
    0xf000, 0xf00a, 0xf0a0, 0xf0aa, 0xfa00, 0xfa0a, 0xfa50, 0xfaaa,
    0xf555, 0xf55f, 0xf5f5, 0xf5ff, 0xff55, 0xff5f, 0xfff5, 0xffff,
};

/* Same table, except on 14 bits (3-4-4-3) */
static const uint16_t ansitab14[16] =
{
    0x3800, 0x3805, 0x3850, 0x3855, 0x3d00, 0x3d05, 0x3d28, 0x3d55,
    0x3aaa, 0x3aaf, 0x3afa, 0x3aff, 0x3faa, 0x3faf, 0x3ffa, 0x3fff,
};

/** \brief Get the text attribute at the given coordinates.
 *
 *  Get the internal \e libcaca attribute value of the character at the
 *  given coordinates. The attribute value has 32 significant bits,
 *  organised as follows from MSB to LSB:
 *  - 3 bits for the background alpha
 *  - 4 bits for the background red component
 *  - 4 bits for the background green component
 *  - 3 bits for the background blue component
 *  - 3 bits for the foreground alpha
 *  - 4 bits for the foreground red component
 *  - 4 bits for the foreground green component
 *  - 3 bits for the foreground blue component
 *  - 4 bits for the bold, italics, underline and blink flags
 *
 *  If the coordinates are outside the canvas boundaries, the current
 *  attribute is returned.
 *
 *  This function never fails.
 *
 *  \param cv A handle to the libcaca canvas.
 *  \param x X coordinate.
 *  \param y Y coordinate.
 *  \return The requested attribute.
 */
uint32_t caca_get_attr(caca_canvas_t const *cv, int x, int y)
{
    if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
        return cv->curattr;

    return cv->attrs[x + y * cv->width];
}

/** \brief Set the default character attribute.
 *
 *  Set the default character attribute for drawing. Attributes define
 *  foreground and background colour, transparency, bold, italics and
 *  underline styles, as well as blink. String functions such as
 *  caca_printf() and graphical primitive functions such as caca_draw_line()
 *  will use this attribute.
 *
 *  The value of \e attr is either:
 *  - a 32-bit integer as returned by caca_get_attr(), in which case it
 *    also contains colour information,
 *  - a combination (bitwise OR) of style values (\e CACA_UNDERLINE,
 *    \e CACA_BLINK, \e CACA_BOLD and \e CACA_ITALICS), in which case
 *    setting the attribute does not modify the current colour information.
 *
 *  To retrieve the current attribute value, use caca_get_attr(-1,-1).
 *
 *  This function never fails.
 *
 *  \param cv A handle to the libcaca canvas.
 *  \param attr The requested attribute value.
 *  \return This function always returns 0.
 */
int caca_set_attr(caca_canvas_t *cv, uint32_t attr)
{
    if(attr < 0x00000010)
        attr = (cv->curattr & 0xfffffff0) | attr;

    cv->curattr = attr;

    return 0;
}

/** \brief Unset flags in the default character attribute.
 *
 *  Unset flags in the default character attribute for drawing. Attributes
 *  define foreground and background colour, transparency, bold, italics and
 *  underline styles, as well as blink. String functions such as
 *  caca_printf() and graphical primitive functions such as caca_draw_line()
 *  will use this attribute.
 *
 *  The value of \e attr is a combination (bitwise OR) of style values
 *  (\e CACA_UNDERLINE, \e CACA_BLINK, \e CACA_BOLD and \e CACA_ITALICS).
 *  Unsetting these attributes does not modify the current colour information.
 *
 *  To retrieve the current attribute value, use caca_get_attr(-1,-1).
 *
 *  This function never fails.
 *
 *  \param cv A handle to the libcaca canvas.
 *  \param attr The requested attribute values to unset.
 *  \return This function always returns 0.
 */
int caca_unset_attr(caca_canvas_t *cv, uint32_t attr)
{
    cv->curattr &= ~(attr & 0x0000000f);

    return 0;
}

/** \brief Toggle flags in the default character attribute.
 *
 *  Toggle flags in the default character attribute for drawing. Attributes
 *  define foreground and background colour, transparency, bold, italics and
 *  underline styles, as well as blink. String functions such as
 *  caca_printf() and graphical primitive functions such as caca_draw_line()
 *  will use this attribute.
 *
 *  The value of \e attr is a combination (bitwise OR) of style values
 *  (\e CACA_UNDERLINE, \e CACA_BLINK, \e CACA_BOLD and \e CACA_ITALICS).
 *  Toggling these attributes does not modify the current colour information.
 *
 *  To retrieve the current attribute value, use caca_get_attr(-1,-1).
 *
 *  This function never fails.
 *
 *  \param cv A handle to the libcaca canvas.
 *  \param attr The requested attribute values to toggle.
 *  \return This function always returns 0.
 */
int caca_toggle_attr(caca_canvas_t *cv, uint32_t attr)
{
    cv->curattr ^= attr & 0x0000000f;

    return 0;
}

/** \brief Set the character attribute at the given coordinates.
 *
 *  Set the character attribute, without changing the character's value. If
 *  the character at the given coordinates is a fullwidth character, both
 *  cells' attributes are replaced.
 *
 *  The value of \e attr is either:
 *  - a 32-bit integer as returned by caca_get_attr(), in which case it
 *    also contains colour information,
 *  - a combination (bitwise OR) of style values (\e CACA_UNDERLINE,
 *    \e CACA_BLINK, \e CACA_BOLD and \e CACA_ITALICS), in which case
 *    setting the attribute does not modify the current colour information.
 *
 *  This function never fails.
 *
 *  \param cv A handle to the libcaca canvas.
 *  \param x X coordinate.
 *  \param y Y coordinate.
 *  \param attr The requested attribute value.
 *  \return This function always returns 0.
 */
int caca_put_attr(caca_canvas_t *cv, int x, int y, uint32_t attr)
{
    uint32_t *curattr, *curchar;
    int xmin, xmax;

    if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
        return 0;

    xmin = xmax = x;

    curchar = cv->chars + x + y * cv->width;
    curattr = cv->attrs + x + y * cv->width;

    if(attr < 0x00000010)
        curattr[0] = (curattr[0] & 0xfffffff0) | attr;
    else
        curattr[0] = attr;

    if(x && curchar[0] == CACA_MAGIC_FULLWIDTH)
    {
        curattr[-1] = curattr[0];
        xmin--;
    }
    else if(x + 1 < (int)cv->width && curchar[1] == CACA_MAGIC_FULLWIDTH)
    {
        curattr[1] = curattr[0];
        xmax++;
    }

    if(!cv->dirty_disabled)
        caca_add_dirty_rect(cv, xmin, y, xmax - xmin + 1, 1);

    return 0;
}

/** \brief Set the default colour pair for text (ANSI version).
 *
 *  Set the default ANSI colour pair for text drawing. String functions such
 *  as caca_printf() and graphical primitive functions such as caca_draw_line()
 *  will use these attributes.
 *
 *  Color values are those defined in caca.h, such as CACA_RED
 *  or CACA_TRANSPARENT.
 *
 *  If an error occurs, -1 is returned and \b errno is set accordingly:
 *  - \c EINVAL At least one of the colour values is invalid.
 *
 *  \param cv A handle to the libcaca canvas.
 *  \param fg The requested ANSI foreground colour.
 *  \param bg The requested ANSI background colour.
 *  \return 0 in case of success, -1 if an error occurred.
 */
int caca_set_color_ansi(caca_canvas_t *cv, uint8_t fg, uint8_t bg)
{
    uint32_t attr;

    if(fg > 0x20 || bg > 0x20)
    {
        seterrno(EINVAL);
        return -1;
    }

    attr = ((uint32_t)(bg | 0x40) << 18) | ((uint32_t)(fg | 0x40) << 4);
    cv->curattr = (cv->curattr & 0x0000000f) | attr;

    return 0;
}

/** \brief Set the default colour pair for text (truecolor version).
 *
 *  Set the default ARGB colour pair for text drawing. String functions such
 *  as caca_printf() and graphical primitive functions such as caca_draw_line()
 *  will use these attributes.
 *
 *  Colors are 16-bit ARGB values, each component being coded on 4 bits. For
 *  instance, 0xf088 is solid dark cyan (A=15 R=0 G=8 B=8), and 0x8fff is
 *  white with 50% alpha (A=8 R=15 G=15 B=15).
 *
 *  This function never fails.
 *
 *  \param cv A handle to the libcaca canvas.
 *  \param fg The requested ARGB foreground colour.
 *  \param bg The requested ARGB background colour.
 *  \return This function always returns 0.
 */
int caca_set_color_argb(caca_canvas_t *cv, uint16_t fg, uint16_t bg)
{
    uint32_t attr;

    if(fg < 0x100)
        fg += 0x100;

    if(bg < 0x100)
        bg += 0x100;

    fg = ((fg >> 1) & 0x7ff) | ((fg >> 13) << 11);
    bg = ((bg >> 1) & 0x7ff) | ((bg >> 13) << 11);

    attr = ((uint32_t)bg << 18) | ((uint32_t)fg << 4);
    cv->curattr = (cv->curattr & 0x0000000f) | attr;

    return 0;
}

/** \brief Get DOS ANSI information from attribute.
 *
 *  Get the ANSI colour pair for a given attribute. The returned value is
 *  an 8-bit value whose higher 4 bits are the background colour and lower
 *  4 bits are the foreground colour.
 *
 *  If the attribute has ARGB colours, the nearest colour is used. Special
 *  attributes such as \e CACA_DEFAULT and \e CACA_TRANSPARENT are not
 *  handled and are both replaced with \e CACA_LIGHTGRAY for the foreground
 *  colour and \e CACA_BLACK for the background colour.
 *
 *  This function never fails. If the attribute value is outside the expected
 *  32-bit range, higher order bits are simply ignored.
 *
 *  \param attr The requested attribute value.
 *  \return The corresponding DOS ANSI value.
 */
uint8_t caca_attr_to_ansi(uint32_t attr)
{
    uint8_t fg = nearest_ansi((attr >> 4) & 0x3fff);
    uint8_t bg = nearest_ansi(attr >> 18);

    return (fg < 0x10 ? fg : CACA_LIGHTGRAY)
            | ((bg < 0x10 ? bg : CACA_BLACK) << 4);
}

/** \brief Get ANSI foreground information from attribute.
 *
 *  Get the ANSI foreground colour value for a given attribute. The returned
 *  value is either one of the \e CACA_RED, \e CACA_BLACK etc. predefined
 *  colours, or the special value \e CACA_DEFAULT meaning the media's
 *  default foreground value, or the special value \e CACA_TRANSPARENT.
 *
 *  If the attribute has ARGB colours, the nearest colour is returned.
 *
 *  This function never fails. If the attribute value is outside the expected
 *  32-bit range, higher order bits are simply ignored.
 *
 *  \param attr The requested attribute value.
 *  \return The corresponding ANSI foreground value.
 */
uint8_t caca_attr_to_ansi_fg(uint32_t attr)
{
    return nearest_ansi((attr >> 4) & 0x3fff);
}

/** \brief Get ANSI background information from attribute.
 *
 *  Get the ANSI background colour value for a given attribute. The returned
 *  value is either one of the \e CACA_RED, \e CACA_BLACK etc. predefined
 *  colours, or the special value \e CACA_DEFAULT meaning the media's
 *  default background value, or the special value \e CACA_TRANSPARENT.
 *
 *  If the attribute has ARGB colours, the nearest colour is returned.
 *
 *  This function never fails. If the attribute value is outside the expected
 *  32-bit range, higher order bits are simply ignored.
 *
 *  \param attr The requested attribute value.
 *  \return The corresponding ANSI background value.
 */
uint8_t caca_attr_to_ansi_bg(uint32_t attr)
{
    return nearest_ansi(attr >> 18);
}

/** \brief Get 12-bit RGB foreground information from attribute.
 *
 *  Get the 12-bit foreground colour value for a given attribute. The returned
 *  value is a native-endian encoded integer with each red, green and blue
 *  values encoded on 8 bits in the following order:
 *   - 8-11 most significant bits: red
 *   - 4-7 most significant bits: green
 *   - least significant bits: blue
 *
 *  This function never fails. If the attribute value is outside the expected
 *  32-bit range, higher order bits are simply ignored.
 *
 *  \param attr The requested attribute value.
 *  \return The corresponding 12-bit RGB foreground value.
 */
uint16_t caca_attr_to_rgb12_fg(uint32_t attr)
{
    uint16_t fg = (attr >> 4) & 0x3fff;

    if(fg < (0x10 | 0x40))
        return ansitab16[fg ^ 0x40] & 0x0fff;

    if(fg == (CACA_DEFAULT | 0x40))
        return ansitab16[CACA_LIGHTGRAY] & 0x0fff;

    if(fg == (CACA_TRANSPARENT | 0x40))
        return ansitab16[CACA_LIGHTGRAY] & 0x0fff;

    return (fg << 1) & 0x0fff;
}

/** \brief Get 12-bit RGB background information from attribute.
 *
 *  Get the 12-bit background colour value for a given attribute. The returned
 *  value is a native-endian encoded integer with each red, green and blue
 *  values encoded on 8 bits in the following order:
 *   - 8-11 most significant bits: red
 *   - 4-7 most significant bits: green
 *   - least significant bits: blue
 *
 *  This function never fails. If the attribute value is outside the expected
 *  32-bit range, higher order bits are simply ignored.
 *
 *  \param attr The requested attribute value.
 *  \return The corresponding 12-bit RGB background value.
 */
uint16_t caca_attr_to_rgb12_bg(uint32_t attr)
{
    uint16_t bg = attr >> 18;

    if(bg < (0x10 | 0x40))
        return ansitab16[bg ^ 0x40] & 0x0fff;

    if(bg == (CACA_DEFAULT | 0x40))
        return ansitab16[CACA_BLACK] & 0x0fff;

    if(bg == (CACA_TRANSPARENT | 0x40))
        return ansitab16[CACA_BLACK] & 0x0fff;

    return (bg << 1) & 0x0fff;
}

/** \brief Get 64-bit ARGB information from attribute.
 *
 *  Get the 64-bit colour and alpha values for a given attribute. The values
 *  are written as 8-bit integers in the \e argb array in the following order:
 *   - \e argb[0]: background alpha value
 *   - \e argb[1]: background red value
 *   - \e argb[2]: background green value
 *   - \e argb[3]: background blue value
 *   - \e argb[4]: foreground alpha value
 *   - \e argb[5]: foreground red value
 *   - \e argb[6]: foreground green value
 *   - \e argb[7]: foreground blue value
 *
 *  This function never fails. If the attribute value is outside the expected
 *  32-bit range, higher order bits are simply ignored.
 *
 *  \param attr The requested attribute value.
 *  \param argb An array of 8-bit integers.
 */
void caca_attr_to_argb64(uint32_t attr, uint8_t argb[8])
{
    uint16_t fg = (attr >> 4) & 0x3fff;
    uint16_t bg = attr >> 18;

    if(bg < (0x10 | 0x40))
        bg = ansitab16[bg ^ 0x40];
    else if(bg == (CACA_DEFAULT | 0x40))
        bg = ansitab16[CACA_BLACK];
    else if(bg == (CACA_TRANSPARENT | 0x40))
        bg = 0x0fff;
    else
        bg = ((bg << 2) & 0xf000) | ((bg << 1) & 0x0fff);

    argb[0] = bg >> 12;
    argb[1] = (bg >> 8) & 0xf;
    argb[2] = (bg >> 4) & 0xf;
    argb[3] = bg & 0xf;

    if(fg < (0x10 | 0x40))
        fg = ansitab16[fg ^ 0x40];
    else if(fg == (CACA_DEFAULT | 0x40))
        fg = ansitab16[CACA_LIGHTGRAY];
    else if(fg == (CACA_TRANSPARENT | 0x40))
        fg = 0x0fff;
    else
        fg = ((fg << 2) & 0xf000) | ((fg << 1) & 0x0fff);

    argb[4] = fg >> 12;
    argb[5] = (fg >> 8) & 0xf;
    argb[6] = (fg >> 4) & 0xf;
    argb[7] = fg & 0xf;
}

/*
 * XXX: the following functions are local
 */

static uint8_t nearest_ansi(uint16_t argb14)
{
    unsigned int i, best, dist;

    if(argb14 < (0x10 | 0x40))
        return argb14 ^ 0x40;

    if(argb14 == (CACA_DEFAULT | 0x40) || argb14 == (CACA_TRANSPARENT | 0x40))
        return argb14 ^ 0x40;

    if(argb14 < 0x0fff) /* too transparent */
        return CACA_TRANSPARENT;

    best = CACA_DEFAULT;
    dist = 0x3fff;
    for(i = 0; i < 16; i++)
    {
        unsigned int d = 0;
        int a, b;

        a = (ansitab14[i] >> 7) & 0xf;
        b = (argb14 >> 7) & 0xf;
        d += (a - b) * (a - b);

        a = (ansitab14[i] >> 3) & 0xf;
        b = (argb14 >> 3) & 0xf;
        d += (a - b) * (a - b);

        a = (ansitab14[i] << 1) & 0xf;
        b = (argb14 << 1) & 0xf;
        d += (a - b) * (a - b);

        if(d < dist)
        {
            dist = d;
            best = i;
        }
    }

    return best;
}

#define RGB12TO24(i) \
   (((uint32_t)((i & 0xf00) >> 8) * 0x110000) \
  | ((uint32_t)((i & 0x0f0) >> 4) * 0x001100) \
  | ((uint32_t)(i & 0x00f) * 0x000011))

uint32_t _caca_attr_to_rgb24fg(uint32_t attr)
{
    return RGB12TO24(caca_attr_to_rgb12_fg(attr));
}

uint32_t _caca_attr_to_rgb24bg(uint32_t attr)
{
    return RGB12TO24(caca_attr_to_rgb12_bg(attr));
}