Visual Servoing Platform  version 3.6.1 under development (2024-04-29)
vpGaussianFilter.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Gaussian filter class
32  */
33 
34 #include <visp3/core/vpConfig.h>
35 
36 #if defined(VISP_HAVE_SIMDLIB)
37 #include <Simd/SimdLib.h>
38 #include <visp3/core/vpGaussianFilter.h>
39 #include <visp3/core/vpImageConvert.h>
40 
41 #ifndef DOXYGEN_SHOULD_SKIP_THIS
42 class vpGaussianFilter::Impl
43 {
44 public:
45  Impl(unsigned int width, unsigned int height, float sigma, bool deinterleave)
46  : m_funcPtrGray(nullptr), m_funcPtrRGBa(nullptr), m_deinterleave(deinterleave)
47  {
48  const float epsilon = 0.001f;
49  {
50  const size_t channels = 1;
51  m_funcPtrGray = SimdGaussianBlurInit(width, height, channels, &sigma, &epsilon);
52  }
53  {
54  const size_t channels = 4;
55  m_funcPtrRGBa = SimdGaussianBlurInit(width, height, channels, &sigma, &epsilon);
56  }
57  if (m_deinterleave) {
58  m_red.resize(height, width);
59  m_green.resize(height, width);
60  m_blue.resize(height, width);
61 
62  m_redBlurred.resize(height, width);
63  m_greenBlurred.resize(height, width);
64  m_blueBlurred.resize(height, width);
65  }
66  }
67 
68  ~Impl()
69  {
70  if (m_funcPtrGray) {
71  SimdRelease(m_funcPtrGray);
72  }
73 
74  if (m_funcPtrRGBa) {
75  SimdRelease(m_funcPtrRGBa);
76  }
77  }
78 
80  {
81  I_blur.resize(I.getHeight(), I.getWidth());
82  SimdGaussianBlurRun(m_funcPtrGray, I.bitmap, I.getWidth(), I_blur.bitmap, I_blur.getWidth());
83  }
84 
85  void apply(const vpImage<vpRGBa> &I, vpImage<vpRGBa> &I_blur)
86  {
87  I_blur.resize(I.getHeight(), I.getWidth());
88  if (!m_deinterleave) {
89  SimdGaussianBlurRun(m_funcPtrRGBa, reinterpret_cast<unsigned char *>(I.bitmap), I.getWidth() * 4,
90  reinterpret_cast<unsigned char *>(I_blur.bitmap), I_blur.getWidth() * 4);
91  }
92  else {
93  vpImageConvert::split(I, &m_red, &m_green, &m_blue);
94  SimdGaussianBlurRun(m_funcPtrGray, m_red.bitmap, m_red.getWidth(), m_redBlurred.bitmap, m_redBlurred.getWidth());
95  SimdGaussianBlurRun(m_funcPtrGray, m_green.bitmap, m_green.getWidth(), m_greenBlurred.bitmap,
96  m_greenBlurred.getWidth());
97  SimdGaussianBlurRun(m_funcPtrGray, m_blue.bitmap, m_blue.getWidth(), m_blueBlurred.bitmap,
98  m_blueBlurred.getWidth());
99 
100  vpImageConvert::merge(&m_redBlurred, &m_greenBlurred, &m_blueBlurred, nullptr, I_blur);
101  }
102  }
103 
104 protected:
105  void *m_funcPtrGray;
106  void *m_funcPtrRGBa;
107  bool m_deinterleave;
109  vpImage<unsigned char> m_green;
110  vpImage<unsigned char> m_blue;
111  vpImage<unsigned char> m_redBlurred;
112  vpImage<unsigned char> m_greenBlurred;
113  vpImage<unsigned char> m_blueBlurred;
114 };
115 #endif // DOXYGEN_SHOULD_SKIP_THIS
116 
127 vpGaussianFilter::vpGaussianFilter(unsigned int width, unsigned int height, float sigma, bool deinterleave)
128  : m_impl(new Impl(width, height, sigma, deinterleave))
129 { }
130 
132 
140 {
141  m_impl->apply(I, I_blur);
142 }
143 
150 void vpGaussianFilter::apply(const vpImage<vpRGBa> &I, vpImage<vpRGBa> &I_blur) { m_impl->apply(I, I_blur); }
151 
152 #elif !defined(VISP_BUILD_SHARED_LIBS)
153  // Work around to avoid warning: libvisp_core.a(vpGaussianFilter.cpp.o) has no symbols
154 void dummy_vpGaussianFilter() { };
155 
156 #endif
void apply(const vpImage< unsigned char > &I, vpImage< unsigned char > &I_blur)
vpGaussianFilter(unsigned int width, unsigned int height, float sigma, bool deinterleave=false)
static void merge(const vpImage< unsigned char > *R, const vpImage< unsigned char > *G, const vpImage< unsigned char > *B, const vpImage< unsigned char > *a, vpImage< vpRGBa > &RGBa)
static void split(const vpImage< vpRGBa > &src, vpImage< unsigned char > *pR, vpImage< unsigned char > *pG, vpImage< unsigned char > *pB, vpImage< unsigned char > *pa=nullptr)
unsigned int getWidth() const
Definition: vpImage.h:245
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Definition: vpImage.h:783
Type * bitmap
points toward the bitmap
Definition: vpImage.h:139
unsigned int getHeight() const
Definition: vpImage.h:184