summaryrefslogtreecommitdiff
path: root/obsolete/macros/context/contrib/context-degrade/tex/context/third/degrade/t-degrade.tex
blob: b2dbba495c3ea6d470eef42b9d38be6d47cf839f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
%D \enableregime[utf]
%D \module
%D   [      file=t-degrade,
%D        version=2006.09.12,
%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 When
%D \type{http://context.literatesolutions.com/collector/63}
%D is solved, we can make it better using \type{\appliedfigurefilename},
%D \type{\figurewidth} etc, and without redefining \type{\externalfigure}.

%D \macros{setupDegrade}
%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 \setupDegrade[Res=100,Dir=degrade,SDir=.]
%D \stoptyping
%D
%D Example:
%D
%D \starttyping
%D \setupDegrade[Res=600,Dir=degraded-images,SDir=/home/peter/jpegs]
%D \stoptyping

\def\setupDegrade[#1]{\getparameters[Deg][#1]}
\setupDegrade[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 jpeg2ps convert identify bc; do
	if ! type $i &>/dev/null; then
		echo Error: $i is not installed.
		exit 1
	fi
done

EPSTOPDF=epstopdf

if ! type epstopdf &>/dev/null; then
	if type texmfstart &>/dev/null; then
		EPSTOPDF="texmfstart newpstopdf"
	else
		EPSTOPDF="texutil --figures --epstopdf"
	fi
fi

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}
\let\externalfigureO=\externalfigure
\def\externalfigure[#1][#2]{%
  \getparameters[Deg][height=,#2]%
  \processaction[\Degheight][%
    \s!default =>\scratchdimen=\Degwidth  \def\Deg@WH{w},
    \s!unknown =>\scratchdimen=\Degheight \def\Deg@WH{h}]%
  \Command{bash ./\jobname-degrade-script.tmp #1 \ifcase\pdfoutput eps\else
    pdf\fi\space\Deg@WH\space\DegRes\space\withoutpt\the\scratchdimen\space
    \DegSDir\space\DegDir}%
  \doiffileexistselse{\DegDir/#1.jpg}{\def\Deg@File{\DegDir/#1}}{%
    \def\Deg@File{#1}}%
  \externalfigureO[\Deg@File][#2]}

\protect

\doifnotmode{demo}{\endinput}

%D Usage example:
%\usemodule[degrade]
% \setupDegrade[Res=200] Here you can adjust the resolution (unit = dpi).
\starttext
\externalfigure[hacker][width=0.2\textwidth]
\stoptext