# HG changeset patch # User Matti Hamalainen # Date 1503098222 -10800 # Node ID 5bd64397453b5cb6a1d1a3ef3dee16f0adb00992 # Parent 228cab109c6a66c004511b459141b0bd264b3910 More work on PCX writer. diff -r 228cab109c6a -r 5bd64397453b src/libgfx.c --- a/src/libgfx.c Sat Aug 19 01:32:46 2017 +0300 +++ b/src/libgfx.c Sat Aug 19 02:17:02 2017 +0300 @@ -754,6 +754,7 @@ BOOL ret = TRUE; if (pcx->bufOffs > 0) ret = fwrite(pcx->buf, sizeof(Uint8), pcx->bufOffs, pcx->fp) == pcx->bufOffs; + pcx->bufOffs = 0; return ret; } @@ -769,13 +770,30 @@ return dmPCXFlush(pcx); } + +static int dmPCXPutData(DMPCXData *pcx, const Uint8 data, const int count) +{ + if (count == 1 && (data & 0xC0) != 0xC0) + { + if (!dmPCXPutByte(pcx, data)) + return DMERR_FWRITE; + } + else + { + if (!dmPCXPutByte(pcx, 0xC0 | count) || + !dmPCXPutByte(pcx, data)) + return DMERR_FWRITE; + } + return DMERR_OK; +} + + static int dmWritePCXRow(void *cbdata, Uint8 *row, size_t len) { DMPCXData *pcx = (DMPCXData *) cbdata; + int err; size_t soffs = 0; -// fprintf(stderr, "%d, %d * %d = %d\n", len, pcx->header->bpl, pcx->header->nplanes, pcx->header->nplanes * pcx->header->bpl); - pcx->bufOffs = 0; for (int plane = 0; plane < pcx->header->nplanes; plane++) @@ -783,40 +801,26 @@ Uint8 data = dmPCXGetByte(row, len, soffs++), count = 1; -// size_t blen = pcx->header->bpl * pcx->header->nplanes; - size_t blen = pcx->header->bpl; + size_t blen = pcx->header->bpl * pcx->header->nplanes; while (soffs < blen) { - if (data == dmPCXGetByte(row, len, soffs) && count < 63) + if (data == dmPCXGetByte(row, len, soffs) && count < 0x3F) { count++; soffs++; } else { - if (count == 1 && (data & 0xC0) != 0xC0) - { - if (!dmPCXPutByte(pcx, data)) - return DMERR_FWRITE; - } - else - { - if (!dmPCXPutByte(pcx, 0xC0 | count) || - !dmPCXPutByte(pcx, data)) - return DMERR_FWRITE; - } + if ((err = dmPCXPutData(pcx, data, count)) != DMERR_OK) + return err; data = dmPCXGetByte(row, len, soffs++); count = 1; } } - if (count > 1) - { - if (!dmPCXPutByte(pcx, 0xC0 | count) || - !dmPCXPutByte(pcx, data)) - return DMERR_FWRITE; - } + if ((err = dmPCXPutData(pcx, data, count)) != DMERR_OK) + return err; if (!dmPCXFlush(pcx)) return DMERR_FWRITE; @@ -856,8 +860,8 @@ hdr.hres = img->width * spec->scaleX; hdr.vres = img->height * spec->scaleY; hdr.xmin = hdr.ymin = 0; - hdr.xmax = hdr.hres - 1; - hdr.ymax = hdr.vres - 1; + hdr.xmax = (img->width * spec->scaleX) - 1; + hdr.ymax = (img->height * spec->scaleY) - 1; hdr.palInfo = 1; hdr.hScreenSize = hdr.hres; hdr.vScreenSize = hdr.vres; @@ -871,8 +875,14 @@ if (res % 2) hdr.bpl++; hdr.bpl *= 2; - dmMsg(2, "PCX: paletted=%d, nplanes=%d, bpp=%d, bpl=%d\n", - spec->paletted, hdr.nplanes, hdr.bitsPerPlane, hdr.bpl); + dmMsg(2, + "PCX: xmin=%d, ymin=%d, xmax=%d, ymax=%d, res=%dx%d, scr=%dx%d\n", + hdr.xmin, hdr.ymin, hdr.xmax, hdr.ymax, + hdr.hres, hdr.vres, + hdr.hScreenSize, hdr.vScreenSize); + + dmMsg(2, "PCX: nplanes=%d, bpp=%d, bpl=%d, isPaletted=%s\n", + hdr.nplanes, hdr.bitsPerPlane, hdr.bpl, spec->paletted ? "yes" : "no"); // TODO XXX this is also bogus pcx.bufLen = hdr.bpl * 4;