]> Creatis software - gdcm.git/commitdiff
ENH: Minor cleanups + ShowDicom now reread a written image
authormalaterre <malaterre>
Fri, 16 Jul 2004 15:18:05 +0000 (15:18 +0000)
committermalaterre <malaterre>
Fri, 16 Jul 2004 15:18:05 +0000 (15:18 +0000)
src/gdcmDocument.cxx
src/gdcmDocument.h
src/gdcmFile.cxx
src/gdcmFile.h

index c0cff42f3c799aef15868cf75c5dafc4abd48338..5b79c5f8dca126fa23f51cc451fbce1e1e995ff3 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmDocument.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/07/14 14:08:05 $
-  Version:   $Revision: 1.48 $
+  Date:      $Date: 2004/07/16 15:18:05 $
+  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
@@ -98,11 +98,12 @@ gdcmDocument::gdcmDocument(std::string const & inFilename,
                            bool exception_on_error,
                            bool enable_sequences,
                            bool ignore_shadow) 
-              : gdcmElementSet(-1)   {
-   IgnoreShadow   =ignore_shadow;
+              : gdcmElementSet(-1)
+{
+   IgnoreShadow = ignore_shadow;
    //enableSequences=enable_sequences;
    (void)enable_sequences;
-   enableSequences=true; // JPR // TODO : remove params out of the constructor
+   enableSequences = true; // JPR // TODO : remove params out of the constructor
    SetMaxSizeLoadEntry(MAX_SIZE_LOAD_ELEMENT_VALUE); 
    Filename = inFilename;
    Initialise();
@@ -132,7 +133,8 @@ gdcmDocument::gdcmDocument(std::string const & inFilename,
    // Load 'non string' values
       
    std::string PhotometricInterpretation = GetEntryByNumber(0x0028,0x0004);   
-   if( PhotometricInterpretation == "PALETTE COLOR " ) {
+   if( PhotometricInterpretation == "PALETTE COLOR " )
+   {
       LoadEntryVoidArea(0x0028,0x1200);  // gray LUT   
       LoadEntryVoidArea(0x0028,0x1201);  // R    LUT
       LoadEntryVoidArea(0x0028,0x1202);  // G    LUT
@@ -195,9 +197,10 @@ gdcmDocument::~gdcmDocument ()
    RefShaDict = NULL;
 
    // Recursive clean up of sequences
-   for (TagDocEntryHT::iterator it = tagHT.begin(); it != tagHT.end(); ++it )
+   for (TagDocEntryHT::const_iterator it = tagHT.begin(); 
+                                      it != tagHT.end(); ++it )
    { 
-         delete it->second;
+      delete it->second;
    }
    tagHT.clear();
 }
@@ -247,7 +250,7 @@ gdcmDict *gdcmDocument::GetShaDict()
  */
 bool gdcmDocument::SetShaDict(gdcmDict *dict)
 {
-   RefShaDict=dict;
+   RefShaDict = dict;
    return !RefShaDict;
 }
 
@@ -257,7 +260,7 @@ bool gdcmDocument::SetShaDict(gdcmDict *dict)
  */
 bool gdcmDocument::SetShaDict(DictKey dictName)
 {
-   RefShaDict=gdcmGlobal::GetDicts()->GetDict(dictName);
+   RefShaDict = gdcmGlobal::GetDicts()->GetDict(dictName);
    return !RefShaDict;
 }
 
@@ -269,22 +272,24 @@ bool gdcmDocument::SetShaDict(DictKey dictName)
  * @return true when gdcmDocument is the one of a reasonable Dicom/Acr file,
  *         false otherwise. 
  */
-bool gdcmDocument::IsReadable() { 
-
-   if(Filetype==gdcmUnknown) {
+bool gdcmDocument::IsReadable()
+{
+   if( Filetype == gdcmUnknown)
+   {
       std::cout << " gdcmDocument::IsReadable: Filetype " << Filetype
                << " " << "gdcmUnknown " << gdcmUnknown << std::endl; //JPR
       dbg.Verbose(0, "gdcmDocument::IsReadable: wrong filetype");
       return false;
    }
 
-   if(tagHT.empty()) { 
+   if(tagHT.empty())
+   {
       dbg.Verbose(0, "gdcmDocument::IsReadable: no tags in internal"
                      " hash table.");
       return false;
    }
 
-   return(true);
+   return true;
 }
 
 
@@ -296,27 +301,32 @@ bool gdcmDocument::IsReadable() {
  *          the current document. False either when the document contains
  *          no Transfer Syntax, or when the Tranfer Syntaxes don't match.
  */
-bool gdcmDocument::IsGivenTransferSyntax(const std::string & SyntaxToCheck)
+bool gdcmDocument::IsGivenTransferSyntax(std::string const & syntaxToCheck)
 {
-   gdcmDocEntry *Entry = GetDocEntryByNumber(0x0002, 0x0010);
-   if ( !Entry )
+   gdcmDocEntry *entry = GetDocEntryByNumber(0x0002, 0x0010);
+   if ( !entry )
+   {
       return false;
+   }
 
    // The entry might be present but not loaded (parsing and loading
    // happen at differente stages): try loading and proceed with check...
-   LoadDocEntrySafe(Entry);
-   if (gdcmValEntry* ValEntry = dynamic_cast< gdcmValEntry* >(Entry) )
+   LoadDocEntrySafe(entry);
+   if (gdcmValEntry* valEntry = dynamic_cast< gdcmValEntry* >(entry) )
    {
-      std::string Transfer = ValEntry->GetValue();
+      std::string transfer = valEntry->GetValue();
+
       // The actual transfer (as read from disk) might be padded. We
       // first need to remove the potential padding. We can make the
       // weak assumption that padding was not executed with digits...
-      while ( ! isdigit(Transfer[Transfer.length()-1]) )
+      while ( ! isdigit(transfer[transfer.length()-1]) )
       {
-         Transfer.erase(Transfer.length()-1, 1);
+         transfer.erase(transfer.length()-1, 1);
       }
-      if ( Transfer == SyntaxToCheck )
+      if ( transfer == syntaxToCheck )
+      {
          return true;
+      }
    }
    return false;
 }
@@ -451,7 +461,7 @@ bool gdcmDocument::IsDicomV3()
    // Anyway, it's to late check if the 'Preamble' was found ...
    // And ... would it be a rich idea to check ?
    // (some 'no Preamble' DICOM images exist !)
-   return (GetDocEntryByNumber(0x0002, 0x0010) != NULL);
+   return GetDocEntryByNumber(0x0002, 0x0010) != NULL;
 }
 
 /**
@@ -472,12 +482,14 @@ FileType gdcmDocument::GetFileType()
 FILE *gdcmDocument::OpenFile(bool exception_on_error)
   throw(gdcmFileError) 
 {
-  fp=fopen(Filename.c_str(),"rb");
+  fp = fopen(Filename.c_str(),"rb");
 
   if(!fp)
   {
-     if(exception_on_error) 
+     if(exception_on_error)
+     {
         throw gdcmFileError("gdcmDocument::gdcmDocument(const char *, bool)");
+     }
      else
      {
         dbg.Verbose(0, "gdcmDocument::OpenFile cannot open file: ",
@@ -493,22 +505,28 @@ FILE *gdcmDocument::OpenFile(bool exception_on_error)
 
     //ACR -- or DICOM with no Preamble --
     if( zero == 0x0008 || zero == 0x0800 || zero == 0x0002 || zero == 0x0200)
+    {
        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 fp;
+    }
 
     fclose(fp);
     dbg.Verbose(0, "gdcmDocument::OpenFile not DICOM/ACR", Filename.c_str());
   }
-  else {
+  else
+  {
     dbg.Verbose(0, "gdcmDocument::OpenFile cannot open file", Filename.c_str());
   }
-  return NULL;
+
+  return 0;
 }
 
 /**
@@ -519,9 +537,8 @@ bool gdcmDocument::CloseFile()
 {
   int closed = fclose(fp);
   fp = (FILE *)0;
-  if (! closed)
-     return false;
-  return true;
+
+  return closed;
 }
 
 /**
@@ -533,7 +550,6 @@ 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
    
@@ -595,26 +611,25 @@ void gdcmDocument::Write(FILE* fp,FileType filetype)
  */
   
 gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber(
-                                         std::string Value, 
-                                         uint16_t Group, 
-                                         uint16_t Elem )
+                                         std::string value, 
+                                         uint16_t group, 
+                                         uint16_t elem )
 {
-   gdcmDocEntry* CurrentEntry;
-   gdcmValEntry* ValEntry;
+   gdcmValEntry* valEntry;
 
-   CurrentEntry = GetDocEntryByNumber( Group, Elem);
-   if (!CurrentEntry)
+   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)
+      currentEntry = NewDocEntryByNumber(group, elem);
+      if (!currentEntry)
       {
          dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: call to"
                         " NewDocEntryByNumber failed.");
          return NULL;
       }
-      ValEntry = new gdcmValEntry(CurrentEntry);
-      if ( !AddEntry(ValEntry))
+      valEntry = new gdcmValEntry(currentEntry);
+      if ( !AddEntry(valEntry))
       {
          dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: AddEntry"
                         " failed allthough this is a creation.");
@@ -622,18 +637,18 @@ gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber(
    }
    else
    {
-      ValEntry = dynamic_cast< gdcmValEntry* >(CurrentEntry);
-      if ( !ValEntry )
+      valEntry = dynamic_cast< gdcmValEntry* >(currentEntry);
+      if ( !valEntry )
       {
          // We need to promote the gdcmDocEntry to a gdcmValEntry:
-         ValEntry = new gdcmValEntry(CurrentEntry);
-         if (!RemoveEntry(CurrentEntry))
+         valEntry = new gdcmValEntry(currentEntry);
+         if (!RemoveEntry(currentEntry))
          {
             dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: removal"
                            " of previous DocEntry failed.");
             return NULL;
          }
-         if ( !AddEntry(ValEntry))
+         if ( !AddEntry(valEntry))
          {
             dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: adding"
                            " promoted ValEntry failed.");
@@ -642,9 +657,9 @@ gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber(
       }
    }
 
-   SetEntryByNumber(Value, Group, Elem);
+   SetEntryByNumber(value, group, elem);
 
-   return ValEntry;
+   return valEntry;
 }   
 
 /*
@@ -659,22 +674,25 @@ gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber(
 gdcmBinEntry * gdcmDocument::ReplaceOrCreateByNumber(
                                          void *voidArea,
                                          int lgth, 
-                                         uint16_t Group, 
-                                         uint16_t Elem)
+                                         uint16_t group, 
+                                         uint16_t elem)
 {
-   gdcmDocEntry* a;
    gdcmBinEntry* b = 0;
-   a = GetDocEntryByNumber( Group, Elem);
-   if (a == NULL) {
-      a =NewBinEntryByNumber(Group, Elem);
-      if (a == NULL) 
-         return NULL;
+   gdcmDocEntry* a = GetDocEntryByNumber( group, elem);
+   if (!a)
+   {
+      a = NewBinEntryByNumber(group, elem);
+      if (!a)
+      {
+         return 0;
+      }
 
       b = new gdcmBinEntry(a);
       AddEntry(b);
+      b->SetVoidArea(voidArea);
    }   
-   SetEntryByNumber(voidArea, lgth, Group, Elem);
-   b->SetVoidArea(voidArea);
+   SetEntryByNumber(voidArea, lgth, group, elem);
+   //b->SetVoidArea(voidArea);  //what if b == 0 !!
 
    return b;
 }  
@@ -687,11 +705,11 @@ gdcmBinEntry * gdcmDocument::ReplaceOrCreateByNumber(
  * @param Elem element number of the Entry
  * \return  boolean 
  */
-bool gdcmDocument::ReplaceIfExistByNumber(char* Value, uint16_t Group,
-                                                       uint16_t Elem ) 
+bool gdcmDocument::ReplaceIfExistByNumber(const char* value, uint16_t group,
+                                          uint16_t elem ) 
 {
-   std::string v = Value;
-   SetEntryByNumber(v, Group, Elem);
+   std::string v = value;
+   SetEntryByNumber(v, group, elem);
    return true;
 } 
 
@@ -722,8 +740,10 @@ int gdcmDocument::CheckIfEntryExistByNumber(uint16_t group, uint16_t element )
 std::string gdcmDocument::GetEntryByName(TagName tagName)
 {
    gdcmDictEntry *dictEntry = RefPubDict->GetDictEntryByName(tagName); 
-   if( dictEntry == NULL)
+   if( !dictEntry )
+   {
       return GDCM_UNFOUND;
+   }
 
    return GetEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement());
 }
index c44e1c9bb323d1bd8f8c46e6fa50957b57cb0fa4..db1fe1848473d04b4225ac0d7557e8142a71f7cb 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmDocument.h,v $
   Language:  C++
-  Date:      $Date: 2004/07/02 13:55:27 $
-  Version:   $Revision: 1.21 $
+  Date:      $Date: 2004/07/16 15:18:05 $
+  Version:   $Revision: 1.22 $
  
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -134,14 +134,14 @@ public:
    FILE* OpenFile(bool exception_on_error = false) throw(gdcmFileError);
    bool CloseFile();
 
-   void Write(FILE* fp,FileType type);
+   void Write(FILE* fp, FileType type);
 
-   gdcmValEntry* ReplaceOrCreateByNumber(std::string Value,
-                                          uint16_t Group, uint16_t Elem);
+   gdcmValEntry* ReplaceOrCreateByNumber(std::string value,
+                                          uint16_t group, uint16_t elem);
 
    gdcmBinEntry* ReplaceOrCreateByNumber(void *voidArea, int lgth,
-                                          uint16_t Group, uint16_t Elem);
-   bool ReplaceIfExistByNumber (char* Value, uint16_t Group, uint16_t Elem);
+                                          uint16_t group, uint16_t elem);
+   bool ReplaceIfExistByNumber (const char* value, uint16_t group, uint16_t elem);
    
    virtual void* LoadEntryVoidArea(uint16_t Group, uint16_t Element);
    virtual void* LoadEntryVoidArea(gdcmBinEntry*);
@@ -241,10 +241,10 @@ public:
    void SetPrintLevel(int level) { printLevel = level; }
 
    /// Accessor to \ref Filename
-   std::string &GetFileName() { return Filename; }
+   const std::string &GetFileName() { return Filename; }
 
    /// Accessor to \ref Filename
-   void SetFileName(const char* fileName) { Filename = fileName; }
+   void SetFileName(std::string const & fileName) { Filename = fileName; }
 
    /// Accessor to \ref gdcmElementSet::tagHT
    TagDocEntryHT &GetEntry() { return tagHT; };
index ade5daf0a49c2aef3ceaf8ec30f729ac101e0463..8221bd388d21676f8b136f67a19089f411c128cf 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmFile.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/07/02 13:55:28 $
-  Version:   $Revision: 1.115 $
+  Date:      $Date: 2004/07/16 15:18:05 $
+  Version:   $Revision: 1.116 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -263,7 +263,7 @@ size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t maxSize)
    // from Lut R + Lut G + Lut B
    uint8_t *newDest = new uint8_t[ImageDataSize];
    uint8_t *a       = (uint8_t *)destination;
-   uint8_t *lutRGBA =  Header->GetLUTRGBA();
+   uint8_t *lutRGBA = Header->GetLUTRGBA();
 
    if ( lutRGBA )
    {
@@ -288,7 +288,7 @@ size_t gdcmFile::GetImageDataIntoVector (void* destination, size_t maxSize)
 
    std::string spp = "3";        // Samples Per Pixel
    Header->SetEntryByNumber(spp,0x0028,0x0002);
-   std::string rgb= "RGB ";      // Photometric Interpretation
+   std::string rgb = "RGB ";      // Photometric Interpretation
    Header->SetEntryByNumber(rgb,0x0028,0x0004);
    std::string planConfig = "0"; // Planar Configuration
    Header->SetEntryByNumber(planConfig,0x0028,0x0006);
@@ -330,7 +330,6 @@ void * gdcmFile::GetImageDataRaw ()
    if ( Header->HasLUT() )
    {
       /// \todo Let gdcmHeader user a chance to get the right value
-      // ImageDataSize /= 3;  //dangerous
       imgDataSize = ImageDataSizeRaw;
    }
 
@@ -661,14 +660,13 @@ bool gdcmFile::SetImageData(void *inData, size_t expectedSize)
 
 bool gdcmFile::WriteRawData(std::string const & fileName)
 {
-   FILE *fp1;
-   fp1 = fopen(fileName.c_str(), "wb");
+   FILE *fp1 = fopen(fileName.c_str(), "wb");
    if (fp1 == NULL)
    {
       printf("Fail to open (write) file [%s] \n", fileName.c_str());
       return false;
    }
-   fwrite (PixelData,ImageDataSize, 1, fp1);
+   fwrite (PixelData, ImageDataSize, 1, fp1);
    fclose (fp1);
 
    return true;
@@ -736,14 +734,12 @@ bool gdcmFile::WriteAcr (std::string const & fileName)
  */
 bool gdcmFile::WriteBase (std::string const & fileName, FileType type)
 {
-   FILE *fp1;
-
    if ( PixelRead == -1 && type != gdcmExplicitVR)
    {
       return false;
    }
 
-   fp1 = fopen(fileName.c_str(), "wb");
+   FILE *fp1 = fopen(fileName.c_str(), "wb");
    if (fp1 == NULL)
    {
       printf("Failed to open (write) File [%s] \n", fileName.c_str());
@@ -785,9 +781,8 @@ bool gdcmFile::WriteBase (std::string const & fileName, FileType type)
    uint16_t grPixel  = Header->GetGrPixel();
    uint16_t numPixel = Header->GetNumPixel();;
           
-   gdcmDocEntry* PixelElement;
-
-   PixelElement = GetHeader()->GetDocEntryByNumber(grPixel, numPixel);  
+   gdcmDocEntry* PixelElement = 
+      GetHeader()->GetDocEntryByNumber(grPixel, numPixel);  
  
    if ( PixelRead == 1 )
    {
@@ -980,7 +975,7 @@ bool gdcmFile::ReadPixelData(void *destination)
    // ---------------------- Run Length Encoding
    if ( Header->IsRLELossLessTransferSyntax() )
    {
-      bool res = (bool)gdcm_read_RLE_file (fp,destination);
+      bool res = gdcm_read_RLE_file (fp,destination);
       Header->CloseFile();
       return res; 
    }  
index a8926b72541d8532428b8218e0eca0964893d487..be828e5ecafdb9f600da74a22c23661f897f0e5d 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmFile.h,v $
   Language:  C++
-  Date:      $Date: 2004/07/02 13:55:28 $
-  Version:   $Revision: 1.37 $
+  Date:      $Date: 2004/07/16 15:18:05 $
+  Version:   $Revision: 1.38 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -47,9 +47,9 @@ public:
    size_t GetImageDataSizeRaw();
 
    void * GetImageData();
-   size_t GetImageDataIntoVector(void* destination, size_t MaxSize);
+   size_t GetImageDataIntoVector(void* destination, size_t maxSize);
    void * GetImageDataRaw();
-   size_t GetImageDataIntoVectorRaw(void* destination, size_t MaxSize);
+   size_t GetImageDataIntoVectorRaw(void* destination, size_t maxSize);
 
       // Allocates ExpectedSize bytes of memory at this->Data and copies the
       // pointed data to it. Copying the image might look useless but
@@ -58,7 +58,7 @@ public:
       // e.g. VTK) before calling the Write
       
    // voir gdcmHeader::SetImageDataSize ?!?         
-   bool SetImageData     (void * Data, size_t ExpectedSize);
+   bool SetImageData (void * data, size_t expectedSize);
 
    /// \todo When the caller is aware we simply point to the data:
    /// int SetImageDataNoCopy (void * Data, size_t ExpectedSize);
@@ -78,12 +78,12 @@ public:
    virtual bool SetEntryByNumber(std::string const & content,
                                  uint16_t group, uint16_t element)
    { 
-      GetHeader()->SetEntryByNumber(content,group,element);
-      return true;  /// \todo default behavior ?
+      Header->SetEntryByNumber(content,group,element);
+      return true;
    }
      
 protected:
-   bool WriteBase(std::string const & FileName, FileType type);
+   bool WriteBase(std::string const & fileName, FileType type);
 
 private:
    void SwapZone(void* im, int swap, int lgr, int nb);
@@ -91,16 +91,16 @@ private:
    bool ReadPixelData(void * destination);
    
    // For JPEG 8 Bits, body in file gdcmJpeg.cxx
-   bool gdcm_read_JPEG_file     (FILE *fp,void * image_buffer); 
+   bool gdcm_read_JPEG_file     (FILE *fp, void * image_buffer); 
    static int gdcm_read_RLE_fragment(char **areaToRead, long lengthToDecode, 
-                                     long uncompressedSegmentSize,FILE *fp);
+                                     long uncompressedSegmentSize, FILE *fp);
    // For JPEG 12 Bits, body in file gdcmJpeg12.cxx
-   bool gdcm_read_JPEG_file12   (FILE *fp,void * image_buffer);
+   bool gdcm_read_JPEG_file12   (FILE *fp, void* image_buffer);
    // For JPEG 2000, body in file gdcmJpeg2000.cxx
-   bool gdcm_read_JPEG2000_file (FILE *fp,void * image_buffer);
+   bool gdcm_read_JPEG2000_file (FILE *fp, void* image_buffer);
 
    // For Run Length Encoding (TOCHECK)
-   bool gdcm_read_RLE_file      (FILE *fp,void * image_buffer); 
+   bool gdcm_read_RLE_file      (FILE *fp, void* image_buffer); 
 
 // Variables