Commit 17270d743 for imagemagick.org
commit 17270d7435b501920fa2848261e1923473b3bbe7
Author: Chi-Wei Lee <levi74108520963@gmail.com>
Date: Fri Oct 2 18:57:07 2026 +0800
Scale neighborhood contrast to the quantum range (#8996)
diff --git a/MagickCore/statistic.c b/MagickCore/statistic.c
index 08da699c3..df2bc6420 100644
--- a/MagickCore/statistic.c
+++ b/MagickCore/statistic.c
@@ -3077,8 +3077,8 @@ MagickExport Image *StatisticImage(const Image *image,const StatisticType type,
{
case ContrastStatistic:
{
- pixel=ClampToQuantum(MagickAbsoluteValue((maximum-minimum)*
- MagickSafeReciprocal(maximum+minimum)));
+ pixel=ClampToQuantum(QuantumRange*MagickAbsoluteValue((maximum-
+ minimum)*MagickSafeReciprocal(maximum+minimum)));
break;
}
case GradientStatistic:
diff --git a/Makefile.in b/Makefile.in
index 24380b5dc..7eacb9a7b 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -6276,6 +6276,7 @@ TESTS_TESTS = \
tests/cli-pcx.tap \
tests/cli-pipe.tap \
tests/cli-svg.tap \
+ tests/cli-statistic.tap \
tests/cli-uhdr.tap \
tests/cli-uhdr-encoder.tap \
tests/cli-uhdr-resize-filter.tap \
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 5eb351726..a2f8cb8fc 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -45,6 +45,7 @@ TESTS_TESTS = \
tests/cli-pcx.tap \
tests/cli-pipe.tap \
tests/cli-svg.tap \
+ tests/cli-statistic.tap \
tests/cli-uhdr.tap \
tests/cli-uhdr-encoder.tap \
tests/cli-uhdr-resize-filter.tap \
diff --git a/tests/cli-statistic.tap b/tests/cli-statistic.tap
new file mode 100755
index 000000000..574b2f784
--- /dev/null
+++ b/tests/cli-statistic.tap
@@ -0,0 +1,43 @@
+#!/bin/sh
+#
+# Copyright 1999 ImageMagick Studio LLC, a non-profit organization
+# dedicated to making software imaging solutions freely available.
+#
+# You may not use this file except in compliance with the License. You may
+# obtain a copy of the License at
+#
+# https://imagemagick.org/license/
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Regression tests for neighborhood statistics.
+#
+. ./common.shi
+. ${srcdir}/tests/common.shi
+echo "1..4"
+
+# A black-and-white neighborhood has full contrast.
+result=`${MAGICK} -size 3x1 xc:black -fill white -draw 'point 1,0' \
+ -alpha off -statistic Contrast 3x1 -format '%[fx:abs(mean-1)<0.005]' info:-`
+[ "X$result" = "X1" ] && echo "ok" || echo "not ok"
+
+# Contrast is calculated and scaled separately for each color channel.
+result=`${MAGICK} -size 3x1 'xc:rgb(0,85,255)' -fill white \
+ -draw 'point 1,0' -alpha off -statistic Contrast 3x1 \
+ -format '%[fx:abs(r-1)<0.005],%[fx:abs(g-0.5)<0.005],%[fx:abs(b)<0.005]' info:-`
+[ "X$result" = "X1,1,1" ] && echo "ok" || echo "not ok"
+
+# Uniform nonzero neighborhoods have zero contrast.
+result=`${MAGICK} -size 3x1 'xc:rgb(85,85,85)' -alpha off \
+ -statistic Contrast 3x1 -format '%[fx:mean==0]' info:-`
+[ "X$result" = "X1" ] && echo "ok" || echo "not ok"
+
+# An all-black neighborhood also has zero contrast.
+result=`${MAGICK} -size 3x1 xc:black -alpha off -statistic Contrast 3x1 \
+ -format '%[fx:mean==0]' info:-`
+[ "X$result" = "X1" ] && echo "ok" || echo "not ok"
+: