convert nan & inf to null for JSON format

This commit is contained in:
Cristy
2025-05-01 19:31:07 -04:00
parent 95333f3624
commit bbb5192c0c
+23 -4
View File
@@ -511,10 +511,14 @@ static ssize_t PrintChannelFeatures(FILE *file,const PixelChannel channel,
" \"average\": %.*g\n" \
" }\n"
char
*buffer;
ssize_t
n;
n=FormatLocaleFile(file,FeaturesFormat,name,
buffer=AcquireString((char *) NULL);
n=FormatLocaleString(buffer,MagickPathExtent,FeaturesFormat,name,
PrintFeature(channel_features[channel].angular_second_moment),
PrintFeature(channel_features[channel].contrast),
PrintFeature(channel_features[channel].correlation),
@@ -529,6 +533,11 @@ static ssize_t PrintChannelFeatures(FILE *file,const PixelChannel channel,
PrintFeature(channel_features[channel].measure_of_correlation_1),
PrintFeature(channel_features[channel].measure_of_correlation_2),
PrintFeature(channel_features[channel].maximum_correlation_coefficient));
(void) SubstituteString(&buffer,": -inf",": null");
(void) SubstituteString(&buffer,": inf",": null");
(void) SubstituteString(&buffer,": nan",": null");
n=FormatLocaleFile(file,"%s",buffer);
buffer=DestroyString(buffer);
(void) FormatLocaleFile(file," }");
if (separator != MagickFalse)
(void) FormatLocaleFile(file,",");
@@ -714,12 +723,17 @@ static ssize_t PrintChannelStatistics(FILE *file,const PixelChannel channel,
"\"standardDeviation\": %.*g,\n \"kurtosis\": %.*g,\n "\
"\"skewness\": %.*g,\n \"entropy\": %.*g\n }"
char
*buffer;
ssize_t
n;
n=FormatLocaleFile(file,StatisticsFormat,name,GetMagickPrecision(),
channel_statistics[channel].minima == MagickMaximumValue ? 0.0 :
(double) ClampToQuantum(scale*channel_statistics[channel].minima),
buffer=AcquireString((char *) NULL);
n=FormatLocaleString(buffer,MagickPathExtent,StatisticsFormat,name,
GetMagickPrecision(),
channel_statistics[channel].minima == MagickMaximumValue ? 0.0 : (double)
ClampToQuantum(scale*channel_statistics[channel].minima),
GetMagickPrecision(),
channel_statistics[channel].maxima == -MagickMaximumValue ? 0.0 :
(double) ClampToQuantum(scale*channel_statistics[channel].maxima),
@@ -731,6 +745,11 @@ static ssize_t PrintChannelStatistics(FILE *file,const PixelChannel channel,
channel_statistics[channel].kurtosis,GetMagickPrecision(),
channel_statistics[channel].skewness,GetMagickPrecision(),
channel_statistics[channel].entropy);
(void) SubstituteString(&buffer,": -inf",": null");
(void) SubstituteString(&buffer,": inf",": null");
(void) SubstituteString(&buffer,": nan",": null");
n=FormatLocaleFile(file,"%s",buffer);
buffer=DestroyString(buffer);
if (separator != MagickFalse)
(void) FormatLocaleFile(file,",");
(void) FormatLocaleFile(file,"\n");