GDCM: ExtractEncapsulatedFile.cs

GDCM 3.2.8

ExtractEncapsulatedFile.cs
/*=========================================================================
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.
=========================================================================*/
/*
* This example shows how one from C# context can extract a binary blob
* and write out as a file.
* This example is meant for pdf encapsulated file, but can be adapted for other type
* of binary blob.
*
* DICOM file is:
* ...
* (0042,0010) ST (no value available) # 0, 0 DocumentTitle
* (0042,0011) OB 25\50\44\46\2d\31\2e\32\20\0d\25\e2\e3\cf\d3\20\0d\31\30\20\30\20... # 40718, 1 EncapsulatedDocument
* (0042,0012) LO [application/pdf] # 16, 1 MIMETypeOfEncapsulatedDocument
* ...
*
* Usage:
* $ export LD_LIBRARY_PATH=$HOME/Projects/gdcm/debug-gcc/bin
* $ mono bin/ExtractEncapsulatedFile.exe some_pdf_encapsulated.dcm
*/
using System;
using gdcm;
public class ExtractEncapsulatedFile
{
public static int Main(string[] args)
{
string file = args[0];
Reader reader = new Reader();
reader. SetFileName( file );
bool ret = reader. Read();
if( !ret )
{
return 1;
}
File f = reader. GetFile();
DataSet ds = f. GetDataSet();
Tag tencapsulated_stream = new Tag(0x0042,0x0011); // Encapsulated Document
if( !ds. FindDataElement( tencapsulated_stream ) )
{
return 1;
}
// else
DataElement de = ds. GetDataElement( tencapsulated_stream );
ByteValue bv = de. GetByteValue();
uint len = bv. GetLength();
byte[] encapsulated_stream = new byte[len];
bv. GetBuffer( encapsulated_stream, len );
// Write out the decompressed bytes
//System.Console.WriteLine(image.toString());
using (System.IO.Stream stream =
System.IO.File.Open(@"/tmp/dd.pdf",
System.IO.FileMode.Create))
{
System.IO.BinaryWriter writer = new System.IO.BinaryWriter(stream);
writer.Write( encapsulated_stream );
}
return 0;
}
}
gdcm::ByteValue
Class to represent binary value (array of bytes).
Definition gdcmByteValue.h:36
gdcm::ByteValue::GetBuffer
bool GetBuffer(char *buffer, unsigned long length) const
gdcm::ByteValue::GetLength
VL GetLength() const override
Definition gdcmByteValue.h:81
gdcm::DataElement
Class to represent a Data Element either Implicit or Explicit.
Definition gdcmDataElement.h:59
gdcm::DataElement::GetByteValue
const ByteValue * GetByteValue() const
Definition gdcmDataElement.h:133
gdcm::DataSet
Class to represent a Data Set (which contains Data Elements).
Definition gdcmDataSet.h:56
gdcm::DataSet::GetDataElement
const DataElement & GetDataElement(const Tag &t) const
Definition gdcmDataSet.h:188
gdcm::DataSet::FindDataElement
bool FindDataElement(const PrivateTag &t) const
Look up if private tag 't' is present in the dataset:
gdcm::File
a DICOM File
Definition gdcmFile.h:34
gdcm::File::GetDataSet
const DataSet & GetDataSet() const
Get Data Set.
Definition gdcmFile.h:57
gdcm::Reader
Reader ala DOM (Document Object Model).
Definition gdcmReader.h:54
gdcm::Reader::GetFile
const File & GetFile() const
Set/Get File.
Definition gdcmReader.h:72
gdcm::Reader::Read
virtual bool Read()
Main function to read a file.
gdcm::Reader::SetFileName
void SetFileName(const char *filename_native)
gdcm::System
Class to do system operation.
Definition gdcmSystem.h:27
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm
Definition gdcmASN1.h:21