/src/FreeImage/Source/OpenEXR/IlmImf/ImfChannelListAttribute.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 145 lines · 64 code · 28 blank · 53 comment · 8 complexity · 3e34445297de1d765d574de5bf4e0817 MD5 · raw file

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