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; }
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; }
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; }
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; }
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; }