comparison tools/gfxconv.c @ 2586:9807ae37ad69

Require stdbool.h, we require C11 now.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 08 Dec 2022 15:59:22 +0200
parents bb44c48cffac
children d683f47e4dd9
comparison
equal deleted inserted replaced
2585:ef6c826c5b7a 2586:9807ae37ad69
114 static int nconvFormatList = 0; 114 static int nconvFormatList = 0;
115 115
116 116
117 typedef struct 117 typedef struct
118 { 118 {
119 BOOL triplet, alpha; 119 bool triplet, alpha;
120 DMColor color; 120 DMColor color;
121 unsigned int from, to; 121 unsigned int from, to;
122 } DMMapValue; 122 } DMMapValue;
123 123
124 124
134 optPlanedWidth = 1, 134 optPlanedWidth = 1,
135 optForcedInSubFormat = -1, 135 optForcedInSubFormat = -1,
136 optShowHelp = 0; 136 optShowHelp = 0;
137 137
138 unsigned int optInSkip = 0; 138 unsigned int optInSkip = 0;
139 BOOL optInSkipNeg = FALSE; 139 bool optInSkipNeg = false;
140 140
141 int optCropMode = CROP_NONE, 141 int optCropMode = CROP_NONE,
142 optCropX0, optCropY0, 142 optCropX0, optCropY0,
143 optCropW, optCropH; 143 optCropW, optCropH;
144 144
145 BOOL optInMulticolor = FALSE, 145 bool optInMulticolor = false,
146 optSequential = FALSE, 146 optSequential = false,
147 optRemapRemove = FALSE, 147 optRemapRemove = false,
148 optRemapMatchAlpha = FALSE, 148 optRemapMatchAlpha = false,
149 optUsePalette = FALSE; 149 optUsePalette = false;
150 int optRemapMode = REMAP_NONE; 150 int optRemapMode = REMAP_NONE;
151 int optNRemapTable = 0, 151 int optNRemapTable = 0,
152 optScaleMode = SCALE_AUTO; 152 optScaleMode = SCALE_AUTO;
153 float optRemapMaxDist = -1; 153 float optRemapMaxDist = -1;
154 int optRemapNoMatchColor = -1; 154 int optRemapNoMatchColor = -1;
164 { 164 {
165 .scaleX = 1, 165 .scaleX = 1,
166 .scaleY = 1, 166 .scaleY = 1,
167 .nplanes = 0, 167 .nplanes = 0,
168 .bpp = 8, 168 .bpp = 8,
169 .planar = FALSE, 169 .planar = false,
170 .pixfmt = 0, 170 .pixfmt = 0,
171 .compression = FCMP_BEST, 171 .compression = FCMP_BEST,
172 }; 172 };
173 173
174 DMC64ImageConvSpec optC64Spec; 174 DMC64ImageConvSpec optC64Spec;
400 default: return "0"; 400 default: return "0";
401 } 401 }
402 } 402 }
403 403
404 404
405 BOOL dmGetConvFormat(const int type, const int format, DMConvFormat *pfmt) 405 bool dmGetConvFormat(const int type, const int format, DMConvFormat *pfmt)
406 { 406 {
407 for (int i = 0; i < nconvFormatList; i++) 407 for (int i = 0; i < nconvFormatList; i++)
408 { 408 {
409 const DMConvFormat *fmt = &convFormatList[i]; 409 const DMConvFormat *fmt = &convFormatList[i];
410 if (fmt->type == type && 410 if (fmt->type == type &&
411 fmt->format == format) 411 fmt->format == format)
412 { 412 {
413 memcpy(pfmt, fmt, sizeof(DMConvFormat)); 413 memcpy(pfmt, fmt, sizeof(DMConvFormat));
414 return TRUE; 414 return true;
415 } 415 }
416 } 416 }
417 417
418 for (int i = 0; i < nconvFormatList; i++) 418 for (int i = 0; i < nconvFormatList; i++)
419 { 419 {
422 { 422 {
423 const DMConvFormat *fmt = &convFormatList[i]; 423 const DMConvFormat *fmt = &convFormatList[i];
424 const DMC64ImageFormat *cfmt = &dmC64ImageFormats[format]; 424 const DMC64ImageFormat *cfmt = &dmC64ImageFormats[format];
425 memcpy(pfmt, fmt, sizeof(DMConvFormat)); 425 memcpy(pfmt, fmt, sizeof(DMConvFormat));
426 pfmt->fext = cfmt->name; 426 pfmt->fext = cfmt->name;
427 return TRUE; 427 return true;
428 } 428 }
429 } 429 }
430 430
431 return FALSE; 431 return false;
432 } 432 }
433 433
434 434
435 BOOL dmGetC64FormatByExt(const char *fext, int *type, int *format) 435 bool dmGetC64FormatByExt(const char *fext, int *type, int *format)
436 { 436 {
437 if (fext == NULL) 437 if (fext == NULL)
438 return FALSE; 438 return false;
439 439
440 for (int i = 0; i < ndmC64ImageFormats; i++) 440 for (int i = 0; i < ndmC64ImageFormats; i++)
441 { 441 {
442 const DMC64ImageFormat *fmt = &dmC64ImageFormats[i]; 442 const DMC64ImageFormat *fmt = &dmC64ImageFormats[i];
443 if (fmt->fext != NULL && 443 if (fmt->fext != NULL &&
444 strcasecmp(fext, fmt->fext) == 0) 444 strcasecmp(fext, fmt->fext) == 0)
445 { 445 {
446 *type = FFMT_BITMAP; 446 *type = FFMT_BITMAP;
447 *format = i; 447 *format = i;
448 return TRUE; 448 return true;
449 } 449 }
450 } 450 }
451 451
452 return FALSE; 452 return false;
453 } 453 }
454 454
455 455
456 BOOL dmGetFormatByExt(const char *fext, int *type, int *format) 456 bool dmGetFormatByExt(const char *fext, int *type, int *format)
457 { 457 {
458 if (fext == NULL) 458 if (fext == NULL)
459 return FALSE; 459 return false;
460 460
461 for (int i = 0; i < nconvFormatList; i++) 461 for (int i = 0; i < nconvFormatList; i++)
462 { 462 {
463 const DMConvFormat *fmt = &convFormatList[i]; 463 const DMConvFormat *fmt = &convFormatList[i];
464 if (fmt->fext != NULL && 464 if (fmt->fext != NULL &&
465 strcasecmp(fext, fmt->fext) == 0) 465 strcasecmp(fext, fmt->fext) == 0)
466 { 466 {
467 *type = fmt->type; 467 *type = fmt->type;
468 *format = fmt->format; 468 *format = fmt->format;
469 return TRUE; 469 return true;
470 } 470 }
471 } 471 }
472 472
473 return FALSE; 473 return false;
474 } 474 }
475 475
476 476
477 BOOL dmParseMapOptionMapItem(const char *popt, DMMapValue *value, const unsigned int nmax, const char *msg) 477 bool dmParseMapOptionMapItem(const char *popt, DMMapValue *value, const unsigned int nmax, const char *msg)
478 { 478 {
479 char *end, *split, *opt = dm_strdup(popt); 479 char *end, *split, *opt = dm_strdup(popt);
480 480
481 if (opt == NULL) 481 if (opt == NULL)
482 goto out; 482 goto out;
499 unsigned int colR, colG, colB, colA; 499 unsigned int colR, colG, colB, colA;
500 500
501 if (sscanf(opt + 1, "%2x%2x%2x%2x", &colR, &colG, &colB, &colA) == 4 || 501 if (sscanf(opt + 1, "%2x%2x%2x%2x", &colR, &colG, &colB, &colA) == 4 ||
502 sscanf(opt + 1, "%2X%2X%2X%2X", &colR, &colG, &colB, &colA) == 4) 502 sscanf(opt + 1, "%2X%2X%2X%2X", &colR, &colG, &colB, &colA) == 4)
503 { 503 {
504 value->alpha = TRUE; 504 value->alpha = true;
505 value->color.a = colA; 505 value->color.a = colA;
506 } 506 }
507 else 507 else
508 if (sscanf(opt + 1, "%2x%2x%2x", &colR, &colG, &colB) != 3 && 508 if (sscanf(opt + 1, "%2x%2x%2x", &colR, &colG, &colB) != 3 &&
509 sscanf(opt + 1, "%2X%2X%2X", &colR, &colG, &colB) != 3) 509 sscanf(opt + 1, "%2X%2X%2X", &colR, &colG, &colB) != 3)
513 } 513 }
514 514
515 value->color.r = colR; 515 value->color.r = colR;
516 value->color.g = colG; 516 value->color.g = colG;
517 value->color.b = colB; 517 value->color.b = colB;
518 value->triplet = TRUE; 518 value->triplet = true;
519 } 519 }
520 else 520 else
521 { 521 {
522 if (!dmGetIntVal(opt, &value->from, NULL)) 522 if (!dmGetIntVal(opt, &value->from, NULL))
523 { 523 {
524 dmErrorMsg("Invalid %s value '%s', could not parse source value '%s'.\n", msg, popt, opt); 524 dmErrorMsg("Invalid %s value '%s', could not parse source value '%s'.\n", msg, popt, opt);
525 goto out; 525 goto out;
526 } 526 }
527 value->triplet = FALSE; 527 value->triplet = false;
528 } 528 }
529 529
530 // Trim whitespace 530 // Trim whitespace
531 split++; 531 split++;
532 while (*split && isspace(*split)) split++; 532 while (*split && isspace(*split)) split++;
549 dmErrorMsg("Invalid %s map destination color index value %d, must be [0..%d].\n", msg, value->to, nmax); 549 dmErrorMsg("Invalid %s map destination color index value %d, must be [0..%d].\n", msg, value->to, nmax);
550 goto out; 550 goto out;
551 } 551 }
552 552
553 dmFree(opt); 553 dmFree(opt);
554 return TRUE; 554 return true;
555 555
556 out: 556 out:
557 dmFree(opt); 557 dmFree(opt);
558 return FALSE; 558 return false;
559 } 559 }
560 560
561 561
562 BOOL dmParseMapOptionItem(char *opt, char *end, void *pvalue, const int index, const int nmax, const BOOL requireIndex, const char *msg) 562 bool dmParseMapOptionItem(char *opt, char *end, void *pvalue, const int index, const int nmax, const bool requireIndex, const char *msg)
563 { 563 {
564 // Trim whitespace 564 // Trim whitespace
565 if (end != NULL) 565 if (end != NULL)
566 { 566 {
567 *end = 0; 567 *end = 0;
573 // Parse item based on mode 573 // Parse item based on mode
574 if (requireIndex) 574 if (requireIndex)
575 { 575 {
576 DMMapValue *value = (DMMapValue *) pvalue; 576 DMMapValue *value = (DMMapValue *) pvalue;
577 if (!dmParseMapOptionMapItem(opt, &value[index], nmax, msg)) 577 if (!dmParseMapOptionMapItem(opt, &value[index], nmax, msg))
578 return FALSE; 578 return false;
579 } 579 }
580 else 580 else
581 { 581 {
582 unsigned int *value = (unsigned int *) pvalue; 582 unsigned int *value = (unsigned int *) pvalue;
583 char *split = strchr(opt, ':'); 583 char *split = strchr(opt, ':');
584 if (split != NULL) 584 if (split != NULL)
585 { 585 {
586 dmErrorMsg("Unexpected ':' in indexed %s '%s'.\n", msg, opt); 586 dmErrorMsg("Unexpected ':' in indexed %s '%s'.\n", msg, opt);
587 return FALSE; 587 return false;
588 } 588 }
589 589
590 if (!dmGetIntVal(opt, &value[index], NULL)) 590 if (!dmGetIntVal(opt, &value[index], NULL))
591 { 591 {
592 dmErrorMsg("Invalid %s value '%s', could not parse.\n", msg, opt); 592 dmErrorMsg("Invalid %s value '%s', could not parse.\n", msg, opt);
593 return FALSE; 593 return false;
594 } 594 }
595 } 595 }
596 596
597 return TRUE; 597 return true;
598 } 598 }
599 599
600 600
601 BOOL dmParseMapOptionString(char *opt, void *values, int *nvalues, const int nmax, const BOOL requireIndex, const char *msg) 601 bool dmParseMapOptionString(char *opt, void *values, int *nvalues, const int nmax, const bool requireIndex, const char *msg)
602 { 602 {
603 char *start = opt; 603 char *start = opt;
604 604
605 *nvalues = 0; 605 *nvalues = 0;
606 while (*start && *nvalues < nmax) 606 while (*start && *nvalues < nmax)
607 { 607 {
608 char *end = strchr(start, ','); 608 char *end = strchr(start, ',');
609 609
610 if (!dmParseMapOptionItem(start, end, values, *nvalues, nmax, requireIndex, msg)) 610 if (!dmParseMapOptionItem(start, end, values, *nvalues, nmax, requireIndex, msg))
611 return FALSE; 611 return false;
612 612
613 (*nvalues)++; 613 (*nvalues)++;
614 614
615 if (!end) 615 if (!end)
616 break; 616 break;
617 617
618 start = end + 1; 618 start = end + 1;
619 } 619 }
620 620
621 return TRUE; 621 return true;
622 } 622 }
623 623
624 624
625 int dmParseColorRemapFile(const char *filename, DMMapValue *values, int *nvalue, const int nmax) 625 int dmParseColorRemapFile(const char *filename, DMMapValue *values, int *nvalue, const int nmax)
626 { 626 {
661 fclose(fp); 661 fclose(fp);
662 return res; 662 return res;
663 } 663 }
664 664
665 665
666 BOOL dmParseFormatOption(const char *msg1, const char *msg2, char *optArg, int *format, int *subFormat) 666 bool dmParseFormatOption(const char *msg1, const char *msg2, char *optArg, int *format, int *subFormat)
667 { 667 {
668 char *flags = strchr(optArg, ':'); 668 char *flags = strchr(optArg, ':');
669 if (flags != NULL) 669 if (flags != NULL)
670 *flags++ = 0; 670 *flags++ = 0;
671 671
672 if (!dmGetFormatByExt(optArg, format, subFormat) && 672 if (!dmGetFormatByExt(optArg, format, subFormat) &&
673 !dmGetC64FormatByExt(optArg, format, subFormat)) 673 !dmGetC64FormatByExt(optArg, format, subFormat))
674 { 674 {
675 dmErrorMsg("Invalid %s format '%s', see -F / --formats for format list.\n", 675 dmErrorMsg("Invalid %s format '%s', see -F / --formats for format list.\n",
676 msg1, optArg); 676 msg1, optArg);
677 return FALSE; 677 return false;
678 } 678 }
679 679
680 if (flags != NULL) 680 if (flags != NULL)
681 { 681 {
682 switch (*format) 682 switch (*format)
683 { 683 {
684 case FFMT_SPRITE: 684 case FFMT_SPRITE:
685 case FFMT_CHAR: 685 case FFMT_CHAR:
686 if (strcasecmp(flags, "mc") == 0) 686 if (strcasecmp(flags, "mc") == 0)
687 optInMulticolor = TRUE; 687 optInMulticolor = true;
688 else 688 else
689 if (strcasecmp(flags, "sc") == 0) 689 if (strcasecmp(flags, "sc") == 0)
690 optInMulticolor = FALSE; 690 optInMulticolor = false;
691 else 691 else
692 { 692 {
693 dmErrorMsg("Invalid %s format flags for sprite/char '%s', should be 'mc' or 'sc'.\n", 693 dmErrorMsg("Invalid %s format flags for sprite/char '%s', should be 'mc' or 'sc'.\n",
694 msg1, flags); 694 msg1, flags);
695 return FALSE; 695 return false;
696 } 696 }
697 break; 697 break;
698 698
699 default: 699 default:
700 dmErrorMsg("%s format '%s' does not support any flags ('%s').\n", 700 dmErrorMsg("%s format '%s' does not support any flags ('%s').\n",
701 msg2, optArg, flags); 701 msg2, optArg, flags);
702 return FALSE; 702 return false;
703 } 703 }
704 } 704 }
705 705
706 return TRUE; 706 return true;
707 } 707 }
708 708
709 709
710 char *dmParseValWithSep(char **arg, char *last, BOOL (*isok)(const int ch), const char *sep) 710 char *dmParseValWithSep(char **arg, char *last, bool (*isok)(const int ch), const char *sep)
711 { 711 {
712 char *ptr = *arg, *end, *start; 712 char *ptr = *arg, *end, *start;
713 713
714 // Skip any whitespace at start 714 // Skip any whitespace at start
715 while (*ptr != 0 && isspace(*ptr)) 715 while (*ptr != 0 && isspace(*ptr))
742 742
743 return start; 743 return start;
744 } 744 }
745 745
746 746
747 BOOL dmParseIntValTok(const int ch) 747 bool dmParseIntValTok(const int ch)
748 { 748 {
749 return isxdigit(ch) || ch == 'x' || ch == '$'; 749 return isxdigit(ch) || ch == 'x' || ch == '$';
750 } 750 }
751 751
752 752
753 BOOL dmParseIntValWithSep(char **arg, unsigned int *value, char *last, const char *sep) 753 bool dmParseIntValWithSep(char **arg, unsigned int *value, char *last, const char *sep)
754 { 754 {
755 return dmGetIntVal(dmParseValWithSep(arg, last, dmParseIntValTok, sep), value, NULL); 755 return dmGetIntVal(dmParseValWithSep(arg, last, dmParseIntValTok, sep), value, NULL);
756 } 756 }
757 757
758 758
759 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) 759 bool argHandleOpt(const int optN, char *optArg, char *currArg)
760 { 760 {
761 unsigned int tmpUInt; 761 unsigned int tmpUInt;
762 char *tmpStr; 762 char *tmpStr;
763 763
764 switch (optN) 764 switch (optN)
786 786
787 case 12: 787 case 12:
788 if (!dmGetIntVal(optArg, &optInSkip, &optInSkipNeg)) 788 if (!dmGetIntVal(optArg, &optInSkip, &optInSkipNeg))
789 { 789 {
790 dmErrorMsg("Invalid skip value argument '%s'.\n", optArg); 790 dmErrorMsg("Invalid skip value argument '%s'.\n", optArg);
791 return FALSE; 791 return false;
792 } 792 }
793 break; 793 break;
794 794
795 case 14: 795 case 14:
796 { 796 {
797 DMConvFormat fmt; 797 DMConvFormat fmt;
798 798
799 if (!dmParseFormatOption("input", "Input", optArg, &optInType, &optForcedInSubFormat)) 799 if (!dmParseFormatOption("input", "Input", optArg, &optInType, &optForcedInSubFormat))
800 return FALSE; 800 return false;
801 801
802 dmGetConvFormat(optInType, optForcedInSubFormat, &fmt); 802 dmGetConvFormat(optInType, optForcedInSubFormat, &fmt);
803 if ((fmt.flags & DM_FMT_RD) == 0) 803 if ((fmt.flags & DM_FMT_RD) == 0)
804 { 804 {
805 dmErrorMsg("Invalid input format '%s', does not support reading.\n", 805 dmErrorMsg("Invalid input format '%s', does not support reading.\n",
806 fmt.name); 806 fmt.name);
807 return FALSE; 807 return false;
808 } 808 }
809 } 809 }
810 break; 810 break;
811 811
812 case 16: 812 case 16:
813 { 813 {
814 DMConvFormat fmt; 814 DMConvFormat fmt;
815 815
816 if (!dmParseFormatOption("output", "Output", optArg, &optOutType, &optOutFormat)) 816 if (!dmParseFormatOption("output", "Output", optArg, &optOutType, &optOutFormat))
817 return FALSE; 817 return false;
818 818
819 dmGetConvFormat(optOutType, optOutFormat, &fmt); 819 dmGetConvFormat(optOutType, optOutFormat, &fmt);
820 if ((fmt.flags & DM_FMT_WR) == 0) 820 if ((fmt.flags & DM_FMT_WR) == 0)
821 { 821 {
822 dmErrorMsg("Invalid output format '%s', does not support writing.\n", 822 dmErrorMsg("Invalid output format '%s', does not support writing.\n",
823 fmt.name); 823 fmt.name);
824 return FALSE; 824 return false;
825 } 825 }
826 } 826 }
827 break; 827 break;
828 828
829 case 18: 829 case 18:
830 optShowHelp = 3; 830 optShowHelp = 3;
831 break; 831 break;
832 832
833 case 20: 833 case 20:
834 optSequential = TRUE; 834 optSequential = true;
835 break; 835 break;
836 836
837 case 22: 837 case 22:
838 if (strcasecmp(optArg, "help") == 0) 838 if (strcasecmp(optArg, "help") == 0)
839 { 839 {
842 } 842 }
843 else 843 else
844 { 844 {
845 int ncolors; 845 int ncolors;
846 if (!dmParseMapOptionString(optArg, optColorMap, 846 if (!dmParseMapOptionString(optArg, optColorMap,
847 &ncolors, D64_NCOLORS, FALSE, "color index option")) 847 &ncolors, D64_NCOLORS, false, "color index option"))
848 return FALSE; 848 return false;
849 849
850 dmMsg(1, "Set color index mapping: "); 850 dmMsg(1, "Set color index mapping: ");
851 for (int index = 0; index < ncolors; index++) 851 for (int index = 0; index < ncolors; index++)
852 { 852 {
853 dmPrint(1, "[%d:%d]%s", 853 dmPrint(1, "[%d:%d]%s",
862 if (!dmGetIntVal(optArg, &tmpUInt, NULL) || 862 if (!dmGetIntVal(optArg, &tmpUInt, NULL) ||
863 tmpUInt < 1) 863 tmpUInt < 1)
864 { 864 {
865 dmErrorMsg("Invalid count value argument '%s' [1 .. MAXINT]\n", 865 dmErrorMsg("Invalid count value argument '%s' [1 .. MAXINT]\n",
866 optArg); 866 optArg);
867 return FALSE; 867 return false;
868 } 868 }
869 optItemCount = tmpUInt; 869 optItemCount = tmpUInt;
870 break; 870 break;
871 871
872 case 26: 872 case 26:
873 if (!dmGetIntVal(optArg, &tmpUInt, NULL) || 873 if (!dmGetIntVal(optArg, &tmpUInt, NULL) ||
874 tmpUInt < 1 || tmpUInt > 512) 874 tmpUInt < 1 || tmpUInt > 512)
875 { 875 {
876 dmErrorMsg("Invalid planed width value '%s' [1 .. 512]\n", 876 dmErrorMsg("Invalid planed width value '%s' [1 .. 512]\n",
877 optArg); 877 optArg);
878 return FALSE; 878 return false;
879 } 879 }
880 optPlanedWidth = tmpUInt; 880 optPlanedWidth = tmpUInt;
881 break; 881 break;
882 882
883 case 28: 883 case 28:
886 fprintf(stdout, "\n%s\n", argGetHelpTopic(optN)); 886 fprintf(stdout, "\n%s\n", argGetHelpTopic(optN));
887 exit(0); 887 exit(0);
888 } 888 }
889 else 889 else
890 { 890 {
891 BOOL error = FALSE; 891 bool error = false;
892 unsigned int tmpUInt2; 892 unsigned int tmpUInt2;
893 char *tmpStr = dm_strdup(optArg), 893 char *tmpStr = dm_strdup(optArg),
894 *tmpOpt = tmpStr, sep; 894 *tmpOpt = tmpStr, sep;
895 895
896 // Check for "relative scale mode specifier 896 // Check for "relative scale mode specifier
923 if (sep == 0) 923 if (sep == 0)
924 { 924 {
925 optSpec.scaleX = optSpec.scaleY = tmpUInt; 925 optSpec.scaleX = optSpec.scaleY = tmpUInt;
926 } 926 }
927 else 927 else
928 error = TRUE; 928 error = true;
929 } 929 }
930 else 930 else
931 error = TRUE; 931 error = true;
932 932
933 dmFree(tmpStr); 933 dmFree(tmpStr);
934 934
935 if (error) 935 if (error)
936 { 936 {
937 dmErrorMsg( 937 dmErrorMsg(
938 "Invalid scale option value '%s', should be [*]<n> or or <w>:<h>[*<n>].\n", 938 "Invalid scale option value '%s', should be [*]<n> or or <w>:<h>[*<n>].\n",
939 optArg); 939 optArg);
940 return FALSE; 940 return false;
941 } 941 }
942 942
943 if (optSpec.scaleX < 1 || optSpec.scaleX > 50) 943 if (optSpec.scaleX < 1 || optSpec.scaleX > 50)
944 { 944 {
945 dmErrorMsg("Invalid X scale value %d.\n", optSpec.scaleX); 945 dmErrorMsg("Invalid X scale value %d.\n", optSpec.scaleX);
946 return FALSE; 946 return false;
947 } 947 }
948 if (optSpec.scaleY < 1 || optSpec.scaleY > 50) 948 if (optSpec.scaleY < 1 || optSpec.scaleY > 50)
949 { 949 {
950 dmErrorMsg("Invalid Y scale value %d.\n", optSpec.scaleY); 950 dmErrorMsg("Invalid Y scale value %d.\n", optSpec.scaleY);
951 return FALSE; 951 return false;
952 } 952 }
953 } 953 }
954 break; 954 break;
955 955
956 case 30: 956 case 30:
957 optUsePalette = TRUE; 957 optUsePalette = true;
958 break; 958 break;
959 959
960 case 32: 960 case 32:
961 if (!dmGetIntVal(optArg, &tmpUInt, NULL) || 961 if (!dmGetIntVal(optArg, &tmpUInt, NULL) ||
962 tmpUInt < 1 || tmpUInt > 8) 962 tmpUInt < 1 || tmpUInt > 8)
963 { 963 {
964 dmErrorMsg("Invalid number of bitplanes value '%s' [1 .. 8]\n", 964 dmErrorMsg("Invalid number of bitplanes value '%s' [1 .. 8]\n",
965 optArg); 965 optArg);
966 return FALSE; 966 return false;
967 } 967 }
968 optSpec.nplanes = tmpUInt; 968 optSpec.nplanes = tmpUInt;
969 break; 969 break;
970 970
971 case 34: 971 case 34:
972 if (!dmGetIntVal(optArg, &tmpUInt, NULL) || 972 if (!dmGetIntVal(optArg, &tmpUInt, NULL) ||
973 tmpUInt < 1 || tmpUInt > 32) 973 tmpUInt < 1 || tmpUInt > 32)
974 { 974 {
975 dmErrorMsg("Invalid number of bits per plane value '%s' [1 .. 32]\n", 975 dmErrorMsg("Invalid number of bits per plane value '%s' [1 .. 32]\n",
976 optArg); 976 optArg);
977 return FALSE; 977 return false;
978 } 978 }
979 optSpec.bpp = tmpUInt; 979 optSpec.bpp = tmpUInt;
980 break; 980 break;
981 981
982 case 36: 982 case 36:
983 optSpec.planar = TRUE; 983 optSpec.planar = true;
984 break; 984 break;
985 985
986 case 38: 986 case 38:
987 if (!dmGetIntVal(optArg, &tmpUInt, NULL) || 987 if (!dmGetIntVal(optArg, &tmpUInt, NULL) ||
988 tmpUInt > FCMP_BEST) 988 tmpUInt > FCMP_BEST)
989 { 989 {
990 dmErrorMsg("Invalid compression setting '%s' [%d .. %d]\n", 990 dmErrorMsg("Invalid compression setting '%s' [%d .. %d]\n",
991 optArg, FCMP_NONE, FCMP_BEST); 991 optArg, FCMP_NONE, FCMP_BEST);
992 return FALSE; 992 return false;
993 } 993 }
994 optSpec.compression = tmpUInt; 994 optSpec.compression = tmpUInt;
995 break; 995 break;
996 996
997 case 40: 997 case 40:
1001 } 1001 }
1002 else 1002 else
1003 { 1003 {
1004 unsigned int tx0, ty0, tx1, ty1; 1004 unsigned int tx0, ty0, tx1, ty1;
1005 char sep, modeSep = 0, *tmpTok, *tmpStr; 1005 char sep, modeSep = 0, *tmpTok, *tmpStr;
1006 BOOL ok; 1006 bool ok;
1007 if ((tmpTok = tmpStr = dm_strdup(optArg)) == NULL) 1007 if ((tmpTok = tmpStr = dm_strdup(optArg)) == NULL)
1008 { 1008 {
1009 dmErrorMsg("Could not allocate memory for temporary string.\n"); 1009 dmErrorMsg("Could not allocate memory for temporary string.\n");
1010 return FALSE; 1010 return false;
1011 } 1011 }
1012 1012
1013 // Check for 'x0:y0-x1:y1' pattern 1013 // Check for 'x0:y0-x1:y1' pattern
1014 ok = dmParseIntValWithSep(&tmpTok, &tx0, &sep, ":") && 1014 ok = dmParseIntValWithSep(&tmpTok, &tx0, &sep, ":") &&
1015 sep == ':' && 1015 sep == ':' &&
1048 } 1048 }
1049 } 1049 }
1050 else 1050 else
1051 { 1051 {
1052 dmErrorMsg("Invalid crop mode / argument '%s'.\n", optArg); 1052 dmErrorMsg("Invalid crop mode / argument '%s'.\n", optArg);
1053 return FALSE; 1053 return false;
1054 } 1054 }
1055 } 1055 }
1056 break; 1056 break;
1057 1057
1058 case 42: 1058 case 42:
1071 // Parse one sub-option 1071 // Parse one sub-option
1072 char sep, *topt = dmParseValWithSep(&tmpStr, &sep, NULL, "+"); 1072 char sep, *topt = dmParseValWithSep(&tmpStr, &sep, NULL, "+");
1073 1073
1074 // Check what option we have 1074 // Check what option we have
1075 if (strcasecmp(topt, "remove") == 0) 1075 if (strcasecmp(topt, "remove") == 0)
1076 optRemapRemove = TRUE; 1076 optRemapRemove = true;
1077 else 1077 else
1078 if (strcasecmp(topt, "alpha") == 0) 1078 if (strcasecmp(topt, "alpha") == 0)
1079 optRemapMatchAlpha = TRUE; 1079 optRemapMatchAlpha = true;
1080 else 1080 else
1081 if (strncasecmp(topt, "max=", 4) == 0) 1081 if (strncasecmp(topt, "max=", 4) == 0)
1082 { 1082 {
1083 char *start = topt + 4, *end; 1083 char *start = topt + 4, *end;
1084 optRemapMaxDist = strtof(start, &end); 1084 optRemapMaxDist = strtof(start, &end);
1085 1085
1086 if (end == start) 1086 if (end == start)
1087 { 1087 {
1088 dmErrorMsg("Invalid or missing value parameter for -R option flag: '%s'.\n", 1088 dmErrorMsg("Invalid or missing value parameter for -R option flag: '%s'.\n",
1089 topt); 1089 topt);
1090 return FALSE; 1090 return false;
1091 } 1091 }
1092 } 1092 }
1093 else 1093 else
1094 if (strncasecmp(topt, "nomatch=", 8) == 0) 1094 if (strncasecmp(topt, "nomatch=", 8) == 0)
1095 { 1095 {
1098 1098
1099 if (end == start) 1099 if (end == start)
1100 { 1100 {
1101 dmErrorMsg("Invalid or missing value parameter for -R option flag: '%s'.\n", 1101 dmErrorMsg("Invalid or missing value parameter for -R option flag: '%s'.\n",
1102 topt); 1102 topt);
1103 return FALSE; 1103 return false;
1104 } 1104 }
1105 1105
1106 if (optRemapNoMatchColor < -1 || optRemapNoMatchColor > 255) 1106 if (optRemapNoMatchColor < -1 || optRemapNoMatchColor > 255)
1107 { 1107 {
1108 dmErrorMsg("Invalid remap no-match color value %d. Should be [-1 .. 255].\n", 1108 dmErrorMsg("Invalid remap no-match color value %d. Should be [-1 .. 255].\n",
1109 optRemapNoMatchColor); 1109 optRemapNoMatchColor);
1110 return FALSE; 1110 return false;
1111 } 1111 }
1112 } 1112 }
1113 else 1113 else
1114 { 1114 {
1115 dmErrorMsg("Unknown -R option flag '%s'.\n", topt); 1115 dmErrorMsg("Unknown -R option flag '%s'.\n", topt);
1116 return FALSE; 1116 return false;
1117 } 1117 }
1118 } while (*tmpStr != 0); 1118 } while (*tmpStr != 0);
1119 } 1119 }
1120 1120
1121 // Check which remap mode is being requested 1121 // Check which remap mode is being requested
1122 if (strcasecmp(optArg, "auto") == 0) 1122 if (strcasecmp(optArg, "auto") == 0)
1123 { 1123 {
1124 if (optRemapMode != REMAP_NONE && optRemapMode != REMAP_AUTO) 1124 if (optRemapMode != REMAP_NONE && optRemapMode != REMAP_AUTO)
1125 { 1125 {
1126 dmErrorMsg("Remap mode already set to something else than 'auto'. You can only have one remapping mode.\n"); 1126 dmErrorMsg("Remap mode already set to something else than 'auto'. You can only have one remapping mode.\n");
1127 return FALSE; 1127 return false;
1128 } 1128 }
1129 1129
1130 optRemapMode = REMAP_AUTO; 1130 optRemapMode = REMAP_AUTO;
1131 } 1131 }
1132 else 1132 else
1133 { 1133 {
1134 if (optRemapMode != REMAP_NONE && optRemapMode != REMAP_MAPPED) 1134 if (optRemapMode != REMAP_NONE && optRemapMode != REMAP_MAPPED)
1135 { 1135 {
1136 dmErrorMsg("Remap mode already set to something else than 'mapped'. You can only have one remapping mode.\n"); 1136 dmErrorMsg("Remap mode already set to something else than 'mapped'. You can only have one remapping mode.\n");
1137 return FALSE; 1137 return false;
1138 } 1138 }
1139 1139
1140 if (optArg[0] == '@') 1140 if (optArg[0] == '@')
1141 { 1141 {
1142 if (optArg[1] != 0) 1142 if (optArg[1] != 0)
1143 { 1143 {
1144 int res; 1144 int res;
1145 if ((res = dmParseColorRemapFile(optArg + 1, 1145 if ((res = dmParseColorRemapFile(optArg + 1,
1146 optRemapTable, &optNRemapTable, DM_MAX_COLORS)) != DMERR_OK) 1146 optRemapTable, &optNRemapTable, DM_MAX_COLORS)) != DMERR_OK)
1147 return FALSE; 1147 return false;
1148 } 1148 }
1149 else 1149 else
1150 { 1150 {
1151 dmErrorMsg("No remap filename given.\n"); 1151 dmErrorMsg("No remap filename given.\n");
1152 return FALSE; 1152 return false;
1153 } 1153 }
1154 } 1154 }
1155 else 1155 else
1156 { 1156 {
1157 if (!dmParseMapOptionString(optArg, optRemapTable, 1157 if (!dmParseMapOptionString(optArg, optRemapTable,
1158 &optNRemapTable, DM_MAX_COLORS, TRUE, "color remap option")) 1158 &optNRemapTable, DM_MAX_COLORS, true, "color remap option"))
1159 return FALSE; 1159 return false;
1160 } 1160 }
1161 1161
1162 optRemapMode = REMAP_MAPPED; 1162 optRemapMode = REMAP_MAPPED;
1163 } 1163 }
1164 1164
1175 case 46: 1175 case 46:
1176 return argHandleC64PaletteOption(optArg, &optC64Palette, &optPaletteFile); 1176 return argHandleC64PaletteOption(optArg, &optC64Palette, &optPaletteFile);
1177 1177
1178 default: 1178 default:
1179 dmErrorMsg("Unimplemented option argument '%s'.\n", currArg); 1179 dmErrorMsg("Unimplemented option argument '%s'.\n", currArg);
1180 return FALSE; 1180 return false;
1181 } 1181 }
1182 1182
1183 return TRUE; 1183 return true;
1184 } 1184 }
1185 1185
1186 1186
1187 BOOL argHandleFile(char *currArg) 1187 bool argHandleFile(char *currArg)
1188 { 1188 {
1189 if (!optInFilename) 1189 if (!optInFilename)
1190 optInFilename = currArg; 1190 optInFilename = currArg;
1191 else 1191 else
1192 { 1192 {
1193 dmErrorMsg("Source filename already specified, extraneous argument '%s'.\n", 1193 dmErrorMsg("Source filename already specified, extraneous argument '%s'.\n",
1194 currArg); 1194 currArg);
1195 return FALSE; 1195 return false;
1196 } 1196 }
1197 1197
1198 return TRUE; 1198 return true;
1199 } 1199 }
1200 1200
1201 1201
1202 void dmPrintByte(FILE *out, const Uint8 byte, const int format, const BOOL multicolor, const BOOL dir) 1202 void dmPrintByte(FILE *out, const Uint8 byte, const int format, const bool multicolor, const bool dir)
1203 { 1203 {
1204 if (multicolor) 1204 if (multicolor)
1205 { 1205 {
1206 for (int i = 0; i < DM_ASC_NBITS; i += 2) 1206 for (int i = 0; i < DM_ASC_NBITS; i += 2)
1207 { 1207 {
1240 } 1240 }
1241 } 1241 }
1242 } 1242 }
1243 1243
1244 1244
1245 void dmDumpCharASCII(FILE *outFile, const Uint8 *buf, const size_t offs, const int fmt, const BOOL multicolor) 1245 void dmDumpCharASCII(FILE *outFile, const Uint8 *buf, const size_t offs, const int fmt, const bool multicolor)
1246 { 1246 {
1247 for (size_t yc = 0; yc < D64_CHR_HEIGHT_UT; yc++) 1247 for (size_t yc = 0; yc < D64_CHR_HEIGHT_UT; yc++)
1248 { 1248 {
1249 fprintf(outFile, "%04" DM_PRIx_SIZE_T " : ", offs + yc); 1249 fprintf(outFile, "%04" DM_PRIx_SIZE_T " : ", offs + yc);
1250 dmPrintByte(outFile, buf[yc], fmt, multicolor, FALSE); 1250 dmPrintByte(outFile, buf[yc], fmt, multicolor, false);
1251 fprintf(outFile, "\n"); 1251 fprintf(outFile, "\n");
1252 } 1252 }
1253 } 1253 }
1254 1254
1255 1255
1256 void dmDumpSpriteASCII(FILE *outFile, const Uint8 *buf, const size_t offs, const int fmt, BOOL multicolor) 1256 void dmDumpSpriteASCII(FILE *outFile, const Uint8 *buf, const size_t offs, const int fmt, bool multicolor)
1257 { 1257 {
1258 size_t bufOffs, xc, yc; 1258 size_t bufOffs, xc, yc;
1259 1259
1260 for (bufOffs = yc = 0; yc < D64_SPR_HEIGHT_UT; yc++) 1260 for (bufOffs = yc = 0; yc < D64_SPR_HEIGHT_UT; yc++)
1261 { 1261 {
1262 fprintf(outFile, "%04" DM_PRIx_SIZE_T " ", offs + bufOffs); 1262 fprintf(outFile, "%04" DM_PRIx_SIZE_T " ", offs + bufOffs);
1263 for (xc = 0; xc < D64_SPR_WIDTH_UT; xc++) 1263 for (xc = 0; xc < D64_SPR_WIDTH_UT; xc++)
1264 { 1264 {
1265 dmPrintByte(outFile, buf[bufOffs], fmt, multicolor, FALSE); 1265 dmPrintByte(outFile, buf[bufOffs], fmt, multicolor, false);
1266 fprintf(outFile, " "); 1266 fprintf(outFile, " ");
1267 bufOffs++; 1267 bufOffs++;
1268 } 1268 }
1269 fprintf(outFile, "\n"); 1269 fprintf(outFile, "\n");
1270 } 1270 }
1271 } 1271 }
1272 1272
1273 1273
1274 // XXX TODO: we need to evaluate the color vector itself, not just the distance 1274 // XXX TODO: we need to evaluate the color vector itself, not just the distance
1275 float dmGetColorDist(const DMColor *c1, const DMColor *c2, const BOOL alpha) 1275 float dmGetColorDist(const DMColor *c1, const DMColor *c2, const bool alpha)
1276 { 1276 {
1277 const float 1277 const float
1278 dr = (c1->r - c2->r) / 255.0, 1278 dr = (c1->r - c2->r) / 255.0,
1279 dg = (c1->g - c2->g) / 255.0, 1279 dg = (c1->g - c2->g) / 255.0,
1280 db = (c1->b - c2->b) / 255.0; 1280 db = (c1->b - c2->b) / 255.0;
1287 else 1287 else
1288 return (dr * dr + dg * dg + db * db) / 3.0; 1288 return (dr * dr + dg * dg + db * db) / 3.0;
1289 } 1289 }
1290 1290
1291 1291
1292 int dmScanUsedColors(const DMImage *src, const BOOL warn, BOOL *used, int *nused) 1292 int dmScanUsedColors(const DMImage *src, const bool warn, bool *used, int *nused)
1293 { 1293 {
1294 BOOL warned = FALSE; 1294 bool warned = false;
1295 *nused = 0; 1295 *nused = 0;
1296 1296
1297 if (src == NULL || used == NULL || nused == NULL) 1297 if (src == NULL || used == NULL || nused == NULL)
1298 return DMERR_NULLPTR; 1298 return DMERR_NULLPTR;
1299 1299
1302 return dmError(DMERR_INVALID_DATA, 1302 return dmError(DMERR_INVALID_DATA,
1303 "Source image is not paletted.\n"); 1303 "Source image is not paletted.\n");
1304 } 1304 }
1305 1305
1306 for (int index = 0; index < src->pal->ncolors; index++) 1306 for (int index = 0; index < src->pal->ncolors; index++)
1307 used[index] = FALSE; 1307 used[index] = false;
1308 1308
1309 for (int yc = 0; yc < src->height; yc++) 1309 for (int yc = 0; yc < src->height; yc++)
1310 { 1310 {
1311 const Uint8 *dp = src->data + src->pitch * yc; 1311 const Uint8 *dp = src->data + src->pitch * yc;
1312 for (int xc = 0; xc < src->width; xc++) 1312 for (int xc = 0; xc < src->width; xc++)
1314 Uint8 col = dp[xc]; 1314 Uint8 col = dp[xc];
1315 if (col < src->pal->ncolors) 1315 if (col < src->pal->ncolors)
1316 { 1316 {
1317 if (!used[col]) 1317 if (!used[col])
1318 { 1318 {
1319 used[col] = TRUE; 1319 used[col] = true;
1320 (*nused)++; 1320 (*nused)++;
1321 } 1321 }
1322 } 1322 }
1323 else 1323 else
1324 if (warn && !warned) 1324 if (warn && !warned)
1325 { 1325 {
1326 dmErrorMsg("Image contains color indices that are out of bounds of the palette.\n"); 1326 dmErrorMsg("Image contains color indices that are out of bounds of the palette.\n");
1327 warned = TRUE; 1327 warned = true;
1328 } 1328 }
1329 } 1329 }
1330 } 1330 }
1331 1331
1332 return DMERR_OK; 1332 return DMERR_OK;
1374 1374
1375 1375
1376 int dmRemapImageColors(DMImage **pdst, const DMImage *src, 1376 int dmRemapImageColors(DMImage **pdst, const DMImage *src,
1377 const DMPalette *dpal, 1377 const DMPalette *dpal,
1378 const float maxDist, const int noMatchColor, 1378 const float maxDist, const int noMatchColor,
1379 const BOOL alpha, const BOOL removeUnused) 1379 const bool alpha, const bool removeUnused)
1380 { 1380 {
1381 DMPalette *tpal = NULL; 1381 DMPalette *tpal = NULL;
1382 const DMPalette *ppal; 1382 const DMPalette *ppal;
1383 BOOL *used = NULL; 1383 bool *used = NULL;
1384 int *mapping = NULL, *mapped = NULL; 1384 int *mapping = NULL, *mapped = NULL;
1385 int res = DMERR_OK; 1385 int res = DMERR_OK;
1386 BOOL fail = FALSE; 1386 bool fail = false;
1387 1387
1388 if (pdst == NULL || src == NULL || dpal == NULL) 1388 if (pdst == NULL || src == NULL || dpal == NULL)
1389 return DMERR_NULLPTR; 1389 return DMERR_NULLPTR;
1390 1390
1391 if (src->pal == NULL || src->pixfmt != DM_PIXFMT_PALETTE) 1391 if (src->pal == NULL || src->pixfmt != DM_PIXFMT_PALETTE)
1452 1452
1453 dmPrint(0, 1453 dmPrint(0,
1454 "No match for source color #%d. Closest: #%d (%02x %02x %02x) [dist=%1.3f > %1.3f]\n", 1454 "No match for source color #%d. Closest: #%d (%02x %02x %02x) [dist=%1.3f > %1.3f]\n",
1455 sc, closestDC, dcol->r, dcol->g, dcol->b, 1455 sc, closestDC, dcol->r, dcol->g, dcol->b,
1456 closestDist, maxDist); 1456 closestDist, maxDist);
1457 fail = TRUE; 1457 fail = true;
1458 } 1458 }
1459 else 1459 else
1460 { 1460 {
1461 closestDC = noMatchColor; 1461 closestDC = noMatchColor;
1462 } 1462 }
1490 { 1490 {
1491 dmErrorMsg("WARNING! Removing unused colors with 'no-match' color index set may have unintended results.\n"); 1491 dmErrorMsg("WARNING! Removing unused colors with 'no-match' color index set may have unintended results.\n");
1492 } 1492 }
1493 1493
1494 // Get the actually used colors 1494 // Get the actually used colors
1495 if ((res = dmScanUsedColors(src, TRUE, used, &nused)) != DMERR_OK) 1495 if ((res = dmScanUsedColors(src, true, used, &nused)) != DMERR_OK)
1496 goto out; 1496 goto out;
1497 1497
1498 dmMsg(2, "Found %d used color indices.\n", nused); 1498 dmMsg(2, "Found %d used color indices.\n", nused);
1499 1499
1500 // Remove duplicates from the mapped colour indices 1500 // Remove duplicates from the mapped colour indices
1503 for (int n = 0; n < src->pal->ncolors; n++) 1503 for (int n = 0; n < src->pal->ncolors; n++)
1504 if (n != index && 1504 if (n != index &&
1505 mapping[index] == mapping[n] && 1505 mapping[index] == mapping[n] &&
1506 used[n] && used[index]) 1506 used[n] && used[index])
1507 { 1507 {
1508 used[n] = FALSE; 1508 used[n] = false;
1509 } 1509 }
1510 } 1510 }
1511 1511
1512 if (noMatchColor >= 0) 1512 if (noMatchColor >= 0)
1513 used[noMatchColor] = TRUE; 1513 used[noMatchColor] = true;
1514 1514
1515 // Re-count number of actually used indices 1515 // Re-count number of actually used indices
1516 nused = 0; 1516 nused = 0;
1517 for (int index = 0; index < src->pal->ncolors; index++) 1517 for (int index = 0; index < src->pal->ncolors; index++)
1518 if (used[index]) 1518 if (used[index])
1570 1570
1571 1571
1572 int dmMapImageColors(DMImage **pdst, const DMImage *src, 1572 int dmMapImageColors(DMImage **pdst, const DMImage *src,
1573 const DMMapValue *mapTable, const int nmapTable, 1573 const DMMapValue *mapTable, const int nmapTable,
1574 const float maxDist, const int noMatchColor, 1574 const float maxDist, const int noMatchColor,
1575 const BOOL alpha, const BOOL removeUnused) 1575 const bool alpha, const bool removeUnused)
1576 { 1576 {
1577 DMPalette *tpal = NULL; 1577 DMPalette *tpal = NULL;
1578 BOOL *mapped = NULL, *used = NULL; 1578 bool *mapped = NULL, *used = NULL;
1579 int *mapping = NULL; 1579 int *mapping = NULL;
1580 int nused, res = DMERR_OK; 1580 int nused, res = DMERR_OK;
1581 BOOL fail = FALSE; 1581 bool fail = false;
1582 1582
1583 if (pdst == NULL || src == NULL || mapTable == NULL) 1583 if (pdst == NULL || src == NULL || mapTable == NULL)
1584 return DMERR_NULLPTR; 1584 return DMERR_NULLPTR;
1585 1585
1586 if (src->pal == NULL || src->pixfmt != DM_PIXFMT_PALETTE) 1586 if (src->pal == NULL || src->pixfmt != DM_PIXFMT_PALETTE)
1601 } 1601 }
1602 1602
1603 for (int index = 0; index < src->pal->ncolors; index++) 1603 for (int index = 0; index < src->pal->ncolors; index++)
1604 { 1604 {
1605 mapping[index] = -1; 1605 mapping[index] = -1;
1606 mapped[index] = FALSE; 1606 mapped[index] = false;
1607 } 1607 }
1608 1608
1609 if ((res = dmPaletteAlloc(&tpal, src->pal->ncolors, -1)) != DMERR_OK) 1609 if ((res = dmPaletteAlloc(&tpal, src->pal->ncolors, -1)) != DMERR_OK)
1610 { 1610 {
1611 dmErrorMsg("Could not allocate memory for remap palette.\n"); 1611 dmErrorMsg("Could not allocate memory for remap palette.\n");
1650 1650
1651 dmMsg(3, "No RGBA match found for map index %d, #%02x%02x%02x%02x. Closest: #%d (#%02x%02x%02x%02x) [dist=%1.3f > %1.3f]\n", 1651 dmMsg(3, "No RGBA match found for map index %d, #%02x%02x%02x%02x. Closest: #%d (#%02x%02x%02x%02x) [dist=%1.3f > %1.3f]\n",
1652 index, map->color.r, map->color.g, map->color.b, map->color.a, 1652 index, map->color.r, map->color.g, map->color.b, map->color.a,
1653 closestDC, dcol->r, dcol->g, dcol->b, dcol->a, closestDist, maxDist); 1653 closestDC, dcol->r, dcol->g, dcol->b, dcol->a, closestDist, maxDist);
1654 1654
1655 fail = TRUE; 1655 fail = true;
1656 } 1656 }
1657 else 1657 else
1658 { 1658 {
1659 DMColor *dcol = &src->pal->colors[noMatchColor]; 1659 DMColor *dcol = &src->pal->colors[noMatchColor];
1660 closestDC = noMatchColor; 1660 closestDC = noMatchColor;
1663 map->color.r, map->color.g, map->color.b, map->color.a, 1663 map->color.r, map->color.g, map->color.b, map->color.a,
1664 map->to, 1664 map->to,
1665 closestDC, dcol->r, dcol->g, dcol->b, dcol->a, closestDist); 1665 closestDC, dcol->r, dcol->g, dcol->b, dcol->a, closestDist);
1666 1666
1667 mapping[closestDC] = map->to; 1667 mapping[closestDC] = map->to;
1668 mapped[map->to] = TRUE; 1668 mapped[map->to] = true;
1669 } 1669 }
1670 } 1670 }
1671 else 1671 else
1672 { 1672 {
1673 DMColor *dcol = &src->pal->colors[closestDC]; 1673 DMColor *dcol = &src->pal->colors[closestDC];
1676 map->color.r, map->color.g, map->color.b, map->color.a, 1676 map->color.r, map->color.g, map->color.b, map->color.a,
1677 map->to, 1677 map->to,
1678 closestDC, dcol->r, dcol->g, dcol->b, dcol->a, closestDist); 1678 closestDC, dcol->r, dcol->g, dcol->b, dcol->a, closestDist);
1679 1679
1680 mapping[closestDC] = map->to; 1680 mapping[closestDC] = map->to;
1681 mapped[map->to] = TRUE; 1681 mapped[map->to] = true;
1682 } 1682 }
1683 } 1683 }
1684 else 1684 else
1685 { 1685 {
1686 dmMsg(3, "Map index: %d -> %d\n", 1686 dmMsg(3, "Map index: %d -> %d\n",
1687 map->from, map->to); 1687 map->from, map->to);
1688 1688
1689 mapping[map->from] = map->to; 1689 mapping[map->from] = map->to;
1690 mapped[map->to] = TRUE; 1690 mapped[map->to] = true;
1691 } 1691 }
1692 } 1692 }
1693 1693
1694 if (fail) 1694 if (fail)
1695 { 1695 {
1700 // Fill the unmapped colors 1700 // Fill the unmapped colors
1701 if (removeUnused) 1701 if (removeUnused)
1702 { 1702 {
1703 dmMsg(2, "Scanning for used colors.\n"); 1703 dmMsg(2, "Scanning for used colors.\n");
1704 1704
1705 if ((res = dmScanUsedColors(src, TRUE, used, &nused)) != DMERR_OK) 1705 if ((res = dmScanUsedColors(src, true, used, &nused)) != DMERR_OK)
1706 goto out; 1706 goto out;
1707 1707
1708 dmMsg(2, "Removing unused colors: %d -> %d.\n", 1708 dmMsg(2, "Removing unused colors: %d -> %d.\n",
1709 src->pal->ncolors, nused); 1709 src->pal->ncolors, nused);
1710 } 1710 }
1715 { 1715 {
1716 for (int n = 0; n < src->pal->ncolors; n++) 1716 for (int n = 0; n < src->pal->ncolors; n++)
1717 if (!mapped[n]) 1717 if (!mapped[n])
1718 { 1718 {
1719 mapping[index] = n; 1719 mapping[index] = n;
1720 mapped[n] = TRUE; 1720 mapped[n] = true;
1721 break; 1721 break;
1722 } 1722 }
1723 } 1723 }
1724 1724
1725 // Copy mapped palette entries 1725 // Copy mapped palette entries
1983 int dmWriteImage(const char *filename, DMImage *pimage, 1983 int dmWriteImage(const char *filename, DMImage *pimage,
1984 DMImageWriteSpec *spec, const DMImageFormat *fmt) 1984 DMImageWriteSpec *spec, const DMImageFormat *fmt)
1985 { 1985 {
1986 int res = DMERR_OK; 1986 int res = DMERR_OK;
1987 DMImage *image = pimage; 1987 DMImage *image = pimage;
1988 BOOL allocated = FALSE; 1988 bool allocated = false;
1989 1989
1990 // Check if writing is even supported 1990 // Check if writing is even supported
1991 if (fmt->write == NULL || (fmt->flags & DM_FMT_WR) == 0) 1991 if (fmt->write == NULL || (fmt->flags & DM_FMT_WR) == 0)
1992 { 1992 {
1993 return dmError(DMERR_NOT_SUPPORTED, 1993 return dmError(DMERR_NOT_SUPPORTED,
2041 &image, pimage, optRemapTable, optNRemapTable, 2041 &image, pimage, optRemapTable, optNRemapTable,
2042 optRemapMaxDist, optRemapNoMatchColor, 2042 optRemapMaxDist, optRemapNoMatchColor,
2043 optRemapMatchAlpha, optRemapRemove)) != DMERR_OK) 2043 optRemapMatchAlpha, optRemapRemove)) != DMERR_OK)
2044 goto out; 2044 goto out;
2045 2045
2046 allocated = TRUE; 2046 allocated = true;
2047 break; 2047 break;
2048 2048
2049 case REMAP_AUTO: 2049 case REMAP_AUTO:
2050 if (optPaletteData == NULL) 2050 if (optPaletteData == NULL)
2051 { 2051 {
2058 &image, pimage, optPaletteData, 2058 &image, pimage, optPaletteData,
2059 optRemapMaxDist, optRemapNoMatchColor, 2059 optRemapMaxDist, optRemapNoMatchColor,
2060 optRemapMatchAlpha, optRemapRemove)) != DMERR_OK) 2060 optRemapMatchAlpha, optRemapRemove)) != DMERR_OK)
2061 goto out; 2061 goto out;
2062 2062
2063 allocated = TRUE; 2063 allocated = true;
2064 break; 2064 break;
2065 } 2065 }
2066 } 2066 }
2067 else 2067 else
2068 if (optRemapMode != REMAP_NONE) 2068 if (optRemapMode != REMAP_NONE)
2205 2205
2206 return res; 2206 return res;
2207 } 2207 }
2208 2208
2209 2209
2210 Uint8 dmConvertByte(const Uint8 *sp, const BOOL multicolor) 2210 Uint8 dmConvertByte(const Uint8 *sp, const bool multicolor)
2211 { 2211 {
2212 Uint8 byte = 0; 2212 Uint8 byte = 0;
2213 int xc; 2213 int xc;
2214 2214
2215 if (multicolor) 2215 if (multicolor)
2231 2231
2232 return byte; 2232 return byte;
2233 } 2233 }
2234 2234
2235 2235
2236 BOOL dmConvertImage2Char(Uint8 *buf, const DMImage *image, 2236 bool dmConvertImage2Char(Uint8 *buf, const DMImage *image,
2237 const int xoffs, const int yoffs, const BOOL multicolor) 2237 const int xoffs, const int yoffs, const bool multicolor)
2238 { 2238 {
2239 int yc; 2239 int yc;
2240 2240
2241 if (xoffs < 0 || yoffs < 0 || 2241 if (xoffs < 0 || yoffs < 0 ||
2242 xoffs + D64_CHR_WIDTH_PX > image->width || 2242 xoffs + D64_CHR_WIDTH_PX > image->width ||
2243 yoffs + D64_CHR_HEIGHT_PX > image->height) 2243 yoffs + D64_CHR_HEIGHT_PX > image->height)
2244 return FALSE; 2244 return false;
2245 2245
2246 for (yc = 0; yc < D64_CHR_HEIGHT_UT; yc++) 2246 for (yc = 0; yc < D64_CHR_HEIGHT_UT; yc++)
2247 { 2247 {
2248 const Uint8 *sp = image->data + ((yc + yoffs) * image->pitch) + xoffs; 2248 const Uint8 *sp = image->data + ((yc + yoffs) * image->pitch) + xoffs;
2249 buf[yc] = dmConvertByte(sp, multicolor); 2249 buf[yc] = dmConvertByte(sp, multicolor);
2250 } 2250 }
2251 2251
2252 return TRUE; 2252 return true;
2253 } 2253 }
2254 2254
2255 2255
2256 BOOL dmConvertImage2Sprite(Uint8 *buf, const DMImage *image, 2256 bool dmConvertImage2Sprite(Uint8 *buf, const DMImage *image,
2257 const int xoffs, const int yoffs, const BOOL multicolor) 2257 const int xoffs, const int yoffs, const bool multicolor)
2258 { 2258 {
2259 int yc, xc; 2259 int yc, xc;
2260 2260
2261 if (xoffs < 0 || yoffs < 0 || 2261 if (xoffs < 0 || yoffs < 0 ||
2262 xoffs + D64_SPR_WIDTH_PX > image->width || 2262 xoffs + D64_SPR_WIDTH_PX > image->width ||
2263 yoffs + D64_SPR_HEIGHT_PX > image->height) 2263 yoffs + D64_SPR_HEIGHT_PX > image->height)
2264 return FALSE; 2264 return false;
2265 2265
2266 for (yc = 0; yc < D64_SPR_HEIGHT_UT; yc++) 2266 for (yc = 0; yc < D64_SPR_HEIGHT_UT; yc++)
2267 { 2267 {
2268 for (xc = 0; xc < D64_SPR_WIDTH_PX / D64_SPR_WIDTH_UT; xc++) 2268 for (xc = 0; xc < D64_SPR_WIDTH_PX / D64_SPR_WIDTH_UT; xc++)
2269 { 2269 {
2270 const Uint8 *sp = image->data + ((yc + yoffs) * image->pitch) + (xc * 8) + xoffs; 2270 const Uint8 *sp = image->data + ((yc + yoffs) * image->pitch) + (xc * 8) + xoffs;
2271 buf[(yc * D64_SPR_WIDTH_UT) + xc] = dmConvertByte(sp, multicolor); 2271 buf[(yc * D64_SPR_WIDTH_UT) + xc] = dmConvertByte(sp, multicolor);
2272 } 2272 }
2273 } 2273 }
2274 2274
2275 return TRUE; 2275 return true;
2276 } 2276 }
2277 2277
2278 2278
2279 int dmWriteSpritesAndChars(const char *filename, DMImage *image, int outFormat, const BOOL multicolor) 2279 int dmWriteSpritesAndChars(const char *filename, DMImage *image, int outFormat, const bool multicolor)
2280 { 2280 {
2281 int ret = DMERR_OK; 2281 int ret = DMERR_OK;
2282 int outBlockW, outBlockH, bx, by; 2282 int outBlockW, outBlockH, bx, by;
2283 FILE *outFile = NULL; 2283 FILE *outFile = NULL;
2284 Uint8 *tmpBuf = NULL; 2284 Uint8 *tmpBuf = NULL;
2409 offs = 0; 2409 offs = 0;
2410 itemCount = 0; 2410 itemCount = 0;
2411 2411
2412 if (optOutType == FFMT_ANSI || optOutType == FFMT_ASCII) 2412 if (optOutType == FFMT_ANSI || optOutType == FFMT_ASCII)
2413 { 2413 {
2414 BOOL error = FALSE; 2414 bool error = false;
2415 FILE *outFile; 2415 FILE *outFile;
2416 2416
2417 if (optOutFilename == NULL) 2417 if (optOutFilename == NULL)
2418 outFile = stdout; 2418 outFile = stdout;
2419 else 2419 else
2481 outIHeight++; 2481 outIHeight++;
2482 2482
2483 outImage = dmImageAlloc(outWidthPX * outIWidth, outIHeight * outHeight, DM_PIXFMT_PALETTE, -1); 2483 outImage = dmImageAlloc(outWidthPX * outIWidth, outIHeight * outHeight, DM_PIXFMT_PALETTE, -1);
2484 } 2484 }
2485 2485
2486 if ((err = dmC64SetImagePalette(outImage, &optC64Spec, FALSE)) != DMERR_OK) 2486 if ((err = dmC64SetImagePalette(outImage, &optC64Spec, false)) != DMERR_OK)
2487 { 2487 {
2488 dmErrorMsg("Could not allocate C64 palette for output image: %d\n", err); 2488 dmErrorMsg("Could not allocate C64 palette for output image: %d\n", err);
2489 goto out; 2489 goto out;
2490 } 2490 }
2491 2491
2634 goto out; 2634 goto out;
2635 2635
2636 case 2: 2636 case 2:
2637 argShowHelp(); 2637 argShowHelp();
2638 argShowFormats(); 2638 argShowFormats();
2639 argShowC64Formats(stdout, TRUE, TRUE); 2639 argShowC64Formats(stdout, true, true);
2640 argShowC64PaletteHelp(stdout); 2640 argShowC64PaletteHelp(stdout);
2641 2641
2642 for (int n = 0; n < optListN; n++) 2642 for (int n = 0; n < optListN; n++)
2643 { 2643 {
2644 const char *str = argGetHelpTopic(optList[n].id); 2644 const char *str = argGetHelpTopic(optList[n].id);
2647 } 2647 }
2648 goto out; 2648 goto out;
2649 2649
2650 case 3: 2650 case 3:
2651 argShowFormats(); 2651 argShowFormats();
2652 argShowC64Formats(stdout, TRUE, dmVerbosity > 0); 2652 argShowC64Formats(stdout, true, dmVerbosity > 0);
2653 goto out; 2653 goto out;
2654 } 2654 }
2655 2655
2656 // Determine input format, if not specified 2656 // Determine input format, if not specified
2657 if (optInType == FFMT_AUTO && optInFilename != NULL) 2657 if (optInType == FFMT_AUTO && optInFilename != NULL)
2875 optC64Palette = &dmC64DefaultPalettes[0]; 2875 optC64Palette = &dmC64DefaultPalettes[0];
2876 2876
2877 dmMsg(1, "Using internal palette '%s' (%s).\n", 2877 dmMsg(1, "Using internal palette '%s' (%s).\n",
2878 optC64Palette->name, optC64Palette->desc); 2878 optC64Palette->name, optC64Palette->desc);
2879 2879
2880 if ((res = dmC64PaletteFromC64Palette(&optPaletteData, optC64Palette, FALSE)) != DMERR_OK) 2880 if ((res = dmC64PaletteFromC64Palette(&optPaletteData, optC64Palette, false)) != DMERR_OK)
2881 { 2881 {
2882 dmErrorMsg("Could not set up palette: %s.\n", 2882 dmErrorMsg("Could not set up palette: %s.\n",
2883 dmErrorStr(res)); 2883 dmErrorStr(res));
2884 goto out; 2884 goto out;
2885 } 2885 }
2905 if (optC64Palette != NULL) 2905 if (optC64Palette != NULL)
2906 { 2906 {
2907 dmMsg(1, "Using internal palette '%s' (%s).\n", 2907 dmMsg(1, "Using internal palette '%s' (%s).\n",
2908 optC64Palette->name, optC64Palette->desc); 2908 optC64Palette->name, optC64Palette->desc);
2909 2909
2910 if ((res = dmC64PaletteFromC64Palette(&optPaletteData, optC64Palette, FALSE)) != DMERR_OK) 2910 if ((res = dmC64PaletteFromC64Palette(&optPaletteData, optC64Palette, false)) != DMERR_OK)
2911 { 2911 {
2912 dmErrorMsg("Could not set up palette: %s.\n", 2912 dmErrorMsg("Could not set up palette: %s.\n",
2913 dmErrorStr(res)); 2913 dmErrorStr(res));
2914 goto out; 2914 goto out;
2915 } 2915 }