comparison tools/gfxconv.c @ 2459:567b4543f73b

Rename labels more unformly.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 16 Apr 2020 07:48:36 +0300
parents f5f8001490ae
children 2723cf62d039
comparison
equal deleted inserted replaced
2458:f6278408bfae 2459:567b4543f73b
406 BOOL dmParseMapOptionMapItem(const char *popt, DMMapValue *value, const unsigned int nmax, const char *msg) 406 BOOL dmParseMapOptionMapItem(const char *popt, DMMapValue *value, const unsigned int nmax, const char *msg)
407 { 407 {
408 char *end, *split, *opt = dm_strdup(popt); 408 char *end, *split, *opt = dm_strdup(popt);
409 409
410 if (opt == NULL) 410 if (opt == NULL)
411 goto error; 411 goto out;
412 412
413 if ((end = split = strchr(opt, ':')) == NULL) 413 if ((end = split = strchr(opt, ':')) == NULL)
414 { 414 {
415 dmErrorMsg("Invalid %s value '%s', expected <(#|%%)RRGGBB|[$|0x]index>:<[$|0x]index>.\n", 415 dmErrorMsg("Invalid %s value '%s', expected <(#|%%)RRGGBB|[$|0x]index>:<[$|0x]index>.\n",
416 msg, opt); 416 msg, opt);
417 goto error; 417 goto out;
418 } 418 }
419 419
420 // Trim whitespace 420 // Trim whitespace
421 *end = 0; 421 *end = 0;
422 for (end--; end > opt && *end && isspace(*end); end--) 422 for (end--; end > opt && *end && isspace(*end); end--)
436 else 436 else
437 if (sscanf(opt + 1, "%2x%2x%2x", &colR, &colG, &colB) != 3 && 437 if (sscanf(opt + 1, "%2x%2x%2x", &colR, &colG, &colB) != 3 &&
438 sscanf(opt + 1, "%2X%2X%2X", &colR, &colG, &colB) != 3) 438 sscanf(opt + 1, "%2X%2X%2X", &colR, &colG, &colB) != 3)
439 { 439 {
440 dmErrorMsg("Invalid %s value '%s', expected a hex triplet, got '%s'.\n", msg, popt, opt + 1); 440 dmErrorMsg("Invalid %s value '%s', expected a hex triplet, got '%s'.\n", msg, popt, opt + 1);
441 goto error; 441 goto out;
442 } 442 }
443 443
444 value->color.r = colR; 444 value->color.r = colR;
445 value->color.g = colG; 445 value->color.g = colG;
446 value->color.b = colB; 446 value->color.b = colB;
449 else 449 else
450 { 450 {
451 if (!dmGetIntVal(opt, &value->from, NULL)) 451 if (!dmGetIntVal(opt, &value->from, NULL))
452 { 452 {
453 dmErrorMsg("Invalid %s value '%s', could not parse source value '%s'.\n", msg, popt, opt); 453 dmErrorMsg("Invalid %s value '%s', could not parse source value '%s'.\n", msg, popt, opt);
454 goto error; 454 goto out;
455 } 455 }
456 value->triplet = FALSE; 456 value->triplet = FALSE;
457 } 457 }
458 458
459 // Trim whitespace 459 // Trim whitespace
462 462
463 // Parse destination value 463 // Parse destination value
464 if (!dmGetIntVal(split, &value->to, NULL)) 464 if (!dmGetIntVal(split, &value->to, NULL))
465 { 465 {
466 dmErrorMsg("Invalid %s value '%s', could not parse destination value '%s'.\n", msg, popt, split); 466 dmErrorMsg("Invalid %s value '%s', could not parse destination value '%s'.\n", msg, popt, split);
467 goto error; 467 goto out;
468 } 468 }
469 469
470 if (!value->triplet && value->from > 255) 470 if (!value->triplet && value->from > 255)
471 { 471 {
472 dmErrorMsg("Invalid %s map source color index value %d, must be [0..255].\n", msg, value->from); 472 dmErrorMsg("Invalid %s map source color index value %d, must be [0..255].\n", msg, value->from);
473 goto error; 473 goto out;
474 } 474 }
475 475
476 if (value->to > nmax) 476 if (value->to > nmax)
477 { 477 {
478 dmErrorMsg("Invalid %s map destination color index value %d, must be [0..%d].\n", msg, value->to, nmax); 478 dmErrorMsg("Invalid %s map destination color index value %d, must be [0..%d].\n", msg, value->to, nmax);
479 goto error; 479 goto out;
480 } 480 }
481 481
482 dmFree(opt); 482 dmFree(opt);
483 return TRUE; 483 return TRUE;
484 484
485 error: 485 out:
486 dmFree(opt); 486 dmFree(opt);
487 return FALSE; 487 return FALSE;
488 } 488 }
489 489
490 490
572 while (*start && isspace(*start)) start++; 572 while (*start && isspace(*start)) start++;
573 573
574 if (*start != 0 && *start != ';') 574 if (*start != 0 && *start != ';')
575 { 575 {
576 if (!dmParseMapOptionMapItem(line, &values[*nvalue], nmax, "mapping file")) 576 if (!dmParseMapOptionMapItem(line, &values[*nvalue], nmax, "mapping file"))
577 goto error; 577 goto out;
578 578
579 (*nvalue)++; 579 (*nvalue)++;
580 if (*nvalue >= nmax) 580 if (*nvalue >= nmax)
581 { 581 {
582 dmErrorMsg("Too many mapping pairs in '%s', maximum is %d.\n", 582 dmErrorMsg("Too many mapping pairs in '%s', maximum is %d.\n",
583 filename, nmax); 583 filename, nmax);
584 goto error; 584 goto out;
585 } 585 }
586 } 586 }
587 } 587 }
588 588
589 error: 589 out:
590 fclose(fp); 590 fclose(fp);
591 return res; 591 return res;
592 } 592 }
593 593
594 594
1078 1078
1079 if (mapping == NULL || mapped == NULL || used == NULL) 1079 if (mapping == NULL || mapped == NULL || used == NULL)
1080 { 1080 {
1081 res = dmError(DMERR_MALLOC, 1081 res = dmError(DMERR_MALLOC,
1082 "Could not allocate memory for reused palette.\n"); 1082 "Could not allocate memory for reused palette.\n");
1083 goto error; 1083 goto out;
1084 } 1084 }
1085 1085
1086 if ((res = dmPaletteAlloc(&tmpPal, src->pal->ncolors, -1)) != DMERR_OK) 1086 if ((res = dmPaletteAlloc(&tmpPal, src->pal->ncolors, -1)) != DMERR_OK)
1087 { 1087 {
1088 dmErrorMsg("Could not allocate memory for remap palette.\n"); 1088 dmErrorMsg("Could not allocate memory for remap palette.\n");
1089 goto error; 1089 goto out;
1090 } 1090 }
1091 1091
1092 if ((dst = *pdst = dmImageAlloc(src->width, src->height, src->pixfmt, src->bpp)) == NULL) 1092 if ((dst = *pdst = dmImageAlloc(src->width, src->height, src->pixfmt, src->bpp)) == NULL)
1093 { 1093 {
1094 res = dmError(DMERR_MALLOC, 1094 res = dmError(DMERR_MALLOC,
1095 "Could not allocate memory for remapped image.\n"); 1095 "Could not allocate memory for remapped image.\n");
1096 goto error; 1096 goto out;
1097 } 1097 }
1098 1098
1099 dmMsg(1, "Remapping %d output image colors of %d colors.\n", 1099 dmMsg(1, "Remapping %d output image colors of %d colors.\n",
1100 optNRemapTable, src->pal->ncolors); 1100 optNRemapTable, src->pal->ncolors);
1101 1101
1217 // Set new palette, free memory 1217 // Set new palette, free memory
1218 if ((res = dmPaletteCopy(&dst->pal, tmpPal)) != DMERR_OK) 1218 if ((res = dmPaletteCopy(&dst->pal, tmpPal)) != DMERR_OK)
1219 { 1219 {
1220 res = dmError(DMERR_MALLOC, 1220 res = dmError(DMERR_MALLOC,
1221 "Could not allocate memory for final remapped palette.\n"); 1221 "Could not allocate memory for final remapped palette.\n");
1222 goto error; 1222 goto out;
1223 } 1223 }
1224 1224
1225 error: 1225 out:
1226 dmFree(mapping); 1226 dmFree(mapping);
1227 dmFree(mapped); 1227 dmFree(mapped);
1228 dmFree(used); 1228 dmFree(used);
1229 return res; 1229 return res;
1230 } 1230 }
1427 // Encode to target format 1427 // Encode to target format
1428 dmMsg(1, "Encoding C64 bitmap data to format '%s'\n", fmt->name); 1428 dmMsg(1, "Encoding C64 bitmap data to format '%s'\n", fmt->name);
1429 if ((res = dmC64EncodeBMP(&buf, image, fmt)) != DMERR_OK) 1429 if ((res = dmC64EncodeBMP(&buf, image, fmt)) != DMERR_OK)
1430 { 1430 {
1431 dmErrorMsg("Error encoding bitmap data: %s\n", dmErrorStr(res)); 1431 dmErrorMsg("Error encoding bitmap data: %s\n", dmErrorStr(res));
1432 goto error; 1432 goto out;
1433 } 1433 }
1434 1434
1435 // And output the file 1435 // And output the file
1436 dmMsg(1, "Writing output file '%s', %" DM_PRIu_SIZE_T " bytes.\n", 1436 dmMsg(1, "Writing output file '%s', %" DM_PRIu_SIZE_T " bytes.\n",
1437 filename, buf.len); 1437 filename, buf.len);
1438 1438
1439 res = dmWriteDataFile(NULL, filename, buf.data, buf.len); 1439 res = dmWriteDataFile(NULL, filename, buf.data, buf.len);
1440 1440
1441 error: 1441 out:
1442 dmGrowBufFree(&buf); 1442 dmGrowBufFree(&buf);
1443 return res; 1443 return res;
1444 } 1444 }
1445 1445
1446 1446
1536 if ((hdrFilename = dm_strdup_fext(filename, "%s.inc")) == NULL || 1536 if ((hdrFilename = dm_strdup_fext(filename, "%s.inc")) == NULL ||
1537 (prefix = dm_strdup_fext(filename, "img_%s")) == NULL) 1537 (prefix = dm_strdup_fext(filename, "img_%s")) == NULL)
1538 { 1538 {
1539 res = dmError(DMERR_MALLOC, 1539 res = dmError(DMERR_MALLOC,
1540 "Could not allocate memory for filename strings? :O\n"); 1540 "Could not allocate memory for filename strings? :O\n");
1541 goto err; 1541 goto out;
1542 } 1542 }
1543 1543
1544 // Replace any non-alphanumerics in palette ID 1544 // Replace any non-alphanumerics in palette ID
1545 for (int i = 0; prefix[i]; i++) 1545 for (int i = 0; prefix[i]; i++)
1546 prefix[i] = isalnum(prefix[i]) ? tolower(prefix[i]) : '_'; 1546 prefix[i] = isalnum(prefix[i]) ? tolower(prefix[i]) : '_';
1582 1582
1583 if ((res = dmf_open_stdio(filename, "wb", &fp)) != DMERR_OK) 1583 if ((res = dmf_open_stdio(filename, "wb", &fp)) != DMERR_OK)
1584 { 1584 {
1585 dmErrorMsg("Could not open file '%s' for writing.\n", 1585 dmErrorMsg("Could not open file '%s' for writing.\n",
1586 filename); 1586 filename);
1587 goto err; 1587 goto out;
1588 } 1588 }
1589 1589
1590 res = fmt->write(fp, image, spec); 1590 res = fmt->write(fp, image, spec);
1591 1591
1592 dmf_close(fp); 1592 dmf_close(fp);
1593 } 1593 }
1594 1594
1595 err: 1595 out:
1596 if (allocated) 1596 if (allocated)
1597 dmImageFree(image); 1597 dmImageFree(image);
1598 1598
1599 return res; 1599 return res;
1600 } 1600 }
1616 dmMsg(1, "Outputting '%s' format palette of %d entries.\n", 1616 dmMsg(1, "Outputting '%s' format palette of %d entries.\n",
1617 fmt->name, palette->ncolors); 1617 fmt->name, palette->ncolors);
1618 1618
1619 if ((res = dmf_open_stdio(filename, "wb", &fp)) != DMERR_OK) 1619 if ((res = dmf_open_stdio(filename, "wb", &fp)) != DMERR_OK)
1620 { 1620 {
1621 dmErrorMsg("Could not open file '%s' for writing.\n", 1621 dmErrorMsg("Could not open file '%s' for writing: %s\n",
1622 filename); 1622 filename, dmErrorStr(res));
1623 goto err; 1623 goto out;
1624 } 1624 }
1625 1625
1626 res = fmt->write(fp, palette); 1626 res = fmt->write(fp, palette);
1627 1627
1628 err: 1628 out:
1629 dmf_close(fp); 1629 dmf_close(fp);
1630 1630
1631 return res; 1631 return res;
1632 } 1632 }
1633 1633
1727 break; 1727 break;
1728 1728
1729 default: 1729 default:
1730 ret = dmError(DMERR_INVALID_ARGS, 1730 ret = dmError(DMERR_INVALID_ARGS,
1731 "Invalid output format %d, internal error.\n", outFormat); 1731 "Invalid output format %d, internal error.\n", outFormat);
1732 goto error; 1732 goto out;
1733 } 1733 }
1734 1734
1735 if (outBlockW < 1 || outBlockH < 1) 1735 if (outBlockW < 1 || outBlockH < 1)
1736 { 1736 {
1737 ret = dmError(DMERR_INVALID_ARGS, 1737 ret = dmError(DMERR_INVALID_ARGS,
1738 "Source image dimensions too small for conversion, block dimensions %d x %d.\n", 1738 "Source image dimensions too small for conversion, block dimensions %d x %d.\n",
1739 outBlockW, outBlockH); 1739 outBlockW, outBlockH);
1740 goto error; 1740 goto out;
1741 } 1741 }
1742 1742
1743 if ((outFile = fopen(filename, "wb")) == NULL) 1743 if ((outFile = fopen(filename, "wb")) == NULL)
1744 { 1744 {
1745 ret = dmGetErrno(); 1745 ret = dmGetErrno();
1746 dmErrorMsg("Could not open '%s' for writing: %s.\n", 1746 dmErrorMsg("Could not open '%s' for writing: %s.\n",
1747 filename, dmErrorStr(ret)); 1747 filename, dmErrorStr(ret));
1748 goto error; 1748 goto out;
1749 } 1749 }
1750 1750
1751 if ((tmpBuf = dmMalloc(outBufSize)) == NULL) 1751 if ((tmpBuf = dmMalloc(outBufSize)) == NULL)
1752 { 1752 {
1753 dmErrorMsg("Could not allocate %" DM_PRIu_SIZE_T 1753 dmErrorMsg("Could not allocate %" DM_PRIu_SIZE_T
1754 " bytes for conversion buffer.\n", 1754 " bytes for conversion buffer.\n",
1755 outBufSize); 1755 outBufSize);
1756 goto error; 1756 goto out;
1757 } 1757 }
1758 1758
1759 dmMsg(1, "Writing %d x %d = %d blocks of %s data...\n", 1759 dmMsg(1, "Writing %d x %d = %d blocks of %s data...\n",
1760 outBlockW, outBlockH, outBlockW * outBlockH, outType); 1760 outBlockW, outBlockH, outBlockW * outBlockH, outType);
1761 1761
1768 if (!dmConvertImage2Char(tmpBuf, image, 1768 if (!dmConvertImage2Char(tmpBuf, image,
1769 bx * D64_CHR_WIDTH_PX, by * D64_CHR_HEIGHT_PX, 1769 bx * D64_CHR_WIDTH_PX, by * D64_CHR_HEIGHT_PX,
1770 multicolor)) 1770 multicolor))
1771 { 1771 {
1772 ret = DMERR_DATA_ERROR; 1772 ret = DMERR_DATA_ERROR;
1773 goto error; 1773 goto out;
1774 } 1774 }
1775 break; 1775 break;
1776 1776
1777 case FFMT_SPRITE: 1777 case FFMT_SPRITE:
1778 if (!dmConvertImage2Sprite(tmpBuf, image, 1778 if (!dmConvertImage2Sprite(tmpBuf, image,
1779 bx * D64_SPR_WIDTH_PX, by * D64_SPR_HEIGHT_PX, 1779 bx * D64_SPR_WIDTH_PX, by * D64_SPR_HEIGHT_PX,
1780 multicolor)) 1780 multicolor))
1781 { 1781 {
1782 ret = DMERR_DATA_ERROR; 1782 ret = DMERR_DATA_ERROR;
1783 goto error; 1783 goto out;
1784 } 1784 }
1785 break; 1785 break;
1786 } 1786 }
1787 1787
1788 if (!dm_fwrite_str(outFile, tmpBuf, outBufSize)) 1788 if (!dm_fwrite_str(outFile, tmpBuf, outBufSize))
1789 { 1789 {
1790 ret = dmGetErrno(); 1790 ret = dmGetErrno();
1791 dmError(ret, "Error writing data block %d,%d to '%s': %s.\n", 1791 dmError(ret, "Error writing data block %d,%d to '%s': %s.\n",
1792 bx, by, filename, dmErrorStr(ret)); 1792 bx, by, filename, dmErrorStr(ret));
1793 goto error; 1793 goto out;
1794 } 1794 }
1795 } 1795 }
1796 1796
1797 error: 1797 out:
1798 // Cleanup 1798 // Cleanup
1799 if (outFile != NULL) 1799 if (outFile != NULL)
1800 fclose(outFile); 1800 fclose(outFile);
1801 1801
1802 dmFree(tmpBuf); 1802 dmFree(tmpBuf);
1845 if ((outFile = fopen(optOutFilename, "w")) == NULL) 1845 if ((outFile = fopen(optOutFilename, "w")) == NULL)
1846 { 1846 {
1847 ret = dmGetErrno(); 1847 ret = dmGetErrno();
1848 dmError(ret, "Error opening output file '%s': %s.\n", 1848 dmError(ret, "Error opening output file '%s': %s.\n",
1849 optOutFilename, dmErrorStr(ret)); 1849 optOutFilename, dmErrorStr(ret));
1850 goto error; 1850 goto out;
1851 } 1851 }
1852 1852
1853 while (offs + outSize < dataSize && !error && (optItemCount < 0 || itemCount < optItemCount)) 1853 while (offs + outSize < dataSize && !error && (optItemCount < 0 || itemCount < optItemCount))
1854 { 1854 {
1855 fprintf(outFile, "---- : -------------- #%d\n", itemCount); 1855 fprintf(outFile, "---- : -------------- #%d\n", itemCount);
1879 if (optSequential) 1879 if (optSequential)
1880 { 1880 {
1881 if (optOutFilename == NULL) 1881 if (optOutFilename == NULL)
1882 { 1882 {
1883 dmErrorMsg("Sequential image output requires filename template.\n"); 1883 dmErrorMsg("Sequential image output requires filename template.\n");
1884 goto error; 1884 goto out;
1885 } 1885 }
1886 1886
1887 outImage = dmImageAlloc(outWidthPX, outHeight, DM_PIXFMT_PALETTE, -1); 1887 outImage = dmImageAlloc(outWidthPX, outHeight, DM_PIXFMT_PALETTE, -1);
1888 dmMsg(1, "Outputting sequence of %d images @ %d x %d -> %d x %d.\n", 1888 dmMsg(1, "Outputting sequence of %d images @ %d x %d -> %d x %d.\n",
1889 optItemCount, 1889 optItemCount,
1895 { 1895 {
1896 int outIWidth, outIHeight; 1896 int outIWidth, outIHeight;
1897 if (optItemCount <= 0) 1897 if (optItemCount <= 0)
1898 { 1898 {
1899 dmErrorMsg("Single-image output requires count to be set (-n).\n"); 1899 dmErrorMsg("Single-image output requires count to be set (-n).\n");
1900 goto error; 1900 goto out;
1901 } 1901 }
1902 1902
1903 outIWidth = optPlanedWidth; 1903 outIWidth = optPlanedWidth;
1904 outIHeight = (optItemCount / optPlanedWidth); 1904 outIHeight = (optItemCount / optPlanedWidth);
1905 if (optItemCount % optPlanedWidth) 1905 if (optItemCount % optPlanedWidth)
1909 } 1909 }
1910 1910
1911 if ((err = dmC64SetImagePalette(outImage, &optC64Spec, FALSE)) != DMERR_OK) 1911 if ((err = dmC64SetImagePalette(outImage, &optC64Spec, FALSE)) != DMERR_OK)
1912 { 1912 {
1913 dmErrorMsg("Could not allocate C64 palette for output image: %d\n", err); 1913 dmErrorMsg("Could not allocate C64 palette for output image: %d\n", err);
1914 goto error; 1914 goto out;
1915 } 1915 }
1916 1916
1917 while (offs + outSize < dataSize && (optItemCount < 0 || itemCount < optItemCount)) 1917 while (offs + outSize < dataSize && (optItemCount < 0 || itemCount < optItemCount))
1918 { 1918 {
1919 if ((err = dmC64ConvertCSDataToImage(outImage, outX * outWidthPX, outY * outHeight, 1919 if ((err = dmC64ConvertCSDataToImage(outImage, outX * outWidthPX, outY * outHeight,
1920 dataBuf + offs, outWidth, outHeight, optInMulticolor, optColorMap)) != DMERR_OK) 1920 dataBuf + offs, outWidth, outHeight, optInMulticolor, optColorMap)) != DMERR_OK)
1921 { 1921 {
1922 dmErrorMsg("Internal error in conversion of raw data to bitmap: %d.\n", err); 1922 dmErrorMsg("Internal error in conversion of raw data to bitmap: %d.\n", err);
1923 goto error; 1923 goto out;
1924 } 1924 }
1925 1925
1926 if (optSequential) 1926 if (optSequential)
1927 { 1927 {
1928 outFilename = dm_strdup_printf("%s%04d.%s", 1928 outFilename = dm_strdup_printf("%s%04d.%s",
1931 convFormatList[optOutType].fext); 1931 convFormatList[optOutType].fext);
1932 1932
1933 if (outFilename == NULL) 1933 if (outFilename == NULL)
1934 { 1934 {
1935 dmErrorMsg("Could not allocate memory for filename template?\n"); 1935 dmErrorMsg("Could not allocate memory for filename template?\n");
1936 goto error; 1936 goto out;
1937 } 1937 }
1938 1938
1939 ret = dmWriteImage(outFilename, outImage, &optSpec, 1939 ret = dmWriteImage(outFilename, outImage, &optSpec,
1940 &dmImageFormatList[optOutFormat]); 1940 &dmImageFormatList[optOutFormat]);
1941 1941
1979 { 1979 {
1980 if (optSequential) 1980 if (optSequential)
1981 { 1981 {
1982 ret = dmError(DMERR_INVALID_ARGS, 1982 ret = dmError(DMERR_INVALID_ARGS,
1983 "Sequential output not supported for spr/char -> bitmap conversion.\n"); 1983 "Sequential output not supported for spr/char -> bitmap conversion.\n");
1984 goto error; 1984 goto out;
1985 } 1985 }
1986 } 1986 }
1987 1987
1988 error: 1988 out:
1989 return ret; 1989 return ret;
1990 } 1990 }
1991 1991
1992 1992
1993 int main(int argc, char *argv[]) 1993 int main(int argc, char *argv[])