1 /*=========================================================================
4 Module: $RCSfile: vtkGdcmWriter.cxx,v $
6 Date: $Date: 2005/05/11 14:40:58 $
7 Version: $Revision: 1.22 $
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>
30 #ifndef vtkFloatingPointType
31 #define vtkFloatingPointType float
34 vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.22 $");
35 vtkStandardNewMacro(vtkGdcmWriter);
37 //-----------------------------------------------------------------------------
38 // Constructor / Destructor
39 vtkGdcmWriter::vtkGdcmWriter()
41 this->LookupTable = NULL;
42 this->FileDimensionality = 3;
43 this->WriteType = VTK_GDCM_WRITE_TYPE_EXPLICIT_VR;
46 vtkGdcmWriter::~vtkGdcmWriter()
50 //-----------------------------------------------------------------------------
52 void vtkGdcmWriter::PrintSelf(ostream &os, vtkIndent indent)
54 this->Superclass::PrintSelf(os, indent);
56 os << indent << "Write type : " << this->GetWriteTypeAsString();
59 //-----------------------------------------------------------------------------
61 const char *vtkGdcmWriter::GetWriteTypeAsString()
65 case VTK_GDCM_WRITE_TYPE_EXPLICIT_VR :
67 case VTK_GDCM_WRITE_TYPE_IMPLICIT_VR :
69 case VTK_GDCM_WRITE_TYPE_ACR :
71 case VTK_GDCM_WRITE_TYPE_ACR_LIBIDO :
78 //-----------------------------------------------------------------------------
81 * Copy the image and reverse the Y axis
83 // The output data must be deleted by the user of the method !!!
84 size_t ReverseData(vtkImageData *image,unsigned char **data)
87 int *extent = image->GetUpdateExtent();
88 int dim[3] = {extent[1]-extent[0]+1,
89 extent[3]-extent[2]+1,
90 extent[5]-extent[4]+1};
92 size_t lineSize = dim[0] * image->GetScalarSize()
93 * image->GetNumberOfScalarComponents();
94 size_t planeSize = dim[1] * lineSize;
95 size_t size = dim[2] * planeSize;
99 *data = new unsigned char[size];
101 image->GetIncrements(inc);
102 unsigned char *src = (unsigned char *)image->GetScalarPointerForExtent(extent);
103 unsigned char *dst = *data + planeSize - lineSize;
104 for (int plane = extent[4]; plane <= extent[5]; plane++)
106 for (int line = extent[2]; line <= extent[3]; line++)
108 // Copy one line at proper destination:
109 memcpy((void*)dst, (void*)src, lineSize);
111 src += inc[1] * image->GetScalarSize();
114 dst += 2 * planeSize;
126 * Set the data informations in the file
128 void SetImageInformation(gdcm::FileHelper *file, vtkImageData *image)
130 std::ostringstream str;
133 int *extent = image->GetUpdateExtent();
134 int dim[3] = {extent[1]-extent[0]+1,
135 extent[3]-extent[2]+1,
136 extent[5]-extent[4]+1};
140 file->InsertValEntry(str.str(),0x0028,0x0011); // Columns
144 file->InsertValEntry(str.str(),0x0028,0x0010); // Rows
150 //file->Insert(str.str(),0x0028,0x0012); // Planes
151 file->InsertValEntry(str.str(),0x0028,0x0008); // Number of Frames
156 str << image->GetScalarSize()*8;
157 file->InsertValEntry(str.str(),0x0028,0x0100); // Bits Allocated
158 file->InsertValEntry(str.str(),0x0028,0x0101); // Bits Stored
161 str << image->GetScalarSize()*8-1;
162 file->InsertValEntry(str.str(),0x0028,0x0102); // High Bit
165 // FIXME : what do we do when the ScalarType is
166 // VTK_UNSIGNED_INT or VTK_UNSIGNED_LONG
168 if( image->GetScalarType() == VTK_UNSIGNED_CHAR ||
169 image->GetScalarType() == VTK_UNSIGNED_SHORT ||
170 image->GetScalarType() == VTK_UNSIGNED_INT ||
171 image->GetScalarType() == VTK_UNSIGNED_LONG )
173 str << "0"; // Unsigned
177 str << "1"; // Signed
179 file->InsertValEntry(str.str(),0x0028,0x0103); // Pixel Representation
183 str << image->GetNumberOfScalarComponents();
184 file->InsertValEntry(str.str(),0x0028,0x0002); // Samples per Pixel
186 /// \todo : Spacing Between Slices is meaningfull ONLY for CT an MR modality
187 /// We should perform some checkings before forcing the Entry creation
190 vtkFloatingPointType *sp = image->GetSpacing();
193 // We are about to enter floating point value. By default ostringstream are smart and don't do fixed point
194 // thus forcing to fixed point value
195 str.setf( std::ios::fixed );
196 str << sp[1] << "\\" << sp[0];
197 file->InsertValEntry(str.str(),0x0028,0x0030); // Pixel Spacing
200 file->InsertValEntry(str.str(),0x0018,0x0088); // Spacing Between Slices
203 vtkFloatingPointType *org = image->GetOrigin();
205 /// \todo : Image Position Patient is meaningfull ONLY for CT an MR modality
206 /// We should perform some checkings before forcing the Entry creation
209 str << org[0] << "\\" << org[1] << "\\" << org[2];
210 file->InsertValEntry(str.str(),0x0020,0x0032); // Image Position Patient
211 str.unsetf( std::ios::fixed ); //done with floating point values
214 vtkFloatingPointType *rng = image->GetScalarRange();
217 str << rng[1]-rng[0];
218 file->InsertValEntry(str.str(),0x0028,0x1051); // Window Width
220 str << (rng[1]+rng[0])/2.0;
221 file->InsertValEntry(str.str(),0x0028,0x1050); // Window Center
225 size_t size = ReverseData(image,&data);
226 file->SetUserData(data,size);
231 * The call to this method is recursive if there is some files to write
233 void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *image,
238 vtkErrorMacro( << "File must not be open");
242 if( image->GetScalarType() == VTK_FLOAT ||
243 image->GetScalarType() == VTK_DOUBLE )
245 vtkErrorMacro(<< "Bad input type. Scalar type must not be of type "
246 << "VTK_FLOAT or VTKDOUBLE (found:"
247 << image->GetScalarTypeAsString());
251 RecursiveWrite(axis,image, image, file);
252 //WriteDcmFile(this->FileName,image);
255 void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *cache,
256 vtkImageData *image, ofstream *file)
260 // if the file is already open then just write to it
263 vtkErrorMacro( << "File musn't be open");
267 // if we need to open another slice, do it
268 if( (axis + 1) == this->FileDimensionality )
270 // determine the name
273 sprintf(this->InternalFileName, "%s", this->FileName);
277 if (this->FilePrefix)
279 sprintf(this->InternalFileName, this->FilePattern,
280 this->FilePrefix, this->FileNumber);
284 sprintf(this->InternalFileName, this->FilePattern,this->FileNumber);
286 // Remove this code in case user is using VTK 4.2...
287 #if !(VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION == 2)
288 if (this->FileNumber < this->MinimumFileNumber)
290 this->MinimumFileNumber = this->FileNumber;
292 else if (this->FileNumber > this->MaximumFileNumber)
294 this->MaximumFileNumber = this->FileNumber;
300 WriteDcmFile(this->InternalFileName,image);
305 // if the current region is too high a dimension for the file
306 // the we will split the current axis
307 cache->GetAxisUpdateExtent(axis, min, max);
309 // if it is the y axis then flip by default
310 if (axis == 1 && !this->FileLowerLeft)
312 for(idx = max; idx >= min; idx--)
314 cache->SetAxisUpdateExtent(axis, idx, idx);
315 this->RecursiveWrite(axis - 1, cache, image, file);
320 for(idx = min; idx <= max; idx++)
322 cache->SetAxisUpdateExtent(axis, idx, idx);
323 this->RecursiveWrite(axis - 1, cache, image, file);
327 // restore original extent
328 cache->SetAxisUpdateExtent(axis, min, max);
331 void vtkGdcmWriter::WriteDcmFile(char *fileName, vtkImageData *image)
333 // From here, the write of the file begins
334 gdcm::FileHelper *dcmFile = new gdcm::FileHelper();
336 // Set the image informations
337 SetImageInformation(dcmFile, image);
340 switch(this->WriteType)
342 case VTK_GDCM_WRITE_TYPE_EXPLICIT_VR :
343 dcmFile->SetWriteTypeToDcmExplVR();
345 case VTK_GDCM_WRITE_TYPE_IMPLICIT_VR :
346 dcmFile->SetWriteTypeToDcmImplVR();
348 case VTK_GDCM_WRITE_TYPE_ACR :
349 dcmFile->SetWriteTypeToAcr();
351 case VTK_GDCM_WRITE_TYPE_ACR_LIBIDO :
352 dcmFile->SetWriteTypeToAcrLibido();
355 dcmFile->SetWriteTypeToDcmExplVR();
358 if(!dcmFile->Write(fileName))
360 vtkErrorMacro( << "File " << this->FileName << "cannot be written by "
361 << " the gdcm library");
365 if( dcmFile->GetUserData() && dcmFile->GetUserDataSize()>0 )
367 delete[] dcmFile->GetUserData();
372 //-----------------------------------------------------------------------------
375 //-----------------------------------------------------------------------------