SMIL  0.9.1
SampleModule.hpp
1 /*
2  * Copyright (c) 2011-2016, Matthieu FAESSEL and ARMINES
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Matthieu FAESSEL, or ARMINES nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 
30 #ifndef _SAMPLE_MODULE_HPP
31 #define _SAMPLE_MODULE_HPP
32 
33 #include "Core/include/DCore.h"
34 #include "Morpho/include/DMorpho.h"
35 
36 #include <unistd.h> // For usleep
37 
38 
39 namespace smil
40 {
41  // Sample inv function
42  template <class T>
43  RES_T samplePixelFunction(const Image<T> &imIn, Image<T> &imOut)
44  {
45  ASSERT_ALLOCATED(&imIn)
46  ASSERT_SAME_SIZE(&imIn, &imOut)
47 
48  ImageFreezer freeze(imOut);
49 
50  typename Image<T>::lineType pixelsIn = imIn.getPixels();
51  typename Image<T>::lineType pixelsOut = imOut.getPixels();
52 
53  for (size_t i=0;i<imIn.getPixelCount();i++)
54  pixelsOut[i] = ImDtTypes<T>::max() - pixelsIn[i];
55 
56  return RES_OK;
57  }
58 
59 
60  // Sample Morpho functor
61  template <class T>
63  {
64  virtual inline void processPixel(size_t pointOffset, vector<int> &dOffsets)
65  {
66  double pixSum = 0;
67 
68  for (vector<int>::iterator it=dOffsets.begin();it!=dOffsets.end();it++)
69  pixSum += this->pixelsIn[ pointOffset + *it ];
70 
71  this->pixelsOut[ pointOffset ] = T( pixSum / dOffsets.size() );
72 
73  }
74  };
75 
76  template <class T>
77  RES_T sampleMorphoFunction(const Image<T> &imIn, Image<T> &imOut, const StrElt &se=DEFAULT_SE)
78  {
80  return func(imIn, imOut, se);
81  }
82 
83 
84 
85  // Sample (silly) Flooding functor
86  template <class T, class labelT>
87  struct SampleFloodingFunctor: public BaseFlooding<T, labelT>
88  {
89  virtual RES_T initialize(const Image<T> &imIn, Image<labelT> &imLbl, const StrElt &se)
90  {
91  BaseFlooding<T, labelT>::initialize(imIn, imLbl, se);
92  this->imgLbl->updatesEnabled = true; // Enable image repaint
93  }
94 
95  virtual inline void processPixel(const size_t &curOffset)
96  {
97  this->lblPixels[ curOffset ] = UINT16(255);
98 
100 
101  this->imgLbl->modified(); // Trigger image repaint
102  Gui::processEvents(); // Refresh display
103 
104  usleep(1000);
105  }
106  };
107 
108  template <class T>
109  RES_T sampleFloodingFunction(const Image<T> &imIn, Image<T> &imOut, const StrElt &se=DEFAULT_SE)
110  {
112  fill(imOut, T(0));
113  size_t pixNbr = imOut.getPixelCount();
114  // Put two "random" markers
115  imOut.setPixel(pixNbr/3, 1);
116  imOut.setPixel(pixNbr*2/3, 2);
117  return func.flood(imIn, imOut, imOut, se);
118  }
119 
120 
121 }
122 
123 #endif // _SAMPLE_MODULE_HPP
Definition: DColorConvert.h:38
Definition: SampleModule.hpp:87
RES_T fill(Image< T > &imOut, const T &value)
Fill an image with a given value.
Definition: DImageArith.hpp:62
Base structuring element.
Definition: DStructuringElement.h:51
virtual void modified()
Trigger modified event (allows to force display update)
Definition: DImage.hxx:223
Base morpho operator class.
Definition: DMorphImageOperations.hpp:54
Definition: SampleModule.hpp:62
size_t getPixelCount() const
Get the number of pixels.
Definition: DBaseImage.h:150
RES_T setPixel(size_t x, size_t y, size_t z, const T &value)
Set the value of the pixel at pos x,y,z (for 3D image)
Definition: DImage.hpp:145
Main Image class.
Definition: DQVtkViewer.hpp:44
Definition: DMorphoWatershed.hpp:50