root/OpenSceneGraph/trunk/src/osgPlugins/vtf/ReaderWriterVTF.cpp @ 12597

Revision 12597, 26.0 kB (checked in by robert, 3 years ago)

Resolved warnings reported by g++ 4.6's -Wunused-but-set-variable.

Warnings were:

/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osg/ShapeDrawable.cpp: In member function ‘void PrimitiveShapeVisitor::createHalfSphere(unsigned int, unsigned int, float, int, float, const Matrix&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osg/ShapeDrawable.cpp:1409:11: warning: variable ‘nzBase’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osg/ShapeDrawable.cpp:1410:11: warning: variable ‘nRatioBase’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgUtil/DelaunayTriangulator.cpp: In function ‘osgUtil::Triangle_list osgUtil::fillHole(osg::Vec3Array*, std::vector<unsigned int, std::allocator<unsigned int> >)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgUtil/DelaunayTriangulator.cpp:569:27: warning: variable ‘ptest’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgUtil/DelaunayTriangulator.cpp: In member function ‘bool osgUtil::DelaunayTriangulator::triangulate()’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgUtil/DelaunayTriangulator.cpp:979:45: warning: variable ‘curp’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgUtil/RenderStage.cpp: In member function ‘void osgUtil::RenderStage::runCameraSetUp(osg::RenderInfo?&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgUtil/RenderStage.cpp:631:18: warning: variable ‘stencilAttached’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgText/FadeText.cpp: In member function ‘void FadeTextPolytopeData::buildPolytope()’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgText/FadeText.cpp:74:20: warning: variable ‘edge23’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgText/FadeText.cpp:75:20: warning: variable ‘edge30’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgText/Text.cpp: In member function ‘void osgText::Text::computeBackdropPositions(unsigned int) const’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgText/Text.cpp:747:10: warning: variable ‘is_valid_size’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgGA/NodeTrackerManipulator.cpp: In member function ‘virtual bool osgGA::NodeTrackerManipulator::performMovementLeftMouseButton(double, double, double)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgGA/NodeTrackerManipulator.cpp:257:21: warning: variable ‘lookVector’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgGA/NodeTrackerManipulator.cpp:259:21: warning: variable ‘upVector’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgGA/TerrainManipulator.cpp: In member function ‘virtual bool osgGA::TerrainManipulator::performMovementMiddleMouseButton(double, double, double)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgGA/TerrainManipulator.cpp:217:11: warning: variable ‘lookVector’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgGA/TerrainManipulator.cpp:219:11: warning: variable ‘upVector’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgVolume/FixedFunctionTechnique.cpp: In member function ‘virtual void osgVolume::FixedFunctionTechnique::init()’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgVolume/FixedFunctionTechnique.cpp:124:30: warning: variable ‘tf’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgParticle/FluidProgram.cpp: In member function ‘virtual void osgParticle::FluidProgram::execute(double)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgParticle/FluidProgram.cpp:38:23: warning: variable ‘velBefore’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgShadow/ParallelSplitShadowMap.cpp: In member function ‘virtual void osgShadow::ParallelSplitShadowMap::cull(osgUtil::CullVisitor?&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgShadow/ParallelSplitShadowMap.cpp:593:22: warning: variable ‘bb’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgTerrain/GeometryTechnique.cpp: In member function ‘virtual void osgTerrain::GeometryTechnique::generateGeometry(osgTerrain::GeometryTechnique::BufferData?&, osgTerrain::Locator*, const osg::Vec3d&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgTerrain/GeometryTechnique.cpp:777:12: warning: variable ‘i_sampleFactor’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgTerrain/GeometryTechnique.cpp:778:12: warning: variable ‘j_sampleFactor’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/dds/ReaderWriterDDS.cpp: In function ‘osg::Image* ReadDDSFile(std::istream&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/dds/ReaderWriterDDS.cpp:314:10: warning: variable ‘is3dImage’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/dds/ReaderWriterDDS.cpp: In function ‘bool WriteDDSFile(const osg::Image*, std::ostream&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/dds/ReaderWriterDDS.cpp:721:10: warning: variable ‘is3dImage’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/hdr/hdrloader.cpp: In static member function ‘static bool HDRLoader::load(const char*, bool, HDRLoaderResult&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/hdr/hdrloader.cpp:101:10: warning: variable ‘cmd’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/vtf/ReaderWriterVTF.cpp: In function ‘osg::Image* ReadVTFFile(std::istream&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/vtf/ReaderWriterVTF.cpp:360:23: warning: variable ‘base’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/jp2/ReaderWriterJP2.cpp: In function ‘int putdata(jas_stream_t*, jas_image_t*, int)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/jp2/ReaderWriterJP2.cpp:41:13: warning: variable ‘linelen’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/Inventor/ConvertToInventor.cpp: In member function ‘void ConvertToInventor::processGeometry(const osg::Geometry*, ConvertToInventor::InventorState?*)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/Inventor/ConvertToInventor.cpp:1639:10: warning: variable ‘ok’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/Inventor/ConvertFromInventor.cpp: In member function ‘virtual SbBool? SoVRMLImageTextureOsg::readInstance(SoInput?*, short unsigned int)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/Inventor/ConvertFromInventor.cpp:1264:16: warning: variable ‘retval’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/OpenFlight/GeometryRecords.cpp: In member function ‘virtual void flt::Face::readRecord(flt::RecordInputStream?&, flt::Document&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/OpenFlight/GeometryRecords.cpp:369:19: warning: variable ‘secondaryPackedColor’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/OpenFlight/GeometryRecords.cpp: In member function ‘virtual void flt::Mesh::readRecord(flt::RecordInputStream?&, flt::Document&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/OpenFlight/GeometryRecords.cpp:942:19: warning: variable ‘secondaryPackedColor’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/OpenFlight/ReaderWriterFLT.cpp: In member function ‘virtual osgDB::ReaderWriter::ReadResult? FLTReaderWriter::readNode(std::istream&, const Options*) const’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/OpenFlight/ReaderWriterFLT.cpp:427:40: warning: variable ‘pos’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/ive/ShapeAttributeList.cpp: In member function ‘void ive::ShapeAttributeList::write(ive::DataOutputStream?*)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/ive/ShapeAttributeList.cpp:31:48: warning: variable ‘it’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/ac/Geode.cpp: In member function ‘void ac3d::Geode::ProcessGeometry?(std::ostream&, unsigned int)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/ac/Geode.cpp:806:35: warning: variable ‘fRep_s’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/ac/Geode.cpp:806:43: warning: variable ‘fRep_t’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/ac/Geode.cpp:807:35: warning: variable ‘fOffset_s’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/ac/Geode.cpp:807:46: warning: variable ‘fOffset_t’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/ac/Geode.cpp:932:38: warning: variable ‘primLength’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/txp/trpage_geom.cpp: In member function ‘virtual bool trpgGeometry::Write(trpgWriteBuffer&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/txp/trpage_geom.cpp:615:19: warning: variable ‘u’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/txp/trpage_material.cpp: In member function ‘int trpgMatTable::AddMaterial?(const trpgMaterial&, bool)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/txp/trpage_material.cpp:103:10: warning: variable ‘spaceInTable’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/txp/trpage_rarchive.cpp: In member function ‘virtual bool trpgr_Archive::ReadHeader?(bool)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/txp/trpage_rarchive.cpp:261:14: warning: variable ‘headerHasTexTable’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/zip/unzip.cpp: In member function ‘ZRESULT TUnzip::Get(int, ZIPENTRY*)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/zip/unzip.cpp:4055:8: warning: variable ‘hidden’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/zip/unzip.cpp:4055:22: warning: variable ‘system’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/zip/unzip.cpp:4055:36: warning: variable ‘archive’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/zip/ZipArchive.cpp: In member function ‘virtual bool ZipArchive::getFileNames(osgDB::Archive::FileNameList?&) const’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/zip/ZipArchive.cpp:91:37: warning: variable ‘iterEnd’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/pvr/ReaderWriterPVR.cpp: In member function ‘osgDB::ReaderWriter::ReadResult? ReaderWriterPVR::readPVRStream(std::istream&) const’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/pvr/ReaderWriterPVR.cpp:155:14: warning: variable ‘hasAlpha’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgViewer/View.cpp: In function ‘osg::Geometry* create3DSphericalDisplayDistortionMesh(const Vec3&, const Vec3&, const Vec3&, double, double, osg::Image*, const Matrix&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgViewer/View.cpp:737:15: warning: variable ‘cursor’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgViewer/View.cpp: In function ‘osg::Geometry* createParoramicSphericalDisplayDistortionMesh(const Vec3&, const Vec3&, const Vec3&, double, double, osg::Image*, const Matrix&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgViewer/View.cpp:1130:19: warning: variable ‘cursor’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgViewer/View.cpp:1118:15: warning: variable ‘dx’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgViewer/GraphicsWindowX11.cpp: In member function ‘virtual void osgViewer::GraphicsWindowX11::checkEvents()’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgViewer/GraphicsWindowX11.cpp:1181:10: warning: variable ‘destroyWindowRequested’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/cfg/ConfigParser.cpp: In member function ‘bool osgProducer::CameraConfig::parseFile(const string&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/cfg/ConfigParser.cpp:2247:13: warning: variable ‘result’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgQt/QGraphicsViewAdapter.cpp: In member function ‘bool osgQt::QGraphicsViewAdapter::handlePointerEvent(int, int, int)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgQt/QGraphicsViewAdapter.cpp:344:17: warning: variable ‘viewportGeometry’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgdistortion/osgdistortion.cpp: In function ‘osg::Node* createDistortionSubgraph(osg::Node*, const Vec4&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgdistortion/osgdistortion.cpp:125:19: warning: variable ‘cursor’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgdistortion/osgdistortion.cpp:126:19: warning: variable ‘texcoord’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgdistortion/osgdistortion.cpp: In function ‘osg::Geometry* createDomeDistortionMesh(const Vec3&, const Vec3&, const Vec3&, osg::ArgumentParser?&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgdistortion/osgdistortion.cpp:358:15: warning: variable ‘cursor’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgposter/osgposter.cpp: In function ‘int main(int, char**)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgposter/osgposter.cpp:253:31: warning: variable ‘outputTiles’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgthreadedterrain/osgthreadedterrain.cpp: In function ‘int main(int, char**)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgthreadedterrain/osgthreadedterrain.cpp:669:10: warning: variable ‘readParameter’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgtext3D/TextNode.cpp: In member function ‘virtual void osgText::Layout::layout(osgText::TextNode?&) const’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgtext3D/TextNode.cpp:80:11: warning: variable ‘characterHeightScale’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgvolume/osgvolume.cpp: In function ‘int main(int, char**)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgvolume/osgvolume.cpp:678:38: warning: variable ‘internalFormatMode’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgwidgetcanvas/osgwidgetcanvas.cpp: In function ‘bool windowMouseOver(osgWidget::Event&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgwidgetcanvas/osgwidgetcanvas.cpp:27:24: warning: variable ‘xy’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgwidgetcanvas/osgwidgetcanvas.cpp: In function ‘bool widgetMouseOver(osgWidget::Event&)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/examples/osgwidgetcanvas/osgwidgetcanvas.cpp:35:24: warning: variable ‘xy’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/p3d/ReaderWriterP3D.cpp: In member function ‘osg::Node* ReaderWriterP3DXML::parseXmlGraph(osgDB::XmlNode?*, bool, osgDB::Options*) const’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/src/osgPlugins/p3d/ReaderWriterP3D.cpp:2121:10: warning: variable ‘readSlide’ set but not used [-Wunused-but-set-variable]
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/applications/present3D/present3D.cpp: In function ‘int main(int, char**)’:
/home/stephan/Dev/LibSources/OpenSceneGraph-3.0.0-rc2/applications/present3D/present3D.cpp:639:10: warning: variable ‘sizesSpecified’ set but not used [-Wunused-but-set-variable]

  • Property svn:eol-style set to native
Line 
1/**********************************************************************
2*
3*    FILE:            ReaderWriterVTF.cpp
4*
5*    DESCRIPTION:    Class for reading a Valve Texture Format (VTF) file
6*                    into an osg::Image.
7*
8*                    Borrows heavily from the DDS plugin for OSG, as well
9*                    as the Valve Source SDK
10*
11*    CREATED BY:     Jason Daly (jdaly@ist.ucf.edu)
12*
13*    HISTORY:        Created   27.10.2008
14*
15**********************************************************************/
16
17#include <osg/Texture>
18#include <osg/Notify>
19
20#include <osgDB/Registry>
21#include <osgDB/FileNameUtils>
22#include <osgDB/FileUtils>
23
24#include <iomanip>
25#include <stdio.h>
26
27
28enum VTFFlags
29{
30    VTF_FLAGS_POINTSAMPLE                          = 0x00000001,
31    VTF_FLAGS_TRILINEAR                            = 0x00000002,
32    VTF_FLAGS_CLAMP_S                              = 0x00000004,
33    VTF_FLAGS_CLAMP_T                              = 0x00000008,
34    VTF_FLAGS_ANISOTROPIC                          = 0x00000010,
35    VTF_FLAGS_HINT_DXT5                            = 0x00000020,
36    VTF_FLAGS_NOCOMPRESS                           = 0x00000040,
37    VTF_FLAGS_NORMAL                               = 0x00000080,
38    VTF_FLAGS_NOMIP                                = 0x00000100,
39    VTF_FLAGS_NOLOD                                = 0x00000200,
40    VTF_FLAGS_MINMIP                               = 0x00000400,
41    VTF_FLAGS_PROCEDURAL                           = 0x00000800,
42    VTF_FLAGS_ONEBITALPHA                          = 0x00001000,
43    VTF_FLAGS_EIGHTBITALPHA                        = 0x00002000,
44    VTF_FLAGS_ENVMAP                               = 0x00004000,
45    VTF_FLAGS_RENDERTARGET                         = 0x00008000,
46    VTF_FLAGS_DEPTHRENDERTARGET                    = 0x00010000,
47    VTF_FLAGS_NODEBUGOVERRIDE                      = 0x00020000,
48    VTF_FLAGS_SINGLECOPY                           = 0x00040000,
49    VTF_FLAGS_ONEOVERMIPLEVELINALPHA               = 0x00080000,
50    VTF_FLAGS_PREMULTCOLORBYONEOVERMIPLEVEL        = 0x00100000,
51    VTF_FLAGS_NORMALTODUDV                         = 0x00200000,
52    VTF_FLAGS_ALPHATESTMIPGENERATION               = 0x00400000,
53    VTF_FLAGS_NODEPTHBUFFER                        = 0x00800000,
54    VTF_FLAGS_NICEFILTERED                         = 0x01000000,
55    VTF_FLAGS_CLAMP_U                              = 0x02000000,
56    VTF_FLAGS_PRESWIZZLED                          = 0x04000000,
57    VTF_FLAGS_CACHEABLE                            = 0x08000000,
58    VTF_FLAGS_UNFILTERABLE_OK                      = 0x10000000,
59    VTF_FLAGS_LASTFLAG                             = 0x10000000
60};
61
62
63enum VTFCubeMapFaceIndex
64{
65    VTF_CUBEMAP_FACE_RIGHT = 0,
66    VTF_CUBEMAP_FACE_LEFT,
67    VTF_CUBEMAP_FACE_BACK,
68    VTF_CUBEMAP_FACE_FRONT,
69    VTF_CUBEMAP_FACE_UP,
70    VTF_CUBEMAP_FACE_DOWN,
71    VTF_CUBEMAP_FACE_SPHEREMAP,
72    VTF_CUBEMAP_FACE_COUNT
73};
74
75
76enum VTFLookDir
77{
78    VTF_LOOK_DOWN_X = 0,
79    VTF_LOOK_DOWN_NEGX,
80    VTF_LOOK_DOWN_Y = 0,
81    VTF_LOOK_DOWN_NEGY,
82    VTF_LOOK_DOWN_Z = 0,
83    VTF_LOOK_DOWN_NEGZ
84};
85
86
87enum VTFImageFormat
88{
89    VTF_FORMAT_UNKNOWN    = -1,
90    VTF_FORMAT_RGBA8888   = 0,
91    VTF_FORMAT_ABGR8888,
92    VTF_FORMAT_RGB888,
93    VTF_FORMAT_BGR888,
94    VTF_FORMAT_RGB565,
95    VTF_FORMAT_I8,
96    VTF_FORMAT_IA88,
97    VTF_FORMAT_P8,
98    VTF_FORMAT_A8,
99    VTF_FORMAT_RGB888_BLUESCREEN,
100    VTF_FORMAT_BGR888_BLUESCREEN,
101    VTF_FORMAT_ARGB8888,
102    VTF_FORMAT_BGRA8888,
103    VTF_FORMAT_DXT1,
104    VTF_FORMAT_DXT3,
105    VTF_FORMAT_DXT5,
106    VTF_FORMAT_BGRX8888,
107    VTF_FORMAT_BGR565,
108    VTF_FORMAT_BGRX5551,
109    VTF_FORMAT_BGRA4444,
110    VTF_FORMAT_DXT1_ONEBITALPHA,
111    VTF_FORMAT_BGRA5551,
112    VTF_FORMAT_UV88,
113    VTF_FORMAT_UVWQ8888,
114    VTF_FORMAT_RGBA16161616F,
115    VTF_FORMAT_RGBA16161616,
116    VTF_FORMAT_UVLX8888,
117    VTF_FORMAT_R32F,
118    VTF_FORMAT_RGB323232F,
119    VTF_FORMAT_RGBA32323232F,
120    VTF_NUM_IMAGE_FORMATS
121};
122
123
124#define VTF_FORMAT_DEFAULT   ((VTFImageFormat)-2)
125
126
127struct VTFFileHeader
128{
129    char             magic_number[4];
130    unsigned int     file_version[2];
131    unsigned int     header_size;
132    unsigned short   image_width;
133    unsigned short   image_height;
134    unsigned int     image_flags;
135    unsigned short   num_frames;
136    unsigned short   start_frame;
137
138    unsigned char    padding_0[4];
139    osg::Vec3f       reflectivity_value;
140    unsigned char    padding_1[4];
141
142    float            bump_scale;
143    unsigned int     image_format;
144    unsigned char    num_mip_levels;
145    unsigned char    low_res_image_format;
146    unsigned char    padding_2[3];
147    unsigned char    low_res_image_width;
148    unsigned char    low_res_image_height;
149    unsigned short   image_depth;
150};
151
152
153//
154// Structure of a DXT-1 compressed texture block
155// see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/Opaque_and_1_Bit_Alpha_Textures.asp
156//
157struct DXT1TexelsBlock
158{
159    unsigned short color_0;     // colors at their
160    unsigned short color_1;     // extreme
161    unsigned int   texels4x4;   // interpolated colors (2 bits per texel)
162};
163
164
165bool ConvertImageFormat(unsigned int vtfFormat, int& internalFormat,
166                        int& pixelFormat, int& dataType)
167{
168    bool supported;
169
170    // Assume a supported format to start
171    supported = true;
172
173    // Decode the format
174    switch (vtfFormat)
175    {
176        case VTF_FORMAT_DEFAULT:
177            supported = false;
178            break;
179
180        case VTF_FORMAT_UNKNOWN:
181            supported = false;
182            break;
183
184        case VTF_FORMAT_RGBA8888:
185            internalFormat = GL_RGBA;
186            pixelFormat = GL_RGBA;
187            dataType = GL_UNSIGNED_BYTE;
188            break;
189
190        case VTF_FORMAT_ABGR8888:
191            internalFormat = GL_RGBA;
192            pixelFormat = GL_ABGR_EXT;
193            dataType = GL_UNSIGNED_BYTE;
194            break;
195
196        case VTF_FORMAT_RGB888:
197            internalFormat = GL_RGB;
198            pixelFormat = GL_RGB;
199            dataType = GL_UNSIGNED_BYTE;
200            break;
201
202        case VTF_FORMAT_BGR888:
203            internalFormat = GL_RGB;
204            pixelFormat = GL_BGR;
205            dataType = GL_UNSIGNED_BYTE;
206            break;
207
208        case VTF_FORMAT_RGB565:
209            internalFormat = GL_RGB;
210            pixelFormat = GL_RGB;
211            dataType = GL_UNSIGNED_SHORT_5_6_5;
212            break;
213
214        case VTF_FORMAT_I8:
215            internalFormat = GL_LUMINANCE;
216            pixelFormat = GL_LUMINANCE;
217            dataType = GL_UNSIGNED_BYTE;
218            break;
219
220        case VTF_FORMAT_IA88:
221            internalFormat = GL_LUMINANCE_ALPHA;
222            pixelFormat = GL_LUMINANCE_ALPHA;
223            dataType = GL_UNSIGNED_BYTE;
224            break;
225
226        case VTF_FORMAT_P8:
227            // 8-bit paletted image, not supported
228            supported = false;
229            break;
230
231        case VTF_FORMAT_A8:
232            internalFormat = GL_ALPHA;
233            pixelFormat = GL_ALPHA;
234            dataType = GL_UNSIGNED_BYTE;
235            break;
236
237        case VTF_FORMAT_RGB888_BLUESCREEN:
238            // Ignore the "bluescreen" specification for now
239            internalFormat = GL_RGB;
240            pixelFormat = GL_RGB;
241            dataType = GL_UNSIGNED_BYTE;
242            break;
243
244        case VTF_FORMAT_BGR888_BLUESCREEN:
245            // Ignore the "bluescreen" specification for now
246            internalFormat = GL_RGB;
247            pixelFormat = GL_BGR;
248            dataType = GL_UNSIGNED_BYTE;
249            break;
250
251        case VTF_FORMAT_ARGB8888:
252            // ARGB not supported
253            supported = false;
254            break;
255
256        case VTF_FORMAT_BGRA8888:
257            internalFormat = GL_RGBA;
258            pixelFormat = GL_BGRA;
259            dataType = GL_UNSIGNED_BYTE;
260            break;
261
262        case VTF_FORMAT_DXT1:
263            internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
264            pixelFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
265            dataType = GL_UNSIGNED_BYTE;
266            break;
267
268        case VTF_FORMAT_DXT3:
269            internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
270            pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
271            dataType = GL_UNSIGNED_BYTE;
272            break;
273
274        case VTF_FORMAT_DXT5:
275            internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
276            pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
277            dataType = GL_UNSIGNED_BYTE;
278            break;
279
280        case VTF_FORMAT_BGRX8888:
281            internalFormat = GL_RGB;
282            pixelFormat = GL_BGRA;
283            dataType = GL_UNSIGNED_BYTE;
284            break;
285
286        case VTF_FORMAT_BGR565:
287            internalFormat = GL_RGB;
288            pixelFormat = GL_BGR;
289            dataType = GL_UNSIGNED_SHORT_5_6_5_REV;
290            break;
291
292        case VTF_FORMAT_BGRX5551:
293            internalFormat = GL_RGB;
294            pixelFormat = GL_BGRA;
295            dataType = GL_UNSIGNED_SHORT_5_5_5_1;
296            break;
297
298        case VTF_FORMAT_BGRA4444:
299            internalFormat = GL_RGBA;
300            pixelFormat = GL_BGRA;
301            dataType = GL_UNSIGNED_SHORT_4_4_4_4;
302            break;
303
304        case VTF_FORMAT_DXT1_ONEBITALPHA:
305            internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
306            pixelFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
307            dataType = GL_UNSIGNED_BYTE;
308            break;
309
310        case VTF_FORMAT_BGRA5551:
311            internalFormat = GL_RGBA;
312            pixelFormat = GL_BGRA;
313            dataType = GL_UNSIGNED_SHORT_5_5_5_1;
314            break;
315
316        case VTF_FORMAT_UV88:
317            supported = false;
318            break;
319
320        case VTF_FORMAT_UVWQ8888:
321            supported = false;
322            break;
323
324        case VTF_FORMAT_RGBA16161616F:
325            internalFormat = GL_RGBA;
326            pixelFormat = GL_RGBA;
327            dataType = GL_HALF_FLOAT_NV;
328            break;
329
330        case VTF_FORMAT_RGBA16161616:
331            internalFormat = GL_RGBA;
332            pixelFormat = GL_RGBA;
333            dataType = GL_UNSIGNED_SHORT;
334            break;
335
336        case VTF_FORMAT_UVLX8888:
337            supported = false;
338            break;
339
340        default:
341            supported = false;
342            break;
343    }
344
345    // Return whether or not the format is supported
346    return supported;
347}
348
349
350osg::Image* ReadVTFFile(std::istream& _istream)
351{
352    VTFFileHeader     vtf_header;
353    bool              supported;
354    int               internalFormat;
355    int               pixelFormat;
356    int               dataType;
357    int               s, t, r;
358    unsigned int      lrSize;
359    unsigned char *   imageData;
360    unsigned int      size;
361    int               mip;
362    int               mipSize;
363    int               mipOffset;
364   
365    // Validate the file with the 'VTF\0' magic number
366    _istream.read(&vtf_header.magic_number[0], 4);
367    if ((vtf_header.magic_number[0] != 'V') ||
368        (vtf_header.magic_number[1] != 'T') ||
369        (vtf_header.magic_number[2] != 'F') ||
370        (vtf_header.magic_number[3] != 0))
371    {
372        // Not a VTF file, so bail
373        OSG_WARN << "VTF file is invalid" << std::endl;
374        return NULL;
375    }
376
377    // Now, read the rest of the header
378    _istream.read((char *)&vtf_header.file_version[0], 8);
379    _istream.read((char *)&vtf_header.header_size, 4);
380    _istream.read((char *)&vtf_header.image_width, 2);
381    _istream.read((char *)&vtf_header.image_height, 2);
382    _istream.read((char *)&vtf_header.image_flags, 4);
383    _istream.read((char *)&vtf_header.num_frames, 2);
384    _istream.read((char *)&vtf_header.start_frame, 2);
385    _istream.ignore(4);
386    _istream.read((char *)&vtf_header.reflectivity_value, 12);
387    _istream.ignore(4);
388    _istream.read((char *)&vtf_header.bump_scale, 4);
389    _istream.read((char *)&vtf_header.image_format, 4);
390    _istream.read((char *)&vtf_header.num_mip_levels, 1);
391    _istream.read((char *)&vtf_header.low_res_image_format, 4);
392    _istream.read((char *)&vtf_header.low_res_image_width, 1);
393    _istream.read((char *)&vtf_header.low_res_image_height, 1);
394
395    // No depth in textures earlier than version 7.2
396    if ((vtf_header.file_version[0] < 7) ||
397        ((vtf_header.file_version[0] == 7) &&
398         (vtf_header.file_version[1] < 2)))
399    {
400        // No depth in header, set it to 1
401        vtf_header.image_depth = 1;
402    }
403    else
404    {
405        // Read the image depth
406        _istream.read((char *)&vtf_header.image_depth, 2);
407    }
408
409    // Skip past the rest of the header's space
410    std::streampos filePos = _istream.tellg();
411    _istream.ignore(vtf_header.header_size - filePos);
412
413    // Environment maps not supported
414    if (vtf_header.image_flags & VTF_FLAGS_ENVMAP)
415    {
416        OSG_WARN << "VTF Environment maps not supported";
417        OSG_WARN << std::endl;
418        return NULL;
419    }
420
421    OSG_INFO << "VTF Header: (" << sizeof(VTFFileHeader);
422    OSG_INFO << " bytes)" << std::endl;
423    OSG_INFO << "   magic_number = ";
424    OSG_INFO << vtf_header.magic_number[0];
425    OSG_INFO << vtf_header.magic_number[1];
426    OSG_INFO << vtf_header.magic_number[2];
427    OSG_INFO << vtf_header.magic_number[3] << std:: endl;
428    OSG_INFO << "   file_version = ";
429    OSG_INFO << vtf_header.file_version[0] << ".";
430    OSG_INFO << vtf_header.file_version[1] << std:: endl;
431    OSG_INFO << "   header_size  = ";
432    OSG_INFO << vtf_header.header_size << std::endl;
433    OSG_INFO << "   image_width  = ";
434    OSG_INFO << vtf_header.image_width << std::endl;
435    OSG_INFO << "   image_height = ";
436    OSG_INFO << vtf_header.image_height << std::endl;
437    OSG_INFO << "   num_frames   = ";
438    OSG_INFO << vtf_header.num_frames << std::endl;
439    OSG_INFO << "   start_frame  = ";
440    OSG_INFO << vtf_header.start_frame << std::endl;
441    OSG_INFO << "   reflectivity = ";
442    OSG_INFO << vtf_header.reflectivity_value.x() << ", ";
443    OSG_INFO << vtf_header.reflectivity_value.y() << ", ";
444    OSG_INFO << vtf_header.reflectivity_value.z() << std::endl;
445    OSG_INFO << "   bump_scale   = ";
446    OSG_INFO << vtf_header.bump_scale << std::endl;
447    OSG_INFO << "   image_format = ";
448    OSG_INFO << vtf_header.image_format << std::endl;
449    OSG_INFO << "   num_mip_lvls = ";
450    OSG_INFO << (int)vtf_header.num_mip_levels << std::endl;
451    OSG_INFO << "   lr_image_fmt = ";
452    OSG_INFO << (int)vtf_header.low_res_image_format << std::endl;
453    OSG_INFO << "   lr_width     = ";
454    OSG_INFO << (int)vtf_header.low_res_image_width << std::endl;
455    OSG_INFO << "   lr_height    = ";
456    OSG_INFO << (int)vtf_header.low_res_image_height << std::endl;
457    OSG_INFO << "   image_depth  = ";
458    OSG_INFO << (int)vtf_header.image_depth << std::endl;
459
460    // Before we get to the real image, we need to skip over the "low res"
461    // image that's often stored along with VTF textures, so get the
462    // low-res image dimensions
463    s = vtf_header.low_res_image_width;
464    t = vtf_header.low_res_image_height;
465    r = 1;
466    OSG_INFO << "Low-res s = " << s << std::endl;
467    OSG_INFO << "Low-res t = " << t << std::endl;
468
469    // See if the low-res image is there
470    lrSize = 0;
471    if ((s > 0) && (t > 0))
472    {
473        supported = ConvertImageFormat(vtf_header.low_res_image_format,
474                                       internalFormat, pixelFormat, dataType);
475
476        // If we don't recognize the format, we can't locate the real image
477        // in the file, so we have to bail
478        if (!supported)
479        {
480            OSG_WARN << "Low-res image format is not supported";
481            OSG_WARN << " (" << vtf_header.low_res_image_format;
482            OSG_WARN << ")" << std::endl;
483            return NULL;
484        }
485
486        // Allocate an osg::Image for the lo-res image metadata
487        osg::ref_ptr<osg::Image> loResImage = new osg::Image();   
488
489        // Set the image metadata, and figure out how many bytes to read
490        loResImage->setImage(s, t, r, internalFormat, pixelFormat, dataType,
491                             0, osg::Image::USE_NEW_DELETE);
492        lrSize = loResImage->getTotalSizeInBytes();
493
494        // Skip over the low-res image data
495        OSG_INFO << "Low-res size = " << lrSize << std::endl;
496        _istream.ignore(lrSize);
497    }
498
499    // Compute the base position of the high-res image data
500    // unsigned int base = vtf_header.header_size + lrSize;
501
502    // Now, get the internal format, pixel format, and data type from the
503    // full-size image format, and check whether the format is supported
504    supported = ConvertImageFormat(vtf_header.image_format, internalFormat,
505                                   pixelFormat, dataType);
506   
507    // Bail if the format isn't supported
508    if (!supported)
509    {
510        OSG_WARN << "Image format is not supported (";
511        OSG_WARN << vtf_header.image_format << ")";
512        OSG_WARN << std::endl;
513        return NULL;
514    }
515
516    // Get the dimensions of the image
517    s = vtf_header.image_width;
518    t = vtf_header.image_height;
519    r = vtf_header.image_depth;
520
521    // VTF allows either 0 or 1 for 2D images
522    if (r == 0)
523        r = 1;
524
525    // NOTE:  VTF supports animated textures and cube maps.   Currently, we
526    //        only handle a single frame of data, so multiple frames
527    //        are ignored.  Same for cube maps (only one face is loaded).
528
529    // Create the mipmap offsets vector
530    osg::Image::MipmapDataType mipmaps;
531
532    // Deal with mipmaps, if necessary
533    if (vtf_header.num_mip_levels > 1)
534    {
535        // Set up the offsets vector
536        float power2_s = logf((float)s)/logf((float)2);
537        float power2_t = logf((float)t)/logf((float)2);
538        mipmaps.resize((unsigned int)osg::maximum(power2_s,power2_t),0);
539
540        // Calculate the dimensions of each mipmap
541        if ((vtf_header.image_format == VTF_FORMAT_DXT1) ||
542            (vtf_header.image_format == VTF_FORMAT_DXT1_ONEBITALPHA) ||
543            (vtf_header.image_format == VTF_FORMAT_DXT3) ||
544            (vtf_header.image_format == VTF_FORMAT_DXT5))
545        {
546            // Handle S3TC compressed mipmaps
547            int width = vtf_header.image_width;
548            int height = vtf_header.image_height;
549            int blockSize;
550           
551            if ((vtf_header.image_format == VTF_FORMAT_DXT1) ||
552                (vtf_header.image_format == VTF_FORMAT_DXT1_ONEBITALPHA))
553               blockSize = 8;
554            else
555               blockSize = 16;
556
557            int offset = 0;
558            for (unsigned int k = 1;
559                 (k < vtf_header.num_mip_levels) && (width || height);
560                 ++k)
561            {
562                // Clamp dimensions to 1
563                if (width == 0)
564                    width = 1;
565                if (height == 0)
566                    height = 1;
567
568                // Compute and store the offset into the final image data
569                offset += (((width+3)/4) * ((height+3)/4) * blockSize);
570                mipmaps[k-1] = offset;
571
572                // Get the next level's dimensions
573                width >>= 1;
574                height >>= 1;
575            }
576        }
577        else
578        {
579            // Handle uncompressed mipmaps
580            int offset = 0;
581            int width = vtf_header.image_width;
582            int height = vtf_header.image_height;
583            int depth = vtf_header.image_depth;
584            for (unsigned int k = 1;
585                 (k < vtf_header.num_mip_levels) && (width || height || depth);
586                 ++k)
587            {
588                if (width == 0)
589                    width = 1;
590                if (height == 0)
591                    height = 1;
592                if (depth == 0)
593                    depth = 1;
594
595                // Compute and store the offset into the final image data
596                offset += depth * height *
597                    osg::Image::computeRowWidthInBytes(width, pixelFormat,
598                                                       dataType, 1 );
599                mipmaps[k-1] = offset;
600
601                // Get the next level's dimensions
602                width >>= 1;
603                height >>= 1;
604                depth >>= 1;
605            }
606        }
607    }
608
609    // Allocate the resulting osg::Image
610    osg::ref_ptr<osg::Image> osgImage = new osg::Image();   
611
612    // Set the image meta-data, including dimensions, format, data type,
613    // and mipmap levels.  Everything but the image data itself.  We'll use
614    // this to compute the total image size, so we know how much data to read
615    // from the file
616    osgImage->setImage(s, t, r, internalFormat, pixelFormat, dataType,
617                       0, osg::Image::USE_NEW_DELETE);
618    if (mipmaps.size() > 0)
619        osgImage->setMipmapLevels(mipmaps);
620
621    // Compute the total image size
622    size = osgImage->getTotalSizeInBytesIncludingMipmaps();
623    OSG_INFO << "ReadVTFFile info : size = " << size << std::endl;
624    if(size <= 0)
625    {
626        OSG_WARN << "ReadVTFFile warning: size <= 0" << std::endl;
627        return NULL;
628    }
629
630    // Prepare to read the image data
631    imageData = new unsigned char [size];
632    if(!imageData)
633    {
634        OSG_WARN << "ReadVTFFile warning: imageData == NULL";
635        OSG_WARN << std::endl;
636        return NULL;
637    }
638
639    // See if we have mipmaps
640    if (vtf_header.num_mip_levels > 1)
641    {
642        // VTF stores the mipmaps in reverse order from what OpenGL expects, so
643        // we need to read them from the file and store them in order in the
644        // image data array
645        for (mip = vtf_header.num_mip_levels - 2; mip >= 0; mip--)
646        {
647            // Look up the offset for this mip level
648            mipOffset = mipmaps[mip];
649
650            // Calculate the size of the mipmap
651            if (mip == vtf_header.num_mip_levels-2)
652                mipSize = size - mipOffset;
653            else
654                mipSize = mipmaps[mip+1] - mipOffset;
655
656            // Read the image data
657            _istream.read((char*)&imageData[mipOffset], mipSize);
658        }
659
660        // We've read all of the mipmaps except the largest (the original
661        // image), so do that now
662        mipSize = mipmaps[0];
663        _istream.read((char*)imageData, mipSize);
664    }
665    else
666    {
667        // Just read the image data
668        _istream.read((char*)imageData, size);
669    }
670
671/*
672    // Check if alpha information embedded in the 8-byte encoding blocks
673    if (checkIfUsingOneBitAlpha)
674    {
675        const DXT1TexelsBlock *texelsBlock =
676            reinterpret_cast<const DXT1TexelsBlock*>(imageData);
677
678        // Only do the check on the first mipmap level
679        unsigned int numBlocks = mipmaps.size()>0 ? mipmaps[0] / 8 : size / 8;
680
681        for (int i=numBlocks; i>0; --i, ++texelsBlock)
682        {
683            if (texelsBlock->color_0<=texelsBlock->color_1)
684            {
685                // Texture is using the 1-bit alpha encoding, so we need to
686                // update the assumed pixel format
687                internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
688                pixelFormat    = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
689                break;
690            }
691        }
692    }
693*/
694
695    // Now, set the actual image data and mipmap levels
696    osgImage->setImage(s,t,r, internalFormat, pixelFormat, dataType,
697                       imageData, osg::Image::USE_NEW_DELETE);
698    if (mipmaps.size()>0)  osgImage->setMipmapLevels(mipmaps);
699         
700    // Finally, return the image
701    return osgImage.release();
702}
703
704
705bool WriteVTFFile(const osg::Image *img, std::ostream& fout)
706{
707    // Not supported
708    return false;
709}
710
711
712class ReaderWriterVTF : public osgDB::ReaderWriter
713{
714public:
715    virtual const char* className() const
716    {
717        return "VTF Image Reader/Writer";
718    }
719
720    virtual bool acceptsExtension(const std::string& extension) const
721    {
722        return osgDB::equalCaseInsensitive(extension, "vtf");
723    }
724
725    virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const
726    {
727        return readImage(file,options);
728    }
729
730    virtual ReadResult readObject(std::istream& fin, const Options* options) const
731    {
732        return readImage(fin,options);
733    }
734
735    virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options* options) const
736    {
737        std::string ext = osgDB::getLowerCaseFileExtension(file);
738        if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
739
740        std::string fileName = osgDB::findDataFile( file, options );
741   
742        if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
743       
744        osgDB::ifstream stream(fileName.c_str(), std::ios::in | std::ios::binary);
745        if(!stream) return ReadResult::FILE_NOT_HANDLED;
746        ReadResult rr = readImage(stream, options);
747        if(rr.validImage()) rr.getImage()->setFileName(file);
748        return rr;
749    }
750
751    virtual ReadResult readImage(std::istream& fin, const Options* options) const
752    {
753        osg::Image* osgImage = ReadVTFFile(fin);
754        if (osgImage==NULL) return ReadResult::FILE_NOT_HANDLED;
755       
756        if (options && options->getOptionString().find("vtf_flip")!=std::string::npos)
757        {
758            osgImage->flipVertical();
759        }
760       
761        return osgImage;
762    }
763
764    virtual WriteResult writeObject(const osg::Object& object,const std::string& file, const osgDB::ReaderWriter::Options* options) const
765    {
766        const osg::Image* image = dynamic_cast<const osg::Image*>(&object);
767        if (!image) return WriteResult::FILE_NOT_HANDLED;
768
769        return writeImage(*image,file,options);
770    }
771
772    virtual WriteResult writeObject(const osg::Object& object,std::ostream& fout,const Options* options) const
773    {
774        const osg::Image* image = dynamic_cast<const osg::Image*>(&object);
775        if (!image) return WriteResult::FILE_NOT_HANDLED;
776
777        return writeImage(*image,fout,options);
778    }
779
780
781    virtual WriteResult writeImage(const osg::Image &image,const std::string& file, const osgDB::ReaderWriter::Options* options) const
782    {
783        std::string ext = osgDB::getFileExtension(file);
784        if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED;
785
786        osgDB::ofstream fout(file.c_str(), std::ios::out | std::ios::binary);
787        if(!fout) return WriteResult::ERROR_IN_WRITING_FILE;
788
789        return writeImage(image,fout,options);
790    }
791
792    virtual WriteResult writeImage(const osg::Image& image,std::ostream& fout,const Options*) const
793    {
794        bool success = WriteVTFFile(&image, fout);
795
796        if(success)
797            return WriteResult::FILE_SAVED;
798        else
799            return WriteResult::ERROR_IN_WRITING_FILE;
800    }
801};
802
803// now register with Registry to instantiate the above
804// reader/writer.
805REGISTER_OSGPLUGIN(vtf, ReaderWriterVTF)
Note: See TracBrowser for help on using the browser.