Aligning with WRF
[WPS.git] / external / jasper-1.900.29 / test / bin / run_codec_test
blobf63ef8dede29508b209af434f2598af1ab769871
1 #! /bin/bash
2 # Copyright (c) 2016 Michael David Adams
4 cmd_dir=$(dirname "$0") || exit 1
5 source "$cmd_dir/utilities" || exit 1
6 source "$cmd_dir/jpcod" || exit 1
8 set_source_and_build_dirs || panic "cannot set source and build directories"
9 tmp_dir=$(make_tmp_dir)
11 init
13 export IMGINFO_COMMAND="$abs_top_builddir/src/appl/imginfo"
14 IMGCMP="$abs_top_builddir/src/appl/imgcmp"
16 MKDATA=1
17 DATADIR="$tmp_dir"
18 # Only warn if an image is missing.
20 test_file=$cmd_dir/codec_tests
21 JPENC=$cmd_dir/jpenc
22 JPDEC=$cmd_dir/jpdec
23 image_viewer="display -geometry 256x256> -geometry +0+0"
25 ################################################################################
27 ################################################################################
29 usage()
31 cat <<- EOF
32 usage: $0 [options] test...
33 Options:
34 -e encoder
35 -d decoder
36 -D fatal|ignore How to handle distortion constraint violation.
37 -R fatal|ignore How to handle rate constraint violation.
38 -C fatal|ignore How to handle codec failures.
39 EOF
40 exit 2
43 ################################################################################
45 ################################################################################
47 image_path="$IMGPATH"
48 show_image=0
49 missing_image_fatal=1
50 error_fatal=1
51 distortion_violation_fatal=1
52 rate_violation_fatal=1
53 debug=0
54 verbose=0
55 tests=()
56 enc=jasper
57 dec=jasper
58 skip_bugs=1
60 while getopts d:e:i:vBE: opt; do
61 case "$opt" in
63 dec="$OPTARG";;
65 enc="$OPTARG";;
67 image_path="$image_path:$OPTARG";;
69 verbose=$((verbose + 1));;
71 skip_bugs=0;;
73 case "$OPTARG" in
74 ignore)
75 error_fatal=0
76 rate_violation_fatal=0
77 distortion_violation_fatal=0
78 missing_image_fatal=0
80 fatal|*)
81 error_fatal=1
82 rate_violation_fatal=1
83 distortion_violation_fatal=1
84 missing_image_fatal=1
86 esac
88 \?)
89 usage
90 break;;
91 esac
92 done
93 shift $((OPTIND - 1))
95 if [ $# -ge 1 ]; then
96 tests=("$@")
99 ################################################################################
101 ################################################################################
103 if [ -z "${tests[@]}" ]; then
104 tcf_gettestids "$test_file" tests || panic "cannot get tests"
107 [ -d $TMPDIR ] || mkdir -p $TMPDIR
108 [ -d $DATADIR ] || mkdir -p $DATADIR
110 num_errors=0
111 failed_tests=()
112 skipped_tests=()
113 rate_violations=()
114 distortion_violations=()
115 missing_images=()
117 for test in "${tests[@]}"; do
119 echo "############################################################"
121 tcf_gettest "$test_file" "$test" record || \
122 panic "cannot get test information for $test"
124 in_file_base=""
125 enc_opts=()
126 dec_opts=()
127 rate=0
128 max_pae=""
129 min_psnr=""
130 bug=""
131 enc_fmt=jpc
132 #enc_fmt=jp2
134 for tag in "${!record[@]}"; do
135 value="${record[$tag]}"
136 case "$tag" in
137 imgareatlx|imgareatly|tilegrdtlx|tilegrdtly|tilewidth|tileheight|prcwidth|prcheight|cblkwidth|cblkheight|mode|nomct|numrlvls|numgbits|ilyrrates|prg|pterm|termall|lazy|segsym|resetprob|vcausal|sop|eph|roirect)
138 enc_opts+=("$tag=$value")
140 rate)
141 enc_opts+=("$tag=$value")
142 rate="${record[$tag]}"
144 maxlyrs)
145 dec_opts+=("$tag=$value")
147 image)
148 in_file_base="$value";;
149 pae)
150 max_pae="$value";;
151 psnr)
152 min_psnr="$value";;
154 if [ "$test" != "$value" ]; then
155 panic "test mismatch"
158 fmt)
159 enc_fmt="$value";;
160 bug)
161 bug="$value";;
163 echo "unknown option $tag"
164 exit 1
166 esac
167 done
169 # Check for known bug in encoder and/or decoder.
170 has_enc_bug=0
171 case $enc in
172 jasper)
173 case "$bug" in
174 *jasper_enc*|*jasper_cod*)
175 has_enc_bug=1;;
176 esac
178 jj2k)
179 case "$bug" in
180 *jj2000_enc*|*jj2000_cod*)
181 has_enc_bug=1;;
182 esac
184 kakadu)
185 case "$bug" in
186 *kakadu_enc*|*kakadu_cod*)
187 has_enc_bug=1;;
188 esac
191 case "$bug" in
192 *oj_enc*|*oj_cod*)
193 has_enc_bug=1;;
194 esac
196 esac
197 has_dec_bug=0
198 case $dec in
199 jasper)
200 case "$bug" in
201 *jasper_dec*|*jasper_cod*)
202 has_dec_bug=1;;
203 esac
205 jj2k)
206 case "$bug" in
207 *jj2000_dec*|*jj2000_cod*)
208 has_dec_bug=1;;
209 esac
211 kakadu)
212 case "$bug" in
213 *kakadu_dec*|*kakadu_cod*)
214 has_dec_bug=1;;
215 esac
218 case "$bug" in
219 *oj_dec*|*oj_cod*)
220 has_dec_bug=1;;
221 esac
223 esac
224 if [ $has_enc_bug -ne 0 ]; then
225 echo "WARNING: ENCODER HAS KNOWN BUG"
227 if [ $has_dec_bug -ne 0 ]; then
228 echo "WARNING: DECODER HAS KNOWN BUG"
231 if [ $skip_bugs -ne 0 -a \
232 \( $has_enc_bug -ne 0 -o $has_dec_bug -ne 0 \) ]; then
233 echo "WARNING: skipping $test"
234 skipped_tests+=("$test")
235 continue
238 in_file=$(image_which "$image_path" "$in_file_base") || \
239 panic "cannot find image"
240 if [ ! -f $in_file ]; then
241 if [ $missing_image_fatal -ne 0 ]; then
242 echo "ERROR: CANNOT FIND IMAGE $in_file_base"
243 exit 1
245 echo "WARNING: CANNOT FIND IMAGE $in_file_base"
246 echo "WARNING: skipping $test"
247 skipped_tests+=("$test")
248 missing_images+=("$in_file_base")
249 continue
252 if [ $verbose -ne 0 ]; then
253 enc_opts+=(verbose)
254 dec_opts+=(verbose)
256 if [ $debug -ne 0 ]; then
257 enc_opts+=("debug=$debug")
258 dec_opts+=("debug=$debug")
261 image_format=$(image_info "$in_file" format) || \
262 panic "cannot get image format"
263 image_num_comps=$(image_info "$in_file" num_components) || \
264 panic "cannot get number of image components"
265 image_width=$(image_info "$in_file" width) || \
266 panic "cannot get image width"
267 image_height=$(image_info "$in_file" height) || \
268 panic "cannot get image height"
269 image_prec=$(image_info "$in_file" depth) || \
270 panic "cannot get image depth"
271 image_size=$(image_info "$in_file" size) || \
272 panic "cannot get image size"
274 if [ "$image_format" = mif -o "$image_format" = jpc -o \
275 "$image_format" = jp2 -o "$image_format" = pgx ]; then
276 dec_fmt="$image_format"
277 else
278 dec_fmt="$image_format"
279 if [ "$image_num_comps" -eq 1 -a "$image_prec" -gt 8 ]; then
280 dec_fmt=pgx
281 else
282 dec_fmt=pnm
286 if [ $rate = 0 ]; then
287 target_size=0
288 else
289 target_size=$(evalexpr "$image_size * $rate + 1" | realtoint) || panic
292 enc_file="$TMPDIR/test.$enc_fmt"
293 dec_file="$TMPDIR/recon.$dec_fmt"
294 LOGFILE="$TMPDIR/log.txt"
295 #DIFFFILE="$TMPDIR/diff.pnm"
296 DIFFFILE=""
298 #rm -f $TMPDIR/*.pgm $TMPDIR/*.ppm $TMPDIR/*.$dec_fmt
299 rm -f "$enc_file"
300 rm -f "$dec_file" "$DIFFFILE"
302 if [ \( "$image_format" != pnm -a "$image_format" != pgx -a \
303 "$enc" != jasper \) -o \( "$image_format" != pnm -a "$enc" = kakadu \) \
304 ]; then
305 echo "WARNING: encoder cannot handle input image format $image_format... skipping"
306 skipped_tests+=("$test")
307 continue
309 if [ \( "$dec_fmt" != pnm -a "$dec_fmt" != pgx -a "$dec" != jasper \) -o \
310 \( "$dec_fmt" != pnm -a "$dec" = kakadu \) ]; then
311 echo "WARNING: decoder cannot handle output image format $dec_fmt... skipping"
312 skipped_tests+=("$test")
313 continue
316 echo "TESTID=$test ENC=$enc DEC=$dec"
317 if [ $verbose -ne 0 ]; then
318 echo "FMT=$image_format WIDTH=$image_width HEIGHT=$image_height PREC=$image_prec NUMCMPTS=$image_num_comps RAWSIZE=$image_size"
320 echo "test information:"
321 for tag in "${!record[@]}"; do
322 printf " %-16s %-16s\n" "$tag" "${record[$tag]}"
323 done
326 $JPENC software=$enc input=$in_file output=$enc_file fmt=$enc_fmt "${enc_opts[@]}"
327 enc_status=$?
328 if [ $enc_status -eq 2 ]; then
329 echo "WARNING: encoder feature not supported"
330 echo "skipping"
331 skipped_tests+=("$test")
332 continue
334 if [ $enc_status -ne 0 ]; then
335 echo "ERROR: ENCODER FAILURE"
336 error_count=$((error_count + 1))
337 failed_tests+=($test.encode)
338 if [ "$error_fatal" -ne 0 ]; then
339 exit 1
340 else
341 continue
345 enc_size=$(wc -c $enc_file | awk '{print $1}' -) || \
346 panic "cannot get encoded size"
348 $JPDEC software=$dec input=$enc_file output=$dec_file "${dec_opts[@]}"
349 dec_status=$?
350 if [ $dec_status -ne 0 ]; then
351 echo "ERROR: DECODER FAILURE"
352 error_count=$((error_count + 1))
353 failed_tests+=($test.decode)
354 if [ "$error_fatal" -ne 0 ]; then
355 exit 1
356 else
357 continue
361 if [ \( -z "$max_pae" \) -a \( -z "$min_psnr" \) ]; then
362 echo "ERROR: NO ERROR CONSTRAINT SPECIFIED"
363 exit 1
366 if [ $rate != 0 ]; then
367 if [ $enc_size -gt $target_size ]; then
368 echo "ERROR: RATE CONSTRAINT VIOLATED ($enc_size > $target_size)"
369 if [ \( $enc = jasper \) -a \( $rate_violation_fatal -ne 0 \) ]; then
370 exit 1
372 rate_violations+=("$test")
376 FILE0=$(pnutod $in_file)
377 FILE1=$(pnutod $dec_file)
378 PAE=$($IMGCMP -f $FILE0 -F $FILE1 -m pae --max 2> /dev/null) || panic
379 if [ -z "$PAE" ]; then
380 echo "cannot get PAE $FILE0 $FILE1"
381 exit 1
383 if [ $PAE -ne 0 ]; then
384 psnr=$($IMGCMP -f $FILE0 -F $FILE1 -m psnr --min 2> /dev/null)
385 echo "STATUS: LOSSY (ENCFILESIZE=$enc_size ENCSIZE=$target_size PAE=$PAE PSNR=$psnr)"
386 else
387 echo "STATUS: LOSSLESS (ENCFILESIZE=$enc_size)"
388 psnr=1000.0
390 if [ -z "$psnr" ]; then
391 echo "cannot get PSNR"
392 exit 1
394 if [ -n "$max_pae" ]; then
395 if [ $PAE -gt $max_pae ]; then
396 echo "ERROR: PAE CONSTRAINT NOT SATISFIED ($PAE > $max_pae)"
397 if [ $distortion_violation_fatal -ne 0 ]; then
398 $image_viewer $dec_file
399 exit 1
401 distortion_violations+=("$test")
404 if [ -n "$min_psnr" ]; then
405 CONSTRAINT=$(evalrelexpr "$psnr > $min_psnr")
406 if [ $CONSTRAINT -eq 0 ]; then
407 echo "ERROR: PSNR CONSTRAINT NOT SATISFIED ($psnr < $min_psnr)"
408 if [ $distortion_violation_fatal -ne 0 ]; then
409 $image_viewer $dec_file
410 exit 1
412 distortion_violations+=("$test")
416 if [ $show_image -ne 0 ]; then
417 echo "Type Ctrl-Q to exit ImageMagick"
418 $image_viewer $dec_file
419 #PID=$!
420 #sleep 5
421 #kill $PID
424 if [ $MKDATA -ne 0 ]; then
425 cp $enc_file $DATADIR/$test.$enc_fmt
428 done
430 if [ ${#distortion_violations[@]} -ne 0 ]; then
431 echo "ERROR: number of distortion constraint violations ${#distortion_violations[@]}"
432 echo "${distortion_violations[@]}"
434 if [ "${#rate_violations[@]}" -ne 0 ]; then
435 echo "ERROR: number of rate constraint violations ${#rate_violations[@]}"
436 echo "${rate_violations[@]}"
438 if [ "${#missing_images[@]}" -ne 0 ]; then
439 echo "ERROR: number of missing images: ${#missing_images[@]}"
440 echo "skipped images: ${missing_images[@]}"
442 if [ "${#skipped_tests[@]}" -ne 0 ]; then
443 echo "WARNING: skipped ${#skipped_tests[@]} tests"
444 echo "${skipped_tests[@]}"
446 if [ \( $distortion_violation_fatal -ne 0 \) -a \( ${#distortion_violations[@]} -gt 0 \) ]; then
447 exit 1
449 if [ \( $rate_violation_fatal -ne 0 \) -a \( ${#rate_violations[@]} -gt 0 \) ]; then
450 exit 1
453 echo "############################################################"
454 echo "TEST SUMMARY"
455 echo "Number of errors: $num_errors"
456 if [ "${#failed_tests[@]}" -ne 0 ]; then
457 echo "failed tests:"
458 for i in "${failed_tests[@]}"; do
459 echo " $i"
460 done
462 if [ "$num_errors" -gt 0 ]; then
463 echo "STATUS: FAIL"
464 exit 1
466 echo "STATUS: SUCCESS"