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 ChannelListAttribute
     40 //
     41 //-----------------------------------------------------------------------------
     42 
     43 #include <ImfChannelListAttribute.h>
     44 
     45 
     46 namespace Imf {
     47 
     48 namespace {
     49 
     50 template <size_t N>
     51 void checkIsNullTerminated (const char (&str)[N], const char *what)
     52 {
     53     for (int i = 0; i < N; ++i) {
     54         if (str[i] == '\0')
     55             return;
     56    }
     57     std::stringstream s;
     58     s << "Invalid " << what << ": it is more than " << (N - 1)
     59       << " characters long.";
     60     throw Iex::InputExc(s);
     61 }
     62 
     63 } // namespace
     64 
     65 template <>
     66 const char *
     67 ChannelListAttribute::staticTypeName ()
     68 {
     69     return "chlist";
     70 }
     71 
     72 
     73 template <>
     74 void
     75 ChannelListAttribute::writeValueTo (OStream &os, int) const
     76 {
     77     for (ChannelList::ConstIterator i = _value.begin();
     78      i != _value.end();
     79      ++i)
     80     {
     81     //
     82     // Write name
     83     //
     84 
     85     Xdr::write <StreamIO> (os, i.name());
     86 
     87     //
     88     // Write Channel struct
     89     //
     90 
     91     Xdr::write <StreamIO> (os, int (i.channel().type));
     92     Xdr::write <StreamIO> (os, i.channel().pLinear);
     93     Xdr::pad   <StreamIO> (os, 3);
     94     Xdr::write <StreamIO> (os, i.channel().xSampling);
     95     Xdr::write <StreamIO> (os, i.channel().ySampling);
     96     }
     97 
     98     //
     99     // Write end of list marker
    100     //
    101 
    102     Xdr::write <StreamIO> (os, "");
    103 }
    104 
    105 
    106 template <>
    107 void
    108 ChannelListAttribute::readValueFrom (IStream &is, int, int)
    109 {
    110     while (true)
    111     {
    112     //
    113     // Read name; zero length name means end of channel list
    114     //
    115 
    116     char name[Name::SIZE];
    117     Xdr::read <StreamIO> (is, Name::MAX_LENGTH, name);
    118 
    119     if (name[0] == 0)
    120         break;
    121 
    122     checkIsNullTerminated (name, "channel name");
    123 
    124     //
    125     // Read Channel struct
    126     //
    127 
    128     int type;
    129     bool pLinear;
    130     int xSampling;
    131     int ySampling;
    132 
    133     Xdr::read <StreamIO> (is, type);
    134     Xdr::read <StreamIO> (is, pLinear);
    135     Xdr::skip <StreamIO> (is, 3);
    136     Xdr::read <StreamIO> (is, xSampling);
    137     Xdr::read <StreamIO> (is, ySampling);
    138 
    139     _value.insert
    140         (name, Channel (PixelType (type), xSampling, ySampling, pLinear));
    141     }
    142 }
    143 
    144 
    145 } // namespace Imf
    146