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

Language C++ Lines 215
MD5 Hash d891d1596d587985c0928e32f228c960
Repository https://github.com/msteners/OpenSGDevMaster_Toolbox.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*---------------------------------------------------------------------------*\
 *                                OpenSG                                     *
 *                                                                           *
 *                                                                           *
 *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
 *                                                                           *
 *                            www.opensg.org                                 *
 *                                                                           *
 *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
 *                                                                           *
\*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*\
 *                                License                                    *
 *                                                                           *
 * This library is free software; you can redistribute it and/or modify it   *
 * under the terms of the GNU Library General Public License as published    *
 * by the Free Software Foundation, version 2.                               *
 *                                                                           *
 * This library is distributed in the hope that it will be useful, but       *
 * WITHOUT ANY WARRANTY; without even the implied warranty of                *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
 * Library General Public License for more details.                          *
 *                                                                           *
 * You should have received a copy of the GNU Library General Public         *
 * License along with this library; if not, write to the Free Software       *
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
 *                                                                           *
\*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*\
 *                                Changes                                    *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
 *                                                                           *
\*---------------------------------------------------------------------------*/

//-------------------------------
//      Includes                                    
//-------------------------------

#include <cstdlib>
#include <cstdio>

#include "OSGConfig.h"

#include <iostream>
#include <fstream>

#include "OSGLog.h"

#include "OSGMTDImageFileType.h"

OSG_USING_NAMESPACE


/*! \class OSG::MTDImageFileType 
    \ingroup GrpSystemImage

  Image File Type to read/write and store/restore Image objects as
  MTD data.
  
  All the type specific code is included in the class. Does
  not depend on external libs.
  
 */

static const Char8 *suffixArray[] = 
{
    "mtd","opensg","opensgImage"
};

MTDImageFileType MTDImageFileType::_the("image/x-mtd",
                                        suffixArray, sizeof(suffixArray),
                                        (OSG_READ_SUPPORTED | 
                                         OSG_WRITE_SUPPORTED));

//-------------------------------------------------------------------------
/*! Tries to fill the image object with the data read from
    the given stream. Returns true on success.
 */

bool MTDImageFileType::read(      Image         *pImage, 
                                  std::istream  &is, 
                            const std::string   &mimetype)
{
  bool retCode = false;
  Head head;
  void *headData = static_cast<void*>(&head);
  unsigned dataSize, headSize = sizeof(Head);
  
  if(is.read(static_cast<char *>(headData), headSize) && 
     head.netToHost()                                 &&
     pImage->set(Image::PixelFormat(head.pixelFormat), 
                 head.width, 
                 head.height, 
                 head.depth, 
                 head.mipmapCount, 
                 head.frameCount, 
                 float(head.frameDelay) / 1000.0, 
                 0,
                 (head.dataType ? 
                  Image::Type(head.dataType) : Image::OSG_UINT8_IMAGEDATA), 
                 true, 
                 head.sideCount)                       &&
     (dataSize = pImage->getSize())                    && 
     is.read(reinterpret_cast<char *>(pImage->editData()), dataSize )    )
  {
      retCode = true;
  }
  else
  {
      retCode = false;
  }

  return retCode;
}

//-------------------------------------------------------------------------
/*!
Tries to write the image object to the given stream.
Returns true on success.
*/
bool MTDImageFileType::write(const Image        *pImage, 
                                   std::ostream &os, 
                             const std::string  &mimetype)
{
    bool retCode = false;

    Head head;

    const void *headData = static_cast<void *>(&head);
    unsigned dataSize = pImage->getSize(), headSize = sizeof(Head);

    head.pixelFormat  = pImage->getPixelFormat();
    head.width        = pImage->getWidth();
    head.height       = pImage->getHeight();
    head.depth        = pImage->getDepth();
    head.mipmapCount  = pImage->getMipMapCount();
    head.frameCount   = pImage->getFrameCount();
    head.frameDelay   = short(pImage->getFrameDelay() * 1000.0);
    head.sideCount    = pImage->getSideCount();
    head.dataType     = pImage->getDataType();
    head.hostToNet();
  
    if(os.write(static_cast<const char *>(headData), headSize) && 
       dataSize                                                 && 
       os.write(reinterpret_cast<const char *>(pImage->getData()), dataSize) )
    {
        retCode = true;
    }
    else
    {
        retCode = false; 
    }

    return retCode;
}


//-------------------------------------------------------------------------
/*!
Tries to restore the image data from the given memblock.
Returns the amount of data read.
*/
UInt64 MTDImageFileType::restoreData(      Image  *pImage, 
                                     const UChar8 *buffer,
                                           Int32         )
{
    pImage->setData(buffer);

    return pImage->getSize();
}

//-------------------------------------------------------------------------
/*! Tries to store the image data to the given memblock.
    Returns the amount of data written.
 */

UInt64 MTDImageFileType::storeData(const Image  *pImage, 
                                         UChar8 *buffer,
                                         Int32         )
{
    unsigned dataSize = pImage->getSize();
    const UChar8 *src = pImage->getData();

    if(dataSize && src && buffer)
        memcpy(buffer, src, dataSize);
    
    return dataSize;
} 


//-------------------------------------------------------------------------
/*! Constructor used for the singleton object
 */

MTDImageFileType::MTDImageFileType(const Char8  *mimeType,
                                   const Char8  *suffixArray[], 
                                         UInt16  suffixByteCount,
                                         UInt32  flags          ) :

    Inherited(mimeType, suffixArray, suffixByteCount, flags)
{
}

//-------------------------------------------------------------------------
/*! Destructor
 */

MTDImageFileType::~MTDImageFileType(void)
{
}
Back to Top