Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/examples/extended/medical/DICOM/src/DicomHandler.cc

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /examples/extended/medical/DICOM/src/DicomHandler.cc (Version 11.3.0) and /examples/extended/medical/DICOM/src/DicomHandler.cc (Version 9.2.p2)


  1 //                                                  1 //
  2 // *******************************************      2 // ********************************************************************
  3 // * License and Disclaimer                         3 // * License and Disclaimer                                           *
  4 // *                                                4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of th      5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided      6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License      7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/      8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.           9 // * include a list of copyright holders.                             *
 10 // *                                               10 // *                                                                  *
 11 // * Neither the authors of this software syst     11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing fin     12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warran     13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assum     14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file      15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitatio     16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                               17 // *                                                                  *
 18 // * This  code  implementation is the result      18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboratio     19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distri     20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  ag     21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publicati     22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Sof     23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // *******************************************     24 // ********************************************************************
 25 //                                                 25 //
 26 //                                             << 
 27 /// \file medical/DICOM/src/DicomHandler.cc    << 
 28 /// \brief Implementation of the DicomHandler  << 
 29 //                                             << 
 30 // The code was written by :                       26 // The code was written by :
 31 //      *Louis Archambault louis.archambault@p <<  27 //  *Louis Archambault louis.archambault@phy.ulaval.ca,
 32 //      *Luc Beaulieu beaulieu@phy.ulaval.ca       28 //      *Luc Beaulieu beaulieu@phy.ulaval.ca
 33 //      +Vincent Hubert-Tremblay at tigre.2@sy     29 //      +Vincent Hubert-Tremblay at tigre.2@sympatico.ca
 34 //                                                 30 //
 35 //                                                 31 //
 36 // *Centre Hospitalier Universitaire de Quebec     32 // *Centre Hospitalier Universitaire de Quebec (CHUQ),
 37 // Hotel-Dieu de Quebec, departement de Radio-     33 // Hotel-Dieu de Quebec, departement de Radio-oncologie
 38 // 11 cote du palais. Quebec, QC, Canada, G1R      34 // 11 cote du palais. Quebec, QC, Canada, G1R 2J6
 39 // tel (418) 525-4444 #6720                        35 // tel (418) 525-4444 #6720
 40 // fax (418) 691 5268                              36 // fax (418) 691 5268
 41 //                                                 37 //
 42 // + University Laval, Quebec (QC) Canada      <<  38 // + Universit.AŽé Laval, QuŽébec (QC) Canada
 43 //********************************************     39 //*******************************************************
 44 //                                                 40 //
 45 //********************************************     41 //*******************************************************
 46 //                                                 42 //
 47 /// DicomHandler.cc :                          <<  43 //*******************************************************
 48 ///        - Handling of DICM images           <<  44 //
 49 ///         - Reading headers and pixels       <<  45 // DicomHandler.cc :
 50 ///        - Transforming pixel to density and <<  46 //  - Handling of DICM images
 51 ///          files                             <<  47 //  - Reading headers and pixels
                                                   >>  48 //  - Transforming pixel to density and creating *.g4dcm
                                                   >>  49 //    files
                                                   >>  50 //  - Definitions are in DicomHandler.hh
 52 //********************************************     51 //*******************************************************
 53                                                    52 
 54 #include "DicomHandler.hh"                         53 #include "DicomHandler.hh"
 55                                                << 
 56 #include "DicomPhantomZSliceHeader.hh"         << 
 57 #include "DicomPhantomZSliceMerged.hh"         << 
 58                                                << 
 59 #include "G4ios.hh"                            << 
 60 #include "globals.hh"                              54 #include "globals.hh"
                                                   >>  55 #include "G4ios.hh"
                                                   >>  56 #include <fstream>
 61                                                    57 
 62 #include <cctype>                                  58 #include <cctype>
 63 #include <cstring>                                 59 #include <cstring>
 64 #include <fstream>                             << 
 65                                                << 
 66 //....oooOO0OOooo........oooOO0OOooo........oo << 
 67                                                << 
 68 DicomHandler* DicomHandler::fInstance = 0;     << 
 69                                                    60 
 70 //....oooOO0OOooo........oooOO0OOooo........oo << 
 71                                                    61 
 72 DicomHandler* DicomHandler::Instance()         << 
 73 {                                              << 
 74   if (fInstance == 0) {                        << 
 75     static DicomHandler dicomhandler;          << 
 76     fInstance = &dicomhandler;                 << 
 77   }                                            << 
 78   return fInstance;                            << 
 79 }                                              << 
 80                                                << 
 81 //....oooOO0OOooo........oooOO0OOooo........oo << 
 82                                                << 
 83 G4String DicomHandler::GetDicomDataPath()      << 
 84 {                                              << 
 85   // default is current directory              << 
 86   G4String driverPath = ".";                   << 
 87   // check environment                         << 
 88   const char* path = std::getenv("DICOM_PATH") << 
 89                                                << 
 90   if (path) {                                  << 
 91     // if is set in environment                << 
 92     return G4String(path);                     << 
 93   }                                            << 
 94   else {                                       << 
 95     // if DICOM_USE_HEAD, look for data instal << 
 96 #ifdef DICOM_USE_HEAD                          << 
 97     G4cerr << "Warning! DICOM was compiled wit << 
 98            << "the DICOM_PATH was not set!" << << 
 99     G4String datadir = G4GetEnv<G4String>("G4E << 
100     if (datadir.length() > 0) {                << 
101       auto _last = datadir.rfind("/");         << 
102       if (_last != std::string::npos) datadir. << 
103       driverPath = datadir + "/DICOM1.1/DICOM_ << 
104       G4int rc = setenv("DICOM_PATH", driverPa << 
105       G4cerr << "\t --> Using '" << driverPath << 
106       G4ConsumeParameters(rc);                 << 
107     }                                          << 
108 #endif                                         << 
109   }                                            << 
110   return driverPath;                           << 
111 }                                              << 
112                                                << 
113 //....oooOO0OOooo........oooOO0OOooo........oo << 
114                                                << 
115 G4String DicomHandler::GetDicomDataFile()      << 
116 {                                              << 
117 #if defined(DICOM_USE_HEAD) && defined(G4_DCMT << 
118   return GetDicomDataPath() + "/Data.dat.new"; << 
119 #else                                          << 
120   return GetDicomDataPath() + "/Data.dat";     << 
121 #endif                                         << 
122 }                                              << 
123                                                << 
124 //....oooOO0OOooo........oooOO0OOooo........oo << 
125                                                << 
126 #ifdef DICOM_USE_HEAD                          << 
127 DicomHandler::DicomHandler()                   << 
128   : DATABUFFSIZE(8192),                        << 
129     LINEBUFFSIZE(5020),                        << 
130     FILENAMESIZE(512),                         << 
131     fCompression(0),                           << 
132     fNFiles(0),                                << 
133     fRows(0),                                  << 
134     fColumns(0),                               << 
135     fBitAllocated(0),                          << 
136     fMaxPixelValue(0),                         << 
137     fMinPixelValue(0),                         << 
138     fPixelSpacingX(0.),                        << 
139     fPixelSpacingY(0.),                        << 
140     fSliceThickness(0.),                       << 
141     fSliceLocation(0.),                        << 
142     fRescaleIntercept(0),                      << 
143     fRescaleSlope(0),                          << 
144     fLittleEndian(true),                       << 
145     fImplicitEndian(false),                    << 
146     fPixelRepresentation(0),                   << 
147     fNbrequali(0),                             << 
148     fValueDensity(NULL),                       << 
149     fValueCT(NULL),                            << 
150     fReadCalibration(false),                   << 
151     fMergedSlices(NULL),                       << 
152     fCt2DensityFile("null.dat")                << 
153 {                                              << 
154   fMergedSlices = new DicomPhantomZSliceMerged << 
155   fDriverFile = GetDicomDataFile();            << 
156   G4cout << "Reading the DICOM_HEAD project "  << 
157 }                                              << 
158 #else                                          << 
159 DicomHandler::DicomHandler()                       62 DicomHandler::DicomHandler()
160   : DATABUFFSIZE(8192),                        <<  63     : DATABUFFSIZE(8192), LINEBUFFSIZE(128), FILENAMESIZE(512),
161     LINEBUFFSIZE(5020),                        <<  64       compression(0), nFiles(0), rows(0), columns(0),
162     FILENAMESIZE(512),                         <<  65       bitAllocated(0), maxPixelValue(0), minPixelValue(0),
163     fCompression(0),                           <<  66       pixelSpacingX(0.), pixelSpacingY(0.),
164     fNFiles(0),                                <<  67       sliceThickness(0.), sliceLocation(0.),
165     fRows(0),                                  <<  68       rescaleIntercept(0), rescaleSlope(0),
166     fColumns(0),                               <<  69       littleEndian(true), implicitEndian(false),
167     fBitAllocated(0),                          <<  70       pixelRepresentation(0) {
168     fMaxPixelValue(0),                         <<  71     ;
169     fMinPixelValue(0),                         << 
170     fPixelSpacingX(0.),                        << 
171     fPixelSpacingY(0.),                        << 
172     fSliceThickness(0.),                       << 
173     fSliceLocation(0.),                        << 
174     fRescaleIntercept(0),                      << 
175     fRescaleSlope(0),                          << 
176     fLittleEndian(true),                       << 
177     fImplicitEndian(false),                    << 
178     fPixelRepresentation(0),                   << 
179     fNbrequali(0),                             << 
180     fValueDensity(NULL),                       << 
181     fValueCT(NULL),                            << 
182     fReadCalibration(false),                   << 
183     fMergedSlices(NULL),                       << 
184     fDriverFile("Data.dat"),                   << 
185     fCt2DensityFile("CT2Density.dat")          << 
186 {                                              << 
187   fMergedSlices = new DicomPhantomZSliceMerged << 
188 }                                                  72 }
189 #endif                                         << 
190 //....oooOO0OOooo........oooOO0OOooo........oo << 
191                                                << 
192 DicomHandler::~DicomHandler() {}               << 
193                                                    73 
194 //....oooOO0OOooo........oooOO0OOooo........oo <<  74 DicomHandler::~DicomHandler() {
                                                   >>  75     ;
                                                   >>  76 }
195                                                    77 
196 G4int DicomHandler::ReadFile(FILE* dicom, char <<  78 G4int DicomHandler::ReadFile(FILE *dicom, char * filename2)
197 {                                                  79 {
198   G4cout << " ReadFile " << filename2 << G4end     80   G4cout << " ReadFile " << filename2 << G4endl;
                                                   >>  81     G4int returnvalue = 0;
                                                   >>  82     char * buffer = new char[LINEBUFFSIZE];
199                                                    83 
200   G4int returnvalue = 0;                       <<  84     implicitEndian = false;
201   size_t rflag = 0;                            <<  85     littleEndian = true;
202   char* buffer = new char[LINEBUFFSIZE];       << 
203                                                << 
204   fImplicitEndian = false;                     << 
205   fLittleEndian = true;                        << 
206                                                << 
207   rflag = std::fread(buffer, 1, 128, dicom);   << 
208                                                << 
209                                                << 
210   rflag = std::fread(buffer, 1, 4, dicom);     << 
211   // if there is no preamble, the FILE pointer << 
212   if (std::strncmp("DICM", buffer, 4) != 0) {  << 
213     std::fseek(dicom, 0, SEEK_SET);            << 
214     fImplicitEndian = true;                    << 
215   }                                            << 
216                                                << 
217   short readGroupId;  // identify the kind of  << 
218   short readElementId;  // identify a particul << 
219   short elementLength2;  // deal with element  << 
220                          // unsigned int eleme << 
221   // deal with element length in 4 bytes       << 
222   unsigned long elementLength4;  // deal with  << 
223                                                << 
224   char* data = new char[DATABUFFSIZE];         << 
225                                                << 
226   // Read information up to the pixel data     << 
227   while (true) {                               << 
228     // Reading groups and elements :           << 
229     readGroupId = 0;                           << 
230     readElementId = 0;                         << 
231     // group ID                                << 
232     rflag = std::fread(buffer, 2, 1, dicom);   << 
233     GetValue(buffer, readGroupId);             << 
234     // element ID                              << 
235     rflag = std::fread(buffer, 2, 1, dicom);   << 
236     GetValue(buffer, readElementId);           << 
237                                                << 
238     // Creating a tag to be identified afterwa << 
239     G4int tagDictionary = readGroupId * 0x1000 << 
240                                                << 
241     // beginning of the pixels                 << 
242     if (tagDictionary == 0x7FE00010) {         << 
243       // Following 2 fread's are modifications << 
244       // (Jonathan Madsen)                     << 
245       if (!fImplicitEndian) rflag = std::fread << 
246       // (not used for pixels)                 << 
247       rflag = std::fread(buffer, 4, 1, dicom); << 
248       // (not used for pixels)                 << 
249       break;  // Exit to ReadImageData()       << 
250     }                                          << 
251                                                << 
252     // VR or element length                    << 
253     rflag = std::fread(buffer, 2, 1, dicom);   << 
254     GetValue(buffer, elementLength2);          << 
255                                                << 
256     // If value representation (VR) is OB, OW, << 
257     // the next length is 32 bits              << 
258     if ((elementLength2 == 0x424f ||  // "OB"  << 
259          elementLength2 == 0x574f ||  // "OW"  << 
260          elementLength2 == 0x464f ||  // "OF"  << 
261          elementLength2 == 0x5455 ||  // "UT"  << 
262          elementLength2 == 0x5153 ||  // "SQ"  << 
263          elementLength2 == 0x4e55)             << 
264         &&  // "UN"                            << 
265         !fImplicitEndian)                      << 
266     {  // explicit VR                          << 
267                                                << 
268       rflag = std::fread(buffer, 2, 1, dicom); << 
269                                                << 
270       // element length                        << 
271       rflag = std::fread(buffer, 4, 1, dicom); << 
272       GetValue(buffer, elementLength4);        << 
273                                                << 
274       if (elementLength2 == 0x5153) {          << 
275         if (elementLength4 == 0xFFFFFFFF) {    << 
276           read_undefined_nested(dicom);        << 
277           elementLength4 = 0;                  << 
278         }                                      << 
279         else {                                 << 
280           if (read_defined_nested(dicom, eleme << 
281             G4Exception("DicomHandler::ReadFil << 
282                         "Function read_defined << 
283           }                                    << 
284         }                                      << 
285       }                                        << 
286       else {                                   << 
287         // Reading the information with data   << 
288         rflag = std::fread(data, elementLength << 
289       }                                        << 
290     }                                          << 
291     else {                                     << 
292       //  explicit with VR different than prev << 
293       if (!fImplicitEndian || readGroupId == 2 << 
294         // G4cout << "Reading  DICOM files wit << 
295         //  element length (2 bytes)           << 
296         rflag = std::fread(buffer, 2, 1, dicom << 
297         GetValue(buffer, elementLength2);      << 
298         elementLength4 = elementLength2;       << 
299                                                << 
300         rflag = std::fread(data, elementLength << 
301       }                                        << 
302       else {  // Implicit VR                   << 
303                                                << 
304         // G4cout << "Reading  DICOM files wit << 
305                                                << 
306         // element length (4 bytes)            << 
307         if (std::fseek(dicom, -2, SEEK_CUR) != << 
308           G4Exception("DicomHandler::ReadFile" << 
309         }                                      << 
310                                                << 
311         rflag = std::fread(buffer, 4, 1, dicom << 
312         GetValue(buffer, elementLength4);      << 
313                                                << 
314         // G4cout <<  std::hex<< elementLength << 
315                                                << 
316         if (elementLength4 == 0xFFFFFFFF) {    << 
317           read_undefined_nested(dicom);        << 
318           elementLength4 = 0;                  << 
319         }                                      << 
320         else {                                 << 
321           rflag = std::fread(data, elementLeng << 
322         }                                      << 
323       }                                        << 
324     }                                          << 
325                                                << 
326     // NULL termination                        << 
327     data[elementLength4] = '\0';               << 
328                                                << 
329     // analyzing information                   << 
330     GetInformation(tagDictionary, data);       << 
331   }                                            << 
332                                                << 
333   G4String fnameG4DCM = G4String(filename2) +  << 
334                                                << 
335   // Perform functions originally written stra << 
336   DicomPhantomZSliceHeader* zslice = new Dicom << 
337   for (auto ite = fMaterialIndices.cbegin(); i << 
338     zslice->AddMaterial(ite->second);          << 
339   }                                            << 
340                                                << 
341   zslice->SetNoVoxelsX(fColumns / fCompression << 
342   zslice->SetNoVoxelsY(fRows / fCompression);  << 
343   zslice->SetNoVoxelsZ(1);                     << 
344                                                << 
345   zslice->SetMinX(-fPixelSpacingX * fColumns / << 
346   zslice->SetMaxX(fPixelSpacingX * fColumns /  << 
347                                                    86 
348   zslice->SetMinY(-fPixelSpacingY * fRows / 2. <<  87     std::fread( buffer, 1, 128, dicom ); // The first 128 bytes 
349   zslice->SetMaxY(fPixelSpacingY * fRows / 2.) <<  88                                          //are not important
                                                   >>  89     // Reads the "DICOM" letters
                                                   >>  90     std::fread( buffer, 1, 4, dicom );
                                                   >>  91     // if there is no preamble, the FILE pointer is rewinded.
                                                   >>  92     if(std::strncmp("DICM", buffer, 4) != 0) {
                                                   >>  93         std::fseek(dicom, 0, SEEK_SET);
                                                   >>  94         implicitEndian = true;
                                                   >>  95     }
                                                   >>  96 
                                                   >>  97     short readGroupId;    // identify the kind of input data 
                                                   >>  98     short readElementId;  // identify a particular type information
                                                   >>  99     short elementLength2; // deal with element length in 2 bytes
                                                   >> 100     G4int elementLength4; // deal with element length in 4 bytes
                                                   >> 101 
                                                   >> 102     char * data = new char[DATABUFFSIZE];
                                                   >> 103 
                                                   >> 104     // Read information up to the pixel data
                                                   >> 105     while(true) {
                                                   >> 106 
                                                   >> 107   //Reading groups and elements :
                                                   >> 108   readGroupId = 0;
                                                   >> 109   readElementId = 0;
                                                   >> 110   // group ID
                                                   >> 111   std::fread(buffer, 2, 1, dicom);
                                                   >> 112   GetValue(buffer, readGroupId);
                                                   >> 113   // element ID
                                                   >> 114   std::fread(buffer, 2, 1, dicom);
                                                   >> 115   GetValue(buffer, readElementId);
                                                   >> 116 
                                                   >> 117   // Creating a tag to be identified afterward
                                                   >> 118   G4int tagDictionary = readGroupId*0x10000 + readElementId;
                                                   >> 119 
                                                   >> 120 
                                                   >> 121   // VR or element length
                                                   >> 122   std::fread(buffer,2,1,dicom);
                                                   >> 123   GetValue(buffer, elementLength2);
                                                   >> 124 
                                                   >> 125   // If value representation (VR) is OB, OW, SQ, UN, 
                                                   >> 126   //the next length is 32 bits
                                                   >> 127   if((elementLength2 == 0x424f ||  // "OB"
                                                   >> 128       elementLength2 == 0x574f ||  // "OW"
                                                   >> 129       elementLength2 == 0x5153 ||  // "SQ"
                                                   >> 130       elementLength2 == 0x4e55) && // "UN"
                                                   >> 131      !implicitEndian ) {           // explicit VR
                                                   >> 132 
                                                   >> 133       std::fread(buffer, 2, 1, dicom); // Skip 2 reserved bytes
                                                   >> 134 
                                                   >> 135       // element length
                                                   >> 136       std::fread(buffer, 4, 1, dicom);
                                                   >> 137       GetValue(buffer, elementLength4);
                                                   >> 138 
                                                   >> 139       // beginning of the pixels
                                                   >> 140       if(tagDictionary == 0x7FE00010) break;
                                                   >> 141 
                                                   >> 142       // Reading the information with data
                                                   >> 143       std::fread(data, elementLength4,1,dicom);
                                                   >> 144 
                                                   >> 145 
                                                   >> 146   } else { // length is 16 bits :
                                                   >> 147 
                                                   >> 148       if(!implicitEndian || readGroupId == 2) {
                                                   >> 149     // element length (2 bytes)
                                                   >> 150     std::fread(buffer, 2, 1, dicom);
                                                   >> 151     GetValue(buffer, elementLength2);
                                                   >> 152     elementLength4 = elementLength2;
                                                   >> 153 
                                                   >> 154       } else { 
                                                   >> 155     // element length (4 bytes)
                                                   >> 156     if(std::fseek(dicom, -2, SEEK_CUR) != 0) {
                                                   >> 157         G4cerr << "[DicomHandler] fseek failed" << G4endl;
                                                   >> 158         exit(-10);
                                                   >> 159     }
                                                   >> 160     std::fread(buffer, 4, 1, dicom);
                                                   >> 161     GetValue(buffer, elementLength4);
                                                   >> 162       }
                                                   >> 163 
                                                   >> 164       // beginning of the pixels
                                                   >> 165       if(tagDictionary == 0x7FE00010) break;
                                                   >> 166 
                                                   >> 167       std::fread(data, elementLength4, 1, dicom);
                                                   >> 168   }
                                                   >> 169 
                                                   >> 170   // NULL termination
                                                   >> 171   data[elementLength4] = '\0';
                                                   >> 172 
                                                   >> 173   // analyzing information 
                                                   >> 174   GetInformation(tagDictionary, data);
                                                   >> 175     }
                                                   >> 176 
                                                   >> 177     // Creating files to store information
                                                   >> 178     std::ofstream foutG4DCM;
                                                   >> 179     G4String fnameG4DCM = G4String(filename2) + ".g4dcm";
                                                   >> 180     foutG4DCM.open(fnameG4DCM);
                                                   >> 181     G4cout << " opened fnameG4DCM file " << fnameG4DCM << G4endl;
                                                   >> 182 
                                                   >> 183     foutG4DCM << fMaterialIndices.size() << G4endl;
                                                   >> 184     //--- Write materials
                                                   >> 185     size_t ii = 0;
                                                   >> 186     std::map<G4double,G4String>::const_iterator ite;
                                                   >> 187     for( ite = fMaterialIndices.begin(); ite != fMaterialIndices.end(); ite++, ii++ ){
                                                   >> 188       foutG4DCM << ii << " " << (*ite).second << G4endl;
                                                   >> 189     }
                                                   >> 190     //--- Write number of voxels (assume only one voxel in Z)
                                                   >> 191     foutG4DCM << rows/compression << " " << columns/compression << " 1 " << G4endl;
                                                   >> 192     //--- Write minimum and maximum extensions
                                                   >> 193     foutG4DCM << -pixelSpacingX*rows/2 << " " << pixelSpacingX*rows/2 << G4endl;
                                                   >> 194     foutG4DCM << -pixelSpacingY*columns/2 << " " << pixelSpacingY*columns/2 << G4endl;
                                                   >> 195     foutG4DCM << sliceLocation-sliceThickness/2. << " " << sliceLocation+sliceThickness/2. << G4endl;
                                                   >> 196     
                                                   >> 197     ReadData( dicom, filename2 );
                                                   >> 198     
                                                   >> 199     StoreData( foutG4DCM );
                                                   >> 200 
                                                   >> 201     foutG4DCM.close();
                                                   >> 202 
                                                   >> 203     //
                                                   >> 204     delete [] buffer;
                                                   >> 205     delete [] data;
350                                                   206 
351   zslice->SetMinZ(fSliceLocation - fSliceThick << 207     return returnvalue;
352   zslice->SetMaxZ(fSliceLocation + fSliceThick << 
353                                                << 
354   //===                                        << 
355                                                << 
356   ReadData(dicom, filename2);                  << 
357                                                << 
358   // DEPRECIATED                               << 
359   // StoreData( foutG4DCM );                   << 
360   // foutG4DCM.close();                        << 
361                                                << 
362   StoreData(zslice);                           << 
363                                                << 
364   // Dumped 2 file after DicomPhantomZSliceMer << 
365   // zslice->DumpToFile();                     << 
366                                                << 
367   fMergedSlices->AddZSlice(zslice);            << 
368                                                << 
369   //                                           << 
370   delete[] buffer;                             << 
371   delete[] data;                               << 
372                                                << 
373   if (rflag) return returnvalue;               << 
374   return returnvalue;                          << 
375 }                                                 208 }
376                                                   209 
377 //....oooOO0OOooo........oooOO0OOooo........oo << 210 //
378                                                << 211 void DicomHandler::GetInformation(G4int & tagDictionary, char * data) {
379 void DicomHandler::GetInformation(G4int& tagDi << 212     if(tagDictionary == 0x00280010 ) { // Number of Rows
380 {                                              << 213   GetValue(data, rows);
381   if (tagDictionary == 0x00280010) {  // Numbe << 214   std::printf("[0x00280010] Rows -> %i\n",rows);
382     GetValue(data, fRows);                     << 215 
383     std::printf("[0x00280010] Rows -> %i\n", f << 216     } else if(tagDictionary == 0x00280011 ) { // Number of columns
384   }                                            << 217   GetValue(data, columns);
385   else if (tagDictionary == 0x00280011) {  //  << 218   std::printf("[0x00280011] Columns -> %i\n",columns);
386     GetValue(data, fColumns);                  << 219 
387     std::printf("[0x00280011] Columns -> %i\n" << 220     } else if(tagDictionary == 0x00280102 ) { // High bits  ( not used )
388   }                                            << 221   short highBits;
389   else if (tagDictionary == 0x00280102) {  //  << 222   GetValue(data, highBits);
390     short highBits;                            << 223   std::printf("[0x00280102] High bits -> %i\n",highBits);
391     GetValue(data, highBits);                  << 224 
392     std::printf("[0x00280102] High bits -> %i\ << 225     } else if(tagDictionary == 0x00280100 ) { // Bits allocated
393   }                                            << 226   GetValue(data, bitAllocated);
394   else if (tagDictionary == 0x00280100) {  //  << 227   std::printf("[0x00280100] Bits allocated -> %i\n", bitAllocated);
395     GetValue(data, fBitAllocated);             << 228 
396     std::printf("[0x00280100] Bits allocated - << 229     } else if(tagDictionary == 0x00280101 ) { //  Bits stored ( not used )
397   }                                            << 230   short bitStored;
398   else if (tagDictionary == 0x00280101) {  //  << 231   GetValue(data, bitStored);
399     short bitStored;                           << 232   std::printf("[0x00280101] Bits stored -> %i\n",bitStored);
400     GetValue(data, bitStored);                 << 233 
401     std::printf("[0x00280101] Bits stored -> % << 234     } else if(tagDictionary == 0x00280106 ) { //  Min. pixel value
402   }                                            << 235   GetValue(data, minPixelValue);
403   else if (tagDictionary == 0x00280106) {  //  << 236   std::printf("[0x00280106] Min. pixel value -> %i\n", minPixelValue);
404     GetValue(data, fMinPixelValue);            << 237 
405     std::printf("[0x00280106] Min. pixel value << 238     } else if(tagDictionary == 0x00280107 ) { //  Max. pixel value
406   }                                            << 239   GetValue(data, maxPixelValue);
407   else if (tagDictionary == 0x00280107) {  //  << 240   std::printf("[0x00280107] Max. pixel value -> %i\n", maxPixelValue);
408     GetValue(data, fMaxPixelValue);            << 241 
409     std::printf("[0x00280107] Max. pixel value << 242     } else if(tagDictionary == 0x00281053) { //  Rescale slope
410   }                                            << 243   rescaleSlope = atoi(data);
411   else if (tagDictionary == 0x00281053) {  //  << 244   std::printf("[0x00281053] Rescale Slope -> %d\n", rescaleSlope);
412     fRescaleSlope = atoi(data);                << 245 
413     std::printf("[0x00281053] Rescale Slope -> << 246     } else if(tagDictionary == 0x00281052 ) { // Rescalse intercept
414   }                                            << 247   rescaleIntercept = atoi(data);
415   else if (tagDictionary == 0x00281052) {  //  << 248   std::printf("[0x00281052] Rescale Intercept -> %d\n", rescaleIntercept );
416     fRescaleIntercept = atoi(data);            << 249 
417     std::printf("[0x00281052] Rescale Intercep << 250     } else if(tagDictionary == 0x00280103 ) {
418   }                                            << 251   //  Pixel representation ( functions not design to read signed bits )
419   else if (tagDictionary == 0x00280103) {      << 252   pixelRepresentation = atoi(data); // 0: unsigned  1: signed 
420     //  Pixel representation ( functions not d << 253   std::printf("[0x00280103] Pixel Representation -> %i\n", pixelRepresentation);
421     fPixelRepresentation = atoi(data);  // 0:  << 254   if(pixelRepresentation == 1 ) {
422     std::printf("[0x00280103] Pixel Representa << 255       std::printf("### PIXEL REPRESENTATION = 1, BITS ARE SIGNED, ");
423     if (fPixelRepresentation == 1) {           << 256       std::printf("DICOM READING SCAN FOR UNSIGNED VALUE, POSSIBLE ");
424       std::printf("### PIXEL REPRESENTATION =  << 257       std::printf("ERROR !!!!!! -> \n");
425       std::printf("DICOM READING SCAN FOR UNSI << 258   }
426       std::printf("ERROR !!!!!! -> \n");       << 259 
                                                   >> 260     } else if(tagDictionary == 0x00080006 ) { //  Modality
                                                   >> 261   std::printf("[0x00080006] Modality -> %s\n", data);
                                                   >> 262 
                                                   >> 263     } else if(tagDictionary == 0x00080070 ) { //  Manufacturer
                                                   >> 264   std::printf("[0x00080070] Manufacturer -> %s\n", data);
                                                   >> 265 
                                                   >> 266     } else if(tagDictionary == 0x00080080 ) { //  Institution Name
                                                   >> 267   std::printf("[0x00080080] Institution Name -> %s\n", data);
                                                   >> 268 
                                                   >> 269     } else if(tagDictionary == 0x00080081 ) { //  Institution Address
                                                   >> 270   std::printf("[0x00080081] Institution Address -> %s\n", data);
                                                   >> 271 
                                                   >> 272     } else if(tagDictionary == 0x00081040 ) { //  Institution Department Name
                                                   >> 273   std::printf("[0x00081040] Institution Department Name -> %s\n", data);
                                                   >> 274 
                                                   >> 275     } else if(tagDictionary == 0x00081090 ) { //  Manufacturer's Model Name
                                                   >> 276   std::printf("[0x00081090] Manufacturer's Model Name -> %s\n", data);
                                                   >> 277 
                                                   >> 278     } else if(tagDictionary == 0x00181000 ) { //  Device Serial Number
                                                   >> 279   std::printf("[0x00181000] Device Serial Number -> %s\n", data);
                                                   >> 280 
                                                   >> 281     } else if(tagDictionary == 0x00080008 ) { //  Image type ( not used )
                                                   >> 282   std::printf("[0x00080008] Image Types -> %s\n", data);
                                                   >> 283       
                                                   >> 284     } else if(tagDictionary == 0x00283000 ) { //  Modality LUT Sequence ( not used )
                                                   >> 285   std::printf("[0x00283000] Modality LUT Sequence SQ 1 -> %s\n", data);
                                                   >> 286 
                                                   >> 287     } else if(tagDictionary == 0x00283002 ) { // LUT Descriptor ( not used )
                                                   >> 288   std::printf("[0x00283002] LUT Descriptor US or SS 3 -> %s\n", data);
                                                   >> 289 
                                                   >> 290     } else if(tagDictionary == 0x00283003 ) { // LUT Explanation ( not used )
                                                   >> 291   std::printf("[0x00283003] LUT Explanation LO 1 -> %s\n", data);
                                                   >> 292 
                                                   >> 293     } else if(tagDictionary == 0x00283004 ) { // Modality LUT ( not used )
                                                   >> 294   std::printf("[0x00283004] Modality LUT Type LO 1 -> %s\n", data);
                                                   >> 295 
                                                   >> 296     } else if(tagDictionary == 0x00283006 ) { // LUT Data ( not used )
                                                   >> 297   std::printf("[0x00283006] LUT Data US or SS -> %s\n", data);
                                                   >> 298 
                                                   >> 299     } else if(tagDictionary == 0x00283010 ) { // VOI LUT ( not used )
                                                   >> 300   std::printf("[0x00283010] VOI LUT Sequence SQ 1 -> %s\n", data);
                                                   >> 301 
                                                   >> 302     } else if(tagDictionary == 0x00280120 ) { // Pixel Padding Value ( not used )
                                                   >> 303   std::printf("[0x00280120] Pixel Padding Value US or SS 1 -> %s\n", data);
                                                   >> 304 
                                                   >> 305     } else if(tagDictionary == 0x00280030 ) { // Pixel Spacing
                                                   >> 306       G4String datas(data);
                                                   >> 307       int iss = datas.find('\\');
                                                   >> 308       pixelSpacingX = atof( datas.substr(0,iss).c_str() );
                                                   >> 309       pixelSpacingY = atof( datas.substr(iss+2,datas.length()).c_str() );
                                                   >> 310 
                                                   >> 311     } else if(tagDictionary == 0x00200037 ) { // Image Orientation ( not used )
                                                   >> 312   std::printf("[0x00200037] Image Orientation (Patient) -> %s\n", data);
                                                   >> 313 
                                                   >> 314     } else if(tagDictionary == 0x00200032 ) { // Image Position ( not used )
                                                   >> 315   std::printf("[0x00200032] Image Position (Patient,mm) -> %s\n", data);
                                                   >> 316 
                                                   >> 317     } else if(tagDictionary == 0x00180050 ) { // Slice Thickness
                                                   >> 318   sliceThickness = atof(data);
                                                   >> 319   std::printf("[0x00180050] Slice Thickness (mm) -> %f\n", sliceThickness);
                                                   >> 320 
                                                   >> 321     } else if(tagDictionary == 0x00201041 ) { // Slice Location
                                                   >> 322   sliceLocation = atof(data);
                                                   >> 323   std::printf("[0x00201041] Slice Location -> %f\n", sliceLocation);
                                                   >> 324 
                                                   >> 325     } else if(tagDictionary == 0x00280004 ) { // Photometric Interpretation ( not used )
                                                   >> 326   std::printf("[0x00280004] Photometric Interpretation -> %s\n", data);
                                                   >> 327 
                                                   >> 328     } else if(tagDictionary == 0x00020010) { // Endian
                                                   >> 329   if(strcmp(data, "1.2.840.10008.1.2") == 0)
                                                   >> 330       implicitEndian = true;
                                                   >> 331   else if(strncmp(data, "1.2.840.10008.1.2.2", 19) == 0)
                                                   >> 332       littleEndian = false;
                                                   >> 333   //else 1.2.840..10008.1.2.1 (explicit little endian)
                                                   >> 334        
                                                   >> 335   std::printf("[0x00020010] Endian -> %s\n", data);
427     }                                             336     }
428   }                                            << 
429   else if (tagDictionary == 0x00080006) {  //  << 
430     std::printf("[0x00080006] Modality -> %s\n << 
431   }                                            << 
432   else if (tagDictionary == 0x00080070) {  //  << 
433     std::printf("[0x00080070] Manufacturer ->  << 
434   }                                            << 
435   else if (tagDictionary == 0x00080080) {  //  << 
436     std::printf("[0x00080080] Institution Name << 
437   }                                            << 
438   else if (tagDictionary == 0x00080081) {  //  << 
439     std::printf("[0x00080081] Institution Addr << 
440   }                                            << 
441   else if (tagDictionary == 0x00081040) {  //  << 
442     std::printf("[0x00081040] Institution Depa << 
443   }                                            << 
444   else if (tagDictionary == 0x00081090) {  //  << 
445     std::printf("[0x00081090] Manufacturer's M << 
446   }                                            << 
447   else if (tagDictionary == 0x00181000) {  //  << 
448     std::printf("[0x00181000] Device Serial Nu << 
449   }                                            << 
450   else if (tagDictionary == 0x00080008) {  //  << 
451     std::printf("[0x00080008] Image Types -> % << 
452   }                                            << 
453   else if (tagDictionary == 0x00283000) {  //  << 
454     std::printf("[0x00283000] Modality LUT Seq << 
455   }                                            << 
456   else if (tagDictionary == 0x00283002) {  //  << 
457     std::printf("[0x00283002] LUT Descriptor U << 
458   }                                            << 
459   else if (tagDictionary == 0x00283003) {  //  << 
460     std::printf("[0x00283003] LUT Explanation  << 
461   }                                            << 
462   else if (tagDictionary == 0x00283004) {  //  << 
463     std::printf("[0x00283004] Modality LUT Typ << 
464   }                                            << 
465   else if (tagDictionary == 0x00283006) {  //  << 
466     std::printf("[0x00283006] LUT Data US or S << 
467   }                                            << 
468   else if (tagDictionary == 0x00283010) {  //  << 
469     std::printf("[0x00283010] VOI LUT Sequence << 
470   }                                            << 
471   else if (tagDictionary == 0x00280120) {  //  << 
472     std::printf("[0x00280120] Pixel Padding Va << 
473   }                                            << 
474   else if (tagDictionary == 0x00280030) {  //  << 
475     G4String datas(data);                      << 
476     G4int iss = G4int(datas.find('\\'));       << 
477     fPixelSpacingX = atof(datas.substr(0, iss) << 
478     fPixelSpacingY = atof(datas.substr(iss + 1 << 
479   }                                            << 
480   else if (tagDictionary == 0x00200037) {  //  << 
481     std::printf("[0x00200037] Image Orientatio << 
482   }                                            << 
483   else if (tagDictionary == 0x00200032) {  //  << 
484     std::printf("[0x00200032] Image Position ( << 
485   }                                            << 
486   else if (tagDictionary == 0x00180050) {  //  << 
487     fSliceThickness = atof(data);              << 
488     std::printf("[0x00180050] Slice Thickness  << 
489   }                                            << 
490   else if (tagDictionary == 0x00201041) {  //  << 
491     fSliceLocation = atof(data);               << 
492     std::printf("[0x00201041] Slice Location - << 
493   }                                            << 
494   else if (tagDictionary == 0x00280004) {  //  << 
495     // ( not used )                            << 
496     std::printf("[0x00280004] Photometric Inte << 
497   }                                            << 
498   else if (tagDictionary == 0x00020010) {  //  << 
499     if (strcmp(data, "1.2.840.10008.1.2") == 0 << 
500       fImplicitEndian = true;                  << 
501     else if (strncmp(data, "1.2.840.10008.1.2. << 
502       fLittleEndian = false;                   << 
503     // else 1.2.840..10008.1.2.1 (explicit lit << 
504                                                << 
505     std::printf("[0x00020010] Endian -> %s\n", << 
506   }                                            << 
507                                                << 
508   // others                                    << 
509   else {                                       << 
510     // std::printf("[0x%x] -> %s\n", tagDictio << 
511     ;                                          << 
512   }                                            << 
513 }                                              << 
514                                                << 
515 //....oooOO0OOooo........oooOO0OOooo........oo << 
516                                                << 
517 void DicomHandler::StoreData(DicomPhantomZSlic << 
518 {                                              << 
519   G4int mean;                                  << 
520   G4double density;                            << 
521   G4bool overflow = false;                     << 
522                                                << 
523   if (!dcmPZSH) {                              << 
524     return;                                    << 
525   }                                            << 
526                                                   337 
527   dcmPZSH->SetSliceLocation(fSliceLocation);   << 338     // others
528                                                << 339     else {
529   //----- Print indices of material            << 340   std::printf("[0x%x] -> %s\n", tagDictionary, data);
530   if (fCompression == 1) {  // no fCompression << 
531     for (G4int ww = 0; ww < fRows; ++ww) {     << 
532       dcmPZSH->AddRow();                       << 
533       for (G4int xx = 0; xx < fColumns; ++xx)  << 
534         mean = fTab[ww][xx];                   << 
535         density = Pixel2density(mean);         << 
536         dcmPZSH->AddValue(density);            << 
537         dcmPZSH->AddMateID(GetMaterialIndex(G4 << 
538       }                                        << 
539     }                                          << 
540   }                                            << 
541   else {                                       << 
542     // density value is the average of a squar << 
543     // fCompression*fCompression pixels        << 
544     for (G4int ww = 0; ww < fRows; ww += fComp << 
545       dcmPZSH->AddRow();                       << 
546       for (G4int xx = 0; xx < fColumns; xx +=  << 
547         overflow = false;                      << 
548         mean = 0;                              << 
549         for (G4int sumx = 0; sumx < fCompressi << 
550           for (G4int sumy = 0; sumy < fCompres << 
551             if (ww + sumy >= fRows || xx + sum << 
552             mean += fTab[ww + sumy][xx + sumx] << 
553           }                                    << 
554           if (overflow) break;                 << 
555         }                                      << 
556         mean /= fCompression * fCompression;   << 
557                                                   341 
558         if (!overflow) {                       << 
559           density = Pixel2density(mean);       << 
560           dcmPZSH->AddValue(density);          << 
561           dcmPZSH->AddMateID(GetMaterialIndex( << 
562         }                                      << 
563       }                                        << 
564     }                                             342     }
565   }                                            << 
566                                                   343 
567   dcmPZSH->FlipData();                         << 
568 }                                                 344 }
569                                                   345 
570 //....oooOO0OOooo........oooOO0OOooo........oo << 346 void DicomHandler::StoreData(std::ofstream& foutG4DCM) 
571 // This function is depreciated as it is handl << 
572 // DicomPhantomZSliceHeader::DumpToFile        << 
573 void DicomHandler::StoreData(std::ofstream& fo << 
574 {                                                 347 {
575   G4int mean;                                     348   G4int mean;
576   G4double density;                               349   G4double density;
577   G4bool overflow = false;                        350   G4bool overflow = false;
                                                   >> 351   G4int cpt=1;
578                                                   352 
579   //----- Print indices of material            << 353   //----- Print indices of material 
580   if (fCompression == 1) {  // no fCompression << 354   if(compression == 1) { // no compression: each pixel has a density value)
581     for (G4int ww = 0; ww < fRows; ++ww) {     << 355     for( G4int ww = 0; ww < rows; ww++) {
582       for (G4int xx = 0; xx < fColumns; ++xx)  << 356       for( G4int xx = 0; xx < columns; xx++) {
583         mean = fTab[ww][xx];                   << 357   mean = tab[ww][xx];
584         density = Pixel2density(mean);         << 358   density = Pixel2density(mean);
585         foutG4DCM << GetMaterialIndex(G4float( << 359   foutG4DCM << GetMaterialIndex( density ) << " ";
586       }                                           360       }
587       foutG4DCM << G4endl;                        361       foutG4DCM << G4endl;
588     }                                             362     }
589   }                                            << 363     
590   else {                                       << 364   } else {
591     // density value is the average of a squar    365     // density value is the average of a square region of
592     // fCompression*fCompression pixels        << 366     // compression*compression pixels
593     for (G4int ww = 0; ww < fRows; ww += fComp << 367     for(G4int ww = 0; ww < rows ;ww += compression ) {
594       for (G4int xx = 0; xx < fColumns; xx +=  << 368       for(G4int xx = 0; xx < columns ;xx +=compression ) {
595         overflow = false;                      << 369   overflow = false;
596         mean = 0;                              << 370   mean = 0;
597         for (G4int sumx = 0; sumx < fCompressi << 371   for(int sumx = 0; sumx < compression; sumx++) {
598           for (G4int sumy = 0; sumy < fCompres << 372     for(int sumy = 0; sumy < compression; sumy++) {
599             if (ww + sumy >= fRows || xx + sum << 373       if(ww+sumy >= rows || xx+sumx >= columns) overflow = true;
600             mean += fTab[ww + sumy][xx + sumx] << 374       mean += tab[ww+sumy][xx+sumx];
601           }                                    << 375     }
602           if (overflow) break;                 << 376     if(overflow) break;
603         }                                      << 377   }
604         mean /= fCompression * fCompression;   << 378   mean /= compression*compression;
605                                                << 379   cpt = 1;
606         if (!overflow) {                       << 380   
607           density = Pixel2density(mean);       << 381   if(!overflow) {
608           foutG4DCM << GetMaterialIndex(G4floa << 382     G4double density = Pixel2density(mean);
609         }                                      << 383     foutG4DCM << GetMaterialIndex( density ) << " ";
                                                   >> 384   }
610       }                                           385       }
611       foutG4DCM << G4endl;                        386       foutG4DCM << G4endl;
612     }                                             387     }
                                                   >> 388 
613   }                                               389   }
614                                                   390 
615   //----- Print densities                         391   //----- Print densities
616   if (fCompression == 1) {  // no fCompression << 392   if(compression == 1) { // no compression: each pixel has a density value)
617     for (G4int ww = 0; ww < fRows; ww++) {     << 393     for( G4int ww = 0; ww < rows; ww++) {
618       for (G4int xx = 0; xx < fColumns; xx++)  << 394       for( G4int xx = 0; xx < columns; xx++) {
619         mean = fTab[ww][xx];                   << 395   mean = tab[ww][xx];
620         density = Pixel2density(mean);         << 396   density = Pixel2density(mean);
621         foutG4DCM << density << " ";           << 397   foutG4DCM << density << " ";
622         if (xx % 8 == 3) foutG4DCM << G4endl;  << 398   if( xx%8 == 3 ) foutG4DCM << G4endl; // just for nicer reading
623       }                                           399       }
624     }                                             400     }
625   }                                            << 401     
626   else {                                       << 402   } else {
627     // density value is the average of a squar    403     // density value is the average of a square region of
628     // fCompression*fCompression pixels        << 404     // compression*compression pixels
629     for (G4int ww = 0; ww < fRows; ww += fComp << 405     for(G4int ww = 0; ww < rows ;ww += compression ) {
630       for (G4int xx = 0; xx < fColumns; xx +=  << 406       for(G4int xx = 0; xx < columns ;xx +=compression ) {
631         overflow = false;                      << 407   overflow = false;
632         mean = 0;                              << 408   mean = 0;
633         for (G4int sumx = 0; sumx < fCompressi << 409   for(int sumx = 0; sumx < compression; sumx++) {
634           for (G4int sumy = 0; sumy < fCompres << 410     for(int sumy = 0; sumy < compression; sumy++) {
635             if (ww + sumy >= fRows || xx + sum << 411       if(ww+sumy >= rows || xx+sumx >= columns) overflow = true;
636             mean += fTab[ww + sumy][xx + sumx] << 412       mean += tab[ww+sumy][xx+sumx];
637           }                                    << 413     }
638           if (overflow) break;                 << 414     if(overflow) break;
639         }                                      << 415   }
640         mean /= fCompression * fCompression;   << 416   mean /= compression*compression;
641                                                << 417   cpt = 1;
642         if (!overflow) {                       << 418   
643           density = Pixel2density(mean);       << 419   if(!overflow) {
644           foutG4DCM << density << " ";         << 420     G4double density = Pixel2density(mean);
645           if (xx / fCompression % 8 == 3) fout << 421     foutG4DCM << density  << " ";
646           // reading                           << 422     if( xx/compression%8 == 3 ) foutG4DCM << G4endl; // just for nicer reading
647         }                                      << 423   }
648       }                                           424       }
649     }                                             425     }
                                                   >> 426 
650   }                                               427   }
                                                   >> 428 
651 }                                                 429 }
652                                                   430 
653 //....oooOO0OOooo........oooOO0OOooo........oo << 431 void DicomHandler::ReadMaterialIndices( std::ifstream& finData)
654 void DicomHandler::ReadMaterialIndices(std::if << 
655 {                                                 432 {
656   unsigned int nMate;                          << 433   size_t nMate;
657   G4String mateName;                              434   G4String mateName;
658   G4float densityMax;                          << 435   G4double densityMax;
659   finData >> nMate;                               436   finData >> nMate;
660   if (finData.eof()) return;                   << 437   if( finData.eof() ) return;
661                                                   438 
662   G4cout << " ReadMaterialIndices " << nMate <    439   G4cout << " ReadMaterialIndices " << nMate << G4endl;
663   for (unsigned int ii = 0; ii < nMate; ++ii)  << 440   for( size_t ii = 0; ii < nMate; ii++ ){
664     finData >> mateName >> densityMax;            441     finData >> mateName >> densityMax;
665     fMaterialIndices[densityMax] = mateName;      442     fMaterialIndices[densityMax] = mateName;
666     //    G4cout << ii << " ReadMaterialIndice << 443     G4cout << ii << " ReadMaterialIndices " << mateName << " " << densityMax << G4endl;
667     //     << densityMax << G4endl;            << 
668   }                                               444   }
669 }                                              << 
670                                                << 
671 //....oooOO0OOooo........oooOO0OOooo........oo << 
672                                                   445 
673 unsigned int DicomHandler::GetMaterialIndex(G4 << 
674 {                                              << 
675   std::map<G4float, G4String>::const_reverse_i << 
676   std::size_t ii = fMaterialIndices.size();    << 
677                                                << 
678   for (ite = fMaterialIndices.crbegin(); ite ! << 
679     if (density >= (*ite).first) {             << 
680       break;                                   << 
681     }                                          << 
682   }                                            << 
683                                                << 
684   if (ii == fMaterialIndices.size()) {         << 
685     ii = fMaterialIndices.size() - 1;          << 
686   }                                            << 
687                                                << 
688   return unsigned(ii);                         << 
689 }                                                 446 }
690                                                   447 
691 //....oooOO0OOooo........oooOO0OOooo........oo << 448 size_t DicomHandler::GetMaterialIndex( G4double density )
692                                                << 
693 G4int DicomHandler::ReadData(FILE* dicom, char << 
694 {                                                 449 {
695   G4int returnvalue = 0;                       << 450   size_t mateID;
696   size_t rflag = 0;                            << 451   std::map<G4double,G4String>::reverse_iterator ite;
697                                                << 452   G4int ii = fMaterialIndices.size();
698   //  READING THE PIXELS                       << 453   for( ite = fMaterialIndices.rbegin(); ite != fMaterialIndices.rend(); ite++, ii-- ) {
699                                                << 454     if( density >= (*ite).first ) {
700   fTab = new G4int*[fRows];                    << 455       break;
701   for (G4int i = 0; i < fRows; ++i) {          << 
702     fTab[i] = new G4int[fColumns];             << 
703   }                                            << 
704                                                << 
705   if (fBitAllocated == 8) {  // Case 8 bits :  << 
706                                                << 
707     std::printf("@@@ Error! Picture != 16 bits << 
708     std::printf("@@@ Error! Picture != 16 bits << 
709     std::printf("@@@ Error! Picture != 16 bits << 
710                                                << 
711     unsigned char ch = 0;                      << 
712                                                << 
713     for (G4int j = 0; j < fRows; ++j) {        << 
714       for (G4int i = 0; i < fColumns; ++i) {   << 
715         rflag = std::fread(&ch, 1, 1, dicom);  << 
716         fTab[j][i] = ch * fRescaleSlope + fRes << 
717       }                                        << 
718     }                                          << 
719     returnvalue = 1;                           << 
720   }                                            << 
721   else {  //  from 12 to 16 bits :             << 
722     char sbuff[2];                             << 
723     short pixel;                               << 
724     for (G4int j = 0; j < fRows; ++j) {        << 
725       for (G4int i = 0; i < fColumns; ++i) {   << 
726         rflag = std::fread(sbuff, 2, 1, dicom) << 
727         GetValue(sbuff, pixel);                << 
728         fTab[j][i] = pixel * fRescaleSlope + f << 
729       }                                        << 
730     }                                          << 
731   }                                            << 
732                                                << 
733   // Creation of .g4 files wich contains avera << 
734   G4String nameProcessed = filename2 + G4Strin << 
735   FILE* fileOut = std::fopen(nameProcessed.c_s << 
736                                                << 
737   G4cout << "### Writing of " << nameProcessed << 
738                                                << 
739   unsigned int nMate = fMaterialIndices.size() << 
740   rflag = std::fwrite(&nMate, sizeof(unsigned  << 
741   //--- Write materials                        << 
742   for (auto ite = fMaterialIndices.cbegin(); i << 
743     G4String mateName = (*ite).second;         << 
744     for (std::size_t ii = (*ite).second.length << 
745       mateName += " ";                         << 
746     }  // mateName = const_cast<char*>(((*ite) << 
747                                                << 
748     const char* mateNameC = mateName.c_str();  << 
749     rflag = std::fwrite(mateNameC, sizeof(char << 
750   }                                            << 
751                                                << 
752   unsigned int fRowsC = fRows / fCompression;  << 
753   unsigned int fColumnsC = fColumns / fCompres << 
754   unsigned int planesC = 1;                    << 
755   G4float pixelLocationXM = -G4float(fPixelSpa << 
756   G4float pixelLocationXP = G4float(fPixelSpac << 
757   G4float pixelLocationYM = -G4float(fPixelSpa << 
758   G4float pixelLocationYP = G4float(fPixelSpac << 
759   G4float fSliceLocationZM = G4float(fSliceLoc << 
760   G4float fSliceLocationZP = G4float(fSliceLoc << 
761   //--- Write number of voxels (assume only on << 
762   rflag = std::fwrite(&fRowsC, sizeof(unsigned << 
763   rflag = std::fwrite(&fColumnsC, sizeof(unsig << 
764   rflag = std::fwrite(&planesC, sizeof(unsigne << 
765   //--- Write minimum and maximum extensions   << 
766   rflag = std::fwrite(&pixelLocationXM, sizeof << 
767   rflag = std::fwrite(&pixelLocationXP, sizeof << 
768   rflag = std::fwrite(&pixelLocationYM, sizeof << 
769   rflag = std::fwrite(&pixelLocationYP, sizeof << 
770   rflag = std::fwrite(&fSliceLocationZM, sizeo << 
771   rflag = std::fwrite(&fSliceLocationZP, sizeo << 
772   // rflag = std::fwrite(&fCompression, sizeof << 
773                                                << 
774   std::printf("%8i   %8i\n", fRows, fColumns); << 
775   std::printf("%8f   %8f\n", fPixelSpacingX, f << 
776   std::printf("%8f\n", fSliceThickness);       << 
777   std::printf("%8f\n", fSliceLocation);        << 
778   std::printf("%8i\n", fCompression);          << 
779                                                << 
780   G4int compSize = fCompression;               << 
781   G4int mean;                                  << 
782   G4float density;                             << 
783   G4bool overflow = false;                     << 
784                                                << 
785   //----- Write index of material for each pix << 
786   if (compSize == 1) {  // no fCompression: ea << 
787     for (G4int ww = 0; ww < fRows; ++ww) {     << 
788       for (G4int xx = 0; xx < fColumns; ++xx)  << 
789         mean = fTab[ww][xx];                   << 
790         density = Pixel2density(mean);         << 
791         unsigned int mateID = GetMaterialIndex << 
792         rflag = std::fwrite(&mateID, sizeof(un << 
793       }                                        << 
794     }                                          << 
795   }                                            << 
796   else {                                       << 
797     // density value is the average of a squar << 
798     // fCompression*fCompression pixels        << 
799     for (G4int ww = 0; ww < fRows; ww += compS << 
800       for (G4int xx = 0; xx < fColumns; xx +=  << 
801         overflow = false;                      << 
802         mean = 0;                              << 
803         for (G4int sumx = 0; sumx < compSize;  << 
804           for (G4int sumy = 0; sumy < compSize << 
805             if (ww + sumy >= fRows || xx + sum << 
806             mean += fTab[ww + sumy][xx + sumx] << 
807           }                                    << 
808           if (overflow) break;                 << 
809         }                                      << 
810         mean /= compSize * compSize;           << 
811                                                << 
812         if (!overflow) {                       << 
813           density = Pixel2density(mean);       << 
814           unsigned int mateID = GetMaterialInd << 
815           rflag = std::fwrite(&mateID, sizeof( << 
816         }                                      << 
817       }                                        << 
818     }                                          << 
819   }                                            << 
820                                                << 
821   //----- Write density for each pixel         << 
822   if (compSize == 1) {  // no fCompression: ea << 
823     for (G4int ww = 0; ww < fRows; ++ww) {     << 
824       for (G4int xx = 0; xx < fColumns; ++xx)  << 
825         mean = fTab[ww][xx];                   << 
826         density = Pixel2density(mean);         << 
827         rflag = std::fwrite(&density, sizeof(G << 
828       }                                        << 
829     }                                          << 
830   }                                            << 
831   else {                                       << 
832     // density value is the average of a squar << 
833     // fCompression*fCompression pixels        << 
834     for (G4int ww = 0; ww < fRows; ww += compS << 
835       for (G4int xx = 0; xx < fColumns; xx +=  << 
836         overflow = false;                      << 
837         mean = 0;                              << 
838         for (G4int sumx = 0; sumx < compSize;  << 
839           for (G4int sumy = 0; sumy < compSize << 
840             if (ww + sumy >= fRows || xx + sum << 
841             mean += fTab[ww + sumy][xx + sumx] << 
842           }                                    << 
843           if (overflow) break;                 << 
844         }                                      << 
845         mean /= compSize * compSize;           << 
846                                                << 
847         if (!overflow) {                       << 
848           density = Pixel2density(mean);       << 
849           rflag = std::fwrite(&density, sizeof << 
850         }                                      << 
851       }                                        << 
852     }                                             456     }
853   }                                               457   }
                                                   >> 458   //-  G4cout << " GetMaterialIndex " << density << " = " << ii << G4endl;
                                                   >> 459   return  ii;
854                                                   460 
855   rflag = std::fclose(fileOut);                << 
856                                                << 
857   delete[] nameProcessed;                      << 
858                                                << 
859   /*    for ( G4int i = 0; i < fRows; i ++ ) { << 
860         delete [] fTab[i];                     << 
861         }                                      << 
862      delete [] fTab;                           << 
863   */                                           << 
864                                                << 
865   if (rflag) return returnvalue;               << 
866   return returnvalue;                          << 
867 }                                              << 
868                                                << 
869 //....oooOO0OOooo........oooOO0OOooo........oo << 
870                                                << 
871 // DICOM HEAD does not use the calibration cur << 
872                                                << 
873 #ifdef DICOM_USE_HEAD                          << 
874 void DicomHandler::ReadCalibration()           << 
875 {                                              << 
876   fNbrequali = 0;                              << 
877   fReadCalibration = false;                    << 
878   G4cout << "No calibration curve for the DICO << 
879 }                                                 461 }
880 #else                                          << 
881 // Separated out of Pixel2density              << 
882 // No need to read in same calibration EVERY t << 
883 // Increases the speed of reading file by seve << 
884                                                   462 
885 void DicomHandler::ReadCalibration()           << 463 //
886 {                                              << 464 G4int DicomHandler::ReadData(FILE *dicom,char * filename2)
887   fNbrequali = 0;                              << 
888   // CT2Density.dat contains the calibration c << 
889   // number to physical density                << 
890   std::ifstream calibration(fCt2DensityFile.c_ << 
891   calibration >> fNbrequali;                   << 
892   fValueDensity = new G4double[fNbrequali];    << 
893   fValueCT = new G4double[fNbrequali];         << 
894                                                << 
895   if (!calibration) {                          << 
896     G4Exception("DicomHandler::ReadFile", "DIC << 
897                 "@@@ No value to transform pix << 
898   }                                            << 
899   else {  // calibration was successfully open << 
900     for (G4int i = 0; i < fNbrequali; ++i) {   << 
901       calibration >> fValueCT[i] >> fValueDens << 
902     }                                          << 
903   }                                            << 
904   calibration.close();                         << 
905   fReadCalibration = true;                     << 
906 }                                              << 
907 #endif                                         << 
908                                                << 
909 #ifdef DICOM_USE_HEAD                          << 
910 G4float DicomHandler::Pixel2density(G4int pixe << 
911 {                                                 465 {
912   G4float density = -1;                        << 466     G4int returnvalue = 0;
913                                                   467 
914   // Air                                       << 468     //  READING THE PIXELS :
915   if (pixel == -998.) density = 0.001290;      << 469     G4int w = 0;
916   // Soft Tissue                               << 470     G4int len = 0;
917   else if (pixel == 24.)                       << 471     
918     density = 1.00;                            << 472     tab = new G4int*[rows];
919   // Brain                                     << 473     for ( G4int i = 0; i < rows; i ++ ) {
920   else if (pixel == 52.)                       << 474       tab[i] = new G4int[columns];
921     density = 1.03;                            << 475     }
922   // Spinal disc                               << 476 
923   else if (pixel == 92.)                       << 477     if(bitAllocated == 8) { // Case 8 bits :
924     density = 1.10;                            << 478 
925   // Trabecular bone                           << 479   std::printf("@@@ Error! Picture != 16 bits...\n");
926   else if (pixel == 197.)                      << 480   std::printf("@@@ Error! Picture != 16 bits...\n"); 
927     density = 1.18;                            << 481   std::printf("@@@ Error! Picture != 16 bits...\n"); 
928   // Cortical Bone                             << 482 
929   else if (pixel == 923.)                      << 483   unsigned char ch = 0;
930     density = 1.85;                            << 484 
931   // Tooth dentine                             << 485   len = rows*columns;
932   else if (pixel == 1280.)                     << 486   for(G4int j = 0; j < rows; j++) {
933     density = 2.14;                            << 487       for(G4int i = 0; i < columns; i++) {
934   // Tooth enamel                              << 488     w++;
935   else if (pixel == 2310.)                     << 489     std::fread( &ch, 1, 1, dicom);
936     density = 2.89;                            << 490     tab[j][i] = ch*rescaleSlope + rescaleIntercept;
                                                   >> 491       }
                                                   >> 492   }
                                                   >> 493   returnvalue = 1;
                                                   >> 494 
                                                   >> 495     } else { //  from 12 to 16 bits :
                                                   >> 496   char sbuff[2];
                                                   >> 497   short pixel;
                                                   >> 498   len = rows*columns;
                                                   >> 499   for( G4int j = 0; j < rows; j++) {
                                                   >> 500       for( G4int i = 0; i < columns; i++) {
                                                   >> 501     w++;
                                                   >> 502     std::fread(sbuff, 2, 1, dicom);
                                                   >> 503     GetValue(sbuff, pixel);
                                                   >> 504     tab[j][i] = pixel*rescaleSlope + rescaleIntercept;
                                                   >> 505       }
                                                   >> 506   }
                                                   >> 507     }
                                                   >> 508 
                                                   >> 509     // Creation of .g4 files wich contains averaged density data
                                                   >> 510 
                                                   >> 511     char * nameProcessed = new char[FILENAMESIZE];
                                                   >> 512     FILE* processed;
                                                   >> 513 
                                                   >> 514     std::sprintf(nameProcessed,"%s.g4",filename2);
                                                   >> 515     processed = std::fopen(nameProcessed,"w+b");
                                                   >> 516     std::printf("### Writing of %s ###\n",nameProcessed);
                                                   >> 517 
                                                   >> 518     std::fwrite(&rows, 2, 1, processed);
                                                   >> 519     std::fwrite(&columns, 2, 1, processed);
                                                   >> 520     std::fwrite(&pixelSpacingX, 8, 1, processed);
                                                   >> 521     std::fwrite(&pixelSpacingY, 8, 1, processed);
                                                   >> 522     std::fwrite(&sliceThickness, 8, 1, processed);
                                                   >> 523     std::fwrite(&sliceLocation, 8, 1, processed);
                                                   >> 524     std::fwrite(&compression, 2, 1, processed);
                                                   >> 525 
                                                   >> 526     std::printf("%8i   %8i\n",rows,columns);
                                                   >> 527     std::printf("%8f   %8f\n",pixelSpacingX,pixelSpacingY);
                                                   >> 528     std::printf("%8f\n", sliceThickness);
                                                   >> 529     std::printf("%8f\n", sliceLocation);
                                                   >> 530     std::printf("%8i\n", compression);
                                                   >> 531 
                                                   >> 532     G4int compSize = compression;
                                                   >> 533     G4int mean;
                                                   >> 534     G4double density;
                                                   >> 535     G4bool overflow = false;
                                                   >> 536     G4int cpt=1;
                                                   >> 537 
                                                   >> 538     if(compSize == 1) { // no compression: each pixel has a density value)
                                                   >> 539   for( G4int ww = 0; ww < rows; ww++) {
                                                   >> 540       for( G4int xx = 0; xx < columns; xx++) {
                                                   >> 541     mean = tab[ww][xx];
                                                   >> 542     density = Pixel2density(mean);
                                                   >> 543     std::fwrite(&density, sizeof(G4double), 1, processed);
                                                   >> 544             }
                                                   >> 545         }
                                                   >> 546 
                                                   >> 547     } else {
                                                   >> 548   // density value is the average of a square region of
                                                   >> 549   // compression*compression pixels
                                                   >> 550   for(G4int ww = 0; ww < rows ;ww += compSize ) {
                                                   >> 551       for(G4int xx = 0; xx < columns ;xx +=compSize ) {
                                                   >> 552     overflow = false;
                                                   >> 553     mean = 0;
                                                   >> 554     for(int sumx = 0; sumx < compSize; sumx++) {
                                                   >> 555         for(int sumy = 0; sumy < compSize; sumy++) {
                                                   >> 556       if(ww+sumy >= rows || xx+sumx >= columns) overflow = true;
                                                   >> 557       mean += tab[ww+sumy][xx+sumx];
                                                   >> 558         }
                                                   >> 559         if(overflow) break;
                                                   >> 560     }
                                                   >> 561     mean /= compSize*compSize;
                                                   >> 562     cpt = 1;
                                                   >> 563 
                                                   >> 564     if(!overflow) {
                                                   >> 565         G4double density = Pixel2density(mean);
                                                   >> 566         std::fwrite(&density, sizeof(G4double), 1, processed);
                                                   >> 567     }
                                                   >> 568       }
                                                   >> 569 
                                                   >> 570   }
                                                   >> 571     }
                                                   >> 572     std::fclose(processed);
                                                   >> 573 
                                                   >> 574     delete [] nameProcessed;
                                                   >> 575 
                                                   >> 576     /*    for ( G4int i = 0; i < rows; i ++ ) {
                                                   >> 577       delete [] tab[i];
                                                   >> 578     }
                                                   >> 579     delete [] tab;
                                                   >> 580     */
                                                   >> 581 
                                                   >> 582     return returnvalue;
                                                   >> 583 }
                                                   >> 584 
                                                   >> 585 /*
                                                   >> 586   G4int DicomHandler::displayImage(char command[300])
                                                   >> 587   {
                                                   >> 588   //   Display DICOM images using ImageMagick
                                                   >> 589   char commandName[500];
                                                   >> 590   std::sprintf(commandName,"display  %s",command);
                                                   >> 591   std::printf(commandName);
                                                   >> 592   G4int i = system(commandName);
                                                   >> 593   return (G4int )i;
                                                   >> 594   }
                                                   >> 595 */
                                                   >> 596 
                                                   >> 597 G4double DicomHandler::Pixel2density(G4int pixel)
                                                   >> 598 {
                                                   >> 599     G4double density = -1.;
                                                   >> 600     G4int nbrequali = 0;
                                                   >> 601     G4double deltaCT = 0;
                                                   >> 602     G4double deltaDensity = 0;
                                                   >> 603 
                                                   >> 604     // CT2Density.dat contains the calibration curve to convert CT (Hounsfield)
                                                   >> 605     // number to physical density
                                                   >> 606     std::ifstream calibration("CT2Density.dat");
                                                   >> 607     calibration >> nbrequali;
                                                   >> 608 
                                                   >> 609     G4double * valuedensity = new G4double[nbrequali];
                                                   >> 610     G4double * valueCT = new G4double[nbrequali];
                                                   >> 611 
                                                   >> 612     if(!calibration) {
                                                   >> 613   G4cerr << "@@@ No value to transform pixels in density!" << G4endl;
                                                   >> 614   exit(1);
                                                   >> 615 
                                                   >> 616     } else { // calibration was successfully opened
                                                   >> 617   for(G4int i = 0; i < nbrequali; i++) { // Loop to store all the pts in CT2Density.dat
                                                   >> 618       calibration >> valueCT[i] >> valuedensity[i];
                                                   >> 619   }
                                                   >> 620     }
                                                   >> 621     calibration.close();
                                                   >> 622 
                                                   >> 623     for(G4int j = 1; j < nbrequali; j++) {
                                                   >> 624   if( pixel >= valueCT[j-1] && pixel < valueCT[j]) {
                                                   >> 625 
                                                   >> 626       deltaCT = valueCT[j] - valueCT[j-1];
                                                   >> 627       deltaDensity = valuedensity[j] - valuedensity[j-1];
                                                   >> 628 
                                                   >> 629       // interpolating linearly
                                                   >> 630       density = valuedensity[j] - ((valueCT[j] - pixel)*deltaDensity/deltaCT );
                                                   >> 631       break;
                                                   >> 632   }
                                                   >> 633     }
                                                   >> 634 
                                                   >> 635     if(density < 0.) {
                                                   >> 636   std::printf("@@@ Error density = %f && Pixel = %i (0x%x) && deltaDensity/deltaCT = %f\n",density,pixel,pixel, deltaDensity/deltaCT);
                                                   >> 637     }
                                                   >> 638     
                                                   >> 639     delete [] valuedensity;
                                                   >> 640     delete [] valueCT;
937                                                   641 
938   return density;                              << 642     return density;
939 }                                                 643 }
940                                                   644 
941 #else                                          << 
942 //....oooOO0OOooo........oooOO0OOooo........oo << 
943                                                << 
944 G4float DicomHandler::Pixel2density(G4int pixe << 
945 {                                              << 
946   if (!fReadCalibration) {                     << 
947     ReadCalibration();                         << 
948   }                                            << 
949                                                << 
950   G4float density = -1.;                       << 
951   G4double deltaCT = 0;                        << 
952   G4double deltaDensity = 0;                   << 
953                                                << 
954   for (G4int j = 1; j < fNbrequali; ++j) {     << 
955     if (pixel >= fValueCT[j - 1] && pixel < fV << 
956       deltaCT = fValueCT[j] - fValueCT[j - 1]; << 
957       deltaDensity = fValueDensity[j] - fValue << 
958                                                << 
959       // interpolating linearly                << 
960       density = G4float(fValueDensity[j] - ((f << 
961       break;                                   << 
962     }                                          << 
963   }                                            << 
964                                                << 
965   if (density < 0.) {                          << 
966     std::printf(                               << 
967       "@@@ Error density = %f && Pixel = %i \  << 
968       (0x%x) && deltaDensity/deltaCT = %f\n",  << 
969       density, pixel, pixel, deltaDensity / de << 
970   }                                            << 
971                                                << 
972   return density;                              << 
973 }                                              << 
974 #endif                                         << 
975 //....oooOO0OOooo........oooOO0OOooo........oo << 
976                                                   645 
977 void DicomHandler::CheckFileFormat()              646 void DicomHandler::CheckFileFormat()
978 {                                                 647 {
979   std::ifstream checkData(fDriverFile.c_str()) << 648     std::ifstream checkData("Data.dat");
980   char* oneLine = new char[128];               << 649     char * oneLine = new char[128];
981                                                   650 
982   if (!(checkData.is_open())) {  // Check exis << 651     if(!(checkData.is_open())) { //Check existance of Data.dat
983                                                << 
984     G4String message = "\nDicomG4 needs Data.d << 
985     message += " in command line):\n";         << 
986     message += "\tFirst line: number of image  << 
987     message += "\tSecond line: number of image << 
988     message += "\tEach following line contains << 
989     message += " except for the .dcm extension << 
990     G4Exception("DicomHandler::ReadFile", "DIC << 
991   }                                            << 
992                                                   652 
993   checkData >> fCompression;                   << 653   G4cout << "\nDicomG4 needs Data.dat :\n\tFirst line: number of image pixel for a "
994   checkData >> fNFiles;                        << 654          << "voxel (G4Box)\n\tSecond line: number of images (CT slices) to "
995   G4String oneName;                            << 655          << "read\n\tEach following line contains the name of a Dicom image except "
996   checkData.getline(oneLine, 100);             << 656          << "for the .dcm extension\n";
997   std::ifstream testExistence;                 << 657   exit(0);
998   G4bool existAlready = true;                  << 658     }
999   for (G4int rep = 0; rep < fNFiles; ++rep) {  << 659 
1000     checkData.getline(oneLine, 100);          << 660     checkData >> compression;
1001     oneName = oneLine;                        << 661     checkData >> nFiles;
1002     oneName += ".g4dcm";  // create dicomFile << 662     G4String oneName;
1003     G4cout << fNFiles << " test file " << one << 663     checkData.getline(oneLine,100);
1004     testExistence.open(oneName.data());       << 664     std::ifstream testExistence;
1005     if (!(testExistence.is_open())) {         << 665     G4bool existAlready = true;
1006       existAlready = false;                   << 666     for(G4int rep = 0; rep < nFiles; rep++) { 
                                                   >> 667       checkData.getline(oneLine,100);
                                                   >> 668       oneName = oneLine;
                                                   >> 669       oneName += ".g4dcm"; // create dicomFile.g4dcm
                                                   >> 670       G4cout << nFiles << " test file " << oneName << G4endl;
                                                   >> 671       testExistence.open(oneName.data());
                                                   >> 672       if(!(testExistence.is_open())) {
                                                   >> 673   existAlready = false;
                                                   >> 674   testExistence.clear();
                                                   >> 675   testExistence.close();
                                                   >> 676       }
1007       testExistence.clear();                     677       testExistence.clear();
1008       testExistence.close();                     678       testExistence.close();
1009     }                                            679     }
1010     testExistence.clear();                    << 
1011     testExistence.close();                    << 
1012   }                                           << 
1013                                               << 
1014   ReadMaterialIndices(checkData);             << 
1015                                               << 
1016   checkData.close();                          << 
1017   delete[] oneLine;                           << 
1018                                               << 
1019   if (existAlready == false) {  // The files  << 
1020                                               << 
1021     G4cout << "\nAll the necessary images wer << 
1022            << ", starting with .dcm images\n" << 
1023                                               << 
1024     FILE* dicom;                              << 
1025     FILE* lecturePref;                        << 
1026     char* fCompressionc = new char[LINEBUFFSI << 
1027     char* maxc = new char[LINEBUFFSIZE];      << 
1028     // char name[300], inputFile[300];        << 
1029     char* inputFile = new char[FILENAMESIZE]; << 
1030     G4int rflag;                              << 
1031     lecturePref = std::fopen(fDriverFile.c_st << 
1032                                               << 
1033     rflag = std::fscanf(lecturePref, "%s", fC << 
1034     fCompression = atoi(fCompressionc);       << 
1035     rflag = std::fscanf(lecturePref, "%s", ma << 
1036     fNFiles = atoi(maxc);                     << 
1037     G4cout << " fNFiles " << fNFiles << G4end << 
1038                                               << 
1039     /////////////////////                     << 
1040                                               << 
1041 #ifdef DICOM_USE_HEAD                         << 
1042     for (G4int i = 1; i <= fNFiles; ++i) {  / << 
1043       rflag = std::fscanf(lecturePref, "%s",  << 
1044       G4String path = GetDicomDataPath() + "/ << 
1045                                               << 
1046       G4String name = inputFile + G4String(". << 
1047       // Writes the results to a character st << 
1048                                               << 
1049       G4String name2 = path + name;           << 
1050       //  Open input file and give it to gest << 
1051       G4cout << "### Opening " << name2 << "  << 
1052       dicom = std::fopen(name2.c_str(), "rb") << 
1053       // Reading the .dcm in two steps:       << 
1054       //      1.  reading the header          << 
1055       //      2. reading the pixel data and s << 
1056       if (dicom != 0) {                       << 
1057         ReadFile(dicom, inputFile);           << 
1058       }                                       << 
1059       else {                                  << 
1060         G4cout << "\nError opening file : " < << 
1061       }                                       << 
1062       rflag = std::fclose(dicom);             << 
1063     }                                         << 
1064 #else                                         << 
1065                                               << 
1066     for (G4int i = 1; i <= fNFiles; ++i) {  / << 
1067       rflag = std::fscanf(lecturePref, "%s",  << 
1068                                               << 
1069       G4String name = inputFile + G4String(". << 
1070       // Writes the results to a character st << 
1071                                               << 
1072       // G4cout << "check: " << name << G4end << 
1073       //  Open input file and give it to gest << 
1074       G4cout << "### Opening " << name << " a << 
1075       dicom = std::fopen(name.c_str(), "rb"); << 
1076       // Reading the .dcm in two steps:       << 
1077       //      1.  reading the header          << 
1078       //      2. reading the pixel data and s << 
1079       if (dicom != 0) {                       << 
1080         ReadFile(dicom, inputFile);           << 
1081       }                                       << 
1082       else {                                  << 
1083         G4cout << "\nError opening file : " < << 
1084       }                                       << 
1085       rflag = std::fclose(dicom);             << 
1086     }                                         << 
1087 #endif                                        << 
1088                                               << 
1089     rflag = std::fclose(lecturePref);         << 
1090                                               << 
1091     // Checks the spacing is correct. Dumps t << 
1092     fMergedSlices->CheckSlices();             << 
1093                                               << 
1094     delete[] fCompressionc;                   << 
1095     delete[] maxc;                            << 
1096     delete[] inputFile;                       << 
1097     if (rflag) return;                        << 
1098   }                                           << 
1099                                               << 
1100   if (fValueDensity) {                        << 
1101     delete[] fValueDensity;                   << 
1102   }                                           << 
1103   if (fValueCT) {                             << 
1104     delete[] fValueCT;                        << 
1105   }                                           << 
1106   if (fMergedSlices) {                        << 
1107     delete fMergedSlices;                     << 
1108   }                                           << 
1109 }                                             << 
1110                                               << 
1111 //....oooOO0OOooo........oooOO0OOooo........o << 
1112                                               << 
1113 G4int DicomHandler::read_defined_nested(FILE* << 
1114 {                                             << 
1115   //      VARIABLES                           << 
1116   unsigned short item_GroupNumber;            << 
1117   unsigned short item_ElementNumber;          << 
1118   G4int item_Length;                          << 
1119   G4int items_array_length = 0;               << 
1120   char* buffer = new char[LINEBUFFSIZE];      << 
1121   size_t rflag = 0;                           << 
1122                                               << 
1123   while (items_array_length < SQ_Length) {    << 
1124     rflag = std::fread(buffer, 2, 1, nested); << 
1125     GetValue(buffer, item_GroupNumber);       << 
1126                                               << 
1127     rflag = std::fread(buffer, 2, 1, nested); << 
1128     GetValue(buffer, item_ElementNumber);     << 
1129                                               << 
1130     rflag = std::fread(buffer, 4, 1, nested); << 
1131     GetValue(buffer, item_Length);            << 
1132                                                  680 
1133     rflag = std::fread(buffer, item_Length, 1 << 681     ReadMaterialIndices( checkData );
1134                                                  682 
1135     items_array_length = items_array_length + << 683     checkData.close();
1136   }                                           << 684     delete [] oneLine;
1137                                               << 
1138   delete[] buffer;                            << 
1139                                                  685 
1140   if (SQ_Length > items_array_length)         << 686     if( existAlready == false ) { // The files *.g4dcm have to be created
1141     return 0;                                 << 
1142   else                                        << 
1143     return 1;                                 << 
1144   if (rflag) return 1;                        << 
1145 }                                             << 
1146                                                  687 
1147 //....oooOO0OOooo........oooOO0OOooo........o << 688   G4cout << "\nAll the necessary images were not found in processed form, starting "
1148                                               << 689          << "with .dcm images\n";
1149 void DicomHandler::read_undefined_nested(FILE << 690 
1150 {                                             << 691   FILE * dicom;
1151   //      VARIABLES                           << 692   FILE * lecturePref;
1152   unsigned short item_GroupNumber;            << 693   char * compressionc = new char[LINEBUFFSIZE];
1153   unsigned short item_ElementNumber;          << 694   char * maxc = new char[LINEBUFFSIZE];
1154   unsigned int item_Length;                   << 695   //char name[300], inputFile[300];
1155   char* buffer = new char[LINEBUFFSIZE];      << 696   char * name = new char[FILENAMESIZE];
1156   size_t rflag = 0;                           << 697   char * inputFile = new char[FILENAMESIZE];
1157                                               << 698 
1158   do {                                        << 699   lecturePref = std::fopen("Data.dat","r");
1159     rflag = std::fread(buffer, 2, 1, nested); << 700   std::fscanf(lecturePref,"%s",compressionc);
1160     GetValue(buffer, item_GroupNumber);       << 701   compression = atoi(compressionc);
1161                                               << 702   std::fscanf(lecturePref,"%s",maxc);
1162     rflag = std::fread(buffer, 2, 1, nested); << 703   nFiles = atoi(maxc);
1163     GetValue(buffer, item_ElementNumber);     << 704   G4cout << " nFiles " << nFiles << G4endl;
1164                                               << 705 
1165     rflag = std::fread(buffer, 4, 1, nested); << 706   for( G4int i = 1; i <= nFiles; i++ ) { // Begin loop on filenames
1166     GetValue(buffer, item_Length);            << 707 
1167                                               << 708       std::fscanf(lecturePref,"%s",inputFile);
1168     if (item_Length != 0xffffffff)            << 709       std::sprintf(name,"%s.dcm",inputFile);
1169       rflag = std::fread(buffer, item_Length, << 710       std::cout << "check 1: " << name << std::endl;
1170     else                                      << 711       //  Open input file and give it to gestion_dicom :
1171       read_undefined_item(nested);            << 712       std::printf("### Opening %s and reading :\n",name);
                                                   >> 713       dicom = std::fopen(name,"rb");
                                                   >> 714       // Reading the .dcm in two steps:
                                                   >> 715       //      1.  reading the header
                                                   >> 716       //  2. reading the pixel data and store the density in Moyenne.dat
                                                   >> 717       if( dicom != 0 ) {
                                                   >> 718     ReadFile(dicom,inputFile);
                                                   >> 719       } else {
                                                   >> 720     G4cout << "\nError opening file : " << name << G4endl;
                                                   >> 721       }
                                                   >> 722       std::fclose(dicom);
                                                   >> 723   }
                                                   >> 724   std::fclose(lecturePref);
                                                   >> 725 
                                                   >> 726   delete [] compressionc;
                                                   >> 727   delete [] maxc;
                                                   >> 728   delete [] name;
                                                   >> 729   delete [] inputFile;
1172                                                  730 
1173   } while (item_GroupNumber != 0xFFFE || item << 731     } 
1174                                                  732 
1175   delete[] buffer;                            << 
1176   if (rflag) return;                          << 
1177 }                                                733 }
1178                                                  734 
1179 //....oooOO0OOooo........oooOO0OOooo........o << 
1180                                               << 
1181 void DicomHandler::read_undefined_item(FILE*  << 
1182 {                                             << 
1183   //      VARIABLES                           << 
1184   unsigned short item_GroupNumber;            << 
1185   unsigned short item_ElementNumber;          << 
1186   G4int item_Length;                          << 
1187   size_t rflag = 0;                           << 
1188   char* buffer = new char[LINEBUFFSIZE];      << 
1189                                               << 
1190   do {                                        << 
1191     rflag = std::fread(buffer, 2, 1, nested); << 
1192     GetValue(buffer, item_GroupNumber);       << 
1193                                               << 
1194     rflag = std::fread(buffer, 2, 1, nested); << 
1195     GetValue(buffer, item_ElementNumber);     << 
1196                                                  735 
1197     rflag = std::fread(buffer, 4, 1, nested); << 736 template <class Type>
1198     GetValue(buffer, item_Length);            << 737 void DicomHandler::GetValue(char * _val, Type & _rval) {
1199                                                  738 
1200     if (item_Length != 0) rflag = std::fread( << 
1201                                               << 
1202   } while (item_GroupNumber != 0xFFFE || item << 
1203                                               << 
1204   delete[] buffer;                            << 
1205   if (rflag) return;                          << 
1206 }                                             << 
1207                                               << 
1208 //....oooOO0OOooo........oooOO0OOooo........o << 
1209                                               << 
1210 template<class Type>                          << 
1211 void DicomHandler::GetValue(char* _val, Type& << 
1212 {                                             << 
1213 #if BYTE_ORDER == BIG_ENDIAN                     739 #if BYTE_ORDER == BIG_ENDIAN
1214   if (fLittleEndian) {  // little endian      << 740     if(littleEndian) {      // little endian
1215 #else  // BYTE_ORDER == LITTLE_ENDIAN         << 741 #else // BYTE_ORDER == LITTLE_ENDIAN
1216   if (!fLittleEndian) {  // big endian        << 742     if(!littleEndian) {     // big endian
1217 #endif                                           743 #endif
1218     const G4int SIZE = sizeof(_rval);         << 744   const int SIZE = sizeof(_rval);
1219     char ctemp;                               << 745   char ctemp;
1220     for (G4int i = 0; i < SIZE / 2; ++i) {    << 746   for(int i = 0; i < SIZE/2; i++) {
1221       ctemp = _val[i];                        << 747       ctemp = _val[i];
1222       _val[i] = _val[SIZE - 1 - i];           << 748       _val[i] = _val[SIZE - 1 - i];
1223       _val[SIZE - 1 - i] = ctemp;             << 749       _val[SIZE - 1 - i] = ctemp;
                                                   >> 750   }
1224     }                                            751     }
1225   }                                           << 752     _rval = *(Type *)_val;
1226   _rval = *(Type*)_val;                       << 
1227 }                                                753 }
1228                                               << 
1229 //....oooOO0OOooo........oooOO0OOooo........o << 
1230                                                  754