1 /*=========================================================================
4 Module: $RCSfile: vtkgdcmSerieViewer.cxx,v $
6 Date: $Date: 2005/08/22 12:21:03 $
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 =========================================================================*/
18 // This example illustrates how the vtkGdcmReader vtk class can be
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.
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).
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>
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
50 void userSuppliedMirrorFunction (uint8_t *im, gdcm::File *f);
51 void userSuppliedTopDownFunction(uint8_t *im, gdcm::File *f);
53 //----------------------------------------------------------------------------
54 // Callback for the interaction
55 class vtkgdcmObserver : public vtkCommand
58 virtual char const *GetClassName() const
60 return "vtkgdcmObserver";
63 static vtkgdcmObserver *New()
65 return new vtkgdcmObserver;
70 this->ImageViewer = NULL;
73 virtual void Execute(vtkObject *, unsigned long event, void* )
75 if ( this->ImageViewer )
77 if ( event == vtkCommand::CharEvent )
79 int max = ImageViewer->GetWholeZMax();
80 int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
81 ImageViewer->SetZSlice( slice );
82 ImageViewer->GetRenderer()->ResetCameraClippingRange();
83 ImageViewer->Render();
87 vtkImageViewer *ImageViewer;
90 int main(int argc, char *argv[])
93 " \n vtkgdcmSerieViewer : \n",
94 " Display a 'Serie' (same Serie UID) within a Directory ",
95 " You can navigate through the stack by hitting any character key. ",
96 " usage: vtkgdcmSerieViewer dirname=sourcedirectory ",
97 " [noshadowseq][noshadow][noseq] ",
98 " [reverse] [{[mirror]|[topdown]|[rotate]}] ",
100 " sourcedirectory : name of the directory holding the images ",
101 " if it holds more than one serie, ",
102 " only the first one id displayed. ",
103 " noshadowseq: user doesn't want to load Private Sequences ",
104 " noshadow : user doesn't want to load Private groups (odd number) ",
105 " noseq : user doesn't want to load Sequences ",
106 " reverse : user wants to sort the images reverse order ",
107 " mirror : user wants to 'mirror' the images | just some simple ",
108 " topdown : user wants to 'topdown' the images| examples of user ",
109 " rotate : user wants NOT YET MADE | supplied functions ",
110 " check : user wants to force more coherence checking ",
111 " debug : user wants to run the program in 'debug mode' ",
115 // Initialize Arguments Manager
116 gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
118 if (argc == 1 || am->ArgMgrDefined("usage") )
120 am->ArgMgrUsage(usage); // Display 'usage'
125 char *dirName = am->ArgMgrWantString("dirname",usage);
127 int loadMode = 0x00000000;
128 if ( am->ArgMgrDefined("noshadowseq") )
129 loadMode |= NO_SHADOWSEQ;
132 if ( am->ArgMgrDefined("noshadow") )
133 loadMode |= NO_SHADOW;
134 if ( am->ArgMgrDefined("noseq") )
138 bool reverse = am->ArgMgrDefined("reverse");
140 bool mirror = am->ArgMgrDefined("mirror");
141 bool topdown = am->ArgMgrDefined("topdown");
142 bool rotate = am->ArgMgrDefined("rotate");
144 bool check = am->ArgMgrDefined("check");
146 if ( (int)mirror + (int)topdown + (int)rotate > 1)
148 std::cout << "mirror *OR* topDown *OR* rotate !"
154 if (am->ArgMgrDefined("debug"))
155 gdcm::Debug::DebugOn();
157 /* if unused Param we give up */
158 if ( am->ArgMgrPrintUnusedLabels() )
160 am->ArgMgrUsage(usage);
165 delete am; // we don't need Argument Manager any longer
167 // ----------------------- End Arguments Manager ----------------------
169 // ------------ to check Coherent File List as a parameter
171 gdcm::SerieHelper *sh = new gdcm::SerieHelper();
172 sh->SetLoadMode(loadMode);
174 sh->SetSortOrderToReverse();
175 sh->SetDirectory( dirName, true);
180 // For all the Coherent Files lists of the gdcm::Serie
181 gdcm::FileList *l = sh->GetFirstCoherentFileList();
184 std::cout << "Oops! No CoherentFileList found ?!?" << std::endl;
189 nbFiles = l->size() ;
192 std::cout << "Sort list : " << nbFiles << " long" << std::endl;
193 sh->OrderFileList(l); // sort the list
194 break; // The first one is OK. user will have to check
198 std::cout << "Oops! Empty CoherentFileList found ?!?" << std::endl;
200 l = sh->GetNextCoherentFileList();
205 if ( !sh->IsCoherent(l) ) // just be sure (?)
207 std::cout << "Files are not coherent. Stop everything " << std::endl;
213 vtkGdcmReader *reader = vtkGdcmReader::New();
214 reader->AllowLookupTableOff();
217 reader->SetUserFunction (userSuppliedMirrorFunction);
219 reader->SetUserFunction (userSuppliedTopDownFunction);
221 // Only the first FileList is dealt with (just an example)
222 // (The files will not be parsed twice by the reader)
224 //---------------------------------------------------------
225 reader->SetCoherentFileList(l);
226 //---------------------------------------------------------
228 // because we passed a Coherent File List from a SerieHelper,
229 // setting LoadMode is useless in this case
230 // reader->SetLoadMode(NO_SHADOWSEQ);
234 reader->GetOutput()->Print( cout );
236 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
238 vtkImageViewer *viewer = vtkImageViewer::New();
240 if( reader->GetLookupTable() )
243 vtkImageMapToColors *map = vtkImageMapToColors::New ();
244 map->SetInput (reader->GetOutput());
245 map->SetLookupTable (reader->GetLookupTable());
246 map->SetOutputFormatToRGB();
247 viewer->SetInput ( map->GetOutput() );
252 vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
253 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
254 viewer->SetColorWindow (range[1] - range[0]);
256 viewer->SetInput ( reader->GetOutput() );
258 viewer->SetupInteractor (iren);
260 //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
261 //viewer->SetColorWindow (range[1] - range[0]);
262 //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
264 // Here is where we setup the observer,
265 vtkgdcmObserver *obs = vtkgdcmObserver::New();
266 obs->ImageViewer = viewer;
267 iren->AddObserver(vtkCommand::CharEvent,obs);
274 //if you wish you can export dicom to a vtk file
275 vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
276 writer->SetInput( reader->GetOutput());
277 writer->SetFileName( "foo.vtk" );
278 writer->SetFileTypeToBinary();
290 // --------------------------------------------------------
291 // This is just a *very* simple example of user supplied function
292 // to mirror (why not ?) the image
293 // It's *not* part of gdcm.
294 // --------------------------------------------------------
302 imj = (ty *)im +j*nx; \
303 for (i=0;i<nx/2;i++) \
306 imj[i] =imj[nx-1-i]; \
314 imj = (ty *)im +j*nx; \
316 imj[i] =imj[nx/2+1]; \
321 void userSuppliedMirrorFunction(uint8_t *im, gdcm::File *f)
323 if (f->GetZSize() != 1)
325 std::cout << "mirror : Multiframe images not yet dealt with" << std::endl;
329 if (f->GetSamplesPerPixel() != 1 || f->GetBitsAllocated() == 24)
331 std::cout << "mirror : RGB / YBR not yet dealt with" << std::endl;
334 int nx = f->GetXSize();
335 int ny = f->GetYSize();
337 std::string pixelType = f->GetPixelType();
338 if ( pixelType == "8U" || pixelType == "8S" )
343 if ( pixelType == "16U" || pixelType == "16S")
348 std::cout << "mirror : Pixel Size (!=8, !=16) not yet dealt with"
354 // --------------------------------------------------------
355 // This is just a *very* simple example of user supplied function
356 // to topdown (why not ?) the image
357 // It's *not* part of gdcm.
358 // --------------------------------------------------------
364 for (j=0;j<ny/2;j++) \
366 imj = (ty *)im +j*nx; \
367 imJ = (ty *)im +(ny-1-j)*nx; \
376 void userSuppliedTopDownFunction(uint8_t *im, gdcm::File *f)
378 if (f->GetZSize() != 1)
380 std::cout << "mirror : Multiframe images not yet dealt with" << std::endl;
384 if (f->GetSamplesPerPixel() != 1 || f->GetBitsAllocated() == 24)
386 std::cout << "mirror : RGB / YBR not yet dealt with" << std::endl;
389 int nx = f->GetXSize();
390 int ny = f->GetYSize();
392 std::string pixelType = f->GetPixelType();
393 if ( pixelType == "8U" || pixelType == "8S" )
398 if ( pixelType == "16U" || pixelType == "16S")
403 std::cout << "topdown : Pixel Size (!=8, !=16) not yet dealt with"