Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

osg::ImageFileHandler Class Reference
[Image]

Image file Handler. Used to read/write and store/restore image data. See Image for a detailed description. More...

#include <OSGImageFileHandler.h>

List of all members.

Public Member Functions

Destructor
*virtual ~ImageFileHandler (void)
Read/Write
*virtual ImagePtr read (const char *fileName, const char *mimeType=0)
virtual bool read (ImagePtr &image, const char *fileName, const char *mimeType=0)
virtual bool write (const ImagePtr &image, const char *fileName, const char *mimeType=0)
PathHandler
*virtual PathHandlergetPathHandler (void)
virtual void setPathHandler (PathHandler *pPathHandler)
Storage
*virtual UInt64 restore (ImagePtr &image, const UChar8 *buffer, Int32 memSize=-1)
virtual UInt64 store (const ImagePtr &image, const char *mimeType, UChar8 *buffer, Int32 memSize=-1)
virtual UChar8store (const ImagePtr &image, UInt64 &memSize, const char *mimeType=0)
Get Types
*ImageFileTypegetFileType (const char *mimeType, const char *fileName=0, bool validateHeader=false)
ImageFileTypegetDefaultType (void)
virtual int getSuffixList (std::list< const Char8 * > &suffixList, UInt32 flags=ImageFileType::OSG_READ_SUPPORTED|ImageFileType::OSG_WRITE_SUPPORTED)
Print
*void dump (void)

Static Public Member Functions

Get Method
*static ImageFileHandlerthe (void)

Private Member Functions

Constructor
ImageFileHandler (void)
 ImageFileHandler (const ImageFileHandler &obj)

Static Private Member Functions

static bool addImageFileType (ImageFileType &fileType)

Private Attributes

std::map< IDString, ImageFileType * > _suffixTypeMap
PathHandler_pPathHandler

Static Private Attributes

static ImageFileHandler_the = 0
static const std::string _fileNameKey
static const std::string _fullFilePathKey

Friends

class ImageFileType


Detailed Description

Singelton Object/Class which holds all known ImageFileTypes. The class is used to write/read Image objects to/from files and to store/restore image data to/from memory blocks. Utilizes the local pathHandler for file path handler and construction. The PathHandler can be set from the application.

See Image for details.

Definition at line 62 of file OSGImageFileHandler.h.


Constructor & Destructor Documentation

ImageFileHandler::~ImageFileHandler void   )  [virtual]
 

Destructor

Definition at line 495 of file OSGImageFileHandler.cpp.

00496 {
00497     return;
00498 }

ImageFileHandler::ImageFileHandler void   )  [private]
 

Default Constructor

Definition at line 473 of file OSGImageFileHandler.cpp.

References _pPathHandler.

00474 {
00475   _pPathHandler = NULL;
00476   return;
00477 }

ImageFileHandler::ImageFileHandler const ImageFileHandler obj  )  [private]
 

Invalid Copy Constructor

Definition at line 483 of file OSGImageFileHandler.cpp.

References _pPathHandler, and FFATAL.

00484 {
00485     FFATAL (("Run Copy Constructor on Singleton ImageFileHandler !\n"));
00486     
00487     _pPathHandler = NULL;
00488     return;
00489 }


Member Function Documentation

ImagePtr ImageFileHandler::read const char *  fileName,
const char *  mimeType = 0
[virtual]
 

Creates a new image and tries to read the raster data from the given fileName. If the mimeType is not Null the method will try to find the according ImageFileType. Otherwise it will try to use the fileName suffix to determine the mimeType

Definition at line 232 of file OSGImageFileHandler.cpp.

References osg::ImageBase::create(), osg::NullFC, and osg::subRefCP().

Referenced by osg::Image::read().

00233 {
00234     ImagePtr image = Image::create();
00235 
00236     if(read(image, fileName, mimeType) == false)
00237     {
00238         subRefCP(image);
00239         image = NullFC;
00240     }
00241     return image;
00242 }

bool ImageFileHandler::read ImagePtr image,
const char *  fileName,
const char *  mimeType = 0
[virtual]
 

Tries to read the raster data from the given fileName into the given Image. If the mimeType is not Null the method will try to find the according ImageFileType. Otherwise it will try to use the fileName suffix to determine the mimeType

Definition at line 252 of file OSGImageFileHandler.cpp.

References _fileNameKey, _fullFilePathKey, _pPathHandler, FDEBUG, osg::PathHandler::findFile(), getFileType(), osg::ImageFileType::getMimeType(), osg::ImageFileType::read(), and SWARNING.

