blob: c1ed669f97c5ea845207fbe2cd5669189c347917 [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
8error = 'jpeg.error' # Exception
9
10options = {'quality': 75, 'optimize': 0, 'smooth': 0, 'forcegray': 0}
11
12comp = None
13decomp = None
14
15def compress(imgdata, width, height, bytesperpixel):
16 global comp
Guido van Rossum2c2f7311998-08-07 15:28:23 +000017 import cl
18 if comp is None: comp = cl.OpenCompressor(cl.JPEG)
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000019 if bytesperpixel == 1:
Guido van Rossum2c2f7311998-08-07 15:28:23 +000020 format = cl.GRAYSCALE
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000021 elif bytesperpixel == 4:
Guido van Rossum2c2f7311998-08-07 15:28:23 +000022 format = cl.RGBX
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000023 if options['forcegray']:
Guido van Rossum2c2f7311998-08-07 15:28:23 +000024 iformat = cl.GRAYSCALE
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000025 else:
Guido van Rossum2c2f7311998-08-07 15:28:23 +000026 iformat = cl.YUV
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000027 # XXX How to support 'optimize'?
Guido van Rossum2c2f7311998-08-07 15:28:23 +000028 params = [cl.IMAGE_WIDTH, width, cl.IMAGE_HEIGHT, height,
29 cl.ORIGINAL_FORMAT, format,
30 cl.ORIENTATION, cl.BOTTOM_UP,
31 cl.QUALITY_FACTOR, options['quality'],
32 cl.INTERNAL_FORMAT, iformat,
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000033 ]
34 comp.SetParams(params)
35 jpegdata = comp.Compress(1, imgdata)
36 return jpegdata
37
38def decompress(jpegdata):
39 global decomp
Guido van Rossum2c2f7311998-08-07 15:28:23 +000040 import cl
41 if decomp is None: decomp = cl.OpenDecompressor(cl.JPEG)
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000042 headersize = decomp.ReadHeader(jpegdata)
Guido van Rossum2c2f7311998-08-07 15:28:23 +000043 params = [cl.IMAGE_WIDTH, 0, cl.IMAGE_HEIGHT, 0, cl.INTERNAL_FORMAT, 0]
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000044 decomp.GetParams(params)
45 width, height, format = params[1], params[3], params[5]
Guido van Rossum2c2f7311998-08-07 15:28:23 +000046 if format == cl.GRAYSCALE or options['forcegray']:
47 format = cl.GRAYSCALE
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000048 bytesperpixel = 1
49 else:
Guido van Rossum2c2f7311998-08-07 15:28:23 +000050 format = cl.RGBX
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000051 bytesperpixel = 4
52 # XXX How to support 'smooth'?
Guido van Rossum2c2f7311998-08-07 15:28:23 +000053 params = [cl.ORIGINAL_FORMAT, format,
54 cl.ORIENTATION, cl.BOTTOM_UP,
55 cl.FRAME_BUFFER_SIZE, width*height*bytesperpixel]
Guido van Rossum1ce7c6f1997-01-15 19:19:19 +000056 decomp.SetParams(params)
57 imgdata = decomp.Decompress(1, jpegdata)
58 return imgdata, width, height, bytesperpixel
59
60def setoption(name, value):
61 if type(value) <> type(0):
62 raise TypeError, 'jpeg.setoption: numeric options only'
63 if name == 'forcegrey':
64 name = 'forcegray'
65 if not options.has_key(name):
66 raise KeyError, 'jpeg.setoption: unknown option name'
67 options[name] = int(value)
68
69def test():
70 import sys
71 if sys.argv[1:2] == ['-g']:
72 del sys.argv[1]
73 setoption('forcegray', 1)
74 if not sys.argv[1:]:
75 sys.argv.append('/usr/local/images/data/jpg/asterix.jpg')
76 for file in sys.argv[1:]:
77 show(file)
78
79def show(file):
80 import gl, GL, DEVICE
81 jpegdata = open(file, 'r').read()
82 imgdata, width, height, bytesperpixel = decompress(jpegdata)
83 gl.foreground()
84 gl.prefsize(width, height)
85 win = gl.winopen(file)
86 if bytesperpixel == 1:
87 gl.cmode()
88 gl.pixmode(GL.PM_SIZE, 8)
89 gl.gconfig()
90 for i in range(256):
91 gl.mapcolor(i, i, i, i)
92 else:
93 gl.RGBmode()
94 gl.pixmode(GL.PM_SIZE, 32)
95 gl.gconfig()
96 gl.qdevice(DEVICE.REDRAW)
97 gl.qdevice(DEVICE.ESCKEY)
98 gl.qdevice(DEVICE.WINQUIT)
99 gl.qdevice(DEVICE.WINSHUT)
100 gl.lrectwrite(0, 0, width-1, height-1, imgdata)
101 while 1:
102 dev, val = gl.qread()
103 if dev in (DEVICE.ESCKEY, DEVICE.WINSHUT, DEVICE.WINQUIT):
104 break
105 if dev == DEVICE.REDRAW:
106 gl.lrectwrite(0, 0, width-1, height-1, imgdata)
107 gl.winclose(win)
108 # Now test the compression and write the result to a fixed filename
109 newjpegdata = compress(imgdata, width, height, bytesperpixel)
110 open('/tmp/j.jpg', 'w').write(newjpegdata)