PatchFile.cxx

This is a C++ example on how to use gdcm::Attribute

/*=========================================================================
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.
=========================================================================*/
/*
* The image was a broken file where the Pixel Data element was 8 times too big
* Apparently multiplying the BitsAllocated to 4 and multiplying the number of
* frames by 2 would solve the problem
*
* This C++ code can be used to patch the header.
*/
#include "gdcmReader.h"
#include "gdcmImageReader.h"
#include "gdcmWriter.h"
#include "gdcmDataSet.h"
#include "gdcmAttribute.h"
int main(int argc, char *argv[])
{
if( argc < 3 )
{
return 1;
}
const char *f = argv[1];
const char *out = argv[2];
gdcm::Reader r;
r.SetFileName( f );
if( !r.Read() )
{
return 1;
}
gdcm::File &file = r.GetFile();
gdcm::DataSet& ds = file.GetDataSet();
// (0028,0100) US 16 # 2, 1 BitsAllocated
// (0028,0101) US 16 # 2, 1 BitsStored
// (0028,0102) US 15 # 2, 1 HighBit
//
{
gdcm::Attribute<0x28,0x100> at;
at.SetFromDataElement( ds.GetDataElement( at.GetTag() ) );
if( at.GetValue() != 8 )
{
return 1;
}
at.SetValue( 32 );
ds.Replace( at.GetAsDataElement() );
}
{
gdcm::Attribute<0x28,0x101> at;
at.SetFromDataElement( ds.GetDataElement( at.GetTag() ) );
if( at.GetValue() != 8 )
{
return 1;
}
at.SetValue( 32 );
ds.Replace( at.GetAsDataElement() );
}
{
gdcm::Attribute<0x28,0x102> at;
at.SetFromDataElement( ds.GetDataElement( at.GetTag() ) );
if( at.GetValue() != 7 )
{
return 1;
}
at.SetValue( 31 );
ds.Replace( at.GetAsDataElement() );
}
// (0028,0008) IS [56] # 2, 1 NumberOfFrames
{
gdcm::Attribute<0x28,0x8> at;
at.SetFromDataElement( ds.GetDataElement( at.GetTag() ) );
at.SetValue( at.GetValue() * 2 );
ds.Replace( at.GetAsDataElement() );
}
gdcm::Writer w;
w.SetFile( file );
w.SetCheckFileMetaInformation( false );
w.SetFileName( out );
if( !w.Write() )
{
return 1;
}
// Now let's see if we can read it as an image:
gdcm::ImageReader ir;
ir.SetFileName( out );
if(!ir.Read())
{
return 1;
}
gdcm::Image &image = ir.GetImage();
unsigned long len = image.GetBufferLength();
const gdcm::ByteValue *bv = ir.GetFile().GetDataSet().GetDataElement( gdcm::Tag(0x7fe0,0x0010) ).GetByteValue();
if( !bv || len != bv->GetLength() )
{
return 1;
}
std::cout << bv->GetLength() << " " << len << std::endl;
std::cout << "Success to rewrite image !" << std::endl;
image.Print( std::cout );
return 0;
}
gdcm::Attribute
Attribute class This class use template metaprograming tricks to let the user know when the template ...
Definition gdcmAttribute.h:86
gdcm::Attribute::SetValue
void SetValue(ArrayType v, unsigned int idx=0)
Definition gdcmAttribute.h:169
gdcm::Attribute::GetValue
ArrayType & GetValue(unsigned int idx=0)
Definition gdcmAttribute.h:154
gdcm::Attribute::GetTag
static Tag GetTag()
Definition gdcmAttribute.h:98
gdcm::Attribute::SetFromDataElement
void SetFromDataElement(DataElement const &de)
Definition gdcmAttribute.h:206
gdcm::Attribute::GetAsDataElement
DataElement GetAsDataElement() const
Definition gdcmAttribute.h:183
gdcm::Bitmap::GetBufferLength
unsigned long GetBufferLength() const
gdcm::ByteValue
Class to represent binary value (array of bytes).
Definition gdcmByteValue.h:35
gdcm::ByteValue::GetLength
VL GetLength() const override
Definition gdcmByteValue.h:77
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::Replace
void Replace(const DataElement &de)
Replace a dataelement with another one.
Definition gdcmDataSet.h:150
gdcm::File
a DICOM File
Definition gdcmFile.h:34
gdcm::File::GetDataSet
const DataSet & GetDataSet() const
Get Data Set.
Definition gdcmFile.h:57
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::Image
Image.
Definition gdcmImage.h:47
gdcm::Image::Print
void Print(std::ostream &os) const override
print
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::Tag
Class to represent a DICOM Data Element (Attribute) Tag (Group, Element).
Definition gdcmTag.h:39
gdcm::Writer
Writer ala DOM (Document Object Model).
Definition gdcmWriter.h:49
gdcm::Writer::SetCheckFileMetaInformation
void SetCheckFileMetaInformation(bool b)
Undocumented function, do not use (= leave default).
Definition gdcmWriter.h:70
gdcm::Writer::Write
virtual bool Write()
Main function to tell the writer to write.
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
gdcmReader.h
gdcmWriter.h