diff options
author | Marti Maria <marti.maria@littlecms.com> | 2021-04-17 21:48:47 +0300 |
---|---|---|
committer | Marti Maria <marti.maria@littlecms.com> | 2021-04-17 21:48:47 +0300 |
commit | 31f6546c45a874450903c8635fc2d71893502056 (patch) | |
tree | 2450f349b37c4197ac09e09a73b73927c61cb87b /plugins | |
parent | e71a11d3a6bdc1052158598978b4d6c60819bdce (diff) |
add some checks for NAN and denormalized
User buffers may contain invalid float numbers
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/fast_float/src/fast_float_cmyk.c | 2 | ||||
-rw-r--r-- | plugins/fast_float/src/fast_float_internal.h | 20 | ||||
-rw-r--r-- | plugins/fast_float/src/fast_float_lab.c | 19 | ||||
-rw-r--r-- | plugins/fast_float/testbed/fast_float_testbed.c | 6 |
4 files changed, 35 insertions, 12 deletions
diff --git a/plugins/fast_float/src/fast_float_cmyk.c b/plugins/fast_float/src/fast_float_cmyk.c index 7de83a5..d4a8261 100644 --- a/plugins/fast_float/src/fast_float_cmyk.c +++ b/plugins/fast_float/src/fast_float_cmyk.c @@ -64,7 +64,7 @@ cmsINLINE cmsFloat32Number LinearInterpInt(cmsFloat32Number a, cmsFloat32Number // To prevent out of bounds indexing cmsINLINE cmsFloat32Number fclamp100(cmsFloat32Number v) { - return v < 0.0f ? 0.0f : (v > 100.0f ? 100.0f : v); + return ((v < 1.0e-9f) || isnan(v)) ? 0.0f : (v > 100.0f ? 100.0f : v); } diff --git a/plugins/fast_float/src/fast_float_internal.h b/plugins/fast_float/src/fast_float_internal.h index 43de02d..f3bcddd 100644 --- a/plugins/fast_float/src/fast_float_internal.h +++ b/plugins/fast_float/src/fast_float_internal.h @@ -41,6 +41,21 @@ # define cmsINLINE static inline #endif +/// Properly define some macros to accommodate +/// older MSVC versions. +# if defined(_MSC_VER) && _MSC_VER <= 1700 +#include <float.h> +#define isnan _isnan +#define isinf(x) (!_finite((x))) +# endif + +#if !defined(_MSC_VER) && (defined(__STDC_VERSION__) && __STDC_VERSION__ < 199901L) +#if !defined(isinf) +#define isinf(x) (!finite((x))) +#endif +#endif + + // A fast way to convert from/to 16 <-> 8 bits #define FROM_8_TO_16(rgb) (cmsUInt16Number) ((((cmsUInt16Number) (rgb)) << 8)|(rgb)) #define FROM_16_TO_8(rgb) (cmsUInt8Number) ((((rgb) * 65281 + 8388608) >> 24) & 0xFF) @@ -72,11 +87,10 @@ typedef struct { #define MAX_NODES_IN_CURVE 0x8001 - // To prevent out of bounds indexing cmsINLINE cmsFloat32Number fclamp(cmsFloat32Number v) { - return v < 0.0f ? 0.0f : (v > 1.0f ? 1.0f : v); + return ((v < 1.0e-9f) || isnan(v)) ? 0.0f : (v > 1.0f ? 1.0f : v); } // Fast floor conversion logic. @@ -120,7 +134,7 @@ cmsINLINE cmsFloat32Number flerp(const cmsFloat32Number LutTable[], cmsFloat32Nu cmsFloat32Number rest; int cell0, cell1; - if (v <= 0.0) { + if ((v < 1.0e-9f) || isnan(v)) { return LutTable[0]; } else diff --git a/plugins/fast_float/src/fast_float_lab.c b/plugins/fast_float/src/fast_float_lab.c index 109e622..553d5fe 100644 --- a/plugins/fast_float/src/fast_float_lab.c +++ b/plugins/fast_float/src/fast_float_lab.c @@ -144,6 +144,18 @@ int XFormSampler(CMSREGISTER const cmsFloat32Number In[], CMSREGISTER cmsFloat32 return TRUE; } + +// To prevent out of bounds indexing +cmsINLINE cmsFloat32Number fclamp128(cmsFloat32Number v) +{ + return ((v < -128) || isnan(v)) ? -128.0f : (v > 128.0f ? 128.0f : v); +} + +cmsINLINE cmsFloat32Number fclamp100(cmsFloat32Number v) +{ + return ((v < 1.0e-9f) || isnan(v)) ? 0.0f : (v > 100.0f ? 100.0f : v); +} + // A optimized interpolation for Lab. #define DENS(i,j,k) (LutTable[(i)+(j)+(k)+OutChan]) @@ -215,9 +227,10 @@ void LabCLUTEval(struct _cmstransform_struct* CMMcargo, for (ii = 0; ii < PixelsPerLine; ii++) { // Decode Lab and go across sigmoids on a*/b* - l = fclamp((*(cmsFloat32Number*)lin) / 100.0f); - a = LinLerp1D(((*(cmsFloat32Number*)ain) + 128.0f) / 255.0f, pfloat->sigmoidIn); - b = LinLerp1D(((*(cmsFloat32Number*)bin) + 128.0f) / 255.0f, pfloat->sigmoidIn); + l = fclamp100( *(cmsFloat32Number*)lin ) / 100.0f; + + a = LinLerp1D((( fclamp128( *(cmsFloat32Number*)ain)) + 128.0f) / 255.0f, pfloat->sigmoidIn); + b = LinLerp1D((( fclamp128( *(cmsFloat32Number*)bin)) + 128.0f) / 255.0f, pfloat->sigmoidIn); lin += SourceIncrements[0]; ain += SourceIncrements[1]; diff --git a/plugins/fast_float/testbed/fast_float_testbed.c b/plugins/fast_float/testbed/fast_float_testbed.c index 1cacbb9..a03a79f 100644 --- a/plugins/fast_float/testbed/fast_float_testbed.c +++ b/plugins/fast_float/testbed/fast_float_testbed.c @@ -988,16 +988,12 @@ void CheckConversionFloat(void) TryAllValuesFloatAlpha(cmsOpenProfileFromFile("test0.icc", "r"), cmsOpenProfileFromFile("test0.icc", "r"), INTENT_PERCEPTUAL, TRUE); printf("Ok\n"); - - /* - * FIXME!! - * printf("Crash (III) test."); CheckUncommonValues(cmsOpenProfileFromFile("test5.icc", "r"), cmsOpenProfileFromFile("test3.icc", "r"), INTENT_PERCEPTUAL); printf(".."); CheckUncommonValues(cmsOpenProfileFromFile("test5.icc", "r"), cmsOpenProfileFromFile("test0.icc", "r"), INTENT_PERCEPTUAL); printf("Ok\n"); - */ + // Matrix-shaper should be accurate printf("Checking accuracy on Matrix-shaper..."); |