summarylogtreecommitdiffstats
path: root/rxvt-unicode-sixel.patch
blob: 144f6f30ef7438d80e3df9bc80af84a5771f74b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
--- src/Makefile.in
+++ src/Makefile.in
@@ -39,7 +39,7 @@ COMMON = \
	command.o rxvtfont.o init.o main.o misc.o \
	screen.o scrollbar.o scrollbar-next.o scrollbar-rxvt.o \
	scrollbar-xterm.o scrollbar-plain.o xdefaults.o encoding.o \
	rxvttoolkit.o rxvtutil.o keyboard.o rxvtimg.o \
-	ev_cpp.o @PERL_O@
+	ev_cpp.o sixel.o @PERL_O@
 
 COMMON_DAEMON = rxvtdaemon.o
@@ -256,3 +256,5 @@ rxvtperl.o: ../libptytty/src/estl.h emman.h rxvtfont.h rxvttoolkit.h
 rxvtperl.o: rxvtfont.h rxvttoolkit.h callback.h rxvtimg.h scrollbar.h
 rxvtperl.o: rxvtperl.h hookinc.h rsinc.h optinc.h keyboard.h perlxsi.c
 rxvtperl.o: iom_perl.h
+sixel.o: sixel.h
+sixel_hls.o: sixel_hls.h
--- src/command.C
+++ src/command.C
@@ -51,6 +51,7 @@
 #include "rxvtperl.h"
 #include "version.h"
 #include "command.h"
+#include "sixel.h"
 
 #ifdef KEYSYM_RESOURCE
 # include "keyboard.h"
@@ -3268,15 +3271,215 @@ rxvt_term::get_to_st (unicode_t &ends_how)
 void
 rxvt_term::process_dcs_seq ()
 {
-  /*
-   * Not handled yet
-   */
-
-  unicode_t eh;
-  char *s = get_to_st (eh);
-  if (s)
-    free (s);
+  unicode_t ch;
+  unsigned char c;
+  const int max_params = 16;
+  int params[max_params] = { 0 };
+  int nparams = 0;
+  int cmd = 0;
+  enum {
+    DCS_START,
+    DCS_PARAM,
+    DCS_INTERMEDIATE,
+    DCS_PASSTHROUGH,
+    DCS_IGNORE,
+    DCS_ESC
+  };
+  int st = DCS_START;
+  int x, y;
+  sixel_state_t sixel_st = { PS_GROUND };
+  imagelist_t *new_image;
+  line_t l;
+
+  while (1) {
+    if ((ch = next_char ()) == NOCHAR) {
+        pty_fill ();
+        continue;
+    }
+    c = ch & 0xff;
+    switch (st) {
+    case DCS_START:
+    case DCS_PARAM:
+      switch (c) {
+      case '\030':  /* CAN */
+        goto end;
+      case '\032':  /* SUB */
+        st = DCS_IGNORE;
+        break;
+      case '\033':
+        st = DCS_ESC;
+        break;
+      case ' ' ... '/':
+        cmd = cmd << 8 | c;
+        st = cmd > (0xff << 16) ? DCS_IGNORE : DCS_INTERMEDIATE;
+        break;
+      case '0' ... '9':
+        params[nparams] = params[nparams] * 10 + c - '0';
+        st = params[nparams] > 256 ? DCS_IGNORE : DCS_PARAM;
+        break;
+      case ';':
+        if (++nparams == max_params)
+          st = DCS_IGNORE;
+        else
+          params[nparams] = 0;
+        break;
+      case ':':
+        st = DCS_IGNORE;
+        break;
+      case '<' ... '?':
+        cmd = cmd << 8 | c;
+        st = cmd > (0xff << 16) ? DCS_IGNORE : DCS_PARAM;
+        break;
+      case '@' ... '~':
+        cmd = cmd << 8 | c;
+        st = cmd > (0xff << 16) ? DCS_IGNORE : DCS_PASSTHROUGH;
+        break;
+      default:
+        st = DCS_IGNORE;
+        break;
+      }
+      break;
+    case DCS_INTERMEDIATE:
+      switch (c) {
+      case '\030':  /* CAN */
+        goto end;
+      case '\032':  /* SUB */
+        st = DCS_IGNORE;
+        break;
+      case '\033':
+        st = DCS_ESC;
+        break;
+      case ' ' ... '/':
+        cmd = cmd << 8 | c;
+        st = cmd > (0xff << 16) ? DCS_IGNORE : DCS_INTERMEDIATE;
+        break;
+      case '@' ... '~':
+        cmd = cmd << 8 | c;
+        st = cmd > (0xff << 16) ? DCS_IGNORE : DCS_PASSTHROUGH;
+        break;
+      default:
+        st = DCS_IGNORE;
+        break;
+      }
+      break;
+    case DCS_PASSTHROUGH:
+      switch (c) {
+      case '\030':  /* CAN */
+        goto end;
+      case '\032':  /* SUB */
+        st = DCS_IGNORE;
+        break;
+      case '\033':
+        st = DCS_ESC;
+        break;
+      default:
+        switch (cmd) {
+        case 'q':  /* DECSIXEL */
+          switch (sixel_st.state) {
+            case PS_GROUND:
+              {
+                rgba fg = pix_colors[Color_fg];
+                rgba bg = pix_colors[Color_bg];
+                sixel_parser_init(&sixel_st,
+                                  fg.b >> 8 << 16 | fg.g >> 8 << 8 | fg.r >> 8,
+                                  bg.b >> 8 << 16 | bg.g >> 8 << 8 | bg.r >> 8,
+                                  1, fwidth, fheight);
+              }
+              break;
+            default:
+              sixel_parser_parse(&sixel_st, &c, 1);
+              break;
+          }
+          break;
+        default:
+          break;
+        }
+        break;
+      }
+      break;
+    case DCS_IGNORE:
+      switch (c) {
+      case '\030':  /* CAN */
+        goto end;
+      case '\032':  /* SUB */
+        st = DCS_IGNORE;
+        break;
+      case '\033':
+        st = DCS_ESC;
+        break;
+      default:
+        st = DCS_IGNORE;
+        break;
+      }
+      break;
+    case DCS_ESC:
+      switch (c) {
+      case '\\':
+        switch (cmd) {
+        case 'q':  /* DECSIXEL */
+          new_image = (imagelist_t *)rxvt_calloc (1, sizeof(imagelist_t));
+          new_image->pixels = (unsigned char *)rxvt_malloc (sixel_st.image.width * sixel_st.image.height * 4);
+          (void) sixel_parser_finalize (&sixel_st, new_image->pixels);
+          sixel_parser_deinit(&sixel_st);
+          new_image->col = screen.cur.col;
+          new_image->row = screen.cur.row + virtual_lines;
+          new_image->pxwidth = sixel_st.image.width;
+          new_image->pxheight = sixel_st.image.height;
+          if (this->images) {
+            imagelist_t *im;
+            for (im = this->images; im->next; im = im->next)
+              ;
+            new_image->prev = im;
+            im->next = new_image;
+          } else {
+            this->images = new_image;
+          }
 
+          for (y = 0; y < Pixel2Row (new_image->pxheight + fheight - 1); ++y)
+            {
+              if ((priv_modes & PrivMode_SixelDisplay))
+                l = ROW(screen.cur.row + y);
+              else
+                l = ROW(screen.cur.row);
+              for (x = 0; x < min (ncol - screen.cur.col, Pixel2Col (new_image->pxwidth + fwidth - 1)); ++x)
+                {
+                  l.t[screen.cur.col + x] = CHAR_IMAGE;
+                  l.r[screen.cur.col + x] = RS_None;
+                }
+              if (!(priv_modes & PrivMode_SixelDisplay))
+                {
+                  if (y == Pixel2Row (new_image->pxheight + fheight - 1) - 1)  // on last row
+                    {
+                      if ((priv_modes & PrivMode_SixelScrsRight))
+                        {
+                          screen.cur.col += x;
+                        }
+                      else
+                        {
+                          scr_index (UP);
+                          if ((priv_modes & PrivMode_SixelScrsLeft))
+                            scr_gotorc (0, 0, R_RELATIVE);
+                        }
+                    }
+                  else
+                    {
+                      scr_index (UP);
+                    }
+                }
+            }
+          break;
+        default:
+          break;
+        }
+        goto end;
+      default:
+        goto end;
+      }
+    default:
+      break;
+    }
+  }
+end:
   return;
 }
 
