creaImageIO_lib
creaImageIOMultiThreadImageReader.h
Go to the documentation of this file.
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 # This software is governed by the CeCILL-B license under French law and
11 # abiding by the rules of distribution of free software. You can use,
12 # modify and/ or redistribute the software under the terms of the CeCILL-B
13 # license as circulated by CEA, CNRS and INRIA at the following URL
14 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 # or in the file LICENSE.txt.
16 #
17 # As a counterpart to the access to the source code and rights to copy,
18 # modify and redistribute granted by the license, users are provided only
19 # with a limited warranty and the software's author, the holder of the
20 # economic rights, and the successive licensors have only limited
21 # liability.
22 #
23 # The fact that you are presently reading this means that you have had
24 # knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27 
28 #ifndef __creaImageIOThreadedImageReader_h_INCLUDED__
29 #define __creaImageIOThreadedImageReader_h_INCLUDED__
30 
31 #include <creaImageIOSystem.h>
32 #include <creaImageIOImageReader.h>
33 #include <creaImageIOIndexedHeap.h>
34 #include <map>
35 #include <deque>
36 #include <wx/thread.h>
37 #include <queue>
38 
39 
40 
41 namespace creaImageIO
42 {
46  //=====================================================================
47  class ThreadedImageReader;
48  class MultiThreadImageReader;
49  //=====================================================================
50 
51  //=====================================================================
53  {
54  public:
55  friend class ThreadedImageReader;
56  friend class MultiThreadImageReader;
57 
60 
61  typedef enum
62  {
67  Error
68  }
69  EventType;
76  virtual void OnMultiThreadImageReaderEvent( const std::string& filename,
77  EventType type,
78  vtkImageData* image)
79  {}
80  inline void MultiThreadImageReaderEventLock()
81  { mMultiThreadImageReaderUserMutex.Lock(); }
82  inline void MultiThreadImageReaderEventUnlock()
83  { mMultiThreadImageReaderUserMutex.Unlock(); }
84  inline wxMutex& GetMultiThreadImageReaderUserMutex()
85  { return mMultiThreadImageReaderUserMutex; }
86  private:
88  void MultiThreadImageReaderSendEvent( const std::string& filename,
89  EventType type,
90  vtkImageData* image);
92  };
93  //=====================================================================
94 
95  //=====================================================================
98 
101  {
102  public:
103  friend class ThreadedImageReader;
104 
106  MultiThreadImageReader(int number_of_threads = 1);
109 
112  bool Start();
114  void Stop();
115 
120  const std::string& filename,
121  int priority );
122 
126  vtkImageData* GetImage(const std::string& filename);
127 
129  int GetMaximalPriority();
130 
132  void OnMultiThreadImageReaderEvent( const std::string& filename,
133  EventType type,
134  vtkImageData* image);
135 
137  void getAttributes(const std::string filename, std::map <std::string , std::string> &infos, std::vector<std::string> i_attr);
138 
139  protected:
140  bool mDone;
144  {
145  public:
147  const std::string& filename,
148  int prio=0)
149  : mUser(user),
150  mFilename(filename),
151  mPriority(prio),
152  mIndex(-1),
153  mUnloadIndex(-1),
154  mImage(0)
155  {}
157  {
158  if (mImage>0)
159  {
160  // std::cout << "Refs = "<<mImage->GetReferenceCount()<<std::endl;
161  mImage->Delete();
162  }
163  }
166  const std::string& GetFilename() const { return mFilename; }
167  int GetPriority() const { return mPriority; }
168  void SetPriority(int p) { mPriority=p; }
169  int& Index() { return mIndex; }
170  int& UnloadIndex() { return mUnloadIndex; }
171  vtkImageData* GetImage() const { return mImage; }
172  void SetImage( vtkImageData* i ) { mImage=i; }
173 
174  std::map<std::string, std::string> getAttributes(const std::vector<std::string> i_attr);
175  private:
177  std::string mFilename;
179  int mIndex;
181  vtkImageData* mImage;
182  };
183  //
184 
187 
190  {
191  bool operator() (ImageToLoadPtr const & a, ImageToLoadPtr const & b)
192  const
193  {
194  return ( a->GetPriority() > b->GetPriority() );
195  }
196  };
199  {
200  bool operator() (ImageToLoadPtr const & a, ImageToLoadPtr const & b)
201  const
202  {
203  return ( a->GetPriority() < b->GetPriority() );
204  }
205  };
206 
207 
210  {
211  bool operator() (ImageToLoadPtr const & a, ImageToLoadPtr const & b)
212  const
213  {
214  return ( a->GetFilename() < b->GetFilename() );
215  }
216  };
217 
220  {
221  int& operator()(ImageToLoadPtr & t) const { return t->Index(); }
222  };
225  {
226  int& operator()(ImageToLoadPtr & t) const { return t->UnloadIndex(); }
227  };
228 
230  void SignalImageRead(ImageToLoadPtr p, bool purge);
231 
233  typedef std::map<ImageToLoadPtr,vtkImageData*,
245 
247  typedef std::vector<boost::shared_ptr<ThreadedImageReader> > ThreadedImageReaderListType;
248  //typedef std::vector<ThreadedImageReader* > ThreadedImageReaderListType;
255  // wxMutex mMutex;
256 
258  std::string mRequestedFilename;
260  vtkImageData* mRequestedImage;
261 
264 
271 
272  void UpdateUnloadPriority(ImageToLoadPtr p, int priority);
273  long mTotalMem;
275 
276 
277  }; // class MultiThreadImageReader
278  //=====================================================================
279 
280 
281 
282 } // namespace creaImageIO
283 
284 
285 
286 #endif // #ifndef __creaImageIOThreadedImageReader_h_INCLUDED__