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