]> Creatis software - gdcm.git/blobdiff - src/gdcmFile.cxx
COMP: Fix comp on gcc295
[gdcm.git] / src / gdcmFile.cxx
index 60c86429471f044929d325fd96a35d72c336410d..035e25c3d5935b736cd172d9be5fe46558427c41 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmFile.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/11/25 10:24:34 $
-  Version:   $Revision: 1.163 $
+  Date:      $Date: 2004/11/30 14:17:52 $
+  Version:   $Revision: 1.167 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -17,6 +17,7 @@
 =========================================================================*/
 
 #include "gdcmFile.h"
+#include "gdcmDocument.h"
 #include "gdcmDebug.h"
 #include "gdcmUtil.h"
 #include "gdcmBinEntry.h"
@@ -396,14 +397,31 @@ uint8_t* File::GetLutRGBA()
  */
 bool File::WriteBase (std::string const & fileName, FileType type)
 {
-   std::ofstream* fp1 = new std::ofstream(fileName.c_str(), 
-                              std::ios::out | std::ios::binary);
-   if (fp1 == NULL)
+   switch(type)
    {
-      dbg.Verbose(2, "Failed to open (write) File: " , fileName.c_str());
-      return false;
+      case ImplicitVR:
+         SetWriteFileTypeToImplicitVR();
+         break;
+      case ExplicitVR:
+         SetWriteFileTypeToExplicitVR();
+         break;
+      case ACR:
+      case ACR_LIBIDO:
+         SetWriteFileTypeToACR();
+         break;
+      default:
+         SetWriteFileTypeToExplicitVR();
    }
 
+   if( type == ACR_LIBIDO )
+   {
+      SetWriteToLibido();
+   }
+   else
+   {
+      SetWriteToNoLibido();
+   }
+  
    switch(WriteMode)
    {
       case WMODE_NATIVE :
@@ -431,10 +449,10 @@ bool File::WriteBase (std::string const & fileName, FileType type)
    }*/
    // ----------------- End of Special Patch ----------------
 
-   bool check=CheckWriteIntegrity();
+   bool check = CheckWriteIntegrity();
    if(check)
    {
-      HeaderInternal->Write(fp1,type);
+      check = HeaderInternal->Write(fileName,type);
    }
 
    // --------------------------------------------------------------
@@ -449,9 +467,8 @@ bool File::WriteBase (std::string const & fileName, FileType type)
    // ----------------- End of Special Patch ----------------
 
    RestoreWrite();
-
-   fp1->close();
-   delete fp1;
+   RestoreWriteFileType();
+   RestoreWriteOfLibido();
 
    return check;
 }
