]> Creatis software - gdcm.git/blob - vtk/vtkgdcmSerieViewer.cxx
vtkgdcmSerieViewer shows an example for using user supplied function.
[gdcm.git] / vtk / vtkgdcmSerieViewer.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: vtkgdcmSerieViewer.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/30 18:37:48 $
7   Version:   $Revision: 1.4 $
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 // This example illustrates how the vtkGdcmReader vtk class can be
19 // used in order to:
20 //  * produce a simple (vtk based) Dicom image STACK VIEWER.
21 //  * dump the stack considered as a volume in a vtkStructuredPoints
22 //    vtk file: the vtk gdcm wrappers can be seen as a simple way to convert
23 //    a stack of Dicom images into a native vtk volume.
24 //
25 // Usage:
26 //  * the Directory name that contains the Dicom images constituting the stack 
27 //    should be given as command line argument,
28 //  * you can navigate through the stack by hitting any character key,
29 //  * the produced vtk file is named "foo.vtk" (in the invocation directory).
30 // 
31 //----------------------------------------------------------------------------
32 #include <vtkRenderWindowInteractor.h>
33 #include <vtkImageViewer.h>
34 #include <vtkStructuredPoints.h>
35 #include <vtkStructuredPointsWriter.h>
36 #include <vtkCommand.h>
37 #include <vtkRenderer.h>
38 #include <vtkImageMapToColors.h>
39 #include <vtkLookupTable.h>
40
41 #include "vtkGdcmReader.h"
42 #include "gdcmDocument.h"  // for NO_SHADOWSEQ
43 #include "gdcmSerieHelper.h"
44 #include "gdcmDebug.h"
45 #include "gdcmArgMgr.h" // for Argument Manager functions
46 #ifndef vtkFloatingPointType
47 #define vtkFloatingPointType float
48 #endif
49
50 void userSuppliedMirrorFunction (uint8_t *im, gdcm::File *f);
51 void userSuppliedTopDownFunction(uint8_t *im, gdcm::File *f);
52
53 //----------------------------------------------------------------------------
54 // Callback for the interaction
55 class vtkgdcmObserver : public vtkCommand
56 {
57 public:
58    virtual char const *GetClassName() const 
59    { 
60       return "vtkgdcmObserver";
61    }
62
63    static vtkgdcmObserver *New() 
64    { 
65       return new vtkgdcmObserver; 
66    }
67
68    vtkgdcmObserver()
69    {
70       this->ImageViewer = NULL;
71    }
72
73    virtual void Execute(vtkObject *, unsigned long event, void* )
74    {
75       if ( this->ImageViewer )
76       {
77          if ( event == vtkCommand::CharEvent )
78          {
79             int max = ImageViewer->GetWholeZMax();
80             int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
81             ImageViewer->SetZSlice( slice );
82             ImageViewer->GetRenderer()->ResetCameraClippingRange();
83             ImageViewer->Render();
84          }
85       }
86    }
87    vtkImageViewer *ImageViewer;
88 };
89
90 int main(int argc, char *argv[])
91 {
92    START_USAGE(usage)
93    " \n vtkgdcmSerieViewer : \n",
94    " Display a Serie within a Directory                                       ",
95    " You can navigate through the stack by hitting any character key.         ",
96    " usage: vtkgdcmSerieViewer filein=fileName [noshadowseq][noshadow][noseq] ",
97    "                           [reverse] [{[mirror]|[topdown]|[rotate]}]      ",
98    "                           [check][debug]                                 ",
99    "      noshadowseq: user doesn't want to load Private Sequences            ",
100    "      noshadow   : user doesn't want to load Private groups (odd number)  ",
101    "      noseq      : user doesn't want to load Sequences                    ",
102    "      mirror     : user wants to 'mirror' the images | just some simple   ",
103    "      topdown    : user wants to 'topdown' the images| examples of user   ",
104    "      rotate     : user wants NOT YET MADE           | supplied functions ",
105    "      debug      : user wants to run the program in 'debug mode'          ",
106    FINISH_USAGE
107
108
109    // Initialize Arguments Manager   
110    gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
111   
112    if (argc == 1 || am->ArgMgrDefined("usage") )
113    {
114       am->ArgMgrUsage(usage); // Display 'usage'
115       delete am;
116       return 0;
117    }
118
119    char *dirName = am->ArgMgrWantString("dirname",usage);
120
121    int loadMode = 0x00000000;
122    if ( am->ArgMgrDefined("noshadowseq") )
123       loadMode |= NO_SHADOWSEQ;
124    else 
125    {
126       if ( am->ArgMgrDefined("noshadow") )
127          loadMode |= NO_SHADOW;
128       if ( am->ArgMgrDefined("noseq") )
129          loadMode |= NO_SEQ;
130    }
131
132    bool reverse = am->ArgMgrDefined("reverse");
133
134    bool mirror  = am->ArgMgrDefined("mirror");
135    bool topdown = am->ArgMgrDefined("topdown");
136    bool rotate  = am->ArgMgrDefined("rotate");
137
138   bool check   = am->ArgMgrDefined("check");
139
140    if ( (int)mirror + (int)topdown + (int)rotate > 1)
141    {
142       std::cout << "mirror *OR* topDown *OR* rotate !"
143                 << std::endl;
144       delete am;
145       return 0;
146    }
147
148    if (am->ArgMgrDefined("debug"))
149       gdcm::Debug::DebugOn();
150
151    /* if unused Param we give up */
152    if ( am->ArgMgrPrintUnusedLabels() )
153    {
154       am->ArgMgrUsage(usage);
155       delete am;
156       return 0;
157    } 
158
159    delete am;  // we don't need Argument Manager any longer
160
161    // ----------------------- End Arguments Manager ----------------------
162   
163    // ------------ to check Coherent File List as a parameter
164
165    gdcm::SerieHelper *sh = new gdcm::SerieHelper();
166    sh->SetLoadMode(loadMode);
167    if (reverse)
168       sh->SetSortOrderToReverse();
169    sh->SetDirectory( dirName, true);
170     
171    // Just to see
172
173    int nbFiles;
174    // For all the Coherent Files lists of the gdcm::Serie
175    gdcm::FileList *l = sh->GetFirstCoherentFileList();
176    if (l == 0 )
177    {
178       std::cout << "Oops! No CoherentFileList found ?!?" << std::endl;
179       return 0;
180    }
181    while (l)
182    { 
183       nbFiles = l->size() ;
184       if ( l->size() > 1 )
185       {
186          std::cout << "Sort list : " << nbFiles << " long" << std::endl;
187          sh->OrderFileList(l);  // sort the list
188          break;  // The first one is OK. user will have to check
189       }
190       else
191       {
192          std::cout << "Oops! Empty CoherentFileList found ?!?" << std::endl;
193       }
194       l = sh->GetNextCoherentFileList();
195    }
196
197    if (check)
198    {
199       if ( !sh->IsCoherent(l) ) // just be sure (?)
200       {
201          std::cout << "Files are not coherent. Stop everything " << std::endl;
202          delete sh;
203          return 0;
204       }
205    }
206
207    vtkGdcmReader *reader = vtkGdcmReader::New();
208    reader->AllowLookupTableOff();
209
210    if (mirror)
211       reader->SetUserFunction (userSuppliedMirrorFunction);
212    else if (topdown)
213       reader->SetUserFunction (userSuppliedTopDownFunction);
214
215    // Only the first FileList is dealt with (just an example)
216    // (The files will not be parsed twice by the reader)
217
218    //---------------------------------------------------------
219    reader->SetCoherentFileList(l);
220    //---------------------------------------------------------
221
222    // because we passed a Coherent File List from a SerieHelper,
223    // setting LoadMode is useless in this case
224    //  reader->SetLoadMode(NO_SHADOWSEQ);  
225    reader->Update();
226
227    //print debug info:
228    reader->GetOutput()->Print( cout );
229
230    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
231
232    vtkImageViewer *viewer = vtkImageViewer::New();
233
234    if( reader->GetLookupTable() )
235    {
236       //convert to color:
237       vtkImageMapToColors *map = vtkImageMapToColors::New ();
238       map->SetInput (reader->GetOutput());
239       map->SetLookupTable (reader->GetLookupTable());
240       map->SetOutputFormatToRGB();
241       viewer->SetInput ( map->GetOutput() );
242       map->Delete();
243    }
244    else
245    {
246       vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
247       viewer->SetColorLevel (0.5 * (range[1] + range[0]));
248       viewer->SetColorWindow (range[1] - range[0]);
249
250       viewer->SetInput ( reader->GetOutput() );
251    }
252    viewer->SetupInteractor (iren);
253   
254    //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
255    //viewer->SetColorWindow (range[1] - range[0]);
256    //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
257
258    // Here is where we setup the observer, 
259    vtkgdcmObserver *obs = vtkgdcmObserver::New();
260    obs->ImageViewer = viewer;
261    iren->AddObserver(vtkCommand::CharEvent,obs);
262    obs->Delete();
263
264    //viewer->Render();
265    iren->Initialize();
266    iren->Start();
267
268    //if you wish you can export dicom to a vtk file  
269    vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
270    writer->SetInput( reader->GetOutput());
271    writer->SetFileName( "foo.vtk" );
272    writer->SetFileTypeToBinary();
273    //writer->Write();
274
275    reader->Delete();
276    iren->Delete();
277    viewer->Delete();
278    writer->Delete();
279
280    return 0;
281 }
282
283
284 // --------------------------------------------------------
285 // This is just a *very* simple example of user supplied function
286 //      to mirror (why not ?) the image
287 // It's *not* part of gdcm.
288 // --------------------------------------------------------
289
290 #define UF(ty)                          \
291    int i, j;                            \
292    ty *imj;                             \
293    ty tamp;                             \
294    for (j=0;j<ny;j++)                   \
295    {                                    \
296       imj = (ty *)im +j*nx;             \
297       for (i=0;i<nx/2;i++)              \
298       {                                 \
299         tamp       =imj[i];             \
300         imj[i]     =imj[nx-1-i];        \
301         imj[nx-1-i]=tamp;               \
302       }                                 \
303    }                                    \
304    if (nx%2 != 0)                       \
305    {                                    \
306       for (j=0;j<ny;j++)                \
307       {                                 \
308         imj = (ty *)im  +j*nx;          \
309         tamp       =imj[i];             \
310         imj[i]     =imj[nx/2+1];        \
311         imj[nx/2+1]=tamp;               \
312       }                                 \
313    }
314
315 void userSuppliedMirrorFunction(uint8_t *im, gdcm::File *f)
316 {
317    if (f->GetZSize() != 1)
318    {
319       std::cout << "mirror : Multiframe images not yet dealt with" << std::endl;
320       return;
321    }
322
323    if (f->GetSamplesPerPixel() != 1 || f->GetBitsAllocated() == 24)
324    {
325       std::cout << "mirror : RGB / YBR not yet dealt with" << std::endl;
326       return;
327    }
328    int nx = f->GetXSize();
329    int ny = f->GetYSize();
330
331    std::string pixelType = f->GetPixelType();
332    if ( pixelType ==  "8U" || pixelType == "8S" )
333    {
334       UF(uint8_t)
335       return;
336    }
337    if ( pixelType == "16U" || pixelType == "16S")
338    {
339       UF(uint16_t)
340       return;
341    }
342    std::cout << "mirror : Pixel Size (!=8, !=16) not yet dealt with" 
343              << std::endl;
344    return;
345 }
346
347
348 // --------------------------------------------------------
349 // This is just a *very* simple example of user supplied function
350 //      to topdown (why not ?) the image
351 // It's *not* part of gdcm.
352 // --------------------------------------------------------
353
354 #define UF2(ty)                         \
355    int i, j;                            \
356    ty *imj, *imJ;                       \
357    ty tamp;                             \
358    for (j=0;j<ny/2;j++)                 \
359    {                                    \
360       imj = (ty *)im +j*nx;             \
361       imJ = (ty *)im +(ny-1-j)*nx;      \
362       for (i=0;i<nx;i++)                \
363       {                                 \
364         tamp       =imj[i];             \
365         imj[i]     =imJ[i];             \
366         imJ[i]     =tamp;               \
367       }                                 \
368    }
369
370 void userSuppliedTopDownFunction(uint8_t *im, gdcm::File *f)
371 {
372    if (f->GetZSize() != 1)
373    {
374       std::cout << "mirror : Multiframe images not yet dealt with" << std::endl;
375       return;
376    }
377
378    if (f->GetSamplesPerPixel() != 1 || f->GetBitsAllocated() == 24)
379    {
380       std::cout << "mirror : RGB / YBR not yet dealt with" << std::endl;
381       return;
382    }
383    int nx = f->GetXSize();
384    int ny = f->GetYSize();
385
386    std::string pixelType = f->GetPixelType();
387    if ( pixelType ==  "8U" || pixelType == "8S" )
388    {
389       UF2(uint8_t)
390       return;
391    }
392    if ( pixelType == "16U" || pixelType == "16S")
393    {
394       UF2(uint16_t)
395       return;
396    }
397    std::cout << "topdown : Pixel Size (!=8, !=16) not yet dealt with" 
398              << std::endl;
399    return;
400 }
401
402
403
404