#!/usr/bin/perl -s -w

# NAME
#        convert-to-dp - make tiff- and pdf-files usable for Distributed
#        Proofreaders
#
# SYNOPSIS
#        convert-to-dp [-options] input-file.ext
# 
# DESCRIPTION
#        This will convert input-file.ext to files named 001.png and 
#        001-half.png where 001 is the page number. The 001-half.png is
#        the page scaled to a width of 500 pixels. The files will be put
#        in a directory called input-file.
#
#        ext must be either pdf or tif.
#
# OPTIONS
#        -left val   cut the page at val pixels from the left edge
#        -right val  cut the page at val pixels from the right edge
#        -top val    cut the page at val pixels from the top
#        -bottom val cut the page at val pixels from the bottom
# 
# REQUIREMENT
#        - tiffsplit from libtiff
#        - tifftopnm, pnmcut, pnminvert, pnmdepth, pnmtopng from netpbm
#        - file from the file package
#        - pdftopbm from xpdf
#
# AUTHOR
#        Copyright (C) 2002-11-21 Ole Tange http://ole.tange.dk/dp/
#
# LICENSE
#        Released under GNU General Public License 

my $file;
for $file (@ARGV) {
    process_one_file($file);
}

sub process_one_file {
    my $file=shift;
    my $filetype=`file $file`; $filetype =~ s/$file//;
    my $tif = $filetype=~/TIFF/;
    my $pdf = $filetype=~/PDF/;
    if(not $tif and not $pdf) {
	warn "Unknown filetype for $file. Only pdf and tif is known";
	return;
    }
    my ($base)= ($file=~m:/([^/]+)\.(tiff?|pdf)$:i);
    if(not $base) {
	warn("Please rename $file to $file.pdf or $file.tif"); 
	return;
    }
    -d $base or 
	mkdir ($base, 0777) || die("Cannot make dir $base");

    # Split tiffile to x??.tif
    $tif and print qx{ tiffsplit $file $base/x };
    # Split pdffile to x-??????.pbm
    $pdf and print qx{ pdftopbm $file $base/x };

    # check if -left/-right/-top/-bottom is set
    my $pnmcut = pnmcut_command();

    chdir $base;
    my $pageno="001";
    my $sourcepage;
    my @pagefiles;
    $tif and @pagefiles=(<x??.tif>);
    $pdf and @pagefiles=(<x-??????.pbm>);
    for $sourcepage (@pagefiles) {
	$tif and print qx {
	    # Convert $sourcepage to PNM-format
	    # Invert the page back (It seems tifftopnm inverts the page?!)
	    # Cut the page out
	    tifftopnm $sourcepage | pnminvert | $pnmcut > /tmp/$$.pnm;
	};
	$pdf and print qx {
	    # Cut the page out
	    cat $sourcepage | $pnmcut > /tmp/$$.pnm;
	};
	print qx {
	    # Create black/white version
	    # Convert to png-file
	    pnmdepth 1 < /tmp/$$.pnm | pnmtopng > $pageno.png;
		
	    # Scale to width 500
	    # Use only 16-colours of gray (4-bit)
	    # Convert to png-file
	    pnmscale -width 500 < /tmp/$$.pnm | pnmdepth 4 | pnmtopng > $pageno-half.png;
	    rm /tmp/$$.pnm;
	};
	$pageno++;
    }
    unlink (@pagefiles);
    chdir "..";
}

sub pnmcut_command {
    my $cmd="";
    $left and $cmd.="-left $left ";
    $right and $cmd.="-right $right ";
    $top and $cmd.="-top $top ";
    $bottom and $cmd.="-bottom $bottom ";
    if($cmd) {
	$cmd = "pnmcut $cmd";
    } else {
	$cmd = "cat";
    }
    return $cmd;
}


