fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Image / FileIO / OSGCHDRImageFileType.cpp
blobd9f729aca249ac255a631af362fc0fc205981d38
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>
45 #include <cmath>
46 #include <memory.h>
48 #include "OSGConfig.h"
50 #include <iostream>
51 #include <fstream>
53 #include "OSGLog.h"
54 #include "OSGImageFileHandler.h"
55 #include "OSGPathHandler.h"
56 #include "OSGFileSystem.h"
57 #include "OSGImageFunctions.h"
59 #include "OSGCHDRImageFileType.h"
61 // Static Class Varible implementations:
62 static const OSG::Char8 *suffixArray[] =
64 "chdr"
67 OSG_BEGIN_NAMESPACE
69 /*! \class HDRImageFileType
71 Image File Type to read/write and store/restore Image objects as
72 HDR data.
74 All the type specific code is included in the class. Does
75 not depend on external libs.
79 /*****************************
80 * Types
81 *****************************/
83 CHDRImageFileType CHDRImageFileType::_the("chdr",
84 suffixArray, sizeof(suffixArray),
85 OSG_READ_SUPPORTED);
88 /*****************************
89 * Classvariables
90 *****************************/
93 /********************************
94 * Class methodes
95 *******************************/
98 /*******************************
99 *public
100 *******************************/
102 //-------------------------------------------------------------------------
104 Tries to fill the image object with the data read from
105 the given input stream. Returns true on success.
108 bool CHDRImageFileType::read( Image *image,
109 std::istream &is,
110 const std::string &mimetype)
112 ImageUnrecPtr pTmpImage = Image::create();
114 bool returnValue = Inherited::read(pTmpImage, is, mimetype);
116 if(returnValue == false)
118 pTmpImage = NULL;
120 return returnValue;
124 returnValue = convertCrossToCubeMap(pTmpImage,
125 image);
127 pTmpImage = NULL;
129 return returnValue;
132 //-------------------------------------------------------------------------
134 Tries to write the image object to the given output stream.
135 Returns true on success.
138 bool CHDRImageFileType::write(const Image *image,
139 std::ostream &os,
140 const std::string &mimetype)
142 #if 0
143 if(image->getDataType() != Image::OSG_FLOAT32_IMAGEDATA)
145 FWARNING(("HDRImageFileType::write: Image has non float data "
146 "type!\n"));
147 return false;
150 if(!os.good())
151 return false;
153 int width = image->getWidth();
154 int height = image->getHeight();
156 os << "#?RADIANCE" << std::endl;
157 os << "# Written with OpenSG" << std::endl;
158 os << "FORMAT=32-bit_rle_rgbe" << std::endl;
159 os << "EXPOSURE=" << 1.0f << std::endl << std::endl;
160 os << "-Y " << height << " +X " << width << std::endl;
162 RGBE *rgbe_scan = new RGBE[width];
164 Real32 *data = ((Real32 *)(image->getData()));
166 //upside down !!!
167 for(int y=height-1;y>=0;y--)
169 if (fwritecolrs(os,
170 &data[y * width * 3],
171 rgbe_scan,
172 width,
173 height) < 0)
175 delete [] rgbe_scan;
176 return false;
180 delete [] rgbe_scan;
181 #endif
183 return true;
186 //-------------------------------------------------------------------------
188 Tries to restore the image data from the given memblock.
189 Returns the amount of data read.
192 UInt64 CHDRImageFileType::restoreData( Image *image,
193 const UChar8 *buffer,
194 Int32 ) const
196 image->setData(buffer);
198 return image->getSize();
201 //-------------------------------------------------------------------------
203 Tries to store the image data to the given memblock.
204 Returns the amount of data written.
207 UInt64 CHDRImageFileType::storeData(const Image *image,
208 UChar8 *buffer,
209 Int32 OSG_CHECK_ARG(memSize)) const
211 UInt32 dataSize = image->getSize();
213 const UChar8 *src = image->getData();
215 if(dataSize && src && buffer)
216 memcpy(buffer, src, dataSize);
218 return dataSize;
222 //-------------------------------------------------------------------------
224 Constructor used for the singleton object
227 CHDRImageFileType::CHDRImageFileType(const Char8 *mimeType,
228 const Char8 *suffixArray[],
229 UInt16 suffixByteCount,
230 UInt32 flags ) :
231 Inherited(mimeType,
232 suffixArray,
233 suffixByteCount,
234 flags )
238 //-------------------------------------------------------------------------
240 Destructor
243 CHDRImageFileType::~CHDRImageFileType(void)
247 OSG_END_NAMESPACE