annotate Lib/test/test_imgfile.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents e4bb6f174b00
children
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
47030
e4bb6f174b00 Deprecate imgfile for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 45631
diff changeset
7 from test.test_support import verbose, unlink, findfile, import_module
5328
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4807
diff changeset
8
47030
e4bb6f174b00 Deprecate imgfile for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 45631
diff changeset
9 imgfile = import_module('imgfile', deprecated=True)
e4bb6f174b00 Deprecate imgfile for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 45631
diff changeset
10 import uu
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
11
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 8127
diff changeset
12
4806
a3c477259505 Minor semantic cleanup.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4804
diff changeset
13 def testimage(name):
4807
8d2c8b080401 More and better comments.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4806
diff changeset
14 """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
15 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
16 """
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
17
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
18 import sys
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
19 import os
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
20
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
21 outputfile = '/tmp/deleteme'
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
22
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
23 # 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
24 try:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
25 # This function returns a tuple (x, y, z) where x and y are the size
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
26 # of the image in pixels and z is the number of bytes per pixel. Only
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
27 # 3 byte RGB pixels and 1 byte greyscale pixels are supported.
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
28 sizes = imgfile.getsizes(name)
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
29 except imgfile.error:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
30 # get a more qualified path component of the script...
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
31 if __name__ == '__main__':
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
32 ourname = sys.argv[0]
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
33 else: # ...or the full path of the module
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
34 ourname = sys.modules[__name__].__file__
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
35
16517
eee67a84e5fa String method conversion.
Eric S. Raymond <esr@thyrsus.com>
parents: 15966
diff changeset
36 parts = ourname.split(os.sep)
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
37 parts[-1] = name
16533
c48af39cb292 joinfields -> join.
Eric S. Raymond <esr@thyrsus.com>
parents: 16517
diff changeset
38 name = os.sep.join(parts)
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
39 sizes = imgfile.getsizes(name)
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
40 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
41 print 'Opening test image: %s, sizes: %s' % (name, str(sizes))
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
42 # 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
43 # 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
44 # 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
45 # is the first in the string. This format is suitable to pass
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 8127
diff changeset
46 # to gl.lrectwrite, for instance.
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
47 image = imgfile.read(name)
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 8127
diff changeset
48
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
49 # 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
50 # 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
51 # 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
52 # are stored as 4 byte values of which only the lower three
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 8127
diff changeset
53 # bytes are used). These are the formats returned by gl.lrectread.
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
54 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
55 print 'Writing output file'
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 8127
diff changeset
56 imgfile.write (outputfile, image, sizes[0], sizes[1], sizes[2])
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 8127
diff changeset
57
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
58
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
59 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
60 print 'Opening scaled test image: %s, sizes: %s' % (name, str(sizes))
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
61 # 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
62 # 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
63 # 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
64 # 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
65 # 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
66 # 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
67 # 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
68 # '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
69 # 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
70 # 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
71 # 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
72 # is the users' responsibility.
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
73 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
74 print 'Filtering with "impulse"'
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
75 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
76
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
77 # 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
78 # 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
79 # top (flag is zero, compatible with SGI GL) or from top to
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 8127
diff changeset
80 # bottom(flag is one, compatible with X). The default is zero.
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
81 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
82 print 'Switching to X compatibility'
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 8127
diff changeset
83 imgfile.ttob (1)
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
84
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
85 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
86 print 'Filtering with "triangle"'
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
87 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
88 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
89 print 'Switching back to SGI compatibility'
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 8127
diff changeset
90 imgfile.ttob (0)
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 8127
diff changeset
91
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
92 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
93 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
94 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
95 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
96
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
97 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
98 print 'Writing output file'
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 8127
diff changeset
99 imgfile.write (outputfile, simage, sizes[0]/2, sizes[1]/2, sizes[2])
4804
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
100
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
101 os.unlink(outputfile)
a2c427f9db02 Test script for the SGI imgfile module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
102
45631
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
103
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
104 def test_main():
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
105
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
106 uu.decode(findfile('testrgb.uue'), 'test.rgb')
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
107 uu.decode(findfile('greyrgb.uue'), 'greytest.rgb')
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
108
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
109 # Test a 3 byte color image
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
110 testimage('test.rgb')
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
111
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
112 # Test a 1 byte greyscale image
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
113 testimage('greytest.rgb')
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
114
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
115 unlink('test.rgb')
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
116 unlink('greytest.rgb')
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
117
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
118 if __name__ == '__main__':
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
119 test_main()