# HG changeset patch # User Matti Hamalainen # Date 1353531840 -7200 # Node ID d0c0c6baeb57023c8007ee24d482078d7b91c31b # Parent 2a70f5902b70c95ba37368ca8494b9effb7f3b90 Split the RLE decoding from DrazPaint/Lace and Amica paint decoders to a generic function. diff -r 2a70f5902b70 -r d0c0c6baeb57 lib64gfx.c --- a/lib64gfx.c Wed Nov 21 21:20:42 2012 +0200 +++ b/lib64gfx.c Wed Nov 21 23:04:00 2012 +0200 @@ -142,21 +142,15 @@ } -static int fmtDecodeDrazPaintPacked(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) +static int dmDecodeGenericRLE(Uint8 **mem, Uint8 **pdstEnd, const Uint8 *src, const Uint8 *srcEnd, const Uint8 rleMarker) { - int res; - Uint8 rleMarker; - Uint8 *mem, *dst, *dstEnd; - const Uint8 *src, *srcEnd; + Uint8 *dst, *dstEnd; - if ((mem = dmMalloc(C64_RAM_SIZE)) == NULL) - return -1; + if ((*mem = dmMalloc(C64_RAM_SIZE)) == NULL) + return DMERR_MALLOC; - rleMarker = *(buf + 0x0d); - src = buf + 0x0e; - srcEnd = buf + len; - dst = mem; - dstEnd = mem + C64_RAM_SIZE; + dst = *mem; + dstEnd = *mem + C64_RAM_SIZE; while (src <= srcEnd && dst <= dstEnd) { @@ -171,9 +165,24 @@ else *dst++ = c; } - - res = dmC64DecodeGenericBMP(img, mem, dst - mem + 1, fmt); + + *pdstEnd = dst; + + return DMERR_OK; +} + +static int fmtDecodeDrazPaintPacked(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) +{ + int res; + Uint8 *mem = NULL, *dstEnd; + + if ((res = dmDecodeGenericRLE(&mem, &dstEnd, buf + 0x0e, buf + len, *(buf + 0x0d))) != DMERR_OK) + goto out; + + res = dmC64DecodeGenericBMP(img, mem, dstEnd - mem + 1, fmt); + +out: dmFree(mem); return res; } @@ -227,35 +236,15 @@ static int fmtDecodeAmicaPaintPacked(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) { int res; - Uint8 *mem, *dst, *dstEnd; - const Uint8 *src, *srcEnd; + Uint8 *mem = NULL, *dstEnd; - if ((mem = dmMalloc(C64_RAM_SIZE)) == NULL) - return -1; - - src = buf; - srcEnd = buf + len; - dst = mem; - dstEnd = mem + C64_RAM_SIZE; + if ((res = dmDecodeGenericRLE(&mem, &dstEnd, buf, buf + len, 0xC2)) != DMERR_OK) + goto out; - while (src <= srcEnd && dst <= dstEnd) - { - int c = *src++; - if (c == 0xC2 && src + 2 <= srcEnd) - { - int cnt = *src++; - c = *src++; - while (cnt-- && dst <= dstEnd) - *dst++ = c; - } - else - *dst++ = c; - } + res = dmC64DecodeGenericBMP(img, mem, dstEnd - mem + 1, fmt); - res = dmC64DecodeGenericBMP(img, mem, dst - mem + 1, fmt); - +out: dmFree(mem); - return res; }