80 : DATABUFFSIZE(8192), LINEBUFFSIZE(5020), FILENAMESIZE(512),
81 fCompression(0), fNFiles(0), fRows(0), fColumns(0),
82 fBitAllocated(0), fMaxPixelValue(0), fMinPixelValue(0),
83 fPixelSpacingX(0.), fPixelSpacingY(0.),
84 fSliceThickness(0.), fSliceLocation(0.),
85 fRescaleIntercept(0), fRescaleSlope(0),
86 fLittleEndian(true), fImplicitEndian(false),
87 fPixelRepresentation(0), nbrequali(0),
88 valuedensity(NULL),valueCT(NULL),readCalibration(false),
89 mergedSlices(NULL),driverFile(
"Data.dat"),ct2densityFile(
"CT2Density.dat")
107 G4int returnvalue = 0;
size_t rflag = 0;
113 rflag = std::fread( buffer, 1, 128, dicom );
116 rflag = std::fread( buffer, 1, 4, dicom );
118 if(std::strncmp(
"DICM", buffer, 4) != 0) {
119 std::fseek(dicom, 0, SEEK_SET);
125 short elementLength2;
127 unsigned long elementLength4;
139 rflag = std::fread(buffer, 2, 1, dicom);
142 rflag = std::fread(buffer, 2, 1, dicom);
146 G4int tagDictionary = readGroupId*0x10000 + readElementId;
149 if(tagDictionary == 0x7FE00010) {
151 rflag = std::fread(buffer,2,1,dicom);
152 rflag = std::fread(buffer,4,1,dicom);
157 rflag = std::fread(buffer,2,1,dicom);
162 if((elementLength2 == 0x424f ||
163 elementLength2 == 0x574f ||
164 elementLength2 == 0x464f ||
165 elementLength2 == 0x5455 ||
166 elementLength2 == 0x5153 ||
167 elementLength2 == 0x4e55) &&
170 rflag = std::fread(buffer, 2, 1, dicom);
173 rflag = std::fread(buffer, 4, 1, dicom);
176 if(elementLength2 == 0x5153)
178 if(elementLength4 == 0xFFFFFFFF)
184 G4cerr <<
"Function read_defined_nested() failed!" <<
G4endl;
189 rflag = std::fread(data, elementLength4,1,dicom);
200 rflag = std::fread(buffer, 2, 1, dicom);
202 elementLength4 = elementLength2;
204 rflag = std::fread(data, elementLength4, 1, dicom);
211 if(std::fseek(dicom, -2, SEEK_CUR) != 0) {
215 rflag = std::fread(buffer, 4, 1, dicom);
220 if(elementLength4 == 0xFFFFFFFF)
225 rflag = std::fread(data, elementLength4, 1, dicom);
232 data[elementLength4] =
'\0';
243 std::map<G4float,G4String>::const_iterator ite;
304 if (rflag)
return returnvalue;
312 if(tagDictionary == 0x00280010 ) {
314 std::printf(
"[0x00280010] Rows -> %i\n",
fRows);
316 }
else if(tagDictionary == 0x00280011 ) {
318 std::printf(
"[0x00280011] Columns -> %i\n",
fColumns);
320 }
else if(tagDictionary == 0x00280102 ) {
323 std::printf(
"[0x00280102] High bits -> %i\n",highBits);
325 }
else if(tagDictionary == 0x00280100 ) {
327 std::printf(
"[0x00280100] Bits allocated -> %i\n",
fBitAllocated);
329 }
else if(tagDictionary == 0x00280101 ) {
332 std::printf(
"[0x00280101] Bits stored -> %i\n",bitStored);
334 }
else if(tagDictionary == 0x00280106 ) {
336 std::printf(
"[0x00280106] Min. pixel value -> %i\n",
fMinPixelValue);
338 }
else if(tagDictionary == 0x00280107 ) {
340 std::printf(
"[0x00280107] Max. pixel value -> %i\n",
fMaxPixelValue);
342 }
else if(tagDictionary == 0x00281053) {
344 std::printf(
"[0x00281053] Rescale Slope -> %d\n",
fRescaleSlope);
346 }
else if(tagDictionary == 0x00281052 ) {
350 }
else if(tagDictionary == 0x00280103 ) {
355 std::printf(
"### PIXEL REPRESENTATION = 1, BITS ARE SIGNED, ");
356 std::printf(
"DICOM READING SCAN FOR UNSIGNED VALUE, POSSIBLE ");
357 std::printf(
"ERROR !!!!!! -> \n");
360 }
else if(tagDictionary == 0x00080006 ) {
361 std::printf(
"[0x00080006] Modality -> %s\n", data);
363 }
else if(tagDictionary == 0x00080070 ) {
364 std::printf(
"[0x00080070] Manufacturer -> %s\n", data);
366 }
else if(tagDictionary == 0x00080080 ) {
367 std::printf(
"[0x00080080] Institution Name -> %s\n", data);
369 }
else if(tagDictionary == 0x00080081 ) {
370 std::printf(
"[0x00080081] Institution Address -> %s\n", data);
372 }
else if(tagDictionary == 0x00081040 ) {
373 std::printf(
"[0x00081040] Institution Department Name -> %s\n", data);
375 }
else if(tagDictionary == 0x00081090 ) {
376 std::printf(
"[0x00081090] Manufacturer's Model Name -> %s\n", data);
378 }
else if(tagDictionary == 0x00181000 ) {
379 std::printf(
"[0x00181000] Device Serial Number -> %s\n", data);
381 }
else if(tagDictionary == 0x00080008 ) {
382 std::printf(
"[0x00080008] Image Types -> %s\n", data);
384 }
else if(tagDictionary == 0x00283000 ) {
385 std::printf(
"[0x00283000] Modality LUT Sequence SQ 1 -> %s\n", data);
387 }
else if(tagDictionary == 0x00283002 ) {
388 std::printf(
"[0x00283002] LUT Descriptor US or SS 3 -> %s\n", data);
390 }
else if(tagDictionary == 0x00283003 ) {
391 std::printf(
"[0x00283003] LUT Explanation LO 1 -> %s\n", data);
393 }
else if(tagDictionary == 0x00283004 ) {
394 std::printf(
"[0x00283004] Modality LUT Type LO 1 -> %s\n", data);
396 }
else if(tagDictionary == 0x00283006 ) {
397 std::printf(
"[0x00283006] LUT Data US or SS -> %s\n", data);
399 }
else if(tagDictionary == 0x00283010 ) {
400 std::printf(
"[0x00283010] VOI LUT Sequence SQ 1 -> %s\n", data);
402 }
else if(tagDictionary == 0x00280120 ) {
403 std::printf(
"[0x00280120] Pixel Padding Value US or SS 1 -> %s\n", data);
405 }
else if(tagDictionary == 0x00280030 ) {
407 int iss = datas.find(
'\\');
409 fPixelSpacingY = atof( datas.substr(iss+2,datas.length()).c_str() );
411 }
else if(tagDictionary == 0x00200037 ) {
412 std::printf(
"[0x00200037] Image Orientation (Phantom) -> %s\n", data);
414 }
else if(tagDictionary == 0x00200032 ) {
415 std::printf(
"[0x00200032] Image Position (Phantom,mm) -> %s\n", data);
417 }
else if(tagDictionary == 0x00180050 ) {
419 std::printf(
"[0x00180050] Slice Thickness (mm) -> %f\n",
fSliceThickness);
421 }
else if(tagDictionary == 0x00201041 ) {
423 std::printf(
"[0x00201041] Slice Location -> %f\n",
fSliceLocation);
425 }
else if(tagDictionary == 0x00280004 ) {
426 std::printf(
"[0x00280004] Photometric Interpretation -> %s\n", data);
428 }
else if(tagDictionary == 0x00020010) {
429 if(strcmp(data,
"1.2.840.10008.1.2") == 0)
431 else if(strncmp(data,
"1.2.840.10008.1.2.2", 19) == 0)
435 std::printf(
"[0x00020010] Endian -> %s\n", data);
454 if(!dcmPZSH) {
return; }
480 if(ww+sumy >= fRows || xx+sumx >= fColumns) overflow =
true;
481 mean +=
fTab[ww+sumy][xx+sumx];
527 if(ww+sumy >= fRows || xx+sumx >= fColumns) overflow =
true;
528 mean +=
fTab[ww+sumy][xx+sumx];
550 foutG4DCM << density <<
" ";
551 if( xx%8 == 3 ) foutG4DCM <<
G4endl;
564 if(ww+sumy >= fRows || xx+sumx >= fColumns) overflow =
true;
565 mean +=
fTab[ww+sumy][xx+sumx];
573 foutG4DCM << density <<
" ";
574 if( xx/fCompression%8 == 3 ) foutG4DCM <<
G4endl;
591 if( finData.eof() )
return;
594 for(
unsigned int ii = 0; ii < nMate; ii++ ){
595 finData >> mateName >> densityMax;
597 G4cout << ii <<
" ReadMaterialIndices " << mateName <<
" " << densityMax <<
G4endl;
606 std::map<G4float,G4String>::reverse_iterator ite;
609 if( density >= (*ite).first ) {
626 G4int returnvalue = 0;
size_t rflag = 0;
638 std::printf(
"@@@ Error! Picture != 16 bits...\n");
639 std::printf(
"@@@ Error! Picture != 16 bits...\n");
640 std::printf(
"@@@ Error! Picture != 16 bits...\n");
642 unsigned char ch = 0;
647 rflag = std::fread( &ch, 1, 1, dicom);
659 rflag = std::fread(sbuff, 2, 1, dicom);
670 std::sprintf(nameProcessed,
"%s.g4dcmb",filename2);
671 fileOut = std::fopen(nameProcessed,
"w+b");
672 std::printf(
"### Writing of %s ###\n",nameProcessed);
675 rflag = std::fwrite(&nMate,
sizeof(
unsigned int), 1, fileOut);
677 std::map<G4float,G4String>::const_iterator ite;
680 for(
G4int ii = (*ite).second.length(); ii < 40; ii++ ) {
684 const char* mateNameC = mateName.c_str();
685 rflag = std::fwrite(mateNameC,
sizeof(
char),40, fileOut);
690 unsigned int planesC = 1;
698 rflag = std::fwrite(&fRowsC,
sizeof(
unsigned int), 1, fileOut);
699 rflag = std::fwrite(&fColumnsC,
sizeof(
unsigned int), 1, fileOut);
700 rflag = std::fwrite(&planesC,
sizeof(
unsigned int), 1, fileOut);
702 rflag = std::fwrite(&pixelLocationXM,
sizeof(
G4float), 1, fileOut);
703 rflag = std::fwrite(&pixelLocationXP,
sizeof(
G4float), 1, fileOut);
704 rflag = std::fwrite(&pixelLocationYM,
sizeof(
G4float), 1, fileOut);
705 rflag = std::fwrite(&pixelLocationYP,
sizeof(
G4float), 1, fileOut);
706 rflag = std::fwrite(&fSliceLocationZM,
sizeof(
G4float), 1, fileOut);
707 rflag = std::fwrite(&fSliceLocationZP,
sizeof(
G4float), 1, fileOut);
710 std::printf(
"%8i %8i\n",fRows,
fColumns);
728 rflag = std::fwrite(&mateID,
sizeof(
unsigned int), 1, fileOut);
735 for(
G4int ww = 0; ww <
fRows ;ww += compSize ) {
739 for(
int sumx = 0; sumx < compSize; sumx++) {
740 for(
int sumy = 0; sumy < compSize; sumy++) {
741 if(ww+sumy >= fRows || xx+sumx >= fColumns) overflow =
true;
742 mean +=
fTab[ww+sumy][xx+sumx];
746 mean /= compSize*compSize;
751 rflag = std::fwrite(&mateID,
sizeof(
unsigned int), 1, fileOut);
764 rflag = std::fwrite(&density,
sizeof(
G4float), 1, fileOut);
771 for(
G4int ww = 0; ww <
fRows ;ww += compSize ) {
775 for(
int sumx = 0; sumx < compSize; sumx++) {
776 for(
int sumy = 0; sumy < compSize; sumy++) {
777 if(ww+sumy >= fRows || xx+sumx >= fColumns) overflow =
true;
778 mean +=
fTab[ww+sumy][xx+sumx];
782 mean /= compSize*compSize;
786 rflag = std::fwrite(&density,
sizeof(
G4float), 1, fileOut);
793 rflag = std::fclose(fileOut);
795 delete [] nameProcessed;
803 if (rflag)
return returnvalue;
825 G4cerr <<
"@@@ No value to transform pixels in density!" <<
G4endl;
856 density = valuedensity[j] - ((valueCT[j] - pixel)*deltaDensity/deltaCT );
862 std::printf(
"@@@ Error density = %f && Pixel = %i \
863 (0x%x) && deltaDensity/deltaCT = %f\n",density,pixel,pixel, deltaDensity/deltaCT);
874 char * oneLine =
new char[128];
876 if(!(checkData.is_open())) {
878 G4cout <<
"\nDicomG4 needs Data.dat (or another driver file specified in command line)"
879 <<
":\n\tFirst line: number of image pixel for a "
880 <<
"voxel (G4Box)\n\tSecond line: number of images (CT slices) to "
881 <<
"read\n\tEach following line contains the name of a Dicom image except "
882 <<
"for the .dcm extension\n";
889 checkData.getline(oneLine,100);
890 std::ifstream testExistence;
891 G4bool existAlready =
true;
893 checkData.getline(oneLine,100);
896 G4cout << fNFiles <<
" test file " << oneName <<
G4endl;
897 testExistence.open(oneName.
data());
898 if(!(testExistence.is_open())) {
899 existAlready =
false;
900 testExistence.clear();
901 testExistence.close();
903 testExistence.clear();
904 testExistence.close();
912 if( existAlready ==
false ) {
914 G4cout <<
"\nAll the necessary images were not found in processed form, starting "
915 <<
"with .dcm images\n";
926 lecturePref = std::fopen(
driverFile.c_str(),
"r");
927 rflag = std::fscanf(lecturePref,
"%s",fCompressionc);
928 fCompression = atoi(fCompressionc);
929 rflag = std::fscanf(lecturePref,
"%s",maxc);
930 fNFiles = atoi(maxc);
935 rflag = std::fscanf(lecturePref,
"%s",inputFile);
936 std::sprintf(name,
"%s.dcm",inputFile);
937 std::cout <<
"check 1: " << name << std::endl;
939 std::printf(
"### Opening %s and reading :\n",name);
940 dicom = std::fopen(name,
"rb");
949 rflag = std::fclose(dicom);
951 rflag = std::fclose(lecturePref);
956 delete [] fCompressionc;
976 unsigned short item_GroupNumber;
977 unsigned short item_ElementNumber;
979 G4int items_array_length=0;
983 while(items_array_length < SQ_Length)
985 rflag = std::fread(buffer, 2, 1, nested);
988 rflag = std::fread(buffer, 2, 1, nested);
989 GetValue(buffer, item_ElementNumber);
991 rflag = std::fread(buffer, 4, 1, nested);
994 rflag = std::fread(buffer, item_Length, 1, nested);
996 items_array_length= items_array_length+8+item_Length;
1001 if( SQ_Length>items_array_length )
1005 if (rflag)
return 1;
1013 unsigned short item_GroupNumber;
1014 unsigned short item_ElementNumber;
1015 unsigned int item_Length;
1021 rflag = std::fread(buffer, 2, 1, nested);
1022 GetValue(buffer, item_GroupNumber);
1024 rflag = std::fread(buffer, 2, 1, nested);
1025 GetValue(buffer, item_ElementNumber);
1027 rflag = std::fread(buffer, 4, 1, nested);
1030 if(item_Length!=0xffffffff)
1031 rflag = std::fread(buffer, item_Length, 1, nested);
1036 }
while(item_GroupNumber!=0xFFFE || item_ElementNumber!=0xE0DD || item_Length!=0);
1047 unsigned short item_GroupNumber;
1048 unsigned short item_ElementNumber;
1049 G4int item_Length;
size_t rflag = 0;
1054 rflag = std::fread(buffer, 2, 1, nested);
1055 GetValue(buffer, item_GroupNumber);
1057 rflag = std::fread(buffer, 2, 1, nested);
1058 GetValue(buffer, item_ElementNumber);
1060 rflag = std::fread(buffer, 4, 1, nested);
1065 rflag = std::fread(buffer,item_Length,1,nested);
1068 while(item_GroupNumber!=0xFFFE || item_ElementNumber!=0xE00D || item_Length!=0);
1076 template <
class Type>
1079 #if BYTE_ORDER == BIG_ENDIAN
1081 #else // BYTE_ORDER == LITTLE_ENDIAN
1084 const int SIZE =
sizeof(_rval);
1086 for(
int i = 0; i < SIZE/2; i++) {
1088 _val[i] = _val[SIZE - 1 - i];
1089 _val[SIZE - 1 - i] = ctemp;
1092 _rval = *(Type *)_val;
void GetValue(char *, Type &)
void read_undefined_nested(FILE *)
void GetInformation(G4int &, char *)
Definition of the DicomPhantomZSliceMerged class.
DicomPhantomZSliceMerged * mergedSlices
static DicomHandler * fgInstance
DicomHandler.cc :
static DicomHandler * Instance()
G4GLOB_DLL std::ostream G4cout
unsigned int GetMaterialIndex(G4float density)
G4int ReadFile(FILE *, char *)
void StoreData(std::ofstream &foutG4DCM)
const char * data() const
G4int ReadData(FILE *, char *)
short fPixelRepresentation
Definition of the DicomHandler class.
G4float Pixel2density(G4int pixel)
std::map< G4float, G4String > fMaterialIndices
void AddZSlice(DicomPhantomZSliceHeader *val)
void ReadMaterialIndices(std::ifstream &finData)
G4int read_defined_nested(FILE *, G4int)
void read_undefined_item(FILE *)
G4GLOB_DLL std::ostream G4cerr