1996-12-20 16:49:06 -04:00
|
|
|
#! /usr/bin/env python
|
1997-04-15 21:30:45 -03:00
|
|
|
|
1996-12-20 16:49:06 -04:00
|
|
|
"""Test script for the imageop module. This has the side
|
|
|
|
effect of partially testing the imgfile module as well.
|
|
|
|
Roger E. Masse
|
|
|
|
"""
|
|
|
|
|
2008-09-29 22:31:49 -03:00
|
|
|
from test.test_support import verbose, unlink, import_module, run_unittest
|
1997-04-15 21:30:45 -03:00
|
|
|
|
2008-05-11 21:08:34 -03:00
|
|
|
imageop = import_module('imageop', deprecated=True)
|
2008-09-29 22:31:49 -03:00
|
|
|
import uu, os, unittest
|
|
|
|
|
|
|
|
|
|
|
|
SIZES = (1, 2, 3, 4)
|
|
|
|
_VALUES = (1, 2, 2**10, 2**15-1, 2**15, 2**15+1, 2**31-2, 2**31-1)
|
|
|
|
VALUES = tuple( -x for x in reversed(_VALUES) ) + (0,) + _VALUES
|
|
|
|
AAAAA = "A" * 1024
|
2008-11-18 18:19:37 -04:00
|
|
|
MAX_LEN = 2**20
|
2008-09-29 22:31:49 -03:00
|
|
|
|
|
|
|
|
|
|
|
class InputValidationTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def _check(self, name, size=None, *extra):
|
|
|
|
func = getattr(imageop, name)
|
|
|
|
for height in VALUES:
|
|
|
|
for width in VALUES:
|
|
|
|
strlen = abs(width * height)
|
|
|
|
if size:
|
|
|
|
strlen *= size
|
2008-11-18 18:19:37 -04:00
|
|
|
if strlen < MAX_LEN:
|
2008-09-29 22:31:49 -03:00
|
|
|
data = "A" * strlen
|
|
|
|
else:
|
|
|
|
data = AAAAA
|
|
|
|
if size:
|
|
|
|
arguments = (data, size, width, height) + extra
|
|
|
|
else:
|
|
|
|
arguments = (data, width, height) + extra
|
|
|
|
try:
|
|
|
|
func(*arguments)
|
|
|
|
except (ValueError, imageop.error):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def check_size(self, name, *extra):
|
|
|
|
for size in SIZES:
|
|
|
|
self._check(name, size, *extra)
|
|
|
|
|
|
|
|
def check(self, name, *extra):
|
|
|
|
self._check(name, None, *extra)
|
|
|
|
|
|
|
|
def test_input_validation(self):
|
|
|
|
self.check_size("crop", 0, 0, 0, 0)
|
|
|
|
self.check_size("scale", 1, 0)
|
|
|
|
self.check_size("scale", -1, -1)
|
|
|
|
self.check_size("tovideo")
|
|
|
|
self.check("grey2mono", 128)
|
|
|
|
self.check("grey2grey4")
|
|
|
|
self.check("grey2grey2")
|
|
|
|
self.check("dither2mono")
|
|
|
|
self.check("dither2grey2")
|
|
|
|
self.check("mono2grey", 0, 0)
|
|
|
|
self.check("grey22grey")
|
|
|
|
self.check("rgb2rgb8") # nlen*4 == len
|
|
|
|
self.check("rgb82rgb")
|
|
|
|
self.check("rgb2grey")
|
|
|
|
self.check("grey2rgb")
|
|
|
|
|
2006-02-18 00:14:16 -04:00
|
|
|
|
2008-03-03 00:19:29 -04:00
|
|
|
def test_main():
|
1996-12-20 16:49:06 -04:00
|
|
|
|
2008-09-29 22:31:49 -03:00
|
|
|
run_unittest(InputValidationTests)
|
|
|
|
|
|
|
|
try:
|
|
|
|
import imgfile
|
|
|
|
except ImportError:
|
|
|
|
return
|
|
|
|
|
1997-04-15 21:30:45 -03:00
|
|
|
# Create binary test files
|
2001-10-24 17:42:55 -03:00
|
|
|
uu.decode(get_qualified_path('testrgb'+os.extsep+'uue'), 'test'+os.extsep+'rgb')
|
1997-04-15 21:30:45 -03:00
|
|
|
|
2007-05-20 04:09:50 -03:00
|
|
|
image, width, height = getimage('test'+os.extsep+'rgb')
|
2000-10-23 14:22:08 -03:00
|
|
|
|
1996-12-20 16:49:06 -04:00
|
|
|
# Return the selected part of image, which should by width by height
|
|
|
|
# in size and consist of pixels of psize bytes.
|
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'crop'
|
1996-12-20 16:49:06 -04:00
|
|
|
newimage = imageop.crop (image, 4, width, height, 0, 0, 1, 1)
|
|
|
|
|
|
|
|
# Return image scaled to size newwidth by newheight. No interpolation
|
|
|
|
# is done, scaling is done by simple-minded pixel duplication or removal.
|
|
|
|
# Therefore, computer-generated images or dithered images will
|
2000-10-23 14:22:08 -03:00
|
|
|
# not look nice after scaling.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'scale'
|
1996-12-20 16:49:06 -04:00
|
|
|
scaleimage = imageop.scale(image, 4, width, height, 1, 1)
|
|
|
|
|
|
|
|
# Run a vertical low-pass filter over an image. It does so by computing
|
|
|
|
# each destination pixel as the average of two vertically-aligned source
|
|
|
|
# pixels. The main use of this routine is to forestall excessive flicker
|
2000-10-23 14:22:08 -03:00
|
|
|
# if the image two vertically-aligned source pixels, hence the name.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'tovideo'
|
1996-12-20 16:49:06 -04:00
|
|
|
videoimage = imageop.tovideo (image, 4, width, height)
|
1997-01-15 16:07:07 -04:00
|
|
|
|
1997-01-15 16:58:55 -04:00
|
|
|
# Convert an rgb image to an 8 bit rgb
|
1997-01-15 16:07:07 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'rgb2rgb8'
|
1997-01-15 16:07:07 -04:00
|
|
|
greyimage = imageop.rgb2rgb8(image, width, height)
|
|
|
|
|
|
|
|
# Convert an 8 bit rgb image to a 24 bit rgb image
|
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'rgb82rgb'
|
1997-01-15 16:07:07 -04:00
|
|
|
image = imageop.rgb82rgb(greyimage, width, height)
|
2000-10-23 14:22:08 -03:00
|
|
|
|
1997-01-15 16:58:55 -04:00
|
|
|
# Convert an rgb image to an 8 bit greyscale image
|
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'rgb2grey'
|
1997-01-15 16:58:55 -04:00
|
|
|
greyimage = imageop.rgb2grey(image, width, height)
|
|
|
|
|
|
|
|
# Convert an 8 bit greyscale image to a 24 bit rgb image
|
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'grey2rgb'
|
1997-01-15 16:58:55 -04:00
|
|
|
image = imageop.grey2rgb(greyimage, width, height)
|
2000-10-23 14:22:08 -03:00
|
|
|
|
1996-12-20 16:49:06 -04:00
|
|
|
# Convert a 8-bit deep greyscale image to a 1-bit deep image by
|
2000-07-16 09:04:32 -03:00
|
|
|
# thresholding all the pixels. The resulting image is tightly packed
|
2000-10-23 14:22:08 -03:00
|
|
|
# and is probably only useful as an argument to mono2grey.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'grey2mono'
|
2000-10-23 14:22:08 -03:00
|
|
|
monoimage = imageop.grey2mono (greyimage, width, height, 0)
|
1996-12-20 16:49:06 -04:00
|
|
|
|
1997-01-15 16:07:07 -04:00
|
|
|
# monoimage, width, height = getimage('monotest.rgb')
|
1996-12-20 16:49:06 -04:00
|
|
|
# Convert a 1-bit monochrome image to an 8 bit greyscale or color image.
|
|
|
|
# All pixels that are zero-valued on input get value p0 on output and
|
|
|
|
# all one-value input pixels get value p1 on output. To convert a
|
|
|
|
# monochrome black-and-white image to greyscale pass the values 0 and
|
|
|
|
# 255 respectively.
|
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'mono2grey'
|
1996-12-20 16:49:06 -04:00
|
|
|
greyimage = imageop.mono2grey (monoimage, width, height, 0, 255)
|
|
|
|
|
|
|
|
# Convert an 8-bit greyscale image to a 1-bit monochrome image using a
|
|
|
|
# (simple-minded) dithering algorithm.
|
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'dither2mono'
|
1996-12-20 16:49:06 -04:00
|
|
|
monoimage = imageop.dither2mono (greyimage, width, height)
|
|
|
|
|
|
|
|
# Convert an 8-bit greyscale image to a 4-bit greyscale image without
|
2000-10-23 14:22:08 -03:00
|
|
|
# dithering.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'grey2grey4'
|
2000-10-23 14:22:08 -03:00
|
|
|
grey4image = imageop.grey2grey4 (greyimage, width, height)
|
1996-12-20 16:49:06 -04:00
|
|
|
|
|
|
|
# Convert an 8-bit greyscale image to a 2-bit greyscale image without
|
2000-10-23 14:22:08 -03:00
|
|
|
# dithering.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'grey2grey2'
|
2000-10-23 14:22:08 -03:00
|
|
|
grey2image = imageop.grey2grey2 (greyimage, width, height)
|
1996-12-20 16:49:06 -04:00
|
|
|
|
|
|
|
# Convert an 8-bit greyscale image to a 2-bit greyscale image with
|
|
|
|
# dithering. As for dither2mono, the dithering algorithm is currently
|
2000-10-23 14:22:08 -03:00
|
|
|
# very simple.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'dither2grey2'
|
2000-10-23 14:22:08 -03:00
|
|
|
grey2image = imageop.dither2grey2 (greyimage, width, height)
|
1996-12-20 16:49:06 -04:00
|
|
|
|
2000-10-23 14:22:08 -03:00
|
|
|
# Convert a 4-bit greyscale image to an 8-bit greyscale image.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'grey42grey'
|
2000-10-23 14:22:08 -03:00
|
|
|
greyimage = imageop.grey42grey (grey4image, width, height)
|
1996-12-20 16:49:06 -04:00
|
|
|
|
2000-10-23 14:22:08 -03:00
|
|
|
# Convert a 2-bit greyscale image to an 8-bit greyscale image.
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'grey22grey'
|
1997-04-15 21:30:45 -03:00
|
|
|
image = imageop.grey22grey (grey2image, width, height)
|
|
|
|
|
|
|
|
# Cleanup
|
2001-10-24 17:42:55 -03:00
|
|
|
unlink('test'+os.extsep+'rgb')
|
1996-12-20 16:49:06 -04:00
|
|
|
|
|
|
|
def getimage(name):
|
|
|
|
"""return a tuple consisting of
|
|
|
|
image (in 'imgfile' format) width and height
|
|
|
|
"""
|
2010-02-06 19:06:24 -04:00
|
|
|
import imgfile
|
1996-12-20 16:49:06 -04:00
|
|
|
try:
|
1998-03-26 15:42:58 -04:00
|
|
|
sizes = imgfile.getsizes(name)
|
1996-12-20 16:49:06 -04:00
|
|
|
except imgfile.error:
|
1998-03-26 15:42:58 -04:00
|
|
|
name = get_qualified_path(name)
|
|
|
|
sizes = imgfile.getsizes(name)
|
1996-12-20 16:49:06 -04:00
|
|
|
if verbose:
|
1998-03-26 15:42:58 -04:00
|
|
|
print 'imgfile opening test image: %s, sizes: %s' % (name, str(sizes))
|
1996-12-20 16:49:06 -04:00
|
|
|
|
|
|
|
image = imgfile.read(name)
|
|
|
|
return (image, sizes[0], sizes[1])
|
|
|
|
|
1997-01-15 16:07:07 -04:00
|
|
|
def get_qualified_path(name):
|
1997-05-14 15:57:21 -03:00
|
|
|
""" return a more qualified path to name"""
|
1997-01-15 16:07:07 -04:00
|
|
|
import sys
|
|
|
|
import os
|
1997-10-01 19:10:32 -03:00
|
|
|
path = sys.path
|
|
|
|
try:
|
1998-03-26 15:42:58 -04:00
|
|
|
path = [os.path.dirname(__file__)] + path
|
1997-10-01 19:10:32 -03:00
|
|
|
except NameError:
|
1998-03-26 15:42:58 -04:00
|
|
|
pass
|
1997-10-01 19:10:32 -03:00
|
|
|
for dir in path:
|
1998-03-26 15:42:58 -04:00
|
|
|
fullname = os.path.join(dir, name)
|
|
|
|
if os.path.exists(fullname):
|
|
|
|
return fullname
|
1997-01-15 16:07:07 -04:00
|
|
|
return name
|
|
|
|
|
2008-03-03 00:19:29 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|