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