+ // Anatomical Images.
+ std::cout << " ========= Create Anatomical images" << std::endl;
+
+ strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
+ imageNumber = 0;
+ currentTime = timeStart;
+
+ for (int fr=0; fr<nf; fr++) // Loop on the frames
+ {
+ //std::cout << "Frame nb " << l << std::endl;
+ for( int j=0; j<ny; j++)
+ {
+ int l =0;
+ for (int i=0; i<nx; i++)
+ {
+ //eatwhite(from);
+ char c;
+ for (;;)
+ {
+ if (!from.get(c))
+ break;
+ if (!isspace(c))
+ {
+ from.putback(c);
+ break;
+ }
+ }
+ from >> str1;
+ val = (float)atof(str1.c_str());
+ // std::cout << " " << val;
+ *(f+ /*l*nx*ny + */j*nx+i) = val;
+
+ if(from.eof())
+ {
+ std::cout << "Missing values at [" << std::dec << j <<"," << i << "]"
+ << std::endl;
+ break;
+ }
+ l++;
+ }
+ }
+
+ uint16_t *img = new uint16_t[ny*nx];
+ uint16_t *ptr = img;
+ float *tmp = f /* + l*ny*nx */ ; // start on image nb l.
+ for( int k=0; k<ny*nx; k++)
+ {
+ *ptr = (int16_t)(*tmp);
+ tmp ++;
+ ptr++;
+ }
+ std::ostringstream str;
+ GDCM_NAME_SPACE::File *file;
+ file = GDCM_NAME_SPACE::File::New();
+
+ // Set the image size
+ str.str("");
+ str << nx;
+ file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
+ str.str("");
+ str << ny;
+ file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
+
+ // Set the pixel type
+ // 16; //8, 16, 32
+ file->InsertEntryString("16",0x0028,0x0100,"US"); // Bits Allocated
+ str.str("");
+ str << 16; // may be 12 or 16 if componentSize =16
+ file->InsertEntryString("16",0x0028,0x0101,"US"); // Bits Stored
+
+ file->InsertEntryString("15",0x0028,0x0102,"US"); // High Bit
+
+ // Set the pixel representation // 0/1 1 : signed
+ file->InsertEntryString("0",0x0028,0x0103, "US"); // Pixel Representation
+
+ // Set the samples per pixel // 1:Grey level, 3:RGB
+ file->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
+
+/*
+ // Set Rescale Intercept
+ str.str("");
+ str << div;
+ file->InsertEntryString(str.str(),0x0028,0x1052,"DS");
+
+ // Set Rescale Slope
+ str.str("");
+ str << mini;
+ file->InsertEntryString(str.str(),0x0028,0x1053,"DS");
+*/
+
+// 0020 0037 DS 6 Image Orientation (Patient)
+ file->InsertEntryString("1.0\\0.0\\0.0\\0.0\\1.0\\0.0",0x0020,0x0037, "DS"); //[1\0\0\0\1\0] : Axial
+
+// 0020 0032 DS 3 Image Position (Patient)
+ char charImagePosition[256];
+ sprintf(charImagePosition,"0.0\\0.0\\%f",float(l1%nf));
+
+// 0020 0x1041 DS 1 Slice Location
+ sprintf(charImagePosition,"%f",float(l1%nf));
+ file->InsertEntryString(charImagePosition,0x0020,0x1041, "DS");
+
+//0008 103e LO 1 Series Description
+ file->InsertEntryString(imageName,0x0008,0x103e, "LO");
+
+ file->InsertEntryString(strStudyUID,0x0020,0x000d,"UI");
+ file->InsertEntryString(strSerieUID,0x0020,0x000e,"UI");
+ file->InsertEntryString(patName,0x0010,0x0010, "PN"); // Patient's Name
+
+//0020 0011 "IS" Series Number
+ sprintf(charImagePosition,"%04d",serieNumber+1);
+ file->InsertEntryString(charImagePosition,0x0020,0x0011, "IS");
+
+//0020 0011 "IS" Instance Number
+ sprintf(charImagePosition,"%04d",imageNumber);
+ file->InsertEntryString(charImagePosition,0x0020,0x0013, "IS");
+
+//0018 1060 "DS" Time Trigger
+ sprintf(charImagePosition,"%f",currentTime);
+ file->InsertEntryString(charImagePosition,0x0018,0x1060, "DS");
+ // file->Print();
+
+ GDCM_NAME_SPACE::FileHelper *fh;
+ fh = GDCM_NAME_SPACE::FileHelper::New(file);
+ // cast is just to avoid warnings (*no* conversion)
+ fh->SetImageData((uint8_t *)img,nx*ny*sizeof(uint16_t));
+ fh->SetWriteModeToRaw();
+ fh->SetWriteTypeToDcmExplVR();
+
+ fh->SetWriteTypeToDcmExplVR();
+
+ char numero[10];
+ sprintf(numero, "%02d", l1);
+ std::string fileName = imageName + ".Anatomical." + numero + ".dcm";
+ std::cout << "fileName " << fileName << std::endl;
+
+ if( !fh->Write(fileName))
+ std::cout << "Failed for [" << fileName << "]\n"
+ << " File is unwrittable" << std::endl;
+
+ delete img;
+ currentTime += temporalResolution;
+ imageNumber ++;
+
+ } // end loop on frames
+