00254 {
00255     bool        retCode = false;
00256     std::string fullFilePath;
00257     
00258     if( _pPathHandler != NULL )
00259     {
00260         fullFilePath = _pPathHandler->findFile(fileName);
00261     }
00262     else
00263     {
00264         fullFilePath = fileName;
00265     }
00266     
00267     if(fullFilePath.empty())
00268     {
00269         SWARNING << "couldn't find image file " << fileName << std::endl;
00270         return false;
00271     }
00272 
00273     ImageFileType   *type = getFileType(mimeType, fullFilePath.c_str(), true);
00274 
00275     if(type)
00276     {
00277         FDEBUG(("try to image read %s as %s\n", 
00278                 fullFilePath.c_str(), 
00279                 type->getMimeType()));
00280 
00281         retCode = type->read(image, fullFilePath.c_str());
00282 
00283         if(retCode)
00284         {
00285             FDEBUG(("image: %dx%d\n", image->getWidth(), image->getHeight()));
00286             image->setAttachmentField(_fileNameKey, fileName);
00287             image->setAttachmentField(_fullFilePathKey, fullFilePath);
00288         }
00289         else
00290         {
00291             SWARNING << "could not read " << fullFilePath << std::endl;
00292         }
00293     }
00294     else
00295     {
00296         SWARNING << "could not read " << fullFilePath
00297                  << "; unknown image format" << std::endl;
00298     }
00299 
00300     return retCode;
00301 }

bool ImageFileHandler::write const ImagePtr image,
const char *  fileName,
const char *  mimeType = 0
[virtual]
 

Tries to write the raster data (from the given Image) to the given fileName. If the mimeType is not Null the method will try to find the according ImageFileType. Otherwise it will try to use the fileName suffix to determine the mimeType

Definition at line 311 of file OSGImageFileHandler.cpp.

References _fileNameKey, getFileType(), osg::ImageFileType::getMimeType(), SINFO, SWARNING, and osg::ImageFileType::write().

Referenced by osg::Image::write().

00313 {
00314     bool            retCode = false;
00315     ImageFileType   *type;
00316     const std::string     *fNAttachment;
00317     
00318     if (!fileName && (fNAttachment = image->findAttachmentField(_fileNameKey)))
00319       fileName = fNAttachment->c_str();
00320 
00321     if ((type = getFileType(mimeType, fileName)))
00322     {
00323         SINFO << "try to write " << fileName << " as "
00324               << type->getMimeType() << std::endl;
00325         retCode = type->write(image, fileName);
00326     }
00327     else
00328     {
00329         SWARNING << "can't write " << fileName 
00330                  << "; unknown image format" << std::endl;
00331     }
00332 
00333     return retCode;
00334 }

PathHandler * ImageFileHandler::getPathHandler void   )  [virtual]
 

Returns the path handler used

Definition at line 340 of file OSGImageFileHandler.cpp.

References _pPathHandler.

Referenced by osg::VRMLInlineDesc::endNode(), osg::SceneFileHandler::initPathHandler(), and osg::DATImageFileType::read().

00341 {
00342     return _pPathHandler;
00343 }

void ImageFileHandler::setPathHandler PathHandler pPathHandler  )  [virtual]
 

Method to set the path handler.

Definition at line 349 of file OSGImageFileHandler.cpp.

References _pPathHandler.

Referenced by osg::SceneFileHandler::initPathHandler().

00350 {
00351     _pPathHandler = pPathHandler;
00352 }

UInt64 ImageFileHandler::restore ImagePtr image,
const UChar8 buffer,
Int32  memSize = -1
[virtual]
 

Tries to restore the raster data from the given memblock into the given Image. If the mimeType is not Null the method will try to find the according ImageFileType. Otherwise it will try to use the fileName suffix to determine the mimeType

Definition at line 362 of file OSGImageFileHandler.cpp.

References osg::ImageFileType::restore().

00364 {
00365     return ImageFileType::restore(image, buffer, memSize);
00366 }

UInt64 ImageFileHandler::store const ImagePtr image,
const char *  mimeType,
UChar8 buffer,
Int32  memSize = -1
[virtual]
 

Tries to store the raster data (from the given Image) to the given memBlock. If the mimeType is not Null the method will try to find the according ImageFileType. Otherwise it will try to use the fileName suffix to determine the mimeType

Definition at line 376 of file OSGImageFileHandler.cpp.

References getDefaultType(), getFileType(), and osg::ImageFileType::store().

00378 {
00379     ImageFileType   *type;
00380 
00381     type = mimeType ? getFileType(mimeType) : getDefaultType();
00382 
00383     return type->store(image, buffer, memSize);
00384 }

UChar8 * ImageFileHandler::store const ImagePtr image,
UInt64 memSize,
const char *  mimeType = 0
[virtual]
 

Tries to store the raster data (from the given Image) to a new memBlock. The method will automatically allocate and return a sufficient amount of memory with new. The application has to free the memory with 'delete [] mem' If the mimeType is not Null the method will try to find the according ImageFileType. Otherwise it will try to use the fileName suffix to determine the mimeType

