Sixel Graphics

Sixel Graphics

May 4, 2021
graphics

Reading time: 1 minutes and 6 seconds.

What are Sixels?

VT330/VT340 Programmer Reference Manual, Ch. 14 Sixel Graphics.

Escape Character

DCS (Device Control String): 0x90

OR

ESC+P: 0x1B + 0x50

Termination

ST (String Terminator) 0x0

OR

ESC+\: 0x1B + 0x5C

Control Functions

LSB (least-significant bit) at the top.

>>> 0x90 0x40 = 0x90 0b000001
■
□
□
□
□
□

>>> 0x90 0x74 = 0x90 0b110100
■
□
■
□
■
■
>>> sixel2str = lambda i : '\n'.join(['■' if (((i-0x3F) >> j) & 1) else '□' for j in range(6)])
>>> sixels2str = lambda il : '\n'.join([' '.join(l) for l in zip(*[[c for c in sixel2str(i) if c != '\n'] for i in il])])
>>> print(sixel2str(0x74))






# Brian Kernighan's algo.
def count_bits(n):
    count = 0
    while n:
        n &= (n-1)
        count += 1

    return count

countbits = lambda n : (count := 0, (while n: (n &= (n-1)), count += 1), count)[-1]
for i, c in enumerate(range(ord('?'), ord('?')+64)):
    print(f"|`{bin(i).replace('0b','').rjust(6, '0')}`|`{chr(c)}`|`{hex(c).upper().replace('X','x')}`|")
Sixel Char Hex
000000 ? 0x3F
000001 @ 0x40
000010 A 0x41
000011 B 0x42
000100 C 0x43
000101 D 0x44
000110 E 0x45
000111 F 0x46
001000 G 0x47
001001 H 0x48
001010 I 0x49
001011 J 0x4A
001100 K 0x4B
001101 L 0x4C
001110 M 0x4D
001111 N 0x4E
010000 O 0x4F
010001 P 0x50
010010 Q 0x51
010011 R 0x52
010100 S 0x53
010101 T 0x54
010110 U 0x55
010111 V 0x56
011000 W 0x57
011001 X 0x58
011010 Y 0x59
011011 Z 0x5A
011100 [ 0x5B
011101 \ 0x5C
011110 ] 0x5D
011111 ^ 0x5E
100000 _ 0x5F
100001 ` 0x60
100010 a 0x61
100011 b 0x62
100100 c 0x63
100101 d 0x64
100110 e 0x65
100111 f 0x66
101000 g 0x67
101001 h 0x68
101010 i 0x69
101011 j 0x6A
101100 k 0x6B
101101 l 0x6C
101110 m 0x6D
101111 n 0x6E
110000 o 0x6F
110001 p 0x70
110010 q 0x71
110011 r 0x72
110100 s 0x73
110101 t 0x74
110110 u 0x75
110111 v 0x76
111000 w 0x77
111001 x 0x78
111010 y 0x79
111011 z 0x7A
111100 { 0x7B
111101 | 0x7C
111110 } 0x7D
111111 ~ 0x7E