-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartpngdiff
executable file
·136 lines (112 loc) · 3.9 KB
/
smartpngdiff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/sh
#
# pngdiff.sh <ok_png> <png> <diff_png>
#
# Compare produced PNG to a reference file, using ImageMagick.
#
# Ref: ImageMagick Comparing:
# <http://www.imagemagick.org/Usage/compare/>
#
# Recommended:
# ImageMagick >= 6.4.0 (works crippled with IM 6.2.x)
#
OK_PNG=$1
PNG=$2
DIFF_PNG=$3
if test "$IGNORE_WGS84" != "Y" ; then
# Detect WGS84 branch
grep --quiet "#define WGS84 1" /usr/include/smartmet/newbase/NFmiGlobals.h
wgs84=$(expr $? == 0)
fi
# And use WGS84 result if it exists in WGS84 branch
if [[ $wgs84 == 1 ]]; then
wgs84png=${OK_PNG}.wgs84
if [ -e $wgs84png ]; then
OK_PNG=$wgs84png
fi
fi
if [[ $CI == "" ]]; then
COL_RED="$(tput setaf 1)"
COL_GREEN="$(tput setaf 2)"
COL_YELLOW="$(tput setaf 3)"
COL_NORM="$(tput setaf 7)"
COL_BOLD="$(tput bold)"
COL_OFF="$(tput sgr0)"
fi
if [ \! -f $PNG ]; then
echo "${COL_RED}*** FAILED:${COL_OFF} '${PNG}' not created"
exit -1
fi
# Create a visual difference file
#
composite $OK_PNG $PNG -compose DIFFERENCE png:- | \
convert - -contrast-stretch 0 $DIFF_PNG
# Get measurement of the difference
#
# Metric can be:
# AE (Absolute Error): Number of pixels that were different, after applying fuzziness factor
# PAE (Peak Absolute Error)
# PSNR (Peak Signal/Noise Ratio): dB (1..inf) 1=all different, 20=differences 1/100 of maximum
# MAE (Mean Absolute Error)
# MSE (Mean Squared Error)
# RMSE sqrt of MSE
#
# Note: '-metric AE' and '-fuzz n%' would be a good combo, but requires IM >= 6.4.3
#
#V="$(compare 2>&1 -metric AE -fuzz 5% ${OK_PNG} ${PNG} ${DIFF_PNG})"
# Note: IM 6.4.x outputs just the PSNR number (i.e. 22.432) but 6.2.8 outputs
# "22.432 dB\n300,300,PNG" which is... troublesome
#
#V="$(compare 2>&1 -metric PSNR ${OK_PNG} ${PNG} ${DIFF_PNG} | head -1 | sed "-es/ dB//")"
# Note: IM 6.2.x seems to have trouble with opacity; gives many
# "image opacity differs" errors; 6.4.x gives not.
#
V=$((compare 2>&1 -metric PSNR ${OK_PNG} ${PNG} /dev/null | head -1 | sed "-es/ dB//") || exit 255)
# Cut non-decimal values (error string) out
#
# Note: Need to use 'bc' to handle non-integer comparisons. 'expr' would handle
# decimals as string comparison, i.e. 'expr "7.1" \> 10' --> 1.
#
#echo "<<$V232321>>" | sed -es/[1-9.]//g
#echo "<<$V>>" | sed -es/[1-9.]//g
#
#if [ -n $(echo "$V" | sed -es/[1-9.]//g) ]; then
# echo "${COL_RED}FAIL:${COL_OFF} $V"
# exit 100
# HACK: A real hack. Did not get the above code to work.
#
if [ $(echo "$V" | grep compare | wc -l) = 1 ]; then
echo "${COL_RED}FAIL:${COL_OFF} $V"
if [ $(echo "$V" | grep "opacity differs" | wc -l) = 1 ]; then
echo "${COL_BOLD}ImageMagick >= 6.4.x is needed for this test${COL_OFF}"
fi
exit 100
fi
nw=$(echo $V | wc -w)
if test "$nw" = 1 ; then
matches="inf";
else
matches="0";
fi
V=$(echo $V | sed -e 's/\s..*$//')
if [ "$V" = "$matches" ] ; then
echo "${COL_GREEN}OK:${COL_OFF}"
elif [ $(echo "$V >= 50" | bc) = 1 ]; then
echo "${COL_GREEN}OK:${COL_OFF} PSNR >= 50dB ($V dB)"
exit 0
elif [ $(echo "$V >= 20" | bc) = 1 ]; then
echo "${COL_YELLOW}WARNING:${COL_OFF} 20dB <= PSNR < 50dB ($V dB)"
exit 0
elif [ $(echo "$V >= 0" | bc) = 1 ]; then
echo "${COL_RED}FAIL:${COL_OFF} PSNR < 20dB ($V dB)"
exit 100
fi
# convert -delay 50 results_ok/${PNG} results/${PNG} -loop 0 results_diff/$(PNG:.png=.anim.gif)
# compare -metric AE -fuzz 50% results_ok/$(PNG) results/$(PNG) results_diff/$(PNG)
# This requires IM >= 6.4.2-8 (creates gray scale mask of the differences)
#
# compare -metric AE -fuzz 5% results_ok/$(PNG) results/$(PNG) \
# -compose Src -highlight-color White -lowlight-color Black results_diff/$(PNG)
# composite -compose DIFFERENCE results_ok/$(PNG) results/$(PNG) results_diff/$(PNG)
# convert -threshold 25% -colorize 0/100/0 results_diff/$(PNG) results_diff/2_$(PNG)
# compare -metric PSNR results_ok/$(PNG) results/$(PNG) results_diff/$(PNG)