Skip to content

Commit

Permalink
Utility dcm2str to apply Attributes Format Pattern to dicom file and/…
Browse files Browse the repository at this point in the history
…or command line parameters fix dcm4che#385
  • Loading branch information
gunterze authored and Patrick Peer committed Oct 17, 2019
1 parent 06c58c2 commit 66eddac
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 81 deletions.
16 changes: 8 additions & 8 deletions dcm4che-tool/dcm4che-tool-dcm2str/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
usage: dcm2str -p PATTERN <file(s)|directory(ies)> -s
<[seq/]attr=value>...
usage: dcm2str -p <pattern> [-s <[seq/]attr=value>]..
[<file>..][<directory>..]

Apply Attributes Format Pattern either to one or more DICOM files and/or
DICOM file(s) in one or more directories. If individual attributes are
Expand All @@ -8,7 +8,7 @@
-
Options:
-h,--help display this help and exit
-p <arg> Specify Attributes Format Pattern to be applied.
-p <pattern> Specify Attributes Format Pattern to be applied.
-s <[seq/]attr=value> Specify attributes added to the object(s). It can
be specified by keyword or tag (in hex), e.g.
StudyInstanceUID=1.2.3 or 0020000D=1.2.3.
Expand All @@ -20,31 +20,31 @@
-V,--version output version information and exit

Examples:
=> dcm2str -p {0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}
=> dcm2str -p '{0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}'
image.dcm
Apply Attributes Format Pattern to the specified DICOM file.

=> dcm2str -p {0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}
=> dcm2str -p '{0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}'
image.dcm -sStudyInstanceUID=1.2.3 -sSeriesInstanceUID=1.2.3.4
-sSOPInstanceUID=1.2.3.4.5
Overwrite attributes of the specified DICOM file with specified DICOM
attributes and then apply Attributes Format Pattern to the specified DICOM
file.

=> dcm2str -p {0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}
=> dcm2str -p '{0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}'
image.dcm /path-to-other-DICOM-files-directory -sStudyInstanceUID=1.2.3
-sSeriesInstanceUID=1.2.3.4 -sSOPInstanceUID=1.2.3.4.5
Overwrite attributes of the specified DICOM file and of other DICOM files
in the directory with the specified DICOM attributes and only then apply
Attributes Format Pattern to DICOM file and to other DICOM files in the
directory.

=> dcm2str -p {0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}
=> dcm2str -p '{0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}'
image.dcm /path-to-other-DICOM-files-directory
Apply Attributes Format Pattern to the specified DICOM file and also to
the other DICOM files in the specified directory.

=> dcm2str -p {0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}
=> dcm2str -p '{0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}'
-sStudyInstanceUID=1.2.3 -sSeriesInstanceUID=1.2.3.4
-sSOPInstanceUID=1.2.3.4.5
Apply Attributes Format Pattern to DICOM attributes passed as command line
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,44 @@
import org.dcm4che3.io.DicomInputStream;
import org.dcm4che3.tool.common.CLIUtils;
import org.dcm4che3.util.AttributesFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.MessageFormat;
import java.util.EnumSet;
import java.util.List;
import java.util.ResourceBundle;

/**
* @author Vrinda Nayak <vrinda.nayak@j4care.com>
* @author Gunter Zeilinger <gunterze@gmail.com>
* @since Jan 2019
*/
public class Dcm2Str {
private static final Logger LOG = LoggerFactory.getLogger(Dcm2Str.class);
public class Dcm2Str extends SimpleFileVisitor<Path> {
private static final ResourceBundle rb = ResourceBundle.getBundle("org.dcm4che3.tool.dcm2str.messages");
private static String pattern;
private static String[] uidOptVals;
private static List<String> pathNames;
private final AttributesFormat format;
private final Attributes cliAttrs;

public Dcm2Str(AttributesFormat format, Attributes cliAttrs) {
this.format = format;
this.cliAttrs = cliAttrs;
}

public static void main(String[] args) {
try {
CommandLine cl = parseComandLine(args);
init(cl);
dcm2str();
AttributesFormat format = new AttributesFormat(cl.getOptionValue("p"));
Attributes cliAttrs = new Attributes();
CLIUtils.addAttributes(cliAttrs, cl.getOptionValues("s"));
List<String> pathNames = cl.getArgList();
if (pathNames.isEmpty())
System.out.println(format.format(cliAttrs));
else
for (String pathName : pathNames)
Files.walkFileTree(Paths.get(pathName),
EnumSet.of(FileVisitOption.FOLLOW_LINKS),
Integer.MAX_VALUE,
new Dcm2Str(format, cliAttrs));
} catch (ParseException e) {
System.err.println("dcm2str: " + e.getMessage());
System.err.println(rb.getString("try"));
Expand All @@ -87,6 +97,7 @@ private static CommandLine parseComandLine(String[] args) throws ParseException
CLIUtils.addCommonOptions(opts);
opts.addOption(Option.builder("p")
.hasArg()
.argName("pattern")
.desc(rb.getString("pattern"))
.build());
opts.addOption(Option.builder("s")
Expand All @@ -95,52 +106,22 @@ private static CommandLine parseComandLine(String[] args) throws ParseException
.valueSeparator('=')
.desc(rb.getString("str"))
.build());
return CLIUtils.parseComandLine(args, opts, rb, Dcm2Str.class);
}

private static void init(CommandLine cl) throws Exception {
if ((pattern = cl.getOptionValue("p")) == null)
throw new MissingOptionException("Missing Attributes Format pattern");

uidOptVals = cl.getOptionValues("s");
pathNames = cl.getArgList();
CommandLine cl = CLIUtils.parseComandLine(args, opts, rb, Dcm2Str.class);
if (!cl.hasOption("p"))
throw new MissingOptionException(rb.getString("missing-pattern-opt"));
return cl;
}

private static void dcm2str() throws IOException {
for (String pathName : pathNames) {
Path path = Paths.get(pathName);
if (Files.isDirectory(path)) {
try {
Files.walkFileTree(path, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path filePath, BasicFileAttributes attrs1) throws IOException {
convert(filePath);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
LOG.warn(e.getMessage());
}
} else
convert(path);
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
try (DicomInputStream dis = new DicomInputStream(path.toFile())) {
Attributes dataset = dis.readDataset(-1, -1);
dataset.addAll(cliAttrs);
System.out.println(format.format(dataset));
} catch (IOException e) {
System.err.println("Failed to parse DICOM file " + path);
e.printStackTrace();
}
if (pathNames.isEmpty())
convert(null);
}

private static void convert(Path path) throws IOException {
Attributes attrs = toAttributes(path);
CLIUtils.addAttributes(attrs, uidOptVals);
System.out.println(MessageFormat.format(
rb.getString("converted"),
path != null ? path : "",
new AttributesFormat(pattern).format(attrs)));
}

private static Attributes toAttributes(Path path) throws IOException {
return path != null
? new DicomInputStream(new FileInputStream(path.toFile())).readDataset(-1, -1)
: new Attributes();
return FileVisitResult.CONTINUE;
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
usage= dcm2str -p PATTERN <file(s)|directory(ies)> -s <[seq/]attr=value>...
usage= dcm2str -p <pattern> [-s <[seq/]attr=value>].. [<file>..][<directory>..]
try=Try `dcm2str --help' for more information.
description=\n\
Apply Attributes Format Pattern either to one or more DICOM files and/or DICOM file(s) in one or more directories. \
If individual attributes are specified, the attributes of the DICOM file(s) shall be overwritten by the specified \
attributes before the Attributes Format Pattern is applied. \n\-\n\
attributes before the Attributes Format Pattern is applied.\n-\n\
Options:
example=\n\
Examples: \n\
=> dcm2str -p {0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd} image.dcm \n\
Apply Attributes Format Pattern to the specified DICOM file. \n\ \n\
=> dcm2str -p {0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd} image.dcm -sStudyInstanceUID=1.2.3 \
-sSeriesInstanceUID=1.2.3.4 -sSOPInstanceUID=1.2.3.4.5 \n\
Examples:\n\
=> dcm2str -p '{0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}' image.dcm\n\
Apply Attributes Format Pattern to the specified DICOM file.\n\n\
=> dcm2str -p '{0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}' image.dcm -sStudyInstanceUID=1.2.3 \
-sSeriesInstanceUID=1.2.3.4 -sSOPInstanceUID=1.2.3.4.5\n\
Overwrite attributes of the specified DICOM file with specified DICOM attributes and then apply Attributes Format Pattern \
to the specified DICOM file. \n\ \n\
=> dcm2str -p {0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd} image.dcm /path-to-other-DICOM-files-directory \
-sStudyInstanceUID=1.2.3 -sSeriesInstanceUID=1.2.3.4 -sSOPInstanceUID=1.2.3.4.5 \n\
to the specified DICOM file.\n\n\
=> dcm2str -p '{0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}' image.dcm /path-to-other-DICOM-files-directory \
-sStudyInstanceUID=1.2.3 -sSeriesInstanceUID=1.2.3.4 -sSOPInstanceUID=1.2.3.4.5\n\
Overwrite attributes of the specified DICOM file and of other DICOM files in the directory with the specified DICOM \
attributes and only then apply Attributes Format Pattern to DICOM file and to other DICOM files in the directory. \n\ \n\
=> dcm2str -p {0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd} image.dcm /path-to-other-DICOM-files-directory \n\
Apply Attributes Format Pattern to the specified DICOM file and also to the other DICOM files in the specified directory. \n\ \n\
=> dcm2str -p {0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd} -sStudyInstanceUID=1.2.3 -sSeriesInstanceUID=1.2.3.4 \
-sSOPInstanceUID=1.2.3.4.5 \n\
attributes and only then apply Attributes Format Pattern to DICOM file and to other DICOM files in the directory.\n\n\
=> dcm2str -p '{0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}' image.dcm /path-to-other-DICOM-files-directory\n\
Apply Attributes Format Pattern to the specified DICOM file and also to the other DICOM files in the specified directory.\n\n\
=> dcm2str -p '{0020000D,hash}/{0020000E,hash}/{00080018,hash}/{rnd}' -sStudyInstanceUID=1.2.3 -sSeriesInstanceUID=1.2.3.4 \
-sSOPInstanceUID=1.2.3.4.5\n\
Apply Attributes Format Pattern to DICOM attributes passed as command line parameters.
pattern=Specify Attributes Format Pattern to be applied.
str=Specify attributes added to the object(s). It can be specified by keyword or tag (in hex), \
e.g. StudyInstanceUID=1.2.3 or 0020000D=1.2.3. Attributes in nested Datasets can be specified by including the \
keyword/tag value of the sequence attribute, e.g. 00400275/00400009 for Scheduled Procedure Step ID in the Request \
Attributes Sequence.
converted={0} -> {1}
Attributes Sequence.
missing-pattern-opt=Missing required option: p

0 comments on commit 66eddac

Please sign in to comment.