annotate Lib/test/test_imageop.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b36cb4602e21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
1 #! /usr/bin/env python
5328
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4966
diff changeset
2
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
3 """Test script for the imageop module. This has the side
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
4 effect of partially testing the imgfile module as well.
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
5 Roger E. Masse
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
6 """
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
7
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 46923
diff changeset
8 from test.test_support import verbose, unlink, import_module, run_unittest
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
9
46923
c2ab7a06ed24 Depreate imageop for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 45631
diff changeset
10 imageop = import_module('imageop', deprecated=True)
49347
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 46923
diff changeset
11 import uu, os, unittest
f4aeb113c9ef fix security issue 2: imageop's poor validation of arguments could result in segfaults
Benjamin Peterson <benjamin@python.org>
parents: 46923
diff changeset
12
36563
b9381a5623e2 Suppress new deprecation warnings when running the
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
13
49348
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
14 SIZES = (1, 2, 3, 4)
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
15 _VALUES = (1, 2, 2**10, 2**15-1, 2**15, 2**15+1, 2**31-2, 2**31-1)
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
16 VALUES = tuple( -x for x in reversed(_VALUES) ) + (0,) + _VALUES
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
17 AAAAA = "A" * 1024
49751
c6d16b2a8efa #4317: Fix an Array Bounds Read in imageop.rgb2rgb8.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49348
diff changeset
18 MAX_LEN = 2**20
49348
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
19
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
20
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
21 class InputValidationTests(unittest.TestCase):
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
22
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
23 def _check(self, name, size=None, *extra):
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
24 func = getattr(imageop, name)
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
25 for height in VALUES:
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
26 for width in VALUES:
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
27 strlen = abs(width * height)
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
28 if size:
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
29 strlen *= size
49751
c6d16b2a8efa #4317: Fix an Array Bounds Read in imageop.rgb2rgb8.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 49348
diff changeset
30 if strlen < MAX_LEN:
49348
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
31 data = "A" * strlen
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
32 else:
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
33 data = AAAAA
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
34 if size:
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
35 arguments = (data, size, width, height) + extra
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
36 else:
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
37 arguments = (data, width, height) + extra
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
38 try:
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
39 func(*arguments)
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
40 except (ValueError, imageop.error):
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
41 pass
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
42
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
43 def check_size(self, name, *extra):
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
44 for size in SIZES:
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
45 self._check(name, size, *extra)
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
46
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
47 def check(self, name, *extra):
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
48 self._check(name, None, *extra)
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
49
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
50 def test_input_validation(self):
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
51 self.check_size("crop", 0, 0, 0, 0)
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
52 self.check_size("scale", 1, 0)
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
53 self.check_size("scale", -1, -1)
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
54 self.check_size("tovideo")
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
55 self.check("grey2mono", 128)
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
56 self.check("grey2grey4")
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
57 self.check("grey2grey2")
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
58 self.check("dither2mono")
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
59 self.check("dither2grey2")
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
60 self.check("mono2grey", 0, 0)
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
61 self.check("grey22grey")
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
62 self.check("rgb2rgb8") # nlen*4 == len
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
63 self.check("rgb82rgb")
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
64 self.check("rgb2grey")
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
65 self.check("grey2rgb")
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
66
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
67
45631
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 41794
diff changeset
68 def test_main():
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
69
49348
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
70 run_unittest(InputValidationTests)
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
71
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
72 try:
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
73 import imgfile
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
74 except ImportError:
4156555f3911 backport r66689: imageop could segfault due to poor argument validation
Benjamin Peterson <benjamin@python.org>
parents: 36563
diff changeset
75 return
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
76
5328
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4966
diff changeset
77 # Create binary test files
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 15966
diff changeset
78 uu.decode(get_qualified_path('testrgb'+os.extsep+'uue'), 'test'+os.extsep+'rgb')
5328
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4966
diff changeset
79
41784
b7fb942b403e Remove the rgbimg module. It has been deprecated since Python 2.5.
Brett Cannon <bcannon@gmail.com>
parents: 36563
diff changeset
80 image, width, height = getimage('test'+os.extsep+'rgb')
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
81
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
82 # Return the selected part of image, which should by width by height
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
83 # in size and consist of pixels of psize bytes.
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
84 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
85 print 'crop'
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
86 newimage = imageop.crop (image, 4, width, height, 0, 0, 1, 1)
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
87
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
88 # Return image scaled to size newwidth by newheight. No interpolation
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
89 # is done, scaling is done by simple-minded pixel duplication or removal.
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
90 # Therefore, computer-generated images or dithered images will
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
91 # not look nice after scaling.
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
92 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
93 print 'scale'
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
94 scaleimage = imageop.scale(image, 4, width, height, 1, 1)
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
95
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
96 # Run a vertical low-pass filter over an image. It does so by computing
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
97 # each destination pixel as the average of two vertically-aligned source
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
98 # pixels. The main use of this routine is to forestall excessive flicker
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
99 # if the image two vertically-aligned source pixels, hence the name.
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
100 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
101 print 'tovideo'
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
102 videoimage = imageop.tovideo (image, 4, width, height)
4965
d84ca197868b Modified to use 'rgbimg' instead of the SGI specific 'imgfile' for reading
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4801
diff changeset
103
4966
9bbd57d3609f Opps. Added tests for the last two methods that were missing from
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4965
diff changeset
104 # Convert an rgb image to an 8 bit rgb
4965
d84ca197868b Modified to use 'rgbimg' instead of the SGI specific 'imgfile' for reading
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4801
diff changeset
105 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
106 print 'rgb2rgb8'
4965
d84ca197868b Modified to use 'rgbimg' instead of the SGI specific 'imgfile' for reading
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4801
diff changeset
107 greyimage = imageop.rgb2rgb8(image, width, height)
d84ca197868b Modified to use 'rgbimg' instead of the SGI specific 'imgfile' for reading
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4801
diff changeset
108
d84ca197868b Modified to use 'rgbimg' instead of the SGI specific 'imgfile' for reading
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4801
diff changeset
109 # Convert an 8 bit rgb image to a 24 bit rgb image
d84ca197868b Modified to use 'rgbimg' instead of the SGI specific 'imgfile' for reading
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4801
diff changeset
110 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
111 print 'rgb82rgb'
4965
d84ca197868b Modified to use 'rgbimg' instead of the SGI specific 'imgfile' for reading
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4801
diff changeset
112 image = imageop.rgb82rgb(greyimage, width, height)
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
113
4966
9bbd57d3609f Opps. Added tests for the last two methods that were missing from
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4965
diff changeset
114 # Convert an rgb image to an 8 bit greyscale image
9bbd57d3609f Opps. Added tests for the last two methods that were missing from
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4965
diff changeset
115 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
116 print 'rgb2grey'
4966
9bbd57d3609f Opps. Added tests for the last two methods that were missing from
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4965
diff changeset
117 greyimage = imageop.rgb2grey(image, width, height)
9bbd57d3609f Opps. Added tests for the last two methods that were missing from
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4965
diff changeset
118
9bbd57d3609f Opps. Added tests for the last two methods that were missing from
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4965
diff changeset
119 # Convert an 8 bit greyscale image to a 24 bit rgb image
9bbd57d3609f Opps. Added tests for the last two methods that were missing from
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4965
diff changeset
120 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
121 print 'grey2rgb'
4966
9bbd57d3609f Opps. Added tests for the last two methods that were missing from
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4965
diff changeset
122 image = imageop.grey2rgb(greyimage, width, height)
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
123
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
124 # Convert a 8-bit deep greyscale image to a 1-bit deep image by
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 7747
diff changeset
125 # thresholding all the pixels. The resulting image is tightly packed
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
126 # and is probably only useful as an argument to mono2grey.
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
127 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
128 print 'grey2mono'
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
129 monoimage = imageop.grey2mono (greyimage, width, height, 0)
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
130
4965
d84ca197868b Modified to use 'rgbimg' instead of the SGI specific 'imgfile' for reading
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4801
diff changeset
131 # monoimage, width, height = getimage('monotest.rgb')
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
132 # Convert a 1-bit monochrome image to an 8 bit greyscale or color image.
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
133 # All pixels that are zero-valued on input get value p0 on output and
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
134 # all one-value input pixels get value p1 on output. To convert a
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
135 # monochrome black-and-white image to greyscale pass the values 0 and
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
136 # 255 respectively.
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
137 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
138 print 'mono2grey'
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
139 greyimage = imageop.mono2grey (monoimage, width, height, 0, 255)
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
140
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
141 # Convert an 8-bit greyscale image to a 1-bit monochrome image using a
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
142 # (simple-minded) dithering algorithm.
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
143 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
144 print 'dither2mono'
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
145 monoimage = imageop.dither2mono (greyimage, width, height)
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
146
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
147 # Convert an 8-bit greyscale image to a 4-bit greyscale image without
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
148 # dithering.
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
149 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
150 print 'grey2grey4'
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
151 grey4image = imageop.grey2grey4 (greyimage, width, height)
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
152
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
153 # Convert an 8-bit greyscale image to a 2-bit greyscale image without
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
154 # dithering.
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
155 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
156 print 'grey2grey2'
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
157 grey2image = imageop.grey2grey2 (greyimage, width, height)
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
158
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
159 # Convert an 8-bit greyscale image to a 2-bit greyscale image with
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
160 # dithering. As for dither2mono, the dithering algorithm is currently
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
161 # very simple.
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
162 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
163 print 'dither2grey2'
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
164 grey2image = imageop.dither2grey2 (greyimage, width, height)
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
165
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
166 # Convert a 4-bit greyscale image to an 8-bit greyscale image.
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
167 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
168 print 'grey42grey'
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
169 greyimage = imageop.grey42grey (grey4image, width, height)
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
170
15305
07f24261c6e6 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 13620
diff changeset
171 # Convert a 2-bit greyscale image to an 8-bit greyscale image.
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
172 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
173 print 'grey22grey'
5328
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4966
diff changeset
174 image = imageop.grey22grey (grey2image, width, height)
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4966
diff changeset
175
359e8f815a3a Use uuencoded test images.
Guido van Rossum <guido@python.org>
parents: 4966
diff changeset
176 # Cleanup
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 15966
diff changeset
177 unlink('test'+os.extsep+'rgb')
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
178
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
179 def getimage(name):
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
180 """return a tuple consisting of
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
181 image (in 'imgfile' format) width and height
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
182 """
58763
e4ae79d53e23 Add missing imports.
Georg Brandl <georg@python.org>
parents: 49750
diff changeset
183 import imgfile
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
184 try:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
185 sizes = imgfile.getsizes(name)
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
186 except imgfile.error:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
187 name = get_qualified_path(name)
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
188 sizes = imgfile.getsizes(name)
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
189 if verbose:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
190 print 'imgfile opening test image: %s, sizes: %s' % (name, str(sizes))
4801
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
191
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
192 image = imgfile.read(name)
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
193 return (image, sizes[0], sizes[1])
2d5ee374a73b Test program for the SGI imageop module.
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents:
diff changeset
194
4965
d84ca197868b Modified to use 'rgbimg' instead of the SGI specific 'imgfile' for reading
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4801
diff changeset
195 def get_qualified_path(name):
5516
2456d8ff9fa8 More rational implementation of get_qualified_path() -- search along
Guido van Rossum <guido@python.org>
parents: 5328
diff changeset
196 """ return a more qualified path to name"""
4965
d84ca197868b Modified to use 'rgbimg' instead of the SGI specific 'imgfile' for reading
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4801
diff changeset
197 import sys
d84ca197868b Modified to use 'rgbimg' instead of the SGI specific 'imgfile' for reading
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4801
diff changeset
198 import os
6371
678a3f1d0c80 Fix path search for test data file so it works under more circumstances.
Guido van Rossum <guido@python.org>
parents: 5516
diff changeset
199 path = sys.path
678a3f1d0c80 Fix path search for test data file so it works under more circumstances.
Guido van Rossum <guido@python.org>
parents: 5516
diff changeset
200 try:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
201 path = [os.path.dirname(__file__)] + path
6371
678a3f1d0c80 Fix path search for test data file so it works under more circumstances.
Guido van Rossum <guido@python.org>
parents: 5516
diff changeset
202 except NameError:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
203 pass
6371
678a3f1d0c80 Fix path search for test data file so it works under more circumstances.
Guido van Rossum <guido@python.org>
parents: 5516
diff changeset
204 for dir in path:
7747
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
205 fullname = os.path.join(dir, name)
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
206 if os.path.exists(fullname):
aedd2d0dcb31 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 6371
diff changeset
207 return fullname
4965
d84ca197868b Modified to use 'rgbimg' instead of the SGI specific 'imgfile' for reading
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4801
diff changeset
208 return name
d84ca197868b Modified to use 'rgbimg' instead of the SGI specific 'imgfile' for reading
Roger E. Masse <rmasse@newcnri.cnri.reston.va.us>
parents: 4801
diff changeset
209
45631
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 41794
diff changeset
210 if __name__ == '__main__':
a55136c78b60 Add test_main() functions to various tests where it was simple to do. Done so
Brett Cannon <bcannon@gmail.com>
parents: 41794
diff changeset
211 test_main()