]> Creatis software - gdcm.git/blobdiff - Example/exCurveData.cxx
BUG: fix wrong sizeof
[gdcm.git] / Example / exCurveData.cxx
index 47ec1b3cdb88380f93e4d0a2bbb6ad3cefc32241..5fb9e07cd149fd58623e50d76685bb4c602466a1 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: exCurveData.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/10/18 08:35:43 $
-  Version:   $Revision: 1.4 $
+  Date:      $Date: 2008/03/10 13:12:09 $
+  Version:   $Revision: 1.8 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -67,6 +67,13 @@ inline size_t PrintCurveData(DataValueRepresentation* data, unsigned short numPt
    // ok this is ugly but I need the size outside of the function
    return sizeof(DataValueRepresentation);
 }
+
+template <int datarep> struct DataRepToType;
+template<> struct DataRepToType<0> { typedef unsigned short Type; };
+template<> struct DataRepToType<1> { typedef signed short Type; };
+template<> struct DataRepToType<2> { typedef float Type; };
+template<> struct DataRepToType<3> { typedef double Type; };
+template<> struct DataRepToType<4> { typedef signed long Type; };
  
 /*
  // Example (sorry, we've got no more than this one ...)
@@ -76,12 +83,12 @@ inline size_t PrintCurveData(DataValueRepresentation* data, unsigned short numPt
  * V 5004|0020 [CS]                  [Type of Data] [PHYSIO]
  * V 5004|0022 [LO]             [Curve Description] []
  * V 5004|0103 [US]     [Data Value Representation] [0] x(0)
- * B 5004|3000 [OW]                    [Curve Data] [gdcm::Binary data loaded;length = 1938]
+ * B 5004|3000 [OW]                    [Curve Data] [GDCM_NAME_SPACE::Binary data loaded;length = 1938]
  */
  
 int main(int argc, char *argv[])
 {  
-   gdcm::File *f;
+   GDCM_NAME_SPACE::File *f;
  
    std::cout << "------------------------------------------------" << std::endl;
    std::cout << "Gets the 'Curve Data' from a full gdcm-readable DICOM " << std::endl;
@@ -101,13 +108,13 @@ int main(int argc, char *argv[])
 //   Read the input image.
 // ============================================================
 
-   f = new gdcm::File( );
+   f = GDCM_NAME_SPACE::File::New( );
 
-   f->SetLoadMode(gdcm::LD_NOSEQ | gdcm::LD_NOSHADOW);
+   f->SetLoadMode(GDCM_NAME_SPACE::LD_NOSEQ | GDCM_NAME_SPACE::LD_NOSHADOW);
    f->SetFileName( fileName );
    bool res = f->Load();  
 
-   if( gdcm::Debug::GetDebugFlag() )
+   if( GDCM_NAME_SPACE::Debug::GetDebugFlag() )
    {
       std::cout << "---------------------------------------------" << std::endl;
       f->Print();
@@ -117,7 +124,7 @@ int main(int argc, char *argv[])
        std::cout << "Sorry, " << fileName <<"  not a gdcm-readable "
            << "DICOM / ACR File"
            <<std::endl;
-      delete f;
+      f->Delete();
       return 1;
    }
    std::cout << " ... is readable " << std::endl;
@@ -126,12 +133,12 @@ int main(int argc, char *argv[])
 //   Check whether image contains Overlays ACR-NEMA style.
 // ============================================================
 
-   //* B 5004|3000 [OW]                    [Curve Data] [gdcm::Binary data loaded;length = 1938]
+   //* B 5004|3000 [OW]                    [Curve Data] [GDCM_NAME_SPACE::Binary data loaded;length = 1938]
    std::string curve_data_str = f->GetEntryString(0x5004, 0x3000);
-   if (curve_data_str == gdcm::GDCM_UNFOUND)
+   if (curve_data_str == GDCM_NAME_SPACE::GDCM_UNFOUND)
    {
       std::cout << " Image doesn't contain any Curve Data" << std::endl;
-      delete f;
+      f->Delete();
       return 1;
    }
    std::cout << " File is read! " << std::endl;
@@ -169,43 +176,49 @@ int main(int argc, char *argv[])
    convert >> data_rep;
 
 
-   gdcm::DocEntry *pCurveDataDoc = f->GetDocEntry(0x5004, 0x3000);
-   gdcm::DataEntry *pCurveData = dynamic_cast<gdcm::DataEntry *>(pCurveDataDoc);
+   GDCM_NAME_SPACE::DocEntry *pCurveDataDoc = f->GetDocEntry(0x5004, 0x3000);
+   GDCM_NAME_SPACE::DataEntry *pCurveData = dynamic_cast<GDCM_NAME_SPACE::DataEntry *>(pCurveDataDoc);
    uint8_t *curve_data = pCurveData->GetBinArea();
    
    // From Part3, C.10.2.1.2 Data value representation (p668)
    size_t sz;
+   int sizeofdatarep = 0;
    switch( data_rep)
    {
       case 0:
-         sz = PrintCurveData((unsigned short*)(curve_data), num_points);
+         sz = PrintCurveData((DataRepToType<0>::Type*)(curve_data), num_points);
+         sizeofdatarep = sizeof( DataRepToType<0>::Type );
          break;
       case 1:
-         sz = PrintCurveData((signed short*)(curve_data), num_points);
+         sz = PrintCurveData((DataRepToType<1>::Type*)(curve_data), num_points);
+         sizeofdatarep = sizeof( DataRepToType<1>::Type );
          break;
       case 2:
-         sz = PrintCurveData((float*)(curve_data), num_points);
+         sz = PrintCurveData((DataRepToType<2>::Type*)(curve_data), num_points);
+         sizeofdatarep = sizeof( DataRepToType<2>::Type );
          break;
       case 3:
-         sz = PrintCurveData((double*)(curve_data), num_points);
+         sz = PrintCurveData((DataRepToType<3>::Type*)(curve_data), num_points);
+         sizeofdatarep = sizeof( DataRepToType<3>::Type );
          break;
       case 4:
-         sz = PrintCurveData((signed long*)(curve_data), num_points);
+         sz = PrintCurveData((DataRepToType<4>::Type*)(curve_data), num_points);
+         sizeofdatarep = sizeof( DataRepToType<4>::Type );
          break;
       default:
          std::cerr << "Error don't know the type: " << data_rep_str << std::endl;
-         delete f;
+         f->Delete();
          return 1;
    }
    // Just to make sure that values read are consistant and we won't read out of bound data:
-   assert( sz*num_points == pCurveData->GetLength());
+   assert( sz*num_points*sizeofdatarep == pCurveData->GetLength());
 
    // Write out the data as a file:
    //std::ofstream o("/tmp/curve_data.raw");
    //o.write((char*)curve_data, num_points*sz);
    //o.close();
 
-   delete f;
+   f->Delete();
    return 0;
 }