From 30222c13d66545b6e3a278059117be104cf341cd Mon Sep 17 00:00:00 2001 From: Su_Laus Date: Sat, 29 Feb 2020 17:59:59 +0100 Subject: [PATCH] tif_dirwrite.c: bugfix DoubleToSrational(), which returns plain signed interger values always as unsigned rationals. Add a test into rational_precision2double.c for "-1.0" and some editorials in tif_dirwrite.c. (code is related to 6df997c786928757caea0dd68d26ea5f098f49df changes). --- libtiff/tif_dirwrite.c | 18 +++++++++--------- test/rational_precision2double.c | 5 +++-- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/libtiff/tif_dirwrite.c b/libtiff/tif_dirwrite.c index 410c8adb..4c622804 100644 --- a/libtiff/tif_dirwrite.c +++ b/libtiff/tif_dirwrite.c @@ -162,8 +162,8 @@ static int TIFFWriteDirectoryTagRationalDoubleArray(TIFF* tif, uint32* ndir, TIF static int TIFFWriteDirectoryTagSrationalDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value); static int TIFFWriteDirectoryTagCheckedRationalDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value); static int TIFFWriteDirectoryTagCheckedSrationalDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value); -static void DoubleToRational(double f, uint32 *num, uint32 *denom); -static void DoubleToSrational(double f, int32 *num, int32 *denom); +static void DoubleToRational(double value, uint32 *num, uint32 *denom); +static void DoubleToSrational(double value, int32 *num, int32 *denom); #if 0 static void DoubleToRational_direct(double value, unsigned long *num, unsigned long *denom); static void DoubleToSrational_direct(double value, long *num, long *denom); @@ -2893,7 +2893,7 @@ void DoubleToSrational(double value, int32 *num, int32 *denom) double dblDiff, dblDiff2; unsigned long long ullNum, ullDenom, ullNum2, ullDenom2; - /*-- Check for negative values and use then the positive one for internal calculations. */ + /*-- Check for negative values and use then the positive one for internal calculations, but take the sign into account before returning. */ if (value < 0) { neg = -1; value = -value; } /*-- Check for too big numbers (> LONG_MAX) -- */ @@ -2903,8 +2903,8 @@ void DoubleToSrational(double value, int32 *num, int32 *denom) return; } /*-- Check for easy numbers -- */ - if (value == (uint32)(value)) { - *num = (uint32)value; + if (value == (int32)(value)) { + *num = (int32)(neg * value); *denom = 1; return; } @@ -2936,12 +2936,12 @@ void DoubleToSrational(double value, int32 *num, int32 *denom) dblDiff = fabs(value - ((double)ullNum / (double)ullDenom)); dblDiff2 = fabs(value - ((double)ullNum2 / (double)ullDenom2)); if (dblDiff < dblDiff2) { - *num = (uint32)(neg * (long)ullNum); - *denom = (uint32)ullDenom; + *num = (int32)(neg * (long)ullNum); + *denom = (int32)ullDenom; } else { - *num = (uint32)(neg * (long)ullNum2); - *denom = (uint32)ullDenom2; + *num = (int32)(neg * (long)ullNum2); + *denom = (int32)ullDenom2; } } /*-- DoubleToSrational() --------------*/ diff --git a/test/rational_precision2double.c b/test/rational_precision2double.c index 782fb9fa..e33cc97d 100644 --- a/test/rational_precision2double.c +++ b/test/rational_precision2double.c @@ -498,7 +498,8 @@ write_test_tiff(TIFF* tif, const char* filenameRead, int blnAllCustomTags) { fprintf(stderr, "Can't set TIFFTAG_RATIONAL_DOUBLE tag.\n"); goto failure; } - if (!TIFFSetField(tif, TIFFTAG_SRATIONAL_DOUBLE, (-1.0 * auxDoubleArrayW[101]))) { + /* test for plain integers */ + if (!TIFFSetField(tif, TIFFTAG_SRATIONAL_DOUBLE, (-1.0 ))) { fprintf(stderr, "Can't set TIFFTAG_SRATIONAL_DOUBLE tag.\n"); goto failure; } @@ -764,7 +765,7 @@ write_test_tiff(TIFF* tif, const char* filenameRead, int blnAllCustomTags) { auxDblUnion.dbl = 0; retCode = TIFFGetField(tif, TIFFTAG_SRATIONAL_DOUBLE, &auxDblUnion.dbl); if (!retCode) { fprintf(stderr, "Can't read %s\n", "TIFFTAG_SRATIONAL_DOUBLE"); GOTOFAILURE } - auxDouble = -1.0 * auxDoubleArrayW[101]; + auxDouble = -1.0; dblDiffLimit = RATIONAL_EPS * auxDouble; dblDiff = auxDblUnion.dbl - auxDouble; if (fabs(dblDiff) > fabs(dblDiffLimit)) {