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