#!/bin/sh
# ~jhs/bin/.sh/portrait
# http://www.berklix.com/~jhs/bin/.sh/portrait
# ln -s portrait landscape

# This will be used by	
# http://www.berklix.com/~jhs/src/bsd/fixes/FreeBSD/src/jhs/share/mk/berklix2.mkecho "Deciding if portrait or landscape for $1"

# tiffinfo shows:
#  common to all 4 of
#	   scanjet_landscape scanjet_portrait 
#	     xsane_landscape   xsane_portrait
#    Bits/Sample: 1
#    Orientation: row 0 top, col 0 lhs
#    Photometric Interpretation: min-is-white
#    Planar Configuration: single image plane
#    Resolution: 300, 300 pixels/inch
#    Samples/Pixel: 1
#  scanjet_landscape
#    Compression Scheme: CCITT Group 4
#    FillOrder: msb-to-lsb
#    Image Width: 3509 Image Length: 2408
#    Rows/Strip: 18
#    Software: tiffrotate
#  scanjet_portrait
#    Compression Scheme: CCITT Group 4
#    FillOrder: msb-to-lsb
#    Image Width: 2408 Image Length: 3509
#    Rows/Strip: 27
#    Subfile Type: (0 = 0x0)
#  xsane_landscape
#    Compression Scheme: CCITT Group 3
#    DateTime: 2022:08:27 21:23:06
#    Image Width: 3239 Image Length: 2344
#    Rows/Strip: 20
#    Software: xsane
#  xsane_portrait
#    Compression Scheme: CCITT Group 3
#    DateTime: 2022:08:27 20:48:12
#    Image Width: 2344 Image Length: 3239
#    Rows/Strip: 27
#    Software: xsane
#  Other non standard sizes from `find`:
#   Image Width: 2544 Image Length: 3508	scan mk eye op
#   Image Width: 276 Image Length: 172		scan of id card 
#   Image Width: 276 Image Length: 174		scan of id card
#   Image Width: 278 Image Length: 174		scan of id card
#   Image Width: 279 Image Length: 172		scan of id card
#   Image Width: 4432 Image Length: 3002	xsane using munich scanner of small paper manual
#   Image Width: 4440 Image Length: 2994	xsane using munich scanner of small paper manual
#   Image Width: 4448 Image Length: 2965	xsane using munich scanner of small paper manual
#   Image Width: 4448 Image Length: 3010	xsane using munich scanner of small paper manual
#   Image Width: 4456 Image Length: 2965	xsane using munich scanner of small paper manual
#   Image Width: 4456 Image Length: 2994	xsane using munich scanner of small paper manual
#   Image Width: 4456 Image Length: 3002	xsane using munich scanner of small paper manual
#   Image Width: 4472 Image Length: 3002	xsane using munich scanner of small paper manual
#   Image Width: 4480 Image Length: 2989	xsane using munich scanner of small paper manual
#   Image Width: 4480 Image Length: 3002	xsane using munich scanner of small paper manual
#   Image Width: 4960 Image Length: 4961	scan of print of paper picture
#   Image Width: 4960 Image Length: 7015	scan of id paper 
#   Image Width: 801 Image Length: 1167 	book
#   Image Width: 9920 Image Length: 9921	xsane using aachen scanner of floor plan

# scanjet=Hewlett Packard Network ScanJet 5 http://www.berklix.com/scanjet FBSD-4.11-RELEASE
# xsane=xsane on FreeBSD-12.3-RELEASE
# /usr/local/bin/tiffrotate 
#	Exists in FreeBSD-4.11-RELEASE scanjet
#		usage: tiffrotate { left | right | flip } infile outfile
#		Note flip means flip top & bottom of a portrait,
#		& also flip left & right, ie as if rotated 180 degrees.
#		Useful if top edge of stack of paper is chewed with
#		staples, & you want to feed ADF with bottom edge instead.
#		Caution: no manual.
#		Caution: if infile is a multi page file, outfile
#			is only first page.  So first use tiffsplit,
#			then rotate each page, then append with tiffcp
#	Not in FreeBSD-9.2-RELEASE & 12.3-RELEASE, only
#		gifrotat pnmrotate rotate1_reg rotate2_reg rotatefastalt
#		rotateorth_reg rotateorthtest1 rotatetest1


# Sample of testing aspect ratio:
#	@# Old		grep -q "Software: tiffrotate"
#	@# New		grep -q "${TIFF_LANDSCAPE_A4}"
#	if ( ! ${TIFFINFO} ${.IMPSRC} | \
#			grep -q "${TIFF_LANDSCAPE_A4}" ) ; \
#		then \
#		${ECHO_BUILD} "${ASPECT_PORT}"

# Old test 'grep -q "Software: tiffrotate"' was a crude
# relying on the assumption I produced the tiff with my
# http://www.berklix.com/scanjet/

# New test is not much better, as too precise, what about non A4 sizes ?
# JJLATER look at .tiff scans from other sizes & other people's OS's.
# JJLATER A more sophisticated method would be calculate if aspect ratio, > or < 1

if ( ! ${TIFFINFO} $1 | grep -q "${TIFF_LANDSCAPE_A4}" ) ; \
	then \
		echo PORTRAIT ; \
else	\
		echo LANDSCAPE ; \
fi

