/Lib/plat-irix5/jpeg.py

http://unladen-swallow.googlecode.com/ · Python · 114 lines · 97 code · 10 blank · 7 comment · 21 complexity · 4f202ebacf62557f431e713010443f0a MD5 · raw file

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