summaryrefslogtreecommitdiffstats
path: root/Lib/plat-irix5/torgb.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1992-10-18 17:09:59 (GMT)
committerGuido van Rossum <guido@python.org>1992-10-18 17:09:59 (GMT)
commit2db91358def94cf8081f27b736988320d14eba39 (patch)
treef73562d6cfd16adf8723bbe702be55d13e2122f5 /Lib/plat-irix5/torgb.py
parent0cb8e8cfc09dd0919882da968c3e95bf070e257d (diff)
downloadcpython-2db91358def94cf8081f27b736988320d14eba39.zip
cpython-2db91358def94cf8081f27b736988320d14eba39.tar.gz
cpython-2db91358def94cf8081f27b736988320d14eba39.tar.bz2
Misc changes and new modules. whrandom is "objectified". SOCKET.py
is moved to the sgi subdirectory.
Diffstat (limited to 'Lib/plat-irix5/torgb.py')
-rwxr-xr-xLib/plat-irix5/torgb.py91
1 files changed, 91 insertions, 0 deletions
diff --git a/Lib/plat-irix5/torgb.py b/Lib/plat-irix5/torgb.py
new file mode 100755
index 0000000..e46aca3
--- /dev/null
+++ b/Lib/plat-irix5/torgb.py
@@ -0,0 +1,91 @@
+# Convert "arbitrary" image files to rgb files (SGI's image format).
+# Input may be compressed.
+# The uncompressed file type may be PBM, PGM, PPM, GIF, TIFF, or Sun raster.
+# An exception is raised if the file is not of a recognized type.
+# Returned filename is either the input filename or a temporary filename;
+# in the latter case the caller must ensure that it is removed.
+# Other temporary files used are removed by the function.
+
+import os
+import tempfile
+import pipes
+import imghdr
+
+table = {}
+
+t = pipes.Template().init()
+t.append('fromppm $IN $OUT', 'ff')
+table['ppm'] = t
+
+t = pipes.Template().init()
+t.append('pnmtoppm', '--')
+t.append('fromppm $IN $OUT', 'ff')
+table['pnm'] = t
+table['pgm'] = t
+table['pbm'] = t
+
+t = pipes.Template().init()
+t.append('fromgif $IN $OUT', 'ff')
+table['gif'] = t
+
+t = pipes.Template().init()
+t.append('tifftopnm', '--')
+t.append('(PATH=$PATH:/ufs/guido/bin/sgi; exec pnmtoppm)', '--')
+t.append('fromppm $IN $OUT', 'ff')
+table['tiff'] = t
+
+t = pipes.Template().init()
+t.append('rasttopnm', '--')
+t.append('pnmtoppm', '--')
+t.append('fromppm $IN $OUT', 'ff')
+table['rast'] = t
+
+uncompress = pipes.Template().init()
+uncompress.append('uncompress', '--')
+
+
+error = 'torgb.error' # Exception
+
+def torgb(filename):
+ temps = []
+ ret = None
+ try:
+ ret = _torgb(filename, temps)
+ finally:
+ for temp in temps[:]:
+ if temp <> ret:
+ try:
+ os.unlink(temp)
+ except os.error:
+ pass
+ temps.remove(temp)
+ return ret
+
+def _torgb(filename, temps):
+ if filename[-2:] == '.Z':
+ fname = tempfile.mktemp()
+ temps.append(fname)
+ sts = uncompress.copy(filename, fname)
+ if sts:
+ raise error, filename + ': uncompress failed'
+ else:
+ fname = filename
+ try:
+ ftype = imghdr.what(fname)
+ except IOError, msg:
+ if type(msg) == type(()) and len(msg) == 2 and \
+ type(msg[0]) == type(0) and type(msg[1]) == type(''):
+ msg = msg[1]
+ if type(msg) <> type(''):
+ msg = `msg`
+ raise error, filename + ': ' + msg
+ if ftype == 'rgb':
+ return fname
+ if ftype == None or not table.has_key(ftype):
+ raise error, \
+ filename + ': unsupported image file type ' + `ftype`
+ temp = tempfile.mktemp()
+ sts = table[ftype].copy(fname, temp)
+ if sts:
+ raise error, filename + ': conversion to rgb failed'
+ return temp