Skip to content

Instantly share code, notes, and snippets.

@weeble
Created Aug 22, 2022
Embed
What would you like to do?
Birthday present for @thingskatedid
/* XPM */
static char * kate_moomin_xpm[] = {
"80 80 15 1",
" c None",
". c #000000",
"+ c #AC3232",
"@ c #DF7126",
"# c #FBF236",
"$ c #99E550",
"% c #639BFF",
"& c #76428A",
"* c #FFFFFF",
"= c #4B692F",
"- c #EEC39A",
"; c #D95763",
"> c #524B24",
", c #663931",
"' c #595652",
"................................................................................",
"................................................................................",
"................................................................................",
"................................................................................",
"................................................................................",
"...++.++...@...####..$$$$..%%.%%...&&&&..++.@@@@..####.$$.$$.%%%%....&...++.++..",
"...++.++..@@@..##.##.$$.$$.%%.%%...&&.&&.++.@@.@@..##..$$.$$.%%.%%..&&&..++.++..",
"...+++++.@@.@@.##.##.$$.$$.%%.%%...&&&&..++.@@.@@..##..$$$$$.%%.%%.&&.&&.++.++..",
"...++.++.@@.@@.##.##.$$.$$..%%%%...&&.&&.++.@@.@@..##..$$.$$.%%.%%.&&.&&..++++..",
"...++.++.@@@@@.####..$$$$.....%%...&&.&&.++.@@@@...##..$$.$$.%%.%%.&&&&&....++..",
"...++.++.@@.@@.##....$$....%%%%....&&&&..++.@@.@@..##..$$.$$.%%%%..&&.&&.++++...",
"................................................................................",
".......................***..***....**...*******.*******.........................",
".......................***.****...****..*******.*******.........................",
".......................*******...******...***...***.............................",
".......................******...***..***..***...*****...........................",
".......................*******..***..***..***...*****...........................",
".......................***.****.********..***...***.............................",
".......................***..***.***..***..***...*******.........................",
".......................***...**.***..***..***...*******.........................",
"................................................................................",
"................................................................................",
".........................................*......................................",
"........................................***.....................................",
"..........===..........................**.*.....................................",
".......@..====................****..****........................................",
".......@@.=====...............*..*********......................................",
".......@@..====...............**.*******..*.....................................",
"........@@.=====...............*.***.********...*****...........................",
".........@.======..............*****.******....*******..........................",
".........@.=======..............***********..**********.........................",
".........@.========.............******...**.************........................",
"...........==....................****.....**************........................",
"........==....=========..........****..**.**************........................",
"........================......*...***.******************........@...............",
"........=================...*.*...**********************.......@@@..............",
"......====....-----...====..***.*.*********************........@@@..............",
".....====...--------....==..*****..********************........@@@..............",
"....=====..-....-...........*****..**.****************.........@@...............",
"....===...--.-..-.-..-.......****.***.****************.........@@@@@............",
"......==.-----..---..-.......***..***..*************.........@@@@@@@@...........",
".......=.-----..---..-.......***.*****..**********....*.....@@.@@@@@.@..........",
".........-----------....-....***.******..******.......**...@@@.@@.@@.@@.........",
".....-....-----------...--...***********........*...*.***..@@..@@.@@..@@........",
".....--....--.----.....---...********************...*****..@..-------..@........",
".....---....--...----..---...**********************..****..@.---------.@........",
".....----....------....---....*********************.****.........-..............",
"......--......---.............*********************..***...--..---..---.........",
"..........=.#.....#.....==.....*********************.**.....--.--------.........",
"......=====.####.####...==......********************.**.....--..------..........",
".......====..###..####..==......********************.*.......--....---..........",
"........====..###...###.==.......*******************.*........------............",
"........=====.....=...#.=.......********************...........;;.;;............",
".........============...=.......********************........+.;;;;;;;...........",
"..........============..........*********************.....+++.;;;.;;;.++........",
"..........============..........*********************....++++.;;...;..++++......",
".........==============.........*********************...+++...+++++++++++++.....",
".........==============..........********************...+++..+++++++++..+++.....",
".........=========...==..........********************...++++.+++++++++..+++.....",
".........=============...........********************....+++..+++++++..+++......",
"........=================.........******************......+++.+++++++.++++......",
"........==================........******************.......+...+++++...++.......",
"........===================.......*****************......+.....+++++............",
".........=================.........****************.....++++..+++++++..+........",
"............>>>>>>>>>>===..........***************.....++++++++++++++++++.......",
"............>>>>>>>>>>..............********.*****........+++++++++++++++.......",
".............>>>..>>>...............******...****..............++++++++++.......",
".............>>....>>...............*****....****...............................",
".............,,....,,...............*****....****.............''...''...........",
"...........,,,,....,,,,.............*****...******............''...''...........",
".........,,,,,,....,,,,,,..........******....*******.........'''...'''..........",
".........,,,.,,....,,.,,,.........******.......******.......''''...''''.........",
"................................................................................",
"................................................................................",
"................................................................................",
"................................................................................",
"................................................................................",
"................................................................................",
"................................................................................",
"................................................................................"};
// Public domain.
// Annette Jane Wilson, @weeble
// Parts by Katherine Flavell, @thingskatedid
// Derived from https://gist.github.com/katef/59450aa622315bd35fc27bd383c2dbe6
// It draws a picture, in (blocky) colour!
// It is very late at night. I stopped as soon as it worked. Please be gentle with my C.
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "kate_moomin.xpm"
const char *SEPARATORS = " \t";
const int MAX_COLOURS = 128;
struct {
int x;
int y;
int m;
} BRAILLE[] = {
{ 0, 0, 0x2801 }, { 1, 0, 0x2808 },
{ 0, 1, 0x2802 }, { 1, 1, 0x2810 },
{ 0, 2, 0x2804 }, { 1, 2, 0x2820 },
{ 0, 3, 0x2840 }, { 1, 3, 0x2880 }
};
static void
utf8(unsigned cp, char c[])
{
if (cp <= 0x7f) {
c[0] = cp;
return;
}
if (cp <= 0x7ff) {
c[0] = (cp >> 6) + 192;
c[1] = (cp & 63) + 128;
return;
}
if (0xd800 <= cp && cp <= 0xdfff) {
/* invalid */
goto error;
}
if (cp <= 0xffff) {
c[0] = (cp >> 12) + 224;
c[1] = ((cp >> 6) & 63) + 128;
c[2] = (cp & 63) + 128;
return;
}
if (cp <= 0x10ffff) {
c[0] = (cp >> 18) + 240;
c[1] = ((cp >> 12) & 63) + 128;
c[2] = ((cp >> 6) & 63) + 128;
c[3] = (cp & 63) + 128;
return;
}
error:
fprintf(stderr, "codepoint out of range\n");
exit(1);
}
void set_colour(int colour) {
fprintf(stdout, "\033[38;2;%d;%d;%dm", (colour&0xFF0000)>>16, (colour&0xFF00)>>8, colour&0xFF);
}
int main(int argc, char *argv[]) {
int width, height, colours, cpp;
char **line;
line = kate_moomin_xpm;
char *cursor;
cursor = *line;
char *end;
width = (int)strtol(cursor, &end, 10);
cursor = end;
height = (int)strtol(cursor, &end, 10);
cursor = end;
colours = (int)strtol(cursor, &end, 10);
cursor = end;
cpp = (int)strtol(cursor, &end, 10);
if (colours > MAX_COLOURS) {
goto unsupported;
}
if (cpp != 1) {
goto unsupported;
}
{
int palette[MAX_COLOURS];
for (int i=0; i!=MAX_COLOURS; ++i) {
palette[i] = 0;
}
for (int colour=0; colour!=colours; ++colour) {
line++;
cursor = *line;
char *token;
char colour_code = *cursor;
cursor += 2;
if (*cursor != 'c') {
goto unsupported;
}
cursor += 2;
if (strcmp(cursor, "None") == 0) {
palette[colour_code] = 0;
} else {
if (*cursor != '#') {
goto unsupported;
}
cursor++;
palette[colour_code] = (int)strtol(cursor, &end, 16);
}
}
line++;
int cwidth = width / 2;
int cheight = height / 4;
for (int row=0; row!=cheight; ++row) {
int current_colour = 0;
fputs("\033[0m", stdout);
for (int col=0; col!=cwidth; ++col) {
int char_colour = 0;
int cp = 0;
char s[5] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
for (int i = 0; i!=sizeof BRAILLE / sizeof *BRAILLE; i++) {
char code=line[row*4+BRAILLE[i].y][col*2+BRAILLE[i].x];
int pixel_colour=palette[code];
if (pixel_colour) {
char_colour = pixel_colour;
cp |= BRAILLE[i].m;
}
}
if (char_colour != current_colour && char_colour != 0) {
set_colour(char_colour);
current_colour = char_colour;
}
if (cp == 0) {
cp = ' ';
}
utf8(cp, s);
fputs(s, stdout);
}
fputc('\n', stdout);
}
}
return 0;
error:
fprintf(stderr, "Error reading XPM.\n");
return 1;
unsupported:
fprintf(stderr, "Unsupported feature.\n");
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment