annotate Lib/test/test_imgfile.py @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents 359e8f815a3a
children aedd2d0dcb31
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4807
8d2c8b080401 More and better comments.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4806
diff changeset
1 #! /usr/bin/env python
5328
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
2
4807
8d2c8b080401 More and better comments.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4806
diff changeset
3 """Simple test script for imgfile.c
8d2c8b080401 More and better comments.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4806
diff changeset
4 Roger E. Masse
8d2c8b080401 More and better comments.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4806
diff changeset
5 """
5328
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
6
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
7 from test_support import verbose, unlink
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
8
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
9 import imgfile, uu, os
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
10
5328
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
11
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
12 def main():
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
13
5328
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
14 uu.decode(findfile('testrgb.uue'), 'test.rgb')
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
15 uu.decode(findfile('greyrgb.uue'), 'greytest.rgb')
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
16
4807
8d2c8b080401 More and better comments.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4806
diff changeset
17 # Test a 3 byte color image
4806
a3c477259505 Minor semantic cleanup.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4804
diff changeset
18 testimage('test.rgb')
4807
8d2c8b080401 More and better comments.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4806
diff changeset
19
8d2c8b080401 More and better comments.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4806
diff changeset
20 # Test a 1 byte greyscale image
4806
a3c477259505 Minor semantic cleanup.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4804
diff changeset
21 testimage('greytest.rgb')
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
22
5328
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
23 unlink('test.rgb')
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
24 unlink('greytest.rgb')
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
25
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
26 def findfile(file):
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
27 if os.path.isabs(file): return file
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
28 import sys
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
29 for dn in sys.path:
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
30 fn = os.path.join(dn, file)
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
31 if os.path.exists(fn): return fn
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
32 return file
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
33
4806
a3c477259505 Minor semantic cleanup.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4804
diff changeset
34 def testimage(name):
4807
8d2c8b080401 More and better comments.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4806
diff changeset
35 """Run through the imgfile's battery of possible methods
8d2c8b080401 More and better comments.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4806
diff changeset
36 on the image passed in name.
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
37 """
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
38
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
39 import sys
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
40 import os
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
41 import string
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
42
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
43 outputfile = '/tmp/deleteme'
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
44
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
45 # try opening the name directly
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
46 try:
4807
8d2c8b080401 More and better comments.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4806
diff changeset
47 # This function returns a tuple (x, y, z) where x and y are the size
8d2c8b080401 More and better comments.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4806
diff changeset
48 # of the image in pixels and z is the number of bytes per pixel. Only
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
49 # 3 byte RGB pixels and 1 byte greyscale pixels are supported.
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
50 sizes = imgfile.getsizes(name)
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
51 except imgfile.error:
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
52 # get a more qualified path component of the script...
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
53 if __name__ == '__main__':
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
54 ourname = sys.argv[0]
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
55 else: # ...or the full path of the module
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
56 ourname = sys.modules[__name__].__file__
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
57
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
58 parts = string.splitfields(ourname, os.sep)
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
59 parts[-1] = name
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
60 name = string.joinfields(parts, os.sep)
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
61 sizes = imgfile.getsizes(name)
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
62 if verbose:
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
63 print 'Opening test image: %s, sizes: %s' % (name, str(sizes))
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
64 # This function reads and decodes the image on the specified file,
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
65 # and returns it as a python string. The string has either 1 byte
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
66 # greyscale pixels or 4 byte RGBA pixels. The bottom left pixel
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
67 # is the first in the string. This format is suitable to pass
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
68 # to gl.lrectwrite, for instance.
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
69 image = imgfile.read(name)
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
70
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
71 # This function writes the RGB or greyscale data in data to
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
72 # image file file. x and y give the size of the image, z is
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
73 # 1 for 1 byte greyscale images or 3 for RGB images (which
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
74 # are stored as 4 byte values of which only the lower three
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
75 # bytes are used). These are the formats returned by gl.lrectread.
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
76 if verbose:
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
77 print 'Writing output file'
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
78 imgfile.write (outputfile, image, sizes[0], sizes[1], sizes[2])
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
79
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
80
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
81 if verbose:
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
82 print 'Opening scaled test image: %s, sizes: %s' % (name, str(sizes))
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
83 # This function is identical to read but it returns an image that
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
84 # is scaled to the given x and y sizes. If the filter and blur
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
85 # parameters are omitted scaling is done by simply dropping
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
86 # or duplicating pixels, so the result will be less than perfect,
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
87 # especially for computer-generated images. Alternatively,
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
88 # you can specify a filter to use to smoothen the image after
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
89 # scaling. The filter forms supported are 'impulse', 'box',
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
90 # 'triangle', 'quadratic' and 'gaussian'. If a filter is
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
91 # specified blur is an optional parameter specifying the
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
92 # blurriness of the filter. It defaults to 1.0. readscaled
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
93 # makes no attempt to keep the aspect ratio correct, so that
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
94 # is the users' responsibility.
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
95 if verbose:
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
96 print 'Filtering with "impulse"'
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
97 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'impulse', 2.0)
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
98
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
99 # This function sets a global flag which defines whether the
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
100 # scan lines of the image are read or written from bottom to
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
101 # top (flag is zero, compatible with SGI GL) or from top to
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
102 # bottom(flag is one, compatible with X). The default is zero.
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
103 if verbose:
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
104 print 'Switching to X compatibility'
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
105 imgfile.ttob (1)
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
106
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
107 if verbose:
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
108 print 'Filtering with "triangle"'
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
109 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'triangle', 3.0)
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
110 if verbose:
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
111 print 'Switching back to SGI compatibility'
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
112 imgfile.ttob (0)
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
113
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
114 if verbose: print 'Filtering with "quadratic"'
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
115 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'quadratic')
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
116 if verbose: print 'Filtering with "gaussian"'
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
117 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'gaussian', 1.0)
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
118
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
119 if verbose:
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
120 print 'Writing output file'
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
121 imgfile.write (outputfile, simage, sizes[0]/2, sizes[1]/2, sizes[2])
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
122
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
123 os.unlink(outputfile)
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
124
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
125 main()