Changeset 10819

Show
Ignore:
Timestamp:
11/23/09 12:00:07 (3 years ago)
Author:
robert
Message:

From Chris Hanson, " Add support for "OutputTextureFiles?" option to IVE plugin to permit creation of external
.dds texture files from internally-embedded textures during IVE writes."

From Robert Osfield, fixed a bug in the above submission, and changed the way that the filename of the file is passed into DataOutputStream? to avoid issues with the .ive's plugins ability to read from istreams.

Location:
OpenSceneGraph/trunk/src/osgPlugins
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/src/osgPlugins/ive/DataOutputStream.cpp

    r10762 r10819  
    124124#include <osg/io_utils> 
    125125#include <osgDB/FileUtils> 
     126#include <osgDB/FileNameUtils> 
    126127#include <osgDB/fstream> 
     128#include <osgDB/WriteFile> 
    127129 
    128130#include <stdlib.h> 
     
    143145    _maximumErrorToSizeRatio       = 0.001; 
    144146 
     147    _outputTextureFiles = false; 
     148    _textureFileNameNumber = 0; 
     149 
    145150    _options = options; 
    146151 
    147152    _compressionLevel = 0; 
     153 
     154    if (options) _filename = options->getPluginStringData("filename"); 
     155 
     156    if (_filename.empty()) 
     157    { 
     158        // initialize _filename to a unique identifier in case a real filename is not supplied 
     159        std::ostringstream filenameBuilder; 
     160        filenameBuilder << "file" << ostream; // use address of ostream to formulate unique filename 
     161        _filename = filenameBuilder.str(); 
     162    } 
    148163 
    149164    if (_options.get()) 
     
    158173            setIncludeImageMode(IMAGE_COMPRESS_DATA); 
    159174        } 
    160         osg::notify(osg::DEBUG_INFO) << "ive::DataOutpouStream.setIncludeImageMode()=" << getIncludeImageMode() << std::endl; 
     175        osg::notify(osg::DEBUG_INFO) << "ive::DataOutputStream.setIncludeImageMode()=" << getIncludeImageMode() << std::endl; 
    161176 
    162177        setIncludeExternalReferences(optionsString.find("inlineExternalReferencesInIVEFile")!=std::string::npos); 
    163         osg::notify(osg::DEBUG_INFO) << "ive::DataOutpouStream.setIncludeExternalReferences()=" << getIncludeExternalReferences() << std::endl; 
     178        osg::notify(osg::DEBUG_INFO) << "ive::DataOutputStream.setIncludeExternalReferences()=" << getIncludeExternalReferences() << std::endl; 
    164179 
    165180        setWriteExternalReferenceFiles(optionsString.find("noWriteExternalReferenceFiles")==std::string::npos); 
    166         osg::notify(osg::DEBUG_INFO) << "ive::DataOutpouStream.setWriteExternalReferenceFiles()=" << getWriteExternalReferenceFiles() << std::endl; 
     181        osg::notify(osg::DEBUG_INFO) << "ive::DataOutputStream.setWriteExternalReferenceFiles()=" << getWriteExternalReferenceFiles() << std::endl; 
    167182 
    168183        setUseOriginalExternalReferences(optionsString.find("useOriginalExternalReferences")!=std::string::npos); 
    169         osg::notify(osg::DEBUG_INFO) << "ive::DataOutpouStream.setUseOriginalExternalReferences()=" << getUseOriginalExternalReferences() << std::endl; 
     184        osg::notify(osg::DEBUG_INFO) << "ive::DataOutputStream.setUseOriginalExternalReferences()=" << getUseOriginalExternalReferences() << std::endl; 
     185 
     186        setOutputTextureFiles(optionsString.find("OutputTextureFiles")!=std::string::npos); 
     187        osg::notify(osg::DEBUG_INFO) << "ive::DataOutputStream.setOutputTextureFiles()=" << getOutputTextureFiles() << std::endl; 
    170188 
    171189        _compressionLevel =  (optionsString.find("compressed")!=std::string::npos) ? 1 : 0; 
    172         osg::notify(osg::DEBUG_INFO) << "ive::DataOutpouStream._compressionLevel=" << _compressionLevel << std::endl; 
     190        osg::notify(osg::DEBUG_INFO) << "ive::DataOutputStream._compressionLevel=" << _compressionLevel << std::endl; 
    173191 
    174192        std::string::size_type terrainErrorPos = optionsString.find("TerrainMaximumErrorToSizeRatio="); 
     
    14401458            break; 
    14411459        case IMAGE_REFERENCE_FILE: 
    1442             // Only include image name in stream 
    1443             if (image && !(image->getFileName().empty())){ 
    1444                 writeString(image->getFileName()); 
     1460        { 
     1461            if (image) 
     1462            { 
     1463                // Only include image name in stream 
     1464                std::string fileName = image->getFileName(); 
     1465                // Export an image, if requested 
     1466                if (getOutputTextureFiles()) 
     1467                { 
     1468                    if (fileName.empty()) 
     1469                    { // synthesize a new faux filename 
     1470                        fileName = getTextureFileNameForOutput(); 
     1471                    } 
     1472                    osgDB::writeImageFile(*image, fileName); 
     1473                } 
     1474                writeString(fileName); 
    14451475            } 
    1446             else{ 
     1476            else 
     1477            { 
    14471478                writeString(""); 
    14481479            } 
    14491480            break; 
     1481        } 
    14501482        case IMAGE_INCLUDE_FILE: 
    14511483            // Include image file in stream 
     
    18401872} 
    18411873 
     1874std::string DataOutputStream::getTextureFileNameForOutput() 
     1875{ 
     1876    std::string fileName = osgDB::getNameLessExtension(_filename); 
     1877    if (_textureFileNameNumber>0) 
     1878    { 
     1879        std::ostringstream o; 
     1880        o << '_' << _textureFileNameNumber; 
     1881        fileName += o.str(); 
     1882    } 
     1883     
     1884    fileName += ".dds"; 
     1885    ++_textureFileNameNumber; 
     1886     
     1887    return fileName; 
     1888} 
     1889 
     1890 
     1891 
    18421892void DataOutputStream::setExternalFileWritten(const std::string& filename, bool hasBeenWritten) 
    18431893{ 
  • OpenSceneGraph/trunk/src/osgPlugins/ive/DataOutputStream.h

    r10762 r10819  
    131131    bool getUseOriginalExternalReferences() const {return _useOriginalExternalReferences;}; 
    132132 
     133    // Set and get if export texture files during write 
     134    void setOutputTextureFiles(bool flag) { _outputTextureFiles = flag; } 
     135    bool getOutputTextureFiles() const { return _outputTextureFiles; } 
     136 
     137    // support code for OutputTextureFiles 
     138    virtual std::string getTextureFileNameForOutput(); 
     139    void setFileName(std::string newFileName) {_filename = newFileName;} 
     140    std::string getFileName(void) const {return(_filename);} 
     141 
    133142    void setTerrainMaximumErrorToSizeRatio(double ratio) { _maximumErrorToSizeRatio = ratio; } 
    134143    double getTerrainMaximumErrorToSizeRatio() const { return _maximumErrorToSizeRatio; } 
     
    151160    std::ostream* _ostream; 
    152161    std::ostream* _output_ostream; 
     162    std::string _filename; // not necessary, but optional for use in texture export 
    153163     
    154164    std::stringstream _compressionStream; 
     
    189199 
    190200    IncludeImageMode    _includeImageMode; 
    191      
     201 
     202    bool _outputTextureFiles; 
     203    unsigned int _textureFileNameNumber; 
     204 
    192205    osg::ref_ptr<const osgDB::ReaderWriter::Options> _options; 
    193206 
  • OpenSceneGraph/trunk/src/osgPlugins/ive/ReaderWriterIVE.cpp

    r10762 r10819  
    2929            supportsOption("TerrainMaximumErrorToSizeRatio=value","Export option that controls error matric used to determine terrain HieghtField storage precision."); 
    3030            supportsOption("noLoadExternalReferenceFiles","Import option"); 
     31            supportsOption("OutputTextureFiles","Write out the texture images to file"); 
    3132        } 
    3233     
     
    6970            osg::ref_ptr<Options> local_opt = options ? static_cast<Options*>(options->clone(osg::CopyOp::SHALLOW_COPY)) : new Options; 
    7071            local_opt->getDatabasePathList().push_front(osgDB::getFilePath(fileName)); 
    71              
     72 
    7273            osgDB::ifstream istream(fileName.c_str(), std::ios::in | std::ios::binary); 
    7374            return readNode(istream,local_opt.get()); 
     
    119120            if(local_opt->getDatabasePathList().empty()) 
    120121                local_opt->setDatabasePath(osgDB::getFilePath(fileName)); 
     122 
     123            local_opt->setPluginStringData("filename",fileName); 
     124 
    121125            osgDB::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary); 
    122126            if (!fout) return WriteResult::ERROR_IN_WRITING_FILE; 
     
    135139            if(local_opt->getDatabasePathList().empty()) 
    136140                local_opt->setDatabasePath(osgDB::getFilePath(fileName)); 
     141 
     142            local_opt->setPluginStringData("filename",fileName); 
    137143 
    138144            osgDB::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary); 
  • OpenSceneGraph/trunk/src/osgPlugins/osgTerrain/ImageLayer.cpp

    r8840 r10819  
    1212#include <osgDB/Input> 
    1313#include <osgDB/Output> 
     14#include <osgDB/WriteFile> 
    1415#include <osgDB/ParameterOutput> 
    1516 
     
    6566{ 
    6667    const osgTerrain::ImageLayer& layer = static_cast<const osgTerrain::ImageLayer&>(obj); 
     68 
     69    std::string fileName = layer.getFileName(); 
    6770     
    68     if (!layer.getFileName().empty()) 
     71    if (fw.getOutputTextureFiles()) 
    6972    { 
    70         fw.indent()<<"file "<< layer.getFileName() << std::endl; 
     73        if (fileName.empty()) 
     74        { 
     75            fileName = fw.getTextureFileNameForOutput(); 
     76        } 
     77        osgDB::writeImageFile(*layer.getImage(), fileName); 
     78    } 
     79    if (!fileName.empty()) 
     80    { 
     81        fw.indent()<<"file "<< fw.wrapString(fileName) << std::endl; 
    7182    } 
    7283