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