forked from nroduit/weasis-dicom-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranscoder.java
284 lines (262 loc) · 9.95 KB
/
Transcoder.java
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright (c) 2021 Weasis Team and other contributors.
*
* This program and the accompanying materials are made available under the terms of the Eclipse
* Public License 2.0 which is available at https://www.eclipse.org/legal/epl-2.0, or the Apache
* License, Version 2.0 which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package org.dcm4che3.img;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Tag;
import org.dcm4che3.img.op.MaskArea;
import org.dcm4che3.img.stream.DicomFileInputStream;
import org.dcm4che3.img.stream.ImageDescriptor;
import org.dcm4che3.img.util.Editable;
import org.dcm4che3.img.util.SupplierEx;
import org.dcm4che3.io.DicomOutputStream;
import org.opencv.core.CvType;
import org.opencv.core.MatOfInt;
import org.opencv.imgcodecs.Imgcodecs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.weasis.core.util.FileUtil;
import org.weasis.opencv.data.ImageCV;
import org.weasis.opencv.data.PlanarImage;
import org.weasis.opencv.op.ImageProcessor;
/**
* @author Nicolas Roduit
*/
public class Transcoder {
private static final Logger LOGGER = LoggerFactory.getLogger(Transcoder.class);
public enum Format {
JPEG(".jpg", false, false, false, false),
PNG(".png", true, false, false, false),
TIF(".tif", true, false, true, true),
JP2(".jp2", true, false, false, false),
PNM(".pnm", true, false, false, false),
BMP(".bmp", false, false, false, false),
HDR(".hdr", false, false, false, true);
final String extension;
final boolean support16U;
final boolean support16S;
final boolean support32F;
final boolean support64F;
Format(
String ext,
boolean support16U,
boolean support16S,
boolean support32F,
boolean support64F) {
this.extension = ext;
this.support16U = support16U;
this.support16S = support16S;
this.support32F = support32F;
this.support64F = support64F;
}
}
public static final DicomImageReaderSpi dicomImageReaderSpi = new DicomImageReaderSpi();
private static final DicomImageReadParam dicomImageReadParam = new DicomImageReadParam();
static {
dicomImageReadParam.setReleaseImageAfterProcessing(true);
}
/**
* Convert a DICOM image to a standard image with some rendering parameters
*
* @param srcPath the path of the source image
* @param dstPath the path of the destination image or the path of a directory in which the source
* image filename will be used
* @param params the standard image conversion parameters
* @return
* @throws Exception
*/
public static List<Path> dcm2image(Path srcPath, Path dstPath, ImageTranscodeParam params)
throws Exception {
List<Path> outFiles = new ArrayList<>();
Format format = params.getFormat();
DicomImageReader reader = new DicomImageReader(dicomImageReaderSpi);
try (DicomFileInputStream inputStream = new DicomFileInputStream(srcPath)) {
MatOfInt map =
format == Format.JPEG
? new MatOfInt(Imgcodecs.IMWRITE_JPEG_QUALITY, getCompressionRatio(params))
: null;
reader.setInput(inputStream);
int nbFrames = reader.getImageDescriptor().getFrames();
int indexSize = (int) Math.log10(nbFrames);
indexSize = nbFrames > 1 ? indexSize + 1 : 0;
for (int i = 0; i < nbFrames; i++) {
PlanarImage img = reader.getPlanarImage(i, params.getReadParam());
boolean rawImg = isPreserveRawImage(params, format, img.type());
if (rawImg) {
img =
ImageRendering.getRawRenderedImage(
img, reader.getImageDescriptor(), params.getReadParam());
} else {
img =
ImageRendering.getDefaultRenderedImage(
img, reader.getImageDescriptor(), params.getReadParam(), i);
}
Path outPath =
writeImage(
img, FileUtil.getOutputPath(srcPath, dstPath), format, map, i + 1, indexSize);
outFiles.add(outPath);
}
} finally {
reader.dispose();
}
return outFiles;
}
/**
* Convert a DICOM image to another DICOM image with a specific transfer syntax
*
* @param srcPath the path of the source image
* @param dstPath the path of the destination image or the path of a directory in which the source
* image filename will be used
* @param params the DICOM conversion parameters
* @throws IOException if an I/O error occurs
*/
public static Path dcm2dcm(Path srcPath, Path dstPath, DicomTranscodeParam params)
throws IOException {
Path outPath = adaptFileExtension(FileUtil.getOutputPath(srcPath, dstPath), ".dcm", ".dcm");
try {
dcm2dcm(srcPath, Files.newOutputStream(outPath), params);
} catch (Exception e) {
FileUtil.delete(outPath);
throw e;
}
return outPath;
}
/**
* Convert a DICOM image to another DICOM image with a specific transfer syntax
*
* @param srcPath the path of the source image
* @param outputStream the output stream where the transcoded data will be written
* @param params the DICOM conversion parameters
* @throws IOException if an I/O error occurs
*/
public static void dcm2dcm(Path srcPath, OutputStream outputStream, DicomTranscodeParam params)
throws IOException {
DicomImageReader reader = new DicomImageReader(dicomImageReaderSpi);
reader.setInput(new DicomFileInputStream(srcPath));
DicomMetaData dicomMetaData = reader.getStreamMetadata();
Attributes dataSet = new Attributes(dicomMetaData.getDicomObject());
dataSet.remove(Tag.PixelData);
Editable<PlanarImage> mask = getMask(dataSet, params);
DicomImageReadParam dicomParams = params.getReadParam();
if (dicomParams == null) {
dicomParams = dicomImageReadParam;
} else {
dicomParams.setReleaseImageAfterProcessing(true);
}
List<SupplierEx<PlanarImage, IOException>> images =
reader.getLazyPlanarImages(dicomParams, mask);
String dstTsuid = params.getOutputTsuid();
DicomJpegWriteParam writeParams = params.getWriteJpegParam();
ImageDescriptor desc = dicomMetaData.getImageDescriptor();
DicomOutputData imgData = new DicomOutputData(images, desc, dstTsuid);
if (!dstTsuid.equals(imgData.getTsuid())) {
dstTsuid = imgData.getTsuid();
if (!DicomOutputData.isNativeSyntax(dstTsuid)) {
writeParams = DicomJpegWriteParam.buildDicomImageWriteParam(dstTsuid);
}
LOGGER.warn("Transcoding into {} is not possible, decompressing {}", dstTsuid, srcPath);
}
try (DicomOutputStream dos = new DicomOutputStream(outputStream, dstTsuid)) {
dos.writeFileMetaInformation(dataSet.createFileMetaInformation(dstTsuid));
if (DicomOutputData.isNativeSyntax(dstTsuid)) {
imgData.writRawImageData(dos, dataSet);
} else {
int[] jpegWriteParams =
imgData.adaptTagsToCompressedImage(
dataSet, imgData.getFirstImage().get(), desc, writeParams);
imgData.writeCompressedImageData(dos, dataSet, jpegWriteParams);
}
} catch (Exception e) {
LOGGER.error("Transcoding image data", e);
} finally {
reader.dispose();
}
}
public static Editable<PlanarImage> getMaskedImage(MaskArea m) {
if (m != null) {
return img -> {
ImageCV mask = MaskArea.drawShape(img.toMat(), m);
if (img.isReleasedAfterProcessing()) {
img.release();
mask.setReleasedAfterProcessing(true);
}
return mask;
};
}
return null;
}
private static Editable<PlanarImage> getMask(Attributes dataSet, DicomTranscodeParam params) {
String stationName = dataSet.getString(Tag.StationName, "*");
return getMaskedImage(params.getMask(stationName));
}
private static int getCompressionRatio(ImageTranscodeParam params) {
if (params == null) {
return 80;
}
return params.getJpegCompressionQuality().orElse(80);
}
private static boolean isPreserveRawImage(ImageTranscodeParam params, Format format, int cvType) {
if (params == null) {
return false;
}
boolean value = params.isPreserveRawImage().orElse(false);
if (value) {
if (format == Format.HDR || cvType == CvType.CV_8U) {
return true; // Convert all values in double so do not apply W/L
} else if (cvType == CvType.CV_16U) {
return format.support16U;
} else if (cvType == CvType.CV_16S) {
return format.support16S;
} else if (cvType == CvType.CV_32F) {
return format.support32F;
} else if (cvType == CvType.CV_64F) {
return format.support64F;
}
}
return value;
}
private static Path adaptFileExtension(Path path, String inExt, String outExt) {
String fname = path.getFileName().toString();
String suffix = FileUtil.getExtension(fname);
if (suffix.equals(outExt)) {
return path;
}
if (suffix.endsWith(inExt)) {
return FileSystems.getDefault()
.getPath(
path.getParent().toString(),
fname.substring(0, fname.length() - inExt.length()) + outExt);
}
return path.resolveSibling(fname + outExt);
}
private static Path writeImage(
PlanarImage img, Path path, Format ext, MatOfInt map, int index, int indexSize) {
Path outPath = adaptFileExtension(path, ".dcm", ext.extension);
outPath = FileUtil.addFileIndex(outPath, index, indexSize);
if (map == null) {
if (!ImageProcessor.writeImage(img.toMat(), outPath.toFile())) {
LOGGER.error("Cannot Transform to {} {}", ext, img);
FileUtil.delete(outPath);
}
} else {
if (!ImageProcessor.writeImage(img.toMat(), outPath.toFile(), map)) {
LOGGER.error("Cannot Transform to {} {}", ext, img);
FileUtil.delete(outPath);
}
}
return outPath;
}
}