Home | History | Annotate | Download | only in test
      1 #! /usr/bin/env python
      2 
      3 """Test script for the imageop module.  This has the side
      4    effect of partially testing the imgfile module as well.
      5    Roger E. Masse
      6 """
      7 
      8 from test.test_support import verbose, unlink, import_module, run_unittest
      9 
     10 imageop = import_module('imageop', deprecated=True)
     11 import uu, os, unittest
     12 
     13 
     14 SIZES = (1, 2, 3, 4)
     15 _VALUES = (1, 2, 2**10, 2**15-1, 2**15, 2**15+1, 2**31-2, 2**31-1)
     16 VALUES = tuple( -x for x in reversed(_VALUES) ) + (0,) + _VALUES
     17 AAAAA = "A" * 1024
     18 MAX_LEN = 2**20
     19 
     20 
     21 class InputValidationTests(unittest.TestCase):
     22 
     23     def _check(self, name, size=None, *extra):
     24         func = getattr(imageop, name)
     25         for height in VALUES:
     26             for width in VALUES:
     27                 strlen = abs(width * height)
     28                 if size:
     29                     strlen *= size
     30                 if strlen < MAX_LEN:
     31                     data = "A" * strlen
     32                 else:
     33                     data = AAAAA
     34                 if size:
     35                     arguments = (data, size, width, height) + extra
     36                 else:
     37                     arguments = (data, width, height) + extra
     38                 try:
     39                     func(*arguments)
     40                 except (ValueError, imageop.error):
     41                     pass
     42 
     43     def check_size(self, name, *extra):
     44         for size in SIZES:
     45             self._check(name, size, *extra)
     46 
     47     def check(self, name, *extra):
     48         self._check(name, None, *extra)
     49 
     50     def test_input_validation(self):
     51         self.check_size("crop", 0, 0, 0, 0)
     52         self.check_size("scale", 1, 0)
     53         self.check_size("scale", -1, -1)
     54         self.check_size("tovideo")
     55         self.check("grey2mono", 128)
     56         self.check("grey2grey4")
     57         self.check("grey2grey2")
     58         self.check("dither2mono")
     59         self.check("dither2grey2")
     60         self.check("mono2grey", 0, 0)
     61         self.check("grey22grey")
     62         self.check("rgb2rgb8") # nlen*4 == len
     63         self.check("rgb82rgb")
     64         self.check("rgb2grey")
     65         self.check("grey2rgb")
     66 
     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 a 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