PCL
ColorFilterArray.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.6.5
6 // ----------------------------------------------------------------------------
7 // pcl/ColorFilterArray.h - Released 2024-01-13T15:47:58Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_ColorFilterArray_h
53 #define __PCL_ColorFilterArray_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/Exception.h>
61 #include <pcl/String.h>
62 
63 namespace pcl
64 {
65 
66 // ----------------------------------------------------------------------------
67 
82 class PCL_CLASS ColorFilterArray
83 {
84 public:
85 
116  ColorFilterArray( const IsoString& pattern, int width, int height, const String& name = String() )
117  : m_pattern( pattern.Uppercase() )
118  , m_width( width )
119  , m_height( height )
120  , m_name( name )
121  {
122  if ( m_width < 1 || m_height < 1 || size_type( NumberOfElements() ) != m_pattern.Length() )
123  throw Error( "Malformed CFA pattern" );
124  for ( auto element : m_pattern )
125  if ( !IsValidCFAElement( element ) )
126  throw Error( "Invalid CFA pattern \'" + m_pattern + "\'" );
127  }
128 
133  ColorFilterArray() = default;
134 
138  ColorFilterArray( const ColorFilterArray& ) = default;
139 
143  ColorFilterArray( ColorFilterArray&& ) = default;
144 
148  ColorFilterArray& operator =( const ColorFilterArray& ) = default;
149 
153  ColorFilterArray& operator =( ColorFilterArray&& ) = default;
154 
158  const IsoString& Pattern() const
159  {
160  return m_pattern;
161  }
162 
166  int Width() const
167  {
168  return m_width;
169  }
170 
174  int Height() const
175  {
176  return m_height;
177  }
178 
182  int NumberOfElements() const
183  {
184  return int( m_pattern.Length() );
185  }
186 
191  bool IsEmpty() const
192  {
193  return m_pattern.IsEmpty();
194  }
195 
199  const String& Name() const
200  {
201  return m_name;
202  }
203 
208  bool IsBayerFilter() const
209  {
210  return m_pattern == "BGGR" || m_pattern == "GRBG" || m_pattern == "GBRG" || m_pattern == "RGGB";
211  }
212 
220  char Element( int x, int y ) const
221  {
222  size_type i = y*m_width + x;
223  if ( i < m_pattern.Length() )
224  return m_pattern[i];
225  return '\0';
226  }
227 
228  /*
229  * Clears all internal structures to yield an empty CFA.
230  */
231  void Clear()
232  {
233  m_pattern.Clear();
234  m_width = m_height = 0;
235  m_name.Clear();
236  }
237 
242  bool operator ==( const ColorFilterArray& x ) const
243  {
244  return m_pattern == x.m_pattern && m_width == x.m_width && m_height == x.m_height;
245  }
246 
252  bool operator <( const ColorFilterArray& x ) const
253  {
254  return m_pattern < x.m_pattern;
255  }
256 
262  static bool IsValidCFAElement( char element )
263  {
264  return strchr( "0RGBWCMY", element ) != nullptr;
265  }
266 
267 private:
268 
269  IsoString m_pattern;
270  int m_width = 0;
271  int m_height = 0;
272  String m_name;
273 };
274 
275 // ----------------------------------------------------------------------------
276 
283 class PCL_CLASS BayerFilterRGGB : public ColorFilterArray
284 {
285 public:
286 
288  : ColorFilterArray( "RGGB", 2, 2, "RGGB Bayer Filter" )
289  {
290  }
291 };
292 
299 class PCL_CLASS BayerFilterBGGR : public ColorFilterArray
300 {
301 public:
302 
304  : ColorFilterArray( "BGGR", 2, 2, "BGGR Bayer Filter" )
305  {
306  }
307 };
308 
315 class PCL_CLASS BayerFilterGBRG : public ColorFilterArray
316 {
317 public:
318 
320  : ColorFilterArray( "GBRG", 2, 2, "GBRG Bayer Filter" )
321  {
322  }
323 };
324 
331 class PCL_CLASS BayerFilterGRBG : public ColorFilterArray
332 {
333 public:
334 
336  : ColorFilterArray( "GRBG", 2, 2, "GRBG Bayer Filter" )
337  {
338  }
339 };
340 
341 // ----------------------------------------------------------------------------
342 
343 } // pcl
344 
345 #endif // __PCL_ColorFilterArray_h
346 
347 // ----------------------------------------------------------------------------
348 // EOF pcl/ColorFilterArray.h - Released 2024-01-13T15:47:58Z
pcl::ColorFilterArray::Element
char Element(int x, int y) const
Definition: ColorFilterArray.h:220
pcl
PCL root namespace.
Definition: AbstractImage.h:76
pcl::ColorFilterArray
Color filter array (CFA) structure.
Definition: ColorFilterArray.h:82
pcl::BayerFilterGBRG
GBRG Bayer filter representation.
Definition: ColorFilterArray.h:315
pcl::String
Unicode (UTF-16) string.
Definition: String.h:8112
pcl::operator==
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2090
pcl::ColorFilterArray::Pattern
const IsoString & Pattern() const
Definition: ColorFilterArray.h:158
pcl::IsoString
Eight-bit string (ISO/IEC-8859-1 or UTF-8 string)
Definition: String.h:5424
pcl::BayerFilterGRBG
GRBG Bayer filter representation.
Definition: ColorFilterArray.h:331
pcl::ColorFilterArray::IsBayerFilter
bool IsBayerFilter() const
Definition: ColorFilterArray.h:208
Exception.h
pcl::size_type
size_t size_type
Definition: Defs.h:612
pcl::ColorFilterArray::NumberOfElements
int NumberOfElements() const
Definition: ColorFilterArray.h:182
pcl::Error
A simple exception with an associated error message.
Definition: Exception.h:238
pcl::ColorFilterArray::Height
int Height() const
Definition: ColorFilterArray.h:174
pcl::ColorFilterArray::Name
const String & Name() const
Definition: ColorFilterArray.h:199
pcl::ColorFilterArray::Width
int Width() const
Definition: ColorFilterArray.h:166
pcl::BayerFilterBGGR
BGGR Bayer filter representation.
Definition: ColorFilterArray.h:299
pcl::BayerFilterRGGB
RGGB Bayer filter representation.
Definition: ColorFilterArray.h:283
String.h
pcl::ColorFilterArray::IsValidCFAElement
static bool IsValidCFAElement(char element)
Definition: ColorFilterArray.h:262
Defs.h
pcl::ColorFilterArray::IsEmpty
bool IsEmpty() const
Definition: ColorFilterArray.h:191
pcl::ColorFilterArray::ColorFilterArray
ColorFilterArray(const IsoString &pattern, int width, int height, const String &name=String())
Definition: ColorFilterArray.h:116
pcl::operator<
bool operator<(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2101