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