X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=src%2FgdcmHeader.cxx;h=29955e13029d535e3746bba7de03d1aabe8dcc59;hb=0fa193d8b312b90f2ee822e1ee87f6e2fbd1dcb5;hp=20ae34ed6d92c373fead8bbf592f35f1c313c103;hpb=ee65673385a2d6192d7d64ed0339bd47515de805;p=gdcm.git diff --git a/src/gdcmHeader.cxx b/src/gdcmHeader.cxx index 20ae34ed..29955e13 100644 --- a/src/gdcmHeader.cxx +++ b/src/gdcmHeader.cxx @@ -1,4 +1,4 @@ -// gdcmHeader.cxx +// $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.68 2003/05/28 19:36:21 frog Exp $ #include #include @@ -12,81 +12,91 @@ #include #include "gdcmUtil.h" #include "gdcmHeader.h" - -#include "iddcmjpeg.h" +using namespace std; // Refer to gdcmHeader::CheckSwap() #define HEADER_LENGTH_TO_READ 256 // Refer to gdcmHeader::SetMaxSizeLoadElementValue() #define _MaxSizeLoadElementValue_ 1024 -VRHT * gdcmHeader::dicom_vr = (VRHT*)0; - void gdcmHeader::Initialise(void) { - if (!gdcmHeader::dicom_vr) - InitVRDict(); - Dicts = new gdcmDictSet(); + dicom_vr = gdcmGlobal::GetVR(); + Dicts = gdcmGlobal::GetDicts(); RefPubDict = Dicts->GetDefaultPubDict(); RefShaDict = (gdcmDict*)0; } -gdcmHeader::gdcmHeader(const char *InFilename, bool exception_on_error) - throw(gdcmFileError) { +gdcmHeader::gdcmHeader(const char *InFilename, bool exception_on_error) { SetMaxSizeLoadElementValue(_MaxSizeLoadElementValue_); filename = InFilename; Initialise(); - fp=fopen(InFilename,"rb"); + if ( !OpenFile(exception_on_error)) + return; + ParseHeader(); + LoadElements(); + CloseFile(); +} + +bool gdcmHeader::OpenFile(bool exception_on_error) + throw(gdcmFileError) { + fp=fopen(filename.c_str(),"rb"); if(exception_on_error) { if(!fp) throw gdcmFileError("gdcmHeader::gdcmHeader(const char *, bool)"); } - else - dbg.Error(!fp, "gdcmHeader::gdcmHeader cannot open file", InFilename); - ParseHeader(); - LoadElements(); + if ( fp ) + return true; + dbg.Verbose(0, "gdcmHeader::gdcmHeader cannot open file", filename.c_str()); + return false; } +bool gdcmHeader::CloseFile(void) { + int closed = fclose(fp); + fp = (FILE *)0; + if (! closed) + return false; + return true; +} gdcmHeader::~gdcmHeader (void) { - //FIXME obviously there is much to be done here ! - fclose(fp); + dicom_vr = (gdcmVR*)0; + Dicts = (gdcmDictSet*)0; + RefPubDict = (gdcmDict*)0; + RefShaDict = (gdcmDict*)0; return; } -void gdcmHeader::InitVRDict (void) { - if (dicom_vr) { - dbg.Verbose(0, "gdcmHeader::InitVRDict:", "VR dictionary allready set"); - return; - } - VRHT *vr = new VRHT; - (*vr)["AE"] = "Application Entity"; // At most 16 bytes - (*vr)["AS"] = "Age String"; // Exactly 4 bytes - (*vr)["AT"] = "Attribute Tag"; // 2 16-bit unsigned short integers - (*vr)["CS"] = "Code String"; // At most 16 bytes - (*vr)["DA"] = "Date"; // Exactly 8 bytes - (*vr)["DS"] = "Decimal String"; // At most 16 bytes - (*vr)["DT"] = "Date Time"; // At most 26 bytes - (*vr)["FL"] = "Floating Point Single"; // 32-bit IEEE 754:1985 float - (*vr)["FD"] = "Floating Point Double"; // 64-bit IEEE 754:1985 double - (*vr)["IS"] = "Integer String"; // At most 12 bytes - (*vr)["LO"] = "Long String"; // At most 64 chars - (*vr)["LT"] = "Long Text"; // At most 10240 chars - (*vr)["OB"] = "Other Byte String"; // String of bytes (vr independant) - (*vr)["OW"] = "Other Word String"; // String of 16-bit words (vr dep) - (*vr)["PN"] = "Person Name"; // At most 64 chars - (*vr)["SH"] = "Short String"; // At most 16 chars - (*vr)["SL"] = "Signed Long"; // Exactly 4 bytes - (*vr)["SQ"] = "Sequence of Items"; // Not Applicable - (*vr)["SS"] = "Signed Short"; // Exactly 2 bytes - (*vr)["ST"] = "Short Text"; // At most 1024 chars - (*vr)["TM"] = "Time"; // At most 16 bytes - (*vr)["UI"] = "Unique Identifier"; // At most 64 bytes - (*vr)["UL"] = "Unsigned Long "; // Exactly 4 bytes - (*vr)["UN"] = "Unknown"; // Any length of bytes - (*vr)["US"] = "Unsigned Short "; // Exactly 2 bytes - (*vr)["UT"] = "Unlimited Text"; // At most 2^32 -1 chars - dicom_vr = vr; -} +// Fourth semantics: +// CMD Command +// META Meta Information +// DIR Directory +// ID +// PAT Patient +// ACQ Acquisition +// REL Related +// IMG Image +// SDY Study +// VIS Visit +// WAV Waveform +// PRC +// DEV Device +// NMI Nuclear Medicine +// MED +// BFS Basic Film Session +// BFB Basic Film Box +// BIB Basic Image Box +// BAB +// IOB +// PJ +// PRINTER +// RT Radio Therapy +// DVH +// SSET +// RES Results +// CRV Curve +// OLY Overlays +// PXL Pixels +// /** * \ingroup gdcmHeader @@ -241,19 +251,6 @@ void gdcmHeader::SwitchSwapToBigEndian(void) { sw = 3412; } -void gdcmHeader::GetPixels(size_t lgrTotale, void* _Pixels) { - size_t pixelsOffset; - pixelsOffset = GetPixelOffset(); - fseek(fp, pixelsOffset, SEEK_SET); - if (IsJPEGLossless()) { - _Pixels=_IdDcmJpegRead(fp); - } else { - fread(_Pixels, 1, lgrTotale, fp); - } -} - - - /** * \ingroup gdcmHeader * \brief Find the value representation of the current tag. @@ -293,7 +290,7 @@ void gdcmHeader::FindVR( gdcmElValue *ElVal) { // CLEANME searching the dicom_vr at each occurence is expensive. // PostPone this test in an optional integrity check at the end // of parsing or only in debug mode. - if ( RealExplicit && !dicom_vr->count(vr) ) + if ( RealExplicit && !dicom_vr->Count(vr) ) RealExplicit= false; if ( RealExplicit ) { @@ -432,6 +429,7 @@ bool gdcmHeader::IsJPEGLossless(void) { return false; LoadElementValueSafe(Element); const char * Transfert = Element->GetValue().c_str(); + printf("TransfertSyntx %s\n",Transfert); if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true; if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true; return false; @@ -617,7 +615,8 @@ void gdcmHeader::FindLength(gdcmElValue * ElVal) { // and the dictionary entry depending on them. guint16 CorrectGroup = SwapShort(ElVal->GetGroup()); guint16 CorrectElem = SwapShort(ElVal->GetElement()); - gdcmDictEntry * NewTag = GetDictEntryByKey(CorrectGroup, CorrectElem); + gdcmDictEntry * NewTag = GetDictEntryByNumber(CorrectGroup, + CorrectElem); if (!NewTag) { // This correct tag is not in the dictionary. Create a new one. NewTag = new gdcmDictEntry(CorrectGroup, CorrectElem); @@ -715,7 +714,6 @@ void gdcmHeader::SetMaxSizeLoadElementValue(long NewSize) { void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) { size_t item_read; guint16 group = ElVal->GetGroup(); - guint16 elem = ElVal->GetElement(); string vr = ElVal->GetVR(); guint32 length = ElVal->GetLength(); bool SkipLoad = false; @@ -803,7 +801,7 @@ void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) { return; } - // FIXME The exact size should be length if we move to strings or whatever + // We need an additional byte for storing \0 that is not on disk char* NewValue = (char*)malloc(length+1); if( !NewValue) { dbg.Verbose(1, "LoadElementValue: Failed to allocate NewValue"); @@ -819,6 +817,7 @@ void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) { return; } ElVal->SetValue(NewValue); + free(NewValue); } /** @@ -864,6 +863,18 @@ guint32 gdcmHeader::ReadInt32(void) { return g; } + +gdcmElValue* gdcmHeader::GetElValueByNumber(guint16 Group, guint16 Elem) { + + gdcmElValue* elValue = PubElValSet.GetElementByNumber(Group, Elem); + if (!elValue) { + dbg.Verbose(1, "gdcmHeader::GetElValueByNumber", + "failed to Locate gdcmElValue"); + return (gdcmElValue*)0; + } + return elValue; +} + /** * \ingroup gdcmHeader * \brief Build a new Element Value from all the low level arguments. @@ -872,15 +883,15 @@ guint32 gdcmHeader::ReadInt32(void) { * @param Group group of the underlying DictEntry * @param Elem element of the underlying DictEntry */ -gdcmElValue* gdcmHeader::NewElValueByKey(guint16 Group, guint16 Elem) { +gdcmElValue* gdcmHeader::NewElValueByNumber(guint16 Group, guint16 Elem) { // Find out if the tag we encountered is in the dictionaries: - gdcmDictEntry * NewTag = GetDictEntryByKey(Group, Elem); + gdcmDictEntry * NewTag = GetDictEntryByNumber(Group, Elem); if (!NewTag) NewTag = new gdcmDictEntry(Group, Elem); gdcmElValue* NewElVal = new gdcmElValue(NewTag); if (!NewElVal) { - dbg.Verbose(1, "gdcmHeader::NewElValueByKey", + dbg.Verbose(1, "gdcmHeader::NewElValueByNumber", "failed to allocate gdcmElValue"); return (gdcmElValue*)0; } @@ -892,9 +903,9 @@ gdcmElValue* gdcmHeader::NewElValueByKey(guint16 Group, guint16 Elem) { * \brief TODO * @param */ -int gdcmHeader::ReplaceOrCreateByNumber(guint16 Group, guint16 Elem, string Value) { +int gdcmHeader::ReplaceOrCreateByNumber(string Value, guint16 Group, guint16 Elem ) { - gdcmElValue* nvElValue=NewElValueByKey(Group, Elem); + gdcmElValue* nvElValue=NewElValueByNumber(Group, Elem); PubElValSet.Add(nvElValue); PubElValSet.SetElValueByNumber(Value, Group, Elem); return(1); @@ -940,7 +951,7 @@ gdcmElValue * gdcmHeader::ReadNextElement(void) { // has to be considered as finished. return (gdcmElValue *)0; - NewElVal = NewElValueByKey(g, n); + NewElVal = NewElValueByNumber(g, n); FindVR(NewElVal); FindLength(NewElVal); if (errno == 1) @@ -969,9 +980,13 @@ bool gdcmHeader::IsAnInteger(gdcmElValue * ElVal) { if ( element == 0 ) { // This is the group length of the group if (length == 4) return true; - else + else { + printf("Erroneous Group Length element length (%04x , %04x) : %d\n", + group, element,length); + dbg.Error("gdcmHeader::IsAnInteger", - "Erroneous Group Length element length."); + "Erroneous Group Length element length."); + } } if ( (vr == "UL") || (vr == "US") || (vr == "SL") || (vr == "SS") ) @@ -1005,7 +1020,8 @@ size_t gdcmHeader::GetPixelOffset(void) { numPixel = 0x1010; else numPixel = 0x0010; - gdcmElValue* PixelElement = PubElValSet.GetElementByNumber(grPixel, numPixel); + gdcmElValue* PixelElement = PubElValSet.GetElementByNumber(grPixel, + numPixel); if (PixelElement) return PixelElement->GetOffset(); else @@ -1022,19 +1038,20 @@ size_t gdcmHeader::GetPixelOffset(void) { * @param element element of the searched DictEntry * @return Corresponding DictEntry when it exists, NULL otherwise. */ -gdcmDictEntry * gdcmHeader::GetDictEntryByKey(guint16 group, guint16 element) { +gdcmDictEntry * gdcmHeader::GetDictEntryByNumber(guint16 group, + guint16 element) { gdcmDictEntry * found = (gdcmDictEntry*)0; if (!RefPubDict && !RefShaDict) { dbg.Verbose(0, "gdcmHeader::GetDictEntry", "we SHOULD have a default dictionary"); } if (RefPubDict) { - found = RefPubDict->GetTagByKey(group, element); + found = RefPubDict->GetTagByNumber(group, element); if (found) return found; } if (RefShaDict) { - found = RefShaDict->GetTagByKey(group, element); + found = RefShaDict->GetTagByNumber(group, element); if (found) return found; } @@ -1287,7 +1304,11 @@ string gdcmHeader::GetElValRepByName(string TagName) { */ int gdcmHeader::SetPubElValByNumber(string content, guint16 group, guint16 element) + +//TODO : homogeneiser les noms : SetPubElValByNumber qui appelle PubElValSet.SetElValueByNumber +// pourquoi pas SetPubElValueByNumber ?? { + return ( PubElValSet.SetElValueByNumber (content, group, element) ); } @@ -1419,18 +1440,36 @@ int gdcmHeader::GetZSize(void) { return 1; } +/** + * \ingroup gdcmHeader + * \brief Return the size (in bytes) of a single pixel of data. + * @return The size in bytes of a single pixel of data. + * + */ +int gdcmHeader::GetPixelSize(void) { + string PixelType = GetPixelType(); + if (PixelType == "8U" || PixelType == "8S") + return 1; + if (PixelType == "16U" || PixelType == "16S") + return 2; + if (PixelType == "32U" || PixelType == "32S") + return 4; + dbg.Verbose(0, "gdcmHeader::GetPixelSize: Unknown pixel type"); + return 0; +} + /** * \ingroup gdcmHeader * \brief Build the Pixel Type of the image. * Possible values are: - * - U8 unsigned 8 bit, - * - S8 signed 8 bit, - * - U16 unsigned 16 bit, - * - S16 signed 16 bit, - * - U32 unsigned 32 bit, - * - S32 signed 32 bit, + * - 8U unsigned 8 bit, + * - 8S signed 8 bit, + * - 16U unsigned 16 bit, + * - 16S signed 16 bit, + * - 32U unsigned 32 bit, + * - 32S signed 32 bit, * \warning 12 bit images appear as 16 bit. - * @return + * @return */ string gdcmHeader::GetPixelType(void) { string BitsAlloc; @@ -1524,11 +1563,11 @@ void gdcmHeader::LoadElements(void) { } } -void gdcmHeader::PrintPubElVal(ostream & os) { +void gdcmHeader::PrintPubElVal(std::ostream & os) { PubElValSet.Print(os); } -void gdcmHeader::PrintPubDict(ostream & os) { +void gdcmHeader::PrintPubDict(std::ostream & os) { RefPubDict->Print(os); }