Skip to content

Commit cacdc08

Browse files
committed
Simple example
1 parent 815ed35 commit cacdc08

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

Examples/Csharp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ set(CSHARP_EXAMPLES
1717
NewSequence
1818
GenerateDICOMDIR
1919
StandardizeFiles
20+
ExplicitLittleEndian
2021
ReformatFile
2122
CompressLossyJPEG
2223
SendFileSCU
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*=========================================================================
2+
3+
Program: GDCM (Grassroots DICOM). A DICOM library
4+
5+
Copyright (c) 2006-2011 Mathieu Malaterre
6+
All rights reserved.
7+
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8+
9+
This software is distributed WITHOUT ANY WARRANTY; without even
10+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11+
PURPOSE. See the above copyright notice for more information.
12+
13+
=========================================================================*/
14+
15+
/*
16+
* Simple C# example to show how one would 'Standardize' a DICOM File-Set
17+
*
18+
* Usage:
19+
* $ export LD_LIBRARY_PATH=$HOME/Projects/gdcm/debug-gcc/bin
20+
* $ mono bin/StandardizeFiles.exe input_path output_path
21+
*/
22+
using System;
23+
using gdcm;
24+
25+
public class ExplicitLittleEndian
26+
{
27+
public static bool ToExplicitLittleEndian(string aSrc, string aDst)
28+
{
29+
using (ImageReader reader = new ImageReader())
30+
{
31+
bool b = System.IO.File.Exists(aSrc);
32+
reader.SetFileName(aSrc);
33+
if (!reader.Read())
34+
{
35+
throw new System.Exception(string.Format("Cannot read '{0}'", aSrc));
36+
}
37+
using (FileExplicitFilter fef = new FileExplicitFilter())
38+
{
39+
fef.SetChangePrivateTags(false);
40+
fef.SetFile(reader.GetFile());
41+
if (!fef.Change())
42+
{
43+
throw new System.Exception(string.Format("Cannot make explicit '{0}'", aSrc));
44+
}
45+
using (var syntax = new TransferSyntax(TransferSyntax.TSType.ExplicitVRLittleEndian))
46+
{
47+
using (ImageChangeTransferSyntax tsc = new ImageChangeTransferSyntax())
48+
{
49+
tsc.SetTransferSyntax(syntax);
50+
tsc.SetInput(reader.GetImage());
51+
tsc.SetForce(true);
52+
if (!tsc.Change())
53+
{
54+
throw new System.Exception(string.Format("Cannot change '{0}'", aSrc));
55+
}
56+
using (var writer = new ImageWriter())
57+
{
58+
writer.SetFile(fef.GetFile());
59+
writer.SetImage(tsc.GetOutput());
60+
writer.SetFileName(aDst);
61+
if (!writer.Write())
62+
{
63+
throw new System.Exception(string.Format("Cannot write to '{0}'", aDst));
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
70+
return true;
71+
}
72+
73+
public static int Main(string[] args)
74+
{
75+
gdcm.FileMetaInformation.SetSourceApplicationEntityTitle( "My Standardize App" );
76+
77+
// http://www.oid-info.com/get/1.3.6.1.4.17434
78+
string THERALYS_ORG_ROOT = "1.3.6.1.4.17434";
79+
gdcm.UIDGenerator.SetRoot( THERALYS_ORG_ROOT );
80+
System.Console.WriteLine( "Root dir is now: " + gdcm.UIDGenerator.GetRoot() );
81+
82+
string dir1 = args[0];
83+
string dir2 = args[1];
84+
85+
// Check input is valid:
86+
if( !gdcm.PosixEmulation.FileIsDirectory(dir1) )
87+
{
88+
System.Console.WriteLine( "Input directory: " + dir1 + " does not exist. Sorry" );
89+
return 1;
90+
}
91+
if( !gdcm.PosixEmulation.FileIsDirectory(dir2) )
92+
{
93+
System.Console.WriteLine( "Output directory: " + dir2 + " does not exist. Sorry" );
94+
return 1;
95+
}
96+
97+
Directory d = new Directory();
98+
uint nfiles = d.Load( dir1, true );
99+
if(nfiles == 0) return 1;
100+
101+
// Process all filenames:
102+
FilenamesType filenames = d.GetFilenames();
103+
for( uint i = 0; i < nfiles; ++i )
104+
{
105+
string filename = filenames[ (int)i ];
106+
string outfilename = filename.Replace( dir1, dir2 );
107+
System.Console.WriteLine( "Filename: " + filename );
108+
System.Console.WriteLine( "Out Filename: " + outfilename );
109+
if( !ToExplicitLittleEndian( filename, outfilename ) )
110+
{
111+
System.Console.WriteLine( "Could not process filename: " + filename );
112+
//return 1;
113+
}
114+
}
115+
116+
117+
return 0;
118+
}
119+
}

0 commit comments

Comments
 (0)