# HG changeset patch # User Matti Hamalainen # Date 1528287735 -10800 # Node ID de8e0a404c0663a86c529f661d8b5f55b4e246be # Parent c9a6f1dae756b1db5539bf94cdee5691873a8095 Refactor fmtDecodeTruePaintPacked() to use more generic DMGrowBuf functions now that they support backwards growing buffers, and get rid of dmReverseGetByte() and dmReversePutByte() helpers. diff -r c9a6f1dae756 -r de8e0a404c06 tools/lib64fmts.c --- a/tools/lib64fmts.c Wed Jun 06 15:20:31 2018 +0300 +++ b/tools/lib64fmts.c Wed Jun 06 15:22:15 2018 +0300 @@ -423,56 +423,54 @@ // some kind of "improved RLE" variant with different modes and a // simplistic "codebook". // -static BOOL fmtTruePaintGetByte(const Uint8 *src, size_t *srcOffs, Uint8 *data, int *res, const int mode) +static int fmtTruePaintGetByte(DMGrowBuf *src, Uint8 *data, const int mode) { - if (!dmReverseGetByte(src, srcOffs, data)) + if (!dmGrowBufGetU8(src, data)) { - *res = dmError(DMERR_INVALID_DATA, + return dmError(DMERR_INVALID_DATA, "TruePaintRLE: Out of input data (N=%d)\n", mode); - return FALSE; } else - return TRUE; + return DMERR_OK; } -static int fmtDecodeTruePaintPacked(DMC64Image *img, const DMGrowBuf *src, const DMC64ImageFormat *fmt) +static int fmtDecodeTruePaintPacked(DMC64Image *img, const DMGrowBuf *psrc, const DMC64ImageFormat *fmt) { int res = DMERR_OK; - Uint8 *dst = NULL; - DMGrowBuf dstTmp; const Uint8 *codeBook1, *codeBook2; - size_t - srcOffs, dstOffs, - dstLen = 0x4be8; - // 1b7e-67e8 decoded by original depacker - // 1c00-67e8 is the actual area used tho + DMGrowBuf dst, src; + DMCompParams cfg; + Uint8 data; + + cfg.func = fmt->name; + cfg.type = DM_COMP_RLE_MARKER; + cfg.flags = DM_RLE_BACKWARDS_OUTPUT | DM_RLE_BACKWARDS_INPUT; + cfg.rleMarkerB = 0xfe; + + // 1b7e-67e8 decoded by original depacker + // 1c00-67e8 is the actual area used tho + cfg.flags |= DM_OUT_CROP_END; + cfg.cropOutLen = 0x67e8 - 0x1c00; // Codebooks: #1 is trampoline table markers, #2 is RLE data table - codeBook1 = src->data + 0x81 - 2; - codeBook2 = src->data + 0x85 - 2; + codeBook1 = psrc->data + 0x81 - 2; + codeBook2 = psrc->data + 0x85 - 2; // Allocate output buffer - if ((dst = dmMalloc0(dstLen)) == NULL) - { - res = dmError(DMERR_MALLOC, - "Could not allocate memory for temporary decompression buffer.\n"); + if ((res = dmGrowBufAlloc(&dst, 64*1024, 4*1024)) != DMERR_OK) goto out; - } - // Begin decompression - srcOffs = src->len; - dstOffs = dstLen; + // As we need to modify the offs, etc. but not the data, + // we will just make a shallow copy of the DMGrowBuf struct + dmGrowBufConstCopy(&src, psrc); + dmSetupRLEBuffers(&dst, &src, &cfg); - while (srcOffs > 0 && dstOffs > 0) + while ((res = fmtTruePaintGetByte(&src, &data, -1)) == DMERR_OK) { - Uint8 data; - int count = 1, scount; + unsigned int count = 1; BOOL found = FALSE; - if (!fmtTruePaintGetByte(src->data, &srcOffs, &data, &res, -1)) - goto out; - for (int n = 0; n < 8; n++) if (codeBook1[n] == data && !found) { @@ -480,7 +478,7 @@ switch (n) { case 4: // Y = 4, JTO = $0B - if (!fmtTruePaintGetByte(src->data, &srcOffs, &data, &res, n)) + if ((res = fmtTruePaintGetByte(&src, &data, n)) != DMERR_OK) goto out; count = data; @@ -494,12 +492,12 @@ // fallthrough case 0: // Y = 0, JTO = $19 - if (!fmtTruePaintGetByte(src->data, &srcOffs, &data, &res, n)) + if ((res = fmtTruePaintGetByte(&src, &data, n)) != DMERR_OK) goto out; break; case 2: // Y = 2, JTO = $07 - if (!fmtTruePaintGetByte(src->data, &srcOffs, &data, &res, n)) + if ((res = fmtTruePaintGetByte(&src, &data, n)) != DMERR_OK) goto out; count = data; @@ -517,23 +515,16 @@ } } - for (scount = count; count; count--) - { - if (!dmReversePutByte(dst, &dstOffs, data)) - { - res = dmError(DMERR_INVALID_DATA, - "TruePaintRLE: Out of output space for run: %d x $%02x!\n", - scount, data); - goto out; - } - } + if ((res = dmGenericRLEOutputRun(&dst, &cfg, data, count)) != DMERR_OK) + goto out; } finish: - res = dmC64DecodeGenericBMP(img, dmGrowBufCreateFrom(&dstTmp, dst, dstLen), fmt); + dmFinishRLEBuffers(&dst, &src, &cfg); + res = dmC64DecodeGenericBMP(img, &dst, fmt); out: - dmFree(dst); + dmGrowBufFree(&dst); return res; } diff -r c9a6f1dae756 -r de8e0a404c06 tools/lib64gfx.c --- a/tools/lib64gfx.c Wed Jun 06 15:20:31 2018 +0300 +++ b/tools/lib64gfx.c Wed Jun 06 15:22:15 2018 +0300 @@ -100,30 +100,6 @@ } -BOOL dmReverseGetByte(const Uint8 *buf, size_t *offs, Uint8 *data) -{ - if (*offs > 0) - { - *data = buf[--(*offs)]; - return TRUE; - } - else - return FALSE; -} - - -BOOL dmReversePutByte(Uint8 *buf, size_t *offs, const Uint8 data) -{ - if (*offs > 0) - { - buf[--(*offs)] = data; - return TRUE; - } - else - return FALSE; -} - - int dmC64MemBlockAlloc(DMC64MemBlock *blk, const size_t size) { if ((blk->data = dmMalloc0(size)) == NULL) diff -r c9a6f1dae756 -r de8e0a404c06 tools/lib64gfx.h --- a/tools/lib64gfx.h Wed Jun 06 15:20:31 2018 +0300 +++ b/tools/lib64gfx.h Wed Jun 06 15:22:15 2018 +0300 @@ -283,8 +283,6 @@ void dmSetDefaultC64Palette(DMImage *img); BOOL dmCompareAddr16(const Uint8 *buf, const size_t offs, const Uint16 addr); int dmC64ImageGetNumBanks(const DMC64ImageFormat *fmt); -BOOL dmReverseGetByte(const Uint8 *buf, size_t *offs, Uint8 *data); -BOOL dmReversePutByte(Uint8 *buf, size_t *offs, const Uint8 data); // C64 bitmap image allocation/freeing int dmC64MemBlockAlloc(DMC64MemBlock *blk, const size_t size);