ยปCore Development>Code coverage>Lib/test/test_imgfile.py

Python code coverage for Lib/test/test_imgfile.py

#countcontent
1n/a#! /usr/bin/env python
2n/a
3n/a"""Simple test script for imgfile.c
4n/a Roger E. Masse
51"""
6n/a
71from test.test_support import verbose, unlink, findfile, import_module
8n/a
91imgfile = import_module('imgfile', deprecated=True)
100import uu
11n/a
12n/a
130def testimage(name):
14n/a """Run through the imgfile's battery of possible methods
15n/a on the image passed in name.
16n/a """
17n/a
180 import sys
190 import os
20n/a
210 outputfile = '/tmp/deleteme'
22n/a
23n/a # try opening the name directly
240 try:
25n/a # This function returns a tuple (x, y, z) where x and y are the size
26n/a # of the image in pixels and z is the number of bytes per pixel. Only
27n/a # 3 byte RGB pixels and 1 byte greyscale pixels are supported.
280 sizes = imgfile.getsizes(name)
290 except imgfile.error:
30n/a # get a more qualified path component of the script...
310 if __name__ == '__main__':
320 ourname = sys.argv[0]
33n/a else: # ...or the full path of the module
340 ourname = sys.modules[__name__].__file__
35n/a
360 parts = ourname.split(os.sep)
370 parts[-1] = name
380 name = os.sep.join(parts)
390 sizes = imgfile.getsizes(name)
400 if verbose:
410 print 'Opening test image: %s, sizes: %s' % (name, str(sizes))
42n/a # This function reads and decodes the image on the specified file,
43n/a # and returns it as a python string. The string has either 1 byte
44n/a # greyscale pixels or 4 byte RGBA pixels. The bottom left pixel
45n/a # is the first in the string. This format is suitable to pass
46n/a # to gl.lrectwrite, for instance.
470 image = imgfile.read(name)
48n/a
49n/a # This function writes the RGB or greyscale data in data to
50n/a # image file file. x and y give the size of the image, z is
51n/a # 1 for 1 byte greyscale images or 3 for RGB images (which
52n/a # are stored as 4 byte values of which only the lower three
53n/a # bytes are used). These are the formats returned by gl.lrectread.
540 if verbose:
550 print 'Writing output file'
560 imgfile.write (outputfile, image, sizes[0], sizes[1], sizes[2])
57n/a
58n/a
590 if verbose:
600 print 'Opening scaled test image: %s, sizes: %s' % (name, str(sizes))
61n/a # This function is identical to read but it returns an image that
62n/a # is scaled to the given x and y sizes. If the filter and blur
63n/a # parameters are omitted scaling is done by simply dropping
64n/a # or duplicating pixels, so the result will be less than perfect,
65n/a # especially for computer-generated images. Alternatively,
66n/a # you can specify a filter to use to smoothen the image after
67n/a # scaling. The filter forms supported are 'impulse', 'box',
68n/a # 'triangle', 'quadratic' and 'gaussian'. If a filter is
69n/a # specified blur is an optional parameter specifying the
70n/a # blurriness of the filter. It defaults to 1.0. readscaled
71n/a # makes no attempt to keep the aspect ratio correct, so that
72n/a # is the users' responsibility.
730 if verbose:
740 print 'Filtering with "impulse"'
750 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'impulse', 2.0)
76n/a
77n/a # This function sets a global flag which defines whether the
78n/a # scan lines of the image are read or written from bottom to
79n/a # top (flag is zero, compatible with SGI GL) or from top to
80n/a # bottom(flag is one, compatible with X). The default is zero.
810 if verbose:
820 print 'Switching to X compatibility'
830 imgfile.ttob (1)
84n/a
850 if verbose:
860 print 'Filtering with "triangle"'
870 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'triangle', 3.0)
880 if verbose:
890 print 'Switching back to SGI compatibility'
900 imgfile.ttob (0)
91n/a
920 if verbose: print 'Filtering with "quadratic"'
930 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'quadratic')
940 if verbose: print 'Filtering with "gaussian"'
950 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'gaussian', 1.0)
96n/a
970 if verbose:
980 print 'Writing output file'
990 imgfile.write (outputfile, simage, sizes[0]/2, sizes[1]/2, sizes[2])
100n/a
1010 os.unlink(outputfile)
102n/a
103n/a
1040def test_main():
105n/a
1060 uu.decode(findfile('testrgb.uue'), 'test.rgb')
1070 uu.decode(findfile('greyrgb.uue'), 'greytest.rgb')
108n/a
109n/a # Test a 3 byte color image
1100 testimage('test.rgb')
111n/a
112n/a # Test a 1 byte greyscale image
1130 testimage('greytest.rgb')
114n/a
1150 unlink('test.rgb')
1160 unlink('greytest.rgb')
117n/a
1180if __name__ == '__main__':
1190 test_main()