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 std::ostringstream message;
879 message <<
"\nDicomG4 needs Data.dat (or another driver file specified"
880 <<
" in command line):\n"
881 <<
"\tFirst line: number of image pixel for a voxel (G4Box)\n"
882 <<
"\tSecond line: number of images (CT slices) to read\n"
883 <<
"\tEach following line contains the name of a Dicom image"
884 <<
" except for the .dcm extension";
892 checkData.getline(oneLine,100);
893 std::ifstream testExistence;
894 G4bool existAlready =
true;
896 checkData.getline(oneLine,100);
899 G4cout << fNFiles <<
" test file " << oneName <<
G4endl;
900 testExistence.open(oneName.
data());
901 if(!(testExistence.is_open())) {
902 existAlready =
false;
903 testExistence.clear();
904 testExistence.close();
906 testExistence.clear();
907 testExistence.close();
915 if( existAlready ==
false ) {
917 G4cout <<
"\nAll the necessary images were not found in processed form, starting "
918 <<
"with .dcm images\n";
929 lecturePref = std::fopen(
driverFile.c_str(),
"r");
930 rflag = std::fscanf(lecturePref,
"%s",fCompressionc);
931 fCompression = atoi(fCompressionc);
932 rflag = std::fscanf(lecturePref,
"%s",maxc);
933 fNFiles = atoi(maxc);
938 rflag = std::fscanf(lecturePref,
"%s",inputFile);
939 std::sprintf(name,
"%s.dcm",inputFile);
940 std::cout <<
"check 1: " << name << std::endl;
942 std::printf(
"### Opening %s and reading :\n",name);
943 dicom = std::fopen(name,
"rb");
952 rflag = std::fclose(dicom);
954 rflag = std::fclose(lecturePref);
959 delete [] fCompressionc;
979 unsigned short item_GroupNumber;
980 unsigned short item_ElementNumber;
982 G4int items_array_length=0;
986 while(items_array_length < SQ_Length)
988 rflag = std::fread(buffer, 2, 1, nested);
991 rflag = std::fread(buffer, 2, 1, nested);
992 GetValue(buffer, item_ElementNumber);
994 rflag = std::fread(buffer, 4, 1, nested);
997 rflag = std::fread(buffer, item_Length, 1, nested);
999 items_array_length= items_array_length+8+item_Length;
1004 if( SQ_Length>items_array_length )
1008 if (rflag)
return 1;
1016 unsigned short item_GroupNumber;
1017 unsigned short item_ElementNumber;
1018 unsigned int item_Length;
1024 rflag = std::fread(buffer, 2, 1, nested);
1025 GetValue(buffer, item_GroupNumber);
1027 rflag = std::fread(buffer, 2, 1, nested);
1028 GetValue(buffer, item_ElementNumber);
1030 rflag = std::fread(buffer, 4, 1, nested);
1033 if(item_Length!=0xffffffff)
1034 rflag = std::fread(buffer, item_Length, 1, nested);
1039 }
while(item_GroupNumber!=0xFFFE || item_ElementNumber!=0xE0DD || item_Length!=0);
1050 unsigned short item_GroupNumber;
1051 unsigned short item_ElementNumber;
1052 G4int item_Length;
size_t rflag = 0;
1057 rflag = std::fread(buffer, 2, 1, nested);
1058 GetValue(buffer, item_GroupNumber);
1060 rflag = std::fread(buffer, 2, 1, nested);
1061 GetValue(buffer, item_ElementNumber);
1063 rflag = std::fread(buffer, 4, 1, nested);
1068 rflag = std::fread(buffer,item_Length,1,nested);
1071 while(item_GroupNumber!=0xFFFE || item_ElementNumber!=0xE00D || item_Length!=0);
1079 template <
class Type>
1082 #if BYTE_ORDER == BIG_ENDIAN
1084 #else // BYTE_ORDER == LITTLE_ENDIAN
1087 const int SIZE =
sizeof(_rval);
1089 for(
int i = 0; i < SIZE/2; i++) {
1091 _val[i] = _val[SIZE - 1 - i];
1092 _val[SIZE - 1 - i] = ctemp;
1095 _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