MergeTwoFiles.cxx
/*=========================================================================
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 will show how one can read in two DICOM files, use the dataset
* from file1 and use image from file2 to save it in a 3rd file.
*
* Eg:
* MergeTwoFiles gdcmData/012345.002.050.dcm gdcmData/test.acr merge.dcm
*/
#include "gdcmReader.h"
#include "gdcmImageReader.h"
#include "gdcmImageWriter.h"
#include "gdcmWriter.h"
#include "gdcmDataSet.h"
#include "gdcmAttribute.h"
int main(int argc, char *argv[])
{
if( argc < 3 )
{
return 1;
}
const char *file1 = argv[1];
const char *file2 = argv[2];
const char *file3 = argv[3];
// Read file1
gdcm::ImageReader reader1;
reader1.SetFileName( file1 );
if( !reader1.Read() )
{
return 1;
}
// Read file2
gdcm::ImageReader reader2;
reader2.SetFileName( file2 );
if( !reader2.Read() )
{
return 1;
}
// Ok now let's take the DataSet from file1 and the Image from file2
// Warning: if file2 is -for example- a Secondary Capture Storage, then it has no
// Image Orientation (Patient) thus any Image Orientation (Patient) from file1
// will be discarded...
// let's be fancy. In case reader2 contains explicit, but reader1 is implicit
// we would rather see an implicit output
if( reader1.GetFile().GetHeader().GetDataSetTransferSyntax() == gdcm::TransferSyntax::ImplicitVRLittleEndian )
{
reader2.GetImage().SetTransferSyntax( gdcm::TransferSyntax::ImplicitVRLittleEndian );
}
gdcm::ImageWriter writer;
writer.SetFileName( file3 );
writer.SetFile( reader1.GetFile() );
// ImageWriter will always use all of gdcm::Image information an override anything wrong from
// reader1.GetFile(), including the Transfer Syntax
writer.SetImage( reader2.GetImage() );
gdcm::DataSet &ds = reader1.GetFile().GetDataSet();
// Make sure that SOPInstanceUID are different
// Simply removing it is sufficient as gdcm::ImageWriter will generate one by default
// if not found.
ds.Remove( gdcm::Tag(0x0008,0x0018) );
if( !writer.Write() )
{
return 1;
}
return 0;
}
gdcm::Bitmap::SetTransferSyntax
void SetTransferSyntax(TransferSyntax const &ts)
Transfer syntax.
Definition gdcmBitmap.h:69
gdcm::DataSet
Class to represent a Data Set (which contains Data Elements).
Definition gdcmDataSet.h:56
gdcm::DataSet::Remove
SizeType Remove(const Tag &tag)
Completely remove a dataelement from the dataset.
Definition gdcmDataSet.h:172
gdcm::FileMetaInformation::GetDataSetTransferSyntax
const TransferSyntax & GetDataSetTransferSyntax() const
Definition gdcmFileMetaInformation.h:53
gdcm::File::GetDataSet
const DataSet & GetDataSet() const
Get Data Set.
Definition gdcmFile.h:57
gdcm::File::GetHeader
const FileMetaInformation & GetHeader() const
Get File Meta Information.
Definition gdcmFile.h:48
gdcm::ImageReader
ImageReader.
Definition gdcmImageReader.h:34
gdcm::ImageReader::GetImage
const Image & GetImage() const
Return the read image.
gdcm::ImageReader::Read
bool Read() override
gdcm::ImageWriter
ImageWriter.
Definition gdcmImageWriter.h:33
gdcm::ImageWriter::Write
bool Write() override
Write.
gdcm::PixmapWriter::SetImage
virtual void SetImage(Pixmap const &img)
gdcm::Reader::GetFile
const File & GetFile() const
Set/Get File.
Definition gdcmReader.h:72
gdcm::Reader::SetFileName
void SetFileName(const char *filename_native)
gdcm::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm::TransferSyntax::ImplicitVRLittleEndian
@ ImplicitVRLittleEndian
Definition gdcmTransferSyntax.h:62
gdcm::Writer::SetFile
void SetFile(const File &f)
Set/Get the DICOM file (DataSet + Header).
Definition gdcmWriter.h:66
gdcm::Writer::SetFileName
void SetFileName(const char *filename_native)
Set the filename of DICOM file to write:
gdcmAttribute.h
gdcmDataSet.h
gdcmImageReader.h
gdcmImageWriter.h
gdcmReader.h
gdcmWriter.h