Home | History | Annotate | Download | only in test
      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") # nlen*4 == len
     61         self.check("rgb82rgb")
     62         self.check("rgb2grey")
     63         self.check("grey2rgb")
     64         # Issue #24264: Buffer overflow
     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     # Create binary test files
     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     # Return the selected part of image, which should by width by height
     83     # in size and consist of pixels of psize bytes.
     84     if verbose:
     85         print 'crop'
     86     newimage = imageop.crop (image, 4, width, height, 0, 0, 1, 1)
     87 
     88     # Return image scaled to size newwidth by newheight. No interpolation
     89     # is done, scaling is done by simple-minded pixel duplication or removal.
     90     # Therefore, computer-generated images or dithered images will
     91     # not look nice after scaling.
     92     if verbose:
     93         print 'scale'
     94     scaleimage = imageop.scale(image, 4, width, height, 1, 1)
     95 
     96     # Run a vertical low-pass filter over an image. It does so by computing
     97     # each destination pixel as the average of two vertically-aligned source
     98     # pixels. The main use of this routine is to forestall excessive flicker
     99     # if the image two vertically-aligned source pixels,  hence the name.
    100     if verbose:
    101         print 'tovideo'
    102     videoimage = imageop.tovideo (image, 4, width, height)
    103 
    104     # Convert an rgb image to an 8 bit rgb
    105     if verbose:
    106         print 'rgb2rgb8'
    107     greyimage = imageop.rgb2rgb8(image, width, height)
    108 
    109     # Convert an 8 bit rgb image to a 24 bit rgb image
    110     if verbose:
    111         print 'rgb82rgb'
    112     image = imageop.rgb82rgb(greyimage, width, height)
    113 
    114     # Convert an rgb image to an 8 bit greyscale image
    115     if verbose:
    116         print 'rgb2grey'
    117     greyimage = imageop.rgb2grey(image, width, height)
    118 
    119     # Convert an 8 bit greyscale image to a 24 bit rgb image
    120     if verbose:
    121         print 'grey2rgb'
    122     image = imageop.grey2rgb(greyimage, width, height)
    123 
    124     # Convert an 8-bit deep greyscale image to a 1-bit deep image by
    125     # thresholding all the pixels. The resulting image is tightly packed
    126     # and is probably only useful as an argument to mono2grey.
    127     if verbose:
    128         print 'grey2mono'
    129     monoimage = imageop.grey2mono (greyimage, width, height, 0)
    130 
    131     # monoimage, width, height = getimage('monotest.rgb')
    132     # Convert a 1-bit monochrome image to an 8 bit greyscale or color image.
    133     # All pixels that are zero-valued on input get value p0 on output and
    134     # all one-value input pixels get value p1 on output. To convert a
    135     # monochrome  black-and-white image to greyscale pass the values 0 and
    136     # 255 respectively.
    137     if verbose:
    138         print 'mono2grey'
    139     greyimage = imageop.mono2grey (monoimage, width, height, 0, 255)
    140 
    141     # Convert an 8-bit greyscale image to a 1-bit monochrome image using a
    142     # (simple-minded) dithering algorithm.
    143     if verbose:
    144         print 'dither2mono'
    145     monoimage = imageop.dither2mono (greyimage, width, height)
    146 
    147     # Convert an 8-bit greyscale image to a 4-bit greyscale image without
    148     # dithering.
    149     if verbose:
    150         print 'grey2grey4'
    151     grey4image = imageop.grey2grey4 (greyimage, width, height)
    152 
    153     # Convert an 8-bit greyscale image to a 2-bit greyscale image without
    154     # dithering.
    155     if verbose:
    156         print 'grey2grey2'
    157     grey2image = imageop.grey2grey2 (greyimage, width, height)
    158 
    159     # Convert an 8-bit greyscale image to a 2-bit greyscale image with
    160     # dithering. As for dither2mono, the dithering algorithm is currently
    161     # very simple.
    162     if verbose:
    163         print 'dither2grey2'
    164     grey2image = imageop.dither2grey2 (greyimage, width, height)
    165 
    166     # Convert a 4-bit greyscale image to an 8-bit greyscale image.
    167     if verbose:
    168         print 'grey42grey'
    169     greyimage = imageop.grey42grey (grey4image, width, height)
    170 
    171     # Convert a 2-bit greyscale image to an 8-bit greyscale image.
    172     if verbose:
    173         print 'grey22grey'
    174     image = imageop.grey22grey (grey2image, width, height)
    175 
    176     # Cleanup
    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()
    212