import cherrypy
from flup.server.fcgi import WSGIServer
import sys
import os
CURDIR = os.path.dirname(os.path.abspath(__file__))
import accessdata
try:
testing = sys.argv[1] == "test"
BASEURL = "/"
except IndexError:
BASEURL = "/wado/"
testing = False
REQUIRED = ( 'requestType', 'studyUID', 'seriesUID', 'objectUID' )
OPTIONAL = ( 'contentType', 'charset', 'anonymize', 'annotation',
'rows', 'columns', 'region',
'windowWidth', 'windowCenter', 'frameNumber', 'imageQuality',
'presentationUID', 'presentationSeriesUID', 'transferSyntax' )
INVALID_DICOM = ( 'annotation', 'rows', 'columns', 'region',
'windowWidth', 'windowCenter' )
INVALID_NONDICOM = ( 'anonymize', )
def err(msg):
raise Exception, msg
def check_params(kwargs):
valid = REQUIRED + OPTIONAL
curparams = kwargs.keys()
assert kwargs['requestType'] == "WADO"
for par in curparams:
if par not in valid:
err("Unknown parameter: " + par)
for par in REQUIRED:
if par not in curparams:
err("Missing parameter: " + par)
kwargs['contentType'] = kwargs.get('contentType', 'image/jpeg')
if kwargs['contentType'] == 'application/dicom':
for par in INVALID_DICOM:
if par in curparams:
err(par + " is not valid if contentType is application/dicom")
return
for par in INVALID_NONDICOM:
if par in curparams:
err(par + " is valid only if contentType is application/dicom")
if 'annotation' in curparams:
assert kwargs['annotation'] in ('patient', 'technique')
if 'windowWidth' in curparams:
assert 'windowCenter' in curparams
if 'windowCenter' in curparams:
assert 'windowWidth' in curparams
if 'region' in curparams:
region = kwargs['region'].split(',')
assert len(region) == 4
for val in region:
assert 0.0 <= float(val) <= 1.0
class Wado:
@cherrypy.expose
def index(self, **kwargs):
cherrypy.log(str(kwargs))
check_params(kwargs)
cherrypy.response.headers['Content-Type'] = kwargs['contentType']
cherrypy.response.headers['Pragma'] = 'cache'
if kwargs['contentType'] == "application/dicom":
format = "dicom"
else:
format = kwargs['contentType'].replace('image/', '')
image = accessdata.get(studyUID=kwargs['studyUID'],
seriesUID=kwargs['seriesUID'],
objectUID=kwargs['objectUID'],
format=format)
if kwargs['contentType'] == "application/dicom":
return image.raw()
if 'windowWidth' in kwargs:
image.brightness(kwargs['windowWidth'])
if 'windowCenter' in kwargs:
image.contrast(kwargs['windowCenter'])
if 'region' in kwargs:
left, upper, right, lower = [
float(val) for val in kwargs['region'].split(",")
]
width, height = image.img.size
image.crop(width * left, height * upper,
width * right, height * lower)
if 'rows' in kwargs or 'columns' in kwargs:
image.resize(kwargs['rows'], kwargs['columns'])
return image.dump()
if __name__ == "__main__":
cherrypy.config.update({
'tools.gzip.on': True,
'tools.caching.on': True
})
conf = {}
app = cherrypy.tree.mount(Wado(), BASEURL, config=conf)
if testing:
cherrypy.quickstart(app)
else:
cherrypy.engine.start(blocking=False)
try:
WSGIServer(app).run()
finally:
cherrypy.engine.stop()