PageRenderTime 25ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/python/Lib/test/test_imageop.py

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