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

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