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