@@ -3729,6 +3932,7 @@ rxvt_term::process_terminal_mode (int mode, int priv ecb_unused, unsigned int na
 #ifndef NO_BACKSPACE_KEY
                   { 67, PrivMode_BackSpace },   // DECBKM
 #endif
+                  { 80, PrivMode_SixelDisplay },   // DECSDM sixel display mode
                   { 1000, PrivMode_MouseX11 },
                   { 1002, PrivMode_MouseBtnEvent },
                   { 1003, PrivMode_MouseAnyEvent },
@@ -3750,8 +3954,12 @@ rxvt_term::process_terminal_mode (int mode, int priv ecb_unused, unsigned int na
                    { 1049, PrivMode_Screen }, /* xterm extension, clear screen on ti rather than te */
                  // 1051, 1052, 1060, 1061 keyboard emulation NYI
 #if ENABLE_FRILLS
                   { 2004, PrivMode_BracketPaste },
 #endif
+                 // 7730 sixel-scrolls-left mode, originally proposed by mintty
+                  { 7730, PrivMode_SixelScrsLeft },
+                 // 8452 sixel-scrolls-right mode, originally proposed by RLogin
+                  { 8452, PrivMode_SixelScrsRight },
                 };
 
   if (nargs == 0)
@@ -4086,6 +4294,62 @@ rxvt_term::process_sgr_mode (unsigned int nargs, const int *arg)
     }
 }
 
+void
+rxvt_term::process_graphics_attributes (unsigned int nargs, const int *arg)
+{
+  if (nargs != 3)
+    return;
+  switch (arg[0])
+    {
+      case 1:  /* number of sixel color palette */
+        switch (arg[1])
+          {
+            case 1:  /* read */
+              tt_printf ("\033[?%d;%d;%dS", arg[0], 0, DECSIXEL_PALETTE_MAX);
+              break;
+            case 2:  /* reset to default */
+              tt_printf ("\033[?%d;%d;%dS", arg[0], 0, DECSIXEL_PALETTE_MAX);
+              break;
+            case 3:  /* set */
+              if (arg[2] == DECSIXEL_PALETTE_MAX)
+                tt_printf ("\033[?%d;%d;%dS", arg[0], 0, DECSIXEL_PALETTE_MAX);
+              else
+                tt_printf ("\033[?%d;%d;%dS", arg[0], 3, 0);
+              break;
+            case 4:  /* read the maximum value */
+              tt_printf ("\033[?%d;%d;%dS", arg[0], 0, DECSIXEL_PALETTE_MAX);
+              break;
+            default:
+              tt_printf ("\033[?%d;%d;%dS", arg[0], 2, 0);
+              break;
+          }
+        break;
+      case 2:  /* geometory of sixel graphics */
+        switch (arg[1])
+          {
+            case 1:  /* read */
+              tt_printf ("\033[?%d;%d;%d;%dS", arg[0], 0, ncol * fwidth, nrow * fheight);
+              break;
+            case 2:  /* reset to default */
+              tt_printf ("\033[?%d;%d;%d;%dS", arg[0], 3, 0, 0);
+              break;
+            case 3:  /* set */
+              tt_printf ("\033[?%d;%d;%d;%dS", arg[0], 3, 0, 0);
+              break;
+            case 4:  /* read the maximum value */
+              tt_printf ("\033[?%d;%d;%d;%dS", arg[0], 3, 0, 0);
+              break;
+            default:
+              tt_printf ("\033[?%d;%d;%d;%dS", arg[0], 2, 0, 0);
+              break;
+          }
+        break;
+      default:
+        tt_printf ("\033[?%d;%d;%dS", arg[0], 1, 0);
+        break;
+    }
+}
+
 void
 rxvt_term::set_cursor_style (int style)
 {
--- src/command.h
+++ src/command.h
@@ -58,7 +58,7 @@
  * two strings should be the same so that identical read (2) calls may be
  * used.
  */
-#define VT100_ANS	"\033[?1;2c"	/* vt100 answerback */
+#define VT100_ANS	"\033[?1;2c"	/* vt100 answerback(1;2) + sixel (4)*/
 #ifndef ESCZ_ANSWER
 # define ESCZ_ANSWER	VT100_ANS	/* obsolete ANSI ESC[c */
 #endif
--- src/rxvt.h
+++ src/rxvt.h
@@ -400,6 +400,7 @@ enum {
   C0_CAN, C0_EM , C0_SUB, C0_ESC, C0_IS4, C0_IS3, C0_IS2, C0_IS1,
 };
 #define CHAR_ST                 0x9c    /* 0234 */
+#define CHAR_IMAGE              0x01    /* special character for image area */
 
 /*
  * XTerm Operating System Commands: ESC ] Ps;Pt (ST|BEL)
@@ -591,2 +592,11 @@
-#define PrivMode_ExtMouseSGR    (1UL<<27)
+#define PrivMode_SixelDisplay   (1UL<<27) // sixel display mode
+/*  DECSET 7730: sixel scrolling end position
+ *  on: sixel scrolling moves cursor to beginning of the line
+ *  off(default): sixel scrolling moves cursor to left of graphics */
+#define PrivMode_SixelScrsLeft  (1UL<<28)
+/*  DECSET 8452: sixel scrolling end position right
+ *  on: sixel scrolling leaves cursor to right of graphic
+ *  off(default): position after sixel depends on sixel_scrolls_left */
+#define PrivMode_SixelScrsRight (1UL<<29)
+#define PrivMode_ExtMouseSGR    (1UL<<30)
 
@@ -977,6 +978,7 @@ struct TermWin_t
   int            term_start;    /* term lines start here                    */
   int            view_start;    /* scrollback view starts here              */
   int            top_row;       /* topmost row index of scrollback          */
+  int            virtual_lines;
   Window         parent;        /* parent identifier                        */
   Window         vt;            /* vt100 window                             */
   GC             gc;            /* GC for drawing                           */
@@ -1062,2 +1073,14 @@
 
+struct imagelist_t
+{
+  imagelist_t *prev, *next;
+  unsigned char *pixels;
+  Drawable drawable;
+  void *storage;
+  int col;
+  int row;
+  int pxwidth;
+  int pxheight;
+};
+
 /* ------------------------------------------------------------------------- */
@@ -1081,6 +1104,7 @@ struct rxvt_vars : TermWin_t
 #ifdef OFF_FOCUS_FADING
   rxvt_color      pix_colors_unfocused[TOTAL_COLORS];
 #endif
+  imagelist_t     *images;
 };
 
 struct rxvt_term : zero_initialized, rxvt_vars, rxvt_screen
