SoftOpId.h 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//###########################################################################
// This file is part of LImA, a Library for Image Acquisition
//
// Copyright (C) : 2009-2011
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//###########################################################################
22 23
#ifndef __SOFTOPID_H
#define __SOFTOPID_H
seb's avatar
seb committed
24

25 26 27 28 29

#include "SizeUtils.h"

#include "TaskMgr.h"

seb's avatar
seb committed
30
#include "BackgroundSubstraction.h"
31 32 33 34 35
#include "Binning.h"
#include "Bpm.h"
#include "FlatfieldCorrection.h"
#include "Flip.h"
#include "Mask.h"
36
#include "RoiCounter.h"
37
#include "Roi2Spectrum.h"
38
#include "SoftRoi.h"
seb's avatar
seb committed
39

40 41
namespace lima
{
42
  class LIMACORE_API SoftOpBaseClass
43 44 45 46 47 48 49
  {
    friend class SoftOpExternalMgr;
  protected:
    static const int DEFAULT_HISTORY_SIZE = 128;

    virtual ~SoftOpBaseClass() {};

50
    virtual bool addTo(TaskMgr&,int stage) = 0;
51 52 53
    virtual void prepare() = 0;
  };

54 55
  enum SoftOpId
    {
seb's avatar
seb committed
56
      UNDEF,
57 58 59 60 61 62
      BACKGROUNDSUBSTRACTION,
      BINNING,
      BPM,
      FLATFIELDCORRECTION,
      FLIP,
      MASK,
63
      ROICOUNTERS,
64
      ROI2SPECTRUM,
65 66 67
      SOFTROI,
      USER_LINK_TASK,
      USER_SINK_TASK,
68 69
    };

70
  struct LIMACORE_API SoftOpKey
71
  {
72 73 74
    SoftOpKey() : m_id(UNDEF),m_name(NULL) {};
    SoftOpKey(SoftOpId id,const char *name) : m_id(id),m_name(name) {}

75 76 77
    SoftOpId	m_id;
    const char *m_name;
  };
78

79
  struct LIMACORE_API SoftOpInstance
80
  {
81 82
    SoftOpInstance() : m_linkable(false),
		       m_opt(NULL) {};
papillon's avatar
 
papillon committed
83

84 85 86 87 88 89 90 91 92
    SoftOpInstance(const SoftOpKey &akey,
		   const std::string &anAlias) :
      m_key(akey),
      m_alias(anAlias),m_linkable(false),m_opt(NULL) {}
    SoftOpKey		m_key;
    std::string		m_alias;
    bool		m_linkable;
    SoftOpBaseClass*	m_opt;
  };
93
  
94
  class LIMACORE_API SoftOpBackgroundSubstraction : public SoftOpBaseClass
95 96
  {
  public:
97
    SoftOpBackgroundSubstraction();
98
    virtual ~SoftOpBackgroundSubstraction();
99
    
100 101 102
    void setBackgroundImage(Data &aData);
    
  protected:
103
    virtual bool addTo(TaskMgr&,int stage);
104
    virtual void prepare() {};
105
  private:
seb's avatar
seb committed
106
    Tasks::BackgroundSubstraction *m_opt;
107 108
  };

109
  class LIMACORE_API SoftOpBinning : public SoftOpBaseClass
110 111 112 113 114 115 116 117
  {
  public:
    SoftOpBinning();
    virtual ~SoftOpBinning();
    
    void setBinning(int x,int y);
    
  protected:
118
    virtual bool addTo(TaskMgr&,int stage);
119 120 121 122 123
    virtual void prepare() {};
  private:
    Tasks::Binning *m_opt;
  };

124
  class LIMACORE_API SoftOpBpm : public SoftOpBaseClass
125 126 127 128 129 130 131 132 133
  {
  public:
    SoftOpBpm();
    virtual ~SoftOpBpm();
    
    Tasks::BpmTask* 	getTask() 	{return m_task;}
    Tasks::BpmManager* 	getManager() 	{return m_manager;}

  protected:
134
    virtual bool addTo(TaskMgr&,int stage);
135 136 137 138 139 140
    virtual void prepare();
  private:
    Tasks::BpmManager	*m_manager;
    Tasks::BpmTask	*m_task;
  };

141
  class LIMACORE_API SoftOpFlatfieldCorrection : public SoftOpBaseClass
142 143 144 145 146
  {
  public:
    SoftOpFlatfieldCorrection();
    virtual ~SoftOpFlatfieldCorrection();
    
147
    void setFlatFieldImage(Data &aData,bool normalize = true);
148 149
    
  protected:
150
    virtual bool addTo(TaskMgr&,int stage);
151 152 153 154 155
    virtual void prepare() {};
  private:
    Tasks::FlatfieldCorrection *m_opt;
  };

156
  class LIMACORE_API SoftOpFlip : public SoftOpBaseClass
157 158 159 160 161 162 163 164
  {
  public:
    SoftOpFlip();
    virtual ~SoftOpFlip();
    
    void setFlip(bool x,bool y);
    
  protected:
165
    virtual bool addTo(TaskMgr&,int stage);
166 167 168 169 170
    virtual void prepare() {};
  private:
    Tasks::Flip *m_opt;
  };

171
  class LIMACORE_API SoftOpMask : public SoftOpBaseClass
172 173
  {
  public:
174
    enum Type {STANDARD,DUMMY};
175 176
    SoftOpMask();
    virtual ~SoftOpMask();
177

178
    void setMaskImage(Data &aData);
179 180
    void getType(Type&) const;
    void setType(Type);
181
  protected:
182
    virtual bool addTo(TaskMgr&,int stage);
183 184 185 186 187
    virtual void prepare() {};
  private:
    Tasks::Mask *m_opt;
  };

188
  class LIMACORE_API SoftOpRoiCounter : public SoftOpBaseClass
189
  {
190
    DEB_CLASS_NAMESPC(DebModControl,"SoftwareOperation","SoftOpRoiCounter");
191 192 193
  public:
    typedef std::pair<int,std::list<Tasks::RoiCounterResult> > RoiIdAndResults;
    typedef std::pair<int,Roi> RoiIdAndRoi;
194 195 196
    typedef std::pair<std::string,std::list<Tasks::RoiCounterResult> > RoiNameAndResults;
    typedef std::pair<std::string,Roi> RoiNameAndRoi;
    typedef std::pair<std::string,ArcRoi> RoiNameAndArcRoi;
197 198 199
    typedef std::pair<std::string,Tasks::RoiCounterTask*> RoiNameAndTask;
    typedef std::list<RoiNameAndTask> RoiNameAndTaskList;

200
    SoftOpRoiCounter();
201
    virtual ~SoftOpRoiCounter();
202

203 204 205 206 207
    /** Old way to manage roi's counters
     * they are now deprecated, 
     * please use the new set of methods.
     * will be removed in version 2.
     */
208 209 210 211
    void add(const std::list<Roi> &rois); 
    void set(const std::list<Roi> &rois); 
    void get(std::list<Roi>&) const;
    void del(const std::list<int> &roiIds);
212 213 214 215 216 217 218 219 220
    void readCounters(int from,std::list<RoiIdAndResults> &result) const;
    /** New methods set */
    void update(const std::list<RoiNameAndRoi>&);
    void update(const std::list<RoiNameAndArcRoi>&);

    void setLut(const std::string& name,
		const Point& origin,Data &lut);
    void setLutMask(const std::string& name,
		    const Point& origin,Data &lut);
221
    void getTasks(RoiNameAndTaskList&);
222 223 224 225
    // end of new set

    void names(std::list<std::string>& roi_names) const;
    void remove(const std::list<std::string>& names);
226
    void clearAllRoi();		/* clear all roi */
227 228 229 230

    void clearCounterStatus();
    int  getCounterStatus() const;

231 232 233 234 235
    void setMask(Data &aMask);

    void setBufferSize(int size);
    void getBufferSize(int &size) const;
    
236
    void readCounters(int from,std::list<RoiNameAndResults> &result) const;
237
  protected:
238
    virtual bool addTo(TaskMgr&,int stage);
239 240 241
    virtual void prepare();
  private:
    typedef std::pair<Tasks::RoiCounterManager*,Tasks::RoiCounterTask*> ManagerNCounter;
242 243 244 245
    typedef std::map<std::string,ManagerNCounter> Name2ManagerNCounter;

    inline void _get_or_create(const std::string& roi_name,
			       Tasks::RoiCounterManager*&,Tasks::RoiCounterTask*&);
246

247
    Name2ManagerNCounter	m_manager_tasks;
248
    int				m_history_size;
249
    int				m_counter_status;
250
    Data			m_mask;
251
    mutable Cond		m_cond;
252
  };
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
#define ROI_WARNING
#ifdef WIN32
#pragma message ("deprecated use new methods set")
#else
#warning deprecated use new methods set
#endif
  inline void SoftOpRoiCounter::add(const std::list<Roi> &rois)
  {
    ROI_WARNING;
    int nb_rois = m_manager_tasks.size();
    std::list<RoiNameAndRoi> local_rois;
    for(std::list<Roi>::const_iterator i = rois.begin();
	i != rois.end();++i,++nb_rois)
      {
	char buffer[256];
	snprintf(buffer,sizeof(buffer),"roi_%d",nb_rois);
	RoiNameAndRoi roiname_roi(buffer,*i);
	local_rois.push_back(roiname_roi);
      }
    update(local_rois);
  }
  inline void SoftOpRoiCounter::set(const std::list<Roi> &rois)
  {
    clearAllRoi();
    add(rois);
  }
  inline void SoftOpRoiCounter::get(std::list<Roi> &rois) const
  {
    ROI_WARNING;
    DEB_MEMBER_FUNCT();
    std::vector<Roi> v_rois;
    v_rois.resize(m_manager_tasks.size());
    for(Name2ManagerNCounter::const_iterator i = m_manager_tasks.begin();
	i != m_manager_tasks.end();++i)
      {
	int roi_id;
	if(sscanf(i->first.c_str(),"roi_%d",&roi_id) != 1)
	  THROW_CTL_ERROR(InvalidValue) << "You can't mixed old and new methods set";
	int x,y,width,height;
	i->second.second->getRoi(x,y,width,height);
	v_rois[roi_id] = Roi(x,y,width,height);
      }
    for(std::vector<Roi>::iterator i = v_rois.begin();
	i != v_rois.end();++i)
      rois.push_back(*i);
  }
  inline void SoftOpRoiCounter::del(const std::list<int> &roiIds)
  {
    ROI_WARNING;
    std::list<std::string> rois_names;
    for(std::list<int>::const_iterator i = roiIds.begin();
	i != roiIds.end();++i)
      {
	char buffer[256];
	snprintf(buffer,sizeof(buffer),"roi_%d",*i);
	rois_names.push_back(buffer);
      }
    remove(rois_names);
  }
  inline void SoftOpRoiCounter::readCounters(int from,
					     std::list<RoiIdAndResults> &result) const
  {
    ROI_WARNING;
    DEB_MEMBER_FUNCT();
    std::list<RoiNameAndResults> tmp_result;
    readCounters(from,tmp_result);
    for(std::list<RoiNameAndResults>::iterator i = tmp_result.begin();
	i != tmp_result.end();++i)
      {
	int roi_id;
	if(sscanf(i->first.c_str(),"roi_%d",&roi_id) != 1)
	  THROW_CTL_ERROR(InvalidValue) << "You can't mixed old and new methods set";
	result.push_back(RoiIdAndResults(roi_id,i->second));
      }
  }

papillon's avatar
 
papillon committed
329

330
  class LIMACORE_API SoftOpRoi2Spectrum : public SoftOpBaseClass
331 332 333 334 335 336 337 338 339 340 341 342 343
  {
  public:
    typedef std::pair<int,std::list<Tasks::Roi2SpectrumResult> > RoiIdAndResults;
    typedef std::pair<int,Roi> RoiIdAndRoi;
    SoftOpRoi2Spectrum();
    virtual ~SoftOpRoi2Spectrum();

    void add(const std::list<Roi> &rois); 
    void set(const std::list<Roi> &rois); 
    void get(std::list<Roi>&) const;
    void del(const std::list<int> &roiIds);
    void clearAllRoi();		/* clear all roi */

344 345 346
    void getRoiMode(std::list<int>&) const;
    void setRoiMode(int roiId,int mode);

347 348 349 350 351 352 353 354 355 356
    void clearCounterStatus();
    int  getCounterStatus() const;

    // probably needed in future
    //void setMask(Data &aMask);

    void setBufferSize(int size);
    void getBufferSize(int &size) const;
    
    void readCounters(int from,std::list<RoiIdAndResults> &result) const;
357
    void createImage(int roiId,int &from,Data &aData) const;
358
  protected:
359
    virtual bool addTo(TaskMgr&,int stage);
360 361 362 363 364 365 366 367 368 369 370
    virtual void prepare();
  private:
    typedef std::pair<Tasks::Roi2SpectrumManager*,Tasks::Roi2SpectrumTask*> ManagerNCounter;

    std::list<ManagerNCounter>  m_manager_tasks;
    int				m_history_size;
    int				m_counter_status;
    //Data			m_mask;
    mutable Cond		m_cond;
  };

371
  class LIMACORE_API SoftOpSoftRoi : public SoftOpBaseClass
372 373 374 375 376 377 378 379
  {
  public:
    SoftOpSoftRoi();
    virtual ~SoftOpSoftRoi();
    
    void setRoi(int x,int y,int width,int height);
    
  protected:
380
    virtual bool addTo(TaskMgr&,int stage);
381 382 383 384
    virtual void prepare() {};
  private:
    Tasks::SoftRoi *m_opt;
  };
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422

