Skip to content

Read a DCM file and change then write to DCM again

Raphael Kim edited this page Jul 5, 2017 · 1 revision

Read a DCM file and add a new element, then save it again.

  • This lecture may help you understand load DCM and save it again or as a new DCM.

Preparation

  • Compiled, or prebuilt library : libtinydicom.a
  • Inlcude header file "libDCM.h" in your source code.
  • And ability to link libtinydicom.a
  • An example DCM file.

Open DCM file

  • You can read DCM file from your file system.
// decide using namespace of std

using namespace std;

string fileDCM = "LLVM.DCM";
string fileNEW = "LLVM-NEW.DCM";

if ( OpenDCM( fileDCM.c_str() ) == true )
{
	// Get DICOM tag element count
	int elements = GetElementCount(); 
	
	// Proceed if elements exists.
	if ( elements > 0 )
	{
		// Check existing DICOM tag : Modality as "CT"
		string modality = "CT";
		unsigned int newtagID = 0x00080060;
		
		DCMTagElement* tagModal = FindElement( newtagID );
		if ( tagModal == NULL )
		{
			if ( NewElement( newtagID, &tagModal ) == true )
			{
				WriteAnsiString( tagModal, modality.c_str() );
			}
		}
		
		if ( SaveDCM( fileNEW.c_str() == true )
		{
			printf( "New DCM file %s written.\n", fileNEW.c_str() );
		}
		else
		{
			printf( "Failed to write new DCM !\n" );
		}
	}
	
	// Let close DCM file handle.
	CloseDCM();
}