Mercurial > hg > dmlib
diff lib64gfx.c @ 556:44d1e0d4acf3
Improve DMC64Image -> DMImage conversion facilities.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 23 Nov 2012 18:37:09 +0200 |
parents | 3c968e027a58 |
children | cf39f51d4e74 |
line wrap: on
line diff
--- a/lib64gfx.c Fri Nov 23 17:33:55 2012 +0200 +++ b/lib64gfx.c Fri Nov 23 18:37:09 2012 +0200 @@ -700,12 +700,22 @@ // Convert a generic "C64" format bitmap in DMC64Image struct to // a indexed/paletted bitmap image. -int dmC64ConvertGenericBMP2Image(DMImage *dst, const DMC64Image *src) +int dmC64ConvertGenericBMP2Image(DMImage *dst, const DMC64Image *src, const BOOL doubleMC) { - int yc; Uint8 *dp = dst->data; + int divisor = doubleMC ? 4 : 8, + wdivisor = doubleMC ? 2 : 1, + yc; - for (yc = 0; yc < C64_SCR_HEIGHT; yc++) + // Sanity check arguments + if (dst == NULL || src == NULL) + return DMERR_NULLPTR; + + if (dst->width < 8) + return DMERR_INVALID_ARGS; + + // Perform generic conversion + for (yc = 0; yc < dst->height; yc++) { Uint8 *d = dp; const int y = yc / 8, yb = yc & 7; @@ -715,14 +725,14 @@ if ((src->type & D64_FMT_MC) == D64_FMT_HIRES) { - for (xc = 0; xc < C64_SCR_WIDTH; xc++) + for (xc = 0; xc < dst->width; xc++) { const int x = xc / 8; const int scroffs = scroffsy + x; - const int b = src->bitmap[0][bmoffsy + (x * 8) + yb]; + const int bmoffs = bmoffsy + (x * 8) + yb; const int v = 7 - (xc & 7); - if ((b >> v) & 1) + if ((src->bitmap[0][bmoffs] >> v) & 1) *d++ = src->screen[0][scroffs] >> 4; else *d++ = src->screen[0][scroffs] & 15; @@ -730,9 +740,9 @@ } else { - for (xc = 0; xc < C64_SCR_WIDTH / 2; xc++) + for (xc = 0; xc < dst->width / wdivisor; xc++) { - const int x = xc / 4; + const int x = xc / divisor; const int scroffs = scroffsy + x; const int bmoffs = bmoffsy + (x * 8) + yb; const int v = 6 - ((xc * 2) & 6); @@ -771,7 +781,6 @@ } } } - dp += dst->pitch; } @@ -779,6 +788,38 @@ } +int dmC64ConvertBMP2Image(DMImage **pdst, const DMC64Image *src, const DMC64ImageFormat *fmt, const BOOL doubleMC) +{ + int width, res; + DMImage *dst; + + if (pdst == NULL || src == NULL) + return DMERR_NULLPTR; + + // Calculate output image width + if ((src->type & D64_FMT_MC) && !doubleMC) + width = C64_SCR_WIDTH / 2; + else + width = C64_SCR_WIDTH; + + // Allocate image structure + if ((*pdst = dst = dmImageAlloc(width, C64_SCR_HEIGHT)) == NULL) + return DMERR_MALLOC; + + // Set palette + dst->pal = (DMColor *) &dmC64Palette; + dst->ncolors = C64_NCOLORS; + dst->constpal = TRUE; + + // Convert + if (fmt->convertFrom != NULL) + res = fmt->convertFrom(dst, src, doubleMC); + else + res = dmC64ConvertGenericBMP2Image(dst, src, doubleMC); + + return res; +} + int dmReadDataFile(FILE *inFile, const char *filename, Uint8 **pbuf, size_t *pbufSize) {