Debian Codegraph @

examples/export.c

raw
1 /*
2 * export libcaca export test program
3 * Copyright (c) 2006-2012 Sam Hocevar <[email protected]>
4 * All Rights Reserved
5 *
6 * This program is free software. It comes without any warranty, to
7 * the extent permitted by applicable law. You can redistribute it
8 * and/or modify it under the terms of the Do What the Fuck You Want
9 * to Public License, Version 2, as published by Sam Hocevar. See
10 * http://www.wtfpl.net/ for more details.
11 */
12
13 #include "config.h"
14
15 #if !defined(__KERNEL__)
16 # include <stdio.h>
17 # include <stdlib.h>
18 # include <string.h>
19 #endif
20
21 #include "caca.h"
22
23 #define WIDTH 80
24 #define HEIGHT 32
25
26 uint32_t pixels[256*256];
27
28 int main(int argc, char *argv[])
29 {
30 caca_canvas_t *cv;
31 caca_dither_t *dither;
32 void *buffer;
33 char *file, *format;
34 char const * const * exports, * const * p;
35 size_t len;
36 int x, y;
37
38 exports = caca_get_export_list();
39
40 if(argc < 2 || argc > 3)
41 {
42 fprintf(stderr, "%s: wrong argument count\n", argv[0]);
43 fprintf(stderr, "usage: %s [file] <format>\n", argv[0]);
44 fprintf(stderr, "where <format> is one of:\n");
45 for(p = exports; *p; p += 2)
46 fprintf(stderr, " \"%s\" (%s)\n", *p, *(p + 1));
47 exit(-1);
48 }
49
50 if(argc == 2)
51 {
52 file = NULL;
53 format = argv[1];
54 }
55 else
56 {
57 file = argv[1];
58 format = argv[2];
59 }
60
61 for(p = exports; *p; p += 2)
62 if(!strcasecmp(format, *p))
63 break;
64
65 if(!*p)
66 {
67 fprintf(stderr, "%s: unknown format `%s'\n", argv[0], format);
68 fprintf(stderr, "please use one of:\n");
69 for(p = exports; *p; p += 2)
70 fprintf(stderr, " \"%s\" (%s)\n", *p, *(p + 1));
71 exit(-1);
72 }
73
74 if(file)
75 {
76 cv = caca_create_canvas(0, 0);
77 if(caca_import_canvas_from_file(cv, file, "") < 0)
78 {
79 fprintf(stderr, "%s: `%s' has unknown format\n", argv[0], file);
80 exit(-1);
81 }
82 }
83 else
84 {
85 cv = caca_create_canvas(WIDTH, HEIGHT);
86
87 for(y = 0; y < 256; y++)
88 {
89 for(x = 0; x < 256; x++)
90 {
91 uint32_t r = x;
92 uint32_t g = (255 - y + x) / 2;
93 uint32_t b = y * (255 - x) / 256;
94 pixels[y * 256 + x] = (r << 16) | (g << 8) | (b << 0);
95 }
96 }
97
98 dither = caca_create_dither(32, 256, 256, 4 * 256,
99 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0);
100 if(!strcmp(format, "ansi") || !strcmp(format, "utf8"))
101 caca_set_dither_charset(dither, "shades");
102 caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv),
103 caca_get_canvas_height(cv), dither, pixels);
104 caca_free_dither(dither);
105
106 caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK);
107 caca_draw_thin_box(cv, 0, 0, WIDTH - 1, HEIGHT - 1);
108
109 caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE);
110 caca_fill_ellipse(cv, WIDTH / 2, HEIGHT / 2,
111 WIDTH / 4, HEIGHT / 4, ' ');
112
113 caca_set_color_ansi(cv, CACA_LIGHTGRAY, CACA_BLACK);
114 caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 6,
115 " lightgray on black ");
116 caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT);
117 caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 5,
118 " default on transparent ");
119 caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE);
120 caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 4,
121 " black on white ");
122
123 caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE);
124 caca_put_str(cv, WIDTH / 2 - 8, HEIGHT / 2 - 3, "[<><><><> <>--<>]");
125 caca_put_str(cv, WIDTH / 2 - 8, HEIGHT / 2 - 2, "[ドラゴン ボーレ]");
126 caca_put_str(cv, WIDTH / 2 - 7, HEIGHT / 2 + 2, "äβç ░▒▓█▓▒░ ΔЗҒ");
127 caca_put_str(cv, WIDTH / 2 - 5, HEIGHT / 2 + 4, "(\") \\o/ <&>");
128
129 caca_set_attr(cv, CACA_BOLD);
130 caca_put_str(cv, WIDTH / 2 - 16, HEIGHT / 2 + 3, "Bold");
131 caca_set_attr(cv, CACA_BLINK);
132 caca_put_str(cv, WIDTH / 2 - 9, HEIGHT / 2 + 3, "Blink");
133 caca_set_attr(cv, CACA_ITALICS);
134 caca_put_str(cv, WIDTH / 2 - 1, HEIGHT / 2 + 3, "Italics");
135 caca_set_attr(cv, CACA_UNDERLINE);
136 caca_put_str(cv, WIDTH / 2 + 8, HEIGHT / 2 + 3, "Underline");
137 caca_set_attr(cv, 0);
138
139 caca_set_color_ansi(cv, CACA_WHITE, CACA_LIGHTBLUE);
140 caca_put_str(cv, WIDTH / 2 - 7, HEIGHT / 2, " LIBCACA ");
141
142 for(x = 0; x < 16; x++)
143 {
144 caca_set_color_argb(cv, 0xff00 | x, 0xf00f | (x << 4));
145 caca_put_char(cv, WIDTH / 2 - 7 + x, HEIGHT / 2 + 6, '#');
146 }
147 }
148
149 buffer = caca_export_canvas_to_memory(cv, format, &len);
150 fwrite(buffer, len, 1, stdout);
151 free(buffer);
152
153 caca_free_canvas(cv);
154
155 return 0;
156 }
157