# HG changeset patch # User Matti Hamalainen # Date 1526195245 -10800 # Node ID 4288b21e97b99d46c48ab41a9e5ba65c994f9369 # Parent fb60abb09a654efbe69558ea2d21be2b0bf6f534 Improve and simplify Fun Paint 2 format support. diff -r fb60abb09a65 -r 4288b21e97b9 tools/lib64fmts.c --- a/tools/lib64fmts.c Sun May 13 09:51:13 2018 +0300 +++ b/tools/lib64fmts.c Sun May 13 10:07:25 2018 +0300 @@ -98,7 +98,7 @@ DMCompParams cfg; cfg.type = DM_COMP_RLE_MARKER1; - cfg.rleMarker = *(buf + 0x0d); + cfg.rleMarker = buf[0x0d]; if ((res = dmDecodeGenericRLEAlloc(&mem, buf + 0x0e, buf + len, &cfg)) != DMERR_OK) goto out; @@ -332,56 +332,42 @@ static const char *fmtFunPaint2MagicID = "FUNPAINT (MT) "; -static BOOL fmtProbeFunPaint2Header(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) +static int fmtProbeFunPaint2(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) { - return - len > 30 && + if (len > 30 && dmCompareAddr16(buf, 0, fmt->addr) && - strncmp((const char *) (buf + 2), fmtFunPaint2MagicID, strlen(fmtFunPaint2MagicID)) == 0; -} - - -static int fmtProbeFunPaint2Unpacked(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) -{ - if (fmtProbeFunPaint2Header(buf, len, fmt) && - buf[2 + 14] == 0) + strncmp((const char *) (buf + 2), fmtFunPaint2MagicID, strlen(fmtFunPaint2MagicID)) == 0) return DM_PROBE_SCORE_MAX; else return DM_PROBE_SCORE_FALSE; } -static int fmtProbeFunPaint2Packed(const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) -{ - if (fmtProbeFunPaint2Header(buf, len, fmt) && - buf[2 + 14] != 0) - return DM_PROBE_SCORE_MAX; - else - return DM_PROBE_SCORE_FALSE; -} - - -static int fmtDecodeFunPaint2Unpacked(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) -{ - return dmC64DecodeGenericBMP(img, buf + FUNPAINT2_HEADER_SIZE, len - FUNPAINT2_HEADER_SIZE, fmt); -} - - -static int fmtDecodeFunPaint2Packed(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) +static int fmtDecodeFunPaint2(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) { int res; - DMGrowBuf mem; - DMCompParams cfg; + + // Check if the data is compressed + if (buf[14]) + { + DMGrowBuf mem; + DMCompParams cfg; + + cfg.type = DM_COMP_RLE_MARKER1; + cfg.rleMarker = buf[15]; - cfg.type = DM_COMP_RLE_MARKER1; - cfg.rleMarker = *(buf + 15); - if ((res = dmDecodeGenericRLEAlloc(&mem, buf + FUNPAINT2_HEADER_SIZE, buf + len, &cfg)) != DMERR_OK) - goto out; + if ((res = dmDecodeGenericRLEAlloc(&mem, buf + FUNPAINT2_HEADER_SIZE, + buf + len + 1, // XXX TODO: nasty +1 + &cfg)) == DMERR_OK) + res = dmC64DecodeGenericBMP(img, mem.data, mem.len, fmt); - res = dmC64DecodeGenericBMP(img, mem.data, mem.len, fmt); + dmGrowBufFree(&mem); + } + else + { + res = dmC64DecodeGenericBMP(img, buf + FUNPAINT2_HEADER_SIZE, len - FUNPAINT2_HEADER_SIZE, fmt); + } -out: - dmGrowBufFree(&mem); return res; } @@ -881,8 +867,8 @@ "fp2", "FunPaint II (unpacked)", 0x3ff0, 33694, DM_FMT_RD, C64_SCR_WIDTH, C64_SCR_HEIGHT, C64_SCR_CH_WIDTH , C64_SCR_CH_HEIGHT, - fmtProbeFunPaint2Unpacked, - fmtDecodeFunPaint2Unpacked, fmtEncodeFunPaint2Unpacked, + fmtProbeFunPaint2, + fmtDecodeFunPaint2, fmtEncodeFunPaint2Unpacked, NULL, NULL, fmtGetPixelFunPaint2, { @@ -902,8 +888,8 @@ "fp2p", "FunPaint II (packed)", 0x3ff0, 0, DM_FMT_RD, C64_SCR_WIDTH, C64_SCR_HEIGHT, C64_SCR_CH_WIDTH , C64_SCR_CH_HEIGHT, - fmtProbeFunPaint2Packed, - fmtDecodeFunPaint2Packed, fmtEncodeFunPaint2Packed, + NULL, + fmtDecodeFunPaint2, fmtEncodeFunPaint2Packed, NULL, NULL, fmtGetPixelFunPaint2, {