43 (
const std::string& filename,
54 class ThreadedImageReader:
public QThread
64 vtkImageData*
Read(
const std::string& filename);
97 for (
int i=0; i<number_of_threads; i++)
101 mThreadedImageReaderList.push_back(t);
102 std::cout <<
" ===> Thread "<<i
103 <<
" successfully added"<< std::endl;
105 mNumberOfThreadedReadersRunning = 0;
107 mQueue.set(mComparator);
108 mQueue.set(mIndexer);
125 if (mNumberOfThreadedReadersRunning > 0)
return true;
127 ThreadedImageReaderListType::iterator i;
128 for (i =mThreadedImageReaderList.begin();
129 i!=mThreadedImageReaderList.end();
133 if ( !(*i)->isRunning() )
135 std::cout <<
"ERROR starting a thread"<< std::endl;
140 std::cout <<
" ===> Thread "<<(*i)->currentThreadId()
141 <<
" successfully created"<< std::endl;
145 QMutexLocker locker(GetMultiThreadImageReaderUserMutex());
161 ThreadedImageReaderListType::iterator i;
162 for (i =mThreadedImageReaderList.begin();
163 i!=mThreadedImageReaderList.end();
165 { std::cout <<
" ===> Thread "<<(*i)->currentThreadId()
166 <<
" successfully stopped"<< std::endl;
167 if(!(*i)->isFinished())
173 mThreadedImageReaderList.clear();
186 QMutexLocker locker(GetMultiThreadImageReaderUserMutex());
190 if (mNumberOfThreadedReadersRunning <= 0)
199 ImageMapType::iterator j;
200 for (j =mImages.begin();
219 mThreadedImageReaderList.clear();
228 if (p->UnloadIndex()<0)
return;
229 int old_prio = p->GetPriority();
230 if (priority > old_prio)
232 p->SetPriority(priority);
233 mUnloadQueue.downsort(p->UnloadIndex());
235 else if ( old_prio > priority )
237 p->SetPriority(priority);
238 mUnloadQueue.upsort(p->UnloadIndex());
244 std::map <std::string , std::string> &infos,std::vector<std::string> i_attr)
251 const std::string& filename,
254 QMutexLocker lock(GetMultiThreadImageReaderUserMutex());
256 if (mNumberOfThreadedReadersRunning==0)
260 ImageToLoad itl(user,filename);
261 ImageMapType::iterator i = mImages.find(&itl);
262 if (i!=mImages.end())
264 ImageToLoadPtr pitl =
const_cast<ImageToLoadPtr
>(i->first);
266 if (pitl->GetImage() != 0)
270 UpdateUnloadPriority(pitl,priority);
271 SignalImageRead(pitl,
false);
275 ImageToLoadPtr pitl =
new ImageToLoad(user,filename,0);
278 UpdateUnloadPriority(pitl,priority);
279 SignalImageRead(pitl,
true);
284 ImageToLoad itl(user,filename);
285 ImageMapType::iterator i = mImages.find(&itl);
286 if (i!=mImages.end())
289 if (i->first->GetImage() != 0)
292 UpdateUnloadPriority(i->first,priority);
293 SignalImageRead(i->first,
false);
297 ImageToLoadPtr pitl =
const_cast<ImageToLoadPtr
>(i->first);
298 pitl->SetPriority(priority);
300 if (pitl->Index()>=0)
303 mQueue.upsort(pitl->Index());
314 ImageToLoadPtr pitl =
new ImageToLoad(user,filename,priority);
323 (
const std::string& filename,
328 (filename == mRequestedFilename))
330 mRequestedImage = image;
334 mNumberOfThreadedReadersRunning++;
340 mNumberOfThreadedReadersRunning--;
365 ImageToLoad itl(
this,filename);
366 ImageMapType::iterator i = mImages.find(&itl);
367 if (i!=mImages.end())
369 ImageToLoadPtr pitl =
const_cast<ImageToLoadPtr
>(i->first);
371 if (pitl->GetImage() != 0)
374 UpdateUnloadPriority(pitl,
375 GetMaximalPriorityWithoutLocking()+1);
376 return pitl->GetImage();
379 ImageToLoadPtr pitl =
new ImageToLoad(
this,filename,0);
382 UpdateUnloadPriority(pitl,
383 GetMaximalPriorityWithoutLocking()+1);
384 return pitl->GetImage();
465 if ( p->GetUser() == this )
466 GetMultiThreadImageReaderUserMutex()->unlock();
468 p->GetUser()->MultiThreadImageReaderSendEvent
478 GimmickMessage(5,
"Image '"<<p->GetFilename()<<
"' read"<<std::endl);
482 mUnloadQueue.insert(p);
483 p->GetImage()->UpdateInformation();
484 p->GetImage()->PropagateUpdateExtent();
485 long ImMem = p->GetImage()->GetEstimatedMemorySize();
488 GimmickMessage(5,
"==> Image in memory = "<<mUnloadQueue.size()<<std::endl);
489 GimmickMessage(5,
"==> Total mem = "<<mTotalMem<<
" Ko"<<std::endl);
493 while (mTotalMem > mTotalMemMax)
496 " ! Exceeded max of "
497 << mTotalMemMax <<
" Ko : unloading oldest image ... "
499 if ( mUnloadQueue.size() <= 1 )
502 " Only one image : cannot load AND unload it !!"
507 ImageToLoadPtr unload = mUnloadQueue.remove_top();
508 MultiThreadImageReaderUser* user = unload->GetUser();
517 std::string filename = unload->GetFilename();
520 mTotalMem -= unload->GetImage()->GetEstimatedMemorySize();
522 GimmickMessage(5,
" ==> Total mem = "<<mTotalMem<<
" Ko "<<std::endl);
529 user->MultiThreadImageReaderSendEvent
536 if (unload->Index()>=0)
540 unload->Index() = -1;
543 ImageMapType::iterator it = mImages.find(unload);
544 if (it!=mImages.end())
559 QMutexLocker lock(GetMultiThreadImageReaderUserMutex());
560 return GetMaximalPriorityWithoutLocking();
571 max = mQueue.top()->GetPriority();
573 if (mUnloadQueue.size()>0)
575 int max2 = mUnloadQueue.top()->GetPriority();
576 if (max2>max) max=max2;
600 while (!isFinished())
621 vtkImageData* im =
Read(i->GetFilename());
626 MultiThreadImageReader::ImageToLoad itl(0,i->GetFilename());
627 MultiThreadImageReader::ImageMapType::iterator it =