blob: 8eb330de376a82f80578a6196431509f6ecb1fe8 [file] [log] [blame]
Roger E. Masse4455cd81996-12-20 21:52:29 +00001#! /usr/bin/env python
Guido van Rossum7aa9fc51997-04-16 00:30:45 +00002
Roger E. Masse4455cd81996-12-20 21:52:29 +00003"""Simple test script for imgfile.c
4 Roger E. Masse
5"""
Roger E. Masse033d91b1996-12-20 21:41:50 +00006
Guido van Rossume26132c1998-04-23 20:13:30 +00007from test_support import verbose, unlink, findfile
Roger E. Masse033d91b1996-12-20 21:41:50 +00008
Guido van Rossum7aa9fc51997-04-16 00:30:45 +00009import imgfile, uu, os
10
Fred Drake004d5e62000-10-23 17:22:08 +000011
Guido van Rossum7aa9fc51997-04-16 00:30:45 +000012def main():
13
14 uu.decode(findfile('testrgb.uue'), 'test.rgb')
15 uu.decode(findfile('greyrgb.uue'), 'greytest.rgb')
Roger E. Masse033d91b1996-12-20 21:41:50 +000016
Roger E. Masse4455cd81996-12-20 21:52:29 +000017 # Test a 3 byte color image
Roger E. Masse3507f501996-12-20 21:47:58 +000018 testimage('test.rgb')
Fred Drake004d5e62000-10-23 17:22:08 +000019
Roger E. Masse4455cd81996-12-20 21:52:29 +000020 # Test a 1 byte greyscale image
Roger E. Masse3507f501996-12-20 21:47:58 +000021 testimage('greytest.rgb')
Roger E. Masse033d91b1996-12-20 21:41:50 +000022
Guido van Rossum7aa9fc51997-04-16 00:30:45 +000023 unlink('test.rgb')
24 unlink('greytest.rgb')
25
Roger E. Masse3507f501996-12-20 21:47:58 +000026def testimage(name):
Roger E. Masse4455cd81996-12-20 21:52:29 +000027 """Run through the imgfile's battery of possible methods
28 on the image passed in name.
Roger E. Masse033d91b1996-12-20 21:41:50 +000029 """
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 Rossum41360a41998-03-26 19:42:58 +000039 # 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)
Roger E. Masse033d91b1996-12-20 21:41:50 +000043 except imgfile.error:
Guido van Rossum41360a41998-03-26 19:42:58 +000044 # 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__
Roger E. Masse033d91b1996-12-20 21:41:50 +000049
Guido van Rossum41360a41998-03-26 19:42:58 +000050 parts = string.splitfields(ourname, os.sep)
51 parts[-1] = name
52 name = string.joinfields(parts, os.sep)
53 sizes = imgfile.getsizes(name)
Roger E. Masse033d91b1996-12-20 21:41:50 +000054 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000055 print 'Opening test image: %s, sizes: %s' % (name, str(sizes))
Roger E. Masse033d91b1996-12-20 21:41:50 +000056 # 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
Fred Drake004d5e62000-10-23 17:22:08 +000060 # to gl.lrectwrite, for instance.
Roger E. Masse033d91b1996-12-20 21:41:50 +000061 image = imgfile.read(name)
Fred Drake004d5e62000-10-23 17:22:08 +000062
Roger E. Masse033d91b1996-12-20 21:41:50 +000063 # 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
Fred Drake004d5e62000-10-23 17:22:08 +000067 # bytes are used). These are the formats returned by gl.lrectread.
Roger E. Masse033d91b1996-12-20 21:41:50 +000068 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000069 print 'Writing output file'
Fred Drake004d5e62000-10-23 17:22:08 +000070 imgfile.write (outputfile, image, sizes[0], sizes[1], sizes[2])
71
Roger E. Masse033d91b1996-12-20 21:41:50 +000072
73 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000074 print 'Opening scaled test image: %s, sizes: %s' % (name, str(sizes))
Roger E. Masse033d91b1996-12-20 21:41:50 +000075 # 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 Rossum41360a41998-03-26 19:42:58 +000088 print 'Filtering with "impulse"'
Roger E. Masse033d91b1996-12-20 21:41:50 +000089 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
Fred Drake004d5e62000-10-23 17:22:08 +000094 # bottom(flag is one, compatible with X). The default is zero.
Roger E. Masse033d91b1996-12-20 21:41:50 +000095 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +000096 print 'Switching to X compatibility'
Fred Drake004d5e62000-10-23 17:22:08 +000097 imgfile.ttob (1)
Roger E. Masse033d91b1996-12-20 21:41:50 +000098
99 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000100 print 'Filtering with "triangle"'
Roger E. Masse033d91b1996-12-20 21:41:50 +0000101 simage = imgfile.readscaled (name, sizes[0]/2, sizes[1]/2, 'triangle', 3.0)
102 if verbose:
Guido van Rossum41360a41998-03-26 19:42:58 +0000103 print 'Switching back to SGI compatibility'
Fred Drake004d5e62000-10-23 17:22:08 +0000104 imgfile.ttob (0)
105
Roger E. Masse033d91b1996-12-20 21:41:50 +0000106 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 Rossum41360a41998-03-26 19:42:58 +0000112 print 'Writing output file'
Fred Drake004d5e62000-10-23 17:22:08 +0000113 imgfile.write (outputfile, simage, sizes[0]/2, sizes[1]/2, sizes[2])
Roger E. Masse033d91b1996-12-20 21:41:50 +0000114
115 os.unlink(outputfile)
116
117main()