blob: 39e891769d121b533f0d24842b30367a35ef3f0a [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
Brett Cannon46525372008-05-15 03:32:11 +00007from test.test_support import verbose, unlink, findfile, import_module
Roger E. Masse033d91b1996-12-20 21:41:50 +00008
Brett Cannon46525372008-05-15 03:32:11 +00009imgfile = import_module('imgfile', deprecated=True)
10import uu
Guido van Rossum7aa9fc51997-04-16 00:30:45 +000011
Fred Drake004d5e62000-10-23 17:22:08 +000012
Roger E. Masse3507f501996-12-20 21:47:58 +000013def testimage(name):
Roger E. Masse4455cd81996-12-20 21:52:29 +000014 """Run through the imgfile's battery of possible methods
15 on the image passed in name.
Roger E. Masse033d91b1996-12-20 21:41:50 +000016 """
17
18 import sys
19 import os
Roger E. Masse033d91b1996-12-20 21:41:50 +000020
21 outputfile = '/tmp/deleteme'
22
23 # try opening the name directly
24 try:
Guido van Rossum41360a41998-03-26 19:42:58 +000025 # This function returns a tuple (x, y, z) where x and y are the size
26 # of the image in pixels and z is the number of bytes per pixel. Only
27 # 3 byte RGB pixels and 1 byte greyscale pixels are supported.
28 sizes = imgfile.getsizes(name)
Roger E. Masse033d91b1996-12-20 21:41:50 +000029 except imgfile.error:
Guido van Rossum41360a41998-03-26 19:42:58 +000030 # get a more qualified path component of the script...
31 if __name__ == '__main__':
32 ourname = sys.argv[0]
33 else: # ...or the full path of the module
34 ourname = sys.modules[__name__].__file__
Roger E. Masse033d91b1996-12-20 21:41:50 +000035
Eric S. Raymondfc170b12001-02-09 11:51:27 +000036 parts = ourname.split(os.sep)
Guido van Rossum41360a41998-03-26 19:42:58 +000037 parts[-1] = name
Eric S. Raymonddbbbaf22001-02-09 17:05:53 +000038 name = os.sep.join(parts)
Guido van Rossum41360a41998-03-26 19:42:58 +000039 sizes = imgfile.getsizes(name)
Roger E. Masse033d91b1996-12-20 21:41:50 +000040 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000041 print 'Opening test image: %s, sizes: %s' % (name, str(sizes))
Roger E. Masse033d91b1996-12-20 21:41:50 +000042 # This function reads and decodes the image on the specified file,
43 # and returns it as a python string. The string has either 1 byte
44 # greyscale pixels or 4 byte RGBA pixels. The bottom left pixel
45 # is the first in the string. This format is suitable to pass
Fred Drake004d5e62000-10-23 17:22:08 +000046 # to gl.lrectwrite, for instance.
Roger E. Masse033d91b1996-12-20 21:41:50 +000047 image = imgfile.read(name)
Fred Drake004d5e62000-10-23 17:22:08 +000048
Roger E. Masse033d91b1996-12-20 21:41:50 +000049 # This function writes the RGB or greyscale data in data to
50 # image file file. x and y give the size of the image, z is
51 # 1 for 1 byte greyscale images or 3 for RGB images (which
52 # are stored as 4 byte values of which only the lower three
Fred Drake004d5e62000-10-23 17:22:08 +000053 # bytes are used). These are the formats returned by gl.lrectread.
Roger E. Masse033d91b1996-12-20 21:41:50 +000054 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000055 print 'Writing output file'
Fred Drake004d5e62000-10-23 17:22:08 +000056 imgfile.write (outputfile, image, sizes[0], sizes[1], sizes[2])
57
Roger E. Masse033d91b1996-12-20 21:41:50 +000058
59 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000060 print 'Opening scaled test image: %s, sizes: %s' % (name, str(sizes))
Roger E. Masse033d91b1996-12-20 21:41:50 +000061 # This function is identical to read but it returns an image that
62 # is scaled to the given x and y sizes. If the filter and blur
63 # parameters are omitted scaling is done by simply dropping
64 # or duplicating pixels, so the result will be less than perfect,
65 # especially for computer-generated images. Alternatively,
66 # you can specify a filter to use to smoothen the image after
67 # scaling. The filter forms supported are 'impulse', 'box',
68 # 'triangle', 'quadratic' and 'gaussian'. If a filter is
69 # specified blur is an optional parameter specifying the
70 # blurriness of the filter. It defaults to 1.0. readscaled
71 # makes no attempt to keep the aspect ratio correct, so that
72 # is the users' responsibility.
73 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000074 print 'Filtering with "impulse"'
Roger E. Masse033d91b1996-12-20 21:41:50 +000075 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'impulse', 2.0)
76
77 # This function sets a global flag which defines whether the
78 # scan lines of the image are read or written from bottom to
79 # top (flag is zero, compatible with SGI GL) or from top to
Fred Drake004d5e62000-10-23 17:22:08 +000080 # bottom(flag is one, compatible with X). The default is zero.
Roger E. Masse033d91b1996-12-20 21:41:50 +000081 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000082 print 'Switching to X compatibility'
Fred Drake004d5e62000-10-23 17:22:08 +000083 imgfile.ttob (1)
Roger E. Masse033d91b1996-12-20 21:41:50 +000084
85 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000086 print 'Filtering with "triangle"'
Roger E. Masse033d91b1996-12-20 21:41:50 +000087 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'triangle', 3.0)
88 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000089 print 'Switching back to SGI compatibility'
Fred Drake004d5e62000-10-23 17:22:08 +000090 imgfile.ttob (0)
91
Roger E. Masse033d91b1996-12-20 21:41:50 +000092 if verbose: print 'Filtering with "quadratic"'
93 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'quadratic')
94 if verbose: print 'Filtering with "gaussian"'
95 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'gaussian', 1.0)
96
97 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000098 print 'Writing output file'
Fred Drake004d5e62000-10-23 17:22:08 +000099 imgfile.write (outputfile, simage, sizes[0]/2, sizes[1]/2, sizes[2])
Roger E. Masse033d91b1996-12-20 21:41:50 +0000100
101 os.unlink(outputfile)
102
Brett Cannon7dbd9182008-03-03 04:19:29 +0000103
104def test_main():
105
106 uu.decode(findfile('testrgb.uue'), 'test.rgb')
107 uu.decode(findfile('greyrgb.uue'), 'greytest.rgb')
108
109 # Test a 3 byte color image
110 testimage('test.rgb')
111
112 # Test a 1 byte greyscale image
113 testimage('greytest.rgb')
114
115 unlink('test.rgb')
116 unlink('greytest.rgb')
117
118if __name__ == '__main__':
119 test_main()