| 1 |
"""Test script for the imageop module. This has the side |
| 2 |
effect of partially testing the imgfile module as well. |
| 3 |
Roger E. Masse |
| 4 |
""" |
| 5 |
|
| 6 |
from test.test_support import verbose, unlink, import_module, run_unittest |
| 7 |
|
| 8 |
imageop = import_module('imageop', deprecated=True) |
| 9 |
import uu, os, unittest |
| 10 |
|
| 11 |
|
| 12 |
SIZES = (1, 2, 3, 4) |
| 13 |
_VALUES = (1, 2, 2**10, 2**15-1, 2**15, 2**15+1, 2**31-2, 2**31-1) |
| 14 |
VALUES = tuple( -x for x in reversed(_VALUES) ) + (0,) + _VALUES |
| 15 |
AAAAA = "A" * 1024 |
| 16 |
MAX_LEN = 2**20 |
| 17 |
|
| 18 |
|
| 19 |
class InputValidationTests(unittest.TestCase): |
| 20 |
|
| 21 |
def _check(self, name, size=None, *extra): |
| 22 |
func = getattr(imageop, name) |
| 23 |
for height in VALUES: |
| 24 |
for width in VALUES: |
| 25 |
strlen = abs(width * height) |
| 26 |
if size: |
| 27 |
strlen *= size |
| 28 |
if strlen < MAX_LEN: |
| 29 |
data = "A" * strlen |
| 30 |
else: |
| 31 |
data = AAAAA |
| 32 |
if size: |
| 33 |
arguments = (data, size, width, height) + extra |
| 34 |
else: |
| 35 |
arguments = (data, width, height) + extra |
| 36 |
try: |
| 37 |
func(*arguments) |
| 38 |
except (ValueError, imageop.error): |
| 39 |
pass |
| 40 |
|
| 41 |
def check_size(self, name, *extra): |
| 42 |
for size in SIZES: |
| 43 |
self._check(name, size, *extra) |
| 44 |
|
| 45 |
def check(self, name, *extra): |
| 46 |
self._check(name, None, *extra) |
| 47 |
|
| 48 |
def test_input_validation(self): |
| 49 |
self.check_size("crop", 0, 0, 0, 0) |
| 50 |
self.check_size("scale", 1, 0) |
| 51 |
self.check_size("scale", -1, -1) |
| 52 |
self.check_size("tovideo") |
| 53 |
self.check("grey2mono", 128) |
| 54 |
self.check("grey2grey4") |
| 55 |
self.check("grey2grey2") |
| 56 |
self.check("dither2mono") |
| 57 |
self.check("dither2grey2") |
| 58 |
self.check("mono2grey", 0, 0) |
| 59 |
self.check("grey22grey") |
| 60 |
self.check("rgb2rgb8") |
| 61 |
self.check("rgb82rgb") |
| 62 |
self.check("rgb2grey") |
| 63 |
self.check("grey2rgb") |
| 64 |
|
| 65 |
with self.assertRaises(imageop.error): |
| 66 |
imageop.grey2rgb('A'*256, 1, 129) |
| 67 |
|
| 68 |
def test_main(): |
| 69 |
|
| 70 |
run_unittest(InputValidationTests) |
| 71 |
|
| 72 |
try: |
| 73 |
import imgfile |
| 74 |
except ImportError: |
| 75 |
return |
| 76 |
|
| 77 |
|
| 78 |
uu.decode(get_qualified_path('testrgb'+os.extsep+'uue'), 'test'+os.extsep+'rgb') |
| 79 |
|
| 80 |
image, width, height = getimage('test'+os.extsep+'rgb') |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
if verbose: |
| 85 |
print 'crop' |
| 86 |
newimage = imageop.crop (image, 4, width, height, 0, 0, 1, 1) |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
if verbose: |
| 93 |
print 'scale' |
| 94 |
scaleimage = imageop.scale(image, 4, width, height, 1, 1) |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
if verbose: |
| 101 |
print 'tovideo' |
| 102 |
videoimage = imageop.tovideo (image, 4, width, height) |
| 103 |
|
| 104 |
|
| 105 |
if verbose: |
| 106 |
print 'rgb2rgb8' |
| 107 |
greyimage = imageop.rgb2rgb8(image, width, height) |
| 108 |
|
| 109 |
|
| 110 |
if verbose: |
| 111 |
print 'rgb82rgb' |
| 112 |
image = imageop.rgb82rgb(greyimage, width, height) |
| 113 |
|
| 114 |
|
| 115 |
if verbose: |
| 116 |
print 'rgb2grey' |
| 117 |
greyimage = imageop.rgb2grey(image, width, height) |
| 118 |
|
| 119 |
|
| 120 |
if verbose: |
| 121 |
print 'grey2rgb' |
| 122 |
image = imageop.grey2rgb(greyimage, width, height) |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
if verbose: |
| 128 |
print 'grey2mono' |
| 129 |
monoimage = imageop.grey2mono (greyimage, width, height, 0) |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
if verbose: |
| 138 |
print 'mono2grey' |
| 139 |
greyimage = imageop.mono2grey (monoimage, width, height, 0, 255) |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
if verbose: |
| 144 |
print 'dither2mono' |
| 145 |
monoimage = imageop.dither2mono (greyimage, width, height) |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
if verbose: |
| 150 |
print 'grey2grey4' |
| 151 |
grey4image = imageop.grey2grey4 (greyimage, width, height) |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
if verbose: |
| 156 |
print 'grey2grey2' |
| 157 |
grey2image = imageop.grey2grey2 (greyimage, width, height) |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
if verbose: |
| 163 |
print 'dither2grey2' |
| 164 |
grey2image = imageop.dither2grey2 (greyimage, width, height) |
| 165 |
|
| 166 |
|
| 167 |
if verbose: |
| 168 |
print 'grey42grey' |
| 169 |
greyimage = imageop.grey42grey (grey4image, width, height) |
| 170 |
|
| 171 |
|
| 172 |
if verbose: |
| 173 |
print 'grey22grey' |
| 174 |
image = imageop.grey22grey (grey2image, width, height) |
| 175 |
|
| 176 |
|
| 177 |
unlink('test'+os.extsep+'rgb') |
| 178 |
|
| 179 |
def getimage(name): |
| 180 |
"""return a tuple consisting of |
| 181 |
image (in 'imgfile' format) width and height |
| 182 |
""" |
| 183 |
import imgfile |
| 184 |
try: |
| 185 |
sizes = imgfile.getsizes(name) |
| 186 |
except imgfile.error: |
| 187 |
name = get_qualified_path(name) |
| 188 |
sizes = imgfile.getsizes(name) |
| 189 |
if verbose: |
| 190 |
print 'imgfile opening test image: %s, sizes: %s' % (name, str(sizes)) |
| 191 |
|
| 192 |
image = imgfile.read(name) |
| 193 |
return (image, sizes[0], sizes[1]) |
| 194 |
|
| 195 |
def get_qualified_path(name): |
| 196 |
""" return a more qualified path to name""" |
| 197 |
import sys |
| 198 |
import os |
| 199 |
path = sys.path |
| 200 |
try: |
| 201 |
path = [os.path.dirname(__file__)] + path |
| 202 |
except NameError: |
| 203 |
pass |
| 204 |
for dir in path: |
| 205 |
fullname = os.path.join(dir, name) |
| 206 |
if os.path.exists(fullname): |
| 207 |
return fullname |
| 208 |
return name |
| 209 |
|
| 210 |
if __name__ == '__main__': |
| 211 |
test_main() |