source: libcaca/trunk/cucul/transform.c @ 1462

Last change on this file since 1462 was 1462, checked in by Sam Hocevar, 17 years ago
  • Bwarf, typo in the no warranty clause.
  • Property svn:keywords set to Id
File size: 13.2 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: transform.c 1462 2006-12-12 01:53:54Z 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 horizontal and vertical flipping routines.
17 */
18
19#include "config.h"
20#include "common.h"
21
22#if !defined(__KERNEL__)
23#endif
24
25#include "cucul.h"
26#include "cucul_internals.h"
27
28static uint32_t flipchar(uint32_t ch);
29static uint32_t flopchar(uint32_t ch);
30static uint32_t rotatechar(uint32_t ch);
31
32/** \brief Invert a canvas' colours.
33 *
34 *  Invert a canvas' colours (black becomes white, red becomes cyan, etc.)
35 *  without changing the characters in it.
36 *
37 *  This function never fails.
38 *
39 *  \param cv The canvas to invert.
40 *  \return This function always returns 0.
41 */
42int cucul_invert(cucul_canvas_t *cv)
43{
44    uint32_t *attrs = cv->attrs;
45    unsigned int i;
46
47    for(i = cv->height * cv->width; i--; )
48    {
49        *attrs = *attrs ^ 0x000f000f;
50        attrs++;
51    }
52
53    return 0;
54}
55
56/** \brief Flip a canvas horizontally.
57 *
58 *  Flip a canvas horizontally, choosing characters that look like the
59 *  mirrored version wherever possible. Some characters will stay
60 *  unchanged by the process, but the operation is guaranteed to be
61 *  involutive: performing it again gives back the original canvas.
62 *
63 *  This function never fails.
64 *
65 *  \param cv The canvas to flip.
66 *  \return This function always returns 0.
67 */
68int cucul_flip(cucul_canvas_t *cv)
69{
70    unsigned int y;
71
72    for(y = 0; y < cv->height; y++)
73    {
74        uint32_t *cleft = cv->chars + y * cv->width;
75        uint32_t *cright = cleft + cv->width - 1;
76        uint32_t *aleft = cv->attrs + y * cv->width;
77        uint32_t *aright = aleft + cv->width - 1;
78
79        while(cleft < cright)
80        {
81            uint32_t ch;
82            uint32_t attr;
83
84            /* Swap attributes */
85            attr = *aright;
86            *aright-- = *aleft;
87            *aleft++ = attr;
88
89            /* Swap characters */
90            ch = *cright;
91            *cright-- = flipchar(*cleft);
92            *cleft++ = flipchar(ch);
93        }
94
95        if(cleft == cright)
96            *cleft = flipchar(*cleft);
97
98        /* Fix fullwidth characters. Could it be done in one loop? */
99        cleft = cv->chars + y * cv->width;
100        cright = cleft + cv->width - 1;
101        for( ; cleft < cright; cleft++)
102        {
103            if(cleft[0] == CUCUL_MAGIC_FULLWIDTH)
104            {
105                cleft[0] = cleft[1];
106                cleft[1] = CUCUL_MAGIC_FULLWIDTH;
107                cleft++;
108            }
109        }
110    }
111
112    return 0;
113}
114
115/** \brief Flip a canvas vertically.
116 *
117 *  Flip a canvas vertically, choosing characters that look like the
118 *  mirrored version wherever possible. Some characters will stay
119 *  unchanged by the process, but the operation is guaranteed to be
120 *  involutive: performing it again gives back the original canvas.
121 *
122 *  This function never fails.
123 *
124 *  \param cv The canvas to flop.
125 *  \return This function always returns 0.
126 */
127int cucul_flop(cucul_canvas_t *cv)
128{
129    unsigned int x;
130
131    for(x = 0; x < cv->width; x++)
132    {
133        uint32_t *ctop = cv->chars + x;
134        uint32_t *cbottom = ctop + cv->width * (cv->height - 1);
135        uint32_t *atop = cv->attrs + x;
136        uint32_t *abottom = atop + cv->width * (cv->height - 1);
137
138        while(ctop < cbottom)
139        {
140            uint32_t ch;
141            uint32_t attr;
142
143            /* Swap attributes */
144            attr = *abottom; *abottom = *atop; *atop = attr;
145
146            /* Swap characters */
147            ch = *cbottom; *cbottom = flopchar(*ctop); *ctop = flopchar(ch);
148
149            ctop += cv->width; cbottom -= cv->width;
150            atop += cv->width; abottom -= cv->width;
151        }
152
153        if(ctop == cbottom)
154            *ctop = flopchar(*ctop);
155    }
156
157    return 0;
158}
159
160/** \brief Rotate a canvas.
161 *
162 *  Apply a 180-degree transformation to a canvas, choosing characters
163 *  that look like the upside-down version wherever possible. Some
164 *  characters will stay unchanged by the process, but the operation is
165 *  guaranteed to be involutive: performing it again gives back the
166 *  original canvas.
167 *
168 *  This function never fails.
169 *
170 *  \param cv The canvas to rotate.
171 *  \return This function always returns 0.
172 */
173int cucul_rotate(cucul_canvas_t *cv)
174{
175    uint32_t *cbegin = cv->chars;
176    uint32_t *cend = cbegin + cv->width * cv->height - 1;
177    uint32_t *abegin = cv->attrs;
178    uint32_t *aend = abegin + cv->width * cv->height - 1;
179    unsigned int y;
180
181    while(cbegin < cend)
182    {
183        uint32_t ch;
184        uint32_t attr;
185
186        /* Swap attributes */
187        attr = *aend; *aend = *abegin; *abegin = attr;
188
189        /* Swap characters */
190        ch = *cend; *cend = rotatechar(*cbegin); *cbegin = rotatechar(ch);
191
192        cbegin++; cend--; abegin++; aend--;
193    }
194
195    if(cbegin == cend)
196        *cbegin = rotatechar(*cbegin);
197
198    /* Fix fullwidth characters. Could it be done in one loop? */
199    for(y = 0; y < cv->height; y++)
200    {
201        cbegin = cv->chars + y * cv->width;
202        cend = cbegin + cv->width - 1;
203        for( ; cbegin < cend; cbegin++)
204        {
205            if(cbegin[0] == CUCUL_MAGIC_FULLWIDTH)
206            {
207                cbegin[0] = cbegin[1];
208                cbegin[1] = CUCUL_MAGIC_FULLWIDTH;
209                cbegin++;
210            }
211        }
212    }
213
214    return 0;
215}
216
217/* FIXME: as the lookup tables grow bigger, use a log(n) lookup instead
218 * of linear lookup. */
219static uint32_t flipchar(uint32_t ch)
220{
221    int i;
222
223    static uint32_t const noflip[] =
224    {
225         /* ASCII */
226         ' ', '"', '#', '\'', '-', '.', '*', '+', ':', '=', '0', '8',
227         'A', 'H', 'I', 'M', 'O', 'T', 'U', 'V', 'W', 'X', 'Y', '^',
228         '_', 'i', 'o', 'v', 'w', 'x', '|',
229         /* CP437 and box drawing */
230         0x2591, 0x2592, 0x2593, 0x2588, 0x2584, 0x2580, /* ░ ▒ ▓ █ ▄ ▀ */
231         0x2500, 0x2501, 0x2503, 0x2503, 0x253c, 0x254b, /* ─ ━ │ ┃ ┼ ╋ */
232         0x252c, 0x2534, 0x2533, 0x253b, 0x2566, 0x2569, /* ┬ ┴ ┳ ┻ ╦ ╩ */
233         0x2550, 0x2551, 0x256c, /* ═ ║ ╬ */
234         0x2575, 0x2577, 0x2579, 0x257b, /* ╵ ╷ ╹ ╻ */
235         0
236    };
237
238    static uint32_t const pairs[] =
239    {
240         /* ASCII */
241         '(', ')',
242         '/', '\\',
243         '<', '>',
244         '[', ']',
245         'b', 'd',
246         'p', 'q',
247         '{', '}',
248         /* ASCII-Unicode */
249         ';', 0x204f, /* ; ⁏ */
250         '`', 0x00b4, /* ` ´ */
251         ',', 0x02ce, /* , ˎ */
252         'C', 0x03fd, /* C Ͻ */
253         'E', 0x018e, /* E Ǝ */
254         'N', 0x0418, /* N И */
255         'R', 0x042f, /* R Я */
256         'S', 0x01a7, /* S Ƨ */
257         'c', 0x0254, /* c ɔ */
258         'e', 0x0258, /* e ɘ */
259         /* CP437 */
260         0x258c, 0x2590, /* ▌ ▐ */
261         0x2596, 0x2597, /* ▖ ▗ */
262         0x2598, 0x259d, /* ▘ ▝ */
263         0x2599, 0x259f, /* ▙ ▟ */
264         0x259a, 0x259e, /* ▚ ▞ */
265         0x259b, 0x259c, /* ▛ ▜ */
266         0x25ba, 0x25c4, /* ► ◄ */
267         0x2192, 0x2190, /* → ← */
268         0x2310, 0xac,   /* ⌐ ¬ */
269         /* Box drawing */
270         0x250c, 0x2510, /* ┌ ┐ */
271         0x2514, 0x2518, /* └ ┘ */
272         0x251c, 0x2524, /* ├ ┤ */
273         0x250f, 0x2513, /* ┏ ┓ */
274         0x2517, 0x251b, /* ┗ ┛ */
275         0x2523, 0x252b, /* ┣ ┫ */
276         0x2554, 0x2557, /* ╔ ╗ */
277         0x255a, 0x255d, /* ╚ ╝ */
278         0x2560, 0x2563, /* ╠ ╣ */
279         0x2574, 0x2576, /* ╴ ╶ */
280         0x2578, 0x257a, /* ╸ ╺ */
281         0
282    };
283
284    for(i = 0; noflip[i]; i++)
285        if(ch == noflip[i])
286            return ch;
287
288    for(i = 0; pairs[i]; i++)
289        if(ch == pairs[i])
290            return pairs[i ^ 1];
291
292    return ch;
293}
294
295static uint32_t flopchar(uint32_t ch)
296{
297    int i;
298
299    static uint32_t const noflop[] =
300    {
301         /* ASCII */
302         ' ', '(', ')', '*', '+', '-', '0', '3', '8', ':', '<', '=',
303         '>', 'B', 'C', 'D', 'E', 'H', 'I', 'K', 'O', 'X', '[', ']',
304         'c', 'o', '{', '|', '}',
305         /* CP437 and box drawing */
306         0x2591, 0x2592, 0x2593, 0x2588, 0x258c, 0x2590, /* ░ ▒ ▓ █ ▌ ▐ */
307         0x2500, 0x2501, 0x2503, 0x2503, 0x253c, 0x254b, /* ─ ━ │ ┃ ┼ ╋ */
308         0x251c, 0x2524, 0x2523, 0x252b, 0x2560, 0x2563, /* ├ ┤ ┣ ┫ ╠ ╣ */
309         0x2550, 0x2551, 0x256c, /* ═ ║ ╬ */
310         0x2574, 0x2576, 0x2578, 0x257a, /* ╴ ╶ ╸ ╺ */
311         0
312    };
313
314    static uint32_t const pairs[] =
315    {
316         /* ASCII */
317         '/', '\\',
318         'M', 'W',
319         ',', '`',
320         'b', 'p',
321         'd', 'q',
322         'p', 'q',
323         'f', 't',
324         '.', '\'',
325         /* ASCII-Unicode */
326         '_', 0x203e, /* _ ‾ */
327         '!', 0x00a1, /* ! ¡ */
328         'L', 0x0413, /* L Г */
329         'N', 0x0418, /* N И */
330         'P', 0x042c, /* P Ь */
331         'R', 0x0281, /* R ʁ */
332         'S', 0x01a7, /* S Ƨ */
333         'U', 0x0548, /* U Ո */
334         'V', 0x039b, /* V Λ */
335         'h', 0x03bc, /* h μ */
336         'i', 0x1d09, /* i ᴉ */
337         'v', 0x028c, /* v ʌ */
338         'w', 0x028d, /* w ʍ */
339         'y', 0x03bb, /* y λ */
340         /* Not perfect, but better than nothing */
341         '"', 0x201e, /* " „ */
342         'm', 0x026f, /* m ɯ */
343         'n', 'u',
344         /* CP437 */
345         0x2584, 0x2580, /* ▄ ▀ */
346         0x2596, 0x2598, /* ▖ ▘ */
347         0x2597, 0x259d, /* ▗ ▝ */
348         0x2599, 0x259b, /* ▙ ▛ */
349         0x259f, 0x259c, /* ▟ ▜ */
350         0x259a, 0x259e, /* ▚ ▞ */
351         /* Box drawing */
352         0x250c, 0x2514, /* ┌ └ */
353         0x2510, 0x2518, /* ┐ ┘ */
354         0x252c, 0x2534, /* ┬ ┴ */
355         0x250f, 0x2517, /* ┏ ┗ */
356         0x2513, 0x251b, /* ┓ ┛ */
357         0x2533, 0x253b, /* ┳ ┻ */
358         0x2554, 0x255a, /* ╔ ╚ */
359         0x2557, 0x255d, /* ╗ ╝ */
360         0x2566, 0x2569, /* ╦ ╩ */
361         0x2575, 0x2577, /* ╵ ╷ */
362         0x2579, 0x257b, /* ╹ ╻ */
363         0
364    };
365
366    for(i = 0; noflop[i]; i++)
367        if(ch == noflop[i])
368            return ch;
369
370    for(i = 0; pairs[i]; i++)
371        if(ch == pairs[i])
372            return pairs[i ^ 1];
373
374    return ch;
375}
376
377static uint32_t rotatechar(uint32_t ch)
378{
379    int i;
380
381    static uint32_t const norotate[] =
382    {
383         /* ASCII */
384         ' ', '*', '+', '-', '/', '0', '8', ':', '=', 'H', 'I', 'N',
385         'O', 'S', 'X', 'Z', '\\', 'l', 'o', 's', 'x', 'z', '|',
386         /* Unicode */
387         0x2591, 0x2592, 0x2593, 0x2588, 0x259a, 0x259e, /* ░ ▒ ▓ █ ▚ ▞ */
388         0x2500, 0x2501, 0x2503, 0x2503, 0x253c, 0x254b, /* ─ ━ │ ┃ ┼ ╋ */
389         0x2550, 0x2551, 0x256c, /* ═ ║ ╬ */
390         0
391    };
392
393    static uint32_t const pairs[] =
394    {
395         /* ASCII */
396         '(', ')',
397         '<', '>',
398         '[', ']',
399         '{', '}',
400         '.', '\'',
401         '6', '9',
402         'M', 'W',
403         'b', 'q',
404         'd', 'p',
405         'n', 'u',
406         /* ASCII-Unicode */
407         '_', 0x203e, /* _ ‾ */
408         ',', 0x00b4, /* , ´ */
409         '`', 0x02ce, /* ` ˎ */
410         '&', 0x214b, /* & ⅋ */
411         '!', 0x00a1, /* ! ¡ */
412         '?', 0x00bf, /* ? ¿ */
413         'C', 0x03fd, /* C Ͻ */
414         'E', 0x018e, /* E Ǝ */
415         'F', 0x2132, /* F Ⅎ */
416         'U', 0x0548, /* U Ո */
417         'V', 0x039b, /* V Λ */
418         'a', 0x0250, /* a ɐ */
419         'c', 0x0254, /* c ɔ */
420         'e', 0x0259, /* e ə */
421         'f', 0x025f, /* f ɟ */
422         'g', 0x1d77, /* g ᵷ */
423         'h', 0x0265, /* h ɥ */
424         'i', 0x1d09, /* i ᴉ */
425         'k', 0x029e, /* k ʞ */
426         'm', 0x026f, /* m ɯ */
427         'r', 0x0279, /* r ɹ */
428         't', 0x0287, /* t ʇ */
429         'v', 0x028c, /* v ʌ */
430         'w', 0x028d, /* w ʍ */
431         'y', 0x028e, /* y ʎ */
432         /* Not perfect, but better than nothing */
433         '"', 0x201e, /* " „ */
434         /* CP437 */
435         0x258c, 0x2590, /* ▌ ▐ */
436         0x2584, 0x2580, /* ▄ ▀ */
437         0x2596, 0x259d, /* ▖ ▝ */
438         0x2597, 0x2598, /* ▗ ▘ */
439         0x2599, 0x259c, /* ▙ ▜ */
440         0x259f, 0x259b, /* ▟ ▛ */
441         /* Box drawing */
442         0x250c, 0x2518, /* ┌ ┘ */
443         0x2510, 0x2514, /* ┐ └ */
444         0x251c, 0x2524, /* ├ ┤ */
445         0x252c, 0x2534, /* ┬ ┴ */
446         0x250f, 0x251b, /* ┏ ┛ */
447         0x2513, 0x2517, /* ┓ ┗ */
448         0x2523, 0x252b, /* ┣ ┫ */
449         0x2533, 0x253b, /* ┳ ┻ */
450         0x2554, 0x255d, /* ╔ ╝ */
451         0x2557, 0x255a, /* ╗ ╚ */
452         0x2560, 0x2563, /* ╠ ╣ */
453         0x2566, 0x2569, /* ╦ ╩ */
454         0x2574, 0x2576, /* ╴ ╶ */
455         0x2575, 0x2577, /* ╵ ╷ */
456         0x2578, 0x257a, /* ╸ ╺ */
457         0x2579, 0x257b, /* ╹ ╻ */
458         0
459    };
460
461    for(i = 0; norotate[i]; i++)
462        if(ch == norotate[i])
463            return ch;
464
465    for(i = 0; pairs[i]; i++)
466        if(ch == pairs[i])
467            return pairs[i ^ 1];
468
469    return ch;
470}
471
Note: See TracBrowser for help on using the repository browser.