]> Creatis software - gdcm.git/blob - vtk/vtkGdcmWriter.cxx
c9843e32136c2b746bf9962a956b76dfc36fbfc9
[gdcm.git] / vtk / vtkGdcmWriter.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: vtkGdcmWriter.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/12/09 17:08:36 $
7   Version:   $Revision: 1.6 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18                                                                                 
19 #include "gdcmHeader.h"
20 #include "gdcmFile.h"
21 #include "gdcmDebug.h"
22 #include "vtkGdcmWriter.h"
23
24 #include <vtkObjectFactory.h>
25 #include <vtkImageData.h>
26 #include <vtkPointData.h>
27 #include <vtkLookupTable.h>
28
29 vtkCxxRevisionMacro(vtkGdcmWriter, "$Revision: 1.6 $");
30 vtkStandardNewMacro(vtkGdcmWriter);
31
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34 vtkGdcmWriter::vtkGdcmWriter()
35 {
36    this->LookupTable = NULL;
37    this->FileDimensionality = 3;
38 }
39
40 vtkGdcmWriter::~vtkGdcmWriter()
41 {
42 }
43
44 //-----------------------------------------------------------------------------
45 // Print
46 void vtkGdcmWriter::PrintSelf(ostream& os, vtkIndent indent)
47 {
48    this->Superclass::PrintSelf(os,indent);
49 }
50
51 //-----------------------------------------------------------------------------
52 // Public
53
54 //-----------------------------------------------------------------------------
55 // Protected
56 /**
57  * Copy the image and reverse the Y axis
58  */
59 // The output datas must be deleted by the user of the method !!!
60 size_t ReverseData(vtkImageData *image,unsigned char **data)
61 {
62    int inc[3];
63    int *extent = image->GetUpdateExtent();
64    int dim[3] = {extent[1]-extent[0]+1,
65                  extent[3]-extent[2]+1,
66                  extent[5]-extent[4]+1};
67
68    size_t lineSize = dim[0] * image->GetScalarSize()
69                    * image->GetNumberOfScalarComponents();
70    size_t planeSize = dim[1] * lineSize;
71    size_t size = dim[2] * planeSize;
72
73    *data = new unsigned char[size];
74
75    image->GetIncrements(inc);
76    unsigned char *src = (unsigned char *)image->GetScalarPointerForExtent(extent);
77    unsigned char *dst = *data + planeSize - lineSize;
78    for (int plane = extent[4]; plane <= extent[5]; plane++)
79    {
80       for (int line = extent[2]; line <= extent[3]; line++)
81       {
82          // Copy one line at proper destination:
83          memcpy((void*)dst, (void*)src, lineSize);
84
85          src += inc[1]*image->GetScalarSize();
86          dst -= lineSize;
87       }
88       dst += 2 * planeSize;
89    }
90
91    return size;
92 }
93
94 /**
95  * Set the datas informations in the file
96  */
97 void SetImageInformation(gdcm::File *file,vtkImageData *image)
98 {
99    std::ostringstream str;
100
101    // Image size
102    int *extent = image->GetUpdateExtent();
103    int dim[3] = {extent[1]-extent[0]+1,
104                  extent[3]-extent[2]+1,
105                  extent[5]-extent[4]+1};
106
107    str.str("");
108    str << dim[0];
109    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0011);
110
111    str.str("");
112    str << dim[1];
113    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0010);
114
115    if(dim[2]>1)
116    {
117       str.str("");
118       str << dim[2];
119       file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0012);
120    }
121
122    // Pixel type
123    str.str("");
124    str << image->GetScalarSize()*8;
125    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0100);
126    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0101);
127
128    str.str("");
129    str << image->GetScalarSize()*8-1;
130    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0102);
131
132    // Pixel Repr
133    // FIXME : what do we do when the ScalarType is 
134    // VTK_UNSIGNED_INT or VTK_UNSIGNED_LONG
135    str.str("");
136    if( image->GetScalarType() == VTK_UNSIGNED_CHAR ||
137        image->GetScalarType() == VTK_UNSIGNED_SHORT ||
138        image->GetScalarType() == VTK_UNSIGNED_INT ||
139        image->GetScalarType() == VTK_UNSIGNED_LONG )
140    {
141       str << "0"; // Unsigned
142    }
143    else
144    {
145       str << "1"; // Signed
146    }
147    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0103);
148
149    // Samples per pixel
150    str.str("");
151    str << image->GetNumberOfScalarComponents();
152    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0002);
153
154    // Spacing
155    double *sp = image->GetSpacing();
156
157    str.str("");
158    str << sp[0] << "\\" << sp[1];
159    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x0030);
160    str.str("");
161    str << sp[2];
162    file->ReplaceOrCreateByNumber(str.str(),0x0018,0x0088);
163
164    // Origin
165    double *org = image->GetOrigin();
166
167    str.str("");
168    str << org[0] << "\\" << org[1] << "\\" << org[2];
169    file->ReplaceOrCreateByNumber(str.str(),0x0020,0x0032);
170
171    // Window / Level
172    double *rng=image->GetScalarRange();
173
174    str.str("");
175    str << rng[1]-rng[0];
176    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x1051);
177    str.str("");
178    str << (rng[1]+rng[0])/2.0;
179    file->ReplaceOrCreateByNumber(str.str(),0x0028,0x1050);
180
181    // Pixels
182    unsigned char *data;
183    size_t size = ReverseData(image,&data);
184    file->SetImageData(data,size);
185 }
186
187 /**
188  * Write of the files
189  * The call to this method is recursive if there is some files to write
190  */ 
191 void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *image, ofstream *file)
192 {
193    if(file)
194    {
195       vtkErrorMacro( <<  "File musn't be opened");
196       return;
197    }
198
199    if( image->GetScalarType() == VTK_FLOAT || 
200        image->GetScalarType() == VTK_DOUBLE )
201    {
202       vtkErrorMacro(<< "Bad input type. Scalar type musn't be of type "
203                     << "VTK_FLOAT or VTKDOUBLE (found:"
204                     << image->GetScalarTypeAsString());
205       return;
206    }
207
208    RecursiveWrite(axis,image,image,file);
209    //WriteDcmFile(this->FileName,image);
210 }
211
212 void vtkGdcmWriter::RecursiveWrite(int axis, vtkImageData *cache, 
213                                    vtkImageData *image, ofstream *file)
214 {
215    int idx, min, max;
216
217    // if the file is already open then just write to it
218    if( file )
219    {
220       vtkErrorMacro( <<  "File musn't be opened");
221       return;
222    }
223
224    // if we need to open another slice, do it
225    if( (axis + 1) == this->FileDimensionality )
226    {
227       // determine the name
228       if (this->FileName)
229       {
230          sprintf(this->InternalFileName,"%s",this->FileName);
231       }
232       else 
233       {
234          if (this->FilePrefix)
235          {
236             sprintf(this->InternalFileName, this->FilePattern, 
237             this->FilePrefix, this->FileNumber);
238          }
239          else
240          {
241             sprintf(this->InternalFileName, this->FilePattern,this->FileNumber);
242          }
243          if (this->FileNumber < this->MinimumFileNumber)
244          {
245             this->MinimumFileNumber = this->FileNumber;
246          }
247          else if (this->FileNumber > this->MaximumFileNumber)
248          {
249             this->MaximumFileNumber = this->FileNumber;
250          }
251       }
252
253       // Write the file
254       WriteDcmFile(this->InternalFileName,image);
255       ++this->FileNumber;
256       return;
257    }
258
259    // if the current region is too high a dimension forthe file
260    // the we will split the current axis
261    cache->GetAxisUpdateExtent(axis, min, max);
262
263    // if it is the y axis then flip by default
264    if (axis == 1 && !this->FileLowerLeft)
265    {
266       for(idx = max; idx >= min; idx--)
267       {
268          cache->SetAxisUpdateExtent(axis, idx, idx);
269          this->RecursiveWrite(axis - 1, cache, image, file);
270       }
271    }
272    else
273    {
274       for(idx = min; idx <= max; idx++)
275       {
276          cache->SetAxisUpdateExtent(axis, idx, idx);
277          this->RecursiveWrite(axis - 1, cache, image, file);
278       }
279    }
280
281    // restore original extent
282    cache->SetAxisUpdateExtent(axis, min, max);
283 }
284
285 void vtkGdcmWriter::WriteDcmFile(char *fileName,vtkImageData *image)
286 {
287    // From here, the write of the file begins
288    gdcm::File *dcmFile = new gdcm::File();
289
290    // Set the image informations
291    SetImageInformation(dcmFile,image);
292
293    // Write the image
294    if(!dcmFile->Write(fileName))
295    {
296       vtkErrorMacro( << "File "  <<  this->FileName  <<  "couldn't be written by "
297                      << " the gdcm library");
298       std::cerr << "not written \n";
299    }
300
301    delete dcmFile;
302 }
303
304 //-----------------------------------------------------------------------------
305 // Private
306
307 //-----------------------------------------------------------------------------