summaryrefslogtreecommitdiff
path: root/pdf2png
blob: 067644002142a83dc5d043a88a986f7ee97fd4d6 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/bin/bash
###############################################################################
#                                                                             # 
#    Martin Bruchanov, bruxy at regnet dot cz                                 #
#                                                                             # 
#    http://bruxy.regnet.cz/                                                  #
#                                                                             # 
#    Version: 1.2 (Fri Jul 20 13:17:18 CEST 2012)                             #
#                                                                             # 
###############################################################################

## Defaults
DEFAULT_DPI=300    # default output dpi
GS_DPI=300         # gs will rasterize to this size, before resizing
UNSHARP_RADIUS=1.0 # bitmap enhancing with unsharp mask
COLORS=16777216    # default num. of colors, use 16, 256,...

## Global variables
PAGE_START=1       
PAGE_STOP=1
WIDTH=0
HEIGHT=0
PAGE=0
ALL=0

## Help
print_help(){
	echo "PDF2PNG"
	echo "Usage: " `basename $0` [OPTIONS] FILE
	echo
	echo "Options:"	
	echo "	-h   ... this help"
	echo "	-a   ... convert all pages"
	echo "	-f N ... from page number N"
	echo "	-t N ... to page number N"
	echo "	-p N ... only page N (default N = 1)"
	echo "	-d N ... resolution in dpi (default N = $DEFAULT_DPI)"
	echo "	-x N ... bitmap  width in pixel (if y not given, save aspect ratio)"
	echo "	-y N ... bitmap height in pixel (if x not given, save aspect ratio)"
	echo "	-u N ... unsharp radius (default N = $UNSHARP_RADIUS)"
    echo "	-c N ... number of colors (default N = $COLORS)"
	echo 
}

# definition of color escape sequences for ANSI terminal
RED="\033[01;31m"
GREEN="\033[01;32m"

color_echo(){
# 1 -- color escape sequncy
# 2 -- string
# \033[0m -- reset color settings
  echo -e "$1 $2 \033[0m"
}


test_return(){
# 1 -- return code $?
# 2 -- description of operation
echo -e -n $2:
COL=40
# move cursor to column $COL 
echo -en "\033[${COL}G"
if [ $1 -eq 0 ] 
then
	color_echo $GREEN OK
else
	color_echo $RED FALSE
	exit 1
fi
}

if [ $# -lt 1 ] # print help
then
    print_help
    exit 1
fi

## Process CLI

while getopts "af:t:d:hx:y:w:u:p:c:" name
do
	case $name in
	a)	ALL=1
	;;
	f)	PAGE_START=$OPTARG
	;;
	t)	PAGE_STOP=$OPTARG
	;;
	p)	PAGE=$OPTARG
	;;
	d) 	DEFAULT_DPI=$OPTARG
	;;
	x)	WIDTH=$OPTARG
	;;
	y)	HEIGHT=$OPTARG
	;;
	u)	UNSHARP_RADIUS=$OPTARG
	;;
	c)	COLORS=$OPTARG
	;;
	h)	# help
		print_help
		exit 0
	;;
	\?)
		echo "Invalid option!"
		exit 1
	;;
	esac
done


shift $(($OPTIND - 1));

INPUT=$1

OUTPUT=$(basename $INPUT .pdf)

echo "Input file: $INPUT"

## Check all input parameters and set options

if [ -z "$INPUT" ]
then
	echo "No input file!"
	exit 1
fi

# All pages processing

MAX_PAGE=`pdfinfo "$INPUT" | grep Pages | awk '{ print $2}'`

if [ -z $MAX_PAGE ]; then exit; fi

if [ $ALL -eq 1 ]
then
	PAGE_START=1
	PAGE_STOP=$MAX_PAGE
fi

# Page range processing

if [ $PAGE_START -gt 1 ]
then
	if [ $PAGE_STOP -eq 1 ]
	then
		PAGE_STOP=$MAX_PAGE
	fi
fi

if [ $PAGE_STOP -gt 1 ]
then
	if [ $PAGE_START -eq 1 ]
	then
		PAGE_START=1
	fi
fi

if [ $PAGE -gt 0 ]
then
	PAGE_START=$PAGE
	PAGE_STOP=$PAGE
fi

# Image size

if [ $WIDTH -gt 0 ]
then
	if [ $HEIGHT -gt 0 ]
	then
		RESIZE=${WIDTH}x${HEIGHT}\!
	else
		RESIZE=${WIDTH}x
	fi
	DEFAULT_DPI=0
fi

if [ $HEIGHT -gt 0 ]
then
	if [ $WIDTH -gt 0 ]
	then
		RESIZE=${WIDTH}x${HEIGHT}\!
	else
		RESIZE=x${HEIGHT}
	fi
	DEFAULT_DPI=0
fi

if [ $DEFAULT_DPI -gt 0 ]
then
	RESIZE=$(((DEFAULT_DPI*100)/GS_DPI))"%"
fi

echo $RESIZE
#exit

##############################################################################

echo "Rasterizing pages from $PAGE_START to $PAGE_STOP."
echo "Output image size: $RESIZE"

j=1
N=$((PAGE_STOP-PAGE_START+1))
for i in `seq $PAGE_START $PAGE_STOP`
do
	i=`printf %04u $i`
	echo "Processing page no. $i ($j/$N)"
	TMP=/tmp/tmp-${i}.png
	gs -q -sDEVICE=png16m -dBATCH -dNOPAUSE \
		-dFirstPage=$i -dLastPage=$i -r$GS_DPI \
		 -sOutputFile=$TMP "$INPUT" > /dev/null 2>&1

	test_return $? "GhostScript conversion"	

	convert \
		-unsharp "$UNSHARP_RADIUS" \
		-resize "${RESIZE}" \
		-colors $COLORS +dither \
		$TMP png:${OUTPUT}_${i}.png 

	rm -f $TMP
	: $((j++))
done