Duy Dinh - 2013-08-20
 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
#! /bin/bash
# converts png to eps
#
# 2003-04-09 Thomas Henlich
#
# requires: pngcheck pngtopnm sed basename
#           pnmtotiff tiff2ps
#           bc (for images with non-square pixels)
#
# Usage:
# png2eps file.png > file.eps

# for maximum compression set this to -lzw
# (requires LZW compression in pnmtotiff and tiff2ps)
COMPRESS=-packbits
#COMPRESS=-lzw

# we need a tmp file for input to tiff2ps
OUTFILE=/tmp/png2eps.tif.$$

# temp file for the tEXt chunk
TXTFILE=/tmp/png2eps.txt.$$

# command to remove files
RM="rm -f"

# get the resolution (if specified) from the PNG file
CHK=`pngcheck -v $1`

# get height in pixels from the PNG file
HEIGHT=`echo $CHK | sed -ne 's/.* [0-9]\+ x \(.*\) image.*//p;'`

# a rowsperstrip parameter of 'height' (or more) is needed to generate one 
# single strip of data in the TIFF file (reduces file size)
ROWSPERSTRIP="-rowsperstrip $HEIGHT"
# ROWSPERSTRIP="rowsperstrip 1000000"

# for images with square pixels, pngcheck outputs a line like
#   chunk pHYs ... 9646x9646 pixels/meter (245 dpi)
# and we can read the resolution in dpi directly
RES=`echo $CHK | sed -ne '/pHYs/s/.*(\(.*\) dpi.*//p;'`

# for images with non-square pixels, pngcheck outputs a line like
#   chunk pHYs ... 9646x4803 pixels/meter
XRES=`echo $CHK | sed -ne '/pHYs/s/.*: \([0-9]*\)x.* pixels\/meter.*//p;'`
YRES=`echo $CHK | sed -ne '/pHYs/s/.*x\([0-9]*\) pixels\/meter.*//p;'`

# for images with no fixed resolution, pngcheck may output a line like
#   chunk pHYs ... 9646x4803 pixels/unit
# we ignore it (like pdfTeX does)

# set the command-line arguments for pnmtotiff which specify the resolution
if [ "$RES" ] ; then
  echo "png2eps: Image with square pixels ($RES dpi)" 1>&2
  RES='-xres '$RES' -yres '$RES
fi
if [ -z "$RES" ] ; then
  if [ "$XRES" ] ; then
    echo "png2eps: Image with non-square pixels" \n          "($XRES""x$YRES pixels/meter)" 1>&2
# we still have to convert pixels/meter -> dpi
    RES='-xres '`echo .0254*$XRES |bc`' -yres '`echo .0254*$YRES |bc`;
  else
# no resolution was specified and so a default of 72 dpi will be used
    echo "png2eps: No resolution specified, using default (72 dpi)" 1>&2;
  fi
fi

# convert to tiff
pngtopnm -verbose -text $TXTFILE $1 | \n    pnmtotiff $ROWSPERSTRIP $RES $COMPRESS -indexbits=1,2,4,8 >$OUTFILE

# get the title of the figure
TITLE="`sed -ne 's/^Title *\(.*\)//p;' <$TXTFILE`"
# set to input file name if unspecified
[ -z "$TITLE" ] && TITLE=`basename $1`

# convert to EPS, replacing the %%Title comment
tiff2ps -2 -e -z $OUTFILE | sed -e "s/^\(%%Title: \).*/$TITLE/;"

# remove temp files
$RM $OUTFILE $TXTFILE
 

Last edit: Duy Dinh 2013-08-20