Mercurial > hg > dmlib
comparison tools/gfxconv.c @ 1479:c4755b73b93c
Improve how input skipping is done. This, however, currently breaks skipping for
non-c64 image format inputs.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 11 May 2018 02:46:59 +0300 |
parents | 499835ba215c |
children | c3eba254bcc2 |
comparison
equal
deleted
inserted
replaced
1478:d883b4c1cf48 | 1479:c4755b73b93c |
---|---|
834 } | 834 } |
835 } | 835 } |
836 } | 836 } |
837 | 837 |
838 | 838 |
839 void dmDumpCharASCII(FILE *outFile, const Uint8 *buf, int *offs, int format, BOOL multicolor) | 839 void dmDumpCharASCII(FILE *outFile, const Uint8 *buf, const size_t offs, const int fmt, const BOOL multicolor) |
840 { | 840 { |
841 int yc; | 841 for (size_t yc = 0; yc < C64_CHR_HEIGHT; yc++) |
842 | 842 { |
843 for (yc = 0; yc < C64_CHR_HEIGHT; yc++) | 843 fprintf(outFile, "%04" DM_PRIx_SIZE_T " : ", offs + yc); |
844 { | 844 dmPrintByte(outFile, buf[yc], fmt, multicolor); |
845 fprintf(outFile, "%04x : ", *offs); | |
846 dmPrintByte(outFile, buf[yc], format, multicolor); | |
847 fprintf(outFile, "\n"); | 845 fprintf(outFile, "\n"); |
848 (*offs)++; | 846 } |
849 } | 847 } |
850 } | 848 |
851 | 849 |
852 | 850 void dmDumpSpriteASCII(FILE *outFile, const Uint8 *buf, const size_t offs, const int fmt, BOOL multicolor) |
853 void dmDumpSpriteASCII(FILE *outFile, const Uint8 *buf, int *offs, int format, BOOL multicolor) | 851 { |
854 { | 852 size_t bufOffs, xc, yc; |
855 int bufOffs, xc, yc; | |
856 | 853 |
857 for (bufOffs = yc = 0; yc < C64_SPR_HEIGHT; yc++) | 854 for (bufOffs = yc = 0; yc < C64_SPR_HEIGHT; yc++) |
858 { | 855 { |
859 fprintf(outFile, "%04x : ", *offs); | 856 fprintf(outFile, "%04" DM_PRIx_SIZE_T " ", offs + bufOffs); |
860 for (xc = 0; xc < C64_SPR_WIDTH; xc++) | 857 for (xc = 0; xc < C64_SPR_WIDTH; xc++) |
861 { | 858 { |
862 dmPrintByte(outFile, buf[bufOffs], format, multicolor); | 859 dmPrintByte(outFile, buf[bufOffs], fmt, multicolor); |
863 fprintf(outFile, " "); | 860 fprintf(outFile, " "); |
864 bufOffs++; | 861 bufOffs++; |
865 (*offs)++; | |
866 } | 862 } |
867 fprintf(outFile, "\n"); | 863 fprintf(outFile, "\n"); |
868 } | 864 } |
869 (*offs)++; | |
870 } | 865 } |
871 | 866 |
872 | 867 |
873 int dmRemapImageColors(DMImage **pdst, const DMImage *src) | 868 int dmRemapImageColors(DMImage **pdst, const DMImage *src) |
874 { | 869 { |
1407 dmFree(buf); | 1402 dmFree(buf); |
1408 return ret; | 1403 return ret; |
1409 } | 1404 } |
1410 | 1405 |
1411 | 1406 |
1412 int dmDumpSpritesAndChars(FILE *inFile) | 1407 int dmDumpSpritesAndChars(const Uint8 *dataBuf, const size_t dataSize, const size_t realOffs) |
1413 { | 1408 { |
1414 int dataOffs, itemCount, outWidth, outWidthPX, outHeight; | 1409 int ret, itemCount, outWidth, outWidthPX, outHeight; |
1415 int ret = DMERR_OK; | 1410 size_t offs, outSize; |
1416 size_t bufSize; | |
1417 Uint8 *bufData; | |
1418 | 1411 |
1419 switch (optInFormat) | 1412 switch (optInFormat) |
1420 { | 1413 { |
1421 case FFMT_CHAR: | 1414 case FFMT_CHAR: |
1422 bufSize = C64_CHR_SIZE; | 1415 outSize = C64_CHR_SIZE; |
1423 outWidth = C64_CHR_WIDTH; | 1416 outWidth = C64_CHR_WIDTH; |
1424 outWidthPX = C64_CHR_WIDTH_PX; | 1417 outWidthPX = C64_CHR_WIDTH_PX; |
1425 outHeight = C64_CHR_HEIGHT; | 1418 outHeight = C64_CHR_HEIGHT; |
1426 break; | 1419 break; |
1427 | 1420 |
1428 case FFMT_SPRITE: | 1421 case FFMT_SPRITE: |
1429 bufSize = C64_SPR_SIZE; | 1422 outSize = C64_SPR_SIZE; |
1430 outWidth = C64_SPR_WIDTH; | 1423 outWidth = C64_SPR_WIDTH; |
1431 outWidthPX = C64_SPR_WIDTH_PX; | 1424 outWidthPX = C64_SPR_WIDTH_PX; |
1432 outHeight = C64_SPR_HEIGHT; | 1425 outHeight = C64_SPR_HEIGHT; |
1433 break; | 1426 break; |
1434 | 1427 |
1435 default: | 1428 default: |
1436 return dmError(DMERR_INTERNAL, | 1429 return dmError(DMERR_INTERNAL, |
1437 "Invalid input format %d, internal error.\n", optInFormat); | 1430 "Invalid input format %d, internal error.\n", optInFormat); |
1438 } | 1431 } |
1439 | 1432 |
1440 if ((bufData = dmMalloc(bufSize)) == NULL) | 1433 offs = 0; |
1441 { | |
1442 return dmError(DMERR_INTERNAL, | |
1443 "Could not allocate temporary buffer of %d bytes.\n", bufSize); | |
1444 } | |
1445 | |
1446 | |
1447 dataOffs = optInSkip; | |
1448 itemCount = 0; | 1434 itemCount = 0; |
1449 | 1435 |
1450 if (optOutFormat == FFMT_ANSI || optOutFormat == FFMT_ASCII) | 1436 if (optOutFormat == FFMT_ANSI || optOutFormat == FFMT_ASCII) |
1451 { | 1437 { |
1452 BOOL error = FALSE; | 1438 BOOL error = FALSE; |
1461 dmError(ret, "Error opening output file '%s', %d: %s\n", | 1447 dmError(ret, "Error opening output file '%s', %d: %s\n", |
1462 optOutFilename, ret, dmErrorStr(ret)); | 1448 optOutFilename, ret, dmErrorStr(ret)); |
1463 goto error; | 1449 goto error; |
1464 } | 1450 } |
1465 | 1451 |
1466 while (!feof(inFile) && !error && (optItemCount < 0 || itemCount < optItemCount)) | 1452 while (offs + outSize < dataSize && !error && (optItemCount < 0 || itemCount < optItemCount)) |
1467 { | 1453 { |
1468 dmMemset(bufData, 0, bufSize); | |
1469 | |
1470 if (fread(bufData, 1, bufSize, inFile) != bufSize) | |
1471 { | |
1472 dmErrorMsg("Could not read full bufferful (%d bytes) of data at 0x%x.\n", | |
1473 bufSize, dataOffs); | |
1474 error = TRUE; | |
1475 } | |
1476 | |
1477 fprintf(outFile, "---- : -------------- #%d\n", itemCount); | 1454 fprintf(outFile, "---- : -------------- #%d\n", itemCount); |
1478 | 1455 |
1479 switch (optInFormat) | 1456 switch (optInFormat) |
1480 { | 1457 { |
1481 case FFMT_CHAR: | 1458 case FFMT_CHAR: |
1482 dmDumpCharASCII(outFile, bufData, &dataOffs, optOutFormat, optInMulticolor); | 1459 dmDumpCharASCII(outFile, dataBuf + offs, realOffs + offs, optOutFormat, optInMulticolor); |
1483 break; | 1460 break; |
1484 case FFMT_SPRITE: | 1461 case FFMT_SPRITE: |
1485 dmDumpSpriteASCII(outFile, bufData, &dataOffs, optOutFormat, optInMulticolor); | 1462 dmDumpSpriteASCII(outFile, dataBuf + offs, realOffs + offs, optOutFormat, optInMulticolor); |
1486 break; | 1463 break; |
1487 } | 1464 } |
1465 offs += outSize; | |
1488 itemCount++; | 1466 itemCount++; |
1489 } | 1467 } |
1490 | 1468 |
1491 fclose(outFile); | 1469 fclose(outFile); |
1492 } | 1470 } |
1528 outImage = dmImageAlloc(outWidthPX * outIWidth, outIHeight * outHeight, DM_IFMT_PALETTE, -1); | 1506 outImage = dmImageAlloc(outWidthPX * outIWidth, outIHeight * outHeight, DM_IFMT_PALETTE, -1); |
1529 } | 1507 } |
1530 | 1508 |
1531 dmSetDefaultC64Palette(outImage); | 1509 dmSetDefaultC64Palette(outImage); |
1532 | 1510 |
1533 while (!feof(inFile) && (optItemCount < 0 || itemCount < optItemCount)) | 1511 while (offs + outSize < dataSize && (optItemCount < 0 || itemCount < optItemCount)) |
1534 { | 1512 { |
1535 dmMemset(bufData, 0, bufSize); | |
1536 | |
1537 if (fread(bufData, 1, bufSize, inFile) != bufSize) | |
1538 { | |
1539 dmErrorMsg("Could not read full bufferful (%d bytes) of data at 0x%x.\n", | |
1540 bufSize, dataOffs); | |
1541 break; | |
1542 } | |
1543 | |
1544 if ((err = dmC64ConvertCSDataToImage(outImage, outX * outWidthPX, outY * outHeight, | 1513 if ((err = dmC64ConvertCSDataToImage(outImage, outX * outWidthPX, outY * outHeight, |
1545 bufData, outWidth, outHeight, optInMulticolor, optColors)) != DMERR_OK) | 1514 dataBuf + offs, outWidth, outHeight, optInMulticolor, optColors)) != DMERR_OK) |
1546 { | 1515 { |
1547 dmErrorMsg("Internal error in conversion of raw data to bitmap: %d.\n", err); | 1516 dmErrorMsg("Internal error in conversion of raw data to bitmap: %d.\n", err); |
1548 break; | 1517 break; |
1549 } | 1518 } |
1550 | 1519 |
1551 if (optSequential) | 1520 if (optSequential) |
1552 { | 1521 { |
1553 outFilename = dm_strdup_printf("%s%04d.%s", optOutFilename, itemCount, convFormatList[optOutFormat].fext); | 1522 outFilename = dm_strdup_printf("%s%04d.%s", |
1523 optOutFilename, | |
1524 itemCount, | |
1525 convFormatList[optOutFormat].fext); | |
1526 | |
1554 if (outFilename == NULL) | 1527 if (outFilename == NULL) |
1555 { | 1528 { |
1556 dmErrorMsg("Could not allocate memory for filename template?\n"); | 1529 dmErrorMsg("Could not allocate memory for filename template?\n"); |
1557 goto error; | 1530 goto error; |
1558 } | 1531 } |
1573 outX = 0; | 1546 outX = 0; |
1574 outY++; | 1547 outY++; |
1575 } | 1548 } |
1576 } | 1549 } |
1577 | 1550 |
1551 offs += outSize; | |
1578 itemCount++; | 1552 itemCount++; |
1579 } | 1553 } |
1580 | 1554 |
1581 if (!optSequential) | 1555 if (!optSequential) |
1582 { | 1556 { |
1599 "Sequential output not supported for spr/char -> bitmap conversion.\n"); | 1573 "Sequential output not supported for spr/char -> bitmap conversion.\n"); |
1600 goto error; | 1574 goto error; |
1601 } | 1575 } |
1602 } | 1576 } |
1603 | 1577 |
1604 dmFree(bufData); | 1578 ret = DMERR_OK; |
1605 return DMERR_OK; | |
1606 | 1579 |
1607 error: | 1580 error: |
1608 dmFree(bufData); | |
1609 return ret; | 1581 return ret; |
1610 } | 1582 } |
1611 | 1583 |
1612 | 1584 |
1613 int main(int argc, char *argv[]) | 1585 int main(int argc, char *argv[]) |
1614 { | 1586 { |
1615 FILE *inFile = NULL; | 1587 FILE *inFile = NULL; |
1616 const DMC64ImageFormat *inC64Fmt = NULL; | 1588 const DMC64ImageFormat *inC64Fmt = NULL; |
1617 DMC64Image *inC64Image = NULL; | 1589 DMC64Image *inC64Image = NULL; |
1618 Uint8 *dataBuf = NULL; | 1590 Uint8 *dataBuf = NULL, *dataBufOrig = NULL; |
1619 size_t dataSize; | 1591 size_t dataSize, dataSizeOrig; |
1620 int i; | 1592 int i; |
1621 | 1593 |
1622 // Default colors | 1594 // Default colors |
1623 for (i = 0; i < C64_MAX_COLORS; i++) | 1595 for (i = 0; i < C64_MAX_COLORS; i++) |
1624 optColors[i] = i; | 1596 optColors[i] = i; |
1657 dmErrorMsg("Standard input cannot be used without specifying input format.\n"); | 1629 dmErrorMsg("Standard input cannot be used without specifying input format.\n"); |
1658 dmErrorMsg("Perhaps you should try using --help\n"); | 1630 dmErrorMsg("Perhaps you should try using --help\n"); |
1659 goto error; | 1631 goto error; |
1660 } | 1632 } |
1661 inFile = stdin; | 1633 inFile = stdin; |
1634 optInFilename = "stdin"; | |
1662 } | 1635 } |
1663 else | 1636 else |
1664 if ((inFile = fopen(optInFilename, "rb")) == NULL) | 1637 if ((inFile = fopen(optInFilename, "rb")) == NULL) |
1665 { | 1638 { |
1666 int res = dmGetErrno(); | 1639 int res = dmGetErrno(); |
1667 dmErrorMsg("Error opening input file '%s', %d: %s\n", | 1640 dmErrorMsg("Error opening input file '%s', %d: %s\n", |
1668 optInFilename, res, dmErrorStr(res)); | 1641 optInFilename, res, dmErrorStr(res)); |
1669 goto error; | 1642 goto error; |
1670 } | 1643 } |
1671 | 1644 |
1672 if (dmReadDataFile(inFile, NULL, &dataBuf, &dataSize) != 0) | 1645 // Read the input .. |
1646 dmMsg(1, "Reading input from '%s'.\n", optInFilename); | |
1647 | |
1648 if (dmReadDataFile(inFile, NULL, &dataBufOrig, &dataSizeOrig) != 0) | |
1673 goto error; | 1649 goto error; |
1674 | 1650 |
1651 // Check and compute the input skip | |
1652 if (optInSkip > dataSizeOrig) | |
1653 { | |
1654 dmErrorMsg("Input skip value %d is larger than input size %d.\n", | |
1655 optInSkip, dataSizeOrig); | |
1656 goto error; | |
1657 } | |
1658 | |
1659 dataBuf = dataBufOrig + optInSkip; | |
1660 dataSize = dataSizeOrig - optInSkip; | |
1661 | |
1662 | |
1663 // Perform probing, if required | |
1675 if (optInFormat == FFMT_AUTO || optInFormat == FFMT_BITMAP) | 1664 if (optInFormat == FFMT_AUTO || optInFormat == FFMT_BITMAP) |
1676 { | 1665 { |
1677 // Probe for format | 1666 // Probe for format |
1678 const DMC64ImageFormat *forced = NULL; | 1667 const DMC64ImageFormat *forced = NULL; |
1679 int res; | 1668 int res; |
1718 { | 1707 { |
1719 dmErrorMsg("No input format specified, and could not be determined automatically.\n"); | 1708 dmErrorMsg("No input format specified, and could not be determined automatically.\n"); |
1720 exit(1); | 1709 exit(1); |
1721 } | 1710 } |
1722 | 1711 |
1723 // Skip, if needed | |
1724 if (fseek(inFile, optInSkip, SEEK_SET) != 0) | |
1725 { | |
1726 int res = dmGetErrno(); | |
1727 dmErrorMsg("Could not seek to file position %d (0x%x): %s\n", | |
1728 optInSkip, optInSkip, dmErrorStr(res)); | |
1729 goto error; | |
1730 } | |
1731 | |
1732 int inFormat = dmGetConvFormat(optInFormat, optInSubFormat), | 1712 int inFormat = dmGetConvFormat(optInFormat, optInSubFormat), |
1733 outFormat = dmGetConvFormat(optOutFormat, optOutSubFormat); | 1713 outFormat = dmGetConvFormat(optOutFormat, optOutSubFormat); |
1734 | 1714 |
1735 if (inFormat != -1 && outFormat != -1) | 1715 if (inFormat != -1 && outFormat != -1) |
1736 { | 1716 { |
1748 | 1728 |
1749 switch (optInFormat) | 1729 switch (optInFormat) |
1750 { | 1730 { |
1751 case FFMT_SPRITE: | 1731 case FFMT_SPRITE: |
1752 case FFMT_CHAR: | 1732 case FFMT_CHAR: |
1753 dmDumpSpritesAndChars(inFile); | 1733 dmDumpSpritesAndChars(dataBuf, dataSize, optInSkip); |
1754 break; | 1734 break; |
1755 | 1735 |
1756 case FFMT_BITMAP: | 1736 case FFMT_BITMAP: |
1757 { | 1737 { |
1758 DMImage *outImage = NULL; | 1738 DMImage *outImage = NULL; |
1856 | 1836 |
1857 error: | 1837 error: |
1858 if (inFile != NULL) | 1838 if (inFile != NULL) |
1859 fclose(inFile); | 1839 fclose(inFile); |
1860 | 1840 |
1861 dmFree(dataBuf); | 1841 dmFree(dataBufOrig); |
1862 dmC64ImageFree(inC64Image); | 1842 dmC64ImageFree(inC64Image); |
1863 | 1843 |
1864 return 0; | 1844 return 0; |
1865 } | 1845 } |