blob: 327726d631b21649195de3febd3cb68625c32176 [file] [log] [blame]
Roger E. Masse47e98301996-12-20 20:49:06 +00001#! /usr/bin/env python
2"""Test script for the imageop module. This has the side
3 effect of partially testing the imgfile module as well.
4 Roger E. Masse
5"""
6from test_support import verbose
7
8import imageop
9
Roger E. Massee17ec8d1997-01-15 20:07:07 +000010def main(use_rgbimg=1):
Roger E. Masse47e98301996-12-20 20:49:06 +000011
Roger E. Massee17ec8d1997-01-15 20:07:07 +000012 if use_rgbimg:
13 image, width, height = getrgbimage('test.rgb')
14 else:
15 image, width, height = getimage('test.rgb')
16
Roger E. Masse47e98301996-12-20 20:49:06 +000017 # Return the selected part of image, which should by width by height
18 # in size and consist of pixels of psize bytes.
19 if verbose:
20 print 'crop'
21 newimage = imageop.crop (image, 4, width, height, 0, 0, 1, 1)
22
23 # Return image scaled to size newwidth by newheight. No interpolation
24 # is done, scaling is done by simple-minded pixel duplication or removal.
25 # Therefore, computer-generated images or dithered images will
26 # not look nice after scaling.
27 if verbose:
28 print 'scale'
29 scaleimage = imageop.scale(image, 4, width, height, 1, 1)
30
31 # Run a vertical low-pass filter over an image. It does so by computing
32 # each destination pixel as the average of two vertically-aligned source
33 # pixels. The main use of this routine is to forestall excessive flicker
34 # if the image two vertically-aligned source pixels, hence the name.
35 if verbose:
36 print 'tovideo'
37 videoimage = imageop.tovideo (image, 4, width, height)
Roger E. Massee17ec8d1997-01-15 20:07:07 +000038
39 # Convert an rgb image to an 8 bit rgb (greyscale)
40 if verbose:
41 print 'rgb2rgb8'
42 greyimage = imageop.rgb2rgb8(image, width, height)
43
44 # Convert an 8 bit rgb image to a 24 bit rgb image
45 if verbose:
46 print 'rgb82rgb'
47 image = imageop.rgb82rgb(greyimage, width, height)
Roger E. Masse47e98301996-12-20 20:49:06 +000048
Roger E. Masse47e98301996-12-20 20:49:06 +000049 # Convert a 8-bit deep greyscale image to a 1-bit deep image by
50 # tresholding all the pixels. The resulting image is tightly packed
51 # and is probably only useful as an argument to mono2grey.
52 if verbose:
53 print 'grey2mono'
Roger E. Massee17ec8d1997-01-15 20:07:07 +000054 monoimage = imageop.grey2mono (greyimage, width, height, 0)
Roger E. Masse47e98301996-12-20 20:49:06 +000055
Roger E. Massee17ec8d1997-01-15 20:07:07 +000056 # monoimage, width, height = getimage('monotest.rgb')
Roger E. Masse47e98301996-12-20 20:49:06 +000057 # Convert a 1-bit monochrome image to an 8 bit greyscale or color image.
58 # All pixels that are zero-valued on input get value p0 on output and
59 # all one-value input pixels get value p1 on output. To convert a
60 # monochrome black-and-white image to greyscale pass the values 0 and
61 # 255 respectively.
62 if verbose:
63 print 'mono2grey'
64 greyimage = imageop.mono2grey (monoimage, width, height, 0, 255)
65
66 # Convert an 8-bit greyscale image to a 1-bit monochrome image using a
67 # (simple-minded) dithering algorithm.
68 if verbose:
69 print 'dither2mono'
70 monoimage = imageop.dither2mono (greyimage, width, height)
71
Roger E. Masse47e98301996-12-20 20:49:06 +000072 # Convert an 8-bit greyscale image to a 4-bit greyscale image without
73 # dithering.
74 if verbose:
75 print 'grey2grey4'
76 grey4image = imageop.grey2grey4 (greyimage, width, height)
77
78 # Convert an 8-bit greyscale image to a 2-bit greyscale image without
79 # dithering.
80 if verbose:
81 print 'grey2grey2'
82 grey2image = imageop.grey2grey2 (greyimage, width, height)
83
84 # Convert an 8-bit greyscale image to a 2-bit greyscale image with
85 # dithering. As for dither2mono, the dithering algorithm is currently
86 # very simple.
87 if verbose:
88 print 'dither2grey2'
89 grey2image = imageop.dither2grey2 (greyimage, width, height)
90
91 # Convert a 4-bit greyscale image to an 8-bit greyscale image.
92 if verbose:
93 print 'grey42grey'
94 greyimage = imageop.grey42grey (grey4image, width, height)
95
96 # Convert a 2-bit greyscale image to an 8-bit greyscale image.
97 if verbose:
98 print 'grey22grey'
99 image = imageop.grey22grey (grey2image, width, height)
100
Roger E. Massee17ec8d1997-01-15 20:07:07 +0000101def getrgbimage(name):
102 """return a tuple consisting of image (in 'imgfile' format but
103 using rgbimg instead) width and height"""
104
105 import rgbimg
106
107 try:
108 sizes = rgbimg.sizeofimage(name)
109 except rgbimg.error:
110 name = get_qualified_path(name)
111 sizes = rgbimg.sizeofimage(name)
112 if verbose:
113 print 'rgbimg opening test image: %s, sizes: %s' % (name, str(sizes))
114
115 image = rgbimg.longimagedata(name)
116 return (image, sizes[0], sizes[1])
117
Roger E. Masse47e98301996-12-20 20:49:06 +0000118def getimage(name):
119 """return a tuple consisting of
120 image (in 'imgfile' format) width and height
121 """
122
Roger E. Masse47e98301996-12-20 20:49:06 +0000123 import imgfile
Roger E. Massee17ec8d1997-01-15 20:07:07 +0000124
Roger E. Masse47e98301996-12-20 20:49:06 +0000125 try:
126 sizes = imgfile.getsizes(name)
127 except imgfile.error:
Roger E. Massee17ec8d1997-01-15 20:07:07 +0000128 name = get_qualified_path(name)
Roger E. Masse47e98301996-12-20 20:49:06 +0000129 sizes = imgfile.getsizes(name)
130 if verbose:
Roger E. Massee17ec8d1997-01-15 20:07:07 +0000131 print 'imgfile opening test image: %s, sizes: %s' % (name, str(sizes))
Roger E. Masse47e98301996-12-20 20:49:06 +0000132
133 image = imgfile.read(name)
134 return (image, sizes[0], sizes[1])
135
Roger E. Massee17ec8d1997-01-15 20:07:07 +0000136def get_qualified_path(name):
137 """ return a more qualified path to name contructed from argv[1]"""
138 import sys
139 import os
140 import string
141
142 # get a more qualified path component of the script...
143 if __name__ == '__main__':
144 ourname = sys.argv[0]
145 else: # ...or the full path of the module
146 ourname = sys.modules[__name__].__file__
147
148 parts = string.splitfields(ourname, os.sep)
149 parts[-1] = name
150 name = string.joinfields(parts, os.sep)
151 return name
152
153# rgbimg (unlike imgfile) is portable to platforms other than SGI. So we prefer to use it.
154main(use_rgbimg=1)
155