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