From 7eb058ef98b0003b379bf0db33e5c4c673b7f1e6 Mon Sep 17 00:00:00 2001 From: jpr Date: Mon, 20 Sep 2004 18:14:23 +0000 Subject: [PATCH] Last modif before gdcmPixelData class introduction : High Bit and Bits Allocated are now save and restored Color Palettes are now droped out the H Table (and no longer destroyed) --- src/gdcmBinEntry.cxx | 6 +- src/gdcmDocument.cxx | 91 ++------------------ src/gdcmDocument.h | 6 +- src/gdcmFile.cxx | 200 +++++++++++++++++++++++++++++++++++-------- src/gdcmFile.h | 53 +++++++++--- src/gdcmHeader.cxx | 21 +++-- 6 files changed, 228 insertions(+), 149 deletions(-) diff --git a/src/gdcmBinEntry.cxx b/src/gdcmBinEntry.cxx index 7f1217a1..092c1ee1 100644 --- a/src/gdcmBinEntry.cxx +++ b/src/gdcmBinEntry.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmBinEntry.cxx,v $ Language: C++ - Date: $Date: 2004/09/15 03:50:48 $ - Version: $Revision: 1.27 $ + Date: $Date: 2004/09/20 18:14:23 $ + Version: $Revision: 1.28 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -55,7 +55,7 @@ gdcmBinEntry::~gdcmBinEntry() { if (VoidArea) { - //free (VoidArea); + free (VoidArea); VoidArea = 0; // let's be carefull ! } } diff --git a/src/gdcmDocument.cxx b/src/gdcmDocument.cxx index 16adce6f..cd17312c 100644 --- a/src/gdcmDocument.cxx +++ b/src/gdcmDocument.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocument.cxx,v $ Language: C++ - Date: $Date: 2004/09/17 13:11:16 $ - Version: $Revision: 1.81 $ + Date: $Date: 2004/09/20 18:14:23 $ + Version: $Revision: 1.82 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -538,13 +538,10 @@ bool gdcmDocument::CloseFile() void gdcmDocument::Write(FILE* fp,FileType filetype) { /// \todo move the following lines (and a lot of others, to be written) - /// to a future function CheckAndCorrectHeader - - /// WARNING : Si on veut ecrire du DICOM V3 a partir d'un DcmHeader ACR-NEMA - /// no way (check : FileType est un champ de gdcmDocument ...) - /// a moins de se livrer a un tres complique ajout des champs manquants. - /// faire un CheckAndCorrectHeader (?) - + /// to a future function CheckAndCorrectHeader + /// (necessary if user wants to write a DICOM V3 file + /// starting from an ACR-NEMA (V2) gdcmHeader + if (filetype == gdcmImplicitVR) { std::string implicitVRTransfertSyntax = UI1_2_840_10008_1_2; @@ -587,72 +584,6 @@ void gdcmDocument::Write(FILE* fp,FileType filetype) } -/** - * \brief Modifies the value of a given Header Entry (Dicom Element) - * when it exists. Create it with the given value when unexistant. - * @param value (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 Header Entry (NULL when creation - * failed). - */ - -/* -gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber( - std::string const & value, - uint16_t group, - uint16_t elem ) -{ - gdcmValEntry* valEntry = 0; - gdcmDocEntry* currentEntry = GetDocEntryByNumber( group, elem); - - if (!currentEntry) - { - // The entry wasn't present and we simply create the required ValEntry: - currentEntry = NewDocEntryByNumber(group, elem); - if (!currentEntry) - { - dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: call to" - " NewDocEntryByNumber failed."); - return NULL; - } - valEntry = new gdcmValEntry(currentEntry); - if ( !AddEntry(valEntry)) - { - dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: AddEntry" - " failed allthough this is a creation."); - } - } - else - { - valEntry = dynamic_cast< gdcmValEntry* >(currentEntry); - if ( !valEntry ) // Euuuuh? It wasn't a ValEntry - // then we change it to a ValEntry ? - // Shouldn't it be considered as an error ? - { - // We need to promote the gdcmDocEntry to a gdcmValEntry: - valEntry = new gdcmValEntry(currentEntry); - if (!RemoveEntry(currentEntry)) - { - dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: removal" - " of previous DocEntry failed."); - return NULL; - } - if ( !AddEntry(valEntry)) - { - dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: adding" - " promoted ValEntry failed."); - return NULL; - } - } - } - - SetEntryByNumber(value, group, elem); - - return valEntry; -} -*/ - /** * \brief Modifies the value of a given Header Entry (Dicom Element) * when it exists. Create it with the given value when unexistant. @@ -662,13 +593,7 @@ gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber( * @param VR V(alue) R(epresentation) of the Entry -if private Entry- * \return pointer to the modified/created Header Entry (NULL when creation * failed). - */ - - // TODO : write something clever, using default value for VR - // to avoid code duplication - // (I don't know how to tell NewDocEntryByNumber - // that ReplaceOrCreateByNumber was called with a default value) - + */ gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber( std::string const & value, uint16_t group, @@ -923,7 +848,6 @@ std::string gdcmDocument::GetEntryVRByName(TagName const & tagName) return elem->GetVR(); } - /** * \brief Searches within Header Entries (Dicom Elements) parsed with * the public and private dictionaries @@ -1189,7 +1113,6 @@ void* gdcmDocument::LoadEntryVoidArea(uint16_t group, uint16_t elem) { dbg.Verbose(0, "gdcmDocument::LoadEntryVoidArea setting failed."); } - return a; } /** diff --git a/src/gdcmDocument.h b/src/gdcmDocument.h index e7518c66..9923076d 100644 --- a/src/gdcmDocument.h +++ b/src/gdcmDocument.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmDocument.h,v $ Language: C++ - Date: $Date: 2004/09/17 13:11:16 $ - Version: $Revision: 1.38 $ + Date: $Date: 2004/09/20 18:14:23 $ + Version: $Revision: 1.39 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -182,7 +182,7 @@ public: virtual bool SetEntryLengthByNumber(uint32_t length, uint16_t group, uint16_t element); - virtual size_t GetEntryOffsetByNumber(uint16_t group, uint16_t elem); + virtual size_t GetEntryOffsetByNumber (uint16_t group, uint16_t elem); virtual void* GetEntryVoidAreaByNumber(uint16_t group, uint16_t elem); virtual bool SetEntryVoidAreaByNumber(void* a, uint16_t group, uint16_t elem); diff --git a/src/gdcmFile.cxx b/src/gdcmFile.cxx index e94cb485..18e2d7d0 100644 --- a/src/gdcmFile.cxx +++ b/src/gdcmFile.cxx @@ -1,10 +1,10 @@ -/*========================================================================= + /*========================================================================= Program: gdcm Module: $RCSfile: gdcmFile.cxx,v $ Language: C++ - Date: $Date: 2004/09/13 07:49:36 $ - Version: $Revision: 1.125 $ + Date: $Date: 2004/09/20 18:14:23 $ + Version: $Revision: 1.126 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -43,7 +43,7 @@ gdcmFile::gdcmFile(gdcmHeader *header) { Header = header; SelfHeader = false; - SetInitialValues(); + SaveInitialValues(); } /** @@ -65,15 +65,34 @@ gdcmFile::gdcmFile(std::string const & filename ) { Header = new gdcmHeader( filename ); SelfHeader = true; - SetInitialValues(); + SaveInitialValues(); } /** * \ingroup gdcmFile - * \brief Sets some initial for the Constructor + * \brief canonical destructor + * \note If the gdcmHeader was created by the gdcmFile constructor, + * it is destroyed by the gdcmFile */ -void gdcmFile::SetInitialValues() -{ +gdcmFile::~gdcmFile() +{ + if( SelfHeader ) + { + delete Header; + } + Header = 0; + + DeleteInitialValues(); +} + +/** + * \ingroup gdcmFile + * \brief Sets some initial values for the Constructor + * \warning not end user intended + */ +void gdcmFile::SaveInitialValues() +{ + PixelRead = -1; // no ImageData read yet. LastAllocatedPixelDataLength = 0; Pixel_Data = 0; @@ -82,6 +101,7 @@ void gdcmFile::SetInitialValues() InitialPhotInt = ""; InitialPlanConfig = ""; InitialBitsAllocated = ""; + InitialHighBit = ""; InitialRedLUTDescr = 0; InitialGreenLUTDescr = 0; @@ -100,14 +120,17 @@ void gdcmFile::SetInitialValues() InitialSpp = Header->GetEntryByNumber(0x0028,0x0002); InitialPhotInt = Header->GetEntryByNumber(0x0028,0x0004); InitialPlanConfig = Header->GetEntryByNumber(0x0028,0x0006); + InitialBitsAllocated = Header->GetEntryByNumber(0x0028,0x0100); - + InitialHighBit = Header->GetEntryByNumber(0x0028,0x0102); + // the following entries *may* be removed from the H table // (NOT deleted ...) by gdcmFile::GetImageDataIntoVectorRaw // we keep a pointer on them. InitialRedLUTDescr = Header->GetDocEntryByNumber(0x0028,0x1101); InitialGreenLUTDescr = Header->GetDocEntryByNumber(0x0028,0x1102); InitialBlueLUTDescr = Header->GetDocEntryByNumber(0x0028,0x1103); + InitialRedLUTData = Header->GetDocEntryByNumber(0x0028,0x1201); InitialGreenLUTData = Header->GetDocEntryByNumber(0x0028,0x1202); InitialBlueLUTData = Header->GetDocEntryByNumber(0x0028,0x1203); @@ -116,23 +139,135 @@ void gdcmFile::SetInitialValues() /** * \ingroup gdcmFile - * \brief canonical destructor - * \note If the gdcmHeader was created by the gdcmFile constructor, - * it is destroyed by the gdcmFile + * \brief restores some initial values + * \warning not end user intended */ -gdcmFile::~gdcmFile() -{ - if( SelfHeader ) - { - delete Header; +void gdcmFile::RestoreInitialValues() +{ + if ( Header->IsReadable() ) + { + // the following values *may* have been modified + // by gdcmFile::GetImageDataIntoVectorRaw + // we restore their initial value. + if ( InitialSpp != "") + Header->SetEntryByNumber(InitialSpp,0x0028,0x0002); + if ( InitialPhotInt != "") + Header->SetEntryByNumber(InitialPhotInt,0x0028,0x0004); + if ( InitialPlanConfig != "") + + Header->SetEntryByNumber(InitialPlanConfig,0x0028,0x0006); + if ( InitialBitsAllocated != "") + Header->SetEntryByNumber(InitialBitsAllocated,0x0028,0x0100); + if ( InitialHighBit != "") + Header->SetEntryByNumber(InitialHighBit,0x0028,0x0102); + + // the following entries *may* be have been removed from the H table + // (NOT deleted ...) by gdcmFile::GetImageDataIntoVectorRaw + // we restore them. + + if (InitialRedLUTDescr) + Header->AddEntry(InitialRedLUTDescr); + if (InitialGreenLUTDescr) + Header->AddEntry(InitialGreenLUTDescr); + if (InitialBlueLUTDescr) + Header->AddEntry(InitialBlueLUTDescr); + + if (InitialRedLUTData) + Header->AddEntry(InitialBlueLUTDescr); + if (InitialGreenLUTData) + Header->AddEntry(InitialGreenLUTData); + if (InitialBlueLUTData) + Header->AddEntry(InitialBlueLUTData); } - Header = 0; +} + +/** + * \ingroup gdcmFile + * \brief delete initial values (il they were saved) + * of InitialLutDescriptors and InitialLutData + */ +void gdcmFile::DeleteInitialValues() +{ // InitialLutDescriptors and InitialLutData // will have to be deleted if the don't belong any longer -// to the Header H table ... +// to the Header H table when the header is deleted... + + if ( InitialRedLUTDescr ) + delete InitialRedLUTDescr; + + if ( InitialGreenLUTDescr ) + delete InitialGreenLUTDescr; + + if ( InitialBlueLUTDescr ) + delete InitialBlueLUTDescr; + + if ( InitialRedLUTData ) + delete InitialRedLUTData; + + if ( InitialGreenLUTData != NULL) + delete InitialGreenLUTData; + + if ( InitialBlueLUTData != NULL) + delete InitialBlueLUTData; } +/** + * \ingroup gdcmFile + * \brief drop palette related initial values -if any- + * (InitialLutDescriptors and InitialLutData) + * out of header, to make it consistent with the Pixel_Data + * as it's loaded in memory + */ + +//FIXME : Should be nice, if we could let it here. +// will be moved to PixelData class +// Now, the job is done in gdcmHeader.cxx + + /* +void gdcmFile::DropInitialValues() +{ + gdcmHeader* h=GetHeader(); + if ( GetEntryByNumber(0x0028,0x0002).c_str()[0] == '3' ) + { + // if SamplesPerPixel = 3, sure we don't need any LUT ! + // Drop 0028|1101, 0028|1102, 0028|1103 + // Drop 0028|1201, 0028|1202, 0028|1203 + + gdcmDocEntry* e = h->GetDocEntryByNumber(0x0028,0x01101); + if (e) + { + h->RemoveEntryNoDestroy(e); + } + e = h->GetDocEntryByNumber(0x0028,0x1102); + if (e) + { + h->RemoveEntryNoDestroy(e); + } + e = h->GetDocEntryByNumber(0x0028,0x1103); + if (e) + { + h->RemoveEntryNoDestroy(e); + } + e = h->GetDocEntryByNumber(0x0028,0x01201); + if (e) + { + h->RemoveEntryNoDestroy(e); + } + e = h->GetDocEntryByNumber(0x0028,0x1202); + if (e) + { + h->RemoveEntryNoDestroy(e); + } + e = h->GetDocEntryByNumber(0x0028,0x1203); + if (e) + { + h->RemoveEntryNoDestroy(e); + } + } +} +*/ + //----------------------------------------------------------------------------- // Print @@ -412,21 +547,9 @@ size_t gdcmFile::GetImageDataIntoVectorRaw (void *destination, size_t maxSize) // in order to be able to restore the header in a disk-consistent state // (if user asks twice to get the pixels from disk) - if ( PixelRead == -1 ) // File was never "read" before + if ( PixelRead != -1 ) // File was "read" before { - InitialSpp = Header->GetEntryByNumber(0x0028,0x0002); - InitialPhotInt = Header->GetEntryByNumber(0x0028,0x0004); - InitialPlanConfig = Header->GetEntryByNumber(0x0028,0x0006); - InitialBitsAllocated = Header->GetEntryByNumber(0x0028,0x0100); - } - else // File was already "read", the following *may* have been modified - // we restore them to be in a disk-consistent state - { - // FIXME : What happened with the LUTs ? - Header->SetEntryByNumber(InitialSpp,0x0028,0x0002); - Header->SetEntryByNumber(InitialPhotInt,0x0028,0x0004); - Header->SetEntryByNumber(InitialPlanConfig,0x0028,0x0006); - Header->SetEntryByNumber(InitialBitsAllocated,0x0028,0x0100); + RestoreInitialValues(); } PixelRead = 1 ; // PixelRaw @@ -666,20 +789,23 @@ size_t gdcmFile::GetImageDataIntoVectorRaw (void *destination, size_t maxSize) } // now, it's an RGB image // Lets's write it in the Header + + // Droping Palette Color out of the Header + // has been moved to the Write process. - // CreateOrReplaceIfExist ? + // TODO : move 'values' modification to the write process + // : save also (in order to be able to restore) + // : 'high bit' -when not equal to 'bits stored' + 1 + // : 'bits allocated', when it's equal to 12 ?! std::string spp = "3"; // Samples Per Pixel std::string photInt = "RGB "; // Photometric Interpretation std::string planConfig = "0"; // Planar Configuration - - Header->SetEntryByNumber(spp,0x0028,0x0002); Header->SetEntryByNumber(photInt,0x0028,0x0004); Header->SetEntryByNumber(planConfig,0x0028,0x0006); - /// \todo Drop Palette Color out of the Header? return ImageDataSize; } diff --git a/src/gdcmFile.h b/src/gdcmFile.h index 667bb2d5..a1558cf2 100644 --- a/src/gdcmFile.h +++ b/src/gdcmFile.h @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmFile.h,v $ Language: C++ - Date: $Date: 2004/09/17 08:54:26 $ - Version: $Revision: 1.48 $ + Date: $Date: 2004/09/20 18:14:23 $ + Version: $Revision: 1.49 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -94,7 +94,7 @@ protected: private: void SwapZone(void* im, int swap, int lgr, int nb); - void SetInitialValues(); + bool ReadPixelData(void * destination); // For JPEG 8 Bits, body in file gdcmJpeg.cxx @@ -120,6 +120,11 @@ private: // How do we write that in C++ ?) static int gdcm_read_RLE_fragment(char **areaToRead, long lengthToDecode, long uncompressedSegmentSize, FILE *fp); + + void SaveInitialValues(); // will belong to the future gdcmPixelData class + void RestoreInitialValues(); // will belong to the future gdcmPixelData class + void DeleteInitialValues(); // will belong to the future gdcmPixelData class + // members variables: /// \brief Header to use to load the file @@ -129,30 +134,38 @@ private: /// the constructor or passed to the constructor. When false /// the destructor is in charge of deletion. bool SelfHeader; + + /// wether already parsed + bool Parsed; + + // + // --------------- Will be moved to a gdcmPixelData class + // /// \brief to hold the Pixels (when read) - void* Pixel_Data; // (was PixelData; should be removed) + void* Pixel_Data; // (was PixelData) - /// \brief Area length to receive the pixels + /// \brief Area length to receive the Gray Level pixels size_t ImageDataSizeRaw; - /// \brief Area length to receive the RGB pixels - /// from Grey Plane + Palette Color + /// \brief Area length to receive the pixels making RGB + /// from Plane R, Plane G, Plane B + /// or from Grey Plane + Palette Color + /// or from YBR Pixels (or from RGB Pixels, as well) size_t ImageDataSize; /// \brief ==1 if GetImageDataRaw was used /// ==0 if GetImageData was used /// ==-1 if ImageData never read int PixelRead; - - /// wether already parsed - bool Parsed; - + /// \brief length of the last allocated area devoided to receive Pixels /// ( to allow us not to (free + new) if un necessary ) size_t LastAllocatedPixelDataLength; // Initial values of some fields that can be modified during reading process + // (in a future stage, they will be modified just before the writting process + // and restored just after) // if user asked to transform gray level + LUT image into RGB image /// \brief Samples Per Pixel (0x0028,0x0002), as found on disk @@ -160,11 +173,21 @@ private: /// \brief Photometric Interpretation (0x0028,0x0004), as found on disk std::string InitialPhotInt; /// \brief Planar Configuration (0x0028,0x0006), as found on disk - std::string InitialPlanConfig; + std::string InitialPlanConfig; + + // Initial values of some fields that can be modified during reading process + // (in a future stage, they will be modified just before the writting process + // and restored just after) + // if the image was a 'strange' ACR-NEMA + // (Bits Allocated=12, High Bit not equal to Bits stored +1) /// \brief Bits Allocated (0x0028,0x0100), as found on disk std::string InitialBitsAllocated; - + /// \brief High Bit (0x0028,0x0102), as found on disk + std::string InitialHighBit; + // some DocEntry that can be moved out of the H table during reading process + // (in a future stage, they will be modified just before the writting process + // and restored just after) // if user asked to transform gray level + LUT image into RGB image // We keep a pointer on them for a future use. @@ -181,6 +204,10 @@ private: gdcmDocEntry* InitialGreenLUTData; /// \brief Blue Palette Color Lookup Table Data 0028 1203 as read gdcmDocEntry* InitialBlueLUTData; + + // + // --------------- end of future gdcmPixelData class + // }; diff --git a/src/gdcmHeader.cxx b/src/gdcmHeader.cxx index 5ecbbc98..30f8cf4b 100644 --- a/src/gdcmHeader.cxx +++ b/src/gdcmHeader.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: gdcmHeader.cxx,v $ Language: C++ - Date: $Date: 2004/09/14 16:47:08 $ - Version: $Revision: 1.186 $ + Date: $Date: 2004/09/20 18:14:23 $ + Version: $Revision: 1.187 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -120,7 +120,10 @@ void gdcmHeader::Write(FILE* fp,FileType filetype) delete[] dumm; ReplaceOrCreateByNumber(s_lgPix,GrPixel, 0x0000); } - + + // FIXME : should be nice if we could move it to gdcmFile + // (or in future gdcmPixelData class) + // Drop Palette Color, if necessary if ( GetEntryByNumber(0x0028,0x0002).c_str()[0] == '3' ) @@ -132,32 +135,32 @@ void gdcmHeader::Write(FILE* fp,FileType filetype) gdcmDocEntry* e = GetDocEntryByNumber(0x0028,0x01101); if (e) { - RemoveEntry(e); + RemoveEntryNoDestroy(e); } e = GetDocEntryByNumber(0x0028,0x1102); if (e) { - RemoveEntry(e); + RemoveEntryNoDestroy(e); } e = GetDocEntryByNumber(0x0028,0x1103); if (e) { - RemoveEntry(e); + RemoveEntryNoDestroy(e); } e = GetDocEntryByNumber(0x0028,0x01201); if (e) { - RemoveEntry(e); + RemoveEntryNoDestroy(e); } e = GetDocEntryByNumber(0x0028,0x1202); if (e) { - RemoveEntry(e); + RemoveEntryNoDestroy(e); } e = GetDocEntryByNumber(0x0028,0x1203); if (e) { - RemoveEntry(e); + RemoveEntryNoDestroy(e); } } gdcmDocument::Write(fp,filetype); -- 2.48.1