# HG changeset patch # User Matti Hamalainen # Date 1551791358 -7200 # Node ID 81fb21dd326547209831d6d3548245fea925ee0c # Parent dac89484f143c5295aa4e32f9c9cd3c023e93536 Add dmGetNPlanesFromNColors() and use it. diff -r dac89484f143 -r 81fb21dd3265 tools/gfxconv.c --- a/tools/gfxconv.c Tue Mar 05 14:51:02 2019 +0200 +++ b/tools/gfxconv.c Tue Mar 05 15:09:18 2019 +0200 @@ -1424,21 +1424,13 @@ // Determine number of planes, if paletted if (spec->format == DM_COLFMT_PALETTE && - spec->nplanes == 0) - { - for (int n = 8; n >= 0;) - { - if ((image->pal->ncolors - 1) & (1 << n)) - { - spec->nplanes = n + 1; - break; - } - else - n--; - } - } + spec->nplanes == 0 && image->pal != NULL) + spec->nplanes = dmGetNPlanesFromNColors(image->pal->ncolors); + else + if (image->format == DM_COLFMT_GRAYSCALE) + spec->nplanes = 8; - if (spec->nplanes == 0) + if (spec->nplanes <= 0) spec->nplanes = 4; spec->fmtid = fmt->fmtid; diff -r dac89484f143 -r 81fb21dd3265 tools/libgfx.c --- a/tools/libgfx.c Tue Mar 05 14:51:02 2019 +0200 +++ b/tools/libgfx.c Tue Mar 05 15:09:18 2019 +0200 @@ -91,6 +91,23 @@ } +int dmGetNPlanesFromNColors(const int ncolors) +{ + if (ncolors <= 0) + return -1; + + for (int n = 8; n >= 0;) + { + if ((ncolors - 1) & (1 << n)) + return n + 1; + else + n--; + } + + return -2; +} + + BOOL dmCompareColor(const DMColor *c1, const DMColor *c2, const BOOL alpha) { if (c1->r == c2->r && diff -r dac89484f143 -r 81fb21dd3265 tools/libgfx.h --- a/tools/libgfx.h Tue Mar 05 14:51:02 2019 +0200 +++ b/tools/libgfx.h Tue Mar 05 15:09:18 2019 +0200 @@ -132,6 +132,7 @@ int dmImageGetBytesPerPixel(const int format); int dmImageGetBitsPerPixel(const int format); int dmImageProbeGeneric(const Uint8 *buf, const size_t len, const DMImageFormat **fmt, int *index); +int dmGetNPlanesFromNColors(const int ncolors); BOOL dmCompareColor(const DMColor *c1, const DMColor *c2, const BOOL alpha); int dmPaletteAlloc(DMPalette **ppal, const int ncolors, const int ctransp);