home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Demo / sgi / cl / imageview.py next >
Text File  |  1993-02-19  |  2KB  |  64 lines

  1. # View a compressed image file in a GL window.
  2. #
  3. # This program shows how to use the Compression Library to view a
  4. # compressed image and how to get relevant parameters from the image file.
  5. #
  6. # Instead of specifying the CL.ORIENTATION as CL.BOTTOM_UP, we could
  7. # also have used the call gl.pixmode(GL.PM_TTOB, 1)
  8. #
  9. # An alternative way of decompressing a compressed image would be to use
  10. # DecompressImage(), but this requires that the size of the image be
  11. # known beforehand.  The call would be:
  12. # image = cl.DecompressImage(scheme, width, height, CL.RGBX, filep.read())
  13.  
  14. def imageview(file):
  15.     import cl, CL, gl, GL, DEVICE
  16.  
  17.     filep = open(file, 'r')
  18.     header = filep.read(16)
  19.     filep.seek(0)
  20.     scheme = cl.QueryScheme(header)
  21.     decomp = cl.OpenDecompressor(scheme)
  22.     headersize = cl.QueryMaxHeaderSize(scheme)
  23.     header = filep.read(headersize)
  24.     filep.seek(0)
  25.     headersize = decomp.ReadHeader(header)
  26.     width = decomp.GetParam(CL.IMAGE_WIDTH)
  27.     height = decomp.GetParam(CL.IMAGE_HEIGHT)
  28.     params = [CL.ORIGINAL_FORMAT, CL.RGBX, \
  29.           CL.ORIENTATION, CL.BOTTOM_UP, \
  30.           CL.FRAME_BUFFER_SIZE, width*height*CL.BytesPerPixel(CL.RGBX)]
  31.     decomp.SetParams(params)
  32.  
  33.     image = decomp.Decompress(1, filep.read())
  34.     filep.close()
  35.     decomp.CloseDecompressor()
  36.  
  37.     gl.foreground()
  38.     gl.prefsize(width, height)
  39.     win = gl.winopen(file)
  40.     gl.RGBmode()
  41.     gl.pixmode(GL.PM_SIZE, 32)
  42.     gl.gconfig()
  43.     gl.qdevice(DEVICE.REDRAW)
  44.     gl.qdevice(DEVICE.ESCKEY)
  45.     gl.qdevice(DEVICE.WINQUIT)
  46.     gl.qdevice(DEVICE.WINSHUT)
  47.     gl.lrectwrite(0, 0, width-1, height-1, image)
  48.     while 1:
  49.         dev, val = gl.qread()
  50.         if dev in (DEVICE.ESCKEY, DEVICE.WINSHUT, DEVICE.WINQUIT):
  51.             break
  52.         if dev == DEVICE.REDRAW:
  53.             gl.lrectwrite(0, 0, width-1, height-1, image)
  54.     gl.winclose(win)
  55.  
  56. def main():
  57.     import sys
  58.     if len(sys.argv) != 2:
  59.         print 'Usage: imageview file'
  60.         sys.exit(2)
  61.     imageview(sys.argv[1])
  62.  
  63. main()
  64.