1 /*=========================================================================
4 Module: $RCSfile: vtkgdcmSerieViewer2.cxx,v $
6 Date: $Date: 2006/02/03 16:37:50 $
7 Version: $Revision: 1.3 $
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 userSuppliedTopDownFunction(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 int max = ImageViewer->GetWholeZMax();
89 int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
90 ImageViewer->SetZSlice( slice );
91 ImageViewer->GetRenderer()->ResetCameraClippingRange();
92 ImageViewer->Render();
96 vtkImageViewer2 *ImageViewer;
99 int main(int argc, char *argv[])
102 " \n vtkgdcmSerieViewer2 : \n",
103 " Display a 'Serie' (same Serie UID) within a Directory ",
104 " You can navigate through the stack by hitting any character key. ",
105 " usage: vtkgdcmSerieViewer dirname=sourcedirectory ",
106 " [noshadowseq][noshadow][noseq] ",
107 " [reverse] [{[mirror]|[topdown]|[rotate]}] ",
108 " [order=] [check][debug] ",
109 " sourcedirectory : name of the directory holding the images ",
110 " if it holds more than one serie, ",
111 " only the first one id displayed. ",
112 " noshadowseq: user doesn't want to load Private Sequences ",
113 " noshadow : user doesn't want to load Private groups (odd number) ",
114 " noseq : user doesn't want to load Sequences ",
115 " reverse : user wants to sort the images reverse order ",
116 " mirror : user wants to 'mirror' the images | just some simple ",
117 " topdown : user wants to 'topdown' the images| examples of user ",
118 " rotate : NOT YET MADE (useless?) | supplied functions ",
119 " check : user wants to force more coherence checking ",
120 " order= : group1-elem1,group2-elem2,... (in hexa, no space) ",
121 " if we want to use them as a sort criterium ",
122 " Right now : ValEntries only -just an example- ",
124 " order= : order=name if we want to sort on file name (why not ?) ",
125 " debug : user wants to run the program in 'debug mode' ",
129 // Initialize Arguments Manager
130 gdcm::ArgMgr *am= new gdcm::ArgMgr(argc, argv);
132 if (argc == 1 || am->ArgMgrDefined("usage") )
134 am->ArgMgrUsage(usage); // Display 'usage'
139 char *dirName = am->ArgMgrWantString("dirname",usage);
141 int loadMode = gdcm::LD_ALL;
142 if ( am->ArgMgrDefined("noshadowseq") )
143 loadMode |= gdcm::LD_NOSHADOWSEQ;
146 if ( am->ArgMgrDefined("noshadow") )
147 loadMode |= gdcm::LD_NOSHADOW;
148 if ( am->ArgMgrDefined("noseq") )
149 loadMode |= gdcm::LD_NOSEQ;
152 int reverse = am->ArgMgrDefined("reverse");
154 int mirror = am->ArgMgrDefined("mirror");
155 int topdown = am->ArgMgrDefined("topdown");
156 int rotate = am->ArgMgrDefined("rotate");
158 if ( mirror && topdown )
160 std::cout << "mirror *OR* topDown !"
167 std::cout << "'rotate' undealt with -> ignored !"
171 int check = am->ArgMgrDefined("check");
173 // This is so ugly, a cstring is NOT a char * (god damit!)
174 bool bname = ( strcmp(am->ArgMgrGetString("order", "not found"),"name")==0 );
176 elemsToOrderOn = am->ArgMgrGetXInt16Enum("order", &orderNb);
178 if (am->ArgMgrDefined("debug"))
179 gdcm::Debug::DebugOn();
181 /* if unused Param we give up */
182 if ( am->ArgMgrPrintUnusedLabels() )
184 am->ArgMgrUsage(usage);
189 delete am; // we don't need Argument Manager any longer
191 // ----------------------- End Arguments Manager ----------------------
193 gdcm::SerieHelper *sh = gdcm::SerieHelper::New();
194 sh->SetLoadMode(loadMode);
196 sh->SetSortOrderToReverse();
197 sh->SetDirectory( dirName, true);
202 // For all the 'Single Serie UID' FileSets of the gdcm::Serie
203 gdcm::FileList *l = sh->GetFirstSingleSerieUIDFileSet();
206 std::cout << "Oops! No 'Single Serie UID' FileSet found ?!?" << std::endl;
211 sh->SetUserLessThanFunction(userSuppliedLessThanFunction2);
212 else if (orderNb != 0)
213 sh->SetUserLessThanFunction(userSuppliedLessThanFunction);
217 nbFiles = l->size() ;
220 std::cout << "Sort list : " << nbFiles << " long" << std::endl;
221 sh->OrderFileList(l); // sort the list
222 std::cout << "List sorted" << std::endl;
223 break; // The first one is OK. user will have to check
227 std::cout << "Oops! Empty 'Single Serie UID' FileSet found ?!?"
230 l = sh->GetNextSingleSerieUIDFileSet();
235 if ( !sh->IsCoherent(l) ) // just be sure (?)
237 std::cout << "Files are not coherent. Stop everything " << std::endl;
243 vtkGdcmReader *reader = vtkGdcmReader::New();
244 reader->AllowLookupTableOff();
247 reader->SetUserFunction (userSuppliedMirrorFunction);
249 reader->SetUserFunction (userSuppliedTopDownFunction);
251 // Only the first FileList is dealt with (just an example)
252 // (The files will not be parsed twice by the reader)
254 //---------------------------------------------------------
255 reader->SetCoherentFileList(l);
256 //---------------------------------------------------------
258 // because we passed a Coherent File List from a SerieHelper,
259 // setting LoadMode is useless in this case
260 // reader->SetLoadMode(NO_SHADOWSEQ);
264 reader->GetOutput()->Print( cout );
266 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
268 vtkImageViewer2 *viewer = vtkImageViewer2::New();
270 if( reader->GetLookupTable() )
273 vtkImageMapToColors *map = vtkImageMapToColors::New ();
274 map->SetInput (reader->GetOutput());
275 map->SetLookupTable (reader->GetLookupTable());
276 map->SetOutputFormatToRGB();
277 viewer->SetInput ( map->GetOutput() );
282 vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
283 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
284 viewer->SetColorWindow (range[1] - range[0]);
286 viewer->SetInput ( reader->GetOutput() );
288 viewer->SetupInteractor (iren);
290 //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
291 //viewer->SetColorWindow (range[1] - range[0]);
292 //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
294 // Here is where we setup the observer,
295 vtkgdcmObserver *obs = vtkgdcmObserver::New();
296 obs->ImageViewer = viewer;
297 iren->AddObserver(vtkCommand::CharEvent,obs);
304 //if you wish you can export dicom to a vtk file
305 vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
306 writer->SetInput( reader->GetOutput());
307 writer->SetFileName( "foo.vtk" );
308 writer->SetFileTypeToBinary();
320 // --------------------------------------------------------
321 // This is just a *very* simple example of user supplied function
322 // to mirror (why not ?) the image
323 // It's *not* part of gdcm.
324 // --------------------------------------------------------
332 imj = (ty *)im +j*nx; \
333 for (i=0;i<nx/2;i++) \
336 imj[i] =imj[nx-1-i]; \
345 imj = (ty *)im +j*nx; \
347 imj[i] =imj[nx/2+1]; \
352 void userSuppliedMirrorFunction(uint8_t *im, gdcm::File *f)
354 if (f->GetZSize() != 1)
356 std::cout << "mirror : Multiframe images not yet dealt with" << std::endl;
360 if (f->GetSamplesPerPixel() != 1 || f->GetBitsAllocated() == 24)
362 std::cout << "mirror : RGB / YBR not yet dealt with" << std::endl;
365 int nx = f->GetXSize();
366 int ny = f->GetYSize();
368 std::string pixelType = f->GetPixelType();
369 if ( pixelType == "8U" || pixelType == "8S" )
374 if ( pixelType == "16U" || pixelType == "16S")
379 std::cout << "mirror : Pixel Size (!=8, !=16) not yet dealt with"
385 // --------------------------------------------------------
386 // This is just a *very* simple example of user supplied function
387 // to topdown (why not ?) the image
388 // It's *not* part of gdcm.
389 // --------------------------------------------------------
395 for (j=0;j<ny/2;j++) \
397 imj = (ty *)im +j*nx; \
398 imJ = (ty *)im +(ny-1-j)*nx; \
407 void userSuppliedTopDownFunction(uint8_t *im, gdcm::File *f)
409 if (f->GetZSize() != 1)
411 std::cout << "mirror : Multiframe images not yet dealt with" << std::endl;
415 if (f->GetSamplesPerPixel() != 1 || f->GetBitsAllocated() == 24)
417 std::cout << "mirror : RGB / YBR not yet dealt with" << std::endl;
420 int nx = f->GetXSize();
421 int ny = f->GetYSize();
423 std::string pixelType = f->GetPixelType();
424 if ( pixelType == "8U" || pixelType == "8S" )
429 if ( pixelType == "16U" || pixelType == "16S")
434 std::cout << "topdown : Pixel Size (!=8, !=16) not yet dealt with"
439 // --------------------------------------------------------
440 // This is just a *very* simple example of user supplied 'LessThan' function
441 // It's *not* part of gdcm.
443 // Note : orderNb and elemsToOrderOn are here global variables.
444 // Within a 'normal' function they would't be any orderNb and elemsToOrderOn var
445 // User *knows* on what field(s) he wants to compare;
446 // He just writes a decent function.
447 // Here, we want to get info from the command line Argument Manager.
449 // Warning : it's up to 'vtkgdcmSerieViewer' user to find a suitable data set !
450 // --------------------------------------------------------
453 bool userSuppliedLessThanFunction(gdcm::File *f1, gdcm::File *f2)
455 // for *this* user supplied function, I supposed only ValEntries are checked.
458 gdcm::DataEntry *e1,*e2;
459 for (int ri=0; ri<orderNb; ri++)
461 std::cout << std::hex << elemsToOrderOn[2*ri] << "|"
462 << elemsToOrderOn[2*ri+1]
465 e1= f1->GetDataEntry( elemsToOrderOn[2*ri],
466 elemsToOrderOn[2*ri+1]);
468 e2= f2->GetDataEntry( elemsToOrderOn[2*ri],
469 elemsToOrderOn[2*ri+1]);
472 std::cout << std::hex << elemsToOrderOn[2*ri] << "|"
473 << elemsToOrderOn[2*ri+1]
474 << " not found" << std::endl;
477 s1 = e1->GetString();
478 s2 = e2->GetString();
479 std::cout << "[" << s1 << "] vs [" << s2 << "]" << std::endl;
487 return false; // all fields equal
490 // --------------------------------------------------------
491 // This is just an other *very* simple example of user supplied 'LessThan'
493 // It's *not* part of gdcm.
495 // Warning : it's up to 'vtkgdcmSerieViewer' user to find a suitable data set !
496 // --------------------------------------------------------
498 bool userSuppliedLessThanFunction2(gdcm::File *f1, gdcm::File *f2)
500 std::cout << "[" << f1->GetFileName() << "] vs ["
501 << f2->GetFileName() << "]" << std::endl;
502 return f1->GetFileName() < f2->GetFileName();