1 /*=========================================================================
4 Module: $RCSfile: vtkGdcmWriter.cxx,v $
6 Date: $Date: 2005/10/25 14:52:37 $
7 Version: $Revision: 1.26 $
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.26 $")
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->InsertEntryString(str.str(),0x0028,0x0011); // Columns
144 file->InsertEntryString(str.str(),0x0028,0x0010); // Rows
150 //file->Insert(str.str(),0x0028,0x0012); // Planes
151 file->InsertEntryString(str.str(),0x0028,0x0008); // Number of Frames
156 str << image->GetScalarSize()*8;
157 file->InsertEntryString(str.str(),0x0028,0x0100); // Bits Allocated
158 file->InsertEntryString(str.str(),0x0028,0x0101); // Bits Stored
161 str << image->GetScalarSize()*8-1;
162 file->InsertEntryString(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->InsertEntryString(str.str(),0x0028,0x0103); // Pixel Representation
183 str << image->GetNumberOfScalarComponents();
184 file->InsertEntryString(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.
194 // By default ostringstream are smart and don't do fixed point
195 // thus forcing to fixed point value
196 str.setf( std::ios::fixed );
197 str << sp[1] << "\\" << sp[0];
198 file->InsertEntryString(str.str(),0x0028,0x0030); // Pixel Spacing
201 file->InsertEntryString(str.str(),0x0018,0x0088); // Spacing Between Slices
204 vtkFloatingPointType *org = image->GetOrigin();
206 /// \todo : Image Position Patient is meaningfull ONLY for CT an MR modality
207 /// We should perform some checkings before forcing the Entry creation
210 str << org[0] << "\\" << org[1] << "\\" << org[2];
211 file->InsertEntryString(str.str(),0x0020,0x0032); // Image Position Patient
212 str.unsetf( std::ios::fixed ); //done with floating point values
215 vtkFloatingPointType *rng = image->GetScalarRange();
218 str << rng[1]-rng[0];
219 file->InsertEntryString(str.str(),0x0028,0x1051); // Window Width
221 str << (rng[1]+rng[0])/2.0;
222 file->InsertEntryString(str.str(),0x0028,0x1050); // Window Center
226 size_t size = ReverseData(image,&data);
227 file->SetUserData(data,size);
232 * The call to this method is recursive if there is some files to write
234 void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *image,
239 vtkErrorMacro( << "File must not be open");
243 if( image->GetScalarType() == VTK_FLOAT ||
244 image->GetScalarType() == VTK_DOUBLE )
246 vtkErrorMacro(<< "Bad input type. Scalar type must not be of type "
247 << "VTK_FLOAT or VTKDOUBLE (found:"
248 << image->GetScalarTypeAsString());
252 RecursiveWrite(axis,image, image, file);
253 //WriteDcmFile(this->FileName,image);
256 void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *cache,
257 vtkImageData *image, ofstream *file)
261 // if the file is already open then just write to it
264 vtkErrorMacro( << "File musn't be open");
268 // if we need to open another slice, do it
269 if( (axis + 1) == this->FileDimensionality )
271 // determine the name
274 sprintf(this->InternalFileName, "%s", this->FileName);
278 if (this->FilePrefix)
280 sprintf(this->InternalFileName, this->FilePattern,
281 this->FilePrefix, this->FileNumber);
285 sprintf(this->InternalFileName, this->FilePattern,this->FileNumber);
287 // Remove this code in case user is using VTK 4.2...
288 #if !(VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION == 2)
289 if (this->FileNumber < this->MinimumFileNumber)
291 this->MinimumFileNumber = this->FileNumber;
293 else if (this->FileNumber > this->MaximumFileNumber)
295 this->MaximumFileNumber = this->FileNumber;
301 WriteDcmFile(this->InternalFileName,image);
306 // if the current region is too high a dimension for the file
307 // the we will split the current axis
308 cache->GetAxisUpdateExtent(axis, min, max);
310 // if it is the y axis then flip by default
311 if (axis == 1 && !this->FileLowerLeft)
313 for(idx = max; idx >= min; idx--)
315 cache->SetAxisUpdateExtent(axis, idx, idx);
316 this->RecursiveWrite(axis - 1, cache, image, file);
321 for(idx = min; idx <= max; idx++)
323 cache->SetAxisUpdateExtent(axis, idx, idx);
324 this->RecursiveWrite(axis - 1, cache, image, file);
328 // restore original extent
329 cache->SetAxisUpdateExtent(axis, min, max);
332 void vtkGdcmWriter::WriteDcmFile(char *fileName, vtkImageData *image)
334 // From here, the write of the file begins
335 gdcm::FileHelper *dcmFile = gdcm::FileHelper::New();
337 // Set the image informations
338 SetImageInformation(dcmFile, image);
341 switch(this->WriteType)
343 case VTK_GDCM_WRITE_TYPE_EXPLICIT_VR :
344 dcmFile->SetWriteTypeToDcmExplVR();
346 case VTK_GDCM_WRITE_TYPE_IMPLICIT_VR :
347 dcmFile->SetWriteTypeToDcmImplVR();
349 case VTK_GDCM_WRITE_TYPE_ACR :
350 dcmFile->SetWriteTypeToAcr();
352 case VTK_GDCM_WRITE_TYPE_ACR_LIBIDO :
353 dcmFile->SetWriteTypeToAcrLibido();
356 dcmFile->SetWriteTypeToDcmExplVR();
359 if(!dcmFile->Write(fileName))
361 vtkErrorMacro( << "File " << this->FileName << "cannot be written by "
362 << " the gdcm library");
366 if( dcmFile->GetUserData() && dcmFile->GetUserDataSize()>0 )
368 delete[] dcmFile->GetUserData();
373 //-----------------------------------------------------------------------------
376 //-----------------------------------------------------------------------------