From f3316d36985ad16f1d82aea44eddc311cb739d6d Mon Sep 17 00:00:00 2001 From: giraffedata Date: Sat, 22 Nov 2014 18:41:45 +0000 Subject: Fix %g for platform without vasprintf (but scores of %f still exist) git-svn-id: http://svn.code.sf.net/p/netpbm/code/trunk@2320 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- analyzer/pnmhistmap.c | 4 ++-- converter/other/giftopnm.c | 35 ++++++++++++++++++++++++----------- converter/other/pngtopam.c | 14 ++++++++++---- converter/other/pnmtops.c | 16 ++++++++++++++-- converter/pgm/fstopgm.c | 34 +++++++++++++++++++++++++--------- converter/ppm/ilbmtoppm.c | 46 ++++++++++++++++++++++++++++++---------------- doc/HISTORY | 3 +++ lib/libpm.c | 13 +++++++++++++ lib/pm.h | 5 +++++ lib/util/matrix.c | 12 ++++++++---- lib/util/nstring.h | 3 +++ lib/util/vasprintf.c | 13 +++++++++++++ 12 files changed, 150 insertions(+), 48 deletions(-) diff --git a/analyzer/pnmhistmap.c b/analyzer/pnmhistmap.c index f41b0d51..fc1bbbde 100644 --- a/analyzer/pnmhistmap.c +++ b/analyzer/pnmhistmap.c @@ -426,7 +426,7 @@ ppmHist(FILE * const ifP, clipHistogramAll(hist, histWidth, hmax); vscale = (double) histHeight / hmax; - if (verbose) + if (verbose && pm_have_float_format()) pm_message("Done: height = %u, vertical scale factor = %g", hmax, vscale); @@ -477,7 +477,7 @@ reportScale(unsigned int const histWidth, double const hscale = (float)(histWidth-1) / (range-1); - if (hscale - 1.0 < epsilon && verbose) + if (hscale - 1.0 < epsilon && verbose && pm_have_float_format()) pm_message("Horizontal scale factor: %g", hscale); } diff --git a/converter/other/giftopnm.c b/converter/other/giftopnm.c index 17100849..9f4bc8a1 100644 --- a/converter/other/giftopnm.c +++ b/converter/other/giftopnm.c @@ -1673,6 +1673,28 @@ readImageData(FILE * const ifP, +static void +warnUserNotSquare(unsigned int const aspectRatio) { + + const char * baseMsg = + "warning - input pixels are not square, " + "but we are rendering them as square pixels " + "in the output"; + + if (pm_have_float_format()) { + float const r = ((float)aspectRatio + 15.0 ) / 64.0; + + pm_message("%s. To fix the output, run it through " + "'pamscale -%cscale %g'", + baseMsg, + r < 1.0 ? 'x' : 'y', + r < 1.0 ? 1.0 / r : r ); + } else + pm_message("%s", baseMsg); +} + + + static void readGifHeader(FILE * const gifFileP, struct gifScreen * const gifScreenP) { @@ -1734,17 +1756,8 @@ readGifHeader(FILE * const gifFileP, } } - if (gifScreenP->AspectRatio != 0 && gifScreenP->AspectRatio != 49) { - float r; - r = ( (float) gifScreenP->AspectRatio + 15.0 ) / 64.0; - pm_message("warning - input pixels are not square, " - "but we are rendering them as square pixels " - "in the output. " - "To fix the output, run it through " - "'pamscale -%cscale %g'", - r < 1.0 ? 'x' : 'y', - r < 1.0 ? 1.0 / r : r ); - } + if (gifScreenP->AspectRatio != 0 && gifScreenP->AspectRatio != 49) + warnUserNotSquare(gifScreenP->AspectRatio); } diff --git a/converter/other/pngtopam.c b/converter/other/pngtopam.c index e6e68587..59b29f5f 100644 --- a/converter/other/pngtopam.c +++ b/converter/other/pngtopam.c @@ -1243,10 +1243,16 @@ warnNonsquarePixels(struct pngx * const pngxP, (float)pngx_xPixelsPerMeter(pngxP) / pngx_yPixelsPerMeter(pngxP); if (r != 1.0) { - pm_message ("warning - non-square pixels; " - "to fix do a 'pamscale -%cscale %g'", - r < 1.0 ? 'x' : 'y', - r < 1.0 ? 1.0 / r : r ); + const char * const baseMsg = "warning - non-square pixels"; + + if (pm_have_float_format()) + pm_message("%s; to fix do a 'pamscale -%cscale %g'", + baseMsg, + r < 1.0 ? 'x' : 'y', + r < 1.0 ? 1.0 / r : r); + else + pm_message("%s", baseMsg); + *errorLevelP = PNMTOPNG_WARNING_LEVEL; } } diff --git a/converter/other/pnmtops.c b/converter/other/pnmtops.c index 316b7626..cf6b2873 100644 --- a/converter/other/pnmtops.c +++ b/converter/other/pnmtops.c @@ -1124,6 +1124,19 @@ validateComputableBoundingBox(float const scols, +static void +warnUserRescaling(float const scale) { + + const char * const baseMsg = "warning, image too large for page"; + + if (pm_have_float_format()) + pm_message("%s; rescaling to %g", baseMsg, scale); + else + pm_message("%s; rescaling", baseMsg); +} + + + static void computeImagePosition(int const dpiX, int const dpiY, @@ -1231,8 +1244,7 @@ computeImagePosition(int const dpiX, *srowsP = scale * rows * pixfacY; if (scale != requestedScale) - pm_message("warning, image too large for page, rescaling to %g", - scale ); + warnUserRescaling(scale); /* Before May 2001, Pnmtops enforced a 5% margin around the page. If the image would be too big to leave a 5% margin, Pnmtops would diff --git a/converter/pgm/fstopgm.c b/converter/pgm/fstopgm.c index 8a9ed721..1f574604 100644 --- a/converter/pgm/fstopgm.c +++ b/converter/pgm/fstopgm.c @@ -42,6 +42,29 @@ gethexit(FILE * const ifP) { +static void +warnNonsquarePixels(unsigned int const cols, + unsigned int const xcols, + unsigned int const rows, + unsigned int const xrows) { + + const char * const baseMsg = "warning, non-square pixels"; + + if (pm_have_float_format()) { + float const rowratio = (float) xrows / (float) rows; + float const colratio = (float) xcols / (float) cols; + + pm_message("%s; to fix do a 'pamscale -%cscale %g'", + baseMsg, + rowratio > colratio ? 'y' : 'x', + rowratio > colratio ? + rowratio / colratio : colratio / rowratio); + } else + pm_message("%s", baseMsg); +} + + + int main(int argc, const char ** argv) { @@ -110,15 +133,8 @@ main(int argc, const char ** argv) { if (maxval > PGM_OVERALLMAXVAL) pm_error("depth %d is too large. Our maximum is %d", maxval, PGM_OVERALLMAXVAL); - if (xcols != 0 && xrows != 0 && (xcols != cols || xrows != rows)) { - float const rowratio = (float) xrows / (float) rows; - float const colratio = (float) xcols / (float) cols; - - pm_message( - "warning, non-square pixels; to fix do a 'pamscale -%cscale %g'", - rowratio > colratio ? 'y' : 'x', - rowratio > colratio ? rowratio / colratio : colratio / rowratio ); - } + if (xcols != 0 && xrows != 0 && (xcols != cols || xrows != rows)) + warnNonsquarePixels(cols, xcols, rows, xrows); /* Read the hex bits. */ grays = pgm_allocarray(cols, rows); diff --git a/converter/ppm/ilbmtoppm.c b/converter/ppm/ilbmtoppm.c index 3def1797..662be0b5 100644 --- a/converter/ppm/ilbmtoppm.c +++ b/converter/ppm/ilbmtoppm.c @@ -433,6 +433,27 @@ read_clut(FILE * const ifP, +static void +warnNonsquarePixels(uint8_t const xAspect, + uint8_t const yAspect) { + + if (xAspect != yAspect) { + const char * const baseMsg = "warning - non-square pixels"; + + if (pm_have_float_format()) + pm_message("%s; to fix do a 'pamscale -%cscale %g'", + baseMsg, + xAspect > yAspect ? 'x' : 'y', + xAspect > yAspect ? + (float)xAspect/yAspect : + (float)yAspect/xAspect); + else + pm_message("%s", baseMsg); + } +} + + + static BitMapHeader * read_bmhd(FILE * const ifP, IFF_ID const iffid, @@ -440,7 +461,7 @@ read_bmhd(FILE * const ifP, BitMapHeader * bmhdP; - if( chunksize != BitMapHeaderSize ) { + if (chunksize != BitMapHeaderSize) { pm_message("invalid size for %s chunk - skipping it", ID2string(iffid)); skip_chunk(ifP, iffid, chunksize); @@ -467,24 +488,24 @@ read_bmhd(FILE * const ifP, bmhdP->pageWidth = get_big_short(ifP, iffid, &remainingChunksize); bmhdP->pageHeight = get_big_short(ifP, iffid, &remainingChunksize); - if( verbose ) { - if( typeid == ID_ILBM ) + if (verbose) { + if (typeid == ID_ILBM) pm_message("dimensions: %dx%d, %d planes", bmhdP->w, bmhdP->h, bmhdP->nPlanes); else pm_message("dimensions: %dx%d", bmhdP->w, bmhdP->h); - if( typeid == ID_ILBM || typeid == ID_PBM ) { + if (typeid == ID_ILBM || typeid == ID_PBM) { pm_message("compression: %s", bmhdP->compression <= cmpMAXKNOWN ? cmpNAME[bmhdP->compression] : "unknown"); - switch( bmhdP->masking ) { + switch(bmhdP->masking) { case mskNone: break; case mskHasMask: case mskHasTransparentColor: - if( !maskfile ) + if (!maskfile) pm_message("use '-maskfile ' " "to generate a PBM mask file from %s", mskNAME[bmhdP->masking]); @@ -498,27 +519,20 @@ read_bmhd(FILE * const ifP, } } else /* RGBN/RGB8 */ - if( !maskfile ) + if (!maskfile) pm_message("use '-maskfile ' " "to generate a PBM mask file " "from genlock bits"); } /* fix aspect ratio */ - if( bmhdP->xAspect == 0 || bmhdP->yAspect == 0 ) { + if (bmhdP->xAspect == 0 || bmhdP->yAspect == 0) { pm_message("warning - illegal aspect ratio %d:%d, using 1:1", bmhdP->xAspect, bmhdP->yAspect); bmhdP->xAspect = bmhdP->yAspect = 1; } - if( bmhdP->xAspect != bmhdP->yAspect ) { - pm_message("warning - non-square pixels; " - "to fix do a 'pamscale -%cscale %g'", - bmhdP->xAspect > bmhdP->yAspect ? 'x' : 'y', - bmhdP->xAspect > bmhdP->yAspect ? - (float)(bmhdP->xAspect)/bmhdP->yAspect : - (float)(bmhdP->yAspect)/bmhdP->xAspect); - } + warnNonsquarePixels(bmhdP->xAspect, bmhdP->yAspect); } return bmhdP; } diff --git a/doc/HISTORY b/doc/HISTORY index 661d211f..b0445362 100644 --- a/doc/HISTORY +++ b/doc/HISTORY @@ -6,6 +6,9 @@ CHANGE HISTORY not yet BJH Release 10.69.00 + Fix %g in messages where platform doesn't have vasprintf. + (But scores of %f are still left). + anytopnm: convert all images in a multi-image GIF instead of just the first. diff --git a/lib/libpm.c b/lib/libpm.c index 228f42c6..fa8ae4db 100644 --- a/lib/libpm.c +++ b/lib/libpm.c @@ -278,6 +278,19 @@ pm_error(const char format[], ...) { +bool +pm_have_float_format(void) { +/*---------------------------------------------------------------------------- + Return true iff %f, %e, and %g work in format strings for pm_message, etc. + + Where they don't "work," that means the specifier just appears itself in + the formatted strings, e.g. "the number is g". +-----------------------------------------------------------------------------*/ + return pm_vasprintf_knows_float(); +} + + + static void * mallocz(size_t const size) { diff --git a/lib/pm.h b/lib/pm.h index 72ecc919..a24ea7bb 100644 --- a/lib/pm.h +++ b/lib/pm.h @@ -23,6 +23,8 @@ #include #include +#include "pm_c_util.h" + #ifdef __cplusplus extern "C" { #endif @@ -219,6 +221,9 @@ pm_errormsg(const char format[], ...); void PM_GNU_PRINTF_ATTR(1,2) pm_error (const char reason[], ...); +bool +pm_have_float_format(void); + /* Obsolete - use shhopt and user's manual instead */ void pm_usage (const char usage[]); diff --git a/lib/util/matrix.c b/lib/util/matrix.c index 5101f2c3..e9456e93 100644 --- a/lib/util/matrix.c +++ b/lib/util/matrix.c @@ -106,10 +106,14 @@ findLargestIthCoeff(unsigned int const n, maxSoFar = thisA; } } - if (maxSoFar < epsilon) - pm_asprintf(errorP, "Matrix equation has no unique solution. " - "(debug: coeff %u %e < %e)", i, maxSoFar, epsilon); - else { + if (maxSoFar < epsilon) { + const char * const baseMsg = "Matrix equation has no unique solution"; + if (pm_have_float_format()) + pm_asprintf(errorP, "%s. (debug: coeff %u %e < %e)", + baseMsg, i, maxSoFar, epsilon); + else + pm_asprintf(errorP, "%s", baseMsg); + } else { *istarP = maxIdx; *errorP = NULL; } diff --git a/lib/util/nstring.h b/lib/util/nstring.h index 28adda94..7238a76e 100644 --- a/lib/util/nstring.h +++ b/lib/util/nstring.h @@ -175,6 +175,9 @@ pm_vasprintf(const char ** const resultP, const char * const format, va_list args); +bool +pm_vasprintf_knows_float(void); + void pm_strfree(const char * const string); diff --git a/lib/util/vasprintf.c b/lib/util/vasprintf.c index e38252fa..b294763a 100644 --- a/lib/util/vasprintf.c +++ b/lib/util/vasprintf.c @@ -6,6 +6,8 @@ #include #include "pm_config.h" +#include "pm_c_util.h" + #include "nstring.h" @@ -56,3 +58,14 @@ pm_vasprintf(const char ** const resultP, } #endif } + + + +bool +pm_vasprintf_knows_float(void) { +#if HAVE_VASPRINTF + return true; +#else + return false; +#endif +} -- cgit 1.4.1