Definition at line 396 of file OSGImageFileHandler.cpp.

References FFATAL, getDefaultType(), getFileType(), osg::ImageFileType::getMimeType(), osg::ImageFileType::maxBufferSize(), and osg::ImageFileType::store().

00398 {
00399     ImageFileType   *type = 0;
00400     UChar8          *mem = 0;
00401 
00402     type = mimeType ? getFileType(mimeType) : getDefaultType();
00403     memSize = type->maxBufferSize(image);
00404 
00405     if(memSize)
00406     {
00407         mem = new UChar8[size_t(memSize)];
00408         memSize = type->store(image, mem, Int32(memSize));
00409     }
00410     else
00411     {
00412         FFATAL(("Can not store the image as %s\n", type->getMimeType()));
00413     }
00414 
00415     return mem;
00416 }

ImageFileType * ImageFileHandler::getFileType const char *  mimeType,
const char *  fileName = 0,
bool  validateHeader = false
 

Method to find a ImageFileHandler for the given mimeType for fileName suffix. Returns the ImageFileHandler object or Null.

Definition at line 93 of file OSGImageFileHandler.cpp.

References _suffixTypeMap, FWARNING, osg::IDString::set(), osg::stringcasecmp(), osg::IDString::toLower(), and osg::ImageFileType::validateHeader().

Referenced by read(), osg::ImageFileType::restore(), osg::ClusterViewBuffer::setImgTransType(), osg::ImageFileType::store(), store(), and write().

00096 {
00097     IDString                                       suffix;
00098     ImageFileType                                 *type = 0;
00099     std::map<IDString, ImageFileType *>::iterator  sI;
00100     const char                                     separator = '.';
00101     int                                            i, l;
00102     std::ifstream                                  fin;
00103     const char                                     *mtPrefix = "image/";
00104     size_t                                          mtLen = strlen(mtPrefix);
00105 
00106     if(mimeType && *mimeType)
00107     {
00108         if ( (strlen(mimeType) > mtLen) && 
00109              !strncmp (mimeType, mtPrefix, mtLen) )
00110             mimeType += mtLen;
00111 
00112         // check mime type
00113         for(sI = _suffixTypeMap.begin(); sI != _suffixTypeMap.end(); ++sI)
00114         {
00115             if(!stringcasecmp(sI->second->getMimeType(), mimeType))
00116             {
00117                 type = sI->second;
00118                 break;
00119             }
00120         }
00121         if (!type) {
00122           FWARNING (("Invalid mimeType %s in getFileType()\n", mimeType));
00123         }
00124     }
00125 
00126     if(!type && fileName && *fileName)
00127     {
00128         // check file suffix
00129         if(!type)
00130         {
00131             l = strlen(fileName);
00132             for(i = l - 1; i >= 0; i--)
00133             {
00134                 if(fileName[i] == separator)
00135                     break;
00136             }
00137 
00138             if(i >= 0)
00139             {
00140                 suffix.set(&(fileName[i + 1]));
00141                 suffix.toLower();
00142                 sI = _suffixTypeMap.find(suffix);
00143                 type = (sI == _suffixTypeMap.end()) ? 0 : sI->second;
00144             }
00145         }
00146     }
00147 
00148     if(validateHeader)
00149     {
00150         // now validate the header of the file
00151         bool implemented = false;
00152         if(fileName && *fileName &&
00153            type != NULL && !type->validateHeader(fileName, implemented))
00154         {
00155             FWARNING (("Found wrong image header trying to autodetect image type!\n"));
00156             for(sI = _suffixTypeMap.begin(); sI != _suffixTypeMap.end(); ++sI)
00157             {
00158                 type = sI->second;
00159                 if(type != NULL && type->validateHeader(fileName, implemented))
00160                 {
00161                     if(implemented)
00162                     {
00163                         FWARNING (("Autodetected '%s' image type!\n", sI->first.str()));
00164                         return type;
00165                     }
00166                 }
00167             }
00168             FWARNING (("Couldn't autodetect image type!\n"));
00169             return NULL;
00170         }
00171     }
00172 
00173     return type;
00174 }

ImageFileType * ImageFileHandler::getDefaultType void   ) 
 

Returns the default OpenSG ImageFileType

Definition at line 180 of file OSGImageFileHandler.cpp.

References _suffixTypeMap, FFATAL, and osg::IDString::str().

Referenced by store().

00181 {
00182   IDString        dSuffix("opensg");
00183   
00184   std::map<IDString, 
00185     ImageFileType *>::iterator sI = _suffixTypeMap.find(dSuffix);
00186   
00187   
00188   ImageFileType *type = (sI == _suffixTypeMap.end()) ? 0 : sI->second;
00189   
00190   if(!type)
00191     {
00192       FFATAL(("Can not find any default (suffix:%s) image handler\n",
00193               dSuffix.str()));
00194     }
00195   
00196   return type;
00197 }

