Replaced readImageFile() usage with readRefImageFile() to prevent threading issues with caching of imagery in the osgDB::Registry cache.

This commit is contained in:
Robert Osfield 2009-01-21 18:23:55 +00:00
parent e17535813f
commit 908432c732
21 changed files with 94 additions and 97 deletions

View File

@ -715,15 +715,15 @@ osg::Texture2D* ReaderWriter3DS::ReaderObject::createTexture(Lib3dsTextureMap *
osg::notify(osg::DEBUG_INFO) << " LIB3DS_IGNORE_ALPHA "<<((texture->flags)&LIB3DS_IGNORE_ALPHA)<< std::endl;
osg::notify(osg::DEBUG_INFO) << " LIB3DS_RGB_TINT "<<((texture->flags)&LIB3DS_RGB_TINT)<< std::endl;
osg::Image* osg_image = osgDB::readImageFile(fileName.c_str());
if (osg_image==NULL)
osg::ref_ptr<osg::Image> osg_image = osgDB::readRefImageFile(fileName.c_str());
if (!osg_image)
{
osg::notify(osg::NOTICE) << "Warning: Cannot create texture "<<texture->name<< std::endl;
return NULL;
}
osg::Texture2D* osg_texture = new osg::Texture2D;
osg_texture->setImage(osg_image);
osg_texture->setImage(osg_image.get());
// does the texture support transparancy?
transparancy = ((texture->flags)&LIB3DS_ALPHA_SOURCE)!=0;

View File

@ -290,7 +290,7 @@ protected:
osg::StateSet* readTexture(const std::string& filename, const Document& document) const
{
osg::Image* image = osgDB::readImageFile(filename,document.getOptions());
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(filename,document.getOptions());
if (!image) return NULL;
// Create stateset to hold texture and attributes.
@ -300,7 +300,7 @@ protected:
texture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::REPEAT);
texture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::REPEAT);
texture->setResizeNonPowerOfTwoHint(true);
texture->setImage(image);
texture->setImage(image.get());
stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
// Read attribute file

View File

