scanadf

a SANE frontend for acquiring multiple images from scanners equipped with an automatic document feeder (ADF)

News Download sane-bh History Acknowledgements

Here are a few example scan scripts that can be used with scanadf:

You can download the scripts and the g4rap program .


scanscript.sh

This is a very simple and verbose script which converts the normal pnm output into a compressed TIFF file using the pnmtotiff program.



#! /bin/bash
#
# scanscript.sh - a scanscript used with scanadf
#
# Usage: scanscript.sh <imagefile>
#   where imagefile is the image data just scanned

if [ $# -lt 1 ]; then
	echo "Usage: $0 <imagefile>"
	exit 1
fi

PNMTOTIFF=/usr/bin/pnmtotiff
IMAGE_FILE=$1
TIFF_FILE=${IMAGE_FILE%.*}.tif

echo "scanscript.sh"
echo "Image is ${SCAN_WIDTH}x${SCAN_HEIGHT} pixels at ${SCAN_RES}DPI"

echo -n "Converting image..."
$PNMTOTIFF -g4  "$IMAGE_FILE" > "$TIFF_FILE"
status=$?
echo "Status = ${status}"

exit $status
	

ssrawtopbm.sh

A fairly silly example of how to create a pbm file using the -raw scanadf option and a scan script. It's silly because you could accomplish the same thing using scanimage.



#! /bin/bash
#
# ssrawtopbm.sh - a scanscript used with scanadf
#
# Usage: scanscript.sh <imagefile>
#   where imagefile is the image data just scanned

# takes raw output and creates a rawbits pbm by adding the
# necessary header

if [ $# -lt 1 ]; then
	echo "Usage: $0 <imagefile>"
	exit 1
fi

PNMTOTIFF=/usr/bin/pnmtotiff
IMAGE_FILE=$1
PBM_FILE=${IMAGE_FILE%.*}.pbm
TIFF_FILE=${IMAGE_FILE%.*}.tif

echo "Image is ${SCAN_WIDTH}x${SCAN_HEIGHT} pixels at ${SCAN_RES}DPI"

echo "P4" > "$PBM_FILE"
#echo "# ssraw2pbm SANE data follows" >> "$PBM_FILE"
echo "${SCAN_WIDTH} ${SCAN_HEIGHT}" >> "$PBM_FILE"
cat "$IMAGE_FILE" >> "$PBM_FILE"

exit 0

ssrawtotif.sh

This script can be used to take a raw gray frame (without the pbm header) and create a G42D compressed TIFF file. It first adds the header, then calls pnmtotiff to perform the conversion.

This illustrates how the -raw scanadf option can bypass the writing of the header and allow your script to operate on the raw data. This is useful when you're working with compressed formats which do not have SANE frame types defined for them. You can have the backend lie and send it as a gray frame and then deal with the compressed data as you wish. A much better way is to use a script like ssall2tif.sh and a patch to sane.h which defines the correct frame types.



#! /bin/bash
#
# ssrawtotif.sh - a scanscript used with scanadf
#
# Usage: scanscript.sh <imagefile>
#   where imagefile is the image data just scanned

# takes raw output and creates a rawbits pbm by adding the
# necessary header, then converts that file to g4 compressed
# tiff

if [ $# -lt 1 ]; then
	echo "Usage: $0 <imagefile>"
	exit 1
fi

PNMTOTIFF=/usr/bin/pnmtotiff
IMAGE_FILE=$1
PBM_FILE=${IMAGE_FILE%.*}.pbm
TIFF_FILE=${IMAGE_FILE%.*}.tif

echo "Image is ${SCAN_WIDTH}x${SCAN_HEIGHT} pixels at ${SCAN_RES}DPI"

echo "P4" > "$PBM_FILE"
echo "# ssraw2pbm SANE data follows" >> "$PBM_FILE"
echo "${SCAN_WIDTH} ${SCAN_HEIGHT}" >> "$PBM_FILE"
cat "$IMAGE_FILE" >> "$PBM_FILE"
$PNMTOTIFF -g4  "$PBM_FILE" > "$TIFF_FILE"
status=$?
echo "Status = ${status}"

exit $status

ssg4totif.sh

Takes raw compressed g4 output and converts that image data to g4 compressed tiff file using g4rap.

This illustrates how the -raw scanadf option can bypass the writing of the header and allow your script to operate on the raw data. This is useful when you're working with compressed formats which do not have SANE frame types defined for them. You can have the backend lie and send it as a gray frame and then deal with the compressed data as you wish. A much better way is to use a script like ssall2tif.sh and a patch to sane.h which defines the correct frame types.



#! /bin/bash
#
# ssg4totif.sh - a scanscript used with scanadf
#
# Usage: ssg4totif.sh <imagefile>
#   where imagefile is the image data just scanned

# takes raw compressed g4 output and converts that file 
# to g4 compressed tiff file using g4rap

if [ $# -lt 1 ]; then
	echo "Usage: $0 <imagefile>"
	exit 1
fi

G4RAP=/usr/local/scanstation/bin/g4rap
IMAGE_FILE=$1
TIFF_FILE=${IMAGE_FILE%.*}.tif
echo "ssg4totif.sh: SCAN_RES:${SCAN_RES} SCAN_WIDTH:${SCAN_WIDTH} SCAN_HEIGHT:${SCAN_HEIGHT}"
$G4RAP -4 -R "${SCAN_RES}" -x "${SCAN_WIDTH}" -y "${SCAN_HEIGHT}" -o "${TIFF_FILE}" "${IMAGE_FILE}"
status=$?
exit $status
	

ssall2tif.sh

I use the following scan script in a document archiving application in a Bank in New York City. Although this script handles many different image formats, the one that we use 99.9% of the time is the G4-2D compressed format being wrapped into a TIFF file. This is a very common image format for document archiving. This script also handles decoded barcode data which is sent as a text frame.

NOTE: This requires a patch to sane.h which defines the additional/optional frame types for the compressed formats and text (for the barcode data).

You can get the patch on the download page.



#!/bin/sh
#
# ssall2tif.sh - a scanscript used with scanadf
#
# Usage: ssall2tif.sh <imagefile>
#   where imagefile is the image data just scanned

# this script will perform the following conversions:
# g42d -> tif
# gray -> tif
# g31d -> .g31d
# g32d -> .g32d
# text -> .txt (with only the decoded data)

if [ $# -lt 1 ]; then
	echo "Usage: $0 <imagefile>"
	exit 1
fi

G4RAP=$SS_DIR/bin/g4rap
PNMTOTIFF=/usr/bin/pnmtotiff
IMAGE_FILE=$1
base=`expr ${IMAGE_FILE} : '\(.*\)\.'
if [ -z "$base" ]
then
    base=$IMAGE_FILE
fi
    
TIFF_FILE=${base}.tif
TEXT_FILE=${base}.txt

case "$SCAN_FORMAT" in
    g42d )
	$G4RAP -4 -R "$SCAN_RES" -x "$SCAN_WIDTH" -y "$SCAN_HEIGHT" \
	    -o "$TIFF_FILE" "$IMAGE_FILE" ;
	rm -f "$IMAGE_FILE" ;
	;;
    gray )
	$PNMTOTIFF -g4  "$IMAGE_FILE" > "$TIFF_FILE" ;
	rm -f "$IMAGE_FILE" ;
	;;
    text )	
	# put the pure decoded data into a .txt file
	grep '^ <data>' "$IMAGE_FILE" | \
	    sed 's/ <data>//g; s/<\/data>//g' > $TEXT_FILE ;
	# cleanup a bit
	rm -f "$IMAGE_FILE" ;
	;;
    * )
	# unrecognized/unsupported formats
	TARGET_FILE=${IMAGE_FILE%.*}.${SCAN_FORMAT}
	mv $IMAGE_FILE $TARGET_FILE ;
	;;
esac



g4rap

This is a simple program called g4rap which puts the tiff header around the g4 compressed data. It works very efficiently, avoiding the decompression/ recompression of the image data. It uses libtiff so you must have that in order to link it. It is derived from the fax2tiff example program that is part of Sam Leffler's libtiff package.

Here's the synopsis:

usage: g4rap [options] input.g4
where options are:
 -B             input data has min 0 means black
 -L             input data has LSB2MSB bit order (default)
 -M             input data has MSB2LSB bit order
 -W             input data has min 0 means white (default)
 -R #           input data has # resolution (dpi) (default is 200)
 -x             input file has # scan line width in pixels
 -y             input file has # scan lines

 -o out.tif     write output to out.tif
 -4             generate G4-encoded output (default)
 -m             output fill order is MSB2LSB (default is LSB2MSB)
 -v             increase verbosity

You need to supply the -x, -y and -R parameters and probably -o as
well.  You get the -x and -y values from the GET_WINDOW when scanning.
If you use scanadf, you can put this guy in a script and access
the -x, -y, and -R values from environment variables as follows:


-R             SCAN_RES - the image resolution (in DPI)
-x             SCAN_WIDTH - the image width (in pixels)
-y             SCAN_HEIGHT - the image height (in pixels)

Here's a tarball g4rap-0.00.tar.gz.
Tom Martone
Last modified: Mon Feb 5 18:59:21 EST 2001