using System;
using System.IO;
public class FileChangeTS
{
public static byte[] StrToByteArray(string str)
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
return encoding.GetBytes(str);
}
static private void CreateSmallDICOM(string fileName)
{
{
img.SetDimension(0, 512 );
img.SetDimension(1, 512 );
img.SetDimension(2, 2 );
PhotometricInterpretation pi = new PhotometricInterpretation( PhotometricInterpretation.PIType.MONOCHROME2 );
img.SetPhotometricInterpretation( pi );
byte[] buffer = new byte[ 512 * 512 * 2 ];
img.SetDataElement( pixeldata );
string mediastorage = "1.2.840.10008.5.1.4.1.1.7.2";
byte[] val = StrToByteArray(mediastorage);
ms.SetByteValue( val,
new gdcm.
VL( (uint)val.Length) );
ds.Insert( ms );
writer.SetFileName( fileName );
writer.Write();
}
}
static private void CreateBigDICOM(string fileName, string outfilename)
{
{
string nframes = "1000";
ano.Replace(
new gdcm.
Tag(0x0028,0x0008), nframes );
ano.SetInputFileName(fileName);
ano.SetOutputFileName(outfilename);
ano.Write();
}
}
static private void CreateDummyFile(string fileName, long length)
{
using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
fileStream.SetLength(length);
}
}
static private void ReadBytesIntoArray( byte[] array, FileStream source )
{
int numBytesToRead = array.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
int n = source.Read(array, numBytesRead, numBytesToRead);
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
}
static private void AssembleDICOMAndRaw(string dicomfn, string rawdata, string outfn)
{
{
fs.SetTemplateFileName(dicomfn);
fs.SetOutputFileName(outfn);
fs.CheckDataElement( pixeldata );
fs.StartDataElement( pixeldata );
using (FileStream rawSource = new FileStream(rawdata,
FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[512];
for( int i = 0; i < 512 * 1000; ++i )
{
ReadBytesIntoArray( bytes, rawSource );
fs.AppendToDataElement( pixeldata, bytes, (uint)bytes.Length );
}
}
if( !fs.StopDataElement( pixeldata ) )
{
throw new Exception("StopDataElement failed");
}
}
}
static private void CompressIntoJPEG(string rawdicom, string jpegdicom)
{
using( var sfcts = FileChangeTransferSyntax.New() )
{
FileChangeTransferSyntax fcts = sfcts.__ref__();
SimpleSubjectWatcher watcher = new SimpleSubjectWatcher(fcts, "FileChangeTransferSyntax");
gdcm.
TransferSyntax ts =
new TransferSyntax( TransferSyntax.TSType.JPEGLosslessProcess14_1 );
fcts.SetTransferSyntax( ts );
fcts.SetInputFileName( rawdicom );
fcts.SetOutputFileName( jpegdicom );
fcts.Change();
}
}
public static int Main(string[] args)
{
string filename = args[0];
string outfilename = args[1];
string rawfilename = args[2];
string mergefn = args[3];
string jpegfn = args[4];
CreateSmallDICOM(filename);
CreateBigDICOM(filename, outfilename);
CreateDummyFile(rawfilename, 512 * 512 * 1000 );
AssembleDICOMAndRaw(outfilename, rawfilename, mergefn);
CompressIntoJPEG(mergefn, jpegfn);
return 0;
}
}