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

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