X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=src%2FgdcmHeader.cxx;h=85756a3bad2a53283b5a97d81d274af5710fb728;hb=34a471bea70a384767359164ef244ab08b1b842d;hp=d3416cecf6df0b042bdc8d7b6c53bdcb87fa83a4;hpb=4a4e3e139f9921d546542670419574b5c7e81b53;p=gdcm.git diff --git a/src/gdcmHeader.cxx b/src/gdcmHeader.cxx index d3416cec..85756a3b 100644 --- a/src/gdcmHeader.cxx +++ b/src/gdcmHeader.cxx @@ -1,4 +1,4 @@ -// $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.108 2003/11/05 17:14:59 malaterre Exp $ +// $Header: /cvs/public/gdcm/src/Attic/gdcmHeader.cxx,v 1.121 2003/12/22 12:46:16 regrain Exp $ #include "gdcmHeader.h" @@ -11,7 +11,14 @@ #include #endif #include // for isalpha -#include + +#ifdef GDCM_NO_ANSI_STRING_STREAM +# include +# define ostringstream ostrstream +# else +# include +#endif + #include "gdcmUtil.h" #include "gdcmTS.h" @@ -38,7 +45,14 @@ void gdcmHeader::Initialise(void) { * @param InFilename * @param exception_on_error */ -gdcmHeader::gdcmHeader(const char *InFilename, bool exception_on_error) { +gdcmHeader::gdcmHeader(const char *InFilename, + bool exception_on_error, + bool enable_sequences ) { + if (enable_sequences) + enableSequences = 1; + else + enableSequences = 0; + SetMaxSizeLoadElementValue(_MaxSizeLoadElementValue_); filename = InFilename; Initialise(); @@ -65,7 +79,7 @@ gdcmHeader::gdcmHeader(bool exception_on_error) { * @param exception_on_error * @return */ - bool gdcmHeader::OpenFile(bool exception_on_error) +FILE *gdcmHeader::OpenFile(bool exception_on_error) throw(gdcmFileError) { fp=fopen(filename.c_str(),"rb"); if(exception_on_error) { @@ -79,20 +93,22 @@ gdcmHeader::gdcmHeader(bool exception_on_error) { //ACR -- or DICOM with no Preamble if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200) - return true; + return(fp); + //DICOM fseek(fp, 126L, SEEK_CUR); char dicm[4]; fread(dicm, (size_t)4, (size_t)1, fp); if( memcmp(dicm, "DICM", 4) == 0 ) - return true; + return(fp); + fclose(fp); dbg.Verbose(0, "gdcmHeader::gdcmHeader not DICOM/ACR", filename.c_str()); } else { dbg.Verbose(0, "gdcmHeader::gdcmHeader cannot open file", filename.c_str()); } - return false; + return(NULL); } /** @@ -508,7 +524,7 @@ bool gdcmHeader::IsJPEGLossless(void) { const char * Transfert = Element->GetValue().c_str(); if ( memcmp(Transfert+strlen(Transfert)-2 ,"70",2)==0) return true; if ( memcmp(Transfert+strlen(Transfert)-2 ,"55",2)==0) return true; - if (Element->GetValue() == "1.2.840.10008.1.2.4.57") return true; + if (Element->GetValue() == "1.2.840.10008.1.2.4.57") return true; return false; } @@ -627,27 +643,49 @@ bool gdcmHeader::IsDicomV3(void) { * applying this heuristic. */ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) { - if ( FoundLength == 0xffffffff) + + ElVal->SetReadLength(FoundLength); // will be updated only if a bug is found + + if ( FoundLength == 0xffffffff) { FoundLength = 0; + } // Sorry for the patch! // XMedCom did the trick to read some nasty GE images ... else if (FoundLength == 13) { // The following 'if' will be removed when there is no more // images on Creatis HDs with a 13 length for Manufacturer... - if ( (ElVal->GetGroup() != 0x0008) || (ElVal->GetElement() != 0x0070)) { + if ( (ElVal->GetGroup() != 0x0008) || + ( (ElVal->GetElement() != 0x0070) && (ElVal->GetElement() != 0x0080) ) ) { // end of remove area FoundLength =10; + ElVal->SetReadLength(10); // a bug is to be fixed } } // to fix some garbage 'Leonardo' Siemens images // May be commented out to avoid overhead else if ( (ElVal->GetGroup() == 0x0009) && - ( (ElVal->GetElement() == 0x1113) || (ElVal->GetElement() == 0x1114) ) ) - FoundLength =4; - - ElVal->SetLength(FoundLength); + ( (ElVal->GetElement() == 0x1113) || (ElVal->GetElement() == 0x1114) ) ){ + FoundLength =4; + ElVal->SetReadLength(4); // a bug is to be fixed + } + // end of fix + + // to try to 'go inside' SeQuences (with length), and not to skip them + else if ( ElVal->GetVR() == "SQ") { + if (enableSequences) // only if the user does want to ! + FoundLength =0; + } + + // a SeQuence Element is beginning + // Let's forget it's length + // (we want to 'go inside') + else if(ElVal->GetGroup() == 0xfffe){ + FoundLength =0; + } + + ElVal->SetUsableLength(FoundLength); } /** @@ -710,7 +748,7 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) { std::string vr = ElVal->GetVR(); guint16 length16; if( (element == 0x0010) && (group == 0x7fe0) ) { - dbg.SetDebug(0); + dbg.SetDebug(-1); dbg.Verbose(2, "gdcmHeader::FindLength: ", "we reached 7fe0 0010"); } @@ -729,7 +767,7 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) { ElVal->SetLength(FindLengthOB()); return; } - FixFoundLength(ElVal, length32); + FixFoundLength(ElVal, length32); return; } @@ -753,7 +791,7 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) { // appear when we find the first group with big endian encoding. This // is easy to detect since the length of a "Group Length" tag (the // ones with zero as element number) has to be of 4 (0x0004). When we - // encouter 1024 (0x0400) chances are the encoding changed and we + // encounter 1024 (0x0400) chances are the encoding changed and we // found a group with big endian encoding. // We shall use this second strategy. In order to make sure that we // can interpret the presence of an apparently big endian encoded @@ -806,6 +844,7 @@ void gdcmHeader::FixFoundLength(gdcmElValue * ElVal, guint32 FoundLength) { // not coexist in a Data Set and Data Sets nested within it".] // Length is on 4 bytes. FixFoundLength(ElVal, ReadInt32()); + return; } /** @@ -866,7 +905,7 @@ guint16 gdcmHeader::SwapShort(guint16 a) { * @return */ void gdcmHeader::SkipElementValue(gdcmElValue * ElVal) { - SkipBytes(ElVal->GetLength()); + SkipBytes(ElVal->GetLength()); } /** @@ -907,14 +946,17 @@ void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) { // car commencer par les ignorer risque de conduire a qq chose // qui pourrait ne pas etre generalisable // Well, I'm expecting your code !!! + + // the test was commented out to 'go inside' the SeQuences + // we don't any longer skip them ! - if( vr == "SQ" ) - SkipLoad = true; + // if( vr == "SQ" ) + // SkipLoad = true; - // A sequence "contains" a set of Elements. + // A SeQuence "contains" a set of Elements. // (fffe e000) tells us an Element is beginning // (fffe e00d) tells us an Element just ended - // (fffe e0dd) tells us the current SQuence just ended + // (fffe e0dd) tells us the current SeQuence just ended if( group == 0xfffe ) SkipLoad = true; @@ -939,6 +981,7 @@ void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) { s << "gdcm::NotLoaded."; s << " Address:" << (long)ElVal->GetOffset(); s << " Length:" << ElVal->GetLength(); + s << " x(" << std::hex << ElVal->GetLength() << ")"; ElVal->SetValue(s.str()); return; } @@ -946,9 +989,9 @@ void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) { // When an integer is expected, read and convert the following two or // four bytes properly i.e. as an integer as opposed to a string. - // pour les elements de Value Multiplicity > 1 - // on aura en fait une serie d'entiers - // on devrait pouvoir faire + compact (?) + // Actually, elements with Value Multiplicity > 1 + // contain a set of integers (not a single one) + // Any compacter code suggested (?) if ( IsAnInteger(ElVal) ) { guint32 NewInt; @@ -978,6 +1021,9 @@ void gdcmHeader::LoadElementValue(gdcmElValue * ElVal) { } } } +#ifdef GDCM_NO_ANSI_STRING_STREAM + s << std::ends; // to avoid oddities on Solaris +#endif //GDCM_NO_ANSI_STRING_STREAM ElVal->SetValue(s.str()); return; } @@ -1115,7 +1161,6 @@ gdcmElValue* gdcmHeader::NewElValueByNumber(guint16 Group, guint16 Elem) { */ int gdcmHeader::ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem ) { - // TODO : FIXME JPRx // curieux, non ? // on (je) cree une Elvalue ne contenant pas de valeur @@ -1226,6 +1271,7 @@ gdcmElValue * gdcmHeader::ReadNextElement(void) { NewElVal = NewElValueByNumber(g, n); FindVR(NewElVal); FindLength(NewElVal); + if (errno == 1) { // Call it quits return (gdcmElValue *)0; @@ -1427,7 +1473,7 @@ std::string gdcmHeader::GetPubElValRepByNumber(guint16 group, guint16 element) { * \ingroup gdcmHeader * \brief Searches within the public dictionary for element value of * a given tag. - * @param TagName name of the researched element. + * @param TagName name of the searched element. * @return Corresponding element value when it exists, and the string * GDCM_UNFOUND ("gdcm::Unfound") otherwise. */ @@ -1444,7 +1490,7 @@ std::string gdcmHeader::GetPubElValByName(std::string TagName) { * to convert the string typed content to caller's native type * (think of C++ vs Python). The VR is actually of a higher level * of semantics than just the native C++ type. - * @param TagName name of the researched element. + * @param TagName name of the searched element. * @return Corresponding element value representation when it exists, * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise. */ @@ -1459,8 +1505,8 @@ std::string gdcmHeader::GetPubElValRepByName(std::string TagName) { * \ingroup gdcmHeader * \brief Searches within elements parsed with the SHADOW dictionary * for the element value of a given tag. - * @param group Group of the researched tag. - * @param element Element of the researched tag. + * @param group Group of the searched tag. + * @param element Element of the searched tag. * @return Corresponding element value representation when it exists, * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise. */ @@ -1477,8 +1523,8 @@ std::string gdcmHeader::GetShaElValByNumber(guint16 group, guint16 element) { * to convert the string typed content to caller's native type * (think of C++ vs Python). The VR is actually of a higher level * of semantics than just the native C++ type. - * @param group Group of the researched tag. - * @param element Element of the researched tag. + * @param group Group of the searched tag. + * @param element Element of the searched tag. * @return Corresponding element value representation when it exists, * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise. */ @@ -1493,7 +1539,7 @@ std::string gdcmHeader::GetShaElValRepByNumber(guint16 group, guint16 element) { * \ingroup gdcmHeader * \brief Searches within the elements parsed with the shadow dictionary * for an element value of given tag. - * @param TagName name of the researched element. + * @param TagName name of the searched element. * @return Corresponding element value when it exists, and the string * GDCM_UNFOUND ("gdcm::Unfound") otherwise. */ @@ -1510,7 +1556,7 @@ std::string gdcmHeader::GetShaElValByName(std::string TagName) { * to convert the string typed content to caller's native type * (think of C++ vs Python). The VR is actually of a higher level * of semantics than just the native C++ type. - * @param TagName name of the researched element. + * @param TagName name of the searched element. * @return Corresponding element value representation when it exists, * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise. */ @@ -1526,8 +1572,8 @@ std::string gdcmHeader::GetShaElValRepByName(std::string TagName) { * \brief Searches within elements parsed with the public dictionary * and then within the elements parsed with the shadow dictionary * for the element value of a given tag. - * @param group Group of the researched tag. - * @param element Element of the researched tag. + * @param group Group of the searched tag. + * @param element Element of the searched tag. * @return Corresponding element value representation when it exists, * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise. */ @@ -1548,8 +1594,8 @@ std::string gdcmHeader::GetElValByNumber(guint16 group, guint16 element) { * to convert the string typed content to caller's native type * (think of C++ vs Python). The VR is actually of a higher level * of semantics than just the native C++ type. - * @param group Group of the researched tag. - * @param element Element of the researched tag. + * @param group Group of the searched tag. + * @param element Element of the searched tag. * @return Corresponding element value representation when it exists, * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise. */ @@ -1565,7 +1611,7 @@ std::string gdcmHeader::GetElValRepByNumber(guint16 group, guint16 element) { * \brief Searches within elements parsed with the public dictionary * and then within the elements parsed with the shadow dictionary * for the element value of a given tag. - * @param TagName name of the researched element. + * @param TagName name of the searched element. * @return Corresponding element value when it exists, * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise. */ @@ -1586,7 +1632,7 @@ std::string gdcmHeader::GetElValByName(std::string TagName) { * to convert the string typed content to caller's native type * (think of C++ vs Python). The VR is actually of a higher level * of semantics than just the native C++ type. - * @param TagName name of the researched element. + * @param TagName name of the searched element. * @return Corresponding element value representation when it exists, * and the string GDCM_UNFOUND ("gdcm::Unfound") otherwise. */ @@ -1680,7 +1726,7 @@ void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) { rewind(fp); CheckSwap(); - while ( (newElValue = ReadNextElement()) ) { + while ( (newElValue = ReadNextElement()) ) { SkipElementValue(newElValue); PubElValSet.Add(newElValue); } @@ -1688,7 +1734,17 @@ void gdcmHeader::ParseHeader(bool exception_on_error) throw(gdcmFormatError) { /** * \ingroup gdcmHeader - * \brief This predicate, based on hopefully reasonnable heuristics, + * \brief + * @return + */ +FileType gdcmHeader::GetFileType(void) +{ + return(filetype); +} + +/** + * \ingroup gdcmHeader + * \brief This predicate, based on hopefully reasonable heuristics, * decides whether or not the current gdcmHeader was properly parsed * and contains the mandatory information for being considered as * a well formed and usable image. @@ -1747,11 +1803,22 @@ gdcmElValue* gdcmHeader::NewManualElValToPubDict(std::string NewTagName, * public tag based hash table. */ void gdcmHeader::LoadElements(void) { - rewind(fp); - TagElValueHT ht = PubElValSet.GetTagHt(); - for (TagElValueHT::iterator tag = ht.begin(); tag != ht.end(); ++tag) { - LoadElementValue(tag->second); - } + rewind(fp); + + // We don't use any longer the HashTable, since a lot a stuff is missing + // when SeQuences were encountered + // + //TagElValueHT ht = PubElValSet.GetTagHt(); + //for (TagElValueHT::iterator tag = ht.begin(); tag != ht.end(); ++tag) { + // LoadElementValue(tag->second); + //} + + for (ListTag::iterator i = GetListElem().begin(); + i != GetListElem().end(); + ++i){ + LoadElementValue(*i); + } + rewind(fp); // Load 'non string' values @@ -1811,8 +1878,7 @@ void gdcmHeader::PrintPubDict(std::ostream & os) { */ int gdcmHeader::Write(FILE * fp, FileType type) { - - // TODO : move the following lines (and a lot of others) + // TODO : move the following lines (and a lot of others, to be written) // to a future function CheckAndCorrectHeader if (type == ImplicitVR) { @@ -1858,16 +1924,12 @@ void * gdcmHeader::LoadElementVoidArea(guint16 Group, guint16 Elem) { int l=Element->GetLength(); void * a = malloc(l); if(!a) { - std::cout << "Big Broblem (LoadElementVoidArea, malloc) " - << std::hex << Group << " " << Elem << std::endl; return NULL; } /* int res = */ PubElValSet.SetVoidAreaByNumber(a, Group, Elem); // TODO check the result size_t l2 = fread(a, 1, l ,fp); if(l != l2) { - std::cout << "Big Broblem (LoadElementVoidArea, fread) " - << std::hex << Group << " " << Elem << std::endl; free(a); return NULL; } @@ -2089,8 +2151,6 @@ std::string gdcmHeader::GetPixelType(void) { if (Signed == "0") Signed = std::string("U"); else - -std::cout << "GetPixelType : " << BitsAlloc + Signed << std::endl; Signed = std::string("S"); return( BitsAlloc + Signed); @@ -2192,7 +2252,7 @@ int gdcmHeader::GetLUTNbits(void) { * @return Lookup Table RGBA */ -void * gdcmHeader::GetLUTRGBA(void) { +unsigned char * gdcmHeader::GetLUTRGBA(void) { // Not so easy : see // http://www.barre.nom.fr/medical/dicom2/limitations.html#Color%20Lookup%20Tables // and OT-PAL-8-face.dcm @@ -2202,9 +2262,7 @@ void * gdcmHeader::GetLUTRGBA(void) { if (gdcmHeader::GetPubElValByNumber(0x0028,0x0004) != "PALETTE COLOR ") { return NULL; } - - void * LutR,*LutG,*LutB; - int l; + int lengthR, debR, nbitsR; int lengthG, debG, nbitsG; int lengthB, debB, nbitsB; @@ -2259,7 +2317,7 @@ void * gdcmHeader::GetLUTRGBA(void) { } // forge the 4 * 8 Bits Red/Green/Blue/Alpha LUT - char *LUTRGBA = (char *)calloc(1024,1); // 256 * 4 (R, G, B, Alpha) + unsigned char *LUTRGBA = (unsigned char *)calloc(1024,1); // 256 * 4 (R, G, B, Alpha) if (!LUTRGBA) { return NULL; } @@ -2287,7 +2345,7 @@ void * gdcmHeader::GetLUTRGBA(void) { // and fix the code // We give up the checking to avoid some overhead - char *a; + unsigned char *a; int i; a= LUTRGBA+0; for(i=0;i not-for-rats function + * + * \warning WARNING doit-etre etre publique ? + * TODO : y aurait il un inconvenient à fusionner ces 2 fonctions + * + * @param ImageDataSize new Pixel Area Size + * warning : nothing else is checked + */ + +void gdcmHeader::SetImageDataSize(size_t ImageDataSize) { + std::string content1; + char car[20]; + // Assumes ElValue (0x7fe0, 0x0010) exists ... + sprintf(car,"%d",ImageDataSize); + + gdcmElValue *a = GetElValueByNumber(0x7fe0, 0x0010); + a->SetLength(ImageDataSize); + + ImageDataSize+=8; + sprintf(car,"%d",ImageDataSize); + content1=car; + SetPubElValByNumber(content1, 0x7fe0, 0x0000); +} + +