--- /dev/null
+# Copyright © 2013, 2021 Nick Bowler
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+AT_BANNER([Image format tests])
+
+m4_divert_push([PREPARE_TESTS])dnl
+# Read the header from a PAM image (from standard input) and set declared
+# parameters in shell variables. For example, if the PAM header specifies
+# DEPTH 2, the shell variable pam_DEPTH will be set to 2.
+#
+# Returns a nonzero status if the header could not be parsed.
+pam_read_header () {
+ pam_save_IFS=$IFS
+ pam_p7_seen=false pam_endhdr_seen=false
+
+ while IFS= read a; do
+ case $a in
+ P7)
+ $pam_p7_seen && break # duplicated header
+ pam_p7_seen=true
+ ;;
+ ENDHDR)
+ pam_endhdr_seen=true
+ break
+ ;;
+ *)
+ IFS=$pam_save_IFS read b c <<EOF
+$a
+EOF
+ b=AS_TR_SH([$b])
+ AS_VAR_SET([pam_$b], [$c])
+ ;;
+ esac
+ done
+
+ IFS=$pam_save_IFS
+ $pam_p7_seen && $pam_endhdr_seen
+}
+
+pam_check_header () {
+ for pam_arg
+ do
+ AS_UNSET([pam_$pam_arg])
+ done
+
+ pam_read_header || return
+
+ for pam_arg
+ do
+ AS_VAR_SET_IF([pam_$pam_arg],
+ [AS_VAR_COPY([pam_val], [pam_$pam_arg])],
+ [AS_UNSET([pam_val])])
+ AS_ECHO(["$pam_arg${pam_val+ = }${pam_val- unset}"])
+ done
+}
+m4_divert_pop([PREPARE_TESTS])
+
+AT_SETUP([Empty image])
+
+AT_CHECK([lbximg -F pbm -dnf "$testdata/image-0x0" && cat out.000.pbm], [0],
+[P1
+0 0
+])
+
+AT_CLEANUP
+
+AT_SETUP([PAM output format (no palette)])
+
+# image-1x1 (no palette)
+AT_CHECK([lbximg -F pam -dnf "$testdata/image-1x1"])
+AT_CHECK([pam_check_header WIDTH HEIGHT DEPTH TUPLTYPE <out.000.pam], [0],
+[WIDTH = 1
+HEIGHT = 1
+DEPTH = 1
+TUPLTYPE = GRAYSCALE
+])
+
+AT_CLEANUP
+
+AT_SETUP([PAM output format (no palette + transparency)])
+
+# image-1x1 (no palette)
+AT_CHECK([lbximg -F pam -dnf "$testdata/image-1x1-mt"])
+AT_CHECK([pam_check_header WIDTH HEIGHT DEPTH TUPLTYPE <out.000.pam], [0],
+[WIDTH = 1
+HEIGHT = 1
+DEPTH = 2
+TUPLTYPE = GRAYSCALE_ALPHA
+])
+
+AT_CLEANUP
+
+AT_SETUP([PAM output format (palette)])
+
+# image-1x1 (no palette)
+AT_CHECK([lbximg -F pam -df "$testdata/image-1x1" \
+ -p "$testdata/palette-ramp"])
+AT_CHECK([pam_check_header WIDTH HEIGHT DEPTH TUPLTYPE <out.000.pam], [0],
+[WIDTH = 1
+HEIGHT = 1
+DEPTH = 3
+TUPLTYPE = RGB
+])
+
+AT_CLEANUP
+
+AT_SETUP([PAM output format (palette + transparency)])
+
+# image-1x1 (no palette)
+AT_CHECK([lbximg -F pam -df "$testdata/image-1x1-mt" \
+ -p "$testdata/palette-ramp"])
+AT_CHECK([pam_check_header WIDTH HEIGHT DEPTH TUPLTYPE <out.000.pam], [0],
+[WIDTH = 1
+HEIGHT = 1
+DEPTH = 4
+TUPLTYPE = RGB_ALPHA
+])
+
+AT_CLEANUP