@@ -1375,6 +1399,7 @@ struct rxvt_term : zero_initialized, rxvt_vars, rxvt_screen
   int privcases (int mode, unsigned long bit);
   void process_terminal_mode (int mode, int priv, unsigned int nargs, const int *arg);
   void process_sgr_mode (unsigned int nargs, const int *arg);
+  void process_graphics_attributes (unsigned int nargs, const int *arg);
   void set_cursor_style (int style);
   // init.C
   void init (stringvec *argv, stringvec *envv);
--- src/screen.C
+++ src/screen.C
@@ -30,6 +30,174 @@
 #include "rxvtperl.h"           /* NECESSARY */
 
 #include <inttypes.h>
+#include <stdio.h>
+
+// tempfile_t manipulation
+
+typedef struct {
+  FILE *fp;
+  unsigned int ref_counter;
+} tempfile_t;
+
+typedef struct {
+  tempfile_t *tempfile;
+  size_t position;
+} temp_storage_t;
+
+static tempfile_t *tempfile_current = NULL;
+static size_t tempfile_num = 0;
+static size_t const TEMPFILE_MAX_SIZE = 1024 * 1024 * 16;  /* 16MB */
+static size_t const TEMPFILE_MAX_NUM = 16;
+
+static tempfile_t *
+tempfile_new (void)
+{
+  tempfile_t *tempfile;
+  FILE *fp;
+
+  fp = tmpfile();
+  if (!fp)
+    return NULL;
+
+  tempfile = (tempfile_t *)rxvt_malloc (sizeof(tempfile_t));
+  if (!tempfile)
+    return NULL;
+
+  tempfile->fp = fp;
+  tempfile->ref_counter = 1;
+
+  tempfile_num++;
+
+  return tempfile;
+}
+
+static void
+tempfile_destroy(tempfile_t *tempfile)
+{
+  if (tempfile == tempfile_current)
+    tempfile_current = NULL;
+  fclose((FILE *)tempfile->fp);
+  free (tempfile);
+  tempfile_num--;
+}
+
+static void
+tempfile_ref(tempfile_t *tempfile)
+{
+  tempfile->ref_counter++;
+}
+
+static void
+tempfile_deref(tempfile_t *tempfile)
+{
+  if (--tempfile->ref_counter == 0)
+    tempfile_destroy(tempfile);
+}
+
+static size_t
+tempfile_size(tempfile_t *tempfile)
+{
+  struct stat info;
+
+  fstat(fileno(tempfile->fp), &info);
+
+  return info.st_size;
+}
+
+static tempfile_t *
+tempfile_get(void)
+{
+  size_t size;
+
+  if (!tempfile_current)
+    {
+      tempfile_current = tempfile_new();
+      return tempfile_current;
+    }
+
+  /* get file size */
+  size = tempfile_size(tempfile_current);
+
+  /* if the file size reaches TEMPFILE_MAX_SIZE, return new temporary file */
+  if (size > TEMPFILE_MAX_SIZE)
+    {
+      tempfile_current = tempfile_new();
+      return tempfile_current;
+    }
+
+  /* increment reference counter */
+  tempfile_ref (tempfile_current);
+
+  return tempfile_current;
+}
+
+static bool
+tempfile_write(tempfile_t *tempfile, void *p, size_t pos, size_t size)
+{
+  size_t nbytes;
+
+  fseek ((FILE *)tempfile->fp, pos, SEEK_SET);
+  nbytes = fwrite(p, 1, size, tempfile->fp);
+  if (nbytes != size)
+    return false;
+
+  return true;
+}
+
+static bool
+tempfile_read(tempfile_t *tempfile, void *p, size_t pos, size_t size)
+{
+  size_t nbytes;
+
+  fflush((FILE *)tempfile->fp);
+  fseek((FILE *)tempfile->fp, pos, SEEK_SET);
+  nbytes = fread (p, 1, size, (FILE *)tempfile->fp);
+  if (nbytes != size)
+    return false;
+
+  return true;
+}
+
+// temp_storage_t implementation
+
+static temp_storage_t *
+storage_create(void)
+{
+  temp_storage_t *storage;
+  tempfile_t *tempfile;
+
+  tempfile = tempfile_get ();
+  if (!tempfile)
+    return NULL;
+
+  storage = (temp_storage_t *)rxvt_malloc (sizeof(temp_storage_t));
+  if (!storage)
+    return NULL;
+
+  storage->tempfile = tempfile;
+  storage->position = tempfile_size(storage->tempfile);
+
+  return storage;
+}
+
+static void
+storage_destroy (temp_storage_t *storage)
+{
+  tempfile_deref (storage->tempfile);
+  free (storage);
+}
+
+static bool
+storage_write(temp_storage_t *storage, void *p, size_t size)
+{
+  return tempfile_write (storage->tempfile, p, storage->position, size);
+}
+
+static bool
+storage_read(temp_storage_t *storage, void *p, size_t size)
+{
+  return tempfile_read (storage->tempfile, p, storage->position, size);
+}
 
 static inline void
 fill_text (text_t *start, text_t value, int len)
