home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Lib / sgi / jpeg.py < prev    next >
Text File  |  1993-12-29  |  3KB  |  111 lines

  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.  
  8. error = 'jpeg.error' # Exception
  9.  
  10. options = {'quality': 75, 'optimize': 0, 'smooth': 0, 'forcegray': 0}
  11.  
  12. comp = None
  13. decomp = None
  14.  
  15. def compress(imgdata, width, height, bytesperpixel):
  16.     global comp
  17.     import cl, CL
  18.     if comp is None: comp = cl.OpenCompressor(CL.JPEG)
  19.     if bytesperpixel == 1:
  20.         format = CL.GRAYSCALE
  21.     elif bytesperpixel == 4:
  22.         format = CL.RGBX
  23.     if options['forcegray']:
  24.         iformat = CL.GRAYSCALE
  25.     else:
  26.         iformat = CL.YUV
  27.     # XXX How to support 'optimize'?
  28.     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, \
  33.          ]
  34.     comp.SetParams(params)
  35.     jpegdata = comp.Compress(1, imgdata)
  36.     return jpegdata
  37.  
  38. def decompress(jpegdata):
  39.     global decomp
  40.     import cl, CL
  41.     if decomp is None: decomp = cl.OpenDecompressor(CL.JPEG)
  42.     headersize = decomp.ReadHeader(jpegdata)
  43.     params = [CL.IMAGE_WIDTH, 0, CL.IMAGE_HEIGHT, 0, CL.INTERNAL_FORMAT, 0]
  44.     decomp.GetParams(params)
  45.     width, height, format = params[1], params[3], params[5]
  46.     if format == CL.GRAYSCALE or options['forcegray']:
  47.         format = CL.GRAYSCALE
  48.         bytesperpixel = 1
  49.     else:
  50.         format = CL.RGBX
  51.         bytesperpixel = 4
  52.     # XXX How to support 'smooth'?
  53.     params = [CL.ORIGINAL_FORMAT, format, \
  54.           CL.ORIENTATION, CL.BOTTOM_UP, \
  55.           CL.FRAME_BUFFER_SIZE, width*height*bytesperpixel]
  56.     decomp.SetParams(params)
  57.     imgdata = decomp.Decompress(1, jpegdata)
  58.     return imgdata, width, height, bytesperpixel
  59.  
  60. def 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.  
  69. def 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.  
  79. def 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)
  111.