| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# anytopnm - attempt to convert an unknown type of image file to a P?M file. |
| 4 |
# |
| 5 |
# Copyright (C) 1991 by Jef Poskanzer. |
| 6 |
# |
| 7 |
# Permission to use, copy, modify, and distribute this software and its |
| 8 |
# documentation for any purpose and without fee is hereby granted, provided |
| 9 |
# that the above copyright notice appear in all copies and that both that |
| 10 |
# copyright notice and this permission notice appear in supporting |
| 11 |
# documentation. This software is provided "as is" without express or |
| 12 |
# implied warranty. |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
putInputIntoTempfile() { |
| 17 |
|
| 18 |
# $1 is the input file specification |
| 19 |
# $2 is the temporary file name |
| 20 |
|
| 21 |
rm -f "$2" |
| 22 |
if [ "$1" = "-" ] ; then |
| 23 |
cat > "$2" |
| 24 |
else |
| 25 |
# We'd like to do a -e here to test the existence of the filesystem |
| 26 |
# object per se, and thus give a more specific error message. But |
| 27 |
# some systems don't have -e. |
| 28 |
if [ ! -f "$1" ] ; then |
| 29 |
echo "$progname: '$1' does not exist or is not a file" 1>&2 |
| 30 |
exit 1 |
| 31 |
fi |
| 32 |
|
| 33 |
if [ ! -r "$1" ] ; then |
| 34 |
echo "$progname: '$1' is not readable" 1>&2 |
| 35 |
exit 1 |
| 36 |
fi |
| 37 |
|
| 38 |
if [ -z "$1" ] ; then |
| 39 |
echo "$progname: '$1' is empty" 1>&2 |
| 40 |
exit 1 |
| 41 |
fi |
| 42 |
|
| 43 |
cat < "$1" > "$2" |
| 44 |
fi |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
setMimeType() { |
| 50 |
#------------------------------------------------------------------------------ |
| 51 |
# Set variable 'mimeType' to the mime type string (e.g. "application/binary") |
| 52 |
# |
| 53 |
# If we can't tell, set it to "unknown". |
| 54 |
#------------------------------------------------------------------------------ |
| 55 |
# $1 is the file name |
| 56 |
|
| 57 |
# Old versions of 'file' cannot tell us the mime type; they lack any option |
| 58 |
# to do so. |
| 59 |
# |
| 60 |
# Newer ones have a --mime option for that. |
| 61 |
# |
| 62 |
# Still newer ones (ca 2008) have a --mime option, but it does something |
| 63 |
# different - it prints the mime type plus the mime encoding. And they |
| 64 |
# have --mime-type to print just the mime type. |
| 65 |
|
| 66 |
file --mime-type /dev/null >/dev/null 2>/dev/null |
| 67 |
if [ $? -eq 0 ]; then |
| 68 |
# Now that we know the --mime-type option exists, use it. |
| 69 |
mimeType=`file --mime-type "$1" | cut -d: -f2- | cut -c2-` |
| 70 |
else |
| 71 |
# file --mime-type failed; we don't know why, but we assume it's |
| 72 |
# because it is an older 'file' program that doesn't have a --mime-type |
| 73 |
# option. |
| 74 |
file --mime /dev/null >/dev/null 2>/dev/null |
| 75 |
if [ $? -eq 0 ]; then |
| 76 |
# Now that we know the --mime option exists, use it. |
| 77 |
mimeType=`file --mime "$1" | cut -d: -f2- | cut -c2-` |
| 78 |
else |
| 79 |
# file --mime failed; we don't know why, but we assume it's because |
| 80 |
# it is an older 'file' program that doesn't have a --mime option. |
| 81 |
mimeType="unknown" |
| 82 |
fi |
| 83 |
fi |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
computeTypeFromMimeType () { |
| 89 |
|
| 90 |
# $1 is the mime type string |
| 91 |
|
| 92 |
case "$1" in |
| 93 |
|
| 94 |
image/jpeg ) |
| 95 |
filetype="jfif" |
| 96 |
;; |
| 97 |
image/gif ) |
| 98 |
filetype="gif" |
| 99 |
;; |
| 100 |
image/tiff ) |
| 101 |
filetype="tiff" |
| 102 |
;; |
| 103 |
image/bmp ) |
| 104 |
filetype="bmp" |
| 105 |
;; |
| 106 |
image/x-ms-bmp ) |
| 107 |
filetype="bmp" |
| 108 |
;; |
| 109 |
image/png ) |
| 110 |
filetype="png" |
| 111 |
;; |
| 112 |
image/x-portable-bitmap | image/x-portable-pixmap | \ |
| 113 |
image/x-portable-greymap) |
| 114 |
filetype="pnm" |
| 115 |
;; |
| 116 |
image/x-xpm ) |
| 117 |
filetype="xpm" |
| 118 |
;; |
| 119 |
* ) |
| 120 |
filetype=unknown |
| 121 |
;; |
| 122 |
esac |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
computeTypeFromTypeDescription () { |
| 128 |
|
| 129 |
# $1 is the full description from 'file' of the file type |
| 130 |
|
| 131 |
case "$1" in |
| 132 |
|
| 133 |
*PBM* | *PGM* | *PPM* | *Netpbm*PAM*) |
| 134 |
filetype=pnm |
| 135 |
;; |
| 136 |
|
| 137 |
*JPEG* | *JFIF* ) |
| 138 |
filetype=jfif |
| 139 |
;; |
| 140 |
|
| 141 |
*PNG* ) |
| 142 |
filetype=png |
| 143 |
;; |
| 144 |
|
| 145 |
*GIF* ) |
| 146 |
filetype=gif |
| 147 |
;; |
| 148 |
|
| 149 |
*TIFF* ) |
| 150 |
filetype=tiff |
| 151 |
;; |
| 152 |
|
| 153 |
# We have seen "PC bitmap" and "PC bitmap data" |
| 154 |
*PC*bitmap* ) |
| 155 |
filetype=bmp |
| 156 |
;; |
| 157 |
|
| 158 |
*uuencoded* ) |
| 159 |
filetype=uuencoded |
| 160 |
;; |
| 161 |
|
| 162 |
*bzip2*compressed*data* ) |
| 163 |
filetype=bzip2 |
| 164 |
;; |
| 165 |
|
| 166 |
*bzip*compressed*data* ) |
| 167 |
filetype=bzip |
| 168 |
;; |
| 169 |
|
| 170 |
*gzip*compressed*data* ) |
| 171 |
filetype=gzip |
| 172 |
;; |
| 173 |
|
| 174 |
*XWD*X*Window*Dump* ) |
| 175 |
filetype=xwd |
| 176 |
;; |
| 177 |
|
| 178 |
*compress* ) |
| 179 |
filetype=compress |
| 180 |
;; |
| 181 |
|
| 182 |
*btoa* ) |
| 183 |
filetype=btoa |
| 184 |
;; |
| 185 |
|
| 186 |
*Sun* | *rasterfile* ) |
| 187 |
filetype=rast |
| 188 |
;; |
| 189 |
|
| 190 |
*IFF*ILBM* ) |
| 191 |
filetype=ilbm |
| 192 |
;; |
| 193 |
|
| 194 |
*Lisp* ) |
| 195 |
filetype=lispm |
| 196 |
;; |
| 197 |
|
| 198 |
*PC*Paintbrush* ) |
| 199 |
filetype=pcx |
| 200 |
;; |
| 201 |
|
| 202 |
*Bennet* ) |
| 203 |
filetype=ybm |
| 204 |
;; |
| 205 |
|
| 206 |
*pixmap*image*text* ) |
| 207 |
filetype=xpm |
| 208 |
;; |
| 209 |
|
| 210 |
# This has to come after all other 'text' files, or you may be |
| 211 |
# disappointed. |
| 212 |
*text* ) |
| 213 |
filetype=text |
| 214 |
;; |
| 215 |
|
| 216 |
*MicroDesign* ) |
| 217 |
filetype=mda |
| 218 |
;; |
| 219 |
|
| 220 |
* ) |
| 221 |
filetype=unknown |
| 222 |
;; |
| 223 |
esac |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
computeTypeFromFilenameExtension () { |
| 229 |
# $1 is the filename extension (".gif", etc.) |
| 230 |
|
| 231 |
case "$1" in |
| 232 |
|
| 233 |
*.pbm | *.pbm.* | *.pgm | *.pgm.* | *.ppm | *.ppm.* ) |
| 234 |
filetype=pnm |
| 235 |
;; |
| 236 |
*.JPEG | *.jpeg | *.jpg | *.JPG ) |
| 237 |
filetype=jfif |
| 238 |
;; |
| 239 |
*.gif | *.gif.* ) |
| 240 |
filetype=gif |
| 241 |
;; |
| 242 |
*.png | *.PNG ) |
| 243 |
filetype=png |
| 244 |
;; |
| 245 |
*.tif | *.tif.* | *.tiff | *.tiff.* ) |
| 246 |
filetype=tiff |
| 247 |
;; |
| 248 |
*.bmp ) |
| 249 |
filetype=bmp |
| 250 |
;; |
| 251 |
*.x | *.x.* | *.xbm | *.xbm.* | *.x10bm | *.x10bm.* | \ |
| 252 |
*.x11bm | *.x11bm.* | *.bitmap | *.bitmap.* ) |
| 253 |
filetype=xbm |
| 254 |
;; |
| 255 |
*.r | *.r.* | *.rast | *.rast.* ) |
| 256 |
filetype=rast |
| 257 |
;; |
| 258 |
*.mac | *.mac.* | *.macp | *.macp.* ) |
| 259 |
filetype=macp |
| 260 |
;; |
| 261 |
*.g3 | *.g3.* | *.fax | *.fax.* ) |
| 262 |
filetype=g3 |
| 263 |
;; |
| 264 |
*.xwd | *.xwd.* | *.x10wd | *.x10wd.* \ |
| 265 |
| *.x11wd | *.x11wd.* ) |
| 266 |
filetype=xwd |
| 267 |
;; |
| 268 |
*.brush | *.brush.* ) |
| 269 |
filetype=brush |
| 270 |
;; |
| 271 |
*.img | *.img.* ) |
| 272 |
filetype=gem |
| 273 |
;; |
| 274 |
*.pcx | *.pcx.* ) |
| 275 |
filetype=pcx |
| 276 |
;; |
| 277 |
*.pic | *.pic.* | *.pict | *.pict.* | *.pict2 | *.pict2.* ) |
| 278 |
filetype=pict |
| 279 |
;; |
| 280 |
*.fs | *.fs.* | *.face | *.face.* ) |
| 281 |
filetype=fs |
| 282 |
;; |
| 283 |
*.hips | *.hips.* ) |
| 284 |
filetype=hips |
| 285 |
;; |
| 286 |
*.fits | *.fits.* ) |
| 287 |
filetype=fits |
| 288 |
;; |
| 289 |
*.iff | *.iff.* | *.ilbm | *.ilbm.* ) |
| 290 |
filetype=ilbm |
| 291 |
;; |
| 292 |
*.lispm | *.lispm.* ) |
| 293 |
filetype=lispm |
| 294 |
;; |
| 295 |
*.mtv | *.mtv.* ) |
| 296 |
filetype=mtv |
| 297 |
;; |
| 298 |
*.qrt | *.qrt.* ) |
| 299 |
filetype=qrt |
| 300 |
;; |
| 301 |
*.tga | *.tga.* | *.targa | *.targa.* ) |
| 302 |
filetype=tga |
| 303 |
;; |
| 304 |
*.xim | *.xim.* ) |
| 305 |
filetype=xim |
| 306 |
;; |
| 307 |
*.xpm | *.xpm.* | *.xpm2 | *.xpm2.* ) |
| 308 |
filetype=xpm |
| 309 |
;; |
| 310 |
*.pi1 | *.pi1.* ) |
| 311 |
filetype=pi1 |
| 312 |
;; |
| 313 |
*.pi3 | *.pi3.* ) |
| 314 |
filetype=pi3 |
| 315 |
;; |
| 316 |
*.spu | *.spu.* ) |
| 317 |
filetype=spu |
| 318 |
;; |
| 319 |
*.spc | *.spc.* ) |
| 320 |
filetype=spc |
| 321 |
;; |
| 322 |
*.ybm | *.ybm.* | *.face | *.face.* ) |
| 323 |
filetype=ybm |
| 324 |
;; |
| 325 |
*.mda | *.mdp ) |
| 326 |
filetype=mda |
| 327 |
;; |
| 328 |
* ) |
| 329 |
filetype=unknown |
| 330 |
;; |
| 331 |
esac |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
determineType () { |
| 337 |
|
| 338 |
# $1 is the name of the file that contains the subject file's contents |
| 339 |
# $2 is the mime type or "unknown" |
| 340 |
# $3 is the type description from 'file' |
| 341 |
|
| 342 |
if [ "$2" = "unknown" ]; then |
| 343 |
filetype="unknown" |
| 344 |
else |
| 345 |
computeTypeFromMimeType "$2" |
| 346 |
fi |
| 347 |
|
| 348 |
if [ "$filetype" = "unknown" ]; then |
| 349 |
computeTypeFromTypeDescription "$3" |
| 350 |
fi |
| 351 |
|
| 352 |
if [ "$filetype" = "unknown" ]; then |
| 353 |
computeTypeFromFilenameExtension "$4" |
| 354 |
fi |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
convertIt () { |
| 360 |
# Based on the file type computed, do the conversion |
| 361 |
|
| 362 |
# $1 is the input file name |
| 363 |
# $2 is our file type code |
| 364 |
|
| 365 |
case "$2" in |
| 366 |
|
| 367 |
pnm ) |
| 368 |
cat "$file" |
| 369 |
;; |
| 370 |
|
| 371 |
uuencoded ) |
| 372 |
newfile="$tempdir/atn.decode.$$" |
| 373 |
rm -f "$newfile" |
| 374 |
(echo begin 600 $newfile; sed 1d < "$file") | uudecode |
| 375 |
anytopnm "$newfile" |
| 376 |
;; |
| 377 |
|
| 378 |
bzip2 ) |
| 379 |
bzip2 -dk < "$file" | anytopnm - |
| 380 |
;; |
| 381 |
|
| 382 |
bzip ) |
| 383 |
bzip -dk < "$file" | anytopnm - |
| 384 |
;; |
| 385 |
|
| 386 |
gzip ) |
| 387 |
gzip --decompress --to-stdout < "$file" | anytopnm - |
| 388 |
;; |
| 389 |
|
| 390 |
compress ) |
| 391 |
uncompress -c < "$file" | anytopnm - |
| 392 |
;; |
| 393 |
|
| 394 |
btoa ) |
| 395 |
atob < "$file" | anytopnm - |
| 396 |
;; |
| 397 |
|
| 398 |
rast ) |
| 399 |
rasttopnm "$file" |
| 400 |
;; |
| 401 |
|
| 402 |
gif ) |
| 403 |
giftopnm -image=all "$file" |
| 404 |
;; |
| 405 |
|
| 406 |
tiff ) |
| 407 |
tifftopnm "$file" |
| 408 |
;; |
| 409 |
|
| 410 |
ilbm ) |
| 411 |
ilbmtoppm "$file" |
| 412 |
;; |
| 413 |
|
| 414 |
lispm ) |
| 415 |
lispmtopgm "$file" |
| 416 |
;; |
| 417 |
|
| 418 |
pcx ) |
| 419 |
pcxtoppm "$file" |
| 420 |
;; |
| 421 |
|
| 422 |
ybm ) |
| 423 |
ybmtopbm "$file" |
| 424 |
;; |
| 425 |
|
| 426 |
xpm ) |
| 427 |
xpmtoppm < "$file" |
| 428 |
;; |
| 429 |
|
| 430 |
# This has to come after all other 'text' files, or you may be |
| 431 |
# disappointed. |
| 432 |
text ) |
| 433 |
pbmtext -builtin fixed < "$file" |
| 434 |
;; |
| 435 |
|
| 436 |
jfif ) |
| 437 |
jpegtopnm "$file" |
| 438 |
;; |
| 439 |
|
| 440 |
png ) |
| 441 |
pngtopam "$file" |
| 442 |
;; |
| 443 |
|
| 444 |
mda ) |
| 445 |
mdatopbm -d -- "$file" |
| 446 |
;; |
| 447 |
|
| 448 |
bmp ) |
| 449 |
bmptoppm "$file" |
| 450 |
;; |
| 451 |
|
| 452 |
xbm ) |
| 453 |
xbmtopbm "$file" |
| 454 |
;; |
| 455 |
macp ) |
| 456 |
macptopbm "$file" |
| 457 |
;; |
| 458 |
g3 ) |
| 459 |
g3topbm "$file" |
| 460 |
;; |
| 461 |
xwd ) |
| 462 |
xwdtopnm "$file" |
| 463 |
;; |
| 464 |
brush ) |
| 465 |
brushtopbm "$file" |
| 466 |
;; |
| 467 |
img ) |
| 468 |
gemtopbm "$file" |
| 469 |
;; |
| 470 |
pcx ) |
| 471 |
pcxtoppm "$file" |
| 472 |
;; |
| 473 |
pict ) |
| 474 |
picttoppm "$file" |
| 475 |
;; |
| 476 |
fs ) |
| 477 |
fstopgm "$file" |
| 478 |
;; |
| 479 |
hips ) |
| 480 |
hipstopgm "$file" |
| 481 |
;; |
| 482 |
fits ) |
| 483 |
fitstopnm "$file" |
| 484 |
;; |
| 485 |
mtv ) |
| 486 |
mtvtoppm "$file" |
| 487 |
;; |
| 488 |
qrt ) |
| 489 |
qrttoppm "$file" |
| 490 |
;; |
| 491 |
tga ) |
| 492 |
tgatoppm "$file" |
| 493 |
;; |
| 494 |
xim ) |
| 495 |
ximtoppm "$file" |
| 496 |
;; |
| 497 |
pi1 ) |
| 498 |
pi1toppm "$file" |
| 499 |
;; |
| 500 |
pi3 ) |
| 501 |
pi3topbm "$file" |
| 502 |
;; |
| 503 |
spu ) |
| 504 |
sputoppm "$file" |
| 505 |
;; |
| 506 |
spc ) |
| 507 |
spctoppm "$file" |
| 508 |
;; |
| 509 |
* ) |
| 510 |
echo "$progname: INTERNAL ERROR. Illegal value of filetype variable" |
| 511 |
exit 1 |
| 512 |
;; |
| 513 |
|
| 514 |
esac |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
############################################################################### |
| 520 |
# MAINLINE |
| 521 |
############################################################################### |
| 522 |
|
| 523 |
progname=$0 |
| 524 |
|
| 525 |
if [ $# -gt 1 ] ; then |
| 526 |
echo "Too many arguments: $#. The only valid argument is the" \ |
| 527 |
"input file name." 1>&2 |
| 528 |
exit 1 |
| 529 |
elif [ $# -eq 1 ] ; then |
| 530 |
if [ "$1" = "--version" -o "$1" = "-version" ] ; then |
| 531 |
pamtopnm --version ; exit $? |
| 532 |
else |
| 533 |
inputFile="$1" |
| 534 |
fi |
| 535 |
else |
| 536 |
inputFile="-" |
| 537 |
|
| 538 |
fi |
| 539 |
|
| 540 |
tempdir=$(mktemp -d "${TMPDIR:-/tmp}/anytopnm.XXXXXXXX") || |
| 541 |
{ echo "Could not create temporary file. Exiting." 1>&2; exit 1; } |
| 542 |
trap 'rm -rf $tempdir' 0 |
| 543 |
|
| 544 |
# Take out all spaces |
| 545 |
# Find the filename extension for last-ditch efforts later |
| 546 |
|
| 547 |
# We used to do this, but it doesn't work with all Awks. The sed below |
| 548 |
# is universal. 2005.11.02. |
| 549 |
#fileExtension=`echo "$inputFile" | \ |
| 550 |
# $AWK '{gsub(" ","");gsub(".*\\\\.",".");print}'` |
| 551 |
|
| 552 |
fileExtension=`echo "$inputFile" | sed 's/ //;s/.*\././g'` |
| 553 |
|
| 554 |
file="$tempdir/atn.stdin.$$" |
| 555 |
|
| 556 |
putInputIntoTempfile "$inputFile" "$file" |
| 557 |
|
| 558 |
setMimeType "$file" |
| 559 |
|
| 560 |
typeDescription=`file "$file" | cut -d: -f2- | cut -c2-` |
| 561 |
|
| 562 |
determineType "$file" "$mimeType" "$typeDescription" "$fileExtension" |
| 563 |
|
| 564 |
if [ "$filetype" = "unknown" ]; then |
| 565 |
echo "$progname: unknown file type. " \ |
| 566 |
"'file' says mime type is '$mimeType', " 1>&2 |
| 567 |
echo "type description is '$typeDescription'" 1>&2 |
| 568 |
exit 1 |
| 569 |
fi |
| 570 |
|
| 571 |
convertIt $file $filetype |
| 572 |
|
| 573 |
exit 0 |