fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / FileIO / OSB / OSGNFIOOptions.cpp
blob60b2135d3dbc0c98edfb07bd8fca2b8a3b6fa027
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 \*---------------------------------------------------------------------------*/
39 #include "OSGConfig.h"
41 #include <cctype>
42 #include <string>
43 #include <sstream>
45 #include "OSGNFIOOptions.h"
46 #include "OSGNFIOQuantizer.h"
48 #include "OSGLog.h"
50 OSG_USING_NAMESPACE
52 /***************************************************************************\
53 * Description *
54 \***************************************************************************/
56 /*-------------------------------------------------------------------------*\
57 - public -
58 \*-------------------------------------------------------------------------*/
61 /***************************************************************************\
62 * Instance methods *
63 \***************************************************************************/
65 /*----------------------------- constructors -----------------------------*/
67 NFIOOptions::NFIOOptions(void)
68 : _inlineTextures(true),
69 _compressTextures(false),
70 _texturesCompressionQuality(75),
71 _texturesImageType("jpeg"),
72 _quantizePositions(Quantizer::QRES_OFF),
73 _quantizeNormals(Quantizer::QRES_OFF),
74 _quantizeTexCoords(Quantizer::QRES_OFF),
75 _packIndices(false),
76 _unpack16BitIndices(true),
77 _forceVolumeExport(false)
81 /*------------------------------ destructor -------------------------------*/
83 NFIOOptions::~NFIOOptions(void)
87 /*------------------------------ init------ -------------------------------*/
89 void NFIOOptions::init(const std::string &options)
91 // init default parameters
92 _inlineTextures = true;
93 _compressTextures = false;
94 _texturesCompressionQuality = 75;
95 _texturesImageType = "jpeg",
96 _quantizePositions = Quantizer::QRES_OFF;
97 _quantizeNormals = Quantizer::QRES_OFF;
98 _quantizeTexCoords = Quantizer::QRES_OFF;
99 _packIndices = false;
100 _unpack16BitIndices = false;
101 _forceVolumeExport = false;
103 std::string option;
104 std::string::size_type i = 0;
106 // parse options
107 if(options.find("inlineTextures=true") != std::string::npos)
108 _inlineTextures = true;
109 if(options.find("inlineTextures=false") != std::string::npos)
110 _inlineTextures = false;
112 if(options.find("compressTextures=true") != std::string::npos)
113 _compressTextures = true;
114 if(options.find("compressTextures=false") != std::string::npos)
115 _compressTextures = false;
117 option = "texturesCompressionQuality=";
118 if((i=options.find(option)) != std::string::npos)
119 _texturesCompressionQuality = getInteger(options.substr(i+option.size()));
121 option = "texturesImageType=";
122 if((i = options.find(option)) != std::string::npos)
123 _texturesImageType = getString(options.substr(i + option.size()));
125 if(options.find("quantizePositions=0") != std::string::npos)
126 _quantizePositions = Quantizer::QRES_OFF;
127 if(options.find("quantizePositions=8") != std::string::npos)
128 _quantizePositions = Quantizer::QRES_8BIT;
129 if(options.find("quantizePositions=16") != std::string::npos)
130 _quantizePositions = Quantizer::QRES_16BIT;
131 if(options.find("quantizePositions=24") != std::string::npos)
132 _quantizePositions = Quantizer::QRES_24BIT;
134 if(options.find("quantizeNormals=0") != std::string::npos)
135 _quantizeNormals = Quantizer::QRES_OFF;
136 if(options.find("quantizeNormals=8") != std::string::npos)
137 _quantizeNormals = Quantizer::QRES_8BIT;
138 if(options.find("quantizeNormals=16") != std::string::npos)
139 _quantizeNormals = Quantizer::QRES_16BIT;
140 if(options.find("quantizeNormals=24") != std::string::npos)
141 _quantizeNormals = Quantizer::QRES_24BIT;
143 if(options.find("quantizeTexCoords=0") != std::string::npos)
144 _quantizeTexCoords = Quantizer::QRES_OFF;
145 if(options.find("quantizeTexCoords=8") != std::string::npos)
146 _quantizeTexCoords = Quantizer::QRES_8BIT;
147 if(options.find("quantizeTexCoords=16") != std::string::npos)
148 _quantizeTexCoords = Quantizer::QRES_16BIT;
149 if(options.find("quantizeTexCoords=24") != std::string::npos)
150 _quantizeTexCoords = Quantizer::QRES_24BIT;
152 if(options.find("packIndices=true") != std::string::npos)
153 _packIndices = true;
154 if(options.find("packIndices=false") != std::string::npos)
155 _packIndices = false;
157 if(options.find("unpack16BitIndices=true") != std::string::npos)
158 _unpack16BitIndices = true;
159 if(options.find("unpack16BitIndices=false") != std::string::npos)
160 _unpack16BitIndices = false;
162 if(options.find("forceVolumeExport=true") != std::string::npos)
163 _forceVolumeExport = true;
166 void NFIOOptions::init(const OptionSet &options)
168 // init default parameters
169 _inlineTextures = true;
170 _compressTextures = false;
171 _texturesCompressionQuality = 75;
172 _texturesImageType = "jpeg",
173 _quantizePositions = Quantizer::QRES_OFF;
174 _quantizeNormals = Quantizer::QRES_OFF;
175 _quantizeTexCoords = Quantizer::QRES_OFF;
176 _packIndices = false;
177 _unpack16BitIndices = false;
178 _forceVolumeExport = false;
180 OptionSet::const_iterator oIt = options.begin();
181 OptionSet::const_iterator oEnd = options.end ();
183 for(; oIt != oEnd; ++oIt)
185 if(oIt->first == "inlineTextures")
186 _inlineTextures = getBoolOption(oIt->second);
188 if(oIt->first == "compressTextures")
189 _compressTextures = getBoolOption(oIt->second);
191 if(oIt->first == "texturesCompressionQuality")
192 _texturesCompressionQuality = getValue<UInt32>(oIt->second, 75);
194 if(oIt->first == "texturesImageType")
195 _texturesImageType = getValue<std::string>(oIt->second, "jpeg");
197 if(oIt->first == "quantizePositions")
198 _quantizePositions = getQuantizeOption(oIt->second);
200 if(oIt->first == "quantizeNormals")
201 _quantizeNormals = getQuantizeOption(oIt->second);
203 if(oIt->first == "quantizeTexCoords")
204 _quantizeTexCoords = getQuantizeOption(oIt->second);
206 if(oIt->first == "packIndices")
207 _packIndices = getBoolOption(oIt->second);
209 if(oIt->first == "unpack16BitIndices")
210 _unpack16BitIndices = getBoolOption(oIt->second);
212 if(oIt->first == "forceVolumeExport")
213 _forceVolumeExport = getBoolOption(oIt->second);
217 /*------------------------------ options--- -------------------------------*/
219 bool NFIOOptions::inlineTextures(void) const
221 return _inlineTextures;
224 bool NFIOOptions::compressTextures(void) const
226 return _compressTextures;
229 UInt32 NFIOOptions::texturesCompressionQuality(void) const
231 return _texturesCompressionQuality;
234 std::string NFIOOptions::texturesImageType(void) const
236 return _texturesImageType;
239 UInt8 NFIOOptions::quantizePositions(void) const
241 return _quantizePositions;
244 UInt8 NFIOOptions::quantizeNormals(void) const
246 return _quantizeNormals;
249 UInt8 NFIOOptions::quantizeTexCoords(void) const
251 return _quantizeTexCoords;
254 bool NFIOOptions::packIndices(void) const
256 return _packIndices;
259 bool NFIOOptions::unpack16BitIndices(void) const
261 return _unpack16BitIndices;
264 bool NFIOOptions::forceVolumeExport(void) const
266 return _forceVolumeExport;
269 /*------------------------ helper functions ------------------------------*/
271 Int32 NFIOOptions::getInteger(const std::string &str)
273 std::stringstream ss;
274 UInt32 i = 0;
275 while(i < str.length() && str[i] != ',' &&
276 (isdigit(str[i]) || str[i] == '-'))
278 ss << str[i++];
280 Int32 r;
281 ss >> r;
282 return r;
285 std::string NFIOOptions::getString(const std::string &str)
287 std::string rstr;
288 UInt32 i = 0;
289 while(i < str.length() && str[i] != ',')
291 rstr += str[i++];
293 return rstr;
296 bool NFIOOptions::getBoolOption(const IOOption &option)
298 bool retVal = true;
300 if(!option.optValue.empty())
304 retVal = boost::lexical_cast<bool>(option.optValue);
306 catch(boost::bad_lexical_cast &)
308 retVal = false;
312 return retVal;
315 UInt8 NFIOOptions::getQuantizeOption(const IOOption &option)
317 UInt8 retVal = Quantizer::QRES_OFF;
319 if(!option.optValue.empty())
321 UInt8 quanRes;
325 quanRes = boost::lexical_cast<UInt8>(option.optValue);
327 catch(boost::bad_lexical_cast &)
329 quanRes = Quantizer::QRES_OFF;
332 switch(quanRes)
334 case 0: retVal = Quantizer::QRES_OFF;
335 break;
337 case 8: retVal = Quantizer::QRES_8BIT;
338 break;
340 case 16: retVal = Quantizer::QRES_16BIT;
341 break;
343 case 24: retVal = Quantizer::QRES_24BIT;
344 break;
346 default: retVal = Quantizer::QRES_OFF;
347 break;
351 return retVal;