-
Notifications
You must be signed in to change notification settings - Fork 0
/
mizer.js
237 lines (194 loc) · 7.53 KB
/
mizer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
const mizer = exports;
const path = require('path');
const fs = require('fs');
//console.log(__dirname);
const _app_path = __dirname;
/*
fs.writeFileSync('mizer.js.txt', "MizerLibs: [" + _app_path + "]" + path.resolve(_app_path, '..', 'app.asar.unpacked', 'libs') + "/", (err) => {
if (err) throw err;
console.log('Error writing mizer.js.txt');
});
*/
let java;
let jarDir;
if (path.extname(_app_path) === '.asar') {
const java_node_modules_dir = path.resolve(_app_path, '..', 'app.asar.unpacked', 'node_modules', 'java')
java = require(java_node_modules_dir)
jarDir = path.resolve(_app_path, '..', 'app.asar.unpacked', 'libs') + "/"
} else {
java = require('java')
jarDir = _app_path + "/libs/"
}
console.log(jarDir);
["classes",
"antlr-runtime-3.5.2.jar",
"antlr4-runtime-4.7.1.jar",
"commons-io-2.6.jar",
"commons-lang3-3.8.1.jar",
"dcm4che-core-2.0.29.jar",
"dcm4che-iod-2.0.29.jar",
"dcm4che-net-2.0.29.jar",
"dicom-edit4-1.0.3.jar",
"dicomtools-1.7.5.jar",
"framework-1.7.5.2.jar",
"guava-20.0.jar",
"java-uuid-generator-3.1.4.jar",
"jcl-over-slf4j-1.7.26.jar",
"log4j-1.2.17.jar",
"mizer-1.1.2.jar",
"dicom-edit6-1.1.1.jar",
"pixelEditor-1.1.1.jar",
"pixelmed-nrg-20200327.jar",
"pixelmed-codec-20200328.jar",
"pixelmed-imageio-20200328.jar",
"jai-imageio-core-1.3.0.jar",
"jai-imageio-jpeg2000-1.3.0.jar",
"commons-compress-1.12.jar",
"commons-codec-1.10.jar",
"reflections-0.9.11.jar",
"slf4j-api-1.7.26.jar",
"slf4j-log4j12-1.7.26.jar",
"spring-core-4.3.17.RELEASE.jar",
"transaction-1.7.5.jar"].forEach(jar => java.classpath.push(jarDir + jar))
const mizers = java.newInstanceSync("java.util.ArrayList");
mizers.addSync(java.newInstanceSync("org.nrg.dcm.edit.mizer.DE4Mizer"));
mizers.addSync(java.newInstanceSync("org.nrg.dicom.dicomedit.mizer.DE6Mizer"));
const mizerService = java.newInstanceSync("org.nrg.dicom.mizer.service.impl.BaseMizerService", mizers);
/**
* Creates a Java Properties object from a hash of values. This object is what the Mizer service expects for
* variables and values to be used during anonymization.
*
* Add more variables to the return from this function by calling:
*
* variables.setPropertySync('variableName', 'variableValue');
*
* @param variables A hash of variable names and values.
*
* @return A Java Properties object containing the submitted names and values.
*/
mizer.getVariables = (variables) => {
const properties = java.newInstanceSync("java.util.Properties");
// console.log('-----------------------------------');
// console.log(variables);
// console.log('-----------------------------------');
if (variables) {
Object.keys(variables).forEach(key => {
properties.setPropertySync(key, variables[key]);
});
}
return properties;
};
/**
* Add variables, such as from {@link #getVariables()} above, to the return from this function by calling
* context.addSync(variables).
*
* @param script The script for which a context should be created.
*
* @return A script context.
*/
mizer.getScriptContext = (script) => {
const context = java.newInstanceSync("org.nrg.dicom.mizer.service.impl.MizerContextWithScript");
context.setScriptSync(script);
return context;
};
/**
* Add variables, such as from {@link #getVariables()} above, to the return from this function by calling
* context.addSync(variables).
*
* @param scripts The scripts for which contexts should be created.
*
* @return A list of script contexts.
*/
mizer.getScriptContexts = (scripts) => {
const contexts = java.newInstanceSync("java.util.ArrayList");
scripts.forEach(script => {
const context = mizer.getScriptContext(script);
contexts.addSync(context);
});
return contexts;
};
/**
* Gets variables that are referenced in the contexts.
*/
mizer.getReferencedVariables = (contexts) => {
//return mizerService.getReferencedVariablesSync(contexts);
const variableMap = {};
const variables = mizerService.getReferencedVariablesSync(contexts);
let itr = variables.iteratorSync();
while (itr.hasNextSync()) {
variable = itr.nextSync();
let initialValue = variable.getInitialValueSync();
let variableValue = initialValue ? initialValue.asStringSync() : "";
variableMap[variable.getNameSync()] = variableValue;
}
// for (let i = 0; i < variables.sizeSync(); i++) {
// console.log('***********************************', variables.sizeSync(), variables[i]);
// let variable = variables.getSync(i);
// let initialValue = variable.getInitialValueSync();
// let variableValue = initialValue ? initialValue.asStringSync() : "";
// variableMap.set(variable.getNameSync(), variableValue);
// }
console.log('************* REFERENCED VARIABLES ************************');
console.log(variableMap);
return variableMap;
};
/**
* Anonymizes the DICOM object source using the supplied scripts. If variables have already been set on the script
* contexts, the variables parameter can be omitted.
*
* @param source The DICOM object to anonymize.
* @param contexts The script contexts to use for anonymization.
* @param variables A Java Properties object to pass for variable substitution.
*/
mizer.anonymize_old = (source, contexts, variables) => {
const dicom = java.newInstanceSync("java.io.File", source);
contexts.forEach(context => context.addSync(variables));
mizerService.anonymizeSync(dicom, contexts);
};
/**
* Anonymizes the DICOM object source using the supplied scripts. If variables have already been set on the script
* contexts, the variables parameter can be omitted.
*
* @param source The DICOM object to anonymize.
* @param contexts The script contexts to use for anonymization.
* @param variables A Java Properties object to pass for variable substitution.
*/
mizer.anonymize = (source, contexts, variables) => {
const dicom = java.newInstanceSync("java.io.File", source);
let itr = contexts.iteratorSync();
while (itr.hasNextSync()) {
context = itr.nextSync();
context.addSync(variables);
//console.log(context);
}
mizerService.anonymizeSync(dicom, contexts);
};
mizer.anonymize_single = (source, script, variables) => {
const properties = java.newInstanceSync("java.util.Properties");
if (variables) {
Object.keys(variables).forEach(key => {
properties.setPropertySync(key, variables[key]);
});
}
const file = java.newInstanceSync("java.io.File", source);
const context = java.newInstanceSync("org.nrg.dicom.mizer.service.impl.MizerContextWithScript", properties);
context.setScriptSync(script);
const list = java.callStaticMethodSync("java.util.Collections", "singletonList", context);
mizerService.anonymizeSync(file, list);
};
mizer.get_scripts_anon_vars = (scripts) => {
const contexts = mizer.getScriptContexts(scripts);
return mizer.getReferencedVariables(contexts);
}
mizer.generateAlterPixelCode = (rectangles) => {
let lines = rectangles.map(rect => {
return `alterPixels["rectangle", "l=${Math.round(rect[0])}, t=${Math.round(rect[1])}, r=${Math.round(rect[2])}, b=${Math.round(rect[3])}", "solid", "v=100"]`;
})
if (lines.length) {
lines.unshift(`version "6.1"`)
}
return lines.join("\n");
}
mizer.isMizerError = (error_message) => {
return error_message && error_message.indexOf('org.nrg.dicom.mizer.exceptions.MizerException') >= 0
}