@@ -468,21 +485,38 @@ bool File::CheckWriteIntegrity()
 {
    if(Pixel_Data)
    {
+      int numberBitsAllocated = HeaderInternal->GetBitsAllocated();
+      if ( numberBitsAllocated == 0 || numberBitsAllocated == 12 )
+      {
+         numberBitsAllocated = 16;
+      }
+
+      size_t decSize = HeaderInternal->GetXSize()
+                    * HeaderInternal->GetYSize() 
+                    * HeaderInternal->GetZSize()
+                    * ( numberBitsAllocated / 8 )
+                    * HeaderInternal->GetSamplesPerPixel();
+      size_t rgbSize = decSize;
+      if( HeaderInternal->HasLUT() )
+         rgbSize = decSize * 3;
+
       switch(WriteMode)
       {
          case WMODE_NATIVE :
             break;
          case WMODE_DECOMPRESSED :
-            if(GetImageDataRawSize()!=ImageDataSize)
+            if( decSize!=ImageDataSize )
             {
-               std::cerr<<"RAW : "<<GetImageDataRawSize()<<" / "<<ImageDataSize<<std::endl;
+               dbg.Verbose(0, "File::CheckWriteIntegrity: Data size is incorrect");
+               //std::cerr<<"Dec : "<<decSize<<" | "<<ImageDataSize<<std::endl;
                return false;
             }
             break;
          case WMODE_RGB :
-            if(GetImageDataSize()!=ImageDataSize)
+            if( rgbSize!=ImageDataSize )
             {
-               std::cerr<<"RGB : "<<GetImageDataSize()<<" / "<<ImageDataSize<<std::endl;
+               dbg.Verbose(0, "File::CheckWriteIntegrity: Data size is incorrect");
+               //std::cerr<<"RGB : "<<decSize<<" | "<<ImageDataSize<<std::endl;
                return false;
             }
             break;
@@ -641,6 +675,38 @@ void File::RestoreWrite()
    Archive->Restore(0x0028,0x1203);
 }
 
+void File::SetWriteFileTypeToACR()
+{
+   Archive->Push(0x0002,0x0010);
+}
+
+void File::SetWriteFileTypeToExplicitVR()
+{
+   std::string ts = Util::DicomString( 
+      Document::GetTransferSyntaxValue(ExplicitVRLittleEndian).c_str() );
+
+   ValEntry* tss = CopyValEntry(0x0002,0x0010);
+   tss->SetValue(ts);
+   tss->SetLength(ts.length());
+
+   Archive->Push(tss);
+}
+
+void File::SetWriteFileTypeToImplicitVR()
+{
+   std::string ts = Util::DicomString(
+      Document::GetTransferSyntaxValue(ImplicitVRLittleEndian).c_str() );
+
+   ValEntry* tss = CopyValEntry(0x0002,0x0010);
+   tss->SetValue(ts);
+   tss->SetLength(ts.length());
+}
+
+void File::RestoreWriteFileType()
+{
+   Archive->Restore(0x0002,0x0010);
+}
+
 void File::SetWriteToLibido()
 {
    ValEntry *oldRow = dynamic_cast<ValEntry *>(HeaderInternal->GetDocEntryByNumber(0x0028, 0x0010));
@@ -662,12 +728,33 @@ void File::SetWriteToLibido()
       Archive->Push(newRow);
       Archive->Push(newCol);
    }
+
+   ValEntry *libidoCode = CopyValEntry(0x0008,0x0010);
+   libidoCode->SetValue("ACRNEMA_LIBIDO_1.1");
+   libidoCode->SetLength(10);
+   Archive->Push(libidoCode);
+}
+
+void File::SetWriteToNoLibido()
+{
+   ValEntry *recCode = dynamic_cast<ValEntry *>(HeaderInternal->GetDocEntryByNumber(0x0008,0x0010));
+   if( recCode )
+   {
+      if( recCode->GetValue() == "ACRNEMA_LIBIDO_1.1" )
+      {
+         ValEntry *libidoCode = CopyValEntry(0x0008,0x0010);
+         libidoCode->SetValue("");
+         libidoCode->SetLength(0);
+         Archive->Push(libidoCode);
+      }
+   }
 }
 
-void File::RestoreWriteFromLibido()
+void File::RestoreWriteOfLibido()
 {
    Archive->Restore(0x0028,0x0010);
    Archive->Restore(0x0028,0x0011);
+   Archive->Restore(0x0008,0x0010);
 }
 
 ValEntry* File::CopyValEntry(uint16_t group,uint16_t element)
@@ -709,23 +796,6 @@ BinEntry* File::CopyBinEntry(uint16_t group,uint16_t element)
 
 //-----------------------------------------------------------------------------
 // Private
-/**
- * \brief Set the pixel datas in the good entry of the Header
- */
-void File::SetPixelData(uint8_t* data)
-{
-   GetHeader()->SetEntryByNumber( GDCM_BINLOADED,
-      GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
-
-   // Will be 7fe0, 0010 in standard case
-   DocEntry* currentEntry = GetHeader()->GetDocEntryByNumber(GetHeader()->GetGrPixel(), GetHeader()->GetNumPixel());
-   if ( currentEntry )
-   {
-      if ( BinEntry* binEntry = dynamic_cast<BinEntry *>(currentEntry) )
-         // Flag is to false because datas are kept in the gdcmPixelConvert
-         binEntry->SetBinArea( data, false );
-   }
-}
 
 //-----------------------------------------------------------------------------
 } // end namespace gdcm