Created
September 10, 2019 19:51
-
-
Save plonk/ece60e818dc3cb88ae714532a959acab to your computer and use it in GitHub Desktop.
arakiken さんによる https://qiita.com/arakiken/items/626b02cd857d20c12fbc の記事にあるサンプルコードを改変したものです。どのように変更したかは今ちょっと思い出せません。osiire で使ってます
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include "drcssixel.c" | |
static void pua_to_utf8(unsigned char *dst, unsigned int *src, unsigned int len) { | |
unsigned int i; | |
for(i = 0; i < len; i++) { | |
unsigned int code = src[i]; | |
*(dst++) = ((code >> 18) & 0x07) | 0xf0; | |
*(dst++) = ((code >> 12) & 0x3f) | 0x80; | |
*(dst++) = ((code >> 6) & 0x3f) | 0x80; | |
*(dst++) = (code & 0x3f) | 0x80; | |
} | |
} | |
static char charset = '1'; | |
static int quiet = 0; | |
char** parse_options(int argc, char** argv) { | |
int i; | |
for (i = 1; i < argc && argv[i] != NULL;) { | |
if (strcmp(argv[i], "-c") == 0) { | |
if (argv[i + 1] == NULL) { | |
printf("missing argument to -c\n"); | |
exit(1); | |
} | |
charset = argv[i + 1][0]; | |
i += 2; | |
} else if (strcmp(argv[i], "-d") == 0) { | |
if (argv[i + 1] == NULL) { | |
printf("missing argument to -d\n"); | |
exit(1); | |
} | |
char *p = argv[i+1]; | |
col_width = atoi(p); | |
while (*p) { | |
if (*p == 'x') { | |
p++; | |
break; | |
} | |
p++; | |
} | |
if (!*p) { // the string either does not contain 'x' or ends with 'x' | |
printf("invalid argument to -d\n"); | |
exit(1); | |
} | |
line_height = atoi(p); | |
if (col_width <= 0 || line_height <= 0) { | |
printf("Warning: invalid argument to -d (range error)\n"); | |
col_width = line_height = 0; | |
} | |
i += 2; | |
} else if (strcmp(argv[i], "-q") == 0) { | |
quiet = 1; | |
i++; | |
} else { | |
return &argv[i]; | |
} | |
} | |
// no files specified | |
printf("Usage: drcssixel-test [OPTIONS]... [FILES]...\n"); | |
printf(" -c\t\tspecify (first) character set. Defaults to '1'\n"); | |
printf(" -d\t\tspecify cell dimensions. (eg. '8x16')\n"); | |
printf(" -q\t\tdo not show the image.\n"); | |
exit(1); | |
} | |
int main(int argc, char **argv) { | |
unsigned int *buf, *buf_p; | |
int is_96cs = 0; | |
int count; | |
int num_cols, num_rows, col, row; | |
char **files; | |
files = parse_options(argc, argv); | |
for (count = 0; files[count]; count++) { | |
buf = drcs_sixel_from_file(files[count], &charset, &is_96cs, &num_cols, &num_rows); | |
if (buf == NULL) { | |
return 1; | |
} | |
if (!quiet) { | |
pua_to_utf8((char*) buf, buf, num_cols * num_rows); /* UTF32 -> UTF8 */ | |
buf_p = buf; | |
for(row = 0; row < num_rows; row++) { | |
for (col = 0; col < num_cols; col++) { | |
write(STDOUT_FILENO, buf_p++, 4); | |
} | |
write(STDOUT_FILENO, "\n", 1); | |
} | |
} | |
free(buf); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment