1 /* -*-c++-*- */
2 /* osgEarth - Geospatial SDK for OpenSceneGraph
3  * Copyright 2019 Pelican Mapping
4  * http://osgearth.org
5  *
6  * osgEarth is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>
18  */
19 #include <osgEarth/ColorFilter>
20 #include <osgEarth/ThreadingUtils>
21 
22 using namespace osgEarth;
23 
24 ColorFilterRegistry*
instance()25 ColorFilterRegistry::instance()
26 {
27     // OK to be in the local scope since this gets called at static init time
28     // by the OSGEARTH_REGISTER_COLORFILTER macro
29     static ColorFilterRegistry* s_singleton =0L;
30     static Threading::Mutex     s_singletonMutex;
31 
32     if ( !s_singleton )
33     {
34         Threading::ScopedMutexLock lock(s_singletonMutex);
35         if ( !s_singleton )
36         {
37             s_singleton = new ColorFilterRegistry();
38         }
39     }
40     return s_singleton;
41 }
42 
43 bool
readChain(const Config & conf,ColorFilterChain & out_chain)44 ColorFilterRegistry::readChain(const Config& conf, ColorFilterChain& out_chain)
45 {
46     bool createdAtLeastOne = false;
47 
48     // first try to parse the top-level config:
49     ColorFilter* top = createOne( conf );
50     if ( top )
51     {
52         out_chain.push_back( top );
53         createdAtLeastOne = true;
54     }
55 
56     // failing that, treat it like a chain:
57     else
58     {
59         for( ConfigSet::const_iterator i = conf.children().begin(); i != conf.children().end(); ++i )
60         {
61             ColorFilter* object = createOne( *i );
62             if ( object )
63             {
64                 out_chain.push_back( object );
65                 createdAtLeastOne = true;
66             }
67         }
68     }
69 
70     return createdAtLeastOne;
71 }
72 
73 
74 bool
writeChain(const ColorFilterChain & chain,Config & out_config)75 ColorFilterRegistry::writeChain(const ColorFilterChain& chain, Config& out_config)
76 {
77     bool wroteAtLeastOne = false;
78 
79     for( ColorFilterChain::const_iterator i = chain.begin(); i != chain.end(); ++i )
80     {
81         Config conf = i->get()->getConfig();
82         if ( !conf.empty() )
83         {
84             out_config.add( conf );
85             wroteAtLeastOne = true;
86         }
87     }
88 
89     return wroteAtLeastOne;
90 }
91 
92 
93 void
add(const std::string & key,class ColorFilterFactory * factory)94 ColorFilterRegistry::add( const std::string& key, class ColorFilterFactory* factory )
95 {
96     if ( factory )
97         _factories[key] = factory;
98 }
99 
100 ColorFilter*
createOne(const Config & conf) const101 ColorFilterRegistry::createOne(const Config& conf) const
102 {
103     FactoryMap::const_iterator f = _factories.find( conf.key() );
104     if ( f != _factories.end() && f->second != 0L )
105     {
106         ColorFilter* object = f->second->create(conf);
107         if ( object )
108         {
109             return object;
110         }
111     }
112     return 0L;
113 }
114