source: libcaca/trunk/cucul/attr.c @ 2299

Last change on this file since 2299 was 2299, checked in by Sam Hocevar, 12 years ago
  • Remove #include <stdint.h> etc. from "common.h". Instead, make sure that <cucul.h> will provide the C99 types, even if libcaca has been installed.
  • Rename what's left of "common.h" to "stubs.h".
  • Remove all references to erroneous <inttypes.h> from source files.
  • Property svn:keywords set to Id
File size: 15.3 KB
Line 
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: attr.c 2299 2008-04-19 12:42:50Z sam $
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains functions for attribute management and colourspace
17 *  conversions.
18 */
19
20#include "config.h"
21
22#include "cucul.h"
23#include "cucul_internals.h"
24
25static uint8_t nearest_ansi(uint16_t);
26
27/* RGB colours for the ANSI palette. There is no real standard, so we
28 * use the same values as gnome-terminal. The 7th colour (brown) is a bit
29 * special: 0xfa50 instead of 0xfaa0. */
30static const uint16_t ansitab16[16] =
31{
32    0xf000, 0xf00a, 0xf0a0, 0xf0aa, 0xfa00, 0xfa0a, 0xfa50, 0xfaaa,
33    0xf555, 0xf55f, 0xf5f5, 0xf5ff, 0xff55, 0xff5f, 0xfff5, 0xffff,
34};
35
36/* Same table, except on 14 bits (3-4-4-3) */
37static const uint16_t ansitab14[16] =
38{
39    0x3800, 0x3805, 0x3850, 0x3855, 0x3d00, 0x3d05, 0x3d28, 0x3d55,
40    0x3aaa, 0x3aaf, 0x3afa, 0x3aff, 0x3faa, 0x3faf, 0x3ffa, 0x3fff,
41};
42
43/** \brief Get the text attribute at the given coordinates.
44 *
45 *  Get the internal \e libcucul attribute value of the character at the
46 *  given coordinates. The attribute value has 32 significant bits,
47 *  organised as follows from MSB to LSB:
48 *  - 3 bits for the background alpha
49 *  - 4 bits for the background red component
50 *  - 4 bits for the background green component
51 *  - 3 bits for the background blue component
52 *  - 3 bits for the foreground alpha
53 *  - 4 bits for the foreground red component
54 *  - 4 bits for the foreground green component
55 *  - 3 bits for the foreground blue component
56 *  - 4 bits for the bold, italics, underline and blink flags
57 *
58 *  If the coordinates are outside the canvas boundaries, the current
59 *  attribute is returned.
60 *
61 *  This function never fails.
62 *
63 *  \param cv A handle to the libcucul canvas.
64 *  \param x X coordinate.
65 *  \param y Y coordinate.
66 *  \return The requested attribute.
67 */
68unsigned long int cucul_get_attr(cucul_canvas_t const *cv, int x, int y)
69{
70    if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
71        return (unsigned long int)cv->curattr;
72
73    return (unsigned long int)cv->attrs[x + y * cv->width];
74}
75
76/** \brief Set the default character attribute.
77 *
78 *  Set the default character attribute for drawing. Attributes define
79 *  foreground and background colour, transparency, bold, italics and
80 *  underline styles, as well as blink. String functions such as
81 *  caca_printf() and graphical primitive functions such as caca_draw_line()
82 *  will use this attribute.
83 *
84 *  The value of \e attr is either:
85 *  - a 32-bit integer as returned by cucul_get_attr(), in which case it
86 *    also contains colour information,
87 *  - a combination (bitwise OR) of style values (\e CUCUL_UNDERLINE,
88 *    \e CUCUL_BLINK, \e CUCUL_BOLD and \e CUCUL_ITALICS), in which case
89 *    setting the attribute does not modify the current colour information.
90 *
91 *  To retrieve the current attribute value, use cucul_get_attr(-1,-1).
92 *
93 *  If an error occurs, -1 is returned and \b errno is set accordingly:
94 *  - \c EINVAL The attribute value is out of the 32-bit range.
95 *
96 *  \param cv A handle to the libcucul canvas.
97 *  \param attr The requested attribute value.
98 *  \return 0 in case of success, -1 if an error occurred.
99 */
100int cucul_set_attr(cucul_canvas_t *cv, unsigned long int attr)
101{
102    if(sizeof(unsigned long int) > sizeof(uint32_t) && attr > 0xffffffff)
103    {
104        seterrno(EINVAL);
105        return -1;
106    }
107
108    if(attr < 0x00000010)
109        attr = (cv->curattr & 0xfffffff0) | attr;
110
111    cv->curattr = attr;
112
113    return 0;
114}
115
116/** \brief Set the character attribute at the given coordinates.
117 *
118 *  Set the character attribute, without changing the character's value. If
119 *  the character at the given coordinates is a fullwidth character, both
120 *  cells' attributes are replaced.
121 *
122 *  The value of \e attr is either:
123 *  - a 32-bit integer as returned by cucul_get_attr(), in which case it
124 *    also contains colour information,
125 *  - a combination (bitwise OR) of style values (\e CUCUL_UNDERLINE,
126 *    \e CUCUL_BLINK, \e CUCUL_BOLD and \e CUCUL_ITALICS), in which case
127 *    setting the attribute does not modify the current colour information.
128 *
129 *  If an error occurs, -1 is returned and \b errno is set accordingly:
130 *  - \c EINVAL The attribute value is out of the 32-bit range.
131 *
132 *  \param cv A handle to the libcucul canvas.
133 *  \param x X coordinate.
134 *  \param y Y coordinate.
135 *  \param attr The requested attribute value.
136 *  \return 0 in case of success, -1 if an error occurred.
137 */
138int cucul_put_attr(cucul_canvas_t *cv, int x, int y, unsigned long int attr)
139{
140    uint32_t *curattr, *curchar;
141
142    if(sizeof(unsigned long int) > sizeof(uint32_t) && attr > 0xffffffff)
143    {
144        seterrno(EINVAL);
145        return -1;
146    }
147
148    if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
149        return 0;
150
151    curchar = cv->chars + x + y * cv->width;
152    curattr = cv->attrs + x + y * cv->width;
153
154    if(attr < 0x00000010)
155        curattr[0] = (curattr[0] & 0xfffffff0) | attr;
156    else
157        curattr[0] = attr;
158
159    if(x && curchar[0] == CUCUL_MAGIC_FULLWIDTH)
160        curattr[-1] = curattr[0];
161    else if(x + 1 < (int)cv->width && curchar[1] == CUCUL_MAGIC_FULLWIDTH)
162        curattr[1] = curattr[0];
163
164    return 0;
165}
166
167/** \brief Set the default colour pair for text (ANSI version).
168 *
169 *  Set the default ANSI colour pair for text drawing. String functions such
170 *  as caca_printf() and graphical primitive functions such as caca_draw_line()
171 *  will use these attributes.
172 *
173 *  Color values are those defined in cucul.h, such as CUCUL_RED
174 *  or CUCUL_TRANSPARENT.
175 *
176 *  If an error occurs, 0 is returned and \b errno is set accordingly:
177 *  - \c EINVAL At least one of the colour values is invalid.
178 *
179 *  \param cv A handle to the libcucul canvas.
180 *  \param fg The requested ANSI foreground colour.
181 *  \param bg The requested ANSI background colour.
182 *  \return 0 in case of success, -1 if an error occurred.
183 */
184int cucul_set_color_ansi(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
185{
186    uint32_t attr;
187
188    if(fg > 0x20 || bg > 0x20)
189    {
190        seterrno(EINVAL);
191        return -1;
192    }
193
194    attr = ((uint32_t)(bg | 0x40) << 18) | ((uint32_t)(fg | 0x40) << 4);
195    cv->curattr = (cv->curattr & 0x0000000f) | attr;
196
197    return 0;
198}
199
200/** \brief Set the default colour pair for text (truecolor version).
201 *
202 *  Set the default ARGB colour pair for text drawing. String functions such
203 *  as caca_printf() and graphical primitive functions such as caca_draw_line()
204 *  will use these attributes.
205 *
206 *  Colors are 16-bit ARGB values, each component being coded on 4 bits. For
207 *  instance, 0xf088 is solid dark cyan (A=15 R=0 G=8 B=8), and 0x8fff is
208 *  white with 50% alpha (A=8 R=15 G=15 B=15).
209 *
210 *  If an error occurs, 0 is returned and \b errno is set accordingly:
211 *  - \c EINVAL At least one of the colour values is invalid.
212 *
213 *  \param cv A handle to the libcucul canvas.
214 *  \param fg The requested ARGB foreground colour.
215 *  \param bg The requested ARGB background colour.
216 *  \return 0 in case of success, -1 if an error occurred.
217 */
218int cucul_set_color_argb(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
219{
220    uint32_t attr;
221
222    if(fg > 0xffff || bg > 0xffff)
223    {
224        seterrno(EINVAL);
225        return -1;
226    }
227
228    if(fg < 0x100)
229        fg += 0x100;
230
231    if(bg < 0x100)
232        bg += 0x100;
233
234    fg = ((fg >> 1) & 0x7ff) | ((fg >> 13) << 11);
235    bg = ((bg >> 1) & 0x7ff) | ((bg >> 13) << 11);
236
237    attr = ((uint32_t)bg << 18) | ((uint32_t)fg << 4);
238    cv->curattr = (cv->curattr & 0x0000000f) | attr;
239
240    return 0;
241}
242
243/** \brief Get DOS ANSI information from attribute.
244 *
245 *  Get the ANSI colour pair for a given attribute. The returned value is
246 *  an 8-bit value whose higher 4 bits are the background colour and lower
247 *  4 bits are the foreground colour.
248 *
249 *  If the attribute has ARGB colours, the nearest colour is used. Special
250 *  attributes such as \e CUCUL_DEFAULT and \e CUCUL_TRANSPARENT are not
251 *  handled and are both replaced with \e CUCUL_LIGHTGRAY for the foreground
252 *  colour and \e CUCUL_BLACK for the background colour.
253 *
254 *  This function never fails. If the attribute value is outside the expected
255 *  32-bit range, higher order bits are simply ignored.
256 *
257 *  \param attr The requested attribute value.
258 *  \return The corresponding DOS ANSI value.
259 */
260unsigned char cucul_attr_to_ansi(unsigned long int attr)
261{
262    uint8_t fg = nearest_ansi((attr >> 4) & 0x3fff);
263    uint8_t bg = nearest_ansi(attr >> 18);
264
265    return (fg < 0x10 ? fg : CUCUL_LIGHTGRAY)
266            | ((bg < 0x10 ? bg : CUCUL_BLACK) << 4);
267}
268
269/** \brief Get ANSI foreground information from attribute.
270 *
271 *  Get the ANSI foreground colour value for a given attribute. The returned
272 *  value is either one of the \e CUCUL_RED, \e CUCUL_BLACK etc. predefined
273 *  colours, or the special value \e CUCUL_DEFAULT meaning the media's
274 *  default foreground value, or the special value \e CUCUL_TRANSPARENT.
275 *
276 *  If the attribute has ARGB colours, the nearest colour is returned.
277 *
278 *  This function never fails. If the attribute value is outside the expected
279 *  32-bit range, higher order bits are simply ignored.
280 *
281 *  \param attr The requested attribute value.
282 *  \return The corresponding ANSI foreground value.
283 */
284unsigned char cucul_attr_to_ansi_fg(unsigned long int attr)
285{
286    return nearest_ansi(((uint16_t)attr >> 4) & 0x3fff);
287}
288
289/** \brief Get ANSI background information from attribute.
290 *
291 *  Get the ANSI background colour value for a given attribute. The returned
292 *  value is either one of the \e CUCUL_RED, \e CUCUL_BLACK etc. predefined
293 *  colours, or the special value \e CUCUL_DEFAULT meaning the media's
294 *  default background value, or the special value \e CUCUL_TRANSPARENT.
295 *
296 *  If the attribute has ARGB colours, the nearest colour is returned.
297 *
298 *  This function never fails. If the attribute value is outside the expected
299 *  32-bit range, higher order bits are simply ignored.
300 *
301 *  \param attr The requested attribute value.
302 *  \return The corresponding ANSI background value.
303 */
304unsigned char cucul_attr_to_ansi_bg(unsigned long int attr)
305{
306    return nearest_ansi(attr >> 18);
307}
308
309/** \brief Get 12-bit RGB foreground information from attribute.
310 *
311 *  Get the 12-bit foreground colour value for a given attribute. The returned
312 *  value is a native-endian encoded integer with each red, green and blue
313 *  values encoded on 8 bits in the following order:
314 *   - 8-11 most significant bits: red
315 *   - 4-7 most significant bits: green
316 *   - least significant bits: blue
317 *
318 *  This function never fails. If the attribute value is outside the expected
319 *  32-bit range, higher order bits are simply ignored.
320 *
321 *  \param attr The requested attribute value.
322 *  \return The corresponding 12-bit RGB foreground value.
323 */
324unsigned int cucul_attr_to_rgb12_fg(unsigned long int attr)
325{
326    uint16_t fg = (attr >> 4) & 0x3fff;
327
328    if(fg < (0x10 | 0x40))
329        return ansitab16[fg ^ 0x40] & 0x0fff;
330
331    if(fg == (CUCUL_DEFAULT | 0x40))
332        return ansitab16[CUCUL_LIGHTGRAY] & 0x0fff;
333
334    if(fg == (CUCUL_TRANSPARENT | 0x40))
335        return ansitab16[CUCUL_LIGHTGRAY] & 0x0fff;
336
337    return (fg << 1) & 0x0fff;
338}
339
340/** \brief Get 12-bit RGB background information from attribute.
341 *
342 *  Get the 12-bit background colour value for a given attribute. The returned
343 *  value is a native-endian encoded integer with each red, green and blue
344 *  values encoded on 8 bits in the following order:
345 *   - 8-11 most significant bits: red
346 *   - 4-7 most significant bits: green
347 *   - least significant bits: blue
348 *
349 *  This function never fails. If the attribute value is outside the expected
350 *  32-bit range, higher order bits are simply ignored.
351 *
352 *  \param attr The requested attribute value.
353 *  \return The corresponding 12-bit RGB background value.
354 */
355unsigned int cucul_attr_to_rgb12_bg(unsigned long int attr)
356{
357    uint16_t bg = attr >> 18;
358
359    if(bg < (0x10 | 0x40))
360        return ansitab16[bg ^ 0x40] & 0x0fff;
361
362    if(bg == (CUCUL_DEFAULT | 0x40))
363        return ansitab16[CUCUL_BLACK] & 0x0fff;
364
365    if(bg == (CUCUL_TRANSPARENT | 0x40))
366        return ansitab16[CUCUL_BLACK] & 0x0fff;
367
368    return (bg << 1) & 0x0fff;
369}
370
371/** \brief Get 64-bit ARGB information from attribute.
372 *
373 *  Get the 64-bit colour and alpha values for a given attribute. The values
374 *  are written as 8-bit integers in the \e argb array in the following order:
375 *   - \e argb[0]: background alpha value
376 *   - \e argb[1]: background red value
377 *   - \e argb[2]: background green value
378 *   - \e argb[3]: background blue value
379 *   - \e argb[4]: foreground alpha value
380 *   - \e argb[5]: foreground red value
381 *   - \e argb[6]: foreground green value
382 *   - \e argb[7]: foreground blue value
383 *
384 *  This function never fails. If the attribute value is outside the expected
385 *  32-bit range, higher order bits are simply ignored.
386 *
387 *  \param attr The requested attribute value.
388 *  \param argb An array of 8-bit integers.
389 */
390void cucul_attr_to_argb64(unsigned long int attr, unsigned char argb[8])
391{
392    uint16_t fg = (attr >> 4) & 0x3fff;
393    uint16_t bg = attr >> 18;
394
395    if(bg < (0x10 | 0x40))
396        bg = ansitab16[bg ^ 0x40];
397    else if(bg == (CUCUL_DEFAULT | 0x40))
398        bg = ansitab16[CUCUL_BLACK];
399    else if(bg == (CUCUL_TRANSPARENT | 0x40))
400        bg = 0x0fff;
401    else
402        bg = ((bg << 2) & 0xf000) | ((bg << 1) & 0x0fff);
403
404    argb[0] = bg >> 12;
405    argb[1] = (bg >> 8) & 0xf;
406    argb[2] = (bg >> 4) & 0xf;
407    argb[3] = bg & 0xf;
408
409    if(fg < (0x10 | 0x40))
410        fg = ansitab16[fg ^ 0x40];
411    else if(fg == (CUCUL_DEFAULT | 0x40))
412        fg = ansitab16[CUCUL_LIGHTGRAY];
413    else if(fg == (CUCUL_TRANSPARENT | 0x40))
414        fg = 0x0fff;
415    else
416        fg = ((fg << 2) & 0xf000) | ((fg << 1) & 0x0fff);
417
418    argb[4] = fg >> 12;
419    argb[5] = (fg >> 8) & 0xf;
420    argb[6] = (fg >> 4) & 0xf;
421    argb[7] = fg & 0xf;
422}
423
424/*
425 * XXX: the following functions are local
426 */
427
428static uint8_t nearest_ansi(uint16_t argb14)
429{
430    unsigned int i, best, dist;
431
432    if(argb14 < (0x10 | 0x40))
433        return argb14 ^ 0x40;
434
435    if(argb14 == (CUCUL_DEFAULT | 0x40) || argb14 == (CUCUL_TRANSPARENT | 0x40))
436        return argb14 ^ 0x40;
437
438    if(argb14 < 0x0fff) /* too transparent */
439        return CUCUL_TRANSPARENT;
440
441    best = CUCUL_DEFAULT;
442    dist = 0x3fff;
443    for(i = 0; i < 16; i++)
444    {
445        unsigned int d = 0;
446        int a, b;
447
448        a = (ansitab14[i] >> 7) & 0xf;
449        b = (argb14 >> 7) & 0xf;
450        d += (a - b) * (a - b);
451
452        a = (ansitab14[i] >> 3) & 0xf;
453        b = (argb14 >> 3) & 0xf;
454        d += (a - b) * (a - b);
455
456        a = (ansitab14[i] << 1) & 0xf;
457        b = (argb14 << 1) & 0xf;
458        d += (a - b) * (a - b);
459
460        if(d < dist)
461        {
462            dist = d;
463            best = i;
464        }
465    }
466
467    return best;
468}
469
470#define RGB12TO24(i) \
471   (((uint32_t)((i & 0xf00) >> 8) * 0x110000) \
472  | ((uint32_t)((i & 0x0f0) >> 4) * 0x001100) \
473  | ((uint32_t)(i & 0x00f) * 0x000011))
474
475uint32_t _cucul_attr_to_rgb24fg(uint32_t attr)
476{
477    return RGB12TO24(cucul_attr_to_rgb12_fg(attr));
478}
479
480uint32_t _cucul_attr_to_rgb24bg(uint32_t attr)
481{
482    return RGB12TO24(cucul_attr_to_rgb12_bg(attr));
483}
484
Note: See TracBrowser for help on using the repository browser.