Skip to content

Commit a7f6d8c

Browse files
zzambersjudovana
authored andcommitted
Make comparsion of images more reliable
1 parent 891ae86 commit a7f6d8c

1 file changed

Lines changed: 18 additions & 22 deletions

File tree

tests/testlib.bash

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ function beforeBg() {
215215
if [ "x$TTL" = "x" -o "x$TTL" = "x0" ] ; then
216216
echo "manual mode!"
217217
else
218-
import -window root "$REPORT_DIR"/diff-01.png
218+
import -window root "png24:$REPORT_DIR"/diff-01.png
219219
fi
220220
}
221221

@@ -239,7 +239,7 @@ function resolveBg() {
239239
wait "$pid"
240240
else
241241
sleep "$TTL"
242-
import -window root "$REPORT_DIR"/$cname-02.png
242+
import -window root "png24:$REPORT_DIR"/$cname-02.png
243243
ps
244244
#pstree
245245
cpids="$( getDescendants "$pid" )"|| true # gets pids of descendant processes
@@ -249,20 +249,20 @@ function resolveBg() {
249249
for cpid in $cpids ; do kill -9 "$cpid" ; done
250250
fi
251251
sleep 1
252-
import -window root "$REPORT_DIR"/$cname-03.png
252+
import -window root "png24:$REPORT_DIR"/$cname-03.png
253253
if [ "$DIFF" == "true" ] ; then
254254
d12=$(compareImagesSilently $cname 01 02)
255255
d23=$(compareImagesSilently $cname 02 03)
256256
d13=$(compareImagesSilently $cname 01 03 03-01)
257-
if [ "$d12" -gt 15 ] ; then
258-
echo "error! images 1+2 are same, should be not"
257+
if [ "$d12" -lt 1 ] ; then
258+
echo "error! images 1+2 are same (or too similar), should be not"
259259
exit 1
260260
fi
261-
if [ "$d23" -gt 15 ] ; then
262-
echo "error! images 2+3 are same, should be not"
261+
if [ "$d23" -lt 1 ] ; then
262+
echo "error! images 2+3 are same (or too similar), should be not"
263263
exit 1
264264
fi
265-
if [ "$d13" -le 15 ] ; then
265+
if [ "$d13" -gt 0 ] ; then
266266
echo "warning! images 1+3 are different, should be not"
267267
# exit 1 # The difference between first and last stage is probably not relevant, and may cause false-negatives
268268
fi
@@ -286,20 +286,16 @@ function compareImagesSilently() {
286286
if [ "x$idCompOverride" == "x" ] ; then
287287
idCompOverride=$id1-$id2
288288
fi
289-
compare -metric PSNR "$REPORT_DIR"/"$name"-"$id1".png "$REPORT_DIR"/"$name"-"$id2".png "$REPORT_DIR"/"$name"-"$idCompOverride".png 2> "$REPORT_DIR"/res-"$idCompOverride" || r=$?
290-
echo "40+ same, 10- different" 1>&2
291-
# shellcheck disable=SC2155
292-
# shellcheck disable=SC2002
293-
local diff=$(cat "$REPORT_DIR"/res-"$idCompOverride" | sed "s;[. ].*;;")
294-
# behavior of psnr has changed, zero is now reported instead of "inf":
295-
# https://www.imagemagick.org/discourse-server/viewtopic.php?t=31487
296-
# distinguish "inf" case using AE metric to emulate previous behavior
297-
if [ "$diff" -eq 0 ] && compare -metric AE "$REPORT_DIR"/"$name"-"$id1".png "$REPORT_DIR"/"$name"-"$id2".png null: 2>&1 | grep -q '^0' ; then
298-
diff="inf"
299-
fi
300-
if [ "$diff" == "inf" ] ; then
301-
local diff=50 #same
302-
fi
289+
# create black and white mask of difference, which we can use later (to count different pixels)
290+
compare -compose Src -highlight-color White -lowlight-color Black "$REPORT_DIR"/"$name"-"$id1".png "$REPORT_DIR"/"$name"-"$id2".png "$REPORT_DIR"/"$name"-"$idCompOverride".png
291+
local pxTotal; local pxDiff; local diff;
292+
# total pixels in image
293+
pxTotal="$( convert "$REPORT_DIR"/"$name"-"$idCompOverride".png -format "%[fx:w*h]" info: )"
294+
# different pixels
295+
# see: https://www.imagemagick.org/discourse-server/viewtopic.php?t=32078
296+
pxDiff="$( convert "$REPORT_DIR"/"$name"-"$idCompOverride".png -format "%[fx:int(w*h*mean+0.5)]" info: )"
297+
# percent of different pixels (rounded down)
298+
diff="$( LANG=C printf '100 * %f / %f\n' "${pxDiff}" "${pxTotal}" | bc )"
303299
echo $diff
304300
}
305301

0 commit comments

Comments
 (0)