]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | #! /usr/bin/env python\r |
2 | \r | |
3 | """Test script for the imageop module. This has the side\r | |
4 | effect of partially testing the imgfile module as well.\r | |
5 | Roger E. Masse\r | |
6 | """\r | |
7 | \r | |
8 | from test.test_support import verbose, unlink, import_module, run_unittest\r | |
9 | \r | |
10 | imageop = import_module('imageop', deprecated=True)\r | |
11 | import uu, os, unittest\r | |
12 | \r | |
13 | \r | |
14 | SIZES = (1, 2, 3, 4)\r | |
15 | _VALUES = (1, 2, 2**10, 2**15-1, 2**15, 2**15+1, 2**31-2, 2**31-1)\r | |
16 | VALUES = tuple( -x for x in reversed(_VALUES) ) + (0,) + _VALUES\r | |
17 | AAAAA = "A" * 1024\r | |
18 | MAX_LEN = 2**20\r | |
19 | \r | |
20 | \r | |
21 | class InputValidationTests(unittest.TestCase):\r | |
22 | \r | |
23 | def _check(self, name, size=None, *extra):\r | |
24 | func = getattr(imageop, name)\r | |
25 | for height in VALUES:\r | |
26 | for width in VALUES:\r | |
27 | strlen = abs(width * height)\r | |
28 | if size:\r | |
29 | strlen *= size\r | |
30 | if strlen < MAX_LEN:\r | |
31 | data = "A" * strlen\r | |
32 | else:\r | |
33 | data = AAAAA\r | |
34 | if size:\r | |
35 | arguments = (data, size, width, height) + extra\r | |
36 | else:\r | |
37 | arguments = (data, width, height) + extra\r | |
38 | try:\r | |
39 | func(*arguments)\r | |
40 | except (ValueError, imageop.error):\r | |
41 | pass\r | |
42 | \r | |
43 | def check_size(self, name, *extra):\r | |
44 | for size in SIZES:\r | |
45 | self._check(name, size, *extra)\r | |
46 | \r | |
47 | def check(self, name, *extra):\r | |
48 | self._check(name, None, *extra)\r | |
49 | \r | |
50 | def test_input_validation(self):\r | |
51 | self.check_size("crop", 0, 0, 0, 0)\r | |
52 | self.check_size("scale", 1, 0)\r | |
53 | self.check_size("scale", -1, -1)\r | |
54 | self.check_size("tovideo")\r | |
55 | self.check("grey2mono", 128)\r | |
56 | self.check("grey2grey4")\r | |
57 | self.check("grey2grey2")\r | |
58 | self.check("dither2mono")\r | |
59 | self.check("dither2grey2")\r | |
60 | self.check("mono2grey", 0, 0)\r | |
61 | self.check("grey22grey")\r | |
62 | self.check("rgb2rgb8") # nlen*4 == len\r | |
63 | self.check("rgb82rgb")\r | |
64 | self.check("rgb2grey")\r | |
65 | self.check("grey2rgb")\r | |
66 | \r | |
67 | \r | |
68 | def test_main():\r | |
69 | \r | |
70 | run_unittest(InputValidationTests)\r | |
71 | \r | |
72 | try:\r | |
73 | import imgfile\r | |
74 | except ImportError:\r | |
75 | return\r | |
76 | \r | |
77 | # Create binary test files\r | |
78 | uu.decode(get_qualified_path('testrgb'+os.extsep+'uue'), 'test'+os.extsep+'rgb')\r | |
79 | \r | |
80 | image, width, height = getimage('test'+os.extsep+'rgb')\r | |
81 | \r | |
82 | # Return the selected part of image, which should by width by height\r | |
83 | # in size and consist of pixels of psize bytes.\r | |
84 | if verbose:\r | |
85 | print 'crop'\r | |
86 | newimage = imageop.crop (image, 4, width, height, 0, 0, 1, 1)\r | |
87 | \r | |
88 | # Return image scaled to size newwidth by newheight. No interpolation\r | |
89 | # is done, scaling is done by simple-minded pixel duplication or removal.\r | |
90 | # Therefore, computer-generated images or dithered images will\r | |
91 | # not look nice after scaling.\r | |
92 | if verbose:\r | |
93 | print 'scale'\r | |
94 | scaleimage = imageop.scale(image, 4, width, height, 1, 1)\r | |
95 | \r | |
96 | # Run a vertical low-pass filter over an image. It does so by computing\r | |
97 | # each destination pixel as the average of two vertically-aligned source\r | |
98 | # pixels. The main use of this routine is to forestall excessive flicker\r | |
99 | # if the image two vertically-aligned source pixels, hence the name.\r | |
100 | if verbose:\r | |
101 | print 'tovideo'\r | |
102 | videoimage = imageop.tovideo (image, 4, width, height)\r | |
103 | \r | |
104 | # Convert an rgb image to an 8 bit rgb\r | |
105 | if verbose:\r | |
106 | print 'rgb2rgb8'\r | |
107 | greyimage = imageop.rgb2rgb8(image, width, height)\r | |
108 | \r | |
109 | # Convert an 8 bit rgb image to a 24 bit rgb image\r | |
110 | if verbose:\r | |
111 | print 'rgb82rgb'\r | |
112 | image = imageop.rgb82rgb(greyimage, width, height)\r | |
113 | \r | |
114 | # Convert an rgb image to an 8 bit greyscale image\r | |
115 | if verbose:\r | |
116 | print 'rgb2grey'\r | |
117 | greyimage = imageop.rgb2grey(image, width, height)\r | |
118 | \r | |
119 | # Convert an 8 bit greyscale image to a 24 bit rgb image\r | |
120 | if verbose:\r | |
121 | print 'grey2rgb'\r | |
122 | image = imageop.grey2rgb(greyimage, width, height)\r | |
123 | \r | |
124 | # Convert a 8-bit deep greyscale image to a 1-bit deep image by\r | |
125 | # thresholding all the pixels. The resulting image is tightly packed\r | |
126 | # and is probably only useful as an argument to mono2grey.\r | |
127 | if verbose:\r | |
128 | print 'grey2mono'\r | |
129 | monoimage = imageop.grey2mono (greyimage, width, height, 0)\r | |
130 | \r | |
131 | # monoimage, width, height = getimage('monotest.rgb')\r | |
132 | # Convert a 1-bit monochrome image to an 8 bit greyscale or color image.\r | |
133 | # All pixels that are zero-valued on input get value p0 on output and\r | |
134 | # all one-value input pixels get value p1 on output. To convert a\r | |
135 | # monochrome black-and-white image to greyscale pass the values 0 and\r | |
136 | # 255 respectively.\r | |
137 | if verbose:\r | |
138 | print 'mono2grey'\r | |
139 | greyimage = imageop.mono2grey (monoimage, width, height, 0, 255)\r | |
140 | \r | |
141 | # Convert an 8-bit greyscale image to a 1-bit monochrome image using a\r | |
142 | # (simple-minded) dithering algorithm.\r | |
143 | if verbose:\r | |
144 | print 'dither2mono'\r | |
145 | monoimage = imageop.dither2mono (greyimage, width, height)\r | |
146 | \r | |
147 | # Convert an 8-bit greyscale image to a 4-bit greyscale image without\r | |
148 | # dithering.\r | |
149 | if verbose:\r | |
150 | print 'grey2grey4'\r | |
151 | grey4image = imageop.grey2grey4 (greyimage, width, height)\r | |
152 | \r | |
153 | # Convert an 8-bit greyscale image to a 2-bit greyscale image without\r | |
154 | # dithering.\r | |
155 | if verbose:\r | |
156 | print 'grey2grey2'\r | |
157 | grey2image = imageop.grey2grey2 (greyimage, width, height)\r | |
158 | \r | |
159 | # Convert an 8-bit greyscale image to a 2-bit greyscale image with\r | |
160 | # dithering. As for dither2mono, the dithering algorithm is currently\r | |
161 | # very simple.\r | |
162 | if verbose:\r | |
163 | print 'dither2grey2'\r | |
164 | grey2image = imageop.dither2grey2 (greyimage, width, height)\r | |
165 | \r | |
166 | # Convert a 4-bit greyscale image to an 8-bit greyscale image.\r | |
167 | if verbose:\r | |
168 | print 'grey42grey'\r | |
169 | greyimage = imageop.grey42grey (grey4image, width, height)\r | |
170 | \r | |
171 | # Convert a 2-bit greyscale image to an 8-bit greyscale image.\r | |
172 | if verbose:\r | |
173 | print 'grey22grey'\r | |
174 | image = imageop.grey22grey (grey2image, width, height)\r | |
175 | \r | |
176 | # Cleanup\r | |
177 | unlink('test'+os.extsep+'rgb')\r | |
178 | \r | |
179 | def getimage(name):\r | |
180 | """return a tuple consisting of\r | |
181 | image (in 'imgfile' format) width and height\r | |
182 | """\r | |
183 | import imgfile\r | |
184 | try:\r | |
185 | sizes = imgfile.getsizes(name)\r | |
186 | except imgfile.error:\r | |
187 | name = get_qualified_path(name)\r | |
188 | sizes = imgfile.getsizes(name)\r | |
189 | if verbose:\r | |
190 | print 'imgfile opening test image: %s, sizes: %s' % (name, str(sizes))\r | |
191 | \r | |
192 | image = imgfile.read(name)\r | |
193 | return (image, sizes[0], sizes[1])\r | |
194 | \r | |
195 | def get_qualified_path(name):\r | |
196 | """ return a more qualified path to name"""\r | |
197 | import sys\r | |
198 | import os\r | |
199 | path = sys.path\r | |
200 | try:\r | |
201 | path = [os.path.dirname(__file__)] + path\r | |
202 | except NameError:\r | |
203 | pass\r | |
204 | for dir in path:\r | |
205 | fullname = os.path.join(dir, name)\r | |
206 | if os.path.exists(fullname):\r | |
207 | return fullname\r | |
208 | return name\r | |
209 | \r | |
210 | if __name__ == '__main__':\r | |
211 | test_main()\r |