blob: 576a51c6ab227148b87a3bbbbed05e0ccad5e16f [file] [log] [blame]
Roger E. Masse4455cd81996-12-20 21:52:29 +00001#! /usr/bin/env python
Guido van Rossum7aa9fc51997-04-16 00:30:45 +00002
Roger E. Masse4455cd81996-12-20 21:52:29 +00003"""Simple test script for imgfile.c
4 Roger E. Masse
5"""
Roger E. Masse033d91b1996-12-20 21:41:50 +00006
Guido van Rossum7aa9fc51997-04-16 00:30:45 +00007from test_support import verbose, unlink
Roger E. Masse033d91b1996-12-20 21:41:50 +00008
Guido van Rossum7aa9fc51997-04-16 00:30:45 +00009import imgfile, uu, os
10
11
12def main():
13
14 uu.decode(findfile('testrgb.uue'), 'test.rgb')
15 uu.decode(findfile('greyrgb.uue'), 'greytest.rgb')
Roger E. Masse033d91b1996-12-20 21:41:50 +000016
Roger E. Masse4455cd81996-12-20 21:52:29 +000017 # Test a 3 byte color image
Roger E. Masse3507f501996-12-20 21:47:58 +000018 testimage('test.rgb')
Roger E. Masse4455cd81996-12-20 21:52:29 +000019
20 # Test a 1 byte greyscale image
Roger E. Masse3507f501996-12-20 21:47:58 +000021 testimage('greytest.rgb')
Roger E. Masse033d91b1996-12-20 21:41:50 +000022
Guido van Rossum7aa9fc51997-04-16 00:30:45 +000023 unlink('test.rgb')
24 unlink('greytest.rgb')
25
26def findfile(file):
27 if os.path.isabs(file): return file
28 import sys
29 for dn in sys.path:
30 fn = os.path.join(dn, file)
31 if os.path.exists(fn): return fn
32 return file
Roger E. Masse033d91b1996-12-20 21:41:50 +000033
Roger E. Masse3507f501996-12-20 21:47:58 +000034def testimage(name):
Roger E. Masse4455cd81996-12-20 21:52:29 +000035 """Run through the imgfile's battery of possible methods
36 on the image passed in name.
Roger E. Masse033d91b1996-12-20 21:41:50 +000037 """
38
39 import sys
40 import os
41 import string
42
43 outputfile = '/tmp/deleteme'
44
45 # try opening the name directly
46 try:
Roger E. Masse4455cd81996-12-20 21:52:29 +000047 # This function returns a tuple (x, y, z) where x and y are the size
48 # of the image in pixels and z is the number of bytes per pixel. Only
Roger E. Masse033d91b1996-12-20 21:41:50 +000049 # 3 byte RGB pixels and 1 byte greyscale pixels are supported.
50 sizes = imgfile.getsizes(name)
51 except imgfile.error:
52 # get a more qualified path component of the script...
53 if __name__ == '__main__':
54 ourname = sys.argv[0]
55 else: # ...or the full path of the module
56 ourname = sys.modules[__name__].__file__
57
58 parts = string.splitfields(ourname, os.sep)
59 parts[-1] = name
60 name = string.joinfields(parts, os.sep)
61 sizes = imgfile.getsizes(name)
62 if verbose:
63 print 'Opening test image: %s, sizes: %s' % (name, str(sizes))
64 # This function reads and decodes the image on the specified file,
65 # and returns it as a python string. The string has either 1 byte
66 # greyscale pixels or 4 byte RGBA pixels. The bottom left pixel
67 # is the first in the string. This format is suitable to pass
68 # to gl.lrectwrite, for instance.
69 image = imgfile.read(name)
70
71 # This function writes the RGB or greyscale data in data to
72 # image file file. x and y give the size of the image, z is
73 # 1 for 1 byte greyscale images or 3 for RGB images (which
74 # are stored as 4 byte values of which only the lower three
75 # bytes are used). These are the formats returned by gl.lrectread.
76 if verbose:
77 print 'Writing output file'
78 imgfile.write (outputfile, image, sizes[0], sizes[1], sizes[2])
79
80
81 if verbose:
82 print 'Opening scaled test image: %s, sizes: %s' % (name, str(sizes))
83 # This function is identical to read but it returns an image that
84 # is scaled to the given x and y sizes. If the filter and blur
85 # parameters are omitted scaling is done by simply dropping
86 # or duplicating pixels, so the result will be less than perfect,
87 # especially for computer-generated images. Alternatively,
88 # you can specify a filter to use to smoothen the image after
89 # scaling. The filter forms supported are 'impulse', 'box',
90 # 'triangle', 'quadratic' and 'gaussian'. If a filter is
91 # specified blur is an optional parameter specifying the
92 # blurriness of the filter. It defaults to 1.0. readscaled
93 # makes no attempt to keep the aspect ratio correct, so that
94 # is the users' responsibility.
95 if verbose:
96 print 'Filtering with "impulse"'
97 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'impulse', 2.0)
98
99 # This function sets a global flag which defines whether the
100 # scan lines of the image are read or written from bottom to
101 # top (flag is zero, compatible with SGI GL) or from top to
102 # bottom(flag is one, compatible with X). The default is zero.
103 if verbose:
104 print 'Switching to X compatibility'
105 imgfile.ttob (1)
106
107 if verbose:
108 print 'Filtering with "triangle"'
109 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'triangle', 3.0)
110 if verbose:
111 print 'Switching back to SGI compatibility'
112 imgfile.ttob (0)
113
114 if verbose: print 'Filtering with "quadratic"'
115 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'quadratic')
116 if verbose: print 'Filtering with "gaussian"'
117 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'gaussian', 1.0)
118
119 if verbose:
120 print 'Writing output file'
121 imgfile.write (outputfile, simage, sizes[0]/2, sizes[1]/2, sizes[2])
122
123 os.unlink(outputfile)
124
125main()