%D \module
%D   [      file=t-degrade,
%D        version=2005.08.14,
%D          title=\CONTEXT\ User Module,
%D       subtitle=Degrading JPEG images,
%D         author=Peter Münster,
%D           date=\currentdate,
%D      copyright={Peter Münster}]
%C This module is copyrighted by Peter Münster.
%C Please send any comments to pmrb at free.fr.

% 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 2
% 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.

\writestatus{loading}{Degrading JPEG images on the fly}

\unprotect

%D In this module we suppose, that original figures are either eps or jpg.
%D When creating pdf, eps is converted to pdf, and when creating dvi,
%D jpg is converted to eps.
%D Furthermore, jpg-images can be degraded to a given resolution, to get
%D smaller documents.

%D \macros
%D   {DegSetup}
%D
%D Sets the resolution, the directory for degraded images and the
%D directory, where the original images can be found (source directory).
%D
%D Default setup:
%D
%D \starttyping
%D \DegSetup{Res=100,Dir=degrade,SDir=.}
%D \stoptyping
%D
%D Example:
%D
%D \starttyping
%D \DegSetup{Res=600,Dir=degraded-images,SDir=/home/peter/jpegs}
%D \stoptyping

\def\DegSetup#1{\getparameters[Deg][#1]%
  \setupexternalfigures[directory={\DegDir,\DegSDir}]}
\DegSetup{Res=100,Dir=degrade,SDir=.}

%D In order to use this module, you must use \type{\externalfigure}
%D in the following way: \type{\externalfigure[file-prefix][options]},
%D where \type{options} must contain the width or the height.
%D Furthermore, \type{\write18} must be enabled.
%D The shell-script is in a buffer, just to keep this module in only
%D one file.

\startbuffer[degrade-script]
#!/bin/bash

# We consider, that original figures are either eps or jpg.

# Arguments:
# $1 = filename
# $2 = eps or pdf
# $3 = w or h
# $4 = resolution
# $5 = dimension of image
# $6 = source directory
# $7 = destination directory

for i in epstopdf jpeg2ps convert identify; do
	if ! type $i &>/dev/null; then
		echo Error: $i is not installed.
		exit 1
	fi
done

if [ -s "$1.jpg" ]; then
	if [ "$2" = eps ] && [ "$1.jpg" -nt "$1.eps" ]; then
		jpeg2ps "$1.jpg" >"$1.eps"
	fi
else
	if [ "$2" = pdf ] && [ "$1.eps" -nt "$1.pdf" ]; then
		epstopdf "$1.eps"
	fi
fi

X=
[ $3 = h ] && X=x
FORMAT="%$3"
FILE="$6/$1.jpg"
DFILE="$7/$1.jpg"
DFILE_EPS="$7/$1.eps"
DPI="$4"
IW="$5"
MODE="$2"
INCH=72.27

mkdir -p $7

if [ -s "$FILE" ]; then
	W=`identify -format "$FORMAT" "$FILE"`
else
	rm -f "$DFILE"
	exit 0
fi

NW=`echo "$DPI * $IW / $INCH" | bc`

if [ $NW -ge $W ]; then
	rm -f "$DFILE"
else
	if [ -s "$DFILE" ] && \
		[ `identify -format "$FORMAT" "$DFILE"` -eq $NW ]; then
		exit 0
	else
		convert -verbose -resize $X$NW "$FILE" "$DFILE"
		[ $MODE = eps ] && jpeg2ps "$DFILE" >"$DFILE_EPS"
	fi
fi
\stopbuffer

\def\Command{\immediate\write18}
\def\Deg@CreateFile{%
  \getparameters[Deg][height=,\appliedfigureoptions]%
  \processaction[\Degheight][%
    \s!default =>\scratchdimen=\Degwidth  \def\Deg@WH{w},
    \s!unknown =>\scratchdimen=\Degheight \def\Deg@WH{h}]%
  \Command{bash ./\jobname-degrade-script.tmp \appliedfigurefilename\space
    \ifcase\pdfoutput eps\else pdf\fi\space\Deg@WH\space\DegRes\space
    \withoutpt\the\scratchdimen\space\DegSDir\space\DegDir}}
\appendtoks \Deg@CreateFile \to \externalfigurepostprocessors

\protect

%%% Local Variables: 
%%% mode: context
%%% End: 
