# HG changeset patch # User Matti Hamalainen # Date 1353330361 -7200 # Node ID 43ea59887c69e75a4b3d4a87bfc4abc7cb49fbb8 # Parent e8e244036ee424b546b60e61cad5f779c2765a19 Start work on making C64 formats encoding possible by changing DMDecodeOps to DMEncDecOps and adding fields and op enums for custom encode functions, renaming, etc. Split generic op sanity checking into a separate function in preparation for its use in generic encoding function. diff -r e8e244036ee4 -r 43ea59887c69 lib64gfx.c --- a/lib64gfx.c Mon Nov 19 14:04:56 2012 +0200 +++ b/lib64gfx.c Mon Nov 19 15:06:01 2012 +0200 @@ -197,7 +197,7 @@ } -static BOOL fmtDrazLaceSetLaceType(DMC64Image *img, const struct _DMDecodeOp *op, const Uint8 *buf, const size_t len) +static BOOL fmtDrazLaceSetLaceType(DMC64Image *img, const struct _DMC64EncDecOp *op, const Uint8 *buf, const size_t len) { (void) len; @@ -282,7 +282,7 @@ } -static BOOL fmtTruePaintSetLaceType(DMC64Image *img, const struct _DMDecodeOp *op, const Uint8 *buf, const size_t len) +static BOOL fmtTruePaintSetLaceType(DMC64Image *img, const struct _DMC64EncDecOp *op, const Uint8 *buf, const size_t len) { (void) op; (void) buf; @@ -301,10 +301,10 @@ NULL, NULL, NULL, 4, { - { DT_COLOR_RAM, 0x0000, 0, 0, NULL }, - { DT_BITMAP, 0x0800, 0, 0, NULL }, - { DT_SCREEN_RAM, 0x0400, 0, 0, NULL }, - { DT_BGCOLOR, 0x2740, 0, 0, NULL }, + { DT_COLOR_RAM, 0x0000, 0, 0, NULL, NULL }, + { DT_BITMAP, 0x0800, 0, 0, NULL, NULL }, + { DT_SCREEN_RAM, 0x0400, 0, 0, NULL, NULL }, + { DT_BGCOLOR, 0x2740, 0, 0, NULL, NULL }, } }, @@ -314,12 +314,12 @@ NULL, NULL, NULL, 6, { - { DT_COLOR_RAM, 0x0000, 0, 0, NULL }, - { DT_BITMAP, 0x0800, 0, 0, NULL }, - { DT_SCREEN_RAM, 0x0400, 0, 0, NULL }, - { DT_BGCOLOR, 0x2740, 0, 0, NULL }, - { DT_BITMAP, 0x2800, 1, 0, NULL }, - { DT_FUNCTION, 0x2742, 0, 1, fmtDrazLaceSetLaceType }, + { DT_COLOR_RAM, 0x0000, 0, 0, NULL, NULL }, + { DT_BITMAP, 0x0800, 0, 0, NULL, NULL }, + { DT_SCREEN_RAM, 0x0400, 0, 0, NULL, NULL }, + { DT_BGCOLOR, 0x2740, 0, 0, NULL, NULL }, + { DT_BITMAP, 0x2800, 1, 0, NULL, NULL }, + { DT_DEC_FUNCTION, 0x2742, 0, 1, fmtDrazLaceSetLaceType, NULL }, } }, @@ -329,10 +329,10 @@ NULL, NULL, NULL, 4, { - { DT_COLOR_RAM, 0x0000, 0, 0, NULL }, - { DT_BITMAP, 0x0800, 0, 0, NULL }, - { DT_SCREEN_RAM, 0x0400, 0, 0, NULL }, - { DT_BGCOLOR, 0x2740, 0, 0, NULL }, + { DT_COLOR_RAM, 0x0000, 0, 0, NULL, NULL }, + { DT_BITMAP, 0x0800, 0, 0, NULL, NULL }, + { DT_SCREEN_RAM, 0x0400, 0, 0, NULL, NULL }, + { DT_BGCOLOR, 0x2740, 0, 0, NULL, NULL }, } }, @@ -342,12 +342,12 @@ NULL, NULL, NULL, 6, { - { DT_COLOR_RAM, 0x0000, 0, 0, NULL }, - { DT_BITMAP, 0x0800, 0, 0, NULL }, - { DT_SCREEN_RAM, 0x0400, 0, 0, NULL }, - { DT_BGCOLOR, 0x2740, 0, 0, NULL }, - { DT_BITMAP, 0x2800, 1, 0, NULL }, - { DT_FUNCTION, 0x2742, 0, 1, fmtDrazLaceSetLaceType }, + { DT_COLOR_RAM, 0x0000, 0, 0, NULL, NULL }, + { DT_BITMAP, 0x0800, 0, 0, NULL, NULL }, + { DT_SCREEN_RAM, 0x0400, 0, 0, NULL, NULL }, + { DT_BGCOLOR, 0x2740, 0, 0, NULL, NULL }, + { DT_BITMAP, 0x2800, 1, 0, NULL, NULL }, + { DT_DEC_FUNCTION, 0x2742, 0, 1, fmtDrazLaceSetLaceType, NULL }, } }, @@ -357,13 +357,13 @@ NULL, NULL, NULL, 6, { - { DT_SCREEN_RAM, 0x0000, 0, 0, NULL }, - { DT_BGCOLOR, 0x03e8, 0, 0, NULL }, - { DT_BITMAP, 0x0400, 0, 0, NULL }, - { DT_BITMAP, 0x2400, 1, 0, NULL }, - { DT_SCREEN_RAM, 0x4400, 1, 0, NULL }, - { DT_COLOR_RAM, 0x4800, 0, 0, NULL }, - { DT_FUNCTION, 0x0000, 0, 0, fmtTruePaintSetLaceType }, + { DT_SCREEN_RAM, 0x0000, 0, 0, NULL, NULL }, + { DT_BGCOLOR, 0x03e8, 0, 0, NULL, NULL }, + { DT_BITMAP, 0x0400, 0, 0, NULL, NULL }, + { DT_BITMAP, 0x2400, 1, 0, NULL, NULL }, + { DT_SCREEN_RAM, 0x4400, 1, 0, NULL, NULL }, + { DT_COLOR_RAM, 0x4800, 0, 0, NULL, NULL }, + { DT_DEC_FUNCTION, 0x0000, 0, 0, fmtTruePaintSetLaceType, NULL }, } }, @@ -373,10 +373,10 @@ NULL, NULL, NULL, 4, { - { DT_COLOR_RAM, 0x2328, 0, 0, NULL }, - { DT_BITMAP, 0x0000, 0, 0, NULL }, - { DT_SCREEN_RAM, 0x1f40, 0, 0, NULL }, - { DT_BGCOLOR, 0x2710, 0, 0, NULL }, + { DT_COLOR_RAM, 0x2328, 0, 0, NULL, NULL }, + { DT_BITMAP, 0x0000, 0, 0, NULL, NULL }, + { DT_SCREEN_RAM, 0x1f40, 0, 0, NULL, NULL }, + { DT_BGCOLOR, 0x2710, 0, 0, NULL, NULL }, } }, @@ -386,10 +386,10 @@ NULL, NULL, NULL, 4, { - { DT_COLOR_RAM, 0x2328, 0, 0, NULL }, - { DT_BITMAP, 0x0000, 0, 0, NULL }, - { DT_SCREEN_RAM, 0x1f40, 0, 0, NULL }, - { DT_BGCOLOR, 0x2710, 0, 0, NULL }, + { DT_COLOR_RAM, 0x2328, 0, 0, NULL, NULL }, + { DT_BITMAP, 0x0000, 0, 0, NULL, NULL }, + { DT_SCREEN_RAM, 0x1f40, 0, 0, NULL, NULL }, + { DT_BGCOLOR, 0x2710, 0, 0, NULL, NULL }, } }, @@ -423,6 +423,26 @@ } +static int dmC64SanityCheckEncDecOp(const int i, const DMC64EncDecOp *op) +{ + if (op->bank < 0 || op->bank >= C64_SCR_MAX_BANK) + { + dmError("Invalid bank %d definition in generic encode/decode operator %d @ #%d.\n", + op->bank, op->type, i); + return DMERR_INTERNAL; + } + + if (op->type < 0 || op->type >= DT_LAST) + { + dmError("Invalid encode/decode operator type %d @ #%d.\n", + op->type, i); + return DMERR_INTERNAL; + } + + return DMERR_OK; +} + + int dmC64DecodeGenericBMP(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt) { @@ -431,31 +451,22 @@ memset(img, 0, sizeof(*img)); img->type = fmt->type; - for (i = 0; i < fmt->ndecodeOps; i++) + for (i = 0; i < fmt->ndecencOps; i++) { - const DMDecodeOp *op = &fmt->decodeOps[i]; + const DMC64EncDecOp *op = &fmt->decencOps[i]; const Uint8 *src; size_t size; + int res; - if (op->bank < 0 || op->bank >= C64_SCR_MAX_BANK) - { - dmError("Invalid bank %d definition in generic decode operator %d @ #%d.\n", - op->bank, op->type, i); - return DMERR_INTERNAL; - } - - if (op->type < 0 || op->type >= DT_LAST) - { - dmError("Invalid decode operator type %d @ #%d.\n", - op->type, i); - return DMERR_INTERNAL; - } + if ((res = dmC64SanityCheckEncDecOp(i, op)) != DMERR_OK) + return res; size = (op->size == 0) ? dmC64DefaultSizes[op->type] : op->size; if (op->offs + size > len) { - dmError("Decode out of bounds, op #%d type=%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x\n", + dmError("Decode out of bounds, op #%d type=%d, offs=%d ($%04x), " + "bank=%d, size=%d ($%04x) @ %d ($%04x)\n", i, op->type, op->offs, op->offs, op->bank, size, size, len, len); return DMERR_INVALID_DATA; } @@ -469,16 +480,18 @@ case DT_SCREEN_RAM: memcpy(img->screen[op->bank], src, size); break; case DT_BGCOLOR: img->bgcolor = *src; break; case DT_EXTRADATA: memcpy(img->extradata, src, size); break; - case DT_FUNCTION: - if (op->function == NULL) + case DT_DEC_FUNCTION: + if (op->decfunction == NULL) { - dmError("Decode op is a function, but function ptr is NULL: op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x\n", + dmError("Decode op is a function, but function ptr is NULL: " + "op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", i, op->offs, op->offs, op->bank, size, size, len, len); return DMERR_INTERNAL; } - if (!op->function(img, op, buf, len)) + if (!op->decfunction(img, op, buf, len)) { - dmError("Decode op custom function failed: op #%d, offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x\n", + dmError("Decode op custom function failed: op #%d, " + "offs=%d ($%04x), bank=%d, size=%d ($%04x) @ %d ($%04x)\n", i, op->offs, op->offs, op->bank, size, size, len, len); return DMERR_INTERNAL; } diff -r e8e244036ee4 -r 43ea59887c69 lib64gfx.h --- a/lib64gfx.h Mon Nov 19 14:04:56 2012 +0200 +++ b/lib64gfx.h Mon Nov 19 15:06:01 2012 +0200 @@ -105,20 +105,22 @@ DT_BGCOLOR, DT_EXTRADATA, - DT_FUNCTION, + DT_DEC_FUNCTION, + DT_ENC_FUNCTION, DT_LAST, }; -typedef struct _DMDecodeOp +typedef struct _DMC64EncDecOp { int type; size_t offs; int bank; size_t size; - BOOL (*function)(DMC64Image *img, const struct _DMDecodeOp *op, const Uint8 *buf, const size_t len); -} DMDecodeOp; + BOOL (*decfunction)(DMC64Image *img, const struct _DMC64EncDecOp *op, const Uint8 *buf, const size_t len); + BOOL (*encfunction)(const struct _DMC64EncDecOp *op, Uint8 **buf, size_t *len, const DMC64Image *img); +} DMC64EncDecOp; typedef struct _DMC64ImageFormat @@ -132,8 +134,8 @@ int (*convertFrom)(DMImage *, DMC64Image *); int (*convertTo)(DMImage *, DMC64Image *); - int ndecodeOps; - DMDecodeOp decodeOps[16]; + int ndecencOps; + DMC64EncDecOp decencOps[16]; } DMC64ImageFormat;