comparison Resources/GenerateTransferSyntaxes.py @ 3727:090022f1b5e1

auto-generation of primitives to handle transfer syntaxes
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 06 Mar 2020 17:10:03 +0100
parents
children ae31ba2b09a6
comparison
equal deleted inserted replaced
3726:7b7ca203f1a3 3727:090022f1b5e1
1 #!/usr/bin/python
2
3 # Orthanc - A Lightweight, RESTful DICOM Store
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
5 # Department, University Hospital of Liege, Belgium
6 # Copyright (C) 2017-2020 Osimis S.A., Belgium
7 #
8 # This program is free software: you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation, either version 3 of the
11 # License, or (at your option) any later version.
12 #
13 # In addition, as a special exception, the copyright holders of this
14 # program give permission to link the code of its release with the
15 # OpenSSL project's "OpenSSL" library (or with modified versions of it
16 # that use the same license as the "OpenSSL" library), and distribute
17 # the linked executables. You must obey the GNU General Public License
18 # in all respects for all of the code used other than "OpenSSL". If you
19 # modify file(s) with this exception, you may extend this exception to
20 # your version of the file(s), but you are not obligated to do so. If
21 # you do not wish to do so, delete this exception statement from your
22 # version. If you delete this exception statement from all source files
23 # in the program, then also delete it here.
24 #
25 # This program is distributed in the hope that it will be useful, but
26 # WITHOUT ANY WARRANTY; without even the implied warranty of
27 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 # General Public License for more details.
29 #
30 # You should have received a copy of the GNU General Public License
31 # along with this program. If not, see <http://www.gnu.org/licenses/>.
32
33
34 import json
35 import os
36 import re
37 import sys
38
39 BASE = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
40
41
42
43 ## https://www.dicomlibrary.com/dicom/transfer-syntax/
44 ## https://cedocs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=EDICOM_transfer_syntax
45
46
47 with open(os.path.join(BASE, 'Resources', 'DicomTransferSyntaxes.json'), 'r') as f:
48 SYNTAXES = json.loads(f.read())
49
50
51
52 ##
53 ## Generate the "DicomTransferSyntax" enumeration in "Enumerations.h"
54 ##
55
56 path = os.path.join(BASE, 'Core', 'Enumerations.h')
57 with open(path, 'r') as f:
58 a = f.read()
59
60 s = ',\n'.join(map(lambda x: ' DicomTransferSyntax_%s /*!< %s */' % (x['Value'], x['Name']), SYNTAXES))
61
62 a = re.sub('(enum DicomTransferSyntax\s*{)[^}]*?(\s*};)', r'\1\n%s\2' % s, a, re.DOTALL)
63
64 with open(path, 'w') as f:
65 f.write(a)
66
67
68
69 ##
70 ## Generate the "GetTransferSyntaxUid()" function in
71 ## "Enumerations.cpp"
72 ##
73
74 path = os.path.join(BASE, 'Core', 'Enumerations.cpp')
75 with open(path, 'r') as f:
76 a = f.read()
77
78 s = '\n\n'.join(map(lambda x: ' case DicomTransferSyntax_%s:\n return "%s";' % (x['Value'], x['UID']), SYNTAXES))
79 a = re.sub('(GetTransferSyntaxUid\(DicomTransferSyntax.*?\)\s*{\s*switch \([^)]*?\)\s*{)[^}]*?(\s*default:)',
80 r'\1\n%s\2' % s, a, re.DOTALL)
81
82 with open(path, 'w') as f:
83 f.write(a)
84
85
86 ##
87 ## Generate the "GetDcmtkTransferSyntax()" function in
88 ## "FromDcmtkBridge.cpp"
89 ##
90
91 path = os.path.join(BASE, 'Core', 'DicomParsing', 'FromDcmtkBridge.cpp')
92 with open(path, 'r') as f:
93 a = f.read()
94
95 def Format(x):
96 t = ' case DicomTransferSyntax_%s:\n target = %s;\n return true;' % (x['Value'], x['DCMTK'])
97 if 'SinceDCMTK' in x:
98 return '#if DCMTK_VERSION_NUMBER >= %s\n%s\n#endif' % (x['SinceDCMTK'], t)
99 else:
100 return t
101
102 s = '\n\n'.join(map(Format, filter(lambda x: 'DCMTK' in x, SYNTAXES)))
103 a = re.sub('(GetDcmtkTransferSyntax\(E_TransferSyntax.*?\s*DicomTransferSyntax.*?\)\s*{\s*switch \([^)]*?\)\s*{)[^}]*?(\s*default:)',
104 r'\1\n%s\2' % s, a, re.DOTALL)
105
106 with open(path, 'w') as f:
107 f.write(a)