# HG changeset patch # User Matti Hamalainen # Date 1525996019 -10800 # Node ID c4755b73b93c96a4792d65f327122d6c68cc57ad # Parent d883b4c1cf4810969541a380754c3c60396abfd2 Improve how input skipping is done. This, however, currently breaks skipping for non-c64 image format inputs. diff -r d883b4c1cf48 -r c4755b73b93c tools/gfxconv.c --- a/tools/gfxconv.c Fri May 11 02:42:49 2018 +0300 +++ b/tools/gfxconv.c Fri May 11 02:46:59 2018 +0300 @@ -836,37 +836,32 @@ } -void dmDumpCharASCII(FILE *outFile, const Uint8 *buf, int *offs, int format, BOOL multicolor) +void dmDumpCharASCII(FILE *outFile, const Uint8 *buf, const size_t offs, const int fmt, const BOOL multicolor) { - int yc; - - for (yc = 0; yc < C64_CHR_HEIGHT; yc++) + for (size_t yc = 0; yc < C64_CHR_HEIGHT; yc++) { - fprintf(outFile, "%04x : ", *offs); - dmPrintByte(outFile, buf[yc], format, multicolor); + fprintf(outFile, "%04" DM_PRIx_SIZE_T " : ", offs + yc); + dmPrintByte(outFile, buf[yc], fmt, multicolor); fprintf(outFile, "\n"); - (*offs)++; } } -void dmDumpSpriteASCII(FILE *outFile, const Uint8 *buf, int *offs, int format, BOOL multicolor) +void dmDumpSpriteASCII(FILE *outFile, const Uint8 *buf, const size_t offs, const int fmt, BOOL multicolor) { - int bufOffs, xc, yc; + size_t bufOffs, xc, yc; for (bufOffs = yc = 0; yc < C64_SPR_HEIGHT; yc++) { - fprintf(outFile, "%04x : ", *offs); + fprintf(outFile, "%04" DM_PRIx_SIZE_T " ", offs + bufOffs); for (xc = 0; xc < C64_SPR_WIDTH; xc++) { - dmPrintByte(outFile, buf[bufOffs], format, multicolor); + dmPrintByte(outFile, buf[bufOffs], fmt, multicolor); fprintf(outFile, " "); bufOffs++; - (*offs)++; } fprintf(outFile, "\n"); } - (*offs)++; } @@ -1409,24 +1404,22 @@ } -int dmDumpSpritesAndChars(FILE *inFile) +int dmDumpSpritesAndChars(const Uint8 *dataBuf, const size_t dataSize, const size_t realOffs) { - int dataOffs, itemCount, outWidth, outWidthPX, outHeight; - int ret = DMERR_OK; - size_t bufSize; - Uint8 *bufData; + int ret, itemCount, outWidth, outWidthPX, outHeight; + size_t offs, outSize; switch (optInFormat) { case FFMT_CHAR: - bufSize = C64_CHR_SIZE; + outSize = C64_CHR_SIZE; outWidth = C64_CHR_WIDTH; outWidthPX = C64_CHR_WIDTH_PX; outHeight = C64_CHR_HEIGHT; break; case FFMT_SPRITE: - bufSize = C64_SPR_SIZE; + outSize = C64_SPR_SIZE; outWidth = C64_SPR_WIDTH; outWidthPX = C64_SPR_WIDTH_PX; outHeight = C64_SPR_HEIGHT; @@ -1437,14 +1430,7 @@ "Invalid input format %d, internal error.\n", optInFormat); } - if ((bufData = dmMalloc(bufSize)) == NULL) - { - return dmError(DMERR_INTERNAL, - "Could not allocate temporary buffer of %d bytes.\n", bufSize); - } - - - dataOffs = optInSkip; + offs = 0; itemCount = 0; if (optOutFormat == FFMT_ANSI || optOutFormat == FFMT_ASCII) @@ -1463,28 +1449,20 @@ goto error; } - while (!feof(inFile) && !error && (optItemCount < 0 || itemCount < optItemCount)) + while (offs + outSize < dataSize && !error && (optItemCount < 0 || itemCount < optItemCount)) { - dmMemset(bufData, 0, bufSize); - - if (fread(bufData, 1, bufSize, inFile) != bufSize) - { - dmErrorMsg("Could not read full bufferful (%d bytes) of data at 0x%x.\n", - bufSize, dataOffs); - error = TRUE; - } - fprintf(outFile, "---- : -------------- #%d\n", itemCount); switch (optInFormat) { case FFMT_CHAR: - dmDumpCharASCII(outFile, bufData, &dataOffs, optOutFormat, optInMulticolor); + dmDumpCharASCII(outFile, dataBuf + offs, realOffs + offs, optOutFormat, optInMulticolor); break; case FFMT_SPRITE: - dmDumpSpriteASCII(outFile, bufData, &dataOffs, optOutFormat, optInMulticolor); + dmDumpSpriteASCII(outFile, dataBuf + offs, realOffs + offs, optOutFormat, optInMulticolor); break; } + offs += outSize; itemCount++; } @@ -1530,19 +1508,10 @@ dmSetDefaultC64Palette(outImage); - while (!feof(inFile) && (optItemCount < 0 || itemCount < optItemCount)) + while (offs + outSize < dataSize && (optItemCount < 0 || itemCount < optItemCount)) { - dmMemset(bufData, 0, bufSize); - - if (fread(bufData, 1, bufSize, inFile) != bufSize) - { - dmErrorMsg("Could not read full bufferful (%d bytes) of data at 0x%x.\n", - bufSize, dataOffs); - break; - } - if ((err = dmC64ConvertCSDataToImage(outImage, outX * outWidthPX, outY * outHeight, - bufData, outWidth, outHeight, optInMulticolor, optColors)) != DMERR_OK) + dataBuf + offs, outWidth, outHeight, optInMulticolor, optColors)) != DMERR_OK) { dmErrorMsg("Internal error in conversion of raw data to bitmap: %d.\n", err); break; @@ -1550,7 +1519,11 @@ if (optSequential) { - outFilename = dm_strdup_printf("%s%04d.%s", optOutFilename, itemCount, convFormatList[optOutFormat].fext); + outFilename = dm_strdup_printf("%s%04d.%s", + optOutFilename, + itemCount, + convFormatList[optOutFormat].fext); + if (outFilename == NULL) { dmErrorMsg("Could not allocate memory for filename template?\n"); @@ -1575,6 +1548,7 @@ } } + offs += outSize; itemCount++; } @@ -1601,11 +1575,9 @@ } } - dmFree(bufData); - return DMERR_OK; + ret = DMERR_OK; error: - dmFree(bufData); return ret; } @@ -1615,8 +1587,8 @@ FILE *inFile = NULL; const DMC64ImageFormat *inC64Fmt = NULL; DMC64Image *inC64Image = NULL; - Uint8 *dataBuf = NULL; - size_t dataSize; + Uint8 *dataBuf = NULL, *dataBufOrig = NULL; + size_t dataSize, dataSizeOrig; int i; // Default colors @@ -1659,6 +1631,7 @@ goto error; } inFile = stdin; + optInFilename = "stdin"; } else if ((inFile = fopen(optInFilename, "rb")) == NULL) @@ -1669,9 +1642,25 @@ goto error; } - if (dmReadDataFile(inFile, NULL, &dataBuf, &dataSize) != 0) + // Read the input .. + dmMsg(1, "Reading input from '%s'.\n", optInFilename); + + if (dmReadDataFile(inFile, NULL, &dataBufOrig, &dataSizeOrig) != 0) goto error; + // Check and compute the input skip + if (optInSkip > dataSizeOrig) + { + dmErrorMsg("Input skip value %d is larger than input size %d.\n", + optInSkip, dataSizeOrig); + goto error; + } + + dataBuf = dataBufOrig + optInSkip; + dataSize = dataSizeOrig - optInSkip; + + + // Perform probing, if required if (optInFormat == FFMT_AUTO || optInFormat == FFMT_BITMAP) { // Probe for format @@ -1720,15 +1709,6 @@ exit(1); } - // Skip, if needed - if (fseek(inFile, optInSkip, SEEK_SET) != 0) - { - int res = dmGetErrno(); - dmErrorMsg("Could not seek to file position %d (0x%x): %s\n", - optInSkip, optInSkip, dmErrorStr(res)); - goto error; - } - int inFormat = dmGetConvFormat(optInFormat, optInSubFormat), outFormat = dmGetConvFormat(optOutFormat, optOutSubFormat); @@ -1750,7 +1730,7 @@ { case FFMT_SPRITE: case FFMT_CHAR: - dmDumpSpritesAndChars(inFile); + dmDumpSpritesAndChars(dataBuf, dataSize, optInSkip); break; case FFMT_BITMAP: @@ -1858,7 +1838,7 @@ if (inFile != NULL) fclose(inFile); - dmFree(dataBuf); + dmFree(dataBufOrig); dmC64ImageFree(inC64Image); return 0;