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