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