blob: 2354f40541b7e98e0efd479111be564601562b96 [file] [log] [blame]
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +00001# Implement 'jpeg' interface using SGI's compression library
2
3# XXX Options 'smooth' and 'optimize' are ignored.
4
5# XXX It appears that compressing grayscale images doesn't work right;
6# XXX the resulting file causes weirdness.
7
Fred Drakedef00382000-08-18 14:59:33 +00008class error(Exception):
9 pass
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000010
11options = {'quality': 75, 'optimize': 0, 'smooth': 0, 'forcegray': 0}
12
13comp = None
14decomp = None
15
16def compress(imgdata, width, height, bytesperpixel):
17 global comp
Guido van Rossum2c2f7311998-08-07 15:28:23 +000018 import cl
19 if comp is None: comp = cl.OpenCompressor(cl.JPEG)
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000020 if bytesperpixel == 1:
Guido van Rossum2c2f7311998-08-07 15:28:23 +000021 format = cl.GRAYSCALE
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000022 elif bytesperpixel == 4:
Guido van Rossum2c2f7311998-08-07 15:28:23 +000023 format = cl.RGBX
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000024 if options['forcegray']:
Guido van Rossum2c2f7311998-08-07 15:28:23 +000025 iformat = cl.GRAYSCALE
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000026 else:
Guido van Rossum2c2f7311998-08-07 15:28:23 +000027 iformat = cl.YUV
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000028 # XXX How to support 'optimize'?
Guido van Rossum2c2f7311998-08-07 15:28:23 +000029 params = [cl.IMAGE_WIDTH, width, cl.IMAGE_HEIGHT, height,
30 cl.ORIGINAL_FORMAT, format,
31 cl.ORIENTATION, cl.BOTTOM_UP,
32 cl.QUALITY_FACTOR, options['quality'],
33 cl.INTERNAL_FORMAT, iformat,
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000034 ]
35 comp.SetParams(params)
36 jpegdata = comp.Compress(1, imgdata)
37 return jpegdata
38
39def decompress(jpegdata):
40 global decomp
Guido van Rossum2c2f7311998-08-07 15:28:23 +000041 import cl
42 if decomp is None: decomp = cl.OpenDecompressor(cl.JPEG)
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000043 headersize = decomp.ReadHeader(jpegdata)
Guido van Rossum2c2f7311998-08-07 15:28:23 +000044 params = [cl.IMAGE_WIDTH, 0, cl.IMAGE_HEIGHT, 0, cl.INTERNAL_FORMAT, 0]
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000045 decomp.GetParams(params)
46 width, height, format = params[1], params[3], params[5]
Guido van Rossum2c2f7311998-08-07 15:28:23 +000047 if format == cl.GRAYSCALE or options['forcegray']:
48 format = cl.GRAYSCALE
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000049 bytesperpixel = 1
50 else:
Guido van Rossum2c2f7311998-08-07 15:28:23 +000051 format = cl.RGBX
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000052 bytesperpixel = 4
53 # XXX How to support 'smooth'?
Guido van Rossum2c2f7311998-08-07 15:28:23 +000054 params = [cl.ORIGINAL_FORMAT, format,
55 cl.ORIENTATION, cl.BOTTOM_UP,
56 cl.FRAME_BUFFER_SIZE, width*height*bytesperpixel]
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000057 decomp.SetParams(params)
58 imgdata = decomp.Decompress(1, jpegdata)
59 return imgdata, width, height, bytesperpixel
60
61def setoption(name, value):
Fred Drake132dce22000-12-12 23:11:42 +000062 if type(value) is not type(0):
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000063 raise TypeError, 'jpeg.setoption: numeric options only'
64 if name == 'forcegrey':
65 name = 'forcegray'
66 if not options.has_key(name):
67 raise KeyError, 'jpeg.setoption: unknown option name'
68 options[name] = int(value)
69
70def test():
71 import sys
72 if sys.argv[1:2] == ['-g']:
73 del sys.argv[1]
74 setoption('forcegray', 1)
75 if not sys.argv[1:]:
76 sys.argv.append('/usr/local/images/data/jpg/asterix.jpg')
77 for file in sys.argv[1:]:
78 show(file)
79
80def show(file):
81 import gl, GL, DEVICE
82 jpegdata = open(file, 'r').read()
83 imgdata, width, height, bytesperpixel = decompress(jpegdata)
84 gl.foreground()
85 gl.prefsize(width, height)
86 win = gl.winopen(file)
87 if bytesperpixel == 1:
88 gl.cmode()
89 gl.pixmode(GL.PM_SIZE, 8)
90 gl.gconfig()
91 for i in range(256):
92 gl.mapcolor(i, i, i, i)
93 else:
94 gl.RGBmode()
95 gl.pixmode(GL.PM_SIZE, 32)
96 gl.gconfig()
97 gl.qdevice(DEVICE.REDRAW)
98 gl.qdevice(DEVICE.ESCKEY)
99 gl.qdevice(DEVICE.WINQUIT)
100 gl.qdevice(DEVICE.WINSHUT)
101 gl.lrectwrite(0, 0, width-1, height-1, imgdata)
102 while 1:
103 dev, val = gl.qread()
104 if dev in (DEVICE.ESCKEY, DEVICE.WINSHUT, DEVICE.WINQUIT):
105 break
106 if dev == DEVICE.REDRAW:
107 gl.lrectwrite(0, 0, width-1, height-1, imgdata)
108 gl.winclose(win)
109 # Now test the compression and write the result to a fixed filename
110 newjpegdata = compress(imgdata, width, height, bytesperpixel)
111 open('/tmp/j.jpg', 'w').write(newjpegdata)