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
|
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/*
* Simple C# example to show how one would 'Standardize' a DICOM File-Set
*
* Usage:
* $ export LD_LIBRARY_PATH=$HOME/Projects/gdcm/debug-gcc/bin
* $ mono bin/StandardizeFiles.exe input_path output_path
*/
using System;
using gdcm;
public class StandardizeFiles
{
public static bool ProcessOneFile( string filename, string outfilename )
{
PixmapReader reader = new PixmapReader();
reader.SetFileName( filename );
if( !reader.Read() )
{
System.Console.WriteLine( "Could not read: " + filename );
return false;
}
ImageChangeTransferSyntax change = new ImageChangeTransferSyntax();
change.SetForce( false ); // do we really want to recompress when input is alread compressed in same alg ?
change.SetCompressIconImage( false ); // Keep it simple
change.SetTransferSyntax( new TransferSyntax( TransferSyntax.TSType.JPEG2000Lossless ) );
change.SetInput( reader.GetPixmap() );
if( !change.Change() )
{
System.Console.WriteLine( "Could not change: " + filename );
return false;
}
gdcm.FileMetaInformation fmi = reader.GetFile().GetHeader();
// The following three lines make sure to regenerate any value:
fmi.Remove( new gdcm.Tag(0x0002,0x0012) );
fmi.Remove( new gdcm.Tag(0x0002,0x0013) );
fmi.Remove( new gdcm.Tag(0x0002,0x0016) );
PixmapWriter writer = new PixmapWriter();
writer.SetFileName( outfilename );
writer.SetFile( reader.GetFile() );
gdcm.Pixmap pixout = ((PixmapToPixmapFilter)change).GetOutput();
writer.SetPixmap( pixout );
if( !writer.Write() )
{
System.Console.WriteLine( "Could not write: " + outfilename );
return false;
}
return true;
}
public static int Main(string[] args)
{
gdcm.FileMetaInformation.SetSourceApplicationEntityTitle( "My Standardize App" );
// http://www.oid-info.com/get/1.3.6.1.4.17434
string THERALYS_ORG_ROOT = "1.3.6.1.4.17434";
gdcm.UIDGenerator.SetRoot( THERALYS_ORG_ROOT );
System.Console.WriteLine( "Root dir is now: " + gdcm.UIDGenerator.GetRoot() );
string dir1 = args[0];
string dir2 = args[1];
// Check input is valid:
if( !gdcm.PosixEmulation.FileIsDirectory(dir1) )
{
System.Console.WriteLine( "Input directory: " + dir1 + " does not exist. Sorry" );
return 1;
}
if( !gdcm.PosixEmulation.FileIsDirectory(dir2) )
{
System.Console.WriteLine( "Output directory: " + dir2 + " does not exist. Sorry" );
return 1;
}
Directory d = new Directory();
uint nfiles = d.Load( dir1, true );
if(nfiles == 0) return 1;
// Process all filenames:
FilenamesType filenames = d.GetFilenames();
for( uint i = 0; i < nfiles; ++i )
{
string filename = filenames[ (int)i ];
string outfilename = filename.Replace( dir1, dir2 );
System.Console.WriteLine( "Filename: " + filename );
System.Console.WriteLine( "Out Filename: " + outfilename );
if( !ProcessOneFile( filename, outfilename ) )
{
System.Console.WriteLine( "Could not process filename: " + filename );
//return 1;
}
}
return 0;
}
}
|