1 # Implement 'jpeg' interface using SGI's compression library
3 # XXX Options 'smooth' and 'optimize' are ignored.
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)
11 class error(Exception):
14 options
= {'quality': 75, 'optimize': 0, 'smooth': 0, 'forcegray': 0}
19 def compress(imgdata
, width
, height
, bytesperpixel
):
22 if comp
is None: comp
= cl
.OpenCompressor(cl
.JPEG
)
23 if bytesperpixel
== 1:
25 elif bytesperpixel
== 4:
27 if options
['forcegray']:
28 iformat
= cl
.GRAYSCALE
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
, \
38 comp
.SetParams(params
)
39 jpegdata
= comp
.Compress(1, imgdata
)
42 def decompress(jpegdata
):
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']:
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
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':
69 if not options
.has_key(name
):
70 raise KeyError, 'jpeg.setoption: unknown option name'
71 options
[name
] = int(value
)
75 if sys
.argv
[1:2] == ['-g']:
77 setoption('forcegray', 1)
79 sys
.argv
.append('/usr/local/images/data/jpg/asterix.jpg')
80 for file in sys
.argv
[1:]:
85 jpegdata
= open(file, 'r').read()
86 imgdata
, width
, height
, bytesperpixel
= decompress(jpegdata
)
88 gl
.prefsize(width
, height
)
89 win
= gl
.winopen(file)
90 if bytesperpixel
== 1:
92 gl
.pixmode(GL
.PM_SIZE
, 8)
95 gl
.mapcolor(i
, i
, i
, i
)
98 gl
.pixmode(GL
.PM_SIZE
, 32)
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
)
106 dev
, val
= gl
.qread()
107 if dev
in (DEVICE
.ESCKEY
, DEVICE
.WINSHUT
, DEVICE
.WINQUIT
):
109 if dev
== DEVICE
.REDRAW
:
110 gl
.lrectwrite(0, 0, width
-1, height
-1, imgdata
)
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
)