  class SoftPrepareCallbackGen;
  class LIMACORE_API SoftCallback
  {
    friend class SoftPrepareCallbackGen;
  public:
    SoftCallback() : m_callback_gen(NULL) {}
    virtual ~SoftCallback();
  protected:
    virtual void prepare() = 0;
  private:
    SoftPrepareCallbackGen* m_callback_gen;
  };

  class LIMACORE_API SoftPrepareCallbackGen
  {
    DEB_CLASS(DebModControl, "SoftPrepareCallbackGen");
  public:
    SoftPrepareCallbackGen();
    virtual ~SoftPrepareCallbackGen();

    void registerCallback(SoftCallback&);
    void unregisterCallback(SoftCallback&);
  protected:
    virtual void prepare_cb();
  private:
    SoftCallback* m_cb;
  };

  class LIMACORE_API SoftUserLinkTask : public SoftOpBaseClass,
					public SoftPrepareCallbackGen
  {
  public:
    SoftUserLinkTask();
    virtual ~SoftUserLinkTask();

    void setLinkTask(LinkTask*);
  protected:
423
    virtual bool addTo(TaskMgr&,int stage);
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    virtual void prepare() {prepare_cb();}
  private:
    LinkTask*	m_link_task;
  };

  class LIMACORE_API SoftUserSinkTask : public SoftOpBaseClass,
					public SoftPrepareCallbackGen
  {
  public:
    SoftUserSinkTask();
    virtual ~SoftUserSinkTask();

    void setSinkTask(SinkTaskBase*);

  protected:
439
    virtual bool addTo(TaskMgr&,int stage);
440 441 442 443
    virtual void prepare() {prepare_cb();}
  private:
    SinkTaskBase* m_sink_task;
  };
444 445
}
#endif