changeset 1865:3e830bcb001a

Improve color map and ANSI output support.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 24 Jun 2018 17:05:07 +0300
parents 897250732bfd
children 734234e7ca95
files tools/gfxconv.c
diffstat 1 files changed, 37 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/tools/gfxconv.c	Sun Jun 24 17:04:21 2018 +0300
+++ b/tools/gfxconv.c	Sun Jun 24 17:05:07 2018 +0300
@@ -102,7 +102,7 @@
         optRemapRemove = FALSE;
 int     optNRemapTable = 0;
 DMMapValue optRemapTable[DM_MAX_COLORS];
-int     optColors[C64_NCOLORS];
+int     optColorMap[C64_NCOLORS];
 
 DMImageConvSpec optSpec =
 {
@@ -221,6 +221,32 @@
 }
 
 
+const char *dmC64GetANSIFromC64Color(const int col)
+{
+    switch (col)
+    {
+        case  0: return "0;30";    // Black
+        case  1: return "0;1;37";  // White
+        case  2: return "0;31";    // Red
+        case  3: return "0;36";
+        case  4: return "0;35";
+        case  5: return "0;32";
+        case  6: return "0;34";
+        case  7: return "0;1;33";
+        case  8: return "0;33";
+        case  9: return "0;31";
+        case 10: return "0;1;31";
+        case 11: return "0;1;30";
+        case 12: return "0;1;30";
+        case 13: return "0;1;32";
+        case 14: return "0;1;34";
+        case 15: return "0;37";
+
+        default: return "0";
+    }
+}
+
+
 int dmGetConvFormat(const int format, const int subformat)
 {
     for (int i = 0; i < nconvFormatList; i++)
@@ -549,7 +575,7 @@
         case 6:
             {
                 int index, ncolors;
-                if (!dmParseMapOptionString(optArg, optColors,
+                if (!dmParseMapOptionString(optArg, optColorMap,
                     &ncolors, C64_NCOLORS, FALSE, "color table option"))
                     return FALSE;
 
@@ -557,7 +583,7 @@
                 for (index = 0; index < ncolors; index++)
                 {
                     dmPrint(1, "[%d:%d]%s",
-                        index, optColors[index],
+                        index, optColorMap[index],
                         (index < ncolors) ? ", " : "");
                 }
                 dmPrint(1, "\n");
@@ -753,11 +779,8 @@
                     fprintf(out, "%c%c", ch, ch);
                     break;
                 case FFMT_ANSI:
-                    fprintf(out, "%c[0;%d;%dm##%c[0m",
-                        0x1b,
-                        1,
-                        31 + optColors[val],
-                        0x1b);
+                    fprintf(out, "\x1b[%sm##\x1b[0m",
+                        dmC64GetANSIFromC64Color(optColorMap[val]));
                     break;
             }
         }
@@ -767,19 +790,14 @@
         for (i = DM_ASC_NBITS; i; i--)
         {
             int val = (byte & (1ULL << (i - 1))) >> (i - 1);
-            char ch;
             switch (format)
             {
                 case FFMT_ASCII:
-                    ch = val ? '#' : '.';
-                    fputc(ch, out);
+                    fputc(val ? '#' : '.', out);
                     break;
                 case FFMT_ANSI:
-                    fprintf(out, "%c[0;%d;%dm %c[0m",
-                        0x1b,
-                        1,
-                        31 + optColors[val],
-                        0x1b);
+                    fprintf(out, "\x1b[%sm#\x1b[0m",
+                        dmC64GetANSIFromC64Color(optColorMap[val]));
                     break;
             }
         }
@@ -1567,7 +1585,7 @@
         while (offs + outSize < dataSize && (optItemCount < 0 || itemCount < optItemCount))
         {
             if ((err = dmC64ConvertCSDataToImage(outImage, outX * outWidthPX, outY * outHeight,
-                dataBuf + offs, outWidth, outHeight, optInMulticolor, optColors)) != DMERR_OK)
+                dataBuf + offs, outWidth, outHeight, optInMulticolor, optColorMap)) != DMERR_OK)
             {
                 dmErrorMsg("Internal error in conversion of raw data to bitmap: %d.\n", err);
                 break;
@@ -1647,9 +1665,9 @@
     size_t dataSize, dataSizeOrig;
     int i, n;
 
-    // Default colors
+    // Default color mapping
     for (i = 0; i < C64_NCOLORS; i++)
-        optColors[i] = i;
+        optColorMap[i] = i;
 
     // Initialize list of additional conversion formats
     dmC64InitializeFormats();