@ -351,7 +351,7 @@ class TextureData
osg::notify(osg::FATAL) << "osgDB ac3d reader: could not find texture \"" << name << "\"" << std::endl;
return false;
}
mImage = osgDB::readImageFile(absFileName, options);
mImage = osgDB::readRefImageFile(absFileName, options);
if (!mImage.valid())
{
osg::notify(osg::FATAL) << "osgDB ac3d reader: could not read texture \"" << name << "\"" << std::endl;

View File

@ -448,10 +448,10 @@ bool Q3BSPReader::loadTextures(
std::string jpgExtendedName(aLoadData.m_loadTextures[i].m_name);
jpgExtendedName+=".jpg";
osg::Image* image = osgDB::readImageFile(tgaExtendedName);
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(tgaExtendedName);
if (!image)
{
image = osgDB::readImageFile(jpgExtendedName);
image = osgDB::readRefImageFile(jpgExtendedName);
if (!image)
{
aTextureArray.push_back(NULL);
@ -460,7 +460,7 @@ bool Q3BSPReader::loadTextures(
}
osg::Texture2D* texture= new osg::Texture2D;
texture->setImage(image);
texture->setImage(image.get());
texture->setDataVariance(osg::Object::DYNAMIC); // protect from being optimized away as static state.
texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);

View File

@ -559,8 +559,8 @@ ref_ptr<Texture> VBSPReader::readTextureFile(std::string textureName)
{
std::string texFile;
std::string texPath;
Image * texImage;
Texture * texture;
osg::ref_ptr<Image> texImage;
osg::ref_ptr<Texture> texture;
// Find the texture's image file
texFile = std::string(textureName) + ".vtf";
@ -585,7 +585,7 @@ ref_ptr<Texture> VBSPReader::readTextureFile(std::string textureName)
// If we found the file, read it, otherwise bail
if (!texPath.empty())
{
texImage = readImageFile(texPath);
texImage = readRefImageFile(texPath);
// If we got the image, create the texture attribute
if (texImage != NULL)
@ -593,18 +593,15 @@ ref_ptr<Texture> VBSPReader::readTextureFile(std::string textureName)
// Create the texture
if (texImage->t() == 1)
{
texture = new Texture1D();
((Texture1D *)texture)->setImage(texImage);
texture = new Texture1D(texImage.get());
}
else if (texImage->r() == 1)
{
texture = new Texture2D();
((Texture2D *)texture)->setImage(texImage);
texture = new Texture2D(texImage.get());
}
else
{
texture = new Texture3D();
((Texture3D *)texture)->setImage(texImage);
texture = new Texture3D(texImage.get());
}
// Set texture attributes

View File

@ -735,7 +735,7 @@ osg::StateAttribute *daeReader::processTexture( domCommon_color_or_texture_type_
return NULL;
}
//Got a sampler and a surface and an imaged. Time to create the texture stuff for osg
osg::Image *img = NULL;
osg::ref_ptr<osg::Image> img;
if ( dImg->getInit_from() != NULL )
{
// daeURI uri = dImg->getInit_from()->getValue();
@ -765,7 +765,7 @@ osg::StateAttribute *daeReader::processTexture( domCommon_color_or_texture_type_
#else
const char* filename = path.c_str();
#endif
img = osgDB::readImageFile( filename );
img = osgDB::readRefImageFile( filename );
osg::notify(osg::INFO)<<" processTexture(..) - readImage("<<filename<<")"<<std::endl;
@ -786,7 +786,7 @@ osg::StateAttribute *daeReader::processTexture( domCommon_color_or_texture_type_
return NULL;
}
osg::Texture2D *t2D = new osg::Texture2D( img );
osg::Texture2D *t2D = new osg::Texture2D( img.get() );
//set texture parameters
if ( sampler != NULL )
{

View File

@ -54,11 +54,10 @@ public:
if (isTextured()) { // shares common textures
if (!ctx || !tx) { // new texture needed
if (fname.length()>0) {
ctx=osgDB::readImageFile(fname.c_str(),options);
if (ctx) {
ctx=osgDB::readRefImageFile(fname.c_str(),options);
if (ctx.valid()) {
ctx->setFileName(fname);
tx=new Texture2D;
tx->setImage(ctx);
tx=new Texture2D(ctx.get());
tx->setWrap(Texture2D::WRAP_S, Texture2D::REPEAT);
tx->setWrap(Texture2D::WRAP_T, Texture2D::REPEAT);
}
@ -67,8 +66,8 @@ public:
dstate->setTextureAttribute(0, texenv );
}
}
if (ctx && tx) { // texture exists
dstate->setTextureAttributeAndModes(0,tx,osg::StateAttribute::ON);
if (ctx.valid() && tx.valid()) { // texture exists
dstate->setTextureAttributeAndModes(0,tx.get(),osg::StateAttribute::ON);
}
}
}
@ -175,8 +174,8 @@ private:
std::string fname; // picture file
enum atten {NONE, INVERSE_DIST, INVERSE_SQUARE} atyp;
float bright,halfIn,halfOut,falloff; // light brightness
Image *ctx;
Texture2D *tx;
osg::ref_ptr<osg::Image> ctx;
osg::ref_ptr<osg::Texture2D> tx;
int _lightnum;
StateSet *dstate; // used to represent the dw material in OSG
};

View File

@ -666,8 +666,8 @@ class ReaderGEO
pt->setSize(4);
dstate->setAttribute(pt);
if (txidx>=0 && (unsigned int)txidx<txlist.size()) {
dstate->setTextureAttribute(0, txenvlist[txidx] );
dstate->setTextureAttributeAndModes(0,txlist[txidx],osg::StateAttribute::ON);
dstate->setTextureAttribute(0, txenvlist[txidx].get() );
dstate->setTextureAttributeAndModes(0,txlist[txidx].get(),osg::StateAttribute::ON);
const Image *txim=txlist[txidx]->getImage();
if (txim) {
GLint icm=txim->computeNumComponents(txim->getPixelFormat());
@ -684,7 +684,7 @@ class ReaderGEO
matlist[imat]->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE);
dstate->setMode(GL_COLOR_MATERIAL, osg::StateAttribute::ON);
}
dstate->setAttribute(matlist[imat]);
dstate->setAttribute(matlist[imat].get());
Vec4 col=matlist[imat]->getAmbient(Material::FRONT);
if (col[3]<0.99) {
dstate->setMode(GL_BLEND,StateAttribute::ON);
@ -1466,11 +1466,11 @@ class ReaderGEO
const geoField *gfd=gr->getField(GEO_DB_TEX_FILE_NAME);
const char *name = gfd->getChar();
if (name) {
Texture2D *tx=new Texture2D;
Image *ctx=osgDB::readImageFile(name,options);
if (ctx) {
osg::ref_ptr<osg::Texture2D> tx = new Texture2D;
osg::ref_ptr<osg::Image> ctx = osgDB::readImageFile(name,options);
if (ctx.valid()) {
ctx->setFileName(name);
tx->setImage(ctx);
tx->setImage(ctx.get());
}
gfd=gr->getField(GEO_DB_TEX_WRAPS);
osg::Texture2D::WrapMode wm=Texture2D::REPEAT;
@ -1486,7 +1486,7 @@ class ReaderGEO
wm = (iwrap==GEO_DB_TEX_CLAMP) ? Texture2D::CLAMP : Texture2D::REPEAT;
}
tx->setWrap(Texture2D::WRAP_T, wm);
txlist.push_back(tx);
txlist.push_back(tx.get());
osg::TexEnv* texenv = new osg::TexEnv;
osg::TexEnv::Mode md=osg::TexEnv::MODULATE;
gfd=gr->getField(GEO_DB_TEX_ENV);
@ -2014,9 +2014,9 @@ private:
osg::ref_ptr<geoHeaderGeo> theHeader; // an OSG class - has animation vars etc
std::vector<georecord *> geotxlist; // list of geo::textures for this model
std::vector<georecord *> geomatlist; // list of geo::materials for this model
std::vector<osg::Texture2D *> txlist; // list of osg::textures for this model
std::vector<osg::TexEnv *> txenvlist; // list of texture environments for the textures
std::vector<osg::Material *> matlist; // list of materials for current model
std::vector< osg::ref_ptr<osg::Texture2D> > txlist; // list of osg::textures for this model
std::vector< osg::ref_ptr<osg::TexEnv> > txenvlist; // list of texture environments for the textures
std::vector< osg::ref_ptr<osg::Material> > matlist; // list of materials for current model
georecord *cpalrec; // colour palette record
};

View File

@ -1128,7 +1128,7 @@ osg::Image* DataInputStream::readImage(std::string filename)
// Image is not in list.
// Read it from disk,
osg::Image* image = osgDB::readImageFile(filename.c_str(),_options.get());
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(filename.c_str(),_options.get());
// add it to the imageList,
_imageMap[filename] = image;
@ -1136,7 +1136,7 @@ osg::Image* DataInputStream::readImage(std::string filename)
if (_verboseOutput) std::cout<<"read/writeImage() ["<<image<<"]"<<std::endl;
return image;
return image.release();
}
osg::Image* DataInputStream::readImage()

View File

@ -158,9 +158,9 @@ class Logos: public osg::Drawable
void addLogo( RelativePosition pos, std::string name )
{
osg::Image *image = osgDB::readImageFile( name.c_str() );
if( image != NULL )
logos[pos].push_back( image );
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile( name.c_str() );
if( image.valid())
logos[pos].push_back( image.get() );
else
osg::notify(osg::WARN)<< "Logos::addLogo image file not found : " << name << ".\n";
}

View File

@ -242,15 +242,15 @@ osgDB::ReaderWriter::ReadResult ReaderWriterLWO::readNode_LWO1(const std::string
if (lw_material.ctex.flags && strlen(lw_material.ctex.name)!=0)
{
osg::notify(osg::INFO) << "ctex " << lw_material.ctex.name << std::endl;
osg::Image* image = osgDB::readImageFile(lw_material.ctex.name);
if (image)
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(lw_material.ctex.name);
if (image.valid())
{
// create state
osg::StateSet* stateset = new osg::StateSet;
// create texture
osg::Texture2D* texture = new osg::Texture2D;
texture->setImage(image);
texture->setImage(image.get());
// texture wrap mode
static osg::Texture::WrapMode mode[] = {

View File

@ -195,11 +195,14 @@ void Surface::generate_stateset(unsigned int max_tex_units, bool force_arb_compr
osg::notify(osg::WARN) << "Warning: lwosg::Surface: maximum number of texture units (" << max_tex_units << ") has been reached, skipping incoming blocks" << std::endl;
break;
}
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(image_file, db_options);
if (!image) break;
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
if (force_arb_compression)
texture->setInternalFormatMode(osg::Texture::USE_ARB_COMPRESSION);
texture->setImage(osgDB::readImageFile(image_file, db_options));
texture->setImage(image.get());
texture->setWrap(osg::Texture::WRAP_S, osg_wrap_mode(block.get_image_map().width_wrap));
texture->setWrap(osg::Texture::WRAP_T, osg_wrap_mode(block.get_image_map().height_wrap));
texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR);

View File

@ -707,14 +707,14 @@ Lwo2::_generate_statesets_from_surfaces()
// check if exist texture image for this surface
if (surface->image_index >= 0)
{
Image* image = osgDB::readImageFile(_images[surface->image_index]);
osg::ref_ptr<Image> image = osgDB::readRefImageFile(_images[surface->image_index]);
notify(DEBUG_INFO) << "\tloaded image '" << _images[surface->image_index] << "'" << std::endl;
notify(DEBUG_INFO) << "\tresult - " << image << std::endl;
if (image)
if (image.valid())
{
// create texture
Texture2D* texture = new osg::Texture2D;
texture->setImage(image);
texture->setImage(image.get());
state_set->setTextureAttributeAndModes(0, texture, StateAttribute::ON);
// setup texture wrapping

View File

@ -224,14 +224,15 @@ load_md2 (const char *filename, const osgDB::ReaderWriter::Options* options)
#if 0
std::vector<osg::Texture2D*> skin_textures;
for (int si = 0; si < md2_header->numSkins; si++) {
osg::Image *img;
osg::Texture2D *tex;
for (int si = 0; si < md2_header->numSkins; si++)
{
osg::ref_ptr<osg::Image> img;
osg::ref_ptr<osg::Texture2D> tex;
std::string imgname (md2_skins[si].name);
// first try loading the imgname straight
img = osgDB::readImageFile (imgname, options);
if (img) {
img = osgDB::readRefImageFile (imgname, options);
if (img.valid()) {
tex = new osg::Texture2D;
tex->setImage (img);
skin_textures.push_back (tex);
@ -244,18 +245,18 @@ load_md2 (const char *filename, const osgDB::ReaderWriter::Options* options)
{
// it's a pcx, so try bmp and tga, since pcx sucks
std::string basename = imgname.substr (0, imgname.size() - 3);
img = osgDB::readImageFile (basename + "bmp", options);
if (img) {
img = osgDB::readRefImageFile (basename + "bmp", options);
if (img.valid()) {
tex = new osg::Texture2D;
tex->setImage (img);
tex->setImage (img.get());
skin_textures.push_back (tex);
continue;
}
img = osgDB::readImageFile (basename + "tga", options);
if (img) {
img = osgDB::readRefImageFile (basename + "tga", options);
if (img.valid()) {
tex = new osg::Texture2D;
tex->setImage (img);
tex->setImage (img,get());
skin_textures.push_back (tex);
continue;
}
@ -267,16 +268,16 @@ load_md2 (const char *filename, const osgDB::ReaderWriter::Options* options)
}
#else
// load the single skin
osg::Image *skin_image = NULL;
osg::Texture2D *skin_texture = NULL;
osg::ref_ptr<osg::Image> skin_image;
osg::ref_ptr<osg::Texture2D> skin_texture = NULL;
if (md2_header->numSkins > 0) {
std::string imgname (md2_skins[0].name);
do {
// first try loading the imgname straight
skin_image = osgDB::readImageFile (imgname, options);
if (skin_image) break;
skin_image = osgDB::readRefImageFile(imgname, options);
if (skin_image.valid()) break;
// we failed, so check if it's a PCX image
if (imgname.size() > 4 &&
@ -284,17 +285,17 @@ load_md2 (const char *filename, const osgDB::ReaderWriter::Options* options)
{
// it's a pcx, so try bmp and tga, since pcx sucks
std::string basename = imgname.substr (0, imgname.size() - 3);
skin_image = osgDB::readImageFile (basename + "bmp", options);
if (skin_image) break;
skin_image = osgDB::readRefImageFile (basename + "bmp", options);
if (skin_image.valid()) break;
skin_image = osgDB::readImageFile (basename + "tga", options);
if (skin_image) break;
skin_image = osgDB::readRefImageFile (basename + "tga", options);
if (skin_image.valid()) break;
}
} while (0);
if (skin_image) {
if (skin_image.valid()) {
skin_texture = new osg::Texture2D;
skin_texture->setImage (skin_image);
skin_texture->setImage (skin_image.get());
skin_texture->setFilter (osg::Texture2D::MAG_FILTER, osg::Texture2D::NEAREST);
} else {
// couldn't find the referenced texture skin for this model
@ -407,7 +408,7 @@ load_md2 (const char *filename, const osgDB::ReaderWriter::Options* options)
osg::StateSet *state = new osg::StateSet;
if (skin_texture != NULL) {
state->setTextureAttributeAndModes (0, skin_texture, osg::StateAttribute::ON);
state->setTextureAttributeAndModes (0, skin_texture.get(), osg::StateAttribute::ON);
}
base_switch->setStateSet (state);

View File

@ -84,10 +84,10 @@ std::string MDLReader::getToken(std::string str, const char * delim,
ref_ptr<Texture> MDLReader::readTextureFile(std::string textureName)
{
std::string texFile;
std::string texPath;
Image * texImage;
Texture * texture;
std::string texFile;
std::string texPath;
osg::ref_ptr<Image> texImage;
osg::ref_ptr<Texture> texture;
// Find the texture's image file
texFile = std::string(textureName) + ".vtf";
@ -124,26 +124,23 @@ ref_ptr<Texture> MDLReader::readTextureFile(std::string textureName)
// If we found the file, read it, otherwise bail
if (!texPath.empty())
{
texImage = readImageFile(texPath);
texImage = readRefImageFile(texPath);
// If we got the image, create the texture attribute
if (texImage != NULL)
if (texImage.valid())
{
// Create the texture
if (texImage->t() == 1)
{
texture = new Texture1D();
((Texture1D *)texture)->setImage(texImage);
texture = new Texture1D(texImage.get());
}
else if (texImage->r() == 1)
{
texture = new Texture2D();
((Texture2D *)texture)->setImage(texImage);
texture = new Texture2D(texImage.get());
}
else
{
texture = new Texture3D();
((Texture3D *)texture)->setImage(texImage);
texture = new Texture3D(texImage.get());
}
// Set texture attributes

View File

@ -197,13 +197,13 @@ static void load_material_texture( obj::Model &model,
if ( !model.getDatabasePath().empty() )
{
// first try with database path of parent.
image = osgDB::readImageFile(model.getDatabasePath()+'/'+filename);
image = osgDB::readRefImageFile(model.getDatabasePath()+'/'+filename);
}
if ( !image.valid() )
{
// if not already set then try the filename as is.
image = osgDB::readImageFile(filename);
image = osgDB::readRefImageFile(filename);
}
if ( image.valid() )

View File

@ -185,7 +185,7 @@ bool View_readLocalData(osg::Object &obj, osgDB::Input &fr)
if (!filename.empty())
{
intensityMap = osgDB::readImageFile(filename);
intensityMap = osgDB::readRefImageFile(filename);
}
if (intensityMap.valid())

View File

@ -57,12 +57,12 @@ bool ImageLayer_readLocalData(osg::Object& obj, osgDB::Input &fr)
osg::ref_ptr<osg::Image> image;
if (fileType == osgDB::DIRECTORY)
{
image = osgDB::readImageFile(filename+".dicom");
image = osgDB::readRefImageFile(filename+".dicom");
}
else if (fileType == osgDB::REGULAR_FILE)
{
image = osgDB::readImageFile( filename );
image = osgDB::readRefImageFile( filename );
}
osg::notify(osg::NOTICE)<<"image "<<filename<<" pixelFormat "<<std::hex<<image->getPixelFormat()<<" textureFormat "<<image->getInternalTextureFormat()<<" dataType "<<image->getDataType()<<std::endl;

View File

@ -391,10 +391,10 @@ bool TXPArchive::loadTexture(int i)
path += _PATHD ;
std::string theFile = path + filename ;
osg::Image* image = osgDB::readImageFile(theFile);
if (image)
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(theFile);
if (image.valid())
{
osg_texture->setImage(image);
osg_texture->setImage(image.get());
}
else
{

View File

@ -296,7 +296,7 @@ osg::ref_ptr<osg::Node> ReaderWriterVRML2::convertFromVRML(openvrml::node *obj)
const std::string &url = mfs.value[0];
osg::ref_ptr<osg::Image> image = osgDB::readImageFile(url);
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(url);
if (image != 0) {
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;

View File

@ -227,7 +227,7 @@ osg::Geode* ReaderWriterDirectX::convertFromDX(DX::Mesh & mesh,
// Share image/texture pairs
osg::Texture2D* texture = texForImage[mtl.texture[j]];
if (!texture) {
osg::Image* image = osgDB::readImageFile(mtl.texture[j],options);
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(mtl.texture[j],options);
if (!image)
continue;
@ -235,7 +235,7 @@ osg::Geode* ReaderWriterDirectX::convertFromDX(DX::Mesh & mesh,
texture = new osg::Texture2D;
texForImage[mtl.texture[j]] = texture;
texture->setImage(image);
texture->setImage(image.get());
texture->setWrap(osg::Texture2D::WRAP_S, osg::Texture2D::REPEAT);
texture->setWrap(osg::Texture2D::WRAP_T, osg::Texture2D::REPEAT);
}