1 /*=========================================================================
4 Module: $RCSfile: vtkGdcmWriter.cxx,v $
6 Date: $Date: 2004/12/09 11:31:52 $
7 Version: $Revision: 1.5 $
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 =========================================================================*/
19 #include "gdcmHeader.h"
21 #include "gdcmDebug.h"
22 #include "vtkGdcmWriter.h"
24 #include <vtkObjectFactory.h>
25 #include <vtkImageData.h>
26 #include <vtkPointData.h>
27 #include <vtkLookupTable.h>
29 vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.5 $");
30 vtkStandardNewMacro(vtkGdcmWriter);
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34 vtkGdcmWriter::vtkGdcmWriter()
36 this->LookupTable = NULL;
39 vtkGdcmWriter::~vtkGdcmWriter()
43 //-----------------------------------------------------------------------------
45 void vtkGdcmWriter::PrintSelf(ostream& os, vtkIndent indent)
47 this->Superclass::PrintSelf(os,indent);
50 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
56 * Copy the image and reverse the Y axis
58 // The output datas must be deleted by the user of the method !!!
59 size_t ReverseData(vtkImageData *img,unsigned char **data)
61 int *dim = img->GetDimensions();
62 size_t lineSize = dim[0] * img->GetScalarSize()
63 * img->GetNumberOfScalarComponents();
64 size_t planeSize = dim[1] * lineSize;
65 size_t size = dim[2] * planeSize;
67 *data = new unsigned char[size];
69 unsigned char *src = (unsigned char *)img->GetScalarPointer();
70 unsigned char *dst = *data + planeSize - lineSize;
71 for (int plane = 0; plane < dim[2]; plane++)
73 for (int line = 0; line < dim[1]; line++)
75 // Copy one line at proper destination:
76 memcpy((void*)dst, (void*)src, lineSize);
88 * Set the datas informations in the file
90 void SetImageInformation(gdcm::File *file,vtkImageData *image)
92 std::ostringstream str;
95 int *dim = image->GetDimensions();
99 file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0011);
103 file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0010);
109 file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0012);
114 str << image->GetScalarSize()*8;
115 file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0100);
116 file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0101);
119 str << image->GetScalarSize()*8-1;
120 file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0102);
123 // FIXME : what do we do when the ScalarType is
124 // VTK_UNSIGNED_INT or VTK_UNSIGNED_LONG
126 if( image->GetScalarType() == VTK_UNSIGNED_CHAR ||
127 image->GetScalarType() == VTK_UNSIGNED_SHORT ||
128 image->GetScalarType() == VTK_UNSIGNED_INT ||
129 image->GetScalarType() == VTK_UNSIGNED_LONG )
131 str << "0"; // Unsigned
135 str << "1"; // Signed
137 file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0103);
141 str << image->GetNumberOfScalarComponents();
142 file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0002);
145 double *sp = image->GetSpacing();
148 str << sp[0] << "\\" << sp[1];
149 file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0030);
152 file->ReplaceOrCreateByNumber(str.str(),0x0018,0x0088);
155 double *org = image->GetOrigin();
158 str << org[0] << "\\" << org[1] << "\\" << org[2];
159 file->ReplaceOrCreateByNumber(str.str(),0x0020,0x0032);
162 double *rng=image->GetScalarRange();
165 str << rng[1]-rng[0];
166 file->ReplaceOrCreateByNumber(str.str(),0x0028,0x1051);
168 str << (rng[1]+rng[0])/2.0;
169 file->ReplaceOrCreateByNumber(str.str(),0x0028,0x1050);
173 size_t size = ReverseData(image,&data);
174 file->SetImageData(data,size);
179 * The call to this method is recursive if there is some files to write
181 void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *image, ofstream *file)
183 (void)axis; // To avoid warning
186 vtkErrorMacro( << "File musn't be opened");
190 if( image->GetScalarType() == VTK_FLOAT ||
191 image->GetScalarType() == VTK_DOUBLE )
193 vtkErrorMacro(<< "Bad input type. Scalar type musn't be of type "
194 << "VTK_FLOAT or VTKDOUBLE (found:"
195 << image->GetScalarTypeAsString());
199 WriteDcmFile(this->FileName,image);
202 void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *image,
203 vtkImageData *cache, ofstream *file)
205 (void)axis; // To avoid warning
206 (void)image; // To avoid warning
207 (void)cache; // To avoid warning
208 (void)file; // To avoid warning
211 void vtkGdcmWriter::WriteDcmFile(char *fileName,vtkImageData *image)
213 // From here, the write of the file begins
214 gdcm::File *dcmFile = new gdcm::File();
216 // Set the image informations
217 SetImageInformation(dcmFile,image);
220 if(!dcmFile->Write(fileName))
222 vtkErrorMacro( << "File " << this->FileName << "couldn't be written by "
223 << " the gdcm library");
224 std::cerr << "not written \n";
230 //-----------------------------------------------------------------------------
233 //-----------------------------------------------------------------------------