/Source/System/Image/FileIO/OSGMTDImageFileType.cpp

https://github.com/msteners/OpenSGDevMaster_Toolbox · C++ · 214 lines · 106 code · 34 blank · 74 comment · 9 complexity · d891d1596d587985c0928e32f228c960 MD5 · raw file

  1. /*---------------------------------------------------------------------------*\
  2. * OpenSG *
  3. * *
  4. * *
  5. * Copyright (C) 2000-2002 by the OpenSG Forum *
  6. * *
  7. * www.opensg.org *
  8. * *
  9. * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
  10. * *
  11. \*---------------------------------------------------------------------------*/
  12. /*---------------------------------------------------------------------------*\
  13. * License *
  14. * *
  15. * This library is free software; you can redistribute it and/or modify it *
  16. * under the terms of the GNU Library General Public License as published *
  17. * by the Free Software Foundation, version 2. *
  18. * *
  19. * This library is distributed in the hope that it will be useful, but *
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  22. * Library General Public License for more details. *
  23. * *
  24. * You should have received a copy of the GNU Library General Public *
  25. * License along with this library; if not, write to the Free Software *
  26. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
  27. * *
  28. \*---------------------------------------------------------------------------*/
  29. /*---------------------------------------------------------------------------*\
  30. * Changes *
  31. * *
  32. * *
  33. * *
  34. * *
  35. * *
  36. * *
  37. \*---------------------------------------------------------------------------*/
  38. //-------------------------------
  39. // Includes
  40. //-------------------------------
  41. #include <cstdlib>
  42. #include <cstdio>
  43. #include "OSGConfig.h"
  44. #include <iostream>
  45. #include <fstream>
  46. #include "OSGLog.h"
  47. #include "OSGMTDImageFileType.h"
  48. OSG_USING_NAMESPACE
  49. /*! \class OSG::MTDImageFileType
  50. \ingroup GrpSystemImage
  51. Image File Type to read/write and store/restore Image objects as
  52. MTD data.
  53. All the type specific code is included in the class. Does
  54. not depend on external libs.
  55. */
  56. static const Char8 *suffixArray[] =
  57. {
  58. "mtd","opensg","opensgImage"
  59. };
  60. MTDImageFileType MTDImageFileType::_the("image/x-mtd",
  61. suffixArray, sizeof(suffixArray),
  62. (OSG_READ_SUPPORTED |
  63. OSG_WRITE_SUPPORTED));
  64. //-------------------------------------------------------------------------
  65. /*! Tries to fill the image object with the data read from
  66. the given stream. Returns true on success.
  67. */
  68. bool MTDImageFileType::read( Image *pImage,
  69. std::istream &is,
  70. const std::string &mimetype)
  71. {
  72. bool retCode = false;
  73. Head head;
  74. void *headData = static_cast<void*>(&head);
  75. unsigned dataSize, headSize = sizeof(Head);
  76. if(is.read(static_cast<char *>(headData), headSize) &&
  77. head.netToHost() &&
  78. pImage->set(Image::PixelFormat(head.pixelFormat),
  79. head.width,
  80. head.height,
  81. head.depth,
  82. head.mipmapCount,
  83. head.frameCount,
  84. float(head.frameDelay) / 1000.0,
  85. 0,
  86. (head.dataType ?
  87. Image::Type(head.dataType) : Image::OSG_UINT8_IMAGEDATA),
  88. true,
  89. head.sideCount) &&
  90. (dataSize = pImage->getSize()) &&
  91. is.read(reinterpret_cast<char *>(pImage->editData()), dataSize ) )
  92. {
  93. retCode = true;
  94. }
  95. else
  96. {
  97. retCode = false;
  98. }
  99. return retCode;
  100. }
  101. //-------------------------------------------------------------------------
  102. /*!
  103. Tries to write the image object to the given stream.
  104. Returns true on success.
  105. */
  106. bool MTDImageFileType::write(const Image *pImage,
  107. std::ostream &os,
  108. const std::string &mimetype)
  109. {
  110. bool retCode = false;
  111. Head head;
  112. const void *headData = static_cast<void *>(&head);
  113. unsigned dataSize = pImage->getSize(), headSize = sizeof(Head);
  114. head.pixelFormat = pImage->getPixelFormat();
  115. head.width = pImage->getWidth();
  116. head.height = pImage->getHeight();
  117. head.depth = pImage->getDepth();
  118. head.mipmapCount = pImage->getMipMapCount();
  119. head.frameCount = pImage->getFrameCount();
  120. head.frameDelay = short(pImage->getFrameDelay() * 1000.0);
  121. head.sideCount = pImage->getSideCount();
  122. head.dataType = pImage->getDataType();
  123. head.hostToNet();
  124. if(os.write(static_cast<const char *>(headData), headSize) &&
  125. dataSize &&
  126. os.write(reinterpret_cast<const char *>(pImage->getData()), dataSize) )
  127. {
  128. retCode = true;
  129. }
  130. else
  131. {
  132. retCode = false;
  133. }
  134. return retCode;
  135. }
  136. //-------------------------------------------------------------------------
  137. /*!
  138. Tries to restore the image data from the given memblock.
  139. Returns the amount of data read.
  140. */
  141. UInt64 MTDImageFileType::restoreData( Image *pImage,
  142. const UChar8 *buffer,
  143. Int32 )
  144. {
  145. pImage->setData(buffer);
  146. return pImage->getSize();
  147. }
  148. //-------------------------------------------------------------------------
  149. /*! Tries to store the image data to the given memblock.
  150. Returns the amount of data written.
  151. */
  152. UInt64 MTDImageFileType::storeData(const Image *pImage,
  153. UChar8 *buffer,
  154. Int32 )
  155. {
  156. unsigned dataSize = pImage->getSize();
  157. const UChar8 *src = pImage->getData();
  158. if(dataSize && src && buffer)
  159. memcpy(buffer, src, dataSize);
  160. return dataSize;
  161. }
  162. //-------------------------------------------------------------------------
  163. /*! Constructor used for the singleton object
  164. */
  165. MTDImageFileType::MTDImageFileType(const Char8 *mimeType,
  166. const Char8 *suffixArray[],
  167. UInt16 suffixByteCount,
  168. UInt32 flags ) :
  169. Inherited(mimeType, suffixArray, suffixByteCount, flags)
  170. {
  171. }
  172. //-------------------------------------------------------------------------
  173. /*! Destructor
  174. */
  175. MTDImageFileType::~MTDImageFileType(void)
  176. {
  177. }