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