PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 14ms RepoModel.GetById 26ms app.codeStats 0ms

/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//-------------------------------
 40//      Includes                                    
 41//-------------------------------
 42
 43#include <cstdlib>
 44#include <cstdio>
 45
 46#include "OSGConfig.h"
 47
 48#include <iostream>
 49#include <fstream>
 50
 51#include "OSGLog.h"
 52
 53#include "OSGMTDImageFileType.h"
 54
 55OSG_USING_NAMESPACE
 56
 57
 58/*! \class OSG::MTDImageFileType 
 59    \ingroup GrpSystemImage
 60
 61  Image File Type to read/write and store/restore Image objects as
 62  MTD data.
 63  
 64  All the type specific code is included in the class. Does
 65  not depend on external libs.
 66  
 67 */
 68
 69static const Char8 *suffixArray[] = 
 70{
 71    "mtd","opensg","opensgImage"
 72};
 73
 74MTDImageFileType MTDImageFileType::_the("image/x-mtd",
 75                                        suffixArray, sizeof(suffixArray),
 76                                        (OSG_READ_SUPPORTED | 
 77                                         OSG_WRITE_SUPPORTED));
 78
 79//-------------------------------------------------------------------------
 80/*! Tries to fill the image object with the data read from
 81    the given stream. Returns true on success.
 82 */
 83
 84bool MTDImageFileType::read(      Image         *pImage, 
 85                                  std::istream  &is, 
 86                            const std::string   &mimetype)
 87{
 88  bool retCode = false;
 89  Head head;
 90  void *headData = static_cast<void*>(&head);
 91  unsigned dataSize, headSize = sizeof(Head);
 92  
 93  if(is.read(static_cast<char *>(headData), headSize) && 
 94     head.netToHost()                                 &&
 95     pImage->set(Image::PixelFormat(head.pixelFormat), 
 96                 head.width, 
 97                 head.height, 
 98                 head.depth, 
 99                 head.mipmapCount, 
100                 head.frameCount, 
101                 float(head.frameDelay) / 1000.0, 
102                 0,
103                 (head.dataType ? 
104                  Image::Type(head.dataType) : Image::OSG_UINT8_IMAGEDATA), 
105                 true, 
106                 head.sideCount)                       &&
107     (dataSize = pImage->getSize())                    && 
108     is.read(reinterpret_cast<char *>(pImage->editData()), dataSize )    )
109  {
110      retCode = true;
111  }
112  else
113  {
114      retCode = false;
115  }
116
117  return retCode;
118}
119
120//-------------------------------------------------------------------------
121/*!
122Tries to write the image object to the given stream.
123Returns true on success.
124*/
125bool MTDImageFileType::write(const Image        *pImage, 
126                                   std::ostream &os, 
127                             const std::string  &mimetype)
128{
129    bool retCode = false;
130
131    Head head;
132
133    const void *headData = static_cast<void *>(&head);
134    unsigned dataSize = pImage->getSize(), headSize = sizeof(Head);
135
136    head.pixelFormat  = pImage->getPixelFormat();
137    head.width        = pImage->getWidth();
138    head.height       = pImage->getHeight();
139    head.depth        = pImage->getDepth();
140    head.mipmapCount  = pImage->getMipMapCount();
141    head.frameCount   = pImage->getFrameCount();
142    head.frameDelay   = short(pImage->getFrameDelay() * 1000.0);
143    head.sideCount    = pImage->getSideCount();
144    head.dataType     = pImage->getDataType();
145    head.hostToNet();
146  
147    if(os.write(static_cast<const char *>(headData), headSize) && 
148       dataSize                                                 && 
149       os.write(reinterpret_cast<const char *>(pImage->getData()), dataSize) )
150    {
151        retCode = true;
152    }
153    else
154    {
155        retCode = false; 
156    }
157
158    return retCode;
159}
160
161
162//-------------------------------------------------------------------------
163/*!
164Tries to restore the image data from the given memblock.
165Returns the amount of data read.
166*/
167UInt64 MTDImageFileType::restoreData(      Image  *pImage, 
168                                     const UChar8 *buffer,
169                                           Int32         )
170{
171    pImage->setData(buffer);
172
173    return pImage->getSize();
174}
175
176//-------------------------------------------------------------------------
177/*! Tries to store the image data to the given memblock.
178    Returns the amount of data written.
179 */
180
181UInt64 MTDImageFileType::storeData(const Image  *pImage, 
182                                         UChar8 *buffer,
183                                         Int32         )
184{
185    unsigned dataSize = pImage->getSize();
186    const UChar8 *src = pImage->getData();
187
188    if(dataSize && src && buffer)
189        memcpy(buffer, src, dataSize);
190    
191    return dataSize;
192} 
193
194
195//-------------------------------------------------------------------------
196/*! Constructor used for the singleton object
197 */
198
199MTDImageFileType::MTDImageFileType(const Char8  *mimeType,
200                                   const Char8  *suffixArray[], 
201                                         UInt16  suffixByteCount,
202                                         UInt32  flags          ) :
203
204    Inherited(mimeType, suffixArray, suffixByteCount, flags)
205{
206}
207
208//-------------------------------------------------------------------------
209/*! Destructor
210 */
211
212MTDImageFileType::~MTDImageFileType(void)
213{
214}