# HG changeset patch # User Matti Hamalainen # Date 1526011660 -10800 # Node ID 32640e1934d57089ac24cc6295ac9feb20a75972 # Parent 9217fcc727e85b1c84ad7d9df0a4cd6230b2a2e9 Simplify some encoding bits. diff -r 9217fcc727e8 -r 32640e1934d5 tools/lib64gfx.c --- a/tools/lib64gfx.c Fri May 11 07:06:36 2018 +0300 +++ b/tools/lib64gfx.c Fri May 11 07:07:40 2018 +0300 @@ -522,16 +522,12 @@ const char *magicID = (fmt->type & D64_FMT_ILACE) ? "DRAZLACE! 1.0" : "DRAZPAINT 2.0"; // Encode the data to temp buffer - if ((res = dmC64EncodeGenericBMP(&tmp, img, fmt)) != DMERR_OK) + if ((res = dmC64EncodeGenericBMP(TRUE, &tmp, img, fmt)) != DMERR_OK) goto out; // Analyze the data .. rleMarker = 0xff; - // Allocate the output buffer - if ((res = dmGrowBufAlloc(buf, tmp.len + BUF_SIZE_GROW, BUF_SIZE_GROW)) != DMERR_OK) - goto out; - // Add the header bits if (!dmGrowBufPut(buf, magicID, strlen(magicID)) || !dmGrowBufPutU8(buf, rleMarker)) @@ -540,7 +536,7 @@ goto out; } - // And now RLE compress it to the existing buffer + // And now RLE compress the data to the existing buffer res = dmEncodeGenericRLE(buf, tmp.data, tmp.data + tmp.len, rleMarker, 3, 255, DM_RLE_MARKER); @@ -753,11 +749,6 @@ } -{ - -} - - static Uint8 fmtGetPixelFunPaint2( const DMC64Image *img, const int bmoffs, const int scroffs, const int vshift, const int vbitmap, const int raster) @@ -1530,15 +1521,15 @@ } -int dmC64EncodeGenericBMP(DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt) +int dmC64EncodeGenericBMP(const BOOL allocate, DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt) { int res = DMERR_OK; if (img == NULL || fmt == NULL) return DMERR_NULLPTR; - // Allocate the output buffer - if ((res = dmGrowBufAlloc(buf, fmt->size, BUF_SIZE_GROW)) != DMERR_OK) + // Allocate the output buffer if requested + if (allocate && (res = dmGrowBufAlloc(buf, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK) { dmError(res, "Could not allocate %d bytes of memory for C64 image encoding buffer.\n", @@ -1897,31 +1888,24 @@ if (img == NULL || fmt == NULL) return DMERR_NULLPTR; + // Allocate a buffer + if ((res = dmGrowBufAlloc(buf, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK) + goto err; + + // Add the loading address + if (!dmGrowBufPutU8(buf, DM_GET_ADDR_LO(fmt->addr)) || + !dmGrowBufPutU8(buf, DM_GET_ADDR_HI(fmt->addr))) + goto err; + // Attempt to encode the image to a buffer - if ((res = dmGrowBufInit(buf)) != DMERR_OK) - return res; - if (fmt->encode != NULL) res = fmt->encode(buf, img, fmt); else - res = dmC64EncodeGenericBMP(buf, img, fmt); + res = dmC64EncodeGenericBMP(FALSE, buf, img, fmt); if (res != DMERR_OK) goto err; - // Add the loading address - if (!dmGrowBufGrow(buf, 2)) - { - res = DMERR_MALLOC; - goto err; - } - - memmove(buf->data + 2, buf->data, buf->len); - - buf->data[0] = DM_GET_ADDR_LO(fmt->addr); - buf->data[1] = DM_GET_ADDR_HI(fmt->addr); - buf->len += 2; - return DMERR_OK; err: diff -r 9217fcc727e8 -r 32640e1934d5 tools/lib64gfx.h --- a/tools/lib64gfx.h Fri May 11 07:06:36 2018 +0300 +++ b/tools/lib64gfx.h Fri May 11 07:07:40 2018 +0300 @@ -209,7 +209,7 @@ int dmC64ProbeBMP(const Uint8 *buf, const size_t len, const DMC64ImageFormat **fmt); int dmC64DecodeGenericBMP(DMC64Image *img, const Uint8 *buf, const size_t len, const DMC64ImageFormat *fmt); -int dmC64EncodeGenericBMP(DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt); +int dmC64EncodeGenericBMP(const BOOL allocate, DMGrowBuf *buf, const DMC64Image *img, const DMC64ImageFormat *fmt); int dmC64ConvertBMP2Image(DMImage **pdst, const DMC64Image *src, const DMC64ImageFormat *fmt); int dmC64ConvertImage2BMP(DMC64Image **pdst, const DMImage *src, const DMC64ImageFormat *fmt);