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