# HG changeset patch # User Matti Hamalainen # Date 1530007285 -10800 # Node ID 8ad98bc7402c1bbc9843f2e11388614b33b95a87 # Parent 74405ff6bc4af8a5bab8bbb098ba4e30c358b6da Cleanups. diff -r 74405ff6bc4a -r 8ad98bc7402c tools/libgfx.c --- a/tools/libgfx.c Tue Jun 26 12:20:08 2018 +0300 +++ b/tools/libgfx.c Tue Jun 26 13:01:25 2018 +0300 @@ -1423,6 +1423,7 @@ { DMIFFChunk chFORM, chBMHD, chCMAP, chBODY; DMIFFBMHD bmhd; + BOOL planar; Uint32 camg; int ncolors; DMColor *pal; @@ -1499,6 +1500,7 @@ "IFF: Failed to skip chunk end.\n"); } } + return DMERR_OK; } @@ -1583,93 +1585,93 @@ } -static int dmDecodeILBMBody(DMResource *fp, DMIFF *iff, DMImage *img) +static int dmDecodeIFFBody(DMResource *fp, DMIFF *iff, DMImage *img) { - Uint8 *buf; - size_t bufLen; + Uint8 *buf = NULL; + size_t bufLen = 0; int res = DMERR_OK; const int nplanes = iff->bmhd.nplanes; - // Allocate planar decoding buffer - bufLen = ((img->width + 15) / 16) * 2; - if ((buf = dmMalloc(bufLen)) == NULL) - return DMERR_MALLOC; + if (iff->planar) + { + // Allocate planar decoding buffer + bufLen = ((img->width + 15) / 16) * 2; + if ((buf = dmMalloc(bufLen)) == NULL) + return DMERR_MALLOC; - dmMsg(2, "IFF: plane row size %d bytes.\n", bufLen); + dmMsg(2, "IFF: plane row size %d bytes.\n", bufLen); + } // Decode the chunk for (int yc = 0; yc < img->height; yc++) { Uint8 *dp = img->data + (yc * img->pitch); - dmMemset(dp, 0, img->pitch); - - for (int plane = 0; plane < nplanes; plane++) + if (iff->planar) { - // Decompress or read data - if (!dmIFFReadOneRow(fp, iff, buf, bufLen)) + dmMemset(dp, 0, img->pitch); + + for (int plane = 0; plane < nplanes; plane++) { - res = dmError(DMERR_FREAD, - "IFF: Error in reading image plane #%d @ %d.\n", - plane, yc); - goto error; + // Decompress or read data + if (!dmIFFReadOneRow(fp, iff, buf, bufLen)) + { + res = dmError(DMERR_FREAD, + "IFF: Error in reading image plane #%d @ %d.\n", + plane, yc); + goto out; + } + + // Decode bitplane + for (int xc = 0; xc < img->width; xc++) + dp[xc] |= dmDecodeBit(buf, xc) << plane; } - // Decode bitplane - for (int xc = 0; xc < img->width; xc++) - dp[xc] |= dmDecodeBit(buf, xc) << plane; - } + // Read mask data + if (iff->bmhd.masking == IFF_MASK_HAS_MASK) + { + // Decompress or read data + if (!dmIFFReadOneRow(fp, iff, buf, bufLen)) + { + res = dmError(DMERR_FREAD, + "IFF: Error in reading mask plane.\n"); + goto out; + } - // Read mask data - if (iff->bmhd.masking == IFF_MASK_HAS_MASK) + // Decode mask + for (int xc = 0; xc < img->width; xc++) + { + const Uint8 data = dmDecodeBit(buf, xc); + + // Black out any pixels with mask bit 0 + if (!data) + dp[xc] = img->ctransp < 0 ? 0 : img->ctransp; + } + } + } + else { - // Decompress or read data - if (!dmIFFReadOneRow(fp, iff, buf, bufLen)) + if (!dmIFFReadOneRow(fp, iff, dp, img->width)) { res = dmError(DMERR_FREAD, - "IFF: Error in reading mask plane.\n"); - goto error; - } - - // Decode mask - for (int xc = 0; xc < img->width; xc++) - { - const Uint8 data = dmDecodeBit(buf, xc); - - // Black out any pixels with mask bit 0 - if (!data) - dp[xc] = img->ctransp < 0 ? 0 : img->ctransp; + "IFF: Error reading PBM image row #%d.\n", yc); + goto out; } } } -error: +out: dmFree(buf); return res; } -static int dmDecodePBMBody(DMResource *fp, DMIFF *iff, DMImage *img) -{ - for (int yc = 0; yc < img->height; yc++) - { - if (!dmIFFReadOneRow(fp, iff, img->data + (yc * img->pitch), img->width)) - { - return dmError(DMERR_FREAD, - "IFF: Error reading PBM image row #%d.\n", yc); - } - } - - return DMERR_OK; -} - - int dmReadIFFImage(DMResource *fp, DMImage **pimg) { DMIFFChunk chunk; DMIFF iff; Uint32 idsig; - BOOL parsed = FALSE, planar; + BOOL parsed = FALSE; int res = DMERR_OK; dmMemset(&iff, 0, sizeof(iff)); @@ -1693,7 +1695,7 @@ "IFF: Not a IFF ILBM/PBM file.\n"); } - planar = (idsig == IFF_ID_ILBM); + iff.planar = (idsig == IFF_ID_ILBM); while (!parsed && !dmfeof(fp)) { @@ -1814,16 +1816,8 @@ (*pimg)->aspect = (float) iff.bmhd.yasp / (float) iff.bmhd.xasp; // Decode the body - if (planar) - { - if ((res = dmDecodeILBMBody(fp, &iff, *pimg)) != DMERR_OK) - return res; - } - else - { - if ((res = dmDecodePBMBody(fp, &iff, *pimg)) != DMERR_OK) - return res; - } + if ((res = dmDecodeIFFBody(fp, &iff, *pimg)) != DMERR_OK) + return res; if ((res = dmSkipIFFChunkRest(fp, &chunk)) != DMERR_OK) return res; @@ -1873,7 +1867,7 @@ if (iff.camg & IFF_CAMG_HALFBRITE) { void *ptmp; - if (!planar) + if (!iff.planar) { dmErrorMsg("IFF: Non-planar PBM file with Halfbrite enabled! This might not work.\n"); }