comparison tools/gfxconv.c @ 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
comparison
equal deleted inserted replaced
1864:897250732bfd 1865:3e830bcb001a
100 optSequential = FALSE, 100 optSequential = FALSE,
101 optRemapColors = FALSE, 101 optRemapColors = FALSE,
102 optRemapRemove = FALSE; 102 optRemapRemove = FALSE;
103 int optNRemapTable = 0; 103 int optNRemapTable = 0;
104 DMMapValue optRemapTable[DM_MAX_COLORS]; 104 DMMapValue optRemapTable[DM_MAX_COLORS];
105 int optColors[C64_NCOLORS]; 105 int optColorMap[C64_NCOLORS];
106 106
107 DMImageConvSpec optSpec = 107 DMImageConvSpec optSpec =
108 { 108 {
109 .scaleX = 1, 109 .scaleX = 1,
110 .scaleY = 1, 110 .scaleY = 1,
219 "\n" 219 "\n"
220 ); 220 );
221 } 221 }
222 222
223 223
224 const char *dmC64GetANSIFromC64Color(const int col)
225 {
226 switch (col)
227 {
228 case 0: return "0;30"; // Black
229 case 1: return "0;1;37"; // White
230 case 2: return "0;31"; // Red
231 case 3: return "0;36";
232 case 4: return "0;35";
233 case 5: return "0;32";
234 case 6: return "0;34";
235 case 7: return "0;1;33";
236 case 8: return "0;33";
237 case 9: return "0;31";
238 case 10: return "0;1;31";
239 case 11: return "0;1;30";
240 case 12: return "0;1;30";
241 case 13: return "0;1;32";
242 case 14: return "0;1;34";
243 case 15: return "0;37";
244
245 default: return "0";
246 }
247 }
248
249
224 int dmGetConvFormat(const int format, const int subformat) 250 int dmGetConvFormat(const int format, const int subformat)
225 { 251 {
226 for (int i = 0; i < nconvFormatList; i++) 252 for (int i = 0; i < nconvFormatList; i++)
227 { 253 {
228 const DMConvFormat *fmt = &convFormatList[i]; 254 const DMConvFormat *fmt = &convFormatList[i];
547 break; 573 break;
548 574
549 case 6: 575 case 6:
550 { 576 {
551 int index, ncolors; 577 int index, ncolors;
552 if (!dmParseMapOptionString(optArg, optColors, 578 if (!dmParseMapOptionString(optArg, optColorMap,
553 &ncolors, C64_NCOLORS, FALSE, "color table option")) 579 &ncolors, C64_NCOLORS, FALSE, "color table option"))
554 return FALSE; 580 return FALSE;
555 581
556 dmMsg(1, "Set color table: "); 582 dmMsg(1, "Set color table: ");
557 for (index = 0; index < ncolors; index++) 583 for (index = 0; index < ncolors; index++)
558 { 584 {
559 dmPrint(1, "[%d:%d]%s", 585 dmPrint(1, "[%d:%d]%s",
560 index, optColors[index], 586 index, optColorMap[index],
561 (index < ncolors) ? ", " : ""); 587 (index < ncolors) ? ", " : "");
562 } 588 }
563 dmPrint(1, "\n"); 589 dmPrint(1, "\n");
564 } 590 }
565 break; 591 break;
751 case FFMT_ASCII: 777 case FFMT_ASCII:
752 ch = dmASCIIPalette[val]; 778 ch = dmASCIIPalette[val];
753 fprintf(out, "%c%c", ch, ch); 779 fprintf(out, "%c%c", ch, ch);
754 break; 780 break;
755 case FFMT_ANSI: 781 case FFMT_ANSI:
756 fprintf(out, "%c[0;%d;%dm##%c[0m", 782 fprintf(out, "\x1b[%sm##\x1b[0m",
757 0x1b, 783 dmC64GetANSIFromC64Color(optColorMap[val]));
758 1,
759 31 + optColors[val],
760 0x1b);
761 break; 784 break;
762 } 785 }
763 } 786 }
764 } 787 }
765 else 788 else
766 { 789 {
767 for (i = DM_ASC_NBITS; i; i--) 790 for (i = DM_ASC_NBITS; i; i--)
768 { 791 {
769 int val = (byte & (1ULL << (i - 1))) >> (i - 1); 792 int val = (byte & (1ULL << (i - 1))) >> (i - 1);
770 char ch;
771 switch (format) 793 switch (format)
772 { 794 {
773 case FFMT_ASCII: 795 case FFMT_ASCII:
774 ch = val ? '#' : '.'; 796 fputc(val ? '#' : '.', out);
775 fputc(ch, out);
776 break; 797 break;
777 case FFMT_ANSI: 798 case FFMT_ANSI:
778 fprintf(out, "%c[0;%d;%dm %c[0m", 799 fprintf(out, "\x1b[%sm#\x1b[0m",
779 0x1b, 800 dmC64GetANSIFromC64Color(optColorMap[val]));
780 1,
781 31 + optColors[val],
782 0x1b);
783 break; 801 break;
784 } 802 }
785 } 803 }
786 } 804 }
787 } 805 }
1565 dmSetDefaultC64Palette(outImage); 1583 dmSetDefaultC64Palette(outImage);
1566 1584
1567 while (offs + outSize < dataSize && (optItemCount < 0 || itemCount < optItemCount)) 1585 while (offs + outSize < dataSize && (optItemCount < 0 || itemCount < optItemCount))
1568 { 1586 {
1569 if ((err = dmC64ConvertCSDataToImage(outImage, outX * outWidthPX, outY * outHeight, 1587 if ((err = dmC64ConvertCSDataToImage(outImage, outX * outWidthPX, outY * outHeight,
1570 dataBuf + offs, outWidth, outHeight, optInMulticolor, optColors)) != DMERR_OK) 1588 dataBuf + offs, outWidth, outHeight, optInMulticolor, optColorMap)) != DMERR_OK)
1571 { 1589 {
1572 dmErrorMsg("Internal error in conversion of raw data to bitmap: %d.\n", err); 1590 dmErrorMsg("Internal error in conversion of raw data to bitmap: %d.\n", err);
1573 break; 1591 break;
1574 } 1592 }
1575 1593
1645 DMC64Image *inC64Image = NULL, *outC64Image = NULL; 1663 DMC64Image *inC64Image = NULL, *outC64Image = NULL;
1646 Uint8 *dataBuf = NULL, *dataBufOrig = NULL; 1664 Uint8 *dataBuf = NULL, *dataBufOrig = NULL;
1647 size_t dataSize, dataSizeOrig; 1665 size_t dataSize, dataSizeOrig;
1648 int i, n; 1666 int i, n;
1649 1667
1650 // Default colors 1668 // Default color mapping
1651 for (i = 0; i < C64_NCOLORS; i++) 1669 for (i = 0; i < C64_NCOLORS; i++)
1652 optColors[i] = i; 1670 optColorMap[i] = i;
1653 1671
1654 // Initialize list of additional conversion formats 1672 // Initialize list of additional conversion formats
1655 dmC64InitializeFormats(); 1673 dmC64InitializeFormats();
1656 nconvFormatList = ndmImageFormatList + nbaseFormatList; 1674 nconvFormatList = ndmImageFormatList + nbaseFormatList;
1657 convFormatList = dmCalloc(nconvFormatList, sizeof(DMConvFormat)); 1675 convFormatList = dmCalloc(nconvFormatList, sizeof(DMConvFormat));