Int32 ImageFileHandler::getSuffixList std::list< const Char8 * > &  suffixList,
UInt32  flags = ImageFileType::OSG_READ_SUPPORTED | ImageFileType::OSG_WRITE_SUPPORTED
[virtual]
 

Returns the list of supported image suffixes

Definition at line 204 of file OSGImageFileHandler.cpp.

References _suffixTypeMap, and osg::ImageFileType::getFlags().

00206 {
00207     Int32                                         count = 0;
00208     std::map<IDString, ImageFileType *>::iterator sI;
00209 
00210     suffixList.clear();
00211     
00212     for ( sI = _suffixTypeMap.begin(); sI != _suffixTypeMap.end(); ++sI)
00213     {
00214         ImageFileType *type = sI->second;
00215         if(type->getFlags() & flags)
00216         {
00217             suffixList.push_back(sI->first.str());
00218             count++;
00219         }
00220     }
00221 
00222   return count;
00223 }

void ImageFileHandler::dump void   ) 
 

The dump method just writes some object debugging info to the LOG stream

Definition at line 422 of file OSGImageFileHandler.cpp.

References _suffixTypeMap, and FLOG.

00423 {
00424     std::map<IDString, ImageFileType *>::iterator    sI;
00425 
00426     for(sI = _suffixTypeMap.begin(); sI != _suffixTypeMap.end(); sI++)
00427     {
00428       FLOG (( "Image suffix: %s, mimeType: %s\n",
00429               sI->first.str(), sI->second->getMimeType() ));
00430     }
00431 }

ImageFileHandler & ImageFileHandler::the void   )  [static]
 

Static method the get the Singleton Object

Definition at line 504 of file OSGImageFileHandler.cpp.

References _the.

Referenced by osg::VRMLInlineDesc::endNode(), osg::SceneFileHandler::initPathHandler(), osg::Image::read(), osg::DATImageFileType::read(), osg::ImageFileType::restore(), osg::ClusterViewBuffer::setImgTransType(), osg::ImageFileType::store(), and osg::Image::write().

00505 { 
00506   return *_the; 
00507 }

bool ImageFileHandler::addImageFileType ImageFileType fileType  )  [static, private]
 

Internal Method to add a new ImageFileType

Definition at line 437 of file OSGImageFileHandler.cpp.

References _suffixTypeMap, _the, osg::ImageFileType::getSuffixList(), osg::IDString::set(), SWARNING, and osg::IDString::toLower().

Referenced by osg::ImageFileType::ImageFileType().

00438 {
00439     bool                                           retCode = false;
00440     std::list<IDString                 >::const_iterator sI;
00441     std::map <IDString, ImageFileType *>::iterator smI;
00442     IDString                                       suffix;
00443 
00444     if(!_the)
00445         _the = new ImageFileHandler;
00446 
00447     for( sI = fileType.getSuffixList().begin(); 
00448          sI != fileType.getSuffixList().end();
00449         ++sI)
00450     {
00451         suffix.set(sI->str());
00452         suffix.toLower();
00453         smI = _the->_suffixTypeMap.find(suffix);
00454         if(smI != _the->_suffixTypeMap.end())
00455         {
00456             SWARNING << "Can't add an image file type with suffix "
00457                      << suffix << " a second time" << std::endl;
00458         }
00459         else
00460         {
00461             _the->_suffixTypeMap[suffix] = &fileType;
00462             retCode = true;
00463         }
00464     }
00465 
00466     return retCode;
00467 }


Friends And Related Function Documentation

friend class ImageFileType [friend]
 

Definition at line 64 of file OSGImageFileHandler.h.


Member Data Documentation

ImageFileHandler * ImageFileHandler::_the = 0 [static, private]
 

Definition at line 76 of file OSGImageFileHandler.cpp.

Referenced by addImageFileType(), and the().

std::map<IDString, ImageFileType *> osg::ImageFileHandler::_suffixTypeMap [private]
 

Definition at line 147 of file OSGImageFileHandler.h.

Referenced by addImageFileType(), dump(), getDefaultType(), getFileType(), and getSuffixList().

PathHandler* osg::ImageFileHandler::_pPathHandler [private]
 

Definition at line 151 of file OSGImageFileHandler.h.

Referenced by getPathHandler(), ImageFileHandler(), read(), and setPathHandler().

const std::string ImageFileHandler::_fileNameKey [static, private]
 

Referenced by read(), and write().

const std::string ImageFileHandler::_fullFilePathKey [static, private]
 

Referenced by read().


The documentation for this class was generated from the following files:
Generated on Thu Aug 25 04:14:21 2005 for OpenSG by  doxygen 1.4.3