source: libcaca/trunk/cucul/export.c @ 825

Last change on this file since 825 was 825, checked in by Sam Hocevar, 14 years ago
  • Minor documentation improvements.
  • Property svn:keywords set to Id
File size: 21.2 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                All Rights Reserved
6 *
7 *  $Id: export.c 825 2006-04-21 18:09:30Z sam $
8 *
9 *  This library is free software; you can redistribute it and/or
10 *  modify it under the terms of the Do What The Fuck You Want To
11 *  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 various export functions
17 */
18
19#include "config.h"
20
21#if !defined(__KERNEL__)
22#   include <stdlib.h>
23#   include <stdio.h>
24#   include <string.h>
25#endif
26
27#include "cucul.h"
28#include "cucul_internals.h"
29
30static void export_caca(cucul_canvas_t *, cucul_buffer_t *);
31static void export_ansi(cucul_canvas_t *, cucul_buffer_t *);
32static void export_html(cucul_canvas_t *, cucul_buffer_t *);
33static void export_html3(cucul_canvas_t *, cucul_buffer_t *);
34static void export_irc(cucul_canvas_t *, cucul_buffer_t *);
35static void export_ps(cucul_canvas_t *, cucul_buffer_t *);
36static void export_svg(cucul_canvas_t *, cucul_buffer_t *);
37static void export_tga(cucul_canvas_t *, cucul_buffer_t *);
38
39/** \brief Export a canvas into a foreign format.
40 *
41 *  This function exports a libcucul canvas into various foreign formats such
42 *  as ANSI art, HTML, IRC colours, etc. One should use cucul_get_buffer_data()
43 *  and cucul_get_buffer_size() to access the buffer contents. The allocated
44 *  data is valid until cucul_free_buffer() is called.
45 *
46 *  Valid values for \c format are:
47 *
48 *  \li \c "caca": export native libcaca files.
49 *
50 *  \li \c "ansi": export ANSI art (CP437 charset with ANSI colour codes).
51 *
52 *  \li \c "html": export an HTML page with CSS information.
53 *
54 *  \li \c "html3": export an HTML table that should be compatible with
55 *      most navigators, including textmode ones.
56 *
57 *  \li \c "irc": export UTF-8 text with mIRC colour codes.
58 *
59 *  \li \c "ps": export a PostScript document.
60 *
61 *  \li \c "svg": export an SVG vector image.
62 *
63 *  \li \c "tga": export a TGA image.
64 *
65 *  \param cv A libcucul canvas
66 *  \param format A string describing the requested output format.
67 */
68cucul_buffer_t * cucul_create_export(cucul_canvas_t *cv, char const *format)
69{
70    cucul_buffer_t *ex;
71
72    ex = malloc(sizeof(cucul_buffer_t));
73    ex->size = 0;
74    ex->data = NULL;
75
76    if(!strcasecmp("caca", format))
77        export_caca(cv, ex);
78    else if(!strcasecmp("ansi", format))
79        export_ansi(cv, ex);
80    else if(!strcasecmp("html", format))
81        export_html(cv, ex);
82    else if(!strcasecmp("html3", format))
83        export_html3(cv, ex);
84    else if(!strcasecmp("irc", format))
85        export_irc(cv, ex);
86    else if(!strcasecmp("ps", format))
87        export_ps(cv, ex);
88    else if(!strcasecmp("svg", format))
89        export_svg(cv, ex);
90    else if(!strcasecmp("tga", format))
91        export_tga(cv, ex);
92
93    if(ex->size == 0)
94    {
95        free(ex);
96        return NULL;
97    }
98
99    return ex;
100}
101
102/** \brief Get available export formats
103 *
104 *  Return a list of available export formats. The list is a NULL-terminated
105 *  array of strings, interleaving a string containing the internal value for
106 *  the export format, to be used with cucul_create_export(), and a string
107 *  containing the natural language description for that export format.
108 *
109 *  \return An array of strings.
110 */
111char const * const * cucul_get_export_list(void)
112{
113    static char const * const list[] =
114    {
115        "caca", "native libcaca format",
116        "ansi", "ANSI",
117        "html", "HTML",
118        "html3", "backwards-compatible HTML",
119        "irc", "IRC with mIRC colours",
120        "ps", "PostScript document",
121        "svg", "SVG vector image",
122        "tga", "TGA image",
123        NULL, NULL
124    };
125
126    return list;
127}
128
129/*
130 * XXX: the following functions are local.
131 */
132
133/* Generate a native libcaca canvas file. */
134static void export_caca(cucul_canvas_t *cv, cucul_buffer_t *ex)
135{
136    uint32_t *attr = cv->attr;
137    uint32_t *chars = cv->chars;
138    char *cur;
139    uint32_t w, h;
140    unsigned int n;
141
142    /* 16 bytes for the canvas, 8 bytes for each character cell. */
143    ex->size = 16 + 8 * cv->width * cv->height;
144    ex->data = malloc(ex->size);
145
146    cur = ex->data;
147
148    w = cv->width;
149    h = cv->height;
150
151    cur += sprintf(cur, "CACACANV%c%c%c%c%c%c%c%c",
152                   (w >> 24), (w >> 16) & 0xff, (w >> 8) & 0xff, w & 0xff,
153                   (h >> 24), (h >> 16) & 0xff, (h >> 8) & 0xff, h & 0xff);
154
155    for(n = cv->height * cv->width; n--; )
156    {
157        uint32_t ch = *chars++;
158        uint32_t a = *attr++;
159
160        *cur++ = ch >> 24;
161        *cur++ = (ch >> 16) & 0xff;
162        *cur++ = (ch >> 8) & 0xff;
163        *cur++ = ch & 0xff;
164
165        *cur++ = a >> 24;
166        *cur++ = (a >> 16) & 0xff;
167        *cur++ = (a >> 8) & 0xff;
168        *cur++ = a & 0xff;
169    }
170}
171
172/* Generate ANSI representation of current canvas. */
173static void export_ansi(cucul_canvas_t *cv, cucul_buffer_t *ex)
174{
175    static uint8_t const palette[] =
176    {
177        0,  4,  2,  6, 1,  5,  3,  7,
178        8, 12, 10, 14, 9, 13, 11, 15
179    };
180
181    char *cur;
182    unsigned int x, y;
183
184    /* 23 bytes assumed for max length per pixel ('\e[5;1;3x;4y;9x;10ym' plus
185     * 4 max bytes for a UTF-8 character).
186     * Add height*9 to that (zeroes color at the end and jump to next line) */
187    ex->size = (cv->height * 9) + (cv->width * cv->height * 23);
188    ex->data = malloc(ex->size);
189
190    cur = ex->data;
191
192    for(y = 0; y < cv->height; y++)
193    {
194        uint32_t *lineattr = cv->attr + y * cv->width;
195        uint32_t *linechar = cv->chars + y * cv->width;
196
197        uint8_t prevfg = -1;
198        uint8_t prevbg = -1;
199
200        for(x = 0; x < cv->width; x++)
201        {
202            uint8_t fg = palette[_cucul_argb32_to_ansi4fg(lineattr[x])];
203            uint8_t bg = palette[_cucul_argb32_to_ansi4bg(lineattr[x])];
204            uint32_t ch = linechar[x];
205
206            if(fg != prevfg || bg != prevbg)
207            {
208                cur += sprintf(cur, "\033[0;");
209
210                if(fg < 8)
211                    if(bg < 8)
212                        cur += sprintf(cur, "3%d;4%dm", fg, bg);
213                    else
214                        cur += sprintf(cur, "5;3%d;4%d;10%dm",
215                                            fg, bg - 8, bg - 8);
216                else
217                    if(bg < 8)
218                        cur += sprintf(cur, "1;3%d;4%d;9%dm",
219                                            fg - 8, bg, fg - 8);
220                    else
221                        cur += sprintf(cur, "5;1;3%d;4%d;9%d;10%dm",
222                                            fg - 8, bg - 8, fg - 8, bg - 8);
223            }
224
225            *cur++ = ch & 0x7f;
226
227            prevfg = fg;
228            prevbg = bg;
229        }
230
231        cur += sprintf(cur, "\033[0m\r\n");
232    }
233
234    /* Crop to really used size */
235    ex->size = (uintptr_t)(cur - ex->data);
236    ex->data = realloc(ex->data, ex->size);
237}
238
239/* Generate HTML representation of current canvas. */
240static void export_html(cucul_canvas_t *cv, cucul_buffer_t *ex)
241{
242    char *cur;
243    unsigned int x, y, len;
244
245    /* The HTML header: less than 1000 bytes
246     * A line: 7 chars for "<br />\n"
247     * A glyph: 47 chars for "<span style="color:#xxx;background-color:#xxx">"
248     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
249     *          7 chars for "</span>" */
250    ex->size = 1000 + cv->height * (7 + cv->width * (47 + 9 + 7));
251    ex->data = malloc(ex->size);
252
253    cur = ex->data;
254
255    /* HTML header */
256    cur += sprintf(cur, "<html><head>\n");
257    cur += sprintf(cur, "<title>Generated by libcaca %s</title>\n", VERSION);
258    cur += sprintf(cur, "</head><body>\n");
259
260    cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
261                        "font-family: monospace, fixed; font-weight: bold;");
262
263    for(y = 0; y < cv->height; y++)
264    {
265        uint32_t *lineattr = cv->attr + y * cv->width;
266        uint32_t *linechar = cv->chars + y * cv->width;
267
268        for(x = 0; x < cv->width; x += len)
269        {
270            cur += sprintf(cur, "<span style=\"color:#%.03x;"
271                                "background-color:#%.03x\">",
272                                _cucul_argb32_to_rgb12fg(lineattr[x]),
273                                _cucul_argb32_to_rgb12bg(lineattr[x]));
274
275            for(len = 0;
276                x + len < cv->width && lineattr[x + len] == lineattr[x];
277                len++)
278            {
279                if(linechar[x + len] <= 0x00000020)
280                    cur += sprintf(cur, "&nbsp;");
281                else if(linechar[x + len] < 0x00000080)
282                    cur += sprintf(cur, "%c", linechar[x + len]);
283                else
284                    cur += sprintf(cur, "&#%i;", linechar[x + len]);
285            }
286            cur += sprintf(cur, "</span>");
287        }
288        /* New line */
289        cur += sprintf(cur, "<br />\n");
290    }
291
292    cur += sprintf(cur, "</div></body></html>\n");
293
294    /* Crop to really used size */
295    ex->size = strlen(ex->data) + 1;
296    ex->data = realloc(ex->data, ex->size);
297}
298
299/* Export an HTML3 document. This function is way bigger than export_html(),
300 * but permits viewing in old browsers (or limited ones such as links). It
301 * will not work under gecko (mozilla rendering engine) unless you set a
302 * correct header. */
303static void export_html3(cucul_canvas_t *cv, cucul_buffer_t *ex)
304{
305    char *cur;
306    unsigned int x, y, len;
307
308    /* The HTML table markup: less than 1000 bytes
309     * A line: 10 chars for "<tr></tr>\n"
310     * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
311     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
312     *          12 chars for "</font></td>" */
313    ex->size = 1000 + cv->height * (10 + cv->width * (40 + 9 + 12));
314    ex->data = malloc(ex->size);
315
316    cur = ex->data;
317
318    /* Table */
319    cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
320                        cv->height);
321
322    for(y = 0; y < cv->height; y++)
323    {
324        uint32_t *lineattr = cv->attr + y * cv->width;
325        uint32_t *linechar = cv->chars + y * cv->width;
326
327        cur += sprintf(cur, "<tr>");
328
329        for(x = 0; x < cv->width; x += len)
330        {
331            unsigned int i;
332
333            /* Use colspan option to factor cells with same attributes
334             * (see below) */
335            len = 1;
336            while(x + len < cv->width && lineattr[x + len] == lineattr[x])
337                len++;
338
339            cur += sprintf(cur, "<td bgcolor=#%.06x",
340                           _cucul_argb32_to_rgb24bg(lineattr[x]));
341
342            if(len > 1)
343                cur += sprintf(cur, " colspan=%d", len);
344
345            cur += sprintf(cur, "><font color=#%.06x>",
346                           _cucul_argb32_to_rgb24fg(lineattr[x]));
347
348            for(i = 0; i < len; i++)
349            {
350                if(linechar[x + i] <= 0x00000020)
351                    cur += sprintf(cur, "&nbsp;");
352                else if(linechar[x + i] < 0x00000080)
353                    cur += sprintf(cur, "%c", linechar[x + i]);
354                else
355                    cur += sprintf(cur, "&#%i;", linechar[x + i]);
356            }
357
358            cur += sprintf(cur, "</font></td>");
359        }
360        cur += sprintf(cur, "</tr>\n");
361    }
362
363    /* Footer */
364    cur += sprintf(cur, "</table>\n");
365
366    /* Crop to really used size */
367    ex->size = (uintptr_t)(cur - ex->data);
368    ex->data = realloc(ex->data, ex->size);
369}
370
371/* Export a text file with IRC colours */
372static void export_irc(cucul_canvas_t *cv, cucul_buffer_t *ex)
373{
374    static uint8_t const palette[] =
375    {
376        1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
377        14, 12, 9, 11, 4, 13, 8, 0, /* Light */
378    };
379
380    char *cur;
381    unsigned int x, y;
382
383    /* 11 bytes assumed for max length per pixel. Worst case scenario:
384     * ^Cxx,yy   6 bytes
385     * ^B^B      2 bytes
386     * ch        1 byte
387     * \r\n      2 bytes
388     * In real life, the average bytes per pixel value will be around 5.
389     */
390
391    ex->size = 2 + (cv->width * cv->height * 11);
392    ex->data = malloc(ex->size);
393
394    cur = ex->data;
395
396    for(y = 0; y < cv->height; y++)
397    {
398        uint32_t *lineattr = cv->attr + y * cv->width;
399        uint32_t *linechar = cv->chars + y * cv->width;
400
401        uint8_t prevfg = -1;
402        uint8_t prevbg = -1;
403
404        for(x = 0; x < cv->width; x++)
405        {
406            uint8_t fg = palette[_cucul_argb32_to_ansi4fg(lineattr[x])];
407            uint8_t bg = palette[_cucul_argb32_to_ansi4bg(lineattr[x])];
408            uint32_t ch = linechar[x];
409
410            if(bg == prevbg)
411            {
412                if(fg == prevfg)
413                    ; /* Same fg/bg, do nothing */
414                else if(ch == (uint32_t)' ')
415                    fg = prevfg; /* Hackety hack */
416                else
417                {
418                    cur += sprintf(cur, "\x03%d", fg);
419                    if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
420                        cur += sprintf(cur, "\x02\x02");
421                }
422            }
423            else
424            {
425                if(fg == prevfg)
426                    cur += sprintf(cur, "\x03,%d", bg);
427                else
428                    cur += sprintf(cur, "\x03%d,%d", fg, bg);
429
430                if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
431                    cur += sprintf(cur, "\x02\x02");
432            }
433            *cur++ = ch & 0x7f;
434            prevfg = fg;
435            prevbg = bg;
436        }
437        *cur++ = '\r';
438        *cur++ = '\n';
439    }
440
441    /* Crop to really used size */
442    ex->size = (uintptr_t)(cur - ex->data);
443    ex->data = realloc(ex->data, ex->size);
444}
445
446/* Export a PostScript document. */
447static void export_ps(cucul_canvas_t *cv, cucul_buffer_t *ex)
448{
449    static char const *ps_header =
450        "%!\n"
451        "%% libcaca PDF export\n"
452        "%%LanguageLevel: 2\n"
453        "%%Pages: 1\n"
454        "%%DocumentData: Clean7Bit\n"
455        "/csquare {\n"
456        "  newpath\n"
457        "  0 0 moveto\n"
458        "  0 1 rlineto\n"
459        "  1 0 rlineto\n"
460        "  0 -1 rlineto\n"
461        "  closepath\n"
462        "  setrgbcolor\n"
463        "  fill\n"
464        "} def\n"
465        "/S {\n"
466        "  Show\n"
467        "} bind def\n"
468        "/Courier-Bold findfont\n"
469        "8 scalefont\n"
470        "setfont\n"
471        "gsave\n"
472        "6 10 scale\n";
473
474    char *cur;
475    unsigned int x, y;
476
477    /* 200 is arbitrary but should be ok */
478    ex->size = strlen(ps_header) + (cv->width * cv->height * 200);
479    ex->data = malloc(ex->size);
480
481    cur = ex->data;
482
483    /* Header */
484    cur += sprintf(cur, "%s", ps_header);
485
486    /* Background, drawn using csquare macro defined in header */
487    for(y = cv->height; y--; )
488    {
489        uint32_t *lineattr = cv->attr + y * cv->width;
490
491        for(x = 0; x < cv->width; x++)
492        {
493            uint8_t argb[8];
494            _cucul_argb32_to_argb4(*lineattr++, argb);
495            cur += sprintf(cur, "1 0 translate\n %f %f %f csquare\n",
496                           (float)argb[1] * (1.0 / 0xf),
497                           (float)argb[2] * (1.0 / 0xf),
498                           (float)argb[3] * (1.0 / 0xf));
499        }
500
501        /* Return to beginning of the line, and jump to the next one */
502        cur += sprintf(cur, "-%d 1 translate\n", cv->width);
503    }
504
505    cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
506
507    for(y = cv->height; y--; )
508    {
509        uint32_t *lineattr = cv->attr + (cv->height - y - 1) * cv->width;
510        uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width;
511
512        for(x = 0; x < cv->width; x++)
513        {
514            uint8_t argb[8];
515            uint32_t ch = *linechar++;
516
517            _cucul_argb32_to_argb4(*lineattr++, argb);
518
519            cur += sprintf(cur, "newpath\n");
520            cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10 + 2);
521            cur += sprintf(cur, "%f %f %f setrgbcolor\n",
522                           (float)argb[5] * (1.0 / 0xf),
523                           (float)argb[6] * (1.0 / 0xf),
524                           (float)argb[7] * (1.0 / 0xf));
525
526            if(ch < 0x00000020)
527                cur += sprintf(cur, "(?) show\n");
528            else if(ch >= 0x00000080)
529                cur += sprintf(cur, "(?) show\n");
530            else switch((uint8_t)(ch & 0x7f))
531            {
532                case '\\':
533                case '(':
534                case ')':
535                    cur += sprintf(cur, "(\\%c) show\n", ch);
536                    break;
537                default:
538                    cur += sprintf(cur, "(%c) show\n", ch);
539                    break;
540            }
541        }
542    }
543
544    cur += sprintf(cur, "showpage\n");
545
546    /* Crop to really used size */
547    ex->size = (uintptr_t)(cur - ex->data);
548    ex->data = realloc(ex->data, ex->size);
549}
550
551/* Export an SVG vector image */
552static void export_svg(cucul_canvas_t *cv, cucul_buffer_t *ex)
553{
554    static char const svg_header[] =
555        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
556        "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
557        " xmlns=\"http://www.w3.org/2000/svg\""
558        " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
559        " xml:space=\"preserve\" version=\"1.1\"  baseProfile=\"full\">\n";
560
561    char *cur;
562    unsigned int x, y;
563
564    /* 200 is arbitrary but should be ok */
565    ex->size = strlen(svg_header) + (cv->width * cv->height * 200);
566    ex->data = malloc(ex->size);
567
568    cur = ex->data;
569
570    /* Header */
571    cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10,
572                                    cv->width * 6, cv->height * 10);
573
574    cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"12\">\n");
575
576    /* Background */
577    for(y = 0; y < cv->height; y++)
578    {
579        uint32_t *lineattr = cv->attr + y * cv->width;
580
581        for(x = 0; x < cv->width; x++)
582        {
583            cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\""
584                                " width=\"6\" height=\"10\"/>\n",
585                                _cucul_argb32_to_rgb12bg(*lineattr++),
586                                x * 6, y * 10);
587        }
588    }
589
590    /* Text */
591    for(y = 0; y < cv->height; y++)
592    {
593        uint32_t *lineattr = cv->attr + y * cv->width;
594        uint32_t *linechar = cv->chars + y * cv->width;
595
596        for(x = 0; x < cv->width; x++)
597        {
598            uint32_t ch = *linechar++;
599
600            cur += sprintf(cur, "<text style=\"fill:#%.03x\" "
601                                "x=\"%d\" y=\"%d\">",
602                                _cucul_argb32_to_rgb12fg(*lineattr++),
603                                x * 6, (y * 10) + 10);
604            if(ch < 0x00000020)
605                cur += sprintf(cur, "?");
606            else if(ch > 0x0000007f)
607            {
608                static const uint8_t mark[7] =
609                {
610                    0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
611                };
612
613                char buf[10], *parser;
614                int bytes = (ch < 0x800) ? 2 : (ch < 0x10000) ? 3 : 4;
615
616                buf[bytes] = '\0';
617                parser = buf + bytes;
618
619                switch(bytes)
620                {
621                    case 4: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
622                    case 3: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
623                    case 2: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
624                }
625                *--parser = ch | mark[bytes];
626
627                cur += sprintf(cur, "%s", buf);
628            }
629            else switch((uint8_t)ch)
630            {
631                case '>': cur += sprintf(cur, "&gt;"); break;
632                case '<': cur += sprintf(cur, "&lt;"); break;
633                case '&': cur += sprintf(cur, "&amp;"); break;
634                default: cur += sprintf(cur, "%c", ch); break;
635            }
636            cur += sprintf(cur, "</text>\n");
637        }
638    }
639
640    cur += sprintf(cur, " </g>\n");
641    cur += sprintf(cur, "</svg>\n");
642
643    /* Crop to really used size */
644    ex->size = (uintptr_t)(cur - ex->data);
645    ex->data = realloc(ex->data, ex->size);
646}
647
648/* Export a TGA image */
649static void export_tga(cucul_canvas_t *cv, cucul_buffer_t *ex)
650{
651    char const * const * fontlist;
652    char * cur;
653    cucul_font_t *f;
654    unsigned int i, w, h;
655
656    fontlist = cucul_get_font_list();
657    if(!fontlist[0])
658        return;
659
660    f = cucul_load_font(fontlist[0], 0);
661
662    w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
663    h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
664
665    ex->size = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
666    ex->data = malloc(ex->size);
667
668    cur = ex->data;
669
670    /* ID Length */
671    cur += sprintf(cur, "%c", 0);
672    /* Color Map Type: no colormap */
673    cur += sprintf(cur, "%c", 0);
674    /* Image Type: uncompressed truecolor */
675    cur += sprintf(cur, "%c", 2);
676    /* Color Map Specification: no color map */
677    memset(cur, 0, 5); cur += 5;
678
679    /* Image Specification */
680    cur += sprintf(cur, "%c%c", 0, 0); /* X Origin */
681    cur += sprintf(cur, "%c%c", 0, 0); /* Y Origin */
682    cur += sprintf(cur, "%c%c", w & 0xff, w >> 8); /* Width */
683    cur += sprintf(cur, "%c%c", h & 0xff, h >> 8); /* Height */
684    cur += sprintf(cur, "%c", 32); /* Pixel Depth */
685    cur += sprintf(cur, "%c", 40); /* Image Descriptor */
686
687    /* Image ID: no ID */
688    /* Color Map Data: no colormap */
689
690    /* Image Data */
691    cucul_render_canvas(cv, f, cur, w, h, 4 * w);
692
693    /* Swap bytes. What a waste of time. */
694    for(i = 0; i < w * h * 4; i += 4)
695    {
696        char w;
697        w = cur[i]; cur[i] = cur[i + 3]; cur[i + 3] = w;
698        w = cur[i + 1]; cur[i + 1] = cur[i + 2]; cur[i + 2] = w;
699    }
700
701    cucul_free_font(f);
702}
703
Note: See TracBrowser for help on using the repository browser.