bool ImageFileHandlerBase::write(Image const *pImage, Char8 const *fileName, Char8 const *mimeType) { bool retCode = false; ImageFileType *type; const std::string *fNAttachment; if(!fileName && (fNAttachment = pImage->findAttachmentField(_fileNameKey))) { fileName = fNAttachment->c_str(); } if((type = getFileType(mimeType, fileName))) { SINFO << "try to write " << fileName << " as " << type->getMimeType() << std::endl; retCode = type->write(pImage, fileName); } else { SWARNING << "can't write " << fileName << "; unknown image format" << std::endl; } return retCode; }
/*! Tries to store the raster data to the given mem block. Will include a ImageFileType::Head description and the data encoded as 'mimeType' */ UInt64 ImageFileType::store(const ImagePtr &image, const char *mimeType, UChar8 *buffer, Int32 memSize) { ImageFileType *type = ImageFileHandler::the().getFileType(mimeType); return type ? type->store(image, buffer, memSize) : 0; }
bool ImageFileHandlerBase::write(Image const *pImage, std::ostream &os, std::string const &mimeType) { ImageFileType *type = getFileType(mimeType.c_str()); return type == 0 ? false : type->write(pImage, os, mimeType); }
bool ImageFileHandlerBase::read( Image *pImage, std::istream &is, const std::string &mimeType) { ImageFileType *type = getFileType(mimeType.c_str()); return type == 0 ? false : type->read(pImage, is, mimeType); }
PixelBufferPtr ImageFile::load(const IODevicePtr &file, const std::string &type, bool srgb) { SetupDisplay::start(); auto &types = *SetupDisplay::get_image_provider_factory_types(); if (types.find(type) == types.end()) throw Exception("Unknown image provider type " + type); ImageFileType *factory = types[type]; return factory->load(file, srgb); }
void ImageFile::save(PixelBufferPtr buffer, const IODevicePtr &file, const std::string &type) { SetupDisplay::start(); auto &types = *SetupDisplay::get_image_provider_factory_types(); if (types.find(type) == types.end()) throw Exception("Unknown image provider type " + type); ImageFileType *factory = types[type]; factory->save(buffer, file); }
UInt64 ImageFileHandlerBase::store(Image const *pImage, Char8 const *mimeType, UChar8 *buffer, Int32 memSize ) { ImageFileType *type; type = mimeType ? getFileType(mimeType) : getDefaultType(); return type->store(pImage, buffer, memSize); }
bool ImageFileHandlerBase::addImageFileType(ImageFileType &fileType) { bool retCode = false; std::list<std::string >::const_iterator sI; std::map <std::string, ImageFileType *>::iterator smI; std::string suffix; for( sI = fileType.getSuffixList().begin(); sI != fileType.getSuffixList().end(); ++sI) { suffix.assign(sI->c_str()); normalizeSuffix(suffix); smI = _suffixTypeMap.find(suffix); if(smI != _suffixTypeMap.end()) { SWARNING << "Can't add an image file type with suffix " << suffix << " a second time" << std::endl; } else { _suffixTypeMap[suffix] = &fileType; retCode = true; } } std::string mimetype = fileType.getMimeType(); normalizeMimetype(mimetype); TypeMap::iterator tIt = _mimeTypeMap.find(mimetype); if(tIt != _mimeTypeMap.end()) { SWARNING << "Can't add an image file type with mimetype " << mimetype << " a second time" << std::endl; } else { _mimeTypeMap[mimetype] = &fileType; } return retCode; }
/*! Removes the option \a name from the ImageFileType that handles files with the given \a suffix. If the option is not present \c false is returned, \c true otherwise. \param[in] suffix File extension to choose the image file type this option applies to. \param[in] name Name of the option. \return Whether the option was successfully removed. */ bool ImageFileHandlerBase::unsetOption( const std::string &suffix, const std::string &name ) { bool retVal = false; ImageFileType *type = getFileType(suffix.c_str()); if(type != NULL) { retVal = type->unsetOption(name); } return retVal; }
PixelBufferPtr ImageFile::load(const std::string &filename, const std::string &type, bool srgb) { SetupDisplay::start(); auto &types = *SetupDisplay::get_image_provider_factory_types(); if (type != "") { if (types.find(type) == types.end()) throw Exception("Unknown image provider type " + type); ImageFileType *factory = types[type]; return factory->load(filename, srgb); } // Determine file extension and use it to lookup type. std::string ext = FilePath::extension(filename); ext = Text::to_lower(ext); if (types.find(ext) == types.end()) throw Exception(std::string("Unknown image provider type ") + ext); ImageFileType *factory = types[ext]; return factory->load(filename, srgb); }
ImageFileHandlerBase::ImageBlockAccessorPtr ImageFileHandlerBase::open(const Char8 *fileName, const Char8 *mimeType) { ImageBlockAccessorPtr returnValue; std::string fullFilePath; if(_pPathHandler != NULL) { fullFilePath = _pPathHandler->findFile(fileName); } else { fullFilePath = fileName; } if(fullFilePath.empty()) { SWARNING << "couldn't find image file " << fileName << std::endl; return returnValue; } ImageFileType *type = getFileType(mimeType, fullFilePath.c_str(), true); if(type != NULL) { FDEBUG(("try to image read %s as %s\n", fullFilePath.c_str(), type->getMimeType())); returnValue = type->open(fullFilePath.c_str()); } else { SWARNING << "could not read " << fullFilePath << "; unknown image format" << std::endl; } return returnValue; }
Int32 ImageFileHandlerBase::getSuffixList(std::list<const Char8 *> &suffixList, UInt32 flags ) { Int32 count = 0; std::map<std::string, ImageFileType *>::iterator sI; suffixList.clear(); for(sI = _suffixTypeMap.begin(); sI != _suffixTypeMap.end(); ++sI) { ImageFileType *type = sI->second; if(type->getFlags() & flags) { suffixList.push_back(sI->first.c_str()); count++; } } return count; }
UChar8 *ImageFileHandlerBase::store(Image const *pImage, UInt64 &memSize, Char8 const *mimeType) { ImageFileType *type = 0; UChar8 *mem = 0; type = mimeType ? getFileType(mimeType) : getDefaultType(); memSize = type->maxBufferSize(pImage); if(memSize) { mem = new UChar8[size_t(memSize)]; memSize = type->store(pImage, mem, Int32(memSize)); } else { FFATAL(("Can not store the image as %s\n", type->getMimeType())); } return mem; }
/*! Tries to restore the Imagedata from the given memblock. The buffer must include a ImageFileType::Head data block. */ UInt64 ImageFileType::restore( ImagePtr &image, const UChar8 *buffer, Int32 memSize) { unsigned long imageSize, headSize = sizeof(Head); unsigned long size = 0, attachmentSize; Head head; const UChar8 *data = buffer ? (buffer + headSize) : 0; ImageFileType *type; std::string mimeType; Image::Type dataType; if ((image != osg::NullFC) && buffer && (memSize >= headSize)) { // Copy header. Otherwise netToHost would change the original // data structur. memcpy(&head,buffer,sizeof(Head)); head.netToHost(); mimeType = ImageFileHandler::the().determineMimetypeFromSuffix(head.suffix); if((type = ImageFileHandler::the().getFileType(mimeType.c_str(), 0))) { if (head.dataType) dataType = Image::Type(head.dataType); else dataType = Image::OSG_UINT8_IMAGEDATA; image->set(Image::PixelFormat(head.pixelFormat), head.width, head.height, head.depth, head.mipmapCount, head.frameCount, float(head.frameDelay) / 1000.0, 0, dataType,true,head.sideCount ); imageSize = static_cast<unsigned long>( type->restoreData(image, data, memSize - headSize)); attachmentSize = 0; // head->attachmentSize; /* if ((attachmentSize = head->attachmentSize)) { attData = (char*)(buffer + headSize + imageSize); attKey = attData; attValue = 0; for (i = 0; i < (attachmentSize-1); i++) { if (attData[i] == 0) if (attKey) { attValue = &(attData[i+1]); image->setAttachmentField (attKey,attValue); attKey = attValue = 0; } else attKey = &(attData[i+1]); } if (attKey || attValue) { FFATAL (("Attachment restore error\n")); } } */ size = headSize + imageSize + attachmentSize; FDEBUG (( "Restore image data: %lu (%lu/%lu/%lu)\n", size, headSize, imageSize, attachmentSize )); } else { imageSize = 0; FWARNING(("Can not restore image data, invalid mimeType: %s\n", mimeType.empty() == false ? mimeType.c_str() : "Unknown")); } } return size; }
bool ImageFileHandlerBase::read( Image *pImage, const Char8 *fileName, const Char8 *mimeType) { bool retCode = false; std::string fullFilePath; if(_pPathHandler != NULL) { fullFilePath = _pPathHandler->findFile(fileName); } else { fullFilePath = fileName; } if(fullFilePath.empty()) { SWARNING << "couldn't find image file " << fileName << std::endl; return false; } ImageFileType *type = getFileType(mimeType, fullFilePath.c_str(), true); if(type != NULL) { FDEBUG(("try to image read %s as %s\n", fullFilePath.c_str(), type->getMimeType())); retCode = type->read(pImage, fullFilePath.c_str()); if(retCode) { FDEBUG(("image: %dx%d\n", pImage->getWidth(), pImage->getHeight())); pImage->setAttachmentField(_fileNameKey, fileName); pImage->setAttachmentField(_fullFilePathKey, fullFilePath); FilePathAttachment::setFilePath(pImage, BoostPath(fullFilePath)); // converting the path to a absolute path. std::string abspath; if(fullFilePath[0] != '/' && fullFilePath[0] != '\\' && fullFilePath[1] != ':') { std::string base; if(getPathHandler() != NULL) base = getPathHandler()->getBaseFile(); if(base.size() < 2 || (base[0] != '/' && base[0] != '\\' && base[1] != ':')) { const Char8 *cdir = Directory::getCurrent(); abspath = cdir; #ifdef WIN32 abspath += '\\'; #else abspath += '/'; #endif delete [] cdir; } abspath += base; abspath += fullFilePath; } else { abspath = fullFilePath; } pImage->setName(abspath); } else { SWARNING << "could not read " << fullFilePath << std::endl; } } else { SWARNING << "could not read " << fullFilePath << "; unknown image format" << std::endl; } return retCode; }
ImageFileType *ImageFileHandlerBase::getFileType(const Char8 *mimeType, const Char8 *fileName, bool validateHeader) { std::string suffix; ImageFileType *type = 0; std::map<std::string, ImageFileType *>::iterator sI; const char separator = '.'; int i, l; if(mimeType && *mimeType) { std::string mt = mimeType; normalizeMimetype(mt); if(mt.find('/') == std::string::npos) mt.insert(0, "image/"); TypeMap::iterator tIt = _mimeTypeMap.find(mt); if(tIt != _mimeTypeMap.end()) type = tIt->second; if(!type) { FWARNING (("Invalid mimeType %s in getFileType()\n", mimeType)); } } if(!type && fileName && *fileName) { // check file suffix if(!type) { l = strlen(fileName); for(i = l - 1; i >= 0; i--) { if(fileName[i] == separator) break; } if(i >= 0) { suffix.assign(&(fileName[i + 1])); normalizeSuffix(suffix); sI = _suffixTypeMap.find(suffix); type = (sI == _suffixTypeMap.end()) ? 0 : sI->second; } } } if(validateHeader) { // now validate the header of the file bool implemented = false; if( fileName && *fileName && type != NULL && !type->validateHeader(fileName, implemented)) { FWARNING (("Found wrong image header trying to " "autodetect image type!\n")); for(sI = _suffixTypeMap.begin(); sI != _suffixTypeMap.end(); ++sI) { type = sI->second; if(type != NULL && type->validateHeader(fileName, implemented)) { if(implemented) { FWARNING (("Autodetected '%s' image type!\n", sI->first.c_str())); return type; } } } FWARNING (("Couldn't autodetect image type!\n")); return NULL; } } return type; }