@@ -674,6 +842,7 @@ rxvt_term::scr_scroll_text (int row1, int row2, int count) NOTHROW
 
       // scroll everything up 'count' lines
       term_start = (term_start + count) % total_rows;
+      virtual_lines += count;
 
       // now copy lines below the scroll region bottom to the
       // bottom of the screen again, so they look as if they
@@ -2447,6 +2616,168 @@ rxvt_term::scr_refresh () NOTHROW
         }                     /* for (col....) */
     }                         /* for (row....) */
 
+  /*
+   * F: draw sixel images
+   */
+  if (images)
+    {
+      imagelist_t *im;
+      GC clipgc;
+      int trow, brow;  // top, bottom
+      int nlimit = 0xffff;  //  num of allocated rects
+      XRectangle *rects = NULL, *itrect = NULL, *p = NULL;
+      int n, x, y;
+      XImage xi;
+
+      // for each images
+      for (im = images; im; im = im->next)
+        {
+          trow = im->row - virtual_lines;
+          brow = trow + (im->pxheight + fheight - 1) / fheight;
+
+          // if the image is out of scrollback, delete it.
+          if (brow <= top_row)
+            {
+              if (im->prev)
+                im->prev->next = im->next;
+              else
+                images = im->next;
+              if (im->next)
+                im->next->prev = im->prev;
+              if (im->drawable)
+                XFreePixmap (dpy, im->drawable);
+              if (im->storage)
+                storage_destroy ((temp_storage_t *)im->storage);
+              free (im->pixels);
+              free (im);
+              continue;
+            }
+
+          // if the image is out of view, serialize into the storage object(im->storage).
+          if (trow >= view_start + nrow || brow <= view_start)
+            {
+              if (! im->storage)
+                {
+                  if (im->drawable)
+                    {
+                      XFreePixmap (dpy, im->drawable);
+                      im->drawable = NULL;
+                    }
+                  if (! im->storage)
+                      im->storage = storage_create();
+                  if (! storage_write ((temp_storage_t *)im->storage, im->pixels, im->pxwidth * im->pxheight * 4))
+                    break;
+                  free (im->pixels);
+                  im->pixels = NULL;
+                }
+              continue;
+            }
+
+          // ensure the pixmap(im->drawable) is available
+          if (! im->drawable)
+            {
+              // ensure the in-memory pixel buffer(im->pixels) is available
+              if (! im->pixels)
+                {
+                  im->pixels = (unsigned char *)rxvt_malloc (im->pxwidth * im->pxheight * 4);
+                  if (! storage_read ((temp_storage_t *)im->storage, im->pixels, im->pxwidth * im->pxheight * 4))
+                    break;
+                  storage_destroy ((temp_storage_t *)im->storage);
+                  im->storage = NULL;
+                  assert (im->pixels);
+                }
+
+              // create the pixmap object(im->drawable).
+              XInitImage (&xi);
+              xi.format = ZPixmap;
+              xi.data = (char *)im->pixels;
+              xi.width = im->pxwidth;
+              xi.height = im->pxheight;
+              xi.xoffset = 0;
+              xi.byte_order = LSBFirst;
+              xi.bitmap_bit_order = MSBFirst;
+              xi.bits_per_pixel = 32;
+              xi.bytes_per_line = im->pxwidth * 4;
+              xi.bitmap_unit = 32;
+              xi.bitmap_pad = 32;
+              xi.depth = 24;
+              im->drawable = XCreatePixmap(dpy, vt, im->pxwidth, im->pxheight, DefaultDepth (dpy, 0));
+              XPutImage (dpy, im->drawable, gc, &xi, 0, 0, 0, 0, im->pxwidth, im->pxheight);
+            }
+
+          // XXX: this is shoddy work!!
+          // construct clipping rectangle array
+          itrect = rects = NULL;
+          for (y = trow - view_start; y < brow - view_start; y++)  // for rows
+            {
+              if (y >= 0 && y < nrow)
+                {
+                  // compare recent two clip rectangles, combine them if they has same (left, width).
+                  if (itrect - rects > 0 && itrect->x == (itrect - 1)->x && itrect->width == (itrect - 1)->width)
+                    {
+                      // test whether itrect adjacent to itrect - 1
+                      if ((itrect - 1)->y + (itrect - 1)->height == itrect->y)
+                        {
+                          itrect--;
+                          itrect->height += fheight;
+                        }
+                    }
+
+                  for (x = im->col; x < im->col + Pixel2Col (im->pxwidth + fwidth - 1) && x < ncol; x++)  // for cols
+                    {
+                      // lazy initialization for rectangle array
+                      if (!rects)
+                        itrect = rects = (XRectangle *)rxvt_malloc (sizeof (XRectangle) * nlimit);
+                      if (rects == NULL)
+                        break;
+
+                      // resize rectangle array
+                      if (itrect - rects == nlimit)
+                        {
+                          p = (XRectangle *)rxvt_realloc (rects, sizeof (XRectangle) * (nlimit *= 2));
+                          if (p == NULL)
+                            break;
+                          rects = p;
+                        }
+
+                      if (ROW(view_start + y).t[x] == CHAR_IMAGE)
+                        {
+                          // check if current cell is combinable with last clip rectangle
+                          if (itrect - rects > 0 && (itrect - 1)->x + (itrect - 1)->width == Col2Pixel (x) && (itrect - 1)->y == Row2Pixel (y))
+                            {
+                              (itrect - 1)->width += fwidth;
+                            }
+                          else
+                            {
+                              // add new clip rectangle
+                              itrect->x = Col2Pixel (x);
+                              itrect->y = Row2Pixel (y);
+                              itrect->width = fwidth;
+                              itrect->height = fheight;
+                              itrect++;
+                            }
+                        }
+                    }
+                }
+            }
+
+          if (itrect - rects > 0)  // if it should be drawn
+            {
+              clipgc = XCreateGC (dpy, vt, 0, 0);
+              // set clipping region
+              XSetClipRectangles (dpy, clipgc, 0, 0, rects, itrect - rects, YXSorted);
+              XCopyArea (dpy, im->drawable, vt,
+                         clipgc, 0, 0,
+                         im->pxwidth, im->pxheight,
+                         (unsigned int)Width2Pixel (im->col),
+                         (unsigned int)Height2Pixel (im->row - virtual_lines - view_start));
+              XFreeGC (dpy, clipgc);
+            }
+
+          free (rects);
+      }
+    }
+
   /*
    * G: cleanup cursor and display outline cursor if necessary
    */
