1 /*=========================================================================
4 Module: $RCSfile: vtkGdcmWriter.cxx,v $
6 Date: $Date: 2005/04/11 17:01:16 $
7 Version: $Revision: 1.21 $
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 vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.21 $");
31 vtkStandardNewMacro(vtkGdcmWriter);
33 //-----------------------------------------------------------------------------
34 // Constructor / Destructor
35 vtkGdcmWriter::vtkGdcmWriter()
37 this->LookupTable = NULL;
38 this->FileDimensionality = 3;
39 this->WriteType = VTK_GDCM_WRITE_TYPE_EXPLICIT_VR;
42 vtkGdcmWriter::~vtkGdcmWriter()
46 //-----------------------------------------------------------------------------
48 void vtkGdcmWriter::PrintSelf(ostream &os, vtkIndent indent)
50 this->Superclass::PrintSelf(os, indent);
52 os << indent << "Write type : " << this->GetWriteTypeAsString();
55 //-----------------------------------------------------------------------------
57 const char *vtkGdcmWriter::GetWriteTypeAsString()
61 case VTK_GDCM_WRITE_TYPE_EXPLICIT_VR :
63 case VTK_GDCM_WRITE_TYPE_IMPLICIT_VR :
65 case VTK_GDCM_WRITE_TYPE_ACR :
67 case VTK_GDCM_WRITE_TYPE_ACR_LIBIDO :
74 //-----------------------------------------------------------------------------
77 * Copy the image and reverse the Y axis
79 // The output data must be deleted by the user of the method !!!
80 size_t ReverseData(vtkImageData *image,unsigned char **data)
83 int *extent = image->GetUpdateExtent();
84 int dim[3] = {extent[1]-extent[0]+1,
85 extent[3]-extent[2]+1,
86 extent[5]-extent[4]+1};
88 size_t lineSize = dim[0] * image->GetScalarSize()
89 * image->GetNumberOfScalarComponents();
90 size_t planeSize = dim[1] * lineSize;
91 size_t size = dim[2] * planeSize;
95 *data = new unsigned char[size];
97 image->GetIncrements(inc);
98 unsigned char *src = (unsigned char *)image->GetScalarPointerForExtent(extent);
99 unsigned char *dst = *data + planeSize - lineSize;
100 for (int plane = extent[4]; plane <= extent[5]; plane++)
102 for (int line = extent[2]; line <= extent[3]; line++)
104 // Copy one line at proper destination:
105 memcpy((void*)dst, (void*)src, lineSize);
107 src += inc[1] * image->GetScalarSize();
110 dst += 2 * planeSize;
122 * Set the data informations in the file
124 void SetImageInformation(gdcm::FileHelper *file, vtkImageData *image)
126 std::ostringstream str;
129 int *extent = image->GetUpdateExtent();
130 int dim[3] = {extent[1]-extent[0]+1,
131 extent[3]-extent[2]+1,
132 extent[5]-extent[4]+1};
136 file->InsertValEntry(str.str(),0x0028,0x0011); // Columns
140 file->InsertValEntry(str.str(),0x0028,0x0010); // Rows
146 //file->Insert(str.str(),0x0028,0x0012); // Planes
147 file->InsertValEntry(str.str(),0x0028,0x0008); // Number of Frames
152 str << image->GetScalarSize()*8;
153 file->InsertValEntry(str.str(),0x0028,0x0100); // Bits Allocated
154 file->InsertValEntry(str.str(),0x0028,0x0101); // Bits Stored
157 str << image->GetScalarSize()*8-1;
158 file->InsertValEntry(str.str(),0x0028,0x0102); // High Bit
161 // FIXME : what do we do when the ScalarType is
162 // VTK_UNSIGNED_INT or VTK_UNSIGNED_LONG
164 if( image->GetScalarType() == VTK_UNSIGNED_CHAR ||
165 image->GetScalarType() == VTK_UNSIGNED_SHORT ||
166 image->GetScalarType() == VTK_UNSIGNED_INT ||
167 image->GetScalarType() == VTK_UNSIGNED_LONG )
169 str << "0"; // Unsigned
173 str << "1"; // Signed
175 file->InsertValEntry(str.str(),0x0028,0x0103); // Pixel Representation
179 str << image->GetNumberOfScalarComponents();
180 file->InsertValEntry(str.str(),0x0028,0x0002); // Samples per Pixel
182 /// \todo : Spacing Between Slices is meaningfull ONLY for CT an MR modality
183 /// We should perform some checkings before forcing the Entry creation
186 double *sp = image->GetSpacing();
189 // We are about to enter floating point value. By default ostringstream are smart and don't do fixed point
190 // thus forcing to fixed point value
191 str.setf( std::ios::fixed );
192 str << sp[1] << "\\" << sp[0];
193 file->InsertValEntry(str.str(),0x0028,0x0030); // Pixel Spacing
196 file->InsertValEntry(str.str(),0x0018,0x0088); // Spacing Between Slices
199 double *org = image->GetOrigin();
201 /// \todo : Image Position Patient is meaningfull ONLY for CT an MR modality
202 /// We should perform some checkings before forcing the Entry creation
205 str << org[0] << "\\" << org[1] << "\\" << org[2];
206 file->InsertValEntry(str.str(),0x0020,0x0032); // Image Position Patient
207 str.unsetf( std::ios::fixed ); //done with floating point values
210 double *rng=image->GetScalarRange();
213 str << rng[1]-rng[0];
214 file->InsertValEntry(str.str(),0x0028,0x1051); // Window Width
216 str << (rng[1]+rng[0])/2.0;
217 file->InsertValEntry(str.str(),0x0028,0x1050); // Window Center
221 size_t size = ReverseData(image,&data);
222 file->SetUserData(data,size);
227 * The call to this method is recursive if there is some files to write
229 void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *image,
234 vtkErrorMacro( << "File must not be open");
238 if( image->GetScalarType() == VTK_FLOAT ||
239 image->GetScalarType() == VTK_DOUBLE )
241 vtkErrorMacro(<< "Bad input type. Scalar type must not be of type "
242 << "VTK_FLOAT or VTKDOUBLE (found:"
243 << image->GetScalarTypeAsString());
247 RecursiveWrite(axis,image, image, file);
248 //WriteDcmFile(this->FileName,image);
251 void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *cache,
252 vtkImageData *image, ofstream *file)
256 // if the file is already open then just write to it
259 vtkErrorMacro( << "File musn't be open");
263 // if we need to open another slice, do it
264 if( (axis + 1) == this->FileDimensionality )
266 // determine the name
269 sprintf(this->InternalFileName, "%s", this->FileName);
273 if (this->FilePrefix)
275 sprintf(this->InternalFileName, this->FilePattern,
276 this->FilePrefix, this->FileNumber);
280 sprintf(this->InternalFileName, this->FilePattern,this->FileNumber);
282 if (this->FileNumber < this->MinimumFileNumber)
284 this->MinimumFileNumber = this->FileNumber;
286 else if (this->FileNumber > this->MaximumFileNumber)
288 this->MaximumFileNumber = this->FileNumber;
293 WriteDcmFile(this->InternalFileName,image);
298 // if the current region is too high a dimension for the file
299 // the we will split the current axis
300 cache->GetAxisUpdateExtent(axis, min, max);
302 // if it is the y axis then flip by default
303 if (axis == 1 && !this->FileLowerLeft)
305 for(idx = max; idx >= min; idx--)
307 cache->SetAxisUpdateExtent(axis, idx, idx);
308 this->RecursiveWrite(axis - 1, cache, image, file);
313 for(idx = min; idx <= max; idx++)
315 cache->SetAxisUpdateExtent(axis, idx, idx);
316 this->RecursiveWrite(axis - 1, cache, image, file);
320 // restore original extent
321 cache->SetAxisUpdateExtent(axis, min, max);
324 void vtkGdcmWriter::WriteDcmFile(char *fileName, vtkImageData *image)
326 // From here, the write of the file begins
327 gdcm::FileHelper *dcmFile = new gdcm::FileHelper();
329 // Set the image informations
330 SetImageInformation(dcmFile, image);
333 switch(this->WriteType)
335 case VTK_GDCM_WRITE_TYPE_EXPLICIT_VR :
336 dcmFile->SetWriteTypeToDcmExplVR();
338 case VTK_GDCM_WRITE_TYPE_IMPLICIT_VR :
339 dcmFile->SetWriteTypeToDcmImplVR();
341 case VTK_GDCM_WRITE_TYPE_ACR :
342 dcmFile->SetWriteTypeToAcr();
344 case VTK_GDCM_WRITE_TYPE_ACR_LIBIDO :
345 dcmFile->SetWriteTypeToAcrLibido();
348 dcmFile->SetWriteTypeToDcmExplVR();
351 if(!dcmFile->Write(fileName))
353 vtkErrorMacro( << "File " << this->FileName << "cannot be written by "
354 << " the gdcm library");
358 if( dcmFile->GetUserData() && dcmFile->GetUserDataSize()>0 )
360 delete[] dcmFile->GetUserData();
365 //-----------------------------------------------------------------------------
368 //-----------------------------------------------------------------------------