fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Image / FileIO / OSGMTDImageFileType.cpp
blobb2e9c111590c5bf7e054bf918406b822c0f485b5
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 \*---------------------------------------------------------------------------*/
39 //-------------------------------
40 // Includes
41 //-------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #include <iostream>
49 #include <fstream>
51 #include "OSGLog.h"
53 #include "OSGMTDImageFileType.h"
55 static const OSG::Char8 *suffixArray[] =
57 "mtd","opensg","opensgImage"
60 OSG_BEGIN_NAMESPACE
62 /*! \class MTDImageFileType
64 Image File Type to read/write and store/restore Image objects as
65 MTD data.
67 All the type specific code is included in the class. Does
68 not depend on external libs.
72 MTDImageFileType MTDImageFileType::_the("image/x-mtd",
73 suffixArray, sizeof(suffixArray),
74 (OSG_READ_SUPPORTED |
75 OSG_WRITE_SUPPORTED));
77 //-------------------------------------------------------------------------
78 /*! Tries to fill the image object with the data read from
79 the given stream. Returns true on success.
82 bool MTDImageFileType::read( Image *pImage,
83 std::istream &is,
84 const std::string &mimetype)
86 bool retCode = false;
87 Head head;
88 void *headData = static_cast<void*>(&head);
89 unsigned dataSize, headSize = sizeof(Head);
91 if(is.read(static_cast<char *>(headData), headSize) &&
92 head.netToHost() &&
93 pImage->set(Image::PixelFormat(head.pixelFormat),
94 head.width,
95 head.height,
96 head.depth,
97 head.mipmapCount,
98 head.frameCount,
99 float(head.frameDelay) / 1000.0,
101 (head.dataType ?
102 Image::Type(head.dataType) : Image::OSG_UINT8_IMAGEDATA),
103 true,
104 head.sideCount) &&
105 (dataSize = pImage->getSize()) &&
106 is.read(reinterpret_cast<char *>(pImage->editData()), dataSize ) )
108 retCode = true;
110 else
112 retCode = false;
115 return retCode;
118 //-------------------------------------------------------------------------
120 Tries to write the image object to the given stream.
121 Returns true on success.
123 bool MTDImageFileType::write(const Image *pImage,
124 std::ostream &os,
125 const std::string &mimetype)
127 bool retCode = false;
129 Head head;
131 const void *headData = static_cast<void *>(&head);
132 unsigned dataSize = pImage->getSize(), headSize = sizeof(Head);
134 head.pixelFormat = pImage->getPixelFormat();
135 head.width = pImage->getWidth();
136 head.height = pImage->getHeight();
137 head.depth = pImage->getDepth();
138 head.mipmapCount = pImage->getMipMapCount();
139 head.frameCount = pImage->getFrameCount();
140 head.frameDelay = short(pImage->getFrameDelay() * 1000.0);
141 head.sideCount = pImage->getSideCount();
142 head.dataType = pImage->getDataType();
143 head.hostToNet();
145 if(os.write(static_cast<const char *>(headData), headSize) &&
146 dataSize &&
147 os.write(reinterpret_cast<const char *>(pImage->getData()), dataSize) )
149 retCode = true;
151 else
153 retCode = false;
156 return retCode;
160 //-------------------------------------------------------------------------
162 Tries to restore the image data from the given memblock.
163 Returns the amount of data read.
165 UInt64 MTDImageFileType::restoreData( Image *pImage,
166 const UChar8 *buffer,
167 Int32 ) const
169 pImage->setData(buffer);
171 return pImage->getSize();
174 //-------------------------------------------------------------------------
175 /*! Tries to store the image data to the given memblock.
176 Returns the amount of data written.
179 UInt64 MTDImageFileType::storeData(const Image *pImage,
180 UChar8 *buffer,
181 Int32 ) const
183 unsigned dataSize = pImage->getSize();
184 const UChar8 *src = pImage->getData();
186 if(dataSize && src && buffer)
187 memcpy(buffer, src, dataSize);
189 return dataSize;
193 //-------------------------------------------------------------------------
194 /*! Constructor used for the singleton object
197 MTDImageFileType::MTDImageFileType(const Char8 *mimeType,
198 const Char8 *suffixArray[],
199 UInt16 suffixByteCount,
200 UInt32 flags ) :
202 Inherited(mimeType, suffixArray, suffixByteCount, flags)
206 //-------------------------------------------------------------------------
207 /*! Destructor
210 MTDImageFileType::~MTDImageFileType(void)
214 OSG_END_NAMESPACE