📄 gdcmfilehelper.cxx
字号:
/*=========================================================================
Program: gdcm
Module: $RCSfile: gdcmFileHelper.cxx,v $
Language: C++
Date: $Date: 2008-05-30 11:37:00 $
Version: $Revision: 1.26 $
Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
l'Image). All rights reserved. See Doc/License.txt or
http://www.creatis.insa-lyon.fr/Public/Gdcm/License.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 notices for more information.
=========================================================================*/
#include "gdcmFileHelper.h"
#include "gdcmGlobal.h"
#include "gdcmTS.h"
#include "gdcmDocument.h"
#include "gdcmDebug.h"
#include "gdcmUtil.h"
#include "gdcmBinEntry.h"
#include "gdcmValEntry.h"
#include "gdcmSeqEntry.h"
#include "gdcmSQItem.h"
#include "gdcmContentEntry.h"
#include "gdcmFile.h"
#include "gdcmPixelReadConvert.h"
#include "gdcmPixelWriteConvert.h"
#include "gdcmDocEntryArchive.h"
#include "gdcmDictSet.h"
#include "gdcmOrientation.h"
#if defined(__BORLANDC__)
#include <mem.h> // for memset
#endif
#include <fstream>
/*
// ----------------------------- WARNING -------------------------
These lines will be moved to the document-to-be 'User's Guide'
// To read an image, user needs a gdcm::File
gdcm::File *f = new gdcm::File(fileName);
// or (advanced) :
// user may also decide he doesn't want to load some parts of the header
gdcm::File *f = new gdcm::File();
f->SetFileName(fileName);
f->SetLoadMode(LD_NOSEQ); // or
f->SetLoadMode(LD_NOSHADOW); // or
f->SetLoadMode(LD_NOSEQ | LD_NOSHADOW); // or
f->SetLoadMode(LD_NOSHADOWSEQ);
f->Load();
// user can now check some values
std::string v = f->GetEntryValue(groupNb,ElementNb);
// to get the pixels, user needs a gdcm::FileHelper
gdcm::FileHelper *fh = new gdcm::FileHelper(f);
// user may ask not to convert Palette to RGB
uint8_t *pixels = fh->GetImageDataRaw();
int imageLength = fh->GetImageDataRawSize();
// He can now use the pixels, create a new image, ...
uint8_t *userPixels = ...
To re-write the image, user re-uses the gdcm::FileHelper
fh->SetImageData( userPixels, userPixelsLength);
fh->SetTypeToRaw(); // Even if it was possible to convert Palette to RGB
// (WriteMode is set)
fh->SetWriteTypeToDcmExpl(); // he wants Explicit Value Representation
// Little Endian is the default
// no other value is allowed
(-->SetWriteType(ExplicitVR);)
-->WriteType = ExplicitVR;
fh->Write(newFileName); // overwrites the file, if any
// or :
fh->WriteDcmExplVR(newFileName);
// ----------------------------- WARNING -------------------------
These lines will be moved to the document-to-be 'Developer's Guide'
WriteMode : WMODE_RAW / WMODE_RGB
WriteType : ImplicitVR, ExplicitVR, ACR, ACR_LIBIDO
fh1->Write(newFileName);
SetWriteFileTypeToImplicitVR() / SetWriteFileTypeToExplicitVR();
(modifies TransferSyntax)
SetWriteToRaw(); / SetWriteToRGB();
(modifies, when necessary : photochromatic interpretation,
samples per pixel, Planar configuration,
bits allocated, bits stored, high bit -ACR 24 bits-
Pixels element VR, pushes out the LUT )
CheckWriteIntegrity();
(checks user given pixels length)
FileInternal->Write(fileName,WriteType)
fp = opens file(fileName);
ComputeGroup0002Length( );
BitsAllocated 12->16
RemoveEntryNoDestroy(palettes, etc)
Document::WriteContent(fp, writetype);
RestoreWrite();
(moves back to the File all the archived elements)
RestoreWriteFileType();
(pushes back group 0002, with TransferSyntax)
*/
namespace gdcm
{
//-------------------------------------------------------------------------
// Constructor / Destructor
/**
* \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
* file (gdcm::File only deals with the ... header)
* Opens (in read only and when possible) an existing file and checks
* for DICOM compliance. Returns NULL on failure.
* It will be up to the user to load the pixels into memory
* ( GetImageDataSize() + GetImageData() methods)
* \note the in-memory representation of all available tags found in
* the DICOM header is post-poned to first header information access.
* This avoid a double parsing of public part of the header when
* one sets an a posteriori shadow dictionary (efficiency can be
* seen as a side effect).
*/
FileHelper::FileHelper( )
{
FileInternal = new File( );
SelfHeader = true;
Initialize();
}
/**
* \brief Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
* file (File only deals with the ... header)
* Opens (in read only and when possible) an existing file and checks
* for DICOM compliance. Returns NULL on failure.
* It will be up to the user to load the pixels into memory
* ( GetImageDataSize() + GetImageData() methods)
* \note the in-memory representation of all available tags found in
* the DICOM header is post-poned to first header information access.
* This avoid a double parsing of public part of the header when
* user sets an a posteriori shadow dictionary (efficiency can be
* seen as a side effect).
* @param header already built Header
*/
FileHelper::FileHelper(File *header)
{
FileInternal = header;
SelfHeader = false;
Initialize();
if ( FileInternal->IsReadable() )
{
PixelReadConverter->GrabInformationsFromFile( FileInternal );
}
}
#ifndef GDCM_LEGACY_REMOVE
/**
* \brief DEPRECATED : use SetFilename() + SetLoadMode() + Load() methods
* Constructor dedicated to deal with the *pixels* area of a ACR/DICOMV3
* file (gdcm::File only deals with the ... header)
* Opens (in read only and when possible) an existing file and checks
* for DICOM compliance. Returns NULL on failure.
* It will be up to the user to load the pixels into memory
* \note the in-memory representation of all available tags found in
* the DICOM header is post-poned to first header information access.
* This avoid a double parsing of public part of the header when
* one sets an a posteriori shadow dictionary (efficiency can be
* seen as a side effect).
* @param filename file to be opened for parsing
* @deprecated use SetFilename() + Load() methods
*/
FileHelper::FileHelper(std::string const &filename )
{
FileInternal = new File( );
FileInternal->SetFileName( filename );
FileInternal->Load();
SelfHeader = true;
Initialize();
if ( FileInternal->IsReadable() )
{
PixelReadConverter->GrabInformationsFromFile( FileInternal );
}
}
#endif
/**
* \brief canonical destructor
* \note If the header (gdcm::File) was created by the FileHelper constructor,
* it is destroyed by the FileHelper
*/
FileHelper::~FileHelper()
{
if ( PixelReadConverter )
{
delete PixelReadConverter;
}
if ( PixelWriteConverter )
{
delete PixelWriteConverter;
}
if ( Archive )
{
delete Archive;
}
if ( SelfHeader )
{
delete FileInternal;
}
FileInternal = 0;
}
//-----------------------------------------------------------------------------
// Public
/**
* \brief Sets the LoadMode of the internal gdcm::File as a boolean string.
* NO_SEQ, NO_SHADOW, NO_SHADOWSEQ ... (nothing more, right now)
* WARNING : before using NO_SHADOW, be sure *all* your files
* contain accurate values in the 0x0000 element (if any)
* of *each* Shadow Group. The parser will fail if the size is wrong !
* @param loadMode Load mode to be used
*/
void FileHelper::SetLoadMode(int loadMode)
{
GetFile()->SetLoadMode( loadMode );
}
/**
* \brief Sets the LoadMode of the internal gdcm::File
* @param fileName name of the file to be open
*/
void FileHelper::SetFileName(std::string const &fileName)
{
FileInternal->SetFileName( fileName );
}
/**
* \brief Loader
* @return false if file cannot be open or no swap info was found,
* or no tag was found.
*/
bool FileHelper::Load()
{
if ( !FileInternal->Load() )
return false;
PixelReadConverter->GrabInformationsFromFile( FileInternal );
return true;
}
/**
* \brief Accesses an existing DocEntry (i.e. a Dicom Element)
* through it's (group, element) and modifies it's content with
* the given value.
* @param content new value (string) to substitute with
* @param group group number of the Dicom Element to modify
* @param elem element number of the Dicom Element to modify
* \return false if DocEntry not found
*/
bool FileHelper::SetValEntry(std::string const &content,
uint16_t group, uint16_t elem)
{
return FileInternal->SetValEntry(content, group, elem);
}
/**
* \brief Accesses an existing DocEntry (i.e. a Dicom Element)
* through it's (group, element) and modifies it's content with
* the given value.
* @param content new value (void* -> uint8_t*) to substitute with
* @param lgth new value length
* @param group group number of the Dicom Element to modify
* @param elem element number of the Dicom Element to modify
* \return false if DocEntry not found
*/
bool FileHelper::SetBinEntry(uint8_t *content, int lgth,
uint16_t group, uint16_t elem)
{
return FileInternal->SetBinEntry(content, lgth, group, elem);
}
/**
* \brief Modifies the value of a given DocEntry (Dicom entry)
* when it exists. Creates it with the given value when unexistant.
* @param content (string)value to be set
* @param group Group number of the Entry
* @param elem Element number of the Entry
* \return pointer to the modified/created Dicom entry (NULL when creation
* failed).
*/
ValEntry *FileHelper::InsertValEntry(std::string const &content,
uint16_t group, uint16_t elem)
{
return FileInternal->InsertValEntry(content, group, elem);
}
/**
* \brief Modifies the value of a given DocEntry (Dicom entry)
* when it exists. Creates it with the given value when unexistant.
* A copy of the binArea is made to be kept in the Document.
* @param binArea (binary)value to be set
* @param lgth new value length
* @param group Group number of the Entry
* @param elem Element number of the Entry
* \return pointer to the modified/created Dicom entry (NULL when creation
* failed).
*/
BinEntry *FileHelper::InsertBinEntry(uint8_t *binArea, int lgth,
uint16_t group, uint16_t elem)
{
return FileInternal->InsertBinEntry(binArea, lgth, group, elem);
}
/**
* \brief Adds an empty SeqEntry
* (remove any existing entry with same group,elem)
* @param group Group number of the Entry
* @param elem Element number of the Entry
* \return pointer to the created SeqEntry (NULL when creation
* failed).
*/
SeqEntry *FileHelper::InsertSeqEntry(uint16_t group, uint16_t elem)
{
return FileInternal->InsertSeqEntry(group, elem);
}
/**
* \brief Get the size of the image data
* If the image can be RGB (with a lut or by default), the size
* corresponds to the RGB image
* (use GetImageDataRawSize if you want to be sure to get *only*
* the size of the pixels)
* @return The image size
*/
size_t FileHelper::GetImageDataSize()
{
if ( PixelWriteConverter->GetUserData() )
{
return PixelWriteConverter->GetUserDataSize();
}
return PixelReadConverter->GetRGBSize();
}
/**
* \brief Get the size of the image data.
* If the image could be converted to RGB using a LUT,
* this transformation is not taken into account by GetImageDataRawSize
* (use GetImageDataSize if you wish)
* @return The raw image size
*/
size_t FileHelper::GetImageDataRawSize()
{
if ( PixelWriteConverter->GetUserData() )
{
return PixelWriteConverter->GetUserDataSize();
}
return PixelReadConverter->GetRawSize();
}
/**
* \brief brings pixels into memory :
* - Allocates necessary memory,
* - Reads the pixels from disk (uncompress if necessary),
* - Transforms YBR pixels, if any, into RGB pixels,
* - Transforms 3 planes R, G, B, if any, into a single RGB Plane
* - Transforms single Grey plane + 3 Palettes into a RGB Plane
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -