1 /*=========================================================================
4 Module: $RCSfile: vtkGdcmWriter.cxx,v $
6 Date: $Date: 2006/05/09 10:54:57 $
7 Version: $Revision: 1.32 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
20 #include "gdcmFileHelper.h"
21 #include "gdcmDebug.h"
23 #include "vtkGdcmWriter.h"
25 #include <vtkObjectFactory.h>
26 #include <vtkImageData.h>
27 #include <vtkPointData.h>
28 #include <vtkLookupTable.h>
29 #if (VTK_MAJOR_VERSION >= 5)
30 #include <vtkMedicalImageProperties.h>
32 #ifndef vtkFloatingPointType
33 #define vtkFloatingPointType float
36 vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.32 $")
37 vtkStandardNewMacro(vtkGdcmWriter)
39 vtkCxxSetObjectMacro(vtkGdcmWriter,LookupTable,vtkLookupTable);
40 #if (VTK_MAJOR_VERSION >= 5)
41 vtkCxxSetObjectMacro(vtkGdcmWriter,MedicalImageProperties,vtkMedicalImageProperties);
43 //-----------------------------------------------------------------------------
44 // Constructor / Destructor
45 vtkGdcmWriter::vtkGdcmWriter()
47 this->LookupTable = NULL;
48 this->MedicalImageProperties = NULL;
49 this->FileDimensionality = 3;
50 this->WriteType = VTK_GDCM_WRITE_TYPE_EXPLICIT_VR;
52 this->ContentType = VTK_GDCM_WRITE_TYPE_USER_OWN_IMAGE;
55 vtkGdcmWriter::~vtkGdcmWriter()
57 this->SetMedicalImageProperties(NULL);
58 this->SetLookupTable(NULL);
61 //-----------------------------------------------------------------------------
63 void vtkGdcmWriter::PrintSelf(ostream &os, vtkIndent indent)
65 this->Superclass::PrintSelf(os, indent);
67 os << indent << "Write type : " << this->GetWriteTypeAsString();
70 //-----------------------------------------------------------------------------
72 const char *vtkGdcmWriter::GetWriteTypeAsString()
76 case VTK_GDCM_WRITE_TYPE_EXPLICIT_VR :
78 case VTK_GDCM_WRITE_TYPE_IMPLICIT_VR :
80 case VTK_GDCM_WRITE_TYPE_ACR :
82 case VTK_GDCM_WRITE_TYPE_ACR_LIBIDO :
89 //-----------------------------------------------------------------------------
92 * Copy the image and reverse the Y axis
94 // The output data must be deleted by the user of the method !!!
95 size_t ReverseData(vtkImageData *image,unsigned char **data)
98 int *extent = image->GetUpdateExtent();
99 int dim[3] = {extent[1]-extent[0]+1,
100 extent[3]-extent[2]+1,
101 extent[5]-extent[4]+1};
103 size_t lineSize = dim[0] * image->GetScalarSize()
104 * image->GetNumberOfScalarComponents();
105 size_t planeSize = dim[1] * lineSize;
106 size_t size = dim[2] * planeSize;
110 *data = new unsigned char[size];
112 image->GetIncrements(inc);
113 unsigned char *src = (unsigned char *)image->GetScalarPointerForExtent(extent);
114 unsigned char *dst = *data + planeSize - lineSize;
115 for (int plane = extent[4]; plane <= extent[5]; plane++)
117 for (int line = extent[2]; line <= extent[3]; line++)
119 // Copy one line at proper destination:
120 memcpy((void*)dst, (void*)src, lineSize);
122 src += inc[1] * image->GetScalarSize();
125 dst += 2 * planeSize;
137 * Set the medical informations in the file, based on the user passed
138 * vtkMedicalImageProperties
140 #if (VTK_MAJOR_VERSION >= 5)
141 void SetMedicalImageInformation(gdcm::FileHelper *file, vtkMedicalImageProperties *medprop)
144 // PatientName, PatientID, PatientAge, PatientSex, PatientBirthDate, StudyID
145 std::ostringstream str;
148 if (medprop->GetPatientName())
151 str << medprop->GetPatientName();
152 file->InsertEntryString(str.str(),0x0010,0x0010,"PN"); // PN 1 Patient's Name
155 if (medprop->GetPatientID())
158 str << medprop->GetPatientID();
159 file->InsertEntryString(str.str(),0x0010,0x0020,"LO"); // LO 1 Patient ID
162 if (medprop->GetPatientAge())
165 str << medprop->GetPatientAge();
166 file->InsertEntryString(str.str(),0x0010,0x1010,"AS"); // AS 1 Patient's Age
169 if (medprop->GetPatientSex())
172 str << medprop->GetPatientSex();
173 file->InsertEntryString(str.str(),0x0010,0x0040,"CS"); // CS 1 Patient's Sex
176 if (medprop->GetPatientBirthDate())
179 str << medprop->GetPatientBirthDate();
180 file->InsertEntryString(str.str(),0x0010,0x0030,"DA"); // DA 1 Patient's Birth Date
183 if (medprop->GetStudyID())
186 str << medprop->GetStudyID();
187 file->InsertEntryString(str.str(),0x0020,0x0010,"SH"); // SH 1 Study ID
194 * Set the data informations in the file
196 void SetImageInformation(gdcm::FileHelper *file, vtkImageData *image)
198 std::ostringstream str;
201 int *extent = image->GetUpdateExtent();
202 int dim[3] = {extent[1]-extent[0]+1,
203 extent[3]-extent[2]+1,
204 extent[5]-extent[4]+1};
208 file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
212 file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
218 //file->Insert(str.str(),0x0028,0x0012); // Planes
219 file->InsertEntryString(str.str(),0x0028,0x0008,"US"); // Number of Frames
224 str << image->GetScalarSize()*8;
225 file->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
226 file->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
229 str << image->GetScalarSize()*8-1;
230 file->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
233 // FIXME : what do we do when the ScalarType is
234 // VTK_UNSIGNED_INT or VTK_UNSIGNED_LONG
236 if( image->GetScalarType() == VTK_UNSIGNED_CHAR ||
237 image->GetScalarType() == VTK_UNSIGNED_SHORT ||
238 image->GetScalarType() == VTK_UNSIGNED_INT ||
239 image->GetScalarType() == VTK_UNSIGNED_LONG )
241 str << "0"; // Unsigned
245 str << "1"; // Signed
247 file->InsertEntryString(str.str(),0x0028,0x0103,"US"); // Pixel Representation
251 str << image->GetNumberOfScalarComponents();
252 file->InsertEntryString(str.str(),0x0028,0x0002,"US"); // Samples per Pixel
254 /// \todo : Spacing Between Slices is meaningfull ONLY for CT an MR modality
255 /// We should perform some checkings before forcing the Entry creation
258 vtkFloatingPointType *sp = image->GetSpacing();
261 // We are about to enter floating point value.
262 // By default ostringstream are smart and don't do fixed point
263 // thus forcing to fixed point value
264 str.setf( std::ios::fixed );
265 str << sp[1] << "\\" << sp[0];
266 file->InsertEntryString(str.str(),0x0028,0x0030,"DS"); // Pixel Spacing
269 file->InsertEntryString(str.str(),0x0018,0x0088,"DS"); // Spacing Between Slices
272 vtkFloatingPointType *org = image->GetOrigin();
274 /// \todo : Image Position Patient is meaningfull ONLY for CT an MR modality
275 /// We should perform some checkings before forcing the Entry creation
278 str << org[0] << "\\" << org[1] << "\\" << org[2];
279 file->InsertEntryString(str.str(),0x0020,0x0032,"DS"); // Image Position Patient
280 str.unsetf( std::ios::fixed ); //done with floating point values
283 vtkFloatingPointType *rng = image->GetScalarRange();
286 str << rng[1]-rng[0];
287 file->InsertEntryString(str.str(),0x0028,0x1051,"DS"); // Window Width
289 str << (rng[1]+rng[0])/2.0;
290 file->InsertEntryString(str.str(),0x0028,0x1050,"DS"); // Window Center
294 size_t size = ReverseData(image,&data);
295 file->SetUserData(data,size);
300 * The call to this method is recursive if there is some files to write
302 void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *image,
307 vtkErrorMacro( << "File must not be open");
311 if( image->GetScalarType() == VTK_FLOAT ||
312 image->GetScalarType() == VTK_DOUBLE )
314 vtkErrorMacro(<< "Bad input type. Scalar type must not be of type "
315 << "VTK_FLOAT or VTK_DOUBLE (found:"
316 << image->GetScalarTypeAsString() << ")" );
320 RecursiveWrite(axis,image, image, file);
321 //WriteDcmFile(this->FileName,image);
324 void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *cache,
325 vtkImageData *image, ofstream *file)
329 // if the file is already open then just write to it
332 vtkErrorMacro( << "File musn't be open");
336 // if we need to open another slice, do it
337 if( (axis + 1) == this->FileDimensionality )
339 // determine the name
342 sprintf(this->InternalFileName, "%s", this->FileName);
346 if (this->FilePrefix)
348 sprintf(this->InternalFileName, this->FilePattern,
349 this->FilePrefix, this->FileNumber);
353 sprintf(this->InternalFileName, this->FilePattern,this->FileNumber);
355 // Remove this code in case user is using VTK 4.2...
356 #if !(VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION == 2)
357 if (this->FileNumber < this->MinimumFileNumber)
359 this->MinimumFileNumber = this->FileNumber;
361 else if (this->FileNumber > this->MaximumFileNumber)
363 this->MaximumFileNumber = this->FileNumber;
369 WriteDcmFile(this->InternalFileName,image);
374 // if the current region is too high a dimension for the file
375 // the we will split the current axis
376 cache->GetAxisUpdateExtent(axis, min, max);
378 // if it is the y axis then flip by default
379 if (axis == 1 && !this->FileLowerLeft)
381 for(idx = max; idx >= min; idx--)
383 cache->SetAxisUpdateExtent(axis, idx, idx);
384 this->RecursiveWrite(axis - 1, cache, image, file);
389 for(idx = min; idx <= max; idx++)
391 cache->SetAxisUpdateExtent(axis, idx, idx);
392 this->RecursiveWrite(axis - 1, cache, image, file);
396 // restore original extent
397 cache->SetAxisUpdateExtent(axis, min, max);
400 void vtkGdcmWriter::WriteDcmFile(char *fileName, vtkImageData *image)
402 gdcm::FileHelper *dcmFile;
404 dcmFile = gdcm::FileHelper::New(GdcmFile);
406 dcmFile = gdcm::FileHelper::New();
408 // From here, the write of the file begins
410 // Set the medical informations:
411 #if (VTK_MAJOR_VERSION >= 5)
412 SetMedicalImageInformation(dcmFile, this->MedicalImageProperties);
415 // Set the image informations
416 SetImageInformation(dcmFile, image);
419 switch(this->WriteType)
421 case VTK_GDCM_WRITE_TYPE_EXPLICIT_VR :
422 dcmFile->SetWriteTypeToDcmExplVR();
424 case VTK_GDCM_WRITE_TYPE_IMPLICIT_VR :
425 dcmFile->SetWriteTypeToDcmImplVR();
427 case VTK_GDCM_WRITE_TYPE_ACR :
428 dcmFile->SetWriteTypeToAcr();
430 case VTK_GDCM_WRITE_TYPE_ACR_LIBIDO :
431 dcmFile->SetWriteTypeToAcrLibido();
434 dcmFile->SetWriteTypeToDcmExplVR();
437 dcmFile->SetContentType((gdcm::ImageContentType)ContentType);
439 if(!dcmFile->Write(fileName))
441 vtkErrorMacro( << "File " << this->FileName << "cannot be written by "
442 << " the gdcm library");
445 if( dcmFile->GetUserData() && dcmFile->GetUserDataSize()>0 )
447 delete[] dcmFile->GetUserData();
452 //-----------------------------------------------------------------------------
455 //-----------------------------------------------------------------------------