Home | History | Annotate | Download | only in IlmImf
      1 ///////////////////////////////////////////////////////////////////////////
      2 //
      3 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
      4 // Digital Ltd. LLC
      5 //
      6 // All rights reserved.
      7 //
      8 // Redistribution and use in source and binary forms, with or without
      9 // modification, are permitted provided that the following conditions are
     10 // met:
     11 // *       Redistributions of source code must retain the above copyright
     12 // notice, this list of conditions and the following disclaimer.
     13 // *       Redistributions in binary form must reproduce the above
     14 // copyright notice, this list of conditions and the following disclaimer
     15 // in the documentation and/or other materials provided with the
     16 // distribution.
     17 // *       Neither the name of Industrial Light & Magic nor the names of
     18 // its contributors may be used to endorse or promote products derived
     19 // from this software without specific prior written permission.
     20 //
     21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32 //
     33 ///////////////////////////////////////////////////////////////////////////
     34 
     35 
     36 
     37 //-----------------------------------------------------------------------------
     38 //
     39 //	class Channel
     40 //	class ChannelList
     41 //
     42 //-----------------------------------------------------------------------------
     43 
     44 #include <ImfChannelList.h>
     45 #include <Iex.h>
     46 
     47 
     48 using std::string;
     49 using std::set;
     50 
     51 namespace Imf {
     52 
     53 
     54 Channel::Channel (PixelType t, int xs, int ys, bool pl):
     55     type (t),
     56     xSampling (xs),
     57     ySampling (ys),
     58     pLinear (pl)
     59 {
     60     // empty
     61 }
     62 
     63 
     64 bool
     65 Channel::operator == (const Channel &other) const
     66 {
     67     return type == other.type &&
     68        xSampling == other.xSampling &&
     69        ySampling == other.ySampling &&
     70        pLinear == other.pLinear;
     71 }
     72 
     73 
     74 void
     75 ChannelList::insert (const char name[], const Channel &channel)
     76 {
     77     if (name[0] == 0)
     78     THROW (Iex::ArgExc, "Image channel name cannot be an empty string.");
     79 
     80     _map[name] = channel;
     81 }
     82 
     83 
     84 void
     85 ChannelList::insert (const string &name, const Channel &channel)
     86 {
     87     insert (name.c_str(), channel);
     88 }
     89 
     90 
     91 Channel &
     92 ChannelList::operator [] (const char name[])
     93 {
     94     ChannelMap::iterator i = _map.find (name);
     95 
     96     if (i == _map.end())
     97     THROW (Iex::ArgExc, "Cannot find image channel \"" << name << "\".");
     98 
     99     return i->second;
    100 }
    101 
    102 
    103 const Channel &
    104 ChannelList::operator [] (const char name[]) const
    105 {
    106     ChannelMap::const_iterator i = _map.find (name);
    107 
    108     if (i == _map.end())
    109     THROW (Iex::ArgExc, "Cannot find image channel \"" << name << "\".");
    110 
    111     return i->second;
    112 }
    113 
    114 
    115 Channel &
    116 ChannelList::operator [] (const string &name)
    117 {
    118     return this->operator[] (name.c_str());
    119 }
    120 
    121 
    122 const Channel &
    123 ChannelList::operator [] (const string &name) const
    124 {
    125     return this->operator[] (name.c_str());
    126 }
    127 
    128 
    129 Channel *
    130 ChannelList::findChannel (const char name[])
    131 {
    132     ChannelMap::iterator i = _map.find (name);
    133     return (i == _map.end())? 0: &i->second;
    134 }
    135 
    136 
    137 const Channel *
    138 ChannelList::findChannel (const char name[]) const
    139 {
    140     ChannelMap::const_iterator i = _map.find (name);
    141     return (i == _map.end())? 0: &i->second;
    142 }
    143 
    144 
    145 Channel *
    146 ChannelList::findChannel (const string &name)
    147 {
    148     return findChannel (name.c_str());
    149 }
    150 
    151 
    152 const Channel *
    153 ChannelList::findChannel (const string &name) const
    154 {
    155     return findChannel (name.c_str());
    156 }
    157 
    158 
    159 ChannelList::Iterator
    160 ChannelList::begin ()
    161 {
    162     return _map.begin();
    163 }
    164 
    165 
    166 ChannelList::ConstIterator
    167 ChannelList::begin () const
    168 {
    169     return _map.begin();
    170 }
    171 
    172 
    173 ChannelList::Iterator
    174 ChannelList::end ()
    175 {
    176     return _map.end();
    177 }
    178 
    179 
    180 ChannelList::ConstIterator
    181 ChannelList::end () const
    182 {
    183     return _map.end();
    184 }
    185 
    186 
    187 ChannelList::Iterator
    188 ChannelList::find (const char name[])
    189 {
    190     return _map.find (name);
    191 }
    192 
    193 
    194 ChannelList::ConstIterator
    195 ChannelList::find (const char name[]) const
    196 {
    197     return _map.find (name);
    198 }
    199 
    200 
    201 ChannelList::Iterator
    202 ChannelList::find (const string &name)
    203 {
    204     return find (name.c_str());
    205 }
    206 
    207 
    208 ChannelList::ConstIterator
    209 ChannelList::find (const string &name) const
    210 {
    211     return find (name.c_str());
    212 }
    213 
    214 
    215 void
    216 ChannelList::layers (set <string> &layerNames) const
    217 {
    218     layerNames.clear();
    219 
    220     for (ConstIterator i = begin(); i != end(); ++i)
    221     {
    222     string layerName = i.name();
    223     size_t pos = layerName.rfind ('.');
    224 
    225     if (pos != string::npos && pos != 0 && pos + 1 < layerName.size())
    226     {
    227         layerName.erase (pos);
    228         layerNames.insert (layerName);
    229     }
    230     }
    231 }
    232 
    233 
    234 void
    235 ChannelList::channelsInLayer (const string &layerName,
    236                   Iterator &first,
    237                   Iterator &last)
    238 {
    239     channelsWithPrefix (layerName + '.', first, last);
    240 }
    241 
    242 
    243 void
    244 ChannelList::channelsInLayer (const string &layerName,
    245                   ConstIterator &first,
    246                   ConstIterator &last) const
    247 {
    248     channelsWithPrefix (layerName + '.', first, last);
    249 }
    250 
    251 
    252 void
    253 ChannelList::channelsWithPrefix (const char prefix[],
    254                  Iterator &first,
    255                  Iterator &last)
    256 {
    257     first = last = _map.lower_bound (prefix);
    258     int n = strlen (prefix);
    259 
    260     while (last != Iterator (_map.end()) &&
    261        strncmp (last.name(), prefix, n) <= 0)
    262     {
    263     ++last;
    264     }
    265 }
    266 
    267 
    268 void
    269 ChannelList::channelsWithPrefix (const char prefix[],
    270                  ConstIterator &first,
    271                  ConstIterator &last) const
    272 {
    273     first = last = _map.lower_bound (prefix);
    274     int n = strlen (prefix);
    275 
    276     while (last != ConstIterator (_map.end()) &&
    277        strncmp (last.name(), prefix, n) <= 0)
    278     {
    279     ++last;
    280     }
    281 }
    282 
    283 
    284 void
    285 ChannelList::channelsWithPrefix (const string &prefix,
    286                  Iterator &first,
    287                  Iterator &last)
    288 {
    289     return channelsWithPrefix (prefix.c_str(), first, last);
    290 }
    291 
    292 
    293 void
    294 ChannelList::channelsWithPrefix (const string &prefix,
    295                  ConstIterator &first,
    296                  ConstIterator &last) const
    297 {
    298     return channelsWithPrefix (prefix.c_str(), first, last);
    299 }
    300 
    301 
    302 bool
    303 ChannelList::operator == (const ChannelList &other) const
    304 {
    305     ConstIterator i = begin();
    306     ConstIterator j = other.begin();
    307 
    308     while (i != end() && j != other.end())
    309     {
    310     if (!(i.channel() == j.channel()))
    311         return false;
    312 
    313     ++i;
    314     ++j;
    315     }
    316 
    317     return i == end() && j == other.end();
    318 }
    319 
    320 
    321 } // namespace Imf
    322