ยปCore Development>Code coverage>Lib/plat-irix5/jpeg.py

Python code coverage for Lib/plat-irix5/jpeg.py

#countcontent
1n/a# Implement 'jpeg' interface using SGI's compression library
2n/a
3n/a# XXX Options 'smooth' and 'optimize' are ignored.
4n/a
5n/a# XXX It appears that compressing grayscale images doesn't work right;
6n/a# XXX the resulting file causes weirdness.
7n/afrom warnings import warnpy3k
8n/awarnpy3k("the jpeg module has been removed in Python 3.0", stacklevel=2)
9n/adel warnpy3k
10n/a
11n/aclass error(Exception):
12n/a pass
13n/a
14n/aoptions = {'quality': 75, 'optimize': 0, 'smooth': 0, 'forcegray': 0}
15n/a
16n/acomp = None
17n/adecomp = None
18n/a
19n/adef compress(imgdata, width, height, bytesperpixel):
20n/a global comp
21n/a import cl
22n/a if comp is None: comp = cl.OpenCompressor(cl.JPEG)
23n/a if bytesperpixel == 1:
24n/a format = cl.GRAYSCALE
25n/a elif bytesperpixel == 4:
26n/a format = cl.RGBX
27n/a if options['forcegray']:
28n/a iformat = cl.GRAYSCALE
29n/a else:
30n/a iformat = cl.YUV
31n/a # XXX How to support 'optimize'?
32n/a params = [cl.IMAGE_WIDTH, width, cl.IMAGE_HEIGHT, height, \
33n/a cl.ORIGINAL_FORMAT, format, \
34n/a cl.ORIENTATION, cl.BOTTOM_UP, \
35n/a cl.QUALITY_FACTOR, options['quality'], \
36n/a cl.INTERNAL_FORMAT, iformat, \
37n/a ]
38n/a comp.SetParams(params)
39n/a jpegdata = comp.Compress(1, imgdata)
40n/a return jpegdata
41n/a
42n/adef decompress(jpegdata):
43n/a global decomp
44n/a import cl
45n/a if decomp is None: decomp = cl.OpenDecompressor(cl.JPEG)
46n/a headersize = decomp.ReadHeader(jpegdata)
47n/a params = [cl.IMAGE_WIDTH, 0, cl.IMAGE_HEIGHT, 0, cl.INTERNAL_FORMAT, 0]
48n/a decomp.GetParams(params)
49n/a width, height, format = params[1], params[3], params[5]
50n/a if format == cl.GRAYSCALE or options['forcegray']:
51n/a format = cl.GRAYSCALE
52n/a bytesperpixel = 1
53n/a else:
54n/a format = cl.RGBX
55n/a bytesperpixel = 4
56n/a # XXX How to support 'smooth'?
57n/a params = [cl.ORIGINAL_FORMAT, format, \
58n/a cl.ORIENTATION, cl.BOTTOM_UP, \
59n/a cl.FRAME_BUFFER_SIZE, width*height*bytesperpixel]
60n/a decomp.SetParams(params)
61n/a imgdata = decomp.Decompress(1, jpegdata)
62n/a return imgdata, width, height, bytesperpixel
63n/a
64n/adef setoption(name, value):
65n/a if type(value) is not type(0):
66n/a raise TypeError, 'jpeg.setoption: numeric options only'
67n/a if name == 'forcegrey':
68n/a name = 'forcegray'
69n/a if not options.has_key(name):
70n/a raise KeyError, 'jpeg.setoption: unknown option name'
71n/a options[name] = int(value)
72n/a
73n/adef test():
74n/a import sys
75n/a if sys.argv[1:2] == ['-g']:
76n/a del sys.argv[1]
77n/a setoption('forcegray', 1)
78n/a if not sys.argv[1:]:
79n/a sys.argv.append('/usr/local/images/data/jpg/asterix.jpg')
80n/a for file in sys.argv[1:]:
81n/a show(file)
82n/a
83n/adef show(file):
84n/a import gl, GL, DEVICE
85n/a jpegdata = open(file, 'r').read()
86n/a imgdata, width, height, bytesperpixel = decompress(jpegdata)
87n/a gl.foreground()
88n/a gl.prefsize(width, height)
89n/a win = gl.winopen(file)
90n/a if bytesperpixel == 1:
91n/a gl.cmode()
92n/a gl.pixmode(GL.PM_SIZE, 8)
93n/a gl.gconfig()
94n/a for i in range(256):
95n/a gl.mapcolor(i, i, i, i)
96n/a else:
97n/a gl.RGBmode()
98n/a gl.pixmode(GL.PM_SIZE, 32)
99n/a gl.gconfig()
100n/a gl.qdevice(DEVICE.REDRAW)
101n/a gl.qdevice(DEVICE.ESCKEY)
102n/a gl.qdevice(DEVICE.WINQUIT)
103n/a gl.qdevice(DEVICE.WINSHUT)
104n/a gl.lrectwrite(0, 0, width-1, height-1, imgdata)
105n/a while 1:
106n/a dev, val = gl.qread()
107n/a if dev in (DEVICE.ESCKEY, DEVICE.WINSHUT, DEVICE.WINQUIT):
108n/a break
109n/a if dev == DEVICE.REDRAW:
110n/a gl.lrectwrite(0, 0, width-1, height-1, imgdata)
111n/a gl.winclose(win)
112n/a # Now test the compression and write the result to a fixed filename
113n/a newjpegdata = compress(imgdata, width, height, bytesperpixel)
114n/a open('/tmp/j.jpg', 'w').write(newjpegdata)