1 /*=========================================================================
4 Module: $RCSfile: vtkgdcmSerieViewer2.cxx,v $
6 Date: $Date: 2006/05/15 21:28:37 $
7 Version: $Revision: 1.6 $
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
19 // use the result of gdcm::SerieHelper constructor and check
20 // the various Setters :
21 // SerieHelper::SetOrderToReverse,
22 // SerieHelper::SetUserLessThanFunction
23 // SerieHelper::SetLoadMode
24 // vtkGdcmReader::SetUserFunction
25 // vtkGdcmReader::SetCoherentFileList
27 // * the Directory name that contains the Dicom images constituting the stack
28 // should be given as command line argument (keyword : dirname=),
29 // * you can navigate through the stack by hitting any character key,
30 // * the produced vtk file is named "foo.vtk" (in the invocation directory).
32 //----------------------------------------------------------------------------
33 #include <vtkRenderWindowInteractor.h>
34 #include <vtkImageViewer2.h>
35 #include <vtkStructuredPoints.h>
36 #include <vtkStructuredPointsWriter.h>
37 #include <vtkCommand.h>
38 #include <vtkRenderer.h>
39 #include <vtkImageMapToColors.h>
40 #include <vtkLookupTable.h>
42 #include "vtkGdcmReader.h"
43 #include "gdcmDocument.h" // for NO_SHADOWSEQ
44 #include "gdcmSerieHelper.h"
45 #include "gdcmDebug.h"
46 #include "gdcmDataEntry.h"
48 #include "gdcmArgMgr.h" // for Argument Manager functions
49 #include <string.h> // for strcmp
50 #ifndef vtkFloatingPointType
51 #define vtkFloatingPointType float
54 void userSuppliedMirrorFunction (uint8_t *im, gdcm::File *f);
55 void userSuppliedUpsideDownFunction(uint8_t *im, gdcm::File *f);
56 bool userSuppliedLessThanFunction(gdcm::File *f1, gdcm::File *f2);
57 bool userSuppliedLessThanFunction2(gdcm::File *f1, gdcm::File *f2);
60 uint16_t *elemsToOrderOn;
62 //----------------------------------------------------------------------------
63 // Callback for the interaction
64 class vtkgdcmObserver : public vtkCommand
67 virtual char const *GetClassName() const
69 return "vtkgdcmObserver";
72 static vtkgdcmObserver *New()
74 return new vtkgdcmObserver;
79 this->ImageViewer = NULL;
82 virtual void Execute(vtkObject *, unsigned long event, void* )
84 if ( this->ImageViewer )
86 if ( event == vtkCommand::CharEvent )
88 #if (VTK_MAJOR_VERSION >= 5)
89 int max = ImageViewer->GetSliceMax();
90 int slice = (ImageViewer->GetSlice() + 1 ) % ++max;
91 ImageViewer->SetSlice( slice );
93 int max = ImageViewer->GetWholeZMax();
94 int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
95 ImageViewer->SetZSlice( slice );
97 #if !( (VTK_MAJOR_VERSION >= 5) || ( VTK_MAJOR_VERSION == 4 && VTK_MINOR_VERSION >= 5 ) )
98 // This used to be a bug in version VTK 4.4 and earlier
99 ImageViewer->GetRenderer()->ResetCameraClippingRange();
101 ImageViewer->Render();
105 vtkImageViewer2 *ImageViewer;
108 int main(int argc, char *argv[])
111 " \n vtkgdcmSerieViewer2 : \n",
112 " Display a 'Serie' (same Serie UID) within a Directory ",
113 " You can navigate through the stack by hitting any character key. ",
114 " usage: vtkgdcmSerieViewer dirname=sourcedirectory ",
115 " [noshadowseq][noshadow][noseq] ",
116 " [reverse] [{[mirror]|[topdown]|[rotate]}] ",
117 " [order=] [check][debug] ",
118 " sourcedirectory : name of the directory holding the images ",
119 " if it holds more than one serie, ",
120 " only the first one is displayed. ",
121 " noshadowseq: user doesn't want to load Private Sequences ",
122 " noshadow : user doesn't want to load Private groups (odd number) ",
123 " noseq : user doesn't want to load Sequences ",
124 " reverse : user wants to sort the images reverse order ",
125 " mirror : user wants to 'mirror' the images | just some simple",
126 " upsidedown : user wants to 'upsidedown' the images| examples of user",
127 " | definedfunctions",
128 " check : user wants to force more coherence checking ",
129 " order= : group1-elem1,group2-elem2,... (in hexa, no space) ",
130 " if we want to use them as a sort criterium ",
131 " Right now : ValEntries only -just an example- ",
133 " order= : order=name if we want to sort on file name (why not ?) ",
134 " debug : user wants to run the program in 'debug mode' ",
138 // Initialize Arguments Manager
139 gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
141 if (argc == 1 || am->ArgMgrDefined("usage") )
143 am->ArgMgrUsage(usage); // Display 'usage'
148 char *dirName = am->ArgMgrWantString("dirname",usage);
150 int loadMode = gdcm::LD_ALL;
151 if ( am->ArgMgrDefined("noshadowseq") )
152 loadMode |= gdcm::LD_NOSHADOWSEQ;
155 if ( am->ArgMgrDefined("noshadow") )
156 loadMode |= gdcm::LD_NOSHADOW;
157 if ( am->ArgMgrDefined("noseq") )
158 loadMode |= gdcm::LD_NOSEQ;
161 int reverse = am->ArgMgrDefined("reverse");
163 int mirror = am->ArgMgrDefined("mirror");
164 int upsidedown = am->ArgMgrDefined("upsidedown");
166 if ( mirror && upsidedown )
168 std::cout << "*EITHER* mirror *OR* upsidedown !"
174 int check = am->ArgMgrDefined("check");
176 // This is so ugly, a cstring is NOT a char * (god damit!)
177 bool bname = ( strcmp(am->ArgMgrGetString("order", "not found"),"name")==0 );
179 elemsToOrderOn = am->ArgMgrGetXInt16Enum("order", &orderNb);
181 if (am->ArgMgrDefined("debug"))
182 gdcm::Debug::DebugOn();
184 /* if unused Param we give up */
185 if ( am->ArgMgrPrintUnusedLabels() )
187 am->ArgMgrUsage(usage);
192 delete am; // we don't need Argument Manager any longer
194 // ----------------------- End Arguments Manager ----------------------
196 gdcm::SerieHelper *sh = gdcm::SerieHelper::New();
197 sh->SetLoadMode(loadMode);
199 sh->SetSortOrderToReverse();
200 sh->SetDirectory( dirName, true);
205 // For all the 'Single Serie UID' FileSets of the gdcm::Serie
206 gdcm::FileList *l = sh->GetFirstSingleSerieUIDFileSet();
209 std::cout << "Oops! No 'Single Serie UID' FileSet found ?!?" << std::endl;
214 sh->SetUserLessThanFunction(userSuppliedLessThanFunction2);
215 else if (orderNb != 0)
216 sh->SetUserLessThanFunction(userSuppliedLessThanFunction);
220 nbFiles = l->size() ;
223 std::cout << "Sort list : " << nbFiles << " long" << std::endl;
224 sh->OrderFileList(l); // sort the list
225 double zsp = sh->GetZSpacing();
226 std::cout << "List sorted, ZSpacing = " << zsp << std::endl;
227 break; // The first one is OK. user will have to check
231 std::cout << "Oops! Empty 'Single Serie UID' FileSet found ?!?"
234 l = sh->GetNextSingleSerieUIDFileSet();
239 if ( !sh->IsCoherent(l) ) // just be sure (?)
241 std::cout << "Files are not coherent. Stop everything " << std::endl;
247 vtkGdcmReader *reader = vtkGdcmReader::New();
248 reader->AllowLookupTableOff();
251 reader->SetUserFunction (userSuppliedMirrorFunction);
253 reader->SetUserFunction (userSuppliedUpsideDownFunction);
255 // Only the first FileList is dealt with (just an example)
256 // (The files will not be parsed twice by the reader)
258 //---------------------------------------------------------
259 reader->SetCoherentFileList(l);
260 //---------------------------------------------------------
262 // because we passed a Coherent File List from a SerieHelper,
263 // setting LoadMode is useless in this case
264 // reader->SetLoadMode(NO_SHADOWSEQ);
268 reader->GetOutput()->Print( cout );
270 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
272 vtkImageViewer2 *viewer = vtkImageViewer2::New();
274 if( reader->GetLookupTable() )
277 vtkImageMapToColors *map = vtkImageMapToColors::New ();
278 map->SetInput (reader->GetOutput());
279 map->SetLookupTable (reader->GetLookupTable());
280 map->SetOutputFormatToRGB();
281 viewer->SetInput ( map->GetOutput() );
286 vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
287 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
288 viewer->SetColorWindow (range[1] - range[0]);
290 viewer->SetInput ( reader->GetOutput() );
292 viewer->SetupInteractor (iren);
294 //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
295 //viewer->SetColorWindow (range[1] - range[0]);
296 //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
298 // Here is where we setup the observer,
299 vtkgdcmObserver *obs = vtkgdcmObserver::New();
300 obs->ImageViewer = viewer;
301 iren->AddObserver(vtkCommand::CharEvent,obs);
308 //if you wish you can export dicom to a vtk file
309 vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
310 writer->SetInput( reader->GetOutput());
311 writer->SetFileName( "foo.vtk" );
312 writer->SetFileTypeToBinary();
324 // --------------------------------------------------------
325 // This is just a *very* simple example of user supplied function
326 // to mirror (why not ?) the image
327 // It's *not* part of gdcm.
328 // --------------------------------------------------------
336 imj = (ty *)im +j*nx; \
337 for (i=0;i<nx/2;i++) \
340 imj[i] =imj[nx-1-i]; \
349 imj = (ty *)im +j*nx; \
351 imj[i] =imj[nx/2+1]; \
356 void userSuppliedMirrorFunction(uint8_t *im, gdcm::File *f)
358 if (f->GetZSize() != 1)
360 std::cout << "mirror : Multiframe images not yet dealt with" << std::endl;
364 if (f->GetSamplesPerPixel() != 1 || f->GetBitsAllocated() == 24)
366 std::cout << "mirror : RGB / YBR not yet dealt with" << std::endl;
369 int nx = f->GetXSize();
370 int ny = f->GetYSize();
372 std::string pixelType = f->GetPixelType();
373 if ( pixelType == "8U" || pixelType == "8S" )
378 if ( pixelType == "16U" || pixelType == "16S")
383 std::cout << "mirror : Pixel Size (!=8, !=16) not yet dealt with"
389 // --------------------------------------------------------
390 // This is just a *very* simple example of user supplied function
391 // to upsidedown (why not ?) the image
392 // It's *not* part of gdcm.
393 // --------------------------------------------------------
399 for (j=0;j<ny/2;j++) \
401 imj = (ty *)im +j*nx; \
402 imJ = (ty *)im +(ny-1-j)*nx; \
411 void userSuppliedUpsideDownFunction(uint8_t *im, gdcm::File *f)
413 if (f->GetZSize() != 1)
415 std::cout << "mirror : Multiframe images not yet dealt with" << std::endl;
419 if (f->GetSamplesPerPixel() != 1 || f->GetBitsAllocated() == 24)
421 std::cout << "mirror : RGB / YBR not yet dealt with" << std::endl;
424 int nx = f->GetXSize();
425 int ny = f->GetYSize();
427 std::string pixelType = f->GetPixelType();
428 if ( pixelType == "8U" || pixelType == "8S" )
433 if ( pixelType == "16U" || pixelType == "16S")
438 std::cout << "topdown : Pixel Size (!=8, !=16) not yet dealt with"
443 // --------------------------------------------------------
444 // This is just a *very* simple example of user supplied 'LessThan' function
445 // It's *not* part of gdcm.
447 // Note : orderNb and elemsToOrderOn are here global variables.
448 // Within a 'normal' function they would't be any orderNb and elemsToOrderOn var
449 // User *knows* on what field(s) he wants to compare;
450 // He just writes a decent function.
451 // Here, we want to get info from the command line Argument Manager.
453 // Warning : it's up to 'vtkgdcmSerieViewer' user to find a suitable data set !
454 // --------------------------------------------------------
457 bool userSuppliedLessThanFunction(gdcm::File *f1, gdcm::File *f2)
459 // for *this* user supplied function, I supposed only ValEntries are checked.
462 gdcm::DataEntry *e1,*e2;
463 for (int ri=0; ri<orderNb; ri++)
465 std::cout << std::hex << elemsToOrderOn[2*ri] << "|"
466 << elemsToOrderOn[2*ri+1]
469 e1= f1->GetDataEntry( elemsToOrderOn[2*ri],
470 elemsToOrderOn[2*ri+1]);
472 e2= f2->GetDataEntry( elemsToOrderOn[2*ri],
473 elemsToOrderOn[2*ri+1]);
476 std::cout << std::hex << elemsToOrderOn[2*ri] << "|"
477 << elemsToOrderOn[2*ri+1]
478 << " not found" << std::endl;
481 s1 = e1->GetString();
482 s2 = e2->GetString();
483 std::cout << "[" << s1 << "] vs [" << s2 << "]" << std::endl;
491 return false; // all fields equal
494 // --------------------------------------------------------
495 // This is just an other *very* simple example of user supplied 'LessThan'
497 // It's *not* part of gdcm.
499 // Warning : it's up to 'vtkgdcmSerieViewer' user to find a suitable data set !
500 // --------------------------------------------------------
502 bool userSuppliedLessThanFunction2(gdcm::File *f1, gdcm::File *f2)
504 std::cout << "[" << f1->GetFileName() << "] vs ["
505 << f2->GetFileName() << "]" << std::endl;
506 return f1->GetFileName() < f2->GetFileName();