Roger E. Masse | 4455cd8 | 1996-12-20 21:52:29 +0000 | [diff] [blame] | 1 | """Simple test script for imgfile.c |
| 2 | Roger E. Masse |
| 3 | """ |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 4 | |
Brett Cannon | 4652537 | 2008-05-15 03:32:11 +0000 | [diff] [blame] | 5 | from test.test_support import verbose, unlink, findfile, import_module |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 6 | |
Brett Cannon | 4652537 | 2008-05-15 03:32:11 +0000 | [diff] [blame] | 7 | imgfile = import_module('imgfile', deprecated=True) |
| 8 | import uu |
Guido van Rossum | 7aa9fc5 | 1997-04-16 00:30:45 +0000 | [diff] [blame] | 9 | |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 10 | |
Roger E. Masse | 3507f50 | 1996-12-20 21:47:58 +0000 | [diff] [blame] | 11 | def testimage(name): |
Roger E. Masse | 4455cd8 | 1996-12-20 21:52:29 +0000 | [diff] [blame] | 12 | """Run through the imgfile's battery of possible methods |
| 13 | on the image passed in name. |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 14 | """ |
| 15 | |
| 16 | import sys |
| 17 | import os |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 18 | |
| 19 | outputfile = '/tmp/deleteme' |
| 20 | |
| 21 | # try opening the name directly |
| 22 | try: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 23 | # This function returns a tuple (x, y, z) where x and y are the size |
| 24 | # of the image in pixels and z is the number of bytes per pixel. Only |
| 25 | # 3 byte RGB pixels and 1 byte greyscale pixels are supported. |
| 26 | sizes = imgfile.getsizes(name) |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 27 | except imgfile.error: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 28 | # get a more qualified path component of the script... |
| 29 | if __name__ == '__main__': |
| 30 | ourname = sys.argv[0] |
| 31 | else: # ...or the full path of the module |
| 32 | ourname = sys.modules[__name__].__file__ |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 33 | |
Eric S. Raymond | fc170b1 | 2001-02-09 11:51:27 +0000 | [diff] [blame] | 34 | parts = ourname.split(os.sep) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 35 | parts[-1] = name |
Eric S. Raymond | dbbbaf2 | 2001-02-09 17:05:53 +0000 | [diff] [blame] | 36 | name = os.sep.join(parts) |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 37 | sizes = imgfile.getsizes(name) |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 38 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 39 | print 'Opening test image: %s, sizes: %s' % (name, str(sizes)) |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 40 | # This function reads and decodes the image on the specified file, |
| 41 | # and returns it as a python string. The string has either 1 byte |
| 42 | # greyscale pixels or 4 byte RGBA pixels. The bottom left pixel |
| 43 | # is the first in the string. This format is suitable to pass |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 44 | # to gl.lrectwrite, for instance. |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 45 | image = imgfile.read(name) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 46 | |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 47 | # This function writes the RGB or greyscale data in data to |
| 48 | # image file file. x and y give the size of the image, z is |
| 49 | # 1 for 1 byte greyscale images or 3 for RGB images (which |
| 50 | # are stored as 4 byte values of which only the lower three |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 51 | # bytes are used). These are the formats returned by gl.lrectread. |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 52 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 53 | print 'Writing output file' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 54 | imgfile.write (outputfile, image, sizes[0], sizes[1], sizes[2]) |
| 55 | |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 56 | |
| 57 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 58 | print 'Opening scaled test image: %s, sizes: %s' % (name, str(sizes)) |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 59 | # This function is identical to read but it returns an image that |
| 60 | # is scaled to the given x and y sizes. If the filter and blur |
| 61 | # parameters are omitted scaling is done by simply dropping |
| 62 | # or duplicating pixels, so the result will be less than perfect, |
| 63 | # especially for computer-generated images. Alternatively, |
| 64 | # you can specify a filter to use to smoothen the image after |
| 65 | # scaling. The filter forms supported are 'impulse', 'box', |
| 66 | # 'triangle', 'quadratic' and 'gaussian'. If a filter is |
| 67 | # specified blur is an optional parameter specifying the |
| 68 | # blurriness of the filter. It defaults to 1.0. readscaled |
| 69 | # makes no attempt to keep the aspect ratio correct, so that |
| 70 | # is the users' responsibility. |
| 71 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 72 | print 'Filtering with "impulse"' |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 73 | simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'impulse', 2.0) |
| 74 | |
| 75 | # This function sets a global flag which defines whether the |
| 76 | # scan lines of the image are read or written from bottom to |
| 77 | # top (flag is zero, compatible with SGI GL) or from top to |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 78 | # bottom(flag is one, compatible with X). The default is zero. |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 79 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 80 | print 'Switching to X compatibility' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 81 | imgfile.ttob (1) |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 82 | |
| 83 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 84 | print 'Filtering with "triangle"' |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 85 | simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'triangle', 3.0) |
| 86 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 87 | print 'Switching back to SGI compatibility' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 88 | imgfile.ttob (0) |
| 89 | |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 90 | if verbose: print 'Filtering with "quadratic"' |
| 91 | simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'quadratic') |
| 92 | if verbose: print 'Filtering with "gaussian"' |
| 93 | simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'gaussian', 1.0) |
| 94 | |
| 95 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 96 | print 'Writing output file' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 97 | imgfile.write (outputfile, simage, sizes[0]/2, sizes[1]/2, sizes[2]) |
Roger E. Masse | 033d91b | 1996-12-20 21:41:50 +0000 | [diff] [blame] | 98 | |
| 99 | os.unlink(outputfile) |
| 100 | |
Brett Cannon | 7dbd918 | 2008-03-03 04:19:29 +0000 | [diff] [blame] | 101 | |
| 102 | def test_main(): |
| 103 | |
| 104 | uu.decode(findfile('testrgb.uue'), 'test.rgb') |
| 105 | uu.decode(findfile('greyrgb.uue'), 'greytest.rgb') |
| 106 | |
| 107 | # Test a 3 byte color image |
| 108 | testimage('test.rgb') |
| 109 | |
| 110 | # Test a 1 byte greyscale image |
| 111 | testimage('greytest.rgb') |
| 112 | |
| 113 | unlink('test.rgb') |
| 114 | unlink('greytest.rgb') |
| 115 | |
| 116 | if __name__ == '__main__': |
| 117 | test_main() |