PCL
FilterManager.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.8.5
6 // ----------------------------------------------------------------------------
7 // pcl/FilterManager.h - Released 2024-12-28T16:53:48Z
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_FilterManager_h
53 #define __PCL_FilterManager_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/File.h>
61 #include <pcl/StringList.h>
62 
63 namespace pcl
64 {
65 
66 // ----------------------------------------------------------------------------
67 
72 struct PCL_CLASS SampledSpectrumData
73 {
74  String name;
75  String channel; // W,L,R,G,B,Q or empty=unassigned
76  String defaultItem; // channel this should be used as default (W,L,R,G,B,Q)
77  String reference; // published source of the data
78  String data; // CSV: <wavelength_1>,<value_1>, ...,<wavelength_n>,<value_n>
79 
80  bool IsValid() const
81  {
82  return !name.IsEmpty() && !data.IsEmpty();
83  }
84 
85  bool operator ==( const SampledSpectrumData& other ) const
86  {
87  return name.CompareIC( other.name ) == 0;
88  }
89 
90  bool operator <( const SampledSpectrumData& other ) const
91  {
92  return name.CompareIC( other.name ) < 0;
93  }
94 };
95 
96 // ----------------------------------------------------------------------------
97 
102 class PCL_CLASS SampledSpectrumDataList
103 {
104 public:
105 
106  using list_type = Array<SampledSpectrumData>;
107  using iterator = list_type::iterator;
108  using const_iterator = list_type::const_iterator;
109 
110  SampledSpectrumDataList() = default;
111  SampledSpectrumDataList( const SampledSpectrumDataList& ) = default;
112 
113  int Length() const
114  {
115  return int( m_list.Length() );
116  }
117 
118  bool IsEmpty() const
119  {
120  return m_list.IsEmpty();
121  }
122 
123  int Find( const String& name ) const
124  {
125  if ( m_sorted )
126  {
127  const_iterator i = BinarySearch( Begin(), End(), SampledSpectrumData{ name, String(), String(), String() } );
128  if ( i != End() )
129  return int( i - Begin() );
130  }
131  else
132  {
133  for ( int i = 0, n = Length(); i < n; ++i )
134  if ( m_list[i].name == name )
135  return i;
136  }
137  return -1;
138  }
139 
140  int Find( const SampledSpectrumData& data ) const
141  {
142  return Find( data.name );
143  }
144 
145  bool Has( const String& name ) const
146  {
147  return Find( name ) >= 0;
148  }
149 
150  int Has( const SampledSpectrumData& data ) const
151  {
152  return Find( data ) >= 0;
153  }
154 
155  iterator Begin()
156  {
157  return m_list.Begin();
158  }
159 
160  const_iterator Begin() const
161  {
162  return m_list.Begin();
163  }
164 
165  iterator begin()
166  {
167  return Begin();
168  }
169 
170  const_iterator begin() const
171  {
172  return Begin();
173  }
174 
175  iterator End()
176  {
177  return m_list.End();
178  }
179 
180  const_iterator End() const
181  {
182  return m_list.End();
183  }
184 
185  iterator end()
186  {
187  return End();
188  }
189 
190  const_iterator end() const
191  {
192  return End();
193  }
194 
195  const SampledSpectrumData& operator[]( int i ) const
196  {
197  return m_list[i];
198  }
199 
200  SampledSpectrumData& operator[]( int i )
201  {
202  return m_list[i];
203  }
204 
205  const SampledSpectrumData& First() const
206  {
207  PCL_PRECONDITION( !IsEmpty() )
208  return *m_list.Begin();
209  }
210 
211  const SampledSpectrumData& Last() const
212  {
213  PCL_PRECONDITION( !IsEmpty() )
214  return *m_list.ReverseBegin();
215  }
216 
217  void Set( const SampledSpectrumData& s )
218  {
219  if ( m_list.IsEmpty() )
220  {
221  m_list << s;
222  m_sorted = true;
223  }
224  else
225  {
226  int i = Find( s.name );
227  if ( i < 0 )
228  {
229  m_sorted = m_sorted && s > Last();
230  m_list << s;
231  }
232  else
233  m_list[i] = s;
234  }
235  }
236 
237  SampledSpectrumDataList& operator <<( const SampledSpectrumData& s )
238  {
239  Set( s );
240  return *this;
241  }
242 
243  bool IsSorted() const
244  {
245  return m_sorted;
246  }
247 
248  void Sort()
249  {
250  if ( !m_sorted )
251  {
252  m_list.Sort();
253  m_sorted = true;
254  }
255  }
256 
257  void Clear()
258  {
259  m_list.Clear();
260  m_sorted = false;
261  }
262 
263  String UniqueItemName( const String& name )
264  {
265  String uniqueName = name;
266  for ( unsigned i = 0;; )
267  {
268  if ( !Find( uniqueName ) )
269  break;
270  uniqueName = name + String().Format( " (%u)", ++i );
271  }
272  return uniqueName;
273  }
274 
275 private:
276 
277  list_type m_list;
278  bool m_sorted = false;
279 };
280 
281 // ----------------------------------------------------------------------------
282 
287 class PCL_CLASS FilterManager
288 {
289 public:
290 
294  FilterManager() = default;
295 
299  FilterManager( const String& filtersDatabaseFilePath,
300  const String& whiteReferencesDatabaseFilePath = String() )
301  {
302  Initialize( filtersDatabaseFilePath, whiteReferencesDatabaseFilePath );
303  }
304 
308  FilterManager( const FilterManager& ) = default;
309 
313  FilterManager( FilterManager&& ) = default;
314 
318  FilterManager& operator =( const FilterManager& ) = default;
319 
323  FilterManager& operator =( FilterManager&& ) = default;
324 
328  virtual ~FilterManager()
329  {
330  }
331 
332  bool Initialize( const String& filtersDatabaseFilePath = String(),
333  const String& whiteReferencesDatabaseFilePath = String() );
334 
335  bool ImportFilters( const String& filePath, bool merge = true );
336  void ImportFilters( const SampledSpectrumDataList& filters, bool merge = true );
337 
338  bool ImportWhiteReferences( const String& filePath, bool merge = true );
339  void ImportWhiteReferences( const SampledSpectrumDataList& whiteReferences, bool merge = true );
340 
341  void ReloadFilters();
342  void ReloadWhiteReferences();
343 
344  bool IsValid() const
345  {
346  return m_valid;
347  }
348 
349  bool IsModified() const
350  {
351  return m_modified != 0;
352  }
353 
354  const SampledSpectrumDataList& Filters() const
355  {
356  return m_filters;
357  }
358 
359  const SampledSpectrumData& DefaultLuminanceFilter() const
360  {
361  return m_defaultLuminanceFilter;
362  }
363 
364  const SampledSpectrumData& DefaultRedFilter() const
365  {
366  return m_defaultRedFilter;
367  }
368 
369  const SampledSpectrumData& DefaultGreenFilter() const
370  {
371  return m_defaultGreenFilter;
372  }
373 
374  const SampledSpectrumData& DefaultBlueFilter() const
375  {
376  return m_defaultBlueFilter;
377  }
378 
379  const SampledSpectrumData& DefaultDeviceQE() const
380  {
381  return m_defaultDeviceQE;
382  }
383 
384  const SampledSpectrumDataList& WhiteReferences() const
385  {
386  return m_whiteReferences;
387  }
388 
389  const SampledSpectrumData& DefaultWhiteReference() const
390  {
391  return m_defaultWhiteReference;
392  }
393 
394  String FiltersDatabaseFilePath() const
395  {
396  return m_filtersDatabaseFilePath.IsEmpty() ? DefaultFiltersDatabaseFilePath() : m_filtersDatabaseFilePath;
397  }
398 
399  String WhiteReferencesDatabaseFilePath() const
400  {
401  return m_whiteReferencesDatabaseFilePath.IsEmpty() ? DefaultWhiteReferencesDatabaseFilePath() : m_whiteReferencesDatabaseFilePath;
402  }
403 
404  static String ConfigurationFiltersDatabaseFilePath();
405  static String ConfigurationWhiteReferencesDatabaseFilePath();
406 
407  static String DefaultFiltersDatabaseFilePath();
408  static String DefaultWhiteReferencesDatabaseFilePath();
409 
410  static String FactoryDefaultFiltersDatabaseFilePath();
411  static String FactoryDefaultWhiteReferencesDatabaseFilePath();
412 
413  bool UsingDefaultFiltersDatabase() const
414  {
415  return m_filtersDatabaseFilePath.IsEmpty() || m_filtersDatabaseFilePath == DefaultFiltersDatabaseFilePath();
416  }
417 
418  bool UsingDefaultWhiteReferencesDatabase() const
419  {
420  return m_whiteReferencesDatabaseFilePath.IsEmpty() || m_whiteReferencesDatabaseFilePath == DefaultWhiteReferencesDatabaseFilePath();
421  }
422 
423  bool UsingConfigurationFiltersDatabaseFilePath() const
424  {
425  return m_filtersDatabaseFilePath == ConfigurationFiltersDatabaseFilePath();
426  }
427 
428  bool UsingConfigurationWhiteReferencesDatabaseFilePath() const
429  {
430  return m_whiteReferencesDatabaseFilePath == ConfigurationWhiteReferencesDatabaseFilePath();
431  }
432 
433  int FindFilterByName( const String& name ) const
434  {
435  return m_filters.Find( name );
436  }
437 
438  int FindWhiteReferenceByName( const String& name ) const
439  {
440  return m_whiteReferences.Find( name );
441  }
442 
443  void SaveToConfigurationFiltersDatabaseFile();
444  void SaveToConfigurationWhiteReferencesDatabaseFile();
445 
446  static SampledSpectrumDataList LoadFilters( const String& filePath );
447  static SampledSpectrumDataList LoadWhiteReferences( const String& filePath );
448 
449  static void ExportFiltersDatabase( const String& filePath, const SampledSpectrumDataList& filters );
450  static void ExportWhiteReferencesDatabase( const String& filePath, const SampledSpectrumDataList& whiteReferences );
451 
452  static SampledSpectrumData DecodeFilterCSV( const IsoStringList& lines );
453  static SampledSpectrumData DecodeWhiteReferenceCSV( const IsoStringList& lines );
454 
455  static IsoStringList EncodeFilterCSV( const SampledSpectrumData& filter );
456  static IsoStringList EncodeWhiteReferenceCSV( const SampledSpectrumData& whiteReference );
457 
458 private:
459 
460  String m_filtersDatabaseFilePath;
461  SampledSpectrumDataList m_filters;
462  SampledSpectrumData m_defaultLuminanceFilter;
463  SampledSpectrumData m_defaultRedFilter;
464  SampledSpectrumData m_defaultGreenFilter;
465  SampledSpectrumData m_defaultBlueFilter;
466  SampledSpectrumData m_defaultDeviceQE;
467  String m_whiteReferencesDatabaseFilePath;
468  SampledSpectrumDataList m_whiteReferences;
469  SampledSpectrumData m_defaultWhiteReference;
470  bool m_initialized = false;
471  bool m_valid = false;
472  int m_modified = 0;
473 };
474 
475 // ----------------------------------------------------------------------------
476 
477 } // pcl
478 
479 #endif // __PCL_FilterManager_h
480 
481 // ----------------------------------------------------------------------------
482 // EOF pcl/FilterManager.h - Released 2024-12-28T16:53:48Z
Dynamic list of 8-bit strings.
Array< T, A > & operator<<(Array< T, A > &x, const V &v)
Definition: Array.h:2313
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2285
bool operator<(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2296
FI BinarySearch(FI i, FI j, const T &v) noexcept
Definition: Search.h:170
void Sort(BI i, BI j)
Definition: Sort.h:520
PCL root namespace.
Definition: AbstractImage.h:77