PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/System/FileIO/OSB/NFIO_Depreciated/OSGNFIOImage.cpp

https://github.com/msteners/OpenSGDevMaster_Toolbox
C++ | 233 lines | 114 code | 45 blank | 74 comment | 17 complexity | e1195a447b8b88bdf2684d41109884fc MD5 | raw file
Possible License(s): LGPL-2.0, BSD-3-Clause
  1. /*---------------------------------------------------------------------------*\
  2. * OpenSG *
  3. * *
  4. * *
  5. * Copyright (C) 2000,2001,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. #include "OSGConfig.h"
  39. #include "OSGNFIOImage.h"
  40. #include "OSGNFIOFactory.h"
  41. #include <vector>
  42. #include "OSGLog.h"
  43. #include "OSGImageFileHandler.h"
  44. //#include "OSGJPGImageFileType.h"
  45. OSG_USING_NAMESPACE
  46. /***************************************************************************\
  47. * Description *
  48. \***************************************************************************/
  49. /*! \class OSG::NFIOImage
  50. reads and writes a image.
  51. */
  52. /***************************************************************************\
  53. * Class variables *
  54. \***************************************************************************/
  55. NFIOImage NFIOImage::_the;
  56. /*-------------------------------------------------------------------------*\
  57. - public -
  58. \*-------------------------------------------------------------------------*/
  59. /***************************************************************************\
  60. * Instance methods *
  61. \***************************************************************************/
  62. /*----------------------------- constructors -----------------------------*/
  63. NFIOImage::NFIOImage(void) :
  64. NFIOBase("Image")
  65. {
  66. _version = 200;
  67. }
  68. /*------------------------------ destructor -------------------------------*/
  69. NFIOImage::~NFIOImage(void)
  70. {
  71. }
  72. /*-------------------------------------------------------------------------*\
  73. - protected -
  74. \*-------------------------------------------------------------------------*/
  75. FieldContainerPtr NFIOImage::readFC(const std::string &/*typeName*/)
  76. {
  77. FDEBUG(("NFIOImage::readFC\n"));
  78. ImagePtr img = Image::create();
  79. UInt16 version;
  80. _in->getValue(version);
  81. std::string fieldName = readFCFields(img, "", "'cpixel'");
  82. // compressed pixel
  83. if(fieldName == "cpixel")
  84. {
  85. readCompressedPixel(img);
  86. _in->getValue(fieldName); // read end marker
  87. }
  88. if(img->getPixel().empty())
  89. {
  90. // read texture
  91. //beginEditCP(img);
  92. //img->read(img->getName().c_str());
  93. img = ImageFileHandler::the()->read(img->getName().c_str());
  94. //endEditCP(img);
  95. }
  96. return img;
  97. }
  98. void NFIOImage::writeFC(const FieldContainerPtr &fc)
  99. {
  100. FDEBUG(("NFIOImage::witeFC\n"));
  101. static bool jpeg = isJPEGSupported();
  102. ImagePtr img = dynamic_cast<ImagePtr>(fc);
  103. _out->putValue(_version);
  104. bool compressTextures = false;
  105. std::string exclude;
  106. if(!getOptions().inlineTextures())
  107. {
  108. // only write the name field.
  109. exclude = "'dimension' 'width' 'height' 'depth' 'bpp' 'mipMapCount' "
  110. "'frameCount' 'frameDelay' 'pixelFormat' 'pixel' 'frameSize'";
  111. }
  112. else
  113. {
  114. // jpeg supports only 1 and 3 byte per pixel images :-(
  115. if(jpeg && getOptions().compressTextures() &&
  116. img->getDataType() == Image::OSG_UINT8_IMAGEDATA &&
  117. (img->getBpp() == 1 || img->getBpp() == 3))
  118. {
  119. compressTextures = true;
  120. exclude += " 'pixel'";
  121. }
  122. }
  123. writeFCFields(img, exclude, false);
  124. if(compressTextures)
  125. writeCompressedPixel(img);
  126. writeEndMarker();
  127. }
  128. void NFIOImage::readCompressedPixel(ImagePtr &img)
  129. {
  130. static bool jpeg = isJPEGSupported();
  131. std::string fieldType;
  132. _in->getValue(fieldType);
  133. UInt32 size;
  134. UInt32 noe;
  135. _in->getValue(size);
  136. // skip data if jpeg is not supported.
  137. if(!jpeg)
  138. {
  139. FWARNING(("NFIOImage::readCompressedPixel : skipping compressed texture"
  140. ", jpeg is not supported\n"));
  141. _in->skip(size);
  142. return;
  143. }
  144. _in->getValue(noe);
  145. std::vector<UInt8> buffer;
  146. buffer.reserve(noe);
  147. UInt8 p;
  148. for(UInt32 i=0;i<noe;++i)
  149. {
  150. _in->getValue(p);
  151. buffer.push_back(p);
  152. }
  153. //beginEditCP(img);
  154. // FIXME missing in Image.cpp
  155. //UInt64 msize = img->restore(&buffer[0], buffer.size());
  156. //endEditCP(img);
  157. }
  158. void NFIOImage::writeCompressedPixel(const ImagePtr &img)
  159. {
  160. _out->putValue(std::string("cpixel"));
  161. _out->putValue(std::string("MFUInt8"));
  162. std::vector<UInt8> buffer;
  163. buffer.resize(img->getSize(false, false));
  164. // FIXME need to add a option inteface like in the SceneFileHandler.
  165. //JPGImageFileType::the().setQuality(getOptions().texturesCompressionQuality());
  166. // FIXME missing in Image.cpp
  167. //UInt64 msize = img->store("jpeg", &buffer[0]);
  168. UInt64 msize = 0;
  169. UInt32 noe = (UInt32) msize;
  170. UInt32 size = sizeof(UInt32) + sizeof(UInt8) * noe;
  171. _out->putValue(size);
  172. _out->putValue(noe);
  173. for(UInt32 i=0;i<noe;++i)
  174. _out->putValue(buffer[i]);
  175. }
  176. bool NFIOImage::isJPEGSupported(void)
  177. {
  178. std::list<const Char8*> sl;
  179. if(ImageFileHandler::the()->getSuffixList(sl) > 0)
  180. {
  181. for(std::list<const Char8*>::iterator i=sl.begin();i!=sl.end();++i)
  182. {
  183. if(!strcmp(*i, "jpeg"))
  184. return true;
  185. }
  186. }
  187. return false;
  188. }