# HG changeset patch # User Matti Hamalainen # Date 1544009519 -7200 # Node ID 3617ef01c1ded3792e7be3aab6552c2639e320b9 # Parent bd109c0a7b8874b0468aedfcce0470ead6f5ac64 Separate ILBM and PBM subformats of IFF images. diff -r bd109c0a7b88 -r 3617ef01c1de tools/libgfx.c --- a/tools/libgfx.c Wed Dec 05 12:54:28 2018 +0200 +++ b/tools/libgfx.c Wed Dec 05 13:31:59 2018 +0200 @@ -1501,6 +1501,7 @@ int ncolors; DMColor *pal; Uint32 idsig; + char *idstr; } DMIFF; @@ -1802,7 +1803,8 @@ } dmMsg(3, "IFF: FORM is %s format image, with size %d bytes.\n", - iff.idsig == IFF_ID_ILBM ? "ILBM" : (iff.idsig == IFF_ID_PBM ? "PBM" : "ACBM"), + iff.idsig == IFF_ID_ILBM ? "ILBM" : + (iff.idsig == IFF_ID_PBM ? "PBM" : "ACBM"), chunk.size); while (!parsed && !dmfeof(fp)) @@ -2191,25 +2193,29 @@ } -int dmWriteIFFImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *cpspec) +int dmWriteIFFImage(DMResource *fp, const DMImage *img, const DMImageConvSpec *spec) { DMIFF iff; Uint8 *buf = NULL; size_t bufLen; int res = DMERR_OK; - DMImageConvSpec pspec, *spec = &pspec; - - memcpy(&pspec, cpspec, sizeof(DMImageConvSpec)); - - // XXX: Non-paletted ILBM not supported! + //DMImageConvSpec pspec, *spec = &pspec; + + //memcpy(&pspec, cpspec, sizeof(DMImageConvSpec)); + + // XXX: Non-paletted IFF not supported! if (!spec->paletted) { return dmError(DMERR_NOT_SUPPORTED, "Non-paletted IFF is not supported.\n"); } - if (!spec->planar && spec->nplanes < 8) - spec->nplanes = 8; + switch (spec->format) + { + case DM_IMGFMT_IFF_ILBM: iff.idsig = IFF_ID_ILBM; iff.idstr = "ILBM"; break; + case DM_IMGFMT_IFF_PBM : iff.idsig = IFF_ID_PBM; iff.idstr = "PBM"; break; + case DM_IMGFMT_IFF_ACBM: iff.idsig = IFF_ID_ACBM; iff.idstr = "ACBM"; break; + } // Setup headers iff.bmhd.x = 0; @@ -2226,8 +2232,7 @@ iff.bmhd.masking = (img->ctransp < 0) ? IFF_MASK_NONE : spec->mask; iff.bmhd.compression = spec->compression ? IFF_COMP_BYTERUN1 : IFF_COMP_NONE; iff.bmhd.transp = (img->ctransp >= 0 && spec->mask == IFF_MASK_TRANSP) ? img->ctransp : 0xffff; - iff.bmhd.nplanes = spec->nplanes; - iff.idsig = spec->planar ? IFF_ID_ILBM : IFF_ID_PBM; + iff.bmhd.nplanes = (iff.idsig == IFF_ID_PBM && spec->nplanes < 8) ? 8 : spec->nplanes; dmMsg(2, "IFF: nplanes=%d, comp=%d, mask=%d\n", iff.bmhd.nplanes, iff.bmhd.compression, iff.bmhd.masking); @@ -2241,7 +2246,7 @@ { res = dmError(DMERR_FWRITE, "IFF: Error writing %s signature.\n", - spec->planar ? "ILBM" : "PBM"); + iff.idstr); goto out; } @@ -2319,9 +2324,8 @@ if ((res = dmWriteIFFChunkHdr(fp, &iff.chBODY, IFF_ID_BODY)) != DMERR_OK) goto out; - // Allocate encoding buffer - if (spec->planar) + if (iff.idsig == IFF_ID_ILBM) bufLen = (((img->width * spec->scaleX) + 15) / 16) * 2; else bufLen = img->width * spec->scaleX; @@ -2332,59 +2336,61 @@ return DMERR_MALLOC; // Encode the body - for (int yc = 0; yc < img->height; yc++) - for (int yscale = 0; yscale < spec->scaleY; yscale++) { - const Uint8 *sp = img->data + (yc * img->pitch); - - if (spec->planar) + for (int yc = 0; yc < img->height; yc++) + for (int yscale = 0; yscale < spec->scaleY; yscale++) { - for (int plane = 0; plane < spec->nplanes; plane++) + const Uint8 *sp = img->data + (yc * img->pitch); + + if (iff.idsig == IFF_ID_ILBM) { - // Encode bitplane - dmMemset(buf, 0, bufLen); - - for (int xc = 0; xc < img->width * spec->scaleX; xc++) - buf[xc / 8] |= ((sp[xc / spec->scaleX] >> plane) & 1) << (7 - (xc & 7)); - - // Compress / write data - if (!dmIFFWriteOneRow(fp, &iff, buf, bufLen)) + for (int plane = 0; plane < spec->nplanes; plane++) { - res = dmError(DMERR_FWRITE, - "IFF: Error writing image plane #%d @ %d.\n", - plane, yc); - goto out; + // Encode bitplane + dmMemset(buf, 0, bufLen); + + for (int xc = 0; xc < img->width * spec->scaleX; xc++) + buf[xc / 8] |= ((sp[xc / spec->scaleX] >> plane) & 1) << (7 - (xc & 7)); + + // Compress / write data + if (!dmIFFWriteOneRow(fp, &iff, buf, bufLen)) + { + res = dmError(DMERR_FWRITE, + "IFF: Error writing ILBM image plane #%d @ row %d.\n", + plane, yc); + goto out; + } + } + + // Write mask data, if any + if (iff.bmhd.masking == IFF_MASK_HAS_MASK) + { + dmMemset(buf, 0, bufLen); + + for (int xc = 0; xc < img->width * spec->scaleX; xc++) + buf[xc / 8] |= (sp[xc / spec->scaleX] == img->ctransp) << (7 - (xc & 7)); + + if (!dmIFFWriteOneRow(fp, &iff, buf, bufLen)) + { + res = dmError(DMERR_FWRITE, + "IFF: Error writing ILBM mask plane %d.\n", yc); + goto out; + } } } - - // Write mask data, if any - if (iff.bmhd.masking == IFF_MASK_HAS_MASK) + else { - dmMemset(buf, 0, bufLen); - for (int xc = 0; xc < img->width * spec->scaleX; xc++) - buf[xc / 8] |= (sp[xc / spec->scaleX] == img->ctransp) << (7 - (xc & 7)); + buf[xc] = sp[xc / spec->scaleX]; if (!dmIFFWriteOneRow(fp, &iff, buf, bufLen)) { res = dmError(DMERR_FWRITE, - "IFF: Error writing mask plane %d.\n", yc); + "IFF: Error writing PBM image row #%d.\n", yc); goto out; } } } - else - { - for (int xc = 0; xc < img->width * spec->scaleX; xc++) - buf[xc] = sp[xc / spec->scaleX]; - - if (!dmIFFWriteOneRow(fp, &iff, buf, bufLen)) - { - res = dmError(DMERR_FWRITE, - "IFF: Error writing PBM image row #%d.\n", yc); - goto out; - } - } } if ((res = dmWriteIFFChunkFinish(fp, &iff.chBODY)) != DMERR_OK) @@ -2423,8 +2429,14 @@ fmtProbePCX, dmReadPCXImage, dmWritePCXImage, }, { - "iff", "IFF ILBM/PBM/ACBM", - DM_IMGFMT_IFF, DM_FMT_RDWR, + "lbm", "IFF ILBM (interleaved/old DP)", + DM_IMGFMT_IFF_ILBM, DM_FMT_RDWR, + fmtProbeIFF, dmReadIFFImage, dmWriteIFFImage, + }, + { + "pbm", "IFF PBM (DP2e)", + DM_IMGFMT_IFF_PBM, DM_FMT_RDWR, + fmtProbeIFF, dmReadIFFImage, dmWriteIFFImage, fmtProbeIFF, dmReadIFFImage, dmWriteIFFImage, }, { diff -r bd109c0a7b88 -r 3617ef01c1de tools/libgfx.h --- a/tools/libgfx.h Wed Dec 05 12:54:28 2018 +0200 +++ b/tools/libgfx.h Wed Dec 05 13:31:59 2018 +0200 @@ -22,7 +22,8 @@ DM_IMGFMT_PNG, DM_IMGFMT_PPM, DM_IMGFMT_PCX, - DM_IMGFMT_IFF, + DM_IMGFMT_IFF_ILBM, + DM_IMGFMT_IFF_PBM, DM_IMGFMT_RAW, DM_IMGFMT_ARAW, DM_IMGFMT_CDUMP,