--- /dev/null
+++ src/sixel.C
@@ -0,0 +1,681 @@
+// sixel.c (part of mintty)
+// originally written by kmiya@cluti (https://github.com/saitoha/sixel/blob/master/fromsixel.c)
+// Licensed under the terms of the GNU General Public License v3 or later.
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <ctype.h>   /* isdigit */
+#include <string.h>  /* memcpy */
+
+#include "sixel.h"
+
+#define SIXEL_XRGB(r,g,b) (((r) * 255 + 50) / 100) << 0 | \
+                          (((g) * 255 + 50) / 100) << 8 | \
+                          (((b) * 255 + 50) / 100) << 16
+
+static colour const sixel_default_color_table[] = {
+	SIXEL_XRGB(0,  0,  0),   /*  0 Black    */
+	SIXEL_XRGB(20, 20, 80),  /*  1 Blue     */
+	SIXEL_XRGB(80, 13, 13),  /*  2 Red      */
+	SIXEL_XRGB(20, 80, 20),  /*  3 Green    */
+	SIXEL_XRGB(80, 20, 80),  /*  4 Magenta  */
+	SIXEL_XRGB(20, 80, 80),  /*  5 Cyan     */
+	SIXEL_XRGB(80, 80, 20),  /*  6 Yellow   */
+	SIXEL_XRGB(53, 53, 53),  /*  7 Gray 50% */
+	SIXEL_XRGB(26, 26, 26),  /*  8 Gray 25% */
+	SIXEL_XRGB(33, 33, 60),  /*  9 Blue*    */
+	SIXEL_XRGB(60, 26, 26),  /* 10 Red*     */
+	SIXEL_XRGB(33, 60, 33),  /* 11 Green*   */
+	SIXEL_XRGB(60, 33, 60),  /* 12 Magenta* */
+	SIXEL_XRGB(33, 60, 60),  /* 13 Cyan*    */
+	SIXEL_XRGB(60, 60, 33),  /* 14 Yellow*  */
+	SIXEL_XRGB(80, 80, 80),  /* 15 Gray 75% */
+};
+
+/*
+ * Primary color hues:
+ *  blue:    0 degrees
+ *  red:   120 degrees
+ *  green: 240 degrees
+ */
+int
+hls_to_rgb(int hue, int lum, int sat)
+{
+	double min, max;
+	int r, g, b;
+
+	if (sat == 0) {
+		r = g = b = lum;
+	}
+
+	/* https://wikimedia.org/api/rest_v1/media/math/render/svg/17e876f7e3260ea7fed73f69e19c71eb715dd09d */
+	max = lum + sat * (1.0 - (lum > 50 ? (((lum << 2) / 100.0) - 1.0): - (2 * (lum / 100.0) - 1.0))) / 2.0;
+
+	/* https://wikimedia.org/api/rest_v1/media/math/render/svg/f6721b57985ad83db3d5b800dc38c9980eedde1d */
+	min = lum - sat * (1.0 - (lum > 50 ? (((lum << 2) / 100.0) - 1.0): - (2 * (lum / 100.0) - 1.0))) / 2.0;
+
+	/* sixel hue color ring is roteted -120 degree from nowdays general one. */
+	hue = (hue + 240) % 360;
+
+	/* https://wikimedia.org/api/rest_v1/media/math/render/svg/937e8abdab308a22ff99de24d645ec9e70f1e384 */
+	switch (hue / 60) {
+	case 0:  /* 0 <= hue < 60 */
+		r = max;
+		g = (min + (max - min) * (hue / 60.0));
+		b = min;
+		break;
+	case 1:  /* 60 <= hue < 120 */
+		r = min + (max - min) * ((120 - hue) / 60.0);
+		g = max;
+		b = min;
+		break;
+	case 2:  /* 120 <= hue < 180 */
+		r = min;
+		g = max;
+		b = (min + (max - min) * ((hue - 120) / 60.0));
+		break;
+	case 3:  /* 180 <= hue < 240 */
+		r = min;
+		g = (min + (max - min) * ((240 - hue) / 60.0));
+		b = max;
+		break;
+	case 4:  /* 240 <= hue < 300 */
+		r = (min + (max - min) * ((hue - 240) / 60.0));
+		g = min;
+		b = max;
+		break;
+	case 5:  /* 300 <= hue < 360 */
+		r = max;
+		g = min;
+		b = (min + (max - min) * ((360 - hue) / 60.0));
+		break;
+	default:
+		break;
+	}
+
+	return SIXEL_XRGB(r, g, b);
+}
+
+static int
+set_default_color(sixel_image_t *image)
+{
+	int i;
+	int n;
+	int r;
+	int g;
+	int b;
+
+	/* palette initialization */
+	for (n = 1; n < 17; n++) {
+		image->palette[n] = sixel_default_color_table[n - 1];
+	}
+
+	/* colors 17-232 are a 6x6x6 color cube */
+	for (r = 0; r < 6; r++) {
+		for (g = 0; g < 6; g++) {
+			for (b = 0; b < 6; b++) {
+				image->palette[n++] = SIXEL_XRGB(r * 51, g * 51, b * 51);
+			}
+		}
+	}
+
+	/* colors 233-256 are a grayscale ramp, intentionally leaving out */
+	for (i = 0; i < 24; i++) {
+		image->palette[n++] = SIXEL_XRGB(i * 11, i * 11, i * 11);
+	}
+
+	for (; n < DECSIXEL_PALETTE_MAX; n++) {
+		image->palette[n] = SIXEL_XRGB(255, 255, 255);
+	}
+
+	return (0);
+}
+
+static int
+sixel_image_init(
+    sixel_image_t    *image,
+    int              width,
+    int              height,
+    int              fgcolor,
+    int              bgcolor,
+    int              use_private_register)
+{
+	int status = (-1);
+	size_t size;
+
+	size = (size_t)(width * height) * sizeof(sixel_color_no_t);
+	image->width = width;
+	image->height = height;
+	image->data = (sixel_color_no_t *)malloc(size);
+	image->ncolors = 2;
+	image->use_private_register = use_private_register;
+
+	if (image->data == NULL) {
+		status = (-1);
+		goto end;
+	}
+	memset(image->data, 0, size);
+
+	image->palette[0] = bgcolor;
+
+	if (image->use_private_register)
+		image->palette[1] = fgcolor;
+
+	image->palette_modified = 0;
+
+	status = (0);
+
+end:
+	return status;
+}
+
+
+static int
+image_buffer_resize(
+    sixel_image_t   *image,
+    int              width,
+    int              height)
+{
+	int status = (-1);
+	size_t size;
+	sixel_color_no_t *alt_buffer;
+	int n;
+	int min_height;
+
+	size = (size_t)(width * height) * sizeof(sixel_color_no_t);
+	alt_buffer = (sixel_color_no_t *)malloc(size);
+	if (alt_buffer == NULL) {
+		/* free source image */
+		free(image->data);
+		image->data = NULL;
+		status = (-1);
+		goto end;
+	}
+
+	min_height = height > image->height ? image->height: height;
+	if (width > image->width) {  /* if width is extended */
+		for (n = 0; n < min_height; ++n) {
+			/* copy from source image */
+			memcpy(alt_buffer + width * n,
+			       image->data + image->width * n,
+			       (size_t)image->width * sizeof(sixel_color_no_t));
+			/* fill extended area with background color */
+			memset(alt_buffer + width * n + image->width,
+			       0,
+			       (size_t)(width - image->width) * sizeof(sixel_color_no_t));
+		}
+	} else {
+		for (n = 0; n < min_height; ++n) {
+			/* copy from source image */
+			memcpy(alt_buffer + width * n,
+			       image->data + image->width * n,
+			       (size_t)width * sizeof(sixel_color_no_t));
+		}
+	}
+
+	if (height > image->height) {  /* if height is extended */
+		/* fill extended area with background color */
+		memset(alt_buffer + width * image->height,
+		       0,
+		       (size_t)(width * (height - image->height)) * sizeof(sixel_color_no_t));
+	}
+
+	/* free source image */
+	free(image->data);
+
+	image->data = alt_buffer;
+	image->width = width;
+	image->height = height;
+
+	status = (0);
+
+end:
+	return status;
+}
+
+static void
+sixel_image_deinit(sixel_image_t *image)
+{
+	free(image->data);
+	image->data = NULL;
+}
+
+int
+sixel_parser_init(sixel_state_t *st,
+                  colour fgcolor, colour bgcolor,
+                  unsigned char use_private_register,
+                  int cell_width, int cell_height)
+{
+	int status = (-1);
+
+	st->state = PS_DECSIXEL;
+	st->pos_x = 0;
+	st->pos_y = 0;
+	st->max_x = 0;
+	st->max_y = 0;
+	st->attributed_pan = 2;
+	st->attributed_pad = 1;
+	st->attributed_ph = 0;
+	st->attributed_pv = 0;
+	st->repeat_count = 1;
+	st->color_index = 16;
+	st->grid_width = cell_width;
+	st->grid_height = cell_height;
+	st->nparams = 0;
+	st->param = 0;
+
+	/* buffer initialization */
+	status = sixel_image_init(&st->image, 1, 1, fgcolor, bgcolor, use_private_register);
+
+	return status;
+}
+
+int
+sixel_parser_set_default_color(sixel_state_t *st)
+{
+	return set_default_color(&st->image);
+}
+
+int
+sixel_parser_finalize(sixel_state_t *st, unsigned char *pixels)
+{
+	int status = (-1);
+	int sx;
+	int sy;
+	sixel_image_t *image = &st->image;
+	int x, y;
+	sixel_color_no_t *src;
+	unsigned char *dst;
+	int color;
+
+	if (++st->max_x < st->attributed_ph)
+		st->max_x = st->attributed_ph;
+
+	if (++st->max_y < st->attributed_pv)
+		st->max_y = st->attributed_pv;
+
+	sx = (st->max_x + st->grid_width - 1) / st->grid_width * st->grid_width;
+	sy = (st->max_y + st->grid_height - 1) / st->grid_height * st->grid_height;
+
+	if (image->width > sx || image->height > sy) {
+		status = image_buffer_resize(image, sx, sy);
+		if (status < 0)
+			goto end;
+	}
+
+	if (image->use_private_register && image->ncolors > 2 && !image->palette_modified) {
+		status = set_default_color(image);
+		if (status < 0)
+			goto end;
+	}
+
+	src = st->image.data;
+	dst = pixels;
+	for (y = 0; y < st->image.height; ++y) {
+		for (x = 0; x < st->image.width; ++x) {
+			color = st->image.palette[*src++];
+			*dst++ = color >> 16 & 0xff;   /* b */
+			*dst++ = color >> 8 & 0xff;    /* g */
+			*dst++ = color >> 0 & 0xff;    /* r */
+			dst++;                         /* a */
+		}
+		/* fill right padding with bgcolor */
+		for (; x < st->image.width; ++x) {
+			color = st->image.palette[0];  /* bgcolor */
+			*dst++ = color >> 16 & 0xff;   /* b */
+			*dst++ = color >> 8 & 0xff;    /* g */
+			*dst++ = color >> 0 & 0xff;    /* r */
+			dst++;                         /* a */
+		}
+	}
+	/* fill bottom padding with bgcolor */
+	for (; y < st->image.height; ++y) {
+		for (x = 0; x < st->image.width; ++x) {
+			color = st->image.palette[0];  /* bgcolor */
+			*dst++ = color >> 16 & 0xff;   /* b */
+			*dst++ = color >> 8 & 0xff;    /* g */
+			*dst++ = color >> 0 & 0xff;    /* r */
+			dst++;                         /* a */
+		}
+	}
+
+	status = (0);
+
+end:
+	return status;
+}
+
+/* convert sixel data into indexed pixel bytes and palette data */
+int
+sixel_parser_parse(sixel_state_t *st, unsigned char *p, size_t len)
+{
+	int status = (-1);
+	int n;
+	int i;
+	int x;
+	int y;
+	int bits;
+	int sixel_vertical_mask;
+	int sx;
+	int sy;
+	int c;
+	int pos;
+	unsigned char *p0 = p;
+	sixel_image_t *image = &st->image;
+
+	if (! image->data)
+		goto end;
+
+	while (p < p0 + len) {
+		switch (st->state) {
+		case PS_ESC:
+			goto end;
+
+		case PS_DECSIXEL:
+			switch (*p) {
+			case '\x1b':
+				st->state = PS_ESC;
+				p++;
+				break;
+			case '"':
+				st->param = 0;
+				st->nparams = 0;
+				st->state = PS_DECGRA;
+				p++;
+				break;
+			case '!':
+				st->param = 0;
+				st->nparams = 0;
+				st->state = PS_DECGRI;
+				p++;
+				break;
+			case '#':
+				st->param = 0;
+				st->nparams = 0;
+				st->state = PS_DECGCI;
+				p++;
+				break;
+			case '$':
+				/* DECGCR Graphics Carriage Return */
+				st->pos_x = 0;
+				p++;
+				break;
+			case '-':
+				/* DECGNL Graphics Next Line */
+				st->pos_x = 0;
+				if (st->pos_y < DECSIXEL_HEIGHT_MAX - 5 - 6)
+					st->pos_y += 6;
+				else
+					st->pos_y = DECSIXEL_HEIGHT_MAX + 1;
+				p++;
+				break;
+			default:
+				if (*p >= '?' && *p <= '~') {  /* sixel characters */
+					if ((image->width < (st->pos_x + st->repeat_count) || image->height < (st->pos_y + 6))
+					        && image->width < DECSIXEL_WIDTH_MAX && image->height < DECSIXEL_HEIGHT_MAX) {
+						sx = image->width * 2;
+						sy = image->height * 2;
+						while (sx < (st->pos_x + st->repeat_count) || sy < (st->pos_y + 6)) {
+							sx *= 2;
+							sy *= 2;
+						}
+
+						if (sx > DECSIXEL_WIDTH_MAX)
+							sx = DECSIXEL_WIDTH_MAX;
+						if (sy > DECSIXEL_HEIGHT_MAX)
+							sy = DECSIXEL_HEIGHT_MAX;
+
+						status = image_buffer_resize(image, sx, sy);
+						if (status < 0)
+							goto end;
+					}
+
+					if (st->color_index > image->ncolors)
+						image->ncolors = st->color_index;
+
+					if (st->pos_x + st->repeat_count > image->width)
+						st->repeat_count = image->width - st->pos_x;
+
+					if (st->repeat_count > 0 && st->pos_y - 5 < image->height) {
+						bits = *p - '?';
+						if (bits != 0) {
+							sixel_vertical_mask = 0x01;
+							if (st->repeat_count <= 1) {
+								for (i = 0; i < 6; i++) {
+									if ((bits & sixel_vertical_mask) != 0) {
+										pos = image->width * (st->pos_y + i) + st->pos_x;
+										image->data[pos] = st->color_index;
+										if (st->max_x < st->pos_x)
+											st->max_x = st->pos_x;
+										if (st->max_y < (st->pos_y + i))
+											st->max_y = st->pos_y + i;
+									}
+									sixel_vertical_mask <<= 1;
+								}
+							} else {
+								/* st->repeat_count > 1 */
+								for (i = 0; i < 6; i++) {
+									if ((bits & sixel_vertical_mask) != 0) {
+										c = sixel_vertical_mask << 1;
+										for (n = 1; (i + n) < 6; n++) {
+											if ((bits & c) == 0)
+												break;
+											c <<= 1;
+										}
+										for (y = st->pos_y + i; y < st->pos_y + i + n; ++y) {
+											for (x = st->pos_x; x < st->pos_x + st->repeat_count; ++x)
+												image->data[image->width * y + x] = st->color_index;
+										}
+										if (st->max_x < (st->pos_x + st->repeat_count - 1))
+											st->max_x = st->pos_x + st->repeat_count - 1;
+										if (st->max_y < (st->pos_y + i + n - 1))
+											st->max_y = st->pos_y + i + n - 1;
+										i += (n - 1);
+										sixel_vertical_mask <<= (n - 1);
+									}
+									sixel_vertical_mask <<= 1;
+								}
+							}
+						}
+					}
+					if (st->repeat_count > 0)
+						st->pos_x += st->repeat_count;
+					st->repeat_count = 1;
+				}
+				p++;
+				break;
+			}
+			break;
+
+		case PS_DECGRA:
+			/* DECGRA Set Raster Attributes " Pan; Pad; Ph; Pv */
+			switch (*p) {
+			case '\x1b':
+				st->state = PS_ESC;
+				p++;
+				break;
+			case '0':
+			case '1':
+			case '2':
+			case '3':
+			case '4':
+			case '5':
+			case '6':
+			case '7':
+			case '8':
+			case '9':
+				st->param = st->param * 10 + *p - '0';
+				if (st->param > DECSIXEL_PARAMVALUE_MAX)
+					st->param = DECSIXEL_PARAMVALUE_MAX;
+				p++;
+				break;
+			case ';':
+				if (st->nparams < DECSIXEL_PARAMS_MAX)
+					st->params[st->nparams++] = st->param;
+				st->param = 0;
+				p++;
+				break;
+			default:
+				if (st->nparams < DECSIXEL_PARAMS_MAX)
+					st->params[st->nparams++] = st->param;
+				if (st->nparams > 0)
+					st->attributed_pad = st->params[0];
+				if (st->nparams > 1)
+					st->attributed_pan = st->params[1];
+				if (st->nparams > 2 && st->params[2] > 0)
+					st->attributed_ph = st->params[2];
+				if (st->nparams > 3 && st->params[3] > 0)
+					st->attributed_pv = st->params[3];
+
+				if (st->attributed_pan <= 0)
+					st->attributed_pan = 1;
+				if (st->attributed_pad <= 0)
+					st->attributed_pad = 1;
+
+				if (image->width < st->attributed_ph ||
+					image->height < st->attributed_pv) {
+					sx = st->attributed_ph;
+					if (image->width > st->attributed_ph)
+						sx = image->width;
+
+					sy = st->attributed_pv;
+					if (image->height > st->attributed_pv)
+						sy = image->height;
+
+					sx = (sx + st->grid_width - 1) / st->grid_width * st->grid_width;
+					sy = (sy + st->grid_height - 1) / st->grid_height * st->grid_height;
+
+					if (sx > DECSIXEL_WIDTH_MAX)
+						sx = DECSIXEL_WIDTH_MAX;
+					if (sy > DECSIXEL_HEIGHT_MAX)
+						sy = DECSIXEL_HEIGHT_MAX;
+
+					status = image_buffer_resize(image, sx, sy);
+					if (status < 0)
+						goto end;
+				}
+				st->state = PS_DECSIXEL;
+				st->param = 0;
+				st->nparams = 0;
+			}
+			break;
+
+		case PS_DECGRI:
+			/* DECGRI Graphics Repeat Introducer ! Pn Ch */
+			switch (*p) {
+			case '\x1b':
+				st->state = PS_ESC;
+				p++;
+				break;
+			case '0':
+			case '1':
+			case '2':
+			case '3':
+			case '4':
+			case '5':
+			case '6':
+			case '7':
+			case '8':
+			case '9':
+				st->param = st->param * 10 + *p - '0';
+				if (st->param > DECSIXEL_PARAMVALUE_MAX)
+					st->param = DECSIXEL_PARAMVALUE_MAX;
+				p++;
+				break;
+			default:
+				st->repeat_count = st->param;
+				if (st->repeat_count == 0)
+					st->repeat_count = 1;
+				st->state = PS_DECSIXEL;
+				st->param = 0;
+				st->nparams = 0;
+				break;
+			}
+			break;
+
+		case PS_DECGCI:
+			/* DECGCI Graphics Color Introducer # Pc; Pu; Px; Py; Pz */
+			switch (*p) {
+			case '\x1b':
+				st->state = PS_ESC;
+				p++;
+				break;
+			case '0':
+			case '1':
+			case '2':
+			case '3':
+			case '4':
+			case '5':
+			case '6':
+			case '7':
+			case '8':
+			case '9':
+				st->param = st->param * 10 + *p - '0';
+				if (st->param > DECSIXEL_PARAMVALUE_MAX)
+					st->param = DECSIXEL_PARAMVALUE_MAX;
+				p++;
+				break;
+			case ';':
+				if (st->nparams < DECSIXEL_PARAMS_MAX)
+					st->params[st->nparams++] = st->param;
+				st->param = 0;
+				p++;
+				break;
+			default:
+				st->state = PS_DECSIXEL;
+				if (st->nparams < DECSIXEL_PARAMS_MAX)
+					st->params[st->nparams++] = st->param;
+				st->param = 0;
+
+				if (st->nparams > 0) {
+					st->color_index = 1 + st->params[0];  /* offset 1(background color) added */
+					if (st->color_index < 0)
+						st->color_index = 0;
+					else if (st->color_index >= DECSIXEL_PALETTE_MAX)
+						st->color_index = DECSIXEL_PALETTE_MAX - 1;
+				}
+
+				if (st->nparams > 4) {
+					st->image.palette_modified = 1;
+					if (st->params[1] == 1) {
+						/* HLS */
+						if (st->params[2] > 360)
+							st->params[2] = 360;
+						if (st->params[3] > 100)
+							st->params[3] = 100;
+						if (st->params[4] > 100)
+							st->params[4] = 100;
+						image->palette[st->color_index]
+						    = hls_to_rgb(st->params[2], st->params[3], st->params[4]);
+					} else if (st->params[1] == 2) {
+						/* RGB */
+						if (st->params[2] > 100)
+							st->params[2] = 100;
+						if (st->params[3] > 100)
+							st->params[3] = 100;
+						if (st->params[4] > 100)
+							st->params[4] = 100;
+						image->palette[st->color_index]
+						    = SIXEL_XRGB(st->params[2], st->params[3], st->params[4]);
+					}
+				}
+				break;
+			}
+			break;
+		default:
+			break;
+		}
+	}
+
+	status = (0);
+
+end:
+	return status;
+}
+
+void
+sixel_parser_deinit(sixel_state_t *st)
+{
+	if (st)
+		sixel_image_deinit(&st->image);
+}
--- /dev/null
+++ src/sixel.h
@@ -0,0 +1,61 @@
+#ifndef SIXEL_H
+#define SIXEL_H
+
+#include "config.h"
+
+#define DECSIXEL_PARAMS_MAX 16
+#define DECSIXEL_PALETTE_MAX 65535
+#define DECSIXEL_PARAMVALUE_MAX 65535
+#define DECSIXEL_WIDTH_MAX 4096
+#define DECSIXEL_HEIGHT_MAX 4096
+
+typedef unsigned short sixel_color_no_t;
+typedef unsigned int colour;
+
+typedef struct sixel_image_buffer {
+	sixel_color_no_t *data;
+	int width;
+	int height;
+	colour palette[DECSIXEL_PALETTE_MAX];
+	sixel_color_no_t ncolors;
+	int palette_modified;
+	int use_private_register;
+} sixel_image_t;
+
+typedef enum parse_state {
+	PS_GROUND     = 0,
+	PS_ESC        = 1,  /* ESC */
+	PS_DECSIXEL   = 2,  /* DECSIXEL body part ", $, -, ? ... ~ */
+	PS_DECGRA     = 3,  /* DECGRA Set Raster Attributes " Pan; Pad; Ph; Pv */
+	PS_DECGRI     = 4,  /* DECGRI Graphics Repeat Introducer ! Pn Ch */
+	PS_DECGCI     = 5,  /* DECGCI Graphics Color Introducer # Pc; Pu; Px; Py; Pz */
+} parse_state_t;
+
+typedef struct parser_context {
+	parse_state_t state;
+	int pos_x;
+	int pos_y;
+	int max_x;
+	int max_y;
+	int attributed_pan;
+	int attributed_pad;
+	int attributed_ph;
+	int attributed_pv;
+	int repeat_count;
+	int color_index;
+	int bgindex;
+	int grid_width;
+	int grid_height;
+	int param;
+	int nparams;
+	int params[DECSIXEL_PARAMS_MAX];
+	sixel_image_t image;
+} sixel_state_t;
+
+int sixel_parser_init(sixel_state_t *st, colour fgcolor, colour bgcolor, unsigned char use_private_register, int cell_width, int cell_height);
+int sixel_parser_parse(sixel_state_t *st, unsigned char *p, size_t len);
+int sixel_parser_set_default_color(sixel_state_t *st);
+int sixel_parser_finalize(sixel_state_t *st, unsigned char *pixels);
+void sixel_parser_deinit(sixel_state_t